1 // RUN: %clangxx_asan -std=c++11 -O0 %s -o %t
2 // RUN: %run %t 2>&1 | FileCheck %s
3 // RUN: %env_asan_opts=debug=1,verbosity=2 %run %t 2>&1 | FileCheck %s
4 
5 // Test ASan initialization
6 
7 // This test closes the 0, 1, and 2 file descriptors before an exec() and relies
8 // on them remaining closed across an execve(). This is not the case on newer
9 // versions of Android. On PPC with ASLR turned on, this fails when linked with
10 // lld - see https://bugs.llvm.org/show_bug.cgi?id=45076.
11 // UNSUPPORTED: android, powerpc
12 
13 #include <assert.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 
__asan_default_options()18 extern "C" const char *__asan_default_options() {
19   return "test_only_emulate_no_memorymap=1";
20 }
21 
parent(int argc,char ** argv)22 void parent(int argc, char **argv) {
23   fprintf(stderr, "hello\n");
24   // CHECK: hello
25   close(0);
26   close(1);
27   dup2(2, 3);
28   close(2);
29   char *const newargv[] = {argv[0], (char *)"x", nullptr};
30   execv(argv[0], newargv);
31   perror("execve");
32   exit(1);
33 }
34 
child()35 void child() {
36   assert(dup(3) == 0);
37   assert(dup(3) == 1);
38   assert(dup(3) == 2);
39   fprintf(stderr, "world\n");
40   // CHECK: world
41 }
42 
main(int argc,char ** argv)43 int main(int argc, char **argv) {
44   if (argc == 1) {
45     parent(argc, argv);
46   } else {
47     child();
48   }
49 }
50