sanitizers 0.0.11

Interfaces and FFI bindings for the sanitizers interfaces
Documentation
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/// ThreadSanitizer interface.
///
/// For more information about ThreadSanitizer, see
/// https://clang.llvm.org/docs/ThreadSanitizer.html.
use crate::ffi::tsan::*;

use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int, c_uint, c_ulong, c_void};

/// Struct to hold general report data.
pub struct TsanReportData {
    pub description: String,
    pub count: c_int,
    pub stack_count: c_int,
    pub mop_count: c_int,
    pub loc_count: c_int,
    pub mutex_count: c_int,
    pub thread_count: c_int,
    pub unique_tid_count: c_int,
    pub sleep_trace: Vec<*mut c_void>,
}

/// Struct to hold memory operation report data.
pub struct TsanReportMop {
    pub tid: c_int,
    pub addr: *mut c_void,
    pub size: c_int,
    pub write: c_int,
    pub atomic: c_int,
    pub trace: Vec<*mut c_void>,
}

/// Struct to hold location report data.
pub struct TsanReportLoc {
    pub type_: String,
    pub addr: *mut c_void,
    pub start: *mut c_void,
    pub size: c_ulong,
    pub tid: c_int,
    pub fd: c_int,
    pub suppressable: c_int,
    pub trace: Vec<*mut c_void>,
}

/// Struct to hold mutex report data.
pub struct TsanReportMutex {
    pub mutex_id: u64,
    pub addr: *mut c_void,
    pub destroyed: c_int,
    pub trace: Vec<*mut c_void>,
}

/// Struct to hold thread report data.
pub struct TsanReportThread {
    pub tid: c_int,
    pub os_id: u64,
    pub running: c_int,
    pub name: String,
    pub parent_tid: c_int,
    pub trace: Vec<*mut c_void>,
}

/// Establishes a happens-before relation with a preceding acquire on the same
/// address.
pub fn acquire(addr: *mut c_void) {
    unsafe {
        __tsan_acquire(addr);
    }
}

/// Establishes a happens-before relation with a subsequent release on the same
/// address.
pub fn release(addr: *mut c_void) {
    unsafe {
        __tsan_release(addr);
    }
}

/// Annotate creation of a mutex.
pub fn mutex_create(addr: *mut c_void, flags: c_uint) {
    unsafe {
        __tsan_mutex_create(addr, flags);
    }
}

/// Annotate destruction of a mutex.
pub fn mutex_destroy(addr: *mut c_void, flags: c_uint) {
    unsafe {
        __tsan_mutex_destroy(addr, flags);
    }
}

/// Annotate start of lock operation.
pub fn mutex_pre_lock(addr: *mut c_void, flags: c_uint) {
    unsafe {
        __tsan_mutex_pre_lock(addr, flags);
    }
}

/// Annotate end of lock operation.
pub fn mutex_post_lock(addr: *mut c_void, flags: c_uint, recursion: c_int) {
    unsafe {
        __tsan_mutex_post_lock(addr, flags, recursion);
    }
}

/// Annotate start of unlock operation.
pub fn mutex_pre_unlock(addr: *mut c_void, flags: c_uint) -> c_int {
    unsafe { __tsan_mutex_pre_unlock(addr, flags) }
}

/// Annotate end of unlock operation.
pub fn mutex_post_unlock(addr: *mut c_void, flags: c_uint) {
    unsafe {
        __tsan_mutex_post_unlock(addr, flags);
    }
}

/// Annotate start of notify operation.
pub fn mutex_pre_signal(addr: *mut c_void, flags: c_uint) {
    unsafe {
        __tsan_mutex_pre_signal(addr, flags);
    }
}

/// Annotate end of notify operation.
pub fn mutex_post_signal(addr: *mut c_void, flags: c_uint) {
    unsafe {
        __tsan_mutex_post_signal(addr, flags);
    }
}

/// Annotate a region of code where lock/unlock/signal operation diverts to do
/// something else unrelated to the mutex.
pub fn mutex_pre_divert(addr: *mut c_void, flags: c_uint) {
    unsafe {
        __tsan_mutex_pre_divert(addr, flags);
    }
}

/// Annotate end of a region of code where lock/unlock/signal operation diverts
/// to do something else unrelated to the mutex.
pub fn mutex_post_divert(addr: *mut c_void, flags: c_uint) {
    unsafe {
        __tsan_mutex_post_divert(addr, flags);
    }
}

/// Check that the current thread does not hold any mutexes, report a bug
/// otherwise.
pub fn check_no_mutexes_held() {
    unsafe {
        __tsan_check_no_mutexes_held();
    }
}

/// Registers a tag with the specified name.
pub fn external_register_tag(object_type: &str) -> *mut c_void {
    let object_type_cstr = CString::new(object_type).unwrap();
    unsafe { __tsan_external_register_tag(object_type_cstr.as_ptr()) }
}

/// Registers a header for an external tag.
pub fn external_register_header(tag: *mut c_void, header: &str) {
    let header_cstr = CString::new(header).unwrap();
    unsafe {
        __tsan_external_register_header(tag, header_cstr.as_ptr());
    }
}

/// Assigns a tag to a heap object.
pub fn external_assign_tag(addr: *mut c_void, tag: *mut c_void) {
    unsafe {
        __tsan_external_assign_tag(addr, tag);
    }
}

/// Annotates a logical read of the object at the specified address.
pub fn external_read(addr: *mut c_void, caller_pc: *mut c_void, tag: *mut c_void) {
    unsafe {
        __tsan_external_read(addr, caller_pc, tag);
    }
}

/// Annotates a logical write of the object at the specified address.
pub fn external_write(addr: *mut c_void, caller_pc: *mut c_void, tag: *mut c_void) {
    unsafe {
        __tsan_external_write(addr, caller_pc, tag);
    }
}

/// Creates a fiber.
pub fn create_fiber(flags: c_uint) -> *mut c_void {
    unsafe { __tsan_create_fiber(flags) }
}

/// Destroys a fiber.
pub fn destroy_fiber(fiber: *mut c_void) {
    unsafe {
        __tsan_destroy_fiber(fiber);
    }
}

/// Switches to a fiber.
pub fn switch_to_fiber(fiber: *mut c_void, flags: c_uint) {
    unsafe {
        __tsan_switch_to_fiber(fiber, flags);
    }
}

/// Sets a fiber name.
pub fn set_fiber_name(fiber: *mut c_void, name: &str) {
    let name_cstr = CString::new(name).unwrap();
    unsafe {
        __tsan_set_fiber_name(fiber, name_cstr.as_ptr());
    }
}

/// User-provided callback invoked on TSan initialization.
pub fn on_initialize() {
    unsafe {
        __tsan_on_initialize();
    }
}

/// User-provided callback invoked on TSan shutdown.
pub fn on_finalize(failed: c_int) -> c_int {
    unsafe { __tsan_on_finalize(failed) }
}

/// Release TSan internal memory in a best-effort manner.
pub fn flush_memory() {
    unsafe {
        __tsan_flush_memory();
    }
}

/// Returns a report's description.
pub fn get_report_data(report: *mut c_void) -> TsanReportData {
    unsafe {
        let mut description: *const c_char = std::ptr::null();
        let mut count: c_int = 0;
        let mut stack_count: c_int = 0;
        let mut mop_count: c_int = 0;
        let mut loc_count: c_int = 0;
        let mut mutex_count: c_int = 0;
        let mut thread_count: c_int = 0;
        let mut unique_tid_count: c_int = 0;
        let mut sleep_trace: Vec<*mut c_void> = vec![std::ptr::null_mut(); 1024];

        __tsan_get_report_data(
            report,
            &mut description,
            &mut count,
            &mut stack_count,
            &mut mop_count,
            &mut loc_count,
            &mut mutex_count,
            &mut thread_count,
            &mut unique_tid_count,
            sleep_trace.as_mut_ptr(),
            1024,
        );

        TsanReportData {
            description: if description.is_null() {
                String::new()
            } else {
                CStr::from_ptr(description).to_string_lossy().into_owned()
            },
            count,
            stack_count,
            mop_count,
            loc_count,
            mutex_count,
            thread_count,
            unique_tid_count,
            sleep_trace,
        }
    }
}

/// Returns information about stack traces included in the report.
pub fn get_report_stack(report: *mut c_void, idx: c_ulong) -> Vec<*mut c_void> {
    unsafe {
        let mut trace: Vec<*mut c_void> = vec![std::ptr::null_mut(); 1024];
        __tsan_get_report_stack(report, idx, trace.as_mut_ptr(), 1024);
        trace
    }
}

/// Returns information about memory operations included in the report.
pub fn get_report_mop(report: *mut c_void, idx: c_ulong) -> TsanReportMop {
    unsafe {
        let mut tid: c_int = 0;
        let mut addr: *mut c_void = std::ptr::null_mut();
        let mut size: c_int = 0;
        let mut write: c_int = 0;
        let mut atomic: c_int = 0;
        let mut trace: Vec<*mut c_void> = vec![std::ptr::null_mut(); 1024];

        __tsan_get_report_mop(
            report,
            idx,
            &mut tid,
            &mut addr,
            &mut size,
            &mut write,
            &mut atomic,
            trace.as_mut_ptr(),
            1024,
        );

        TsanReportMop {
            tid,
            addr,
            size,
            write,
            atomic,
            trace,
        }
    }
}

/// Returns information about locations included in the report.
pub fn get_report_loc(report: *mut c_void, idx: c_ulong) -> TsanReportLoc {
    unsafe {
        let mut type_: *const c_char = std::ptr::null();
        let mut addr: *mut c_void = std::ptr::null_mut();
        let mut start: *mut c_void = std::ptr::null_mut();
        let mut size: c_ulong = 0;
        let mut tid: c_int = 0;
        let mut fd: c_int = 0;
        let mut suppressable: c_int = 0;
        let mut trace: Vec<*mut c_void> = vec![std::ptr::null_mut(); 1024];

        __tsan_get_report_loc(
            report,
            idx,
            &mut type_,
            &mut addr,
            &mut start,
            &mut size,
            &mut tid,
            &mut fd,
            &mut suppressable,
            trace.as_mut_ptr(),
            1024,
        );

        TsanReportLoc {
            type_: if type_.is_null() {
                String::new()
            } else {
                CStr::from_ptr(type_).to_string_lossy().into_owned()
            },
            addr,
            start,
            size,
            tid,
            fd,
            suppressable,
            trace,
        }
    }
}

/// Returns information about mutexes included in the report.
pub fn get_report_mutex(report: *mut c_void, idx: c_ulong) -> TsanReportMutex {
    unsafe {
        let mut mutex_id: u64 = 0;
        let mut addr: *mut c_void = std::ptr::null_mut();
        let mut destroyed: c_int = 0;
        let mut trace: Vec<*mut c_void> = vec![std::ptr::null_mut(); 1024];

        __tsan_get_report_mutex(
            report,
            idx,
            &mut mutex_id,
            &mut addr,
            &mut destroyed,
            trace.as_mut_ptr(),
            1024,
        );

        TsanReportMutex {
            mutex_id,
            addr,
            destroyed,
            trace,
        }
    }
}

/// Returns information about threads included in the report.
pub fn get_report_thread(report: *mut c_void, idx: c_ulong) -> TsanReportThread {
    unsafe {
        let mut tid: c_int = 0;
        let mut os_id: u64 = 0;
        let mut running: c_int = 0;
        let mut name: *const c_char = std::ptr::null();
        let mut parent_tid: c_int = 0;
        let mut trace: Vec<*mut c_void> = vec![std::ptr::null_mut(); 1024];

        __tsan_get_report_thread(
            report,
            idx,
            &mut tid,
            &mut os_id,
            &mut running,
            &mut name,
            &mut parent_tid,
            trace.as_mut_ptr(),
            1024,
        );

        TsanReportThread {
            tid,
            os_id,
            running,
            name: if name.is_null() {
                String::new()
            } else {
                CStr::from_ptr(name).to_string_lossy().into_owned()
            },
            parent_tid,
            trace,
        }
    }
}

/// Returns information about unique thread IDs included in the report.
pub fn get_report_unique_tid(report: *mut c_void, idx: c_ulong) -> c_int {
    unsafe {
        let mut tid: c_int = 0;
        __tsan_get_report_unique_tid(report, idx, &mut tid);
        tid
    }
}

/// Returns the current report.
pub fn get_current_report() -> *mut c_void {
    unsafe { __tsan_get_current_report() }
}