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

Commit 2fa53c0

Browse files
committed
Dynamic offset calculation in GetSystemInfo.
1 parent 63e98ae commit 2fa53c0

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/shims/windows/foreign_items.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_target::spec::abi::Abi;
88
use crate::*;
99
use shims::foreign_items::EmulateByNameResult;
1010
use shims::windows::sync::EvalContextExt as _;
11+
use smallvec::SmallVec;
1112

1213
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
1314
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
@@ -122,11 +123,42 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
122123
iter::repeat(0u8).take(system_info.layout.size.bytes() as usize),
123124
)?;
124125
// Set selected fields.
126+
let word_ty = this.tcx.mk_ty(TyKind::Uint(UintTy::U16));
125127
let dword_ty = this.tcx.mk_ty(TyKind::Uint(UintTy::U32));
128+
let usize_ty = this.tcx.mk_ty(TyKind::Uint(UintTy::Usize));
129+
let word_layout = this.layout_of(word_ty)?;
126130
let dword_layout = this.layout_of(dword_ty)?;
131+
let usize_layout = this.layout_of(usize_ty)?;
132+
133+
// Using `mplace_field` is error-prone, see: https://github.com/rust-lang/miri/issues/2136.
134+
// Pointer fields have different sizes on different targets.
135+
// To avoid all these issue we calculate the offsets dynamically.
136+
let field_sizes = [
137+
word_layout.size, // 0, wProcessorArchitecture : WORD
138+
word_layout.size, // 1, wReserved : WORD
139+
dword_layout.size, // 2, dwPageSize : DWORD
140+
usize_layout.size, // 3, lpMinimumApplicationAddress : LPVOID
141+
usize_layout.size, // 4, lpMaximumApplicationAddress : LPVOID
142+
usize_layout.size, // 5, dwActiveProcessorMask : DWORD_PTR
143+
dword_layout.size, // 6, dwNumberOfProcessors : DWORD
144+
dword_layout.size, // 7, dwProcessorType : DWORD
145+
dword_layout.size, // 8, dwAllocationGranularity : DWORD
146+
word_layout.size, // 9, wProcessorLevel : WORD
147+
word_layout.size, // 10, wProcessorRevision : WORD
148+
];
149+
let field_offsets: SmallVec<[Size; 11]> = field_sizes
150+
.iter()
151+
.copied()
152+
.scan(Size::ZERO, |a, x| {
153+
let res = Some(*a);
154+
*a += x;
155+
res
156+
})
157+
.collect();
158+
127159
// Set page size.
128160
let page_size = system_info.offset(
129-
Size::from_bytes(4),
161+
field_offsets[2],
130162
MemPlaceMeta::None,
131163
dword_layout,
132164
&this.tcx,
@@ -137,7 +169,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
137169
)?;
138170
// Set number of processors.
139171
let num_cpus = system_info.offset(
140-
Size::from_bytes(32),
172+
field_offsets[6],
141173
MemPlaceMeta::None,
142174
dword_layout,
143175
&this.tcx,

0 commit comments

Comments
 (0)