File: no-fd.cpp

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (54 lines) | stat: -rw-r--r-- 1,531 bytes parent folder | download | duplicates (21)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// RUN: %clangxx_asan -std=c++11 -O0 %s -o %t

// MallocNanoZone=0 disables initialization of the Nano MallocZone on Darwin.
// Initialization of this zone can interfere with this test because the zone
// might log which opens another file descriptor,
// e.g. failing to setup the zone due to ASan taking the memory region it wants.
// RUN: env MallocNanoZone=0 %run %t 2>&1 | FileCheck %s
// RUN: env MallocNanoZone=0 %env_asan_opts=debug=1,verbosity=2 %run %t 2>&1 | FileCheck %s

// Test ASan initialization

// This test closes the 0, 1, and 2 file descriptors before an exec() and relies
// on them remaining closed across an execve(). This is not the case on newer
// versions of Android. On PPC with ASLR turned on, this fails when linked with
// lld - see https://bugs.llvm.org/show_bug.cgi?id=45076.
// UNSUPPORTED: android, target=powerpc{{.*}}

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

extern "C" const char *__asan_default_options() {
  return "test_only_emulate_no_memorymap=1";
}

void parent(int argc, char **argv) {
  fprintf(stderr, "hello\n");
  // CHECK: hello
  close(0);
  close(1);
  dup2(2, 3);
  close(2);
  char *const newargv[] = {argv[0], (char *)"x", nullptr};
  execv(argv[0], newargv);
  perror("execve");
  exit(1);
}

void child() {
  assert(dup(3) == 0);
  assert(dup(3) == 1);
  assert(dup(3) == 2);
  fprintf(stderr, "world\n");
  // CHECK: world
}

int main(int argc, char **argv) {
  if (argc == 1) {
    parent(argc, argv);
  } else {
    child();
  }
}