Skip to content

Commit 562b282

Browse files
use deref_pointer_as instead of deref_pointer
1 parent 5460fbe commit 562b282

File tree

14 files changed

+39
-34
lines changed

14 files changed

+39
-34
lines changed

src/tools/miri/src/shims/alloc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
102102
size: &OpTy<'tcx>,
103103
) -> InterpResult<'tcx, Scalar> {
104104
let this = self.eval_context_mut();
105-
let memptr = this.deref_pointer(memptr)?;
105+
let memptr = this.deref_pointer_as(memptr, this.machine.layouts.mut_raw_ptr)?;
106106
let align = this.read_target_usize(align)?;
107107
let size = this.read_target_usize(size)?;
108108

src/tools/miri/src/shims/backtrace.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
8989
1 => {
9090
let [_flags, buf] = this.check_shim(abi, Conv::Rust, link_name, args)?;
9191

92-
let buf_place = this.deref_pointer(buf)?;
93-
9492
let ptr_layout = this.layout_of(ptr_ty)?;
93+
let buf_place = this.deref_pointer_as(buf, ptr_layout)?;
9594

9695
for (i, ptr) in ptrs.into_iter().enumerate() {
9796
let offset = ptr_layout.size.checked_mul(i.try_into().unwrap(), this).unwrap();

src/tools/miri/src/shims/foreign_items.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
865865
"lgammaf_r" => {
866866
let [x, signp] = this.check_shim(abi, Conv::C, link_name, args)?;
867867
let x = this.read_scalar(x)?.to_f32()?;
868-
let signp = this.deref_pointer(signp)?;
868+
let signp = this.deref_pointer_as(signp, this.machine.layouts.i32)?;
869869

870870
// Using host floats (but it's fine, these operations do not have guaranteed precision).
871871
let (res, sign) = x.to_host().ln_gamma();
@@ -876,7 +876,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
876876
"lgamma_r" => {
877877
let [x, signp] = this.check_shim(abi, Conv::C, link_name, args)?;
878878
let x = this.read_scalar(x)?.to_f64()?;
879-
let signp = this.deref_pointer(signp)?;
879+
let signp = this.deref_pointer_as(signp, this.machine.layouts.i32)?;
880880

881881
// Using host floats (but it's fine, these operations do not have guaranteed precision).
882882
let (res, sign) = x.to_host().ln_gamma();

src/tools/miri/src/shims/time.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
132132
this.assert_target_os_is_unix("localtime_r");
133133
this.check_no_isolation("`localtime_r`")?;
134134

135-
let timep = this.deref_pointer(timep)?;
135+
let time_layout = this.libc_ty_layout("time_t");
136+
let timep = this.deref_pointer_as(timep, time_layout)?;
136137
let result = this.deref_pointer_as(result_op, this.libc_ty_layout("tm"))?;
137138

138139
// The input "represents the number of seconds elapsed since the Epoch,
139140
// 1970-01-01 00:00:00 +0000 (UTC)".
140-
let sec_since_epoch: i64 = this
141-
.read_scalar(&timep)?
142-
.to_int(this.libc_ty_layout("time_t").size)?
143-
.try_into()
144-
.unwrap();
141+
let sec_since_epoch: i64 =
142+
this.read_scalar(&timep)?.to_int(time_layout.size)?.try_into().unwrap();
145143
let dt_utc: DateTime<Utc> =
146144
DateTime::from_timestamp(sec_since_epoch, 0).expect("Invalid timestamp");
147145

@@ -254,7 +252,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
254252
let qpc = i64::try_from(duration.as_nanos()).map_err(|_| {
255253
err_unsup_format!("programs running longer than 2^63 nanoseconds are not supported")
256254
})?;
257-
this.write_scalar(Scalar::from_i64(qpc), &this.deref_pointer(lpPerformanceCount_op)?)?;
255+
this.write_scalar(
256+
Scalar::from_i64(qpc),
257+
&this.deref_pointer_as(lpPerformanceCount_op, this.machine.layouts.i64)?,
258+
)?;
258259
interp_ok(Scalar::from_i32(-1)) // return non-zero on success
259260
}
260261

src/tools/miri/src/shims/unix/foreign_items.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -866,8 +866,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
866866
// These shims are enabled only when the caller is in the standard library.
867867
"pthread_attr_getguardsize" if this.frame_in_std() => {
868868
let [_attr, guard_size] = this.check_shim(abi, Conv::C, link_name, args)?;
869-
let guard_size = this.deref_pointer(guard_size)?;
870869
let guard_size_layout = this.libc_ty_layout("size_t");
870+
let guard_size = this.deref_pointer_as(guard_size, guard_size_layout)?;
871871
this.write_scalar(
872872
Scalar::from_uint(this.machine.page_size, guard_size_layout.size),
873873
&guard_size,
@@ -893,8 +893,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
893893
this.check_shim(abi, Conv::C, link_name, args)?;
894894
let _attr_place =
895895
this.deref_pointer_as(attr_place, this.libc_ty_layout("pthread_attr_t"))?;
896-
let addr_place = this.deref_pointer(addr_place)?;
897-
let size_place = this.deref_pointer(size_place)?;
896+
let addr_place = this.deref_pointer_as(addr_place, this.machine.layouts.usize)?;
897+
let size_place = this.deref_pointer_as(size_place, this.machine.layouts.usize)?;
898898

899899
this.write_scalar(
900900
Scalar::from_uint(this.machine.stack_addr, this.pointer_size()),
@@ -928,7 +928,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
928928
let pwd = this.deref_pointer_as(pwd, this.libc_ty_layout("passwd"))?;
929929
let buf = this.read_pointer(buf)?;
930930
let buflen = this.read_target_usize(buflen)?;
931-
let result = this.deref_pointer(result)?;
931+
let result = this.deref_pointer_as(result, this.machine.layouts.mut_raw_ptr)?;
932932

933933
// Must be for "us".
934934
if uid != UID {

src/tools/miri/src/shims/unix/fs.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
11681168
}
11691169

11701170
let dirp = this.read_target_usize(dirp_op)?;
1171+
let result_place = this.deref_pointer_as(result_op, this.machine.layouts.mut_raw_ptr)?;
11711172

11721173
// Reject if isolation is enabled.
11731174
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
@@ -1253,15 +1254,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
12531254
}
12541255
_ => unreachable!(),
12551256
}
1256-
1257-
let result_place = this.deref_pointer(result_op)?;
12581257
this.write_scalar(this.read_scalar(entry_op)?, &result_place)?;
12591258

12601259
Scalar::from_i32(0)
12611260
}
12621261
None => {
12631262
// end of stream: return 0, assign *result=NULL
1264-
this.write_null(&this.deref_pointer(result_op)?)?;
1263+
this.write_null(&result_place)?;
12651264
Scalar::from_i32(0)
12661265
}
12671266
Some(Err(e)) => {

src/tools/miri/src/shims/unix/macos/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
3030
'tcx: 'a,
3131
{
3232
let this = self.eval_context_mut();
33-
let lock = this.deref_pointer(lock_ptr)?;
33+
let lock = this.deref_pointer_as(lock_ptr, this.libc_ty_layout("os_unfair_lock_s"))?;
3434
this.lazy_sync_get_data(
3535
&lock,
3636
Size::ZERO, // offset for init tracking

src/tools/miri/src/shims/unix/solarish/foreign_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
163163
throw_unsup_format!("pset_info is only supported with list==NULL");
164164
}
165165

166-
let cpus = this.deref_pointer(cpus)?;
166+
let cpus = this.deref_pointer_as(cpus, this.machine.layouts.u32)?;
167167
this.write_scalar(Scalar::from_u32(this.machine.num_cpus), &cpus)?;
168168
this.write_null(dest)?;
169169
}

src/tools/miri/src/shims/unix/sync.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn mutex_create<'tcx>(
170170
mutex_ptr: &OpTy<'tcx>,
171171
kind: MutexKind,
172172
) -> InterpResult<'tcx, PthreadMutex> {
173-
let mutex = ecx.deref_pointer(mutex_ptr)?;
173+
let mutex = ecx.deref_pointer_as(mutex_ptr, ecx.libc_ty_layout("pthread_mutex_t"))?;
174174
let id = ecx.machine.sync.mutex_create();
175175
let data = PthreadMutex { mutex_ref: id, kind };
176176
ecx.lazy_sync_init(&mutex, mutex_init_offset(ecx)?, data.clone())?;
@@ -186,7 +186,7 @@ fn mutex_get_data<'tcx, 'a>(
186186
where
187187
'tcx: 'a,
188188
{
189-
let mutex = ecx.deref_pointer(mutex_ptr)?;
189+
let mutex = ecx.deref_pointer_as(mutex_ptr, ecx.libc_ty_layout("pthread_mutex_t"))?;
190190
ecx.lazy_sync_get_data(
191191
&mutex,
192192
mutex_init_offset(ecx)?,
@@ -265,7 +265,7 @@ fn rwlock_get_data<'tcx, 'a>(
265265
where
266266
'tcx: 'a,
267267
{
268-
let rwlock = ecx.deref_pointer(rwlock_ptr)?;
268+
let rwlock = ecx.deref_pointer_as(rwlock_ptr, ecx.libc_ty_layout("pthread_rwlock_t"))?;
269269
ecx.lazy_sync_get_data(
270270
&rwlock,
271271
rwlock_init_offset(ecx)?,
@@ -383,7 +383,7 @@ fn cond_create<'tcx>(
383383
cond_ptr: &OpTy<'tcx>,
384384
clock: ClockId,
385385
) -> InterpResult<'tcx, PthreadCondvar> {
386-
let cond = ecx.deref_pointer(cond_ptr)?;
386+
let cond = ecx.deref_pointer_as(cond_ptr, ecx.libc_ty_layout("pthread_cond_t"))?;
387387
let id = ecx.machine.sync.condvar_create();
388388
let data = PthreadCondvar { id, clock };
389389
ecx.lazy_sync_init(&cond, cond_init_offset(ecx)?, data)?;
@@ -397,7 +397,7 @@ fn cond_get_data<'tcx, 'a>(
397397
where
398398
'tcx: 'a,
399399
{
400-
let cond = ecx.deref_pointer(cond_ptr)?;
400+
let cond = ecx.deref_pointer_as(cond_ptr, ecx.libc_ty_layout("pthread_cond_t"))?;
401401
ecx.lazy_sync_get_data(
402402
&cond,
403403
cond_init_offset(ecx)?,
@@ -760,7 +760,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
760760
let this = self.eval_context_mut();
761761

762762
let clock_id = condattr_get_clock_id(this, attr_op)?;
763-
this.write_scalar(Scalar::from_i32(clock_id), &this.deref_pointer(clk_id_op)?)?;
763+
this.write_scalar(
764+
Scalar::from_i32(clock_id),
765+
&this.deref_pointer_as(clk_id_op, this.libc_ty_layout("clockid_t"))?,
766+
)?;
764767

765768
interp_ok(())
766769
}

src/tools/miri/src/shims/unix/unnamed_socket.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
362362
let domain = this.read_scalar(domain)?.to_i32()?;
363363
let mut flags = this.read_scalar(type_)?.to_i32()?;
364364
let protocol = this.read_scalar(protocol)?.to_i32()?;
365-
let sv = this.deref_pointer(sv)?;
365+
// This is really a pointer to `[i32; 2]` but we use a ptr-to-first-element representation.
366+
let sv = this.deref_pointer_as(sv, this.machine.layouts.i32)?;
366367

367368
let mut is_sock_nonblock = false;
368369

src/tools/miri/src/shims/windows/env.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
218218

219219
let token = this.read_target_isize(token)?;
220220
let buf = this.read_pointer(buf)?;
221-
let size = this.deref_pointer(size)?;
221+
let size = this.deref_pointer_as(size, this.machine.layouts.u32)?;
222222

223223
if token != -4 {
224224
throw_unsup_format!(

src/tools/miri/src/shims/windows/foreign_items.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
523523
let [handle, name_ptr] = this.check_shim(abi, sys_conv, link_name, args)?;
524524

525525
let handle = this.read_scalar(handle)?;
526-
let name_ptr = this.deref_pointer(name_ptr)?; // the pointer where we should store the ptr to the name
526+
let name_ptr = this.deref_pointer_as(name_ptr, this.machine.layouts.mut_raw_ptr)?; // the pointer where we should store the ptr to the name
527527

528528
let thread = match Handle::try_from_scalar(handle, this)? {
529529
Ok(Handle::Thread(thread)) => Ok(thread),
@@ -725,7 +725,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
725725
"GetConsoleMode" if this.frame_in_std() => {
726726
let [console, mode] = this.check_shim(abi, sys_conv, link_name, args)?;
727727
this.read_target_isize(console)?;
728-
this.deref_pointer(mode)?;
728+
this.deref_pointer_as(mode, this.machine.layouts.u32)?;
729729
// Indicate an error.
730730
this.write_null(dest)?;
731731
}

src/tools/miri/src/shims/windows/sync.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
2929
{
3030
let this = self.eval_context_mut();
3131

32-
let init_once = this.deref_pointer(init_once_ptr)?;
32+
let init_once =
33+
this.deref_pointer_as(init_once_ptr, this.windows_ty_layout("INIT_ONCE"))?;
3334
let init_offset = Size::ZERO;
3435

3536
this.lazy_sync_get_data(
@@ -85,7 +86,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
8586

8687
let id = this.init_once_get_data(init_once_op)?.id;
8788
let flags = this.read_scalar(flags_op)?.to_u32()?;
88-
let pending_place = this.deref_pointer(pending_op)?;
89+
// PBOOL is int*
90+
let pending_place = this.deref_pointer_as(pending_op, this.machine.layouts.i32)?;
8991
let context = this.read_pointer(context_op)?;
9092

9193
if flags != 0 {

src/tools/miri/src/shims/windows/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
2929
let thread = if this.ptr_is_null(this.read_pointer(thread_op)?)? {
3030
None
3131
} else {
32-
let thread_info_place = this.deref_pointer(thread_op)?;
32+
let thread_info_place = this.deref_pointer_as(thread_op, this.machine.layouts.u32)?;
3333
Some(thread_info_place)
3434
};
3535

0 commit comments

Comments
 (0)