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

source code of compiler-rt/test/asan/TestCases/Posix/no-fd.cpp