Skip to content

Commit 209151e

Browse files
Added get_arg to check and transmute opty
1 parent 61e45f8 commit 209151e

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/helpers.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,3 +1312,15 @@ pub(crate) fn windows_check_buffer_size((success, len): (bool, u64)) -> u32 {
13121312
u32::try_from(len).unwrap()
13131313
}
13141314
}
1315+
1316+
pub(crate) fn get_arg<'tcx>(
1317+
ecx: &mut MiriInterpCx<'tcx>,
1318+
op: &OpTy<'tcx>,
1319+
ty: Ty<'tcx>,
1320+
) -> InterpResult<'tcx, OpTy<'tcx>> {
1321+
let ty_layout = ecx.layout_of(ty)?;
1322+
if !op.layout.backend_repr.eq_up_to_validity(&ty_layout.backend_repr) {
1323+
throw_ub_format!("Invalid argument type: ABI mismatch");
1324+
}
1325+
op.transmute(ty_layout, ecx)
1326+
}

src/shims/x86/sse42.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_middle::ty::layout::LayoutOf as _;
55
use rustc_span::Symbol;
66
use rustc_target::callconv::{Conv, FnAbi};
77

8+
use crate::helpers::get_arg;
89
use crate::*;
910

1011
/// A bitmask constant for scrutinizing the immediate byte provided
@@ -204,11 +205,11 @@ fn deconstruct_args<'tcx>(
204205
abi: &FnAbi<'tcx, Ty<'tcx>>,
205206
args: &[OpTy<'tcx>],
206207
) -> InterpResult<'tcx, (OpTy<'tcx>, OpTy<'tcx>, Option<(u64, u64)>, u8)> {
207-
let array_layout_fn = |ecx: &mut MiriInterpCx<'tcx>, imm: u8| {
208+
let array_ty_fn = |ecx: &mut MiriInterpCx<'tcx>, imm: u8| {
208209
if imm & USE_WORDS != 0 {
209-
ecx.layout_of(Ty::new_array(ecx.tcx.tcx, ecx.tcx.types.u16, 8))
210+
Ty::new_array(ecx.tcx.tcx, ecx.tcx.types.u16, 8)
210211
} else {
211-
ecx.layout_of(Ty::new_array(ecx.tcx.tcx, ecx.tcx.types.u8, 16))
212+
Ty::new_array(ecx.tcx.tcx, ecx.tcx.types.u8, 16)
212213
}
213214
};
214215

@@ -230,18 +231,18 @@ fn deconstruct_args<'tcx>(
230231
let len1 = u64::from(ecx.read_scalar(len1)?.to_u32()?.min(default_len));
231232
let len2 = u64::from(ecx.read_scalar(len2)?.to_u32()?.min(default_len));
232233

233-
let array_layout = array_layout_fn(ecx, imm)?;
234-
let str1 = str1.transmute(array_layout, ecx)?;
235-
let str2 = str2.transmute(array_layout, ecx)?;
234+
let array_ty = array_ty_fn(ecx, imm);
235+
let str1 = get_arg(ecx, str1, array_ty)?;
236+
let str2 = get_arg(ecx, str2, array_ty)?;
236237

237238
interp_ok((str1, str2, Some((len1, len2)), imm))
238239
} else {
239240
let [str1, str2, imm] = ecx.check_shim(abi, Conv::C, link_name, args)?;
240241
let imm = ecx.read_scalar(imm)?.to_u8()?;
241242

242-
let array_layout = array_layout_fn(ecx, imm)?;
243-
let str1 = str1.transmute(array_layout, ecx)?;
244-
let str2 = str2.transmute(array_layout, ecx)?;
243+
let array_ty = array_ty_fn(ecx, imm);
244+
let str1 = get_arg(ecx, str1, array_ty)?;
245+
let str2 = get_arg(ecx, str2, array_ty)?;
245246

246247
interp_ok((str1, str2, None, imm))
247248
}

0 commit comments

Comments
 (0)