about summary refs log tree commit diff
path: root/tests/codegen-llvm/sanitizer/sanitize-off.rs
blob: 9f3f7cd9df781cf8b803c7a4462845263f0bc89b (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Verifies that the `#[sanitize(address = "off")]` attribute can be used to
// selectively disable sanitizer instrumentation.
//
//@ needs-sanitizer-address
//@ compile-flags: -Zsanitizer=address -Ctarget-feature=-crt-static -Copt-level=0

#![crate_type = "lib"]
#![feature(sanitize)]

// CHECK:     @UNSANITIZED = constant{{.*}} no_sanitize_address
// CHECK-NOT: @__asan_global_SANITIZED
#[no_mangle]
#[sanitize(address = "off")]
pub static UNSANITIZED: u32 = 0;

// CHECK: @__asan_global_SANITIZED
#[no_mangle]
pub static SANITIZED: u32 = 0;

// CHECK-LABEL: ; sanitize_off::unsanitized
// CHECK-NEXT:  ; Function Attrs:
// CHECK-NOT:   sanitize_address
// CHECK:       start:
// CHECK-NOT:   call void @__asan_report_load
// CHECK:       }
#[sanitize(address = "off")]
pub fn unsanitized(b: &mut u8) -> u8 {
    *b
}

// CHECK-LABEL: ; sanitize_off::sanitized
// CHECK-NEXT:  ; Function Attrs:
// CHECK:       sanitize_address
// CHECK:       start:
// CHECK:       call void @__asan_report_load
// CHECK:       }
pub fn sanitized(b: &mut u8) -> u8 {
    *b
}

#[sanitize(address = "off")]
pub mod foo {
    // CHECK-LABEL: ; sanitize_off::foo::unsanitized
    // CHECK-NEXT:  ; Function Attrs:
    // CHECK-NOT:   sanitize_address
    // CHECK:       start:
    // CHECK-NOT:   call void @__asan_report_load
    // CHECK:       }
    pub fn unsanitized(b: &mut u8) -> u8 {
        *b
    }

    // CHECK-LABEL: ; sanitize_off::foo::sanitized
    // CHECK-NEXT:  ; Function Attrs:
    // CHECK:       sanitize_address
    // CHECK:       start:
    // CHECK:       call void @__asan_report_load
    // CHECK:       }
    #[sanitize(address = "on")]
    pub fn sanitized(b: &mut u8) -> u8 {
        *b
    }
}

pub trait MyTrait {
    fn unsanitized(&self, b: &mut u8) -> u8;
    fn sanitized(&self, b: &mut u8) -> u8;

    // CHECK-LABEL: ; sanitize_off::MyTrait::unsanitized_default
    // CHECK-NEXT:  ; Function Attrs:
    // CHECK-NOT:   sanitize_address
    // CHECK:       start:
    // CHECK-NOT:   call void @__asan_report_load
    // CHECK:       }
    #[sanitize(address = "off")]
    fn unsanitized_default(&self, b: &mut u8) -> u8 {
        *b
    }

    // CHECK-LABEL: ; sanitize_off::MyTrait::sanitized_default
    // CHECK-NEXT:  ; Function Attrs:
    // CHECK:       sanitize_address
    // CHECK:       start:
    // CHECK:       call void @__asan_report_load
    // CHECK:       }
    fn sanitized_default(&self, b: &mut u8) -> u8 {
        *b
    }
}

#[sanitize(address = "off")]
impl MyTrait for () {
    // CHECK-LABEL: ; <() as sanitize_off::MyTrait>::unsanitized
    // CHECK-NEXT:  ; Function Attrs:
    // CHECK-NOT:   sanitize_address
    // CHECK:       start:
    // CHECK-NOT:   call void @__asan_report_load
    // CHECK:       }
    fn unsanitized(&self, b: &mut u8) -> u8 {
        *b
    }

    // CHECK-LABEL: ; <() as sanitize_off::MyTrait>::sanitized
    // CHECK-NEXT:  ; Function Attrs:
    // CHECK:       sanitize_address
    // CHECK:       start:
    // CHECK:       call void @__asan_report_load
    // CHECK:       }
    #[sanitize(address = "on")]
    fn sanitized(&self, b: &mut u8) -> u8 {
        *b
    }
}

pub fn expose_trait(b: &mut u8) -> u8 {
    <() as MyTrait>::unsanitized_default(&(), b);
    <() as MyTrait>::sanitized_default(&(), b)
}

#[sanitize(address = "off")]
pub mod outer {
    #[sanitize(thread = "off")]
    pub mod inner {
        // CHECK-LABEL: ; sanitize_off::outer::inner::unsanitized
        // CHECK-NEXT:  ; Function Attrs:
        // CHECK-NOT:   sanitize_address
        // CHECK:       start:
        // CHECK-NOT:   call void @__asan_report_load
        // CHECK:       }
        pub fn unsanitized() {
            let xs = [0, 1, 2, 3];
            // Avoid optimizing everything out.
            let xs = std::hint::black_box(xs.as_ptr());
            let code = unsafe { *xs.offset(4) };
            std::process::exit(code);
        }
    }
}