xref: /aosp_15_r20/external/compiler-rt/test/msan/strlen_of_shadow.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_msan -O0 %s -o %t && %run %t
2*7c3d14c8STreehugger Robot 
3*7c3d14c8STreehugger Robot // Check that strlen() and similar intercepted functions can be called on shadow
4*7c3d14c8STreehugger Robot // memory.
5*7c3d14c8STreehugger Robot 
6*7c3d14c8STreehugger Robot #include <assert.h>
7*7c3d14c8STreehugger Robot #include <stdint.h>
8*7c3d14c8STreehugger Robot #include <stdio.h>
9*7c3d14c8STreehugger Robot #include <string.h>
10*7c3d14c8STreehugger Robot #include <stdlib.h>
11*7c3d14c8STreehugger Robot #include "test.h"
12*7c3d14c8STreehugger Robot 
mem_to_shadow(const char * p)13*7c3d14c8STreehugger Robot const char *mem_to_shadow(const char *p) {
14*7c3d14c8STreehugger Robot #if defined(__x86_64__)
15*7c3d14c8STreehugger Robot   return (char *)((uintptr_t)p ^ 0x500000000000ULL);
16*7c3d14c8STreehugger Robot #elif defined (__mips64)
17*7c3d14c8STreehugger Robot   return (char *)((uintptr_t)p & ~0x4000000000ULL);
18*7c3d14c8STreehugger Robot #elif defined(__powerpc64__)
19*7c3d14c8STreehugger Robot #define LINEARIZE_MEM(mem) \
20*7c3d14c8STreehugger Robot   (((uintptr_t)(mem) & ~0x200000000000ULL) ^ 0x100000000000ULL)
21*7c3d14c8STreehugger Robot   return (char *)(LINEARIZE_MEM(p) + 0x080000000000ULL);
22*7c3d14c8STreehugger Robot #elif defined(__aarch64__)
23*7c3d14c8STreehugger Robot   return (char *)((uintptr_t)p ^ 0x6000000000ULL);
24*7c3d14c8STreehugger Robot #endif
25*7c3d14c8STreehugger Robot }
26*7c3d14c8STreehugger Robot 
main(void)27*7c3d14c8STreehugger Robot int main(void) {
28*7c3d14c8STreehugger Robot   const char *s = "abcdef";
29*7c3d14c8STreehugger Robot   assert(strlen(s) == 6);
30*7c3d14c8STreehugger Robot   assert(strlen(mem_to_shadow(s)) == 0);
31*7c3d14c8STreehugger Robot 
32*7c3d14c8STreehugger Robot   char *t = new char[42];
33*7c3d14c8STreehugger Robot   t[41] = 0;
34*7c3d14c8STreehugger Robot   assert(strlen(mem_to_shadow(t)) == 41);
35*7c3d14c8STreehugger Robot   return 0;
36*7c3d14c8STreehugger Robot }
37