Add Alex's solution for null handling in TlsRestore

This commit is contained in:
Alex Crichton
2021-10-25 10:04:31 -07:00
committed by Pat Hickey
parent fb549c6ddb
commit 2f2c5231b4

View File

@@ -431,9 +431,12 @@ mod tls {
// null out our own previous field for safety in case it's // null out our own previous field for safety in case it's
// accidentally used later. // accidentally used later.
let raw = raw::get(); let raw = raw::get();
assert!(!raw.is_null()); if !raw.is_null() {
let prev = (*raw).prev.replace(ptr::null()); let prev = (*raw).prev.replace(ptr::null());
raw::replace(prev)?; raw::replace(prev)?;
}
// Null case: we aren't in a wasm context, so theres no tls
// to save for restoration.
Ok(TlsRestore(raw)) Ok(TlsRestore(raw))
} }
@@ -442,6 +445,11 @@ mod tls {
/// This is unsafe because it's intended to only be used within the /// This is unsafe because it's intended to only be used within the
/// context of stack switching within wasmtime. /// context of stack switching within wasmtime.
pub unsafe fn replace(self) -> Result<(), Box<super::Trap>> { pub unsafe fn replace(self) -> Result<(), Box<super::Trap>> {
// Null case: we aren't in a wasm context, so theres no tls
// to restore.
if self.0.is_null() {
return Ok(());
}
// We need to configure our previous TLS pointer to whatever is in // We need to configure our previous TLS pointer to whatever is in
// TLS at this time, and then we set the current state to ourselves. // TLS at this time, and then we set the current state to ourselves.
let prev = raw::get(); let prev = raw::get();