File: fork_threaded.c

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (109 lines) | stat: -rw-r--r-- 2,706 bytes parent folder | download | duplicates (7)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// RUN: %clang -O0 %s -o %t && %env_tool_opts=die_after_fork=0 %run %t

// The test uses pthread barriers which are not available on Darwin.
// UNSUPPORTED: darwin

// FIXME: It probably hangs on this platform.
// UNSUPPORTED: ppc

// FIXME: False stack overflow report
// UNSUPPORTED: android && asan

// FIXME: Requires `FutexWait` implementation. See __asan::InstallAtForkHandler.
// UNSUPPORTED: target={{.*solaris.*}}
// UNSUPPORTED: target={{.*netbsd.*}}
// UNSUPPORTED: target={{.*apple.*}}

// Forking in multithread environment is unsupported. However we already have
// some workarounds, and will add more, so this is the test.
// The test try to check two things:
//  1. Internal mutexes used by `inparent` thread do not deadlock `inchild`
//     thread.
//  2. Stack poisoned by `inparent` is not poisoned in `inchild` thread.

#include <assert.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "sanitizer_common/sanitizer_specific.h"

static const size_t kBufferSize = 8192;

pthread_barrier_t bar;

// Without appropriate workarounds this code can cause the forked process to
// start with locked internal mutexes.
void ShouldNotDeadlock() {
  // Don't bother with leaks, we try to trigger allocator or lsan deadlock.
  __lsan_disable();
  void *volatile p = malloc(10);
  __lsan_do_recoverable_leak_check();
  // Allocator still in broken state, `free` may report errors.
  // free(p);
  __lsan_enable();
}

// Prevent stack buffer cleanup by instrumentation.
#define NOSAN __attribute__((no_sanitize("address", "hwaddress", "memory")))

NOSAN static void *inparent(void *arg) {
  char t[kBufferSize];
  make_mem_bad(t, sizeof(t));

  pthread_barrier_wait(&bar);

  for (;;)
    ShouldNotDeadlock();

  return 0;
}

NOSAN static void *inchild(void *arg) {
  char t[kBufferSize];
  check_mem_is_good(t, sizeof(t));
  ShouldNotDeadlock();
  return 0;
}

int main(void) {
#if __has_feature(hwaddress_sanitizer)
  __hwasan_enable_allocator_tagging();
#endif

  pid_t pid;

  pthread_barrier_init(&bar, NULL, 2);
  pthread_t thread_id;
  while (pthread_create(&thread_id, 0, &inparent, 0) != 0) {
  }
  pthread_barrier_wait(&bar);

  pid = fork();
  switch (pid) {
  case -1:
    perror("fork");
    return -1;
  case 0:
    ShouldNotDeadlock();
    while (pthread_create(&thread_id, 0, &inchild, 0) != 0) {
    }
    pthread_join(thread_id, NULL);
    _exit(0);
    break;
  default: {
    int status;
    pid_t child = waitpid(pid, &status, /*options=*/0);
    assert(pid == child);
    assert(WIFEXITED(status));
    assert(WEXITSTATUS(status) == 0);
    break;
  }
  }

  return 0;
}