]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/hwasan/TestCases/Posix/system-allocator-fallback.cc
Vendor import of compiler-rt trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / hwasan / TestCases / Posix / system-allocator-fallback.cc
1 // RUN: %clangxx %s -o %t -ldl
2 // RUN: %clangxx_hwasan -shared %s -o %t.so -DSHARED_LIB -shared-libsan -Wl,-rpath,%compiler_rt_libdir
3 // RUN: %env_hwasan_opts=disable_allocator_tagging=0 %run %t
4
5 // The dynamic loader on Android O appears to have a bug where it crashes when
6 // dlopening DF_1_GLOBAL libraries.
7 // REQUIRES: android-28
8
9 #include <stddef.h>
10
11 // Test that allocations made by the system allocator can be realloc'd and freed
12 // by the hwasan allocator.
13
14 typedef void run_test_fn(void *(*system_malloc)(size_t size));
15
16 #ifdef SHARED_LIB
17
18 // Call the __sanitizer_ versions of these functions so that the test
19 // doesn't require the Android dynamic loader.
20 extern "C" void *__sanitizer_realloc(void *ptr, size_t size);
21 extern "C" void __sanitizer_free(void *ptr);
22
23 extern "C" run_test_fn run_test;
24 void run_test(void *(*system_malloc)(size_t size)) {
25   void *mem = system_malloc(64);
26   mem = __sanitizer_realloc(mem, 128);
27   __sanitizer_free(mem);
28 }
29
30 #else
31
32 #include <dlfcn.h>
33 #include <stdlib.h>
34 #include <string>
35
36 int main(int argc, char **argv) {
37   std::string path = argv[0];
38   path += ".so";
39   void *lib = dlopen(path.c_str(), RTLD_NOW);
40   if (!lib) {
41     printf("error in dlopen(): %s\n", dlerror());
42     return 1;
43   }
44
45   auto run_test = reinterpret_cast<run_test_fn *>(dlsym(lib, "run_test"));
46   if (!run_test) {
47     printf("failed dlsym\n");
48     return 1;
49   }
50
51   run_test(malloc);
52 }
53
54 #endif