| @@ -101,6 +101,8 @@ impl Drop for Stack { |
| 101 | 101 | pub(crate) struct Snapshot { |
| 102 | 102 | sp: usize, |
| 103 | 103 | words: Vec<usize>, |
| 104 | + /// ThreadSanitizer's call depth on the fiber then (`tsan::set_depth`). |
| 105 | + tsan_depth: usize, |
| 104 | 106 | } |
| 105 | 107 | |
| 106 | 108 | /// Copy `n` words. Volatile, one word at a time: never a `memcpy`, which |
| @@ -162,6 +164,53 @@ mod tsan { |
| 162 | 164 | fn __tsan_create_fiber(flags: u32) -> *mut std::ffi::c_void; |
| 163 | 165 | fn __tsan_destroy_fiber(fiber: *mut std::ffi::c_void); |
| 164 | 166 | fn __tsan_switch_to_fiber(fiber: *mut std::ffi::c_void, flags: u32); |
| 167 | + fn __tsan_testonly_shadow_stack_current_size() -> usize; |
| 168 | + fn __tsan_func_entry(pc: *const std::ffi::c_void); |
| 169 | + fn __tsan_func_exit(); |
| 170 | + } |
| 171 | + |
| 172 | + /// `__tsan_switch_to_fiber` without synchronising the two fibers. |
| 173 | + #[cfg(tilrt_tsan)] |
| 174 | + const NO_SYNC: u32 = 1; |
| 175 | + |
| 176 | + /// TSan's call depth on the running fiber: the number of instrumented |
| 177 | + /// frames it believes are on the stack. |
| 178 | + pub(super) fn depth() -> usize { |
| 179 | + #[cfg(tilrt_tsan)] |
| 180 | + // SAFETY: a query of the TSan runtime. |
| 181 | + return unsafe { __tsan_testonly_shadow_stack_current_size() }; |
| 182 | + #[cfg(not(tilrt_tsan))] |
| 183 | + 0 |
| 184 | + } |
| 185 | + |
| 186 | + /// Set TSan's call depth on suspended `fiber` to `depth`, after its |
| 187 | + /// stack was overwritten with a snapshot (multi-shot resumptions): pop |
| 188 | + /// frames, or push placeholders (their return addresses are unknown, |
| 189 | + /// so reports show them as 0). |
| 190 | + /// |
| 191 | + /// # Safety |
| 192 | + /// `fiber` came from [`create`], and is not running. |
| 193 | + pub(super) unsafe fn set_depth(fiber: Fiber, depth: usize) { |
| 194 | + #[cfg(tilrt_tsan)] |
| 195 | + // SAFETY: `fiber` is suspended; nothing instrumented runs while |
| 196 | + // it is TSan's current fiber here (only TSan's own entry points), |
| 197 | + // and the caller's fiber is current again on return. |
| 198 | + unsafe { |
| 199 | + let me = __tsan_get_current_fiber(); |
| 200 | + __tsan_switch_to_fiber(fiber.0, NO_SYNC); |
| 201 | + let mut d = __tsan_testonly_shadow_stack_current_size(); |
| 202 | + while d > depth { |
| 203 | + __tsan_func_exit(); |
| 204 | + d -= 1; |
| 205 | + } |
| 206 | + while d < depth { |
| 207 | + __tsan_func_entry(std::ptr::null()); |
| 208 | + d += 1; |
| 209 | + } |
| 210 | + __tsan_switch_to_fiber(me, NO_SYNC); |
| 211 | + } |
| 212 | + #[cfg(not(tilrt_tsan))] |
| 213 | + let _ = (fiber, depth); |
| 165 | 214 | } |
| 166 | 215 | |
| 167 | 216 | pub(super) fn current() -> Fiber { |
| @@ -231,6 +280,8 @@ mod imp { |
| 231 | 280 | /// ThreadSanitizer's handles for the resumer and the fiber. |
| 232 | 281 | caller_tsan: tsan::Fiber, |
| 233 | 282 | fiber_tsan: tsan::Fiber, |
| 283 | + /// TSan's call depth on the fiber when it last suspended. |
| 284 | + tsan_depth: usize, |
| 234 | 285 | input: Option<I>, |
| 235 | 286 | output: Option<Y>, |
| 236 | 287 | finished: bool, |
| @@ -249,6 +300,7 @@ mod imp { |
| 249 | 300 | unsafe { |
| 250 | 301 | let c = self.channel; |
| 251 | 302 | (*c).output = Some(value); |
| 303 | + (*c).tsan_depth = tsan::depth(); |
| 252 | 304 | tsan::switch_to((*c).caller_tsan); |
| 253 | 305 | tl_fiber_switch(&raw mut (*c).fiber_sp, (*c).caller_sp); |
| 254 | 306 | (*c).input.take().expect("tilrt: resumed without input") |
| @@ -281,6 +333,7 @@ mod imp { |
| 281 | 333 | fiber_sp: 0, |
| 282 | 334 | caller_tsan: tsan::Fiber::NONE, |
| 283 | 335 | fiber_tsan: tsan::create(), |
| 336 | + tsan_depth: 0, |
| 284 | 337 | input: None, |
| 285 | 338 | output: None, |
| 286 | 339 | finished: false, |
| @@ -345,7 +398,11 @@ mod imp { |
| 345 | 398 | super::copy_words(sp as *const usize, words.as_mut_ptr(), n); |
| 346 | 399 | words.set_len(n); |
| 347 | 400 | } |
| 348 | | − Some(Snapshot { sp, words }) |
| 401 | + Some(Snapshot { |
| 402 | + sp, |
| 403 | + words, |
| 404 | + tsan_depth: self.channel.tsan_depth, |
| 405 | + }) |
| 349 | 406 | } |
| 350 | 407 | |
| 351 | 408 | /// Make this fiber the suspended one `snap` was taken of. What its |
| @@ -359,6 +416,11 @@ mod imp { |
| 359 | 416 | } |
| 360 | 417 | let c = &mut *self.channel; |
| 361 | 418 | c.fiber_sp = snap.sp; |
| 419 | + // TSan's shadow call stack for the fiber must match the frames |
| 420 | + // now on it again, or their returns would underflow it. |
| 421 | + // SAFETY: the fiber is not running. |
| 422 | + unsafe { tsan::set_depth(c.fiber_tsan, snap.tsan_depth) }; |
| 423 | + c.tsan_depth = snap.tsan_depth; |
| 362 | 424 | c.finished = false; |
| 363 | 425 | c.input = None; |
| 364 | 426 | c.output = None; |