xref: /llvm-project/compiler-rt/test/msan/strlen_of_shadow.cpp (revision 310a6f12b5b80f6b77d8551c53e0fc2a2844df07)
1 // RUN: %clangxx_msan -O0 %s -o %t && %run %t
2 
3 // Check that strlen() and similar intercepted functions can be called on shadow
4 // memory.
5 // The mem_to_shadow's part might need rework
6 // XFAIL: target={{.*freebsd.*}}
7 
8 #include <assert.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include "test.h"
14 
mem_to_shadow(const char * p)15 const char *mem_to_shadow(const char *p) {
16 #if defined(__x86_64__)
17   return (char *)((uintptr_t)p ^ 0x500000000000ULL);
18 #elif defined(__loongarch_lp64)
19   return (char *)((uintptr_t)p ^ 0x500000000000ULL);
20 #elif defined (__mips64)
21   return (char *)((uintptr_t)p ^ 0x8000000000ULL);
22 #elif defined(__powerpc64__)
23 #define LINEARIZE_MEM(mem) \
24   (((uintptr_t)(mem) & ~0x200000000000ULL) ^ 0x100000000000ULL)
25   return (char *)(LINEARIZE_MEM(p) + 0x080000000000ULL);
26 #elif defined(__s390x__)
27   return (char *)(((uintptr_t)p & ~0xC00000000000ULL) + 0x080000000000ULL);
28 #elif defined(__aarch64__)
29   return (char *)((uintptr_t)p ^ 0xB00000000000ULL);
30 #endif
31 }
32 
main(void)33 int main(void) {
34   const char *s = "abcdef";
35   assert(strlen(s) == 6);
36   assert(strlen(mem_to_shadow(s)) == 0);
37 
38   char *t = new char[42];
39   t[41] = 0;
40   assert(strlen(mem_to_shadow(t)) == 41);
41   return 0;
42 }
43