• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: 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 (__mips64)
19   return (char *)((uintptr_t)p ^ 0x8000000000ULL);
20 #elif defined(__powerpc64__)
21 #define LINEARIZE_MEM(mem) \
22   (((uintptr_t)(mem) & ~0x200000000000ULL) ^ 0x100000000000ULL)
23   return (char *)(LINEARIZE_MEM(p) + 0x080000000000ULL);
24 #elif defined(__s390x__)
25   return (char *)(((uintptr_t)p & ~0xC00000000000ULL) + 0x080000000000ULL);
26 #elif defined(__aarch64__)
27   return (char *)((uintptr_t)p ^ 0x6000000000ULL);
28 #endif
29 }
30 
main(void)31 int main(void) {
32   const char *s = "abcdef";
33   assert(strlen(s) == 6);
34   assert(strlen(mem_to_shadow(s)) == 0);
35 
36   char *t = new char[42];
37   t[41] = 0;
38   assert(strlen(mem_to_shadow(t)) == 41);
39   return 0;
40 }
41