1// RUN: %clangxx -fsanitize=realtime %s -o %t
2// RUN: not %run %t 2>&1 | FileCheck %s
3// RUN: %clangxx %s -fsanitize=realtime -o - -S -emit-llvm | FileCheck %s --check-prefix=CHECK-ENABLED-IR
4// RUN: %clangxx %s -o - -S -emit-llvm | FileCheck %s --check-prefix=CHECK-DISABLED-IR
5// UNSUPPORTED: ios
6
7#include <stdio.h>
8#include <stdlib.h>
9
10#include "sanitizer/rtsan_interface.h"
11
12void violation() [[clang::nonblocking]] {
13 void *ptr;
14 {
15 __rtsan::ScopedDisabler disabler{};
16 ptr = malloc(size: 2);
17 fprintf(stderr, format: "Allocated pointer %p in disabled context\n", ptr);
18 }
19
20 // ensure nested disablers don't interfere with one another
21 {
22 void *ptr2;
23 __rtsan::ScopedDisabler disabler{};
24 {
25 __rtsan::ScopedDisabler disabler2{};
26 ptr2 = malloc(size: 2);
27 fprintf(stderr, format: "Allocated second pointer %p in disabled context\n",
28 ptr2);
29 }
30
31 free(ptr: ptr2);
32 fprintf(stderr, format: "Free'd second pointer in disabled context\n");
33 }
34
35 free(ptr: ptr);
36}
37
38int main() {
39 violation();
40 return 0;
41 // CHECK: Allocated pointer {{.*}} in disabled context
42 // CHECK: Allocated second pointer {{.*}} in disabled context
43 // CHECK: Free'd second pointer in disabled context
44 // CHECK: ==ERROR: RealtimeSanitizer: unsafe-library-call
45 // CHECK-NOT: {{.*malloc*}}
46 // CHECK-NEXT: {{.*free.*}}
47}
48
49// CHECK-ENABLED-IR: {{.*@__rtsan_disable.*}}
50// CHECK-ENABLED-IR: {{.*@__rtsan_enable.*}}
51
52// CHECK-DISABLED-IR-NOT: {{.*__rtsan_disable.*}}
53// CHECK-DISABLED-IR-NOT: {{.*__rtsan_enable.*}}
54

source code of compiler-rt/test/rtsan/disabler.cpp