File tree 3 files changed +21
-9
lines changed
src/tools/bsan/bsan-rt/src
3 files changed +21
-9
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ use hashbrown::{DefaultHashBuilder, HashMap};
17
17
use rustc_hash:: FxBuildHasher ;
18
18
19
19
use crate :: * ;
20
+ use crate :: shadow:: ShadowHeap ;
20
21
21
22
/// Every action that requires a heap allocation must be performed through a globally
22
23
/// accessible, singleton instance of `GlobalCtx`. Initializing or obtaining
@@ -43,17 +44,21 @@ impl GlobalCtx {
43
44
/// This function will also initialize our shadow heap
44
45
fn new ( hooks : BsanHooks ) -> Self {
45
46
Self {
46
- hooks,
47
+ hooks : hooks . clone ( ) ,
47
48
next_alloc_id : AtomicUsize :: new ( AllocId :: min ( ) . get ( ) ) ,
48
49
next_thread_id : AtomicUsize :: new ( 0 ) ,
49
- shadow_heap : ShadowHeap :: new ( hooks) ,
50
+ shadow_heap : ShadowHeap :: new ( & hooks) ,
50
51
}
51
52
}
52
53
53
54
pub fn shadow_heap ( & self ) -> & ShadowHeap < Provenance > {
54
55
& self . shadow_heap
55
56
}
56
57
58
+ pub fn hooks ( & self ) -> & BsanHooks {
59
+ & self . hooks
60
+ }
61
+
57
62
pub fn new_block < T > ( & self , num_elements : NonZeroUsize ) -> Block < T > {
58
63
let layout = Layout :: array :: < T > ( num_elements. into ( ) ) . unwrap ( ) ;
59
64
let size = NonZeroUsize :: new ( layout. size ( ) ) . unwrap ( ) ;
Original file line number Diff line number Diff line change @@ -152,13 +152,19 @@ pub type Span = usize;
152
152
/// and a borrow tag. We also include a pointer to the "lock" location for the allocation,
153
153
/// which contains all other metadata used to detect undefined behavior.
154
154
#[ repr( C ) ]
155
- #[ derive( Clone , Copy ) ]
155
+ #[ derive( Clone , Copy , Debug ) ]
156
156
pub struct Provenance {
157
157
pub alloc_id : AllocId ,
158
158
pub bor_tag : BorTag ,
159
159
pub alloc_info : * mut c_void ,
160
160
}
161
161
162
+ impl Default for Provenance {
163
+ fn default ( ) -> Self {
164
+ Provenance :: null ( )
165
+ }
166
+ }
167
+
162
168
impl Provenance {
163
169
/// The default provenance value, which is assigned to dangling or invalid
164
170
/// pointers.
@@ -258,14 +264,14 @@ extern "C" fn bsan_shadow_clear(addr: usize, access_size: usize) {}
258
264
/// the result in the return pointer.
259
265
#[ no_mangle]
260
266
unsafe extern "C" fn bsan_load_prov ( prov : * mut Provenance , address : usize ) {
261
- let result = global_ctx ( ) . shadow_heap ( ) . load_prov ( address) ;
267
+ let result = ( * global_ctx ( ) ) . shadow_heap ( ) . load_prov ( address) ;
262
268
* prov = result;
263
269
}
264
270
265
271
/// Stores the given provenance value into shadow memory at the location for the given address.
266
272
#[ no_mangle]
267
273
unsafe extern "C" fn bsan_store_prov ( provenance : * const Provenance , address : usize ) {
268
- let heap = & ( * global_ctx ( ) ) . shadow_heap ( ) ;
274
+ let heap = ( * global_ctx ( ) ) . shadow_heap ( ) ;
269
275
heap. store_prov ( provenance, address) ;
270
276
}
271
277
/// Pushes a shadow stack frame
Original file line number Diff line number Diff line change @@ -134,9 +134,9 @@ pub struct ShadowHeap<T> {
134
134
l1 : L1 < T > ,
135
135
}
136
136
137
- impl < T : Default + Copy > Default for ShadowHeap < T > {
137
+ impl < T > Default for ShadowHeap < T > {
138
138
fn default ( ) -> Self {
139
- Self { l1 : unsafe { L1 :: new ( global_ctx ( ) . hooks ( ) ) } }
139
+ Self { l1 : unsafe { L1 :: new ( ( * global_ctx ( ) ) . hooks ( ) ) } }
140
140
}
141
141
}
142
142
@@ -146,7 +146,8 @@ impl<T> ShadowHeap<T> {
146
146
}
147
147
}
148
148
149
- impl < T : Default + Copy > ShadowHeap < T > {
149
+ impl < T : Default + Copy > ShadowHeap < T > {
150
+
150
151
pub unsafe fn load_prov ( & self , address : usize ) -> T {
151
152
let ( l1_addr, l2_addr) = table_indices ( address) ;
152
153
let mut l2 = ( * self . l1 . entries ) [ l1_addr] ;
@@ -165,7 +166,7 @@ impl<T: Default + Copy> ShadowHeap<T> {
165
166
let mut l2 = ( * self . l1 . entries ) [ l1_addr] ;
166
167
if l2. is_null ( ) {
167
168
let l2_addr = unsafe { ( * self . l1 . entries ) . as_ptr ( ) . add ( l1_addr) as * mut c_void } ;
168
- l2 = & mut L2 :: new ( global_ctx ( ) . hooks ( ) , l2_addr) ;
169
+ l2 = & mut L2 :: new ( ( * global_ctx ( ) ) . hooks ( ) , l2_addr) ;
169
170
( * self . l1 . entries ) [ l1_addr] = l2;
170
171
}
171
172
You can’t perform that action at this time.
0 commit comments