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 // Deflake this test before reinabling for darwin (rdar://74992832)
14 // UNSUPPORTED: darwin
15 
16 #include <assert.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 
21 extern "C" const char *__asan_default_options() {
22   return "test_only_emulate_no_memorymap=1";
23 }
24 
25 void parent(int argc, char **argv) {
26   fprintf(stderr, "hello\n");
27   // CHECK: hello
28   close(0);
29   close(1);
30   dup2(2, 3);
31   close(2);
32   char *const newargv[] = {argv[0], (char *)"x", nullptr};
33   execv(argv[0], newargv);
34   perror("execve");
35   exit(1);
36 }
37 
38 void child() {
39   assert(dup(3) == 0);
40   assert(dup(3) == 1);
41   assert(dup(3) == 2);
42   fprintf(stderr, "world\n");
43   // CHECK: world
44 }
45 
46 int main(int argc, char **argv) {
47   if (argc == 1) {
48     parent(argc, argv);
49   } else {
50     child();
51   }
52 }
53