Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ad9a52d

Browse files
authoredJul 23, 2024
Rollup merge of rust-lang#125834 - workingjubilee:weaken-thir-unsafeck-for-addr-of-static-mut, r=compiler-errors
treat `&raw (const|mut) UNSAFE_STATIC` implied deref as safe Fixes rust-lang#125833 As reported in that and related issues, `static mut STATIC_MUT: T` is very often used in embedded code, and is in many ways equivalent to `static STATIC_CELL: SyncUnsafeCell<T>`. The Rust expression of `&raw mut STATIC_MUT` and `SyncUnsafeCell::get(&STATIC_CELL)` are approximately equal, and both evaluate to `*mut T`. The library function is safe because it has *declared itself* to be safe. However, the raw ref operator is unsafe because all uses of `static mut` are considered unsafe, even though the static's value is not used by this expression (unlike, for example, `&STATIC_MUT`). We can fix this unnatural difference by simply adding the proper exclusion for the safety check inside the THIR unsafeck, so that we do not declare it unsafe if it is not. While the primary concern here is `static mut`, this change is made for all instances of an "unsafe static", which includes a static declared inside `extern "abi" {}`. Hypothetically, we could go as far as generalizing this to all instances of `&raw (const|mut) *ptr`, but today we do not, as we have not actually considered the range of possible expressions that use a similar encoding. We do not even extend this to thread-local equivalents, because they have less clear semantics.
2 parents 7ae76f0 + c039ee8 commit ad9a52d

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed
 

‎panic_unwind/src/seh.rs

+6
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ mod imp {
157157
// going to be cross-lang LTOed anyway. However, using expose is shorter and
158158
// requires less unsafe.
159159
let addr: usize = ptr.expose_provenance();
160+
#[cfg(bootstrap)]
160161
let image_base = unsafe { addr_of!(__ImageBase) }.addr();
162+
#[cfg(not(bootstrap))]
163+
let image_base = addr_of!(__ImageBase).addr();
161164
let offset: usize = addr - image_base;
162165
Self(offset as u32)
163166
}
@@ -250,7 +253,10 @@ extern "C" {
250253
// This is fine since the MSVC runtime uses string comparison on the type name
251254
// to match TypeDescriptors rather than pointer equality.
252255
static mut TYPE_DESCRIPTOR: _TypeDescriptor = _TypeDescriptor {
256+
#[cfg(bootstrap)]
253257
pVFTable: unsafe { addr_of!(TYPE_INFO_VTABLE) } as *const _,
258+
#[cfg(not(bootstrap))]
259+
pVFTable: addr_of!(TYPE_INFO_VTABLE) as *const _,
254260
spare: core::ptr::null_mut(),
255261
name: TYPE_NAME,
256262
};

0 commit comments

Comments
 (0)
Failed to load comments.