Skip to content

Implement ptr_mask intrinsic #2624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/shims/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_middle::{
mir,
ty::{self, FloatTy, Ty},
};
use rustc_target::abi::Integer;
use rustc_target::abi::{Integer, Size};

use crate::*;
use atomic::EvalContextExt as _;
Expand Down Expand Up @@ -120,6 +120,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
this.write_bytes_ptr(ptr, iter::repeat(val_byte).take(byte_count.bytes_usize()))?;
}

"ptr_mask" => {
let [ptr, mask] = check_arg_count(args)?;

let ptr = this.read_pointer(ptr)?;
let mask = this.read_scalar(mask)?.to_machine_usize(this)?;

let masked_addr = Size::from_bytes(ptr.addr().bytes() & mask);

this.write_pointer(Pointer::new(ptr.provenance, masked_addr), dest)?;
}

// Floating-point operations
"fabsf32" => {
let [f] = check_arg_count(args)?;
Expand Down
18 changes: 18 additions & 0 deletions tests/pass/shims/ptr_mask.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(ptr_mask)]
#![feature(strict_provenance)]

fn main() {
let v: u32 = 0xABCDABCD;
let ptr: *const u32 = &v;

// u32 is 4 aligned,
// so the lower `log2(4) = 2` bits of the address are always 0
assert_eq!(ptr.addr() & 0b11, 0);

let tagged_ptr = ptr.map_addr(|a| a | 0b11);
let tag = tagged_ptr.addr() & 0b11;
let masked_ptr = tagged_ptr.mask(!0b11);

assert_eq!(tag, 0b11);
assert_eq!(unsafe { *masked_ptr }, 0xABCDABCD);
}