Skip to content

Commit 35f28d8

Browse files
committed
Don't bother with the CHERI-compatible version of ptr::with_addr
1 parent 6eb76c7 commit 35f28d8

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

library/core/src/ptr/const_ptr.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -243,19 +243,24 @@ impl<T: ?Sized> *const T {
243243
// a wrapping_offset, so we can emulate it as such. This should properly
244244
// restore pointer provenance even under today's compiler.
245245
let self_addr = self.addr();
246-
// Unfortunately, the CHERI-compatible way of defining this operation
247-
// optimizes worse, so we special case it... in a somewhat ad-hoc way
248-
// (checking for 128 bit pointers) because at the time of this writing,
249-
// we don't actually support CHERI yet. Ideally this would be
250-
// `cfg!(target_supports_large_wrapping_offsets)` or something, see
251-
// #96152 for details.
252-
if cfg!(target_pointer_width = "128") {
253-
let offset = (addr as isize).wrapping_sub(self_addr as isize);
254-
// This is the canonical desugarring of this operation
255-
self.cast::<u8>().wrapping_offset(offset).cast::<T>()
256-
} else {
257-
self.cast::<u8>().wrapping_sub(self_addr).wrapping_add(addr).cast::<T>()
258-
}
246+
// In an ideal world (err, an ideal world we'd have an intrinsic, but
247+
// short of that), we'd implement this as follows:
248+
// ```
249+
// let offset = (addr as isize).wrapping_sub(self_addr as isize);
250+
// self.cast::<u8>().wrapping_offset(offset).cast::<T>()
251+
// ```
252+
// This is the canonical desugaring of this operation, and is compatible
253+
// with targets which don't allow large wrapping add/sub/offset
254+
// operations, including CHERI.
255+
//
256+
// Unfortunately, this causes worse codegen than the following
257+
// implementation, which should be correct on all targets we currently
258+
// support (at the moment AFAICT, we don't yet support CHERI, or we'd
259+
// special-case it to use the desugaring listed above).
260+
//
261+
// As a result, we use the following implementation, which would be
262+
// wrong on CHERI, but right everywhere else.
263+
self.cast::<u8>().wrapping_sub(self_addr).wrapping_add(addr).cast::<T>()
259264
}
260265

261266
/// Creates a new pointer by mapping `self`'s address to a new one.

library/core/src/ptr/mut_ptr.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,24 @@ impl<T: ?Sized> *mut T {
247247
// a wrapping_offset, so we can emulate it as such. This should properly
248248
// restore pointer provenance even under today's compiler.
249249
let self_addr = self.addr();
250-
// Unfortunately, the CHERI-compatible way of defining this operation
251-
// optimizes worse, so we special case it... in a somewhat ad-hoc way
252-
// (checking for 128 bit pointers) because at the time of this writing,
253-
// we don't actually support CHERI yet. Ideally this would be
254-
// `cfg!(target_supports_large_wrapping_offsets)` or something, see
255-
// #96152 for details.
256-
if cfg!(target_pointer_width = "128") {
257-
let offset = (addr as isize).wrapping_sub(self_addr as isize);
258-
// This is the canonical desugarring of this operation
259-
self.cast::<u8>().wrapping_offset(offset).cast::<T>()
260-
} else {
261-
self.cast::<u8>().wrapping_sub(self_addr).wrapping_add(addr).cast::<T>()
262-
}
250+
// In an ideal world (err, an ideal world we'd have an intrinsic, but
251+
// short of that), we'd implement this as follows:
252+
// ```
253+
// let offset = (addr as isize).wrapping_sub(self_addr as isize);
254+
// self.cast::<u8>().wrapping_offset(offset).cast::<T>()
255+
// ```
256+
// This is the canonical desugaring of this operation, and is compatible
257+
// with targets which don't allow large wrapping add/sub/offset
258+
// operations, including CHERI.
259+
//
260+
// Unfortunately, this causes worse codegen than the following
261+
// implementation, which should be correct on all targets we currently
262+
// support (at the moment AFAICT, we don't yet support CHERI, or we'd
263+
// special-case it to use the desugaring listed above).
264+
//
265+
// As a result, we use the following implementation, which would be
266+
// wrong on CHERI, but right everywhere else.
267+
self.cast::<u8>().wrapping_sub(self_addr).wrapping_add(addr).cast::<T>()
263268
}
264269

265270
/// Creates a new pointer by mapping `self`'s address to a new one.

0 commit comments

Comments
 (0)