Skip to main content

sanitizers/
asan.rs

1/// AddressSanitizer interface.
2///
3/// For more information about AddressSanitizer, see
4/// https://clang.llvm.org/docs/AddressSanitizer.html.
5use crate::ffi::asan::*;
6
7use std::ffi::CStr;
8use std::os::raw::c_void;
9
10/// Marks a memory region as unaddressable.
11pub fn poison_memory_region(addr: *const c_void, size: usize) {
12    unsafe {
13        __asan_poison_memory_region(addr, size);
14    }
15}
16
17/// Marks a memory region as addressable.
18pub fn unpoison_memory_region(addr: *const c_void, size: usize) {
19    unsafe {
20        __asan_unpoison_memory_region(addr, size);
21    }
22}
23
24/// Checks if an address is poisoned.
25pub fn is_address_poisoned(addr: *const c_void) -> bool {
26    unsafe { __asan_address_is_poisoned(addr) != 0 }
27}
28
29/// Checks if a region is poisoned and returns the address of the first poisoned byte.
30pub fn region_is_poisoned(beg: *mut c_void, size: usize) -> Option<*mut c_void> {
31    let addr = unsafe { __asan_region_is_poisoned(beg, size) };
32    if addr.is_null() {
33        None
34    } else {
35        Some(addr)
36    }
37}
38
39/// Describes an address (useful for calling from the debugger).
40pub fn describe_address(addr: *mut c_void) {
41    unsafe {
42        __asan_describe_address(addr);
43    }
44}
45
46/// Checks if an error has been or is being reported.
47pub fn report_present() -> bool {
48    unsafe { __asan_report_present() != 0 }
49}
50
51/// Gets the PC (program counter) register value of an ASan error.
52pub fn get_report_pc() -> *mut c_void {
53    unsafe { __asan_get_report_pc() }
54}
55
56/// Gets the BP (base pointer) register value of an ASan error.
57pub fn get_report_bp() -> *mut c_void {
58    unsafe { __asan_get_report_bp() }
59}
60
61/// Gets the SP (stack pointer) register value of an ASan error.
62pub fn get_report_sp() -> *mut c_void {
63    unsafe { __asan_get_report_sp() }
64}
65
66/// Gets the address of the report buffer of an ASan error.
67pub fn get_report_address() -> *mut c_void {
68    unsafe { __asan_get_report_address() }
69}
70
71/// Gets access type of an ASan error (0 = read, 1 = write).
72pub fn get_report_access_type() -> i32 {
73    unsafe { __asan_get_report_access_type() }
74}
75
76/// Gets access size of an ASan error in bytes.
77pub fn get_report_access_size() -> usize {
78    unsafe { __asan_get_report_access_size() }
79}
80
81/// Gets the bug description of an ASan error.
82pub fn get_report_description() -> String {
83    unsafe {
84        let desc_ptr = __asan_get_report_description();
85        if desc_ptr.is_null() {
86            String::new()
87        } else {
88            CStr::from_ptr(desc_ptr).to_string_lossy().into_owned()
89        }
90    }
91}
92
93/// Sets a callback function to be called during ASan error reporting.
94pub fn set_error_report_callback(callback: Option<unsafe extern "C" fn(arg1: *const i8)>) {
95    unsafe {
96        __asan_set_error_report_callback(callback);
97    }
98}
99
100/// Prints accumulated statistics to stderr.
101pub fn print_accumulated_stats() {
102    unsafe {
103        __asan_print_accumulated_stats();
104    }
105}
106
107/// User-provided default option settings.
108pub fn default_options() -> String {
109    unsafe {
110        let options_ptr = __asan_default_options();
111        if options_ptr.is_null() {
112            String::new()
113        } else {
114            CStr::from_ptr(options_ptr).to_string_lossy().into_owned()
115        }
116    }
117}