Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 63e98ae

Browse files
committed
Change GetSystemInfo to explicit offset.
1 parent c4ee368 commit 63e98ae

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/shims/windows/foreign_items.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
111111

112112
// Querying system information
113113
"GetSystemInfo" => {
114+
use crate::rustc_middle::ty::{layout::LayoutOf, TyKind, UintTy};
115+
114116
let [system_info] =
115117
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
116118
let system_info = this.deref_operand(system_info)?;
@@ -119,21 +121,28 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
119121
system_info.ptr,
120122
iter::repeat(0u8).take(system_info.layout.size.bytes() as usize),
121123
)?;
122-
let dword_size = Size::from_bytes(4);
123-
124-
// In WinApi SYSTEM_INFO's first field is a 4-byte union, but num_cpus
125-
// inlines it as two 2-byte fields. In num_cpus case all fields are offset by 1
126-
// compared to WinApi. See https://github.com/rust-lang/miri/issues/2136#issuecomment-1133661262.
127-
let first_field = this.mplace_field(&system_info, 0)?;
128-
let offset = if first_field.layout.size.bytes() == 2 { 1 } else { 0 };
129-
130-
let page_size = this.mplace_field(&system_info, 1 + offset)?;
131-
let num_cpus = this.mplace_field(&system_info, 5 + offset)?;
132-
124+
// Set selected fields.
125+
let dword_ty = this.tcx.mk_ty(TyKind::Uint(UintTy::U32));
126+
let dword_layout = this.layout_of(dword_ty)?;
133127
// Set page size.
134-
this.write_scalar(Scalar::from_int(PAGE_SIZE, dword_size), &page_size.into())?;
128+
let page_size = system_info.offset(
129+
Size::from_bytes(4),
130+
MemPlaceMeta::None,
131+
dword_layout,
132+
&this.tcx,
133+
)?;
134+
this.write_scalar(
135+
Scalar::from_int(PAGE_SIZE, dword_layout.size),
136+
&page_size.into(),
137+
)?;
135138
// Set number of processors.
136-
this.write_scalar(Scalar::from_int(NUM_CPUS, dword_size), &num_cpus.into())?;
139+
let num_cpus = system_info.offset(
140+
Size::from_bytes(32),
141+
MemPlaceMeta::None,
142+
dword_layout,
143+
&this.tcx,
144+
)?;
145+
this.write_scalar(Scalar::from_int(NUM_CPUS, dword_layout.size), &num_cpus.into())?;
137146
}
138147

139148
// Thread-local storage

0 commit comments

Comments
 (0)