Skip to content

Commit e1d0507

Browse files
authored
Merge pull request #4140 from geetanshjuneja/deref
Use deref_pointer_as instead of deref_pointer
2 parents 8a893ab + 562b282 commit e1d0507

File tree

14 files changed

+39
-34
lines changed

14 files changed

+39
-34
lines changed

Diff for: src/tools/miri/src/shims/alloc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
9393
size: &OpTy<'tcx>,
9494
) -> InterpResult<'tcx, Scalar> {
9595
let this = self.eval_context_mut();
96-
let memptr = this.deref_pointer(memptr)?;
96+
let memptr = this.deref_pointer_as(memptr, this.machine.layouts.mut_raw_ptr)?;
9797
let align = this.read_target_usize(align)?;
9898
let size = this.read_target_usize(size)?;
9999

Diff for: 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();

Diff for: src/tools/miri/src/shims/foreign_items.rs

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

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

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

Diff for: 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

Diff for: src/tools/miri/src/shims/unix/foreign_items.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
825825
// These shims are enabled only when the caller is in the standard library.
826826
"pthread_attr_getguardsize" if this.frame_in_std() => {
827827
let [_attr, guard_size] = this.check_shim(abi, Conv::C, link_name, args)?;
828-
let guard_size = this.deref_pointer(guard_size)?;
829828
let guard_size_layout = this.libc_ty_layout("size_t");
829+
let guard_size = this.deref_pointer_as(guard_size, guard_size_layout)?;
830830
this.write_scalar(
831831
Scalar::from_uint(this.machine.page_size, guard_size_layout.size),
832832
&guard_size,
@@ -852,8 +852,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
852852
this.check_shim(abi, Conv::C, link_name, args)?;
853853
let _attr_place =
854854
this.deref_pointer_as(attr_place, this.libc_ty_layout("pthread_attr_t"))?;
855-
let addr_place = this.deref_pointer(addr_place)?;
856-
let size_place = this.deref_pointer(size_place)?;
855+
let addr_place = this.deref_pointer_as(addr_place, this.machine.layouts.usize)?;
856+
let size_place = this.deref_pointer_as(size_place, this.machine.layouts.usize)?;
857857

858858
this.write_scalar(
859859
Scalar::from_uint(this.machine.stack_addr, this.pointer_size()),
@@ -887,7 +887,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
887887
let pwd = this.deref_pointer_as(pwd, this.libc_ty_layout("passwd"))?;
888888
let buf = this.read_pointer(buf)?;
889889
let buflen = this.read_target_usize(buflen)?;
890-
let result = this.deref_pointer(result)?;
890+
let result = this.deref_pointer_as(result, this.machine.layouts.mut_raw_ptr)?;
891891

892892
// Must be for "us".
893893
if uid != UID {

Diff for: src/tools/miri/src/shims/unix/fs.rs

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

11741174
let dirp = this.read_target_usize(dirp_op)?;
1175+
let result_place = this.deref_pointer_as(result_op, this.machine.layouts.mut_raw_ptr)?;
11751176

11761177
// Reject if isolation is enabled.
11771178
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
@@ -1257,15 +1258,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
12571258
}
12581259
_ => unreachable!(),
12591260
}
1260-
1261-
let result_place = this.deref_pointer(result_op)?;
12621261
this.write_scalar(this.read_scalar(entry_op)?, &result_place)?;
12631262

12641263
Scalar::from_i32(0)
12651264
}
12661265
None => {
12671266
// end of stream: return 0, assign *result=NULL
1268-
this.write_null(&this.deref_pointer(result_op)?)?;
1267+
this.write_null(&result_place)?;
12691268
Scalar::from_i32(0)
12701269
}
12711270
Some(Err(e)) => {

Diff for: 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

Diff for: 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
}

Diff for: 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
}

Diff for: 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

Diff for: 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!(

Diff for: 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
}

Diff for: 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 {

Diff for: 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)