1use std::num::NonZero;
2use std::sync::Mutex;
3use std::{cmp, iter};
4
5use rand::Rng;
6use rustc_abi::{Align, ExternAbi, FieldIdx, FieldsShape, Size, Variants};
7use rustc_data_structures::fx::{FxBuildHasher, FxHashSet};
8use rustc_hir::def::{DefKind, Namespace};
9use rustc_hir::def_id::{CRATE_DEF_INDEX, CrateNum, DefId, LOCAL_CRATE};
10use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
11use rustc_middle::middle::dependency_format::Linkage;
12use rustc_middle::middle::exported_symbols::ExportedSymbol;
13use rustc_middle::ty::layout::{LayoutOf, MaybeResult, TyAndLayout};
14use rustc_middle::ty::{self, FnSigKind, IntTy, Ty, TyCtxt, UintTy};
15use rustc_session::config::CrateType;
16use rustc_span::{Span, Symbol};
17use rustc_symbol_mangling::mangle_internal_symbol;
18use rustc_target::spec::Os;
19
20use crate::*;
21
22fn try_resolve_did(tcx: TyCtxt<'_>, path: &[&str], namespace: Option<Namespace>) -> Option<DefId> {
26 let _trace = enter_trace_span!("try_resolve_did", ?path);
27
28 fn find_children<'tcx: 'a, 'a>(
30 tcx: TyCtxt<'tcx>,
31 item: DefId,
32 name: &'a str,
33 ) -> impl Iterator<Item = DefId> + 'a {
34 let name = Symbol::intern(name);
35 tcx.module_children(item)
36 .iter()
37 .filter(move |item| item.ident.name == name)
38 .map(move |item| item.res.def_id())
39 }
40
41 let (&crate_name, path) = path.split_first().expect("paths must have at least one segment");
43 let (modules, item) = if let Some(namespace) = namespace {
44 let (&item_name, modules) =
45 path.split_last().expect("non-module paths must have at least 2 segments");
46 (modules, Some((item_name, namespace)))
47 } else {
48 (path, None)
49 };
50
51 'crates: for krate in
56 tcx.crates(()).iter().filter(|&&krate| tcx.crate_name(krate).as_str() == crate_name)
57 {
58 let mut cur_item = DefId { krate: *krate, index: CRATE_DEF_INDEX };
59 for &segment in modules {
61 let Some(next_item) = find_children(tcx, cur_item, segment)
62 .find(|&item| tcx.def_kind(item) == DefKind::Mod)
63 else {
64 continue 'crates;
65 };
66 cur_item = next_item;
67 }
68 match item {
70 Some((item_name, namespace)) => {
71 let Some(item) = find_children(tcx, cur_item, item_name)
72 .find(|&item| tcx.def_kind(item).ns() == Some(namespace))
73 else {
74 continue 'crates;
75 };
76 return Some(item);
77 }
78 None => {
79 return Some(cur_item);
81 }
82 }
83 }
84 None
86}
87
88pub fn try_resolve_path<'tcx>(
90 tcx: TyCtxt<'tcx>,
91 path: &[&str],
92 namespace: Namespace,
93) -> Option<ty::Instance<'tcx>> {
94 let did = try_resolve_did(tcx, path, Some(namespace))?;
95 Some(ty::Instance::mono(tcx, did))
96}
97
98#[track_caller]
100pub fn resolve_path<'tcx>(
101 tcx: TyCtxt<'tcx>,
102 path: &[&str],
103 namespace: Namespace,
104) -> ty::Instance<'tcx> {
105 try_resolve_path(tcx, path, namespace)
106 .unwrap_or_else(|| panic!("failed to find required Rust item: {path:?}"))
107}
108
109#[track_caller]
111pub fn path_ty_layout<'tcx>(cx: &impl LayoutOf<'tcx>, path: &[&str]) -> TyAndLayout<'tcx> {
112 let ty = resolve_path(cx.tcx(), path, Namespace::TypeNS).ty(cx.tcx(), cx.typing_env());
113 cx.layout_of(ty).to_result().ok().unwrap()
114}
115
116pub fn iter_exported_symbols<'tcx>(
118 tcx: TyCtxt<'tcx>,
119 mut f: impl FnMut(CrateNum, DefId) -> InterpResult<'tcx>,
120) -> InterpResult<'tcx> {
121 let crate_items = tcx.hir_crate_items(());
125 for def_id in crate_items.definitions() {
126 let exported = tcx.def_kind(def_id).has_codegen_attrs() && {
127 let codegen_attrs = tcx.codegen_fn_attrs(def_id);
128 codegen_attrs.contains_extern_indicator()
129 || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER)
130 || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
131 };
132 let exported_mono = exported && {
135 let generics = tcx.generics_of(def_id);
136 !generics.requires_monomorphization(tcx)
137 };
138 if exported_mono {
139 f(LOCAL_CRATE, def_id.into())?;
140 }
141 }
142
143 let dependency_formats = tcx.dependency_formats(());
148 let dependency_format = dependency_formats
150 .get(&CrateType::Executable)
151 .expect("interpreting a non-executable crate");
152 for cnum in dependency_format
153 .iter_enumerated()
154 .filter_map(|(num, &linkage)| (linkage != Linkage::NotLinked).then_some(num))
155 {
156 if cnum == LOCAL_CRATE {
157 continue; }
159
160 for &(symbol, _export_info) in tcx.exported_non_generic_symbols(cnum) {
163 if let ExportedSymbol::NonGeneric(def_id) = symbol {
164 f(cnum, def_id)?;
165 }
166 }
167 }
168 interp_ok(())
169}
170
171impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
172pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
173 fn have_module(&self, path: &[&str]) -> bool {
175 try_resolve_did(*self.eval_context_ref().tcx, path, None).is_some()
176 }
177
178 fn eval_path(&self, path: &[&str]) -> MPlaceTy<'tcx> {
180 let this = self.eval_context_ref();
181 let instance = resolve_path(*this.tcx, path, Namespace::ValueNS);
182 this.eval_global(instance).unwrap_or_else(|err| {
184 panic!("failed to evaluate required Rust item: {path:?}\n{err:?}")
185 })
186 }
187 fn eval_path_scalar(&self, path: &[&str]) -> Scalar {
188 let this = self.eval_context_ref();
189 let val = this.eval_path(path);
190 this.read_scalar(&val)
191 .unwrap_or_else(|err| panic!("failed to read required Rust item: {path:?}\n{err:?}"))
192 }
193
194 fn eval_libc(&self, name: &str) -> Scalar {
196 if self.eval_context_ref().tcx.sess.target.os == Os::Windows {
197 panic!(
198 "`libc` crate is not reliably available on Windows targets; Miri should not use it there"
199 );
200 }
201 self.eval_path_scalar(&["libc", name])
202 }
203
204 fn eval_libc_i32(&self, name: &str) -> i32 {
206 self.eval_libc(name).to_i32().unwrap_or_else(|_err| {
208 panic!("required libc item has unexpected type (not `i32`): {name}")
209 })
210 }
211
212 fn eval_libc_u32(&self, name: &str) -> u32 {
214 self.eval_libc(name).to_u32().unwrap_or_else(|_err| {
216 panic!("required libc item has unexpected type (not `u32`): {name}")
217 })
218 }
219
220 fn eval_libc_u64(&self, name: &str) -> u64 {
222 self.eval_libc(name).to_u64().unwrap_or_else(|_err| {
224 panic!("required libc item has unexpected type (not `u64`): {name}")
225 })
226 }
227
228 fn eval_windows(&self, module: &str, name: &str) -> Scalar {
230 self.eval_context_ref().eval_path_scalar(&["std", "sys", "pal", "windows", module, name])
231 }
232
233 fn eval_windows_u32(&self, module: &str, name: &str) -> u32 {
235 self.eval_windows(module, name).to_u32().unwrap_or_else(|_err| {
237 panic!("required Windows item has unexpected type (not `u32`): {module}::{name}")
238 })
239 }
240
241 fn eval_windows_u64(&self, module: &str, name: &str) -> u64 {
243 self.eval_windows(module, name).to_u64().unwrap_or_else(|_err| {
245 panic!("required Windows item has unexpected type (not `u64`): {module}::{name}")
246 })
247 }
248
249 fn libc_ty_layout(&self, name: &str) -> TyAndLayout<'tcx> {
251 let this = self.eval_context_ref();
252 if this.tcx.sess.target.os == Os::Windows {
253 panic!(
254 "`libc` crate is not reliably available on Windows targets; Miri should not use it there"
255 );
256 }
257 path_ty_layout(this, &["libc", name])
258 }
259
260 fn windows_ty_layout(&self, name: &str) -> TyAndLayout<'tcx> {
262 let this = self.eval_context_ref();
263 path_ty_layout(this, &["std", "sys", "pal", "windows", "c", name])
264 }
265
266 fn libc_array_ty_layout(&self, name: &str, size: u64) -> TyAndLayout<'tcx> {
268 let this = self.eval_context_ref();
269 let elem_ty_layout = this.libc_ty_layout(name);
270 let array_ty = Ty::new_array(*this.tcx, elem_ty_layout.ty, size);
271 this.layout_of(array_ty).unwrap()
272 }
273
274 fn try_project_field_named<P: Projectable<'tcx, Provenance>>(
276 &self,
277 base: &P,
278 name: &str,
279 ) -> InterpResult<'tcx, Option<P>> {
280 let this = self.eval_context_ref();
281 let adt = base.layout().ty.ty_adt_def().unwrap();
282 for (idx, field) in adt.non_enum_variant().fields.iter_enumerated() {
283 if field.name.as_str() == name {
284 return interp_ok(Some(this.project_field(base, idx)?));
285 }
286 }
287 interp_ok(None)
288 }
289
290 fn project_field_named<P: Projectable<'tcx, Provenance>>(
292 &self,
293 base: &P,
294 name: &str,
295 ) -> InterpResult<'tcx, P> {
296 interp_ok(
297 self.try_project_field_named(base, name)?
298 .unwrap_or_else(|| bug!("no field named {} in type {}", name, base.layout().ty)),
299 )
300 }
301
302 fn write_int(
306 &mut self,
307 i: impl Into<i128>,
308 dest: &impl Writeable<'tcx, Provenance>,
309 ) -> InterpResult<'tcx> {
310 assert!(
311 dest.layout().backend_repr.is_scalar(),
312 "write_int on non-scalar type {}",
313 dest.layout().ty
314 );
315 let val = if dest.layout().backend_repr.is_signed() {
316 Scalar::from_int(i, dest.layout().size)
317 } else {
318 Scalar::from_uint(u128::try_from(i.into()).unwrap(), dest.layout().size)
320 };
321 self.eval_context_mut().write_scalar(val, dest)
322 }
323
324 fn write_int_fields(
326 &mut self,
327 values: &[i128],
328 dest: &impl Writeable<'tcx, Provenance>,
329 ) -> InterpResult<'tcx> {
330 let this = self.eval_context_mut();
331 for (idx, &val) in values.iter().enumerate() {
332 let idx = FieldIdx::from_usize(idx);
333 let field = this.project_field(dest, idx)?;
334 this.write_int(val, &field)?;
335 }
336 interp_ok(())
337 }
338
339 fn write_int_fields_named(
341 &mut self,
342 values: &[(&str, i128)],
343 dest: &impl Writeable<'tcx, Provenance>,
344 ) -> InterpResult<'tcx> {
345 let this = self.eval_context_mut();
346 for &(name, val) in values.iter() {
347 let field = this.project_field_named(dest, name)?;
348 this.write_int(val, &field)?;
349 }
350 interp_ok(())
351 }
352
353 fn write_null(&mut self, dest: &impl Writeable<'tcx, Provenance>) -> InterpResult<'tcx> {
355 self.write_int(0, dest)
356 }
357
358 fn ptr_is_null(&self, ptr: Pointer) -> InterpResult<'tcx, bool> {
360 interp_ok(ptr.addr().bytes() == 0)
361 }
362
363 fn gen_random(&mut self, ptr: Pointer, len: u64) -> InterpResult<'tcx> {
365 if len == 0 {
371 return interp_ok(());
372 }
373 let this = self.eval_context_mut();
374
375 let mut data = vec![0; usize::try_from(len).unwrap()];
376
377 if this.machine.communicate() {
378 getrandom::fill(&mut data)
380 .map_err(|err| err_unsup_format!("host getrandom failed: {}", err))?;
381 } else {
382 let rng = this.machine.rng.get_mut();
383 rng.fill_bytes(&mut data);
384 }
385
386 this.write_bytes_ptr(ptr, data.iter().copied())
387 }
388
389 fn call_function(
395 &mut self,
396 f: ty::Instance<'tcx>,
397 caller_abi: ExternAbi,
398 args: &[ImmTy<'tcx>],
399 dest: Option<&MPlaceTy<'tcx>>,
400 cont: ReturnContinuation,
401 ) -> InterpResult<'tcx> {
402 let this = self.eval_context_mut();
403
404 let mir = this.load_mir(f.def, None)?;
406 let dest = match dest {
407 Some(dest) => dest.clone(),
408 None => MPlaceTy::fake_alloc_zst(this.machine.layouts.unit),
409 };
410
411 let sig = this.tcx.mk_fn_sig(
413 args.iter().map(|a| a.layout.ty),
414 dest.layout.ty,
415 FnSigKind::default().set_abi(caller_abi).set_safety(rustc_hir::Safety::Safe),
418 );
419 let caller_fn_abi = this.fn_abi_of_fn_ptr(ty::Binder::dummy(sig), ty::List::empty())?;
420
421 this.init_stack_frame(
423 f,
424 mir,
425 caller_fn_abi,
426 &args.iter().map(|a| FnArg::Copy(a.clone().into())).collect::<Vec<_>>(),
427 false,
428 &dest.into(),
429 cont,
430 )
431 }
432
433 fn call_thread_root_function(
435 &mut self,
436 f: ty::Instance<'tcx>,
437 caller_abi: ExternAbi,
438 args: &[ImmTy<'tcx>],
439 dest: Option<&MPlaceTy<'tcx>>,
440 span: Span,
441 ) -> InterpResult<'tcx> {
442 let this = self.eval_context_mut();
443 assert!(this.active_thread_stack().is_empty());
444 assert!(this.active_thread_ref().origin_span.is_dummy());
445 this.active_thread_mut().origin_span = span;
446 this.call_function(f, caller_abi, args, dest, ReturnContinuation::Stop { cleanup: true })
447 }
448
449 fn visit_freeze_sensitive(
453 &self,
454 place: &MPlaceTy<'tcx>,
455 size: Size,
456 mut action: impl FnMut(AllocRange, bool) -> InterpResult<'tcx>,
457 ) -> InterpResult<'tcx> {
458 let this = self.eval_context_ref();
459 trace!("visit_frozen(place={:?}, size={:?})", *place, size);
460 debug_assert_eq!(
461 size,
462 this.size_and_align_of_val(place)?
463 .map(|(size, _)| size)
464 .unwrap_or_else(|| place.layout.size)
465 );
466 let start_addr = place.ptr().addr();
470 let mut cur_addr = start_addr;
471 let mut unsafe_cell_action = |unsafe_cell_ptr: &Pointer, unsafe_cell_size: Size| {
474 let unsafe_cell_addr = unsafe_cell_ptr.addr();
477 assert!(unsafe_cell_addr >= cur_addr);
478 let frozen_size = unsafe_cell_addr - cur_addr;
479 if frozen_size != Size::ZERO {
481 action(alloc_range(cur_addr - start_addr, frozen_size), true)?;
482 }
483 cur_addr += frozen_size;
484 if unsafe_cell_size != Size::ZERO {
486 action(
487 alloc_range(cur_addr - start_addr, unsafe_cell_size),
488 false,
489 )?;
490 }
491 cur_addr += unsafe_cell_size;
492 interp_ok(())
494 };
495 {
497 let mut visitor = UnsafeCellVisitor {
498 ecx: this,
499 unsafe_cell_action: |place| {
500 trace!("unsafe_cell_action on {:?}", place.ptr());
501 let unsafe_cell_size = this
503 .size_and_align_of_val(place)?
504 .map(|(size, _)| size)
505 .unwrap_or_else(|| place.layout.size);
507 if unsafe_cell_size != Size::ZERO {
509 unsafe_cell_action(&place.ptr(), unsafe_cell_size)
510 } else {
511 interp_ok(())
512 }
513 },
514 };
515 visitor.visit_value(place)?;
516 }
517 unsafe_cell_action(&place.ptr().wrapping_offset(size, this), Size::ZERO)?;
520 return interp_ok(());
522
523 struct UnsafeCellVisitor<'ecx, 'tcx, F>
526 where
527 F: FnMut(&MPlaceTy<'tcx>) -> InterpResult<'tcx>,
528 {
529 ecx: &'ecx MiriInterpCx<'tcx>,
530 unsafe_cell_action: F,
531 }
532
533 impl<'ecx, 'tcx, F> ValueVisitor<'tcx, MiriMachine<'tcx>> for UnsafeCellVisitor<'ecx, 'tcx, F>
534 where
535 F: FnMut(&MPlaceTy<'tcx>) -> InterpResult<'tcx>,
536 {
537 type V = MPlaceTy<'tcx>;
538
539 #[inline(always)]
540 fn ecx(&self) -> &MiriInterpCx<'tcx> {
541 self.ecx
542 }
543
544 fn visit_value(&mut self, v: &MPlaceTy<'tcx>) -> InterpResult<'tcx> {
546 trace!("UnsafeCellVisitor: {:?} {:?}", *v, v.layout.ty);
547 let is_unsafe_cell = match v.layout.ty.kind() {
548 ty::Adt(adt, _) =>
549 Some(adt.did()) == self.ecx.tcx.lang_items().unsafe_cell_type(),
550 _ => false,
551 };
552 if is_unsafe_cell {
553 (self.unsafe_cell_action)(v)
555 } else if self.ecx.type_is_freeze(v.layout.ty) {
556 interp_ok(())
558 } else if matches!(v.layout.fields, FieldsShape::Union(..)) {
559 (self.unsafe_cell_action)(v)
561 } else {
562 match v.layout.variants {
569 Variants::Multiple { .. } => {
570 (self.unsafe_cell_action)(v)
578 }
579 Variants::Single { .. } | Variants::Empty => {
580 self.walk_value(v)
583 }
584 }
585 }
586 }
587
588 fn visit_union(
589 &mut self,
590 _v: &MPlaceTy<'tcx>,
591 _fields: NonZero<usize>,
592 ) -> InterpResult<'tcx> {
593 bug!("we should have already handled unions in `visit_value`")
594 }
595 }
596 }
597
598 fn check_no_isolation(&self, name: &str) -> InterpResult<'tcx> {
602 if !self.eval_context_ref().machine.communicate() {
603 self.reject_in_isolation(name, RejectOpWith::Abort)?;
604 }
605 interp_ok(())
606 }
607
608 fn reject_in_isolation(&self, op_name: &str, reject_with: RejectOpWith) -> InterpResult<'tcx> {
611 let this = self.eval_context_ref();
612 match reject_with {
613 RejectOpWith::Abort => isolation_abort_error(op_name),
614 RejectOpWith::WarningWithoutBacktrace => {
615 static DEDUP: Mutex<FxHashSet<String>> =
617 Mutex::new(FxHashSet::with_hasher(FxBuildHasher));
618 let mut emitted_warnings = DEDUP.lock().unwrap();
619 if !emitted_warnings.contains(op_name) {
620 emitted_warnings.insert(op_name.to_owned());
622 this.tcx
623 .dcx()
624 .warn(format!("{op_name} was made to return an error due to isolation"));
625 }
626
627 interp_ok(())
628 }
629 RejectOpWith::Warning => {
630 this.emit_diagnostic(NonHaltingDiagnostic::RejectedIsolatedOp(op_name.to_string()));
631 interp_ok(())
632 }
633 RejectOpWith::NoWarning => interp_ok(()), }
635 }
636
637 fn assert_target_os(&self, target_os: Os, name: &str) {
641 assert_eq!(
642 self.eval_context_ref().tcx.sess.target.os,
643 target_os,
644 "`{name}` is only available on the `{target_os}` target OS",
645 )
646 }
647
648 fn check_target_os(&self, target_oses: &[Os], name: Symbol) -> InterpResult<'tcx> {
652 let target_os = &self.eval_context_ref().tcx.sess.target.os;
653 if !target_oses.contains(target_os) {
654 throw_unsup_format!("`{name}` is not supported on {target_os}");
655 }
656 interp_ok(())
657 }
658
659 fn assert_target_os_is_unix(&self, name: &str) {
663 assert!(self.target_os_is_unix(), "`{name}` is only available for unix targets",);
664 }
665
666 fn target_os_is_unix(&self) -> bool {
667 self.eval_context_ref().tcx.sess.target.families.iter().any(|f| f == "unix")
668 }
669
670 fn deref_pointer_as(
672 &self,
673 op: &impl Projectable<'tcx, Provenance>,
674 layout: TyAndLayout<'tcx>,
675 ) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
676 let this = self.eval_context_ref();
677 let ptr = this.read_pointer(op)?;
678 interp_ok(this.ptr_to_mplace(ptr, layout))
679 }
680
681 fn deref_pointer_and_offset(
683 &self,
684 op: &impl Projectable<'tcx, Provenance>,
685 offset: u64,
686 base_layout: TyAndLayout<'tcx>,
687 value_layout: TyAndLayout<'tcx>,
688 ) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
689 let this = self.eval_context_ref();
690 let op_place = this.deref_pointer_as(op, base_layout)?;
691 let offset = Size::from_bytes(offset);
692
693 assert!(base_layout.size >= offset + value_layout.size);
695 let value_place = op_place.offset(offset, value_layout, this)?;
696 interp_ok(value_place)
697 }
698
699 fn deref_pointer_and_read(
700 &self,
701 op: &impl Projectable<'tcx, Provenance>,
702 offset: u64,
703 base_layout: TyAndLayout<'tcx>,
704 value_layout: TyAndLayout<'tcx>,
705 ) -> InterpResult<'tcx, Scalar> {
706 let this = self.eval_context_ref();
707 let value_place = this.deref_pointer_and_offset(op, offset, base_layout, value_layout)?;
708 this.read_scalar(&value_place)
709 }
710
711 fn deref_pointer_and_write(
712 &mut self,
713 op: &impl Projectable<'tcx, Provenance>,
714 offset: u64,
715 value: impl Into<Scalar>,
716 base_layout: TyAndLayout<'tcx>,
717 value_layout: TyAndLayout<'tcx>,
718 ) -> InterpResult<'tcx, ()> {
719 let this = self.eval_context_mut();
720 let value_place = this.deref_pointer_and_offset(op, offset, base_layout, value_layout)?;
721 this.write_scalar(value, &value_place)
722 }
723
724 fn read_byte_slice<'a>(&'a self, slice: &ImmTy<'tcx>) -> InterpResult<'tcx, &'a [u8]>
726 where
727 'tcx: 'a,
728 {
729 let this = self.eval_context_ref();
730 let (ptr, len) = slice.to_scalar_pair();
731 let ptr = ptr.to_pointer(this)?;
732 let len = len.to_target_usize(this)?;
733 let bytes = this.read_bytes_ptr_strip_provenance(ptr, Size::from_bytes(len))?;
734 interp_ok(bytes)
735 }
736
737 fn read_c_str<'a>(&'a self, ptr: Pointer) -> InterpResult<'tcx, &'a [u8]>
739 where
740 'tcx: 'a,
741 {
742 let this = self.eval_context_ref();
743 let size1 = Size::from_bytes(1);
744
745 let mut len = Size::ZERO;
747 loop {
748 let alloc = this.get_ptr_alloc(ptr.wrapping_offset(len, this), size1)?.unwrap(); let byte = alloc.read_integer(alloc_range(Size::ZERO, size1))?.to_u8()?;
752 if byte == 0 {
753 break;
754 } else {
755 len += size1;
756 }
757 }
758
759 this.read_bytes_ptr_strip_provenance(ptr, len)
761 }
762
763 fn write_c_str(
769 &mut self,
770 c_str: &[u8],
771 ptr: Pointer,
772 size: u64,
773 ) -> InterpResult<'tcx, (bool, u64)> {
774 let string_length = u64::try_from(c_str.len()).unwrap();
777 let string_length = string_length.strict_add(1);
778 if size < string_length {
779 return interp_ok((false, string_length));
780 }
781 self.eval_context_mut()
782 .write_bytes_ptr(ptr, c_str.iter().copied().chain(iter::once(0u8)))?;
783 interp_ok((true, string_length))
784 }
785
786 fn read_c_str_with_char_size<T>(
789 &self,
790 mut ptr: Pointer,
791 size: Size,
792 align: Align,
793 ) -> InterpResult<'tcx, Vec<T>>
794 where
795 T: TryFrom<u128>,
796 <T as TryFrom<u128>>::Error: std::fmt::Debug,
797 {
798 assert_ne!(size, Size::ZERO);
799
800 let this = self.eval_context_ref();
801
802 this.check_ptr_align(ptr, align)?;
803
804 let mut wchars = Vec::new();
805 loop {
806 let alloc = this.get_ptr_alloc(ptr, size)?.unwrap(); let wchar_int = alloc.read_integer(alloc_range(Size::ZERO, size))?.to_bits(size)?;
810 if wchar_int == 0 {
811 break;
812 } else {
813 wchars.push(wchar_int.try_into().unwrap());
814 ptr = ptr.wrapping_offset(size, this);
815 }
816 }
817
818 interp_ok(wchars)
819 }
820
821 fn read_wide_str(&self, ptr: Pointer) -> InterpResult<'tcx, Vec<u16>> {
823 self.read_c_str_with_char_size(ptr, Size::from_bytes(2), Align::from_bytes(2).unwrap())
824 }
825
826 fn write_wide_str(
833 &mut self,
834 wide_str: &[u16],
835 ptr: Pointer,
836 size: u64,
837 ) -> InterpResult<'tcx, (bool, u64)> {
838 let string_length = u64::try_from(wide_str.len()).unwrap();
841 let string_length = string_length.strict_add(1);
842 if size < string_length {
843 return interp_ok((false, string_length));
844 }
845
846 let size2 = Size::from_bytes(2);
848 let this = self.eval_context_mut();
849 this.check_ptr_align(ptr, Align::from_bytes(2).unwrap())?;
850 let mut alloc = this.get_ptr_alloc_mut(ptr, size2 * string_length)?.unwrap(); for (offset, wchar) in wide_str.iter().copied().chain(iter::once(0x0000)).enumerate() {
852 let offset = u64::try_from(offset).unwrap();
853 alloc.write_scalar(alloc_range(size2 * offset, size2), Scalar::from_u16(wchar))?;
854 }
855 interp_ok((true, string_length))
856 }
857
858 fn read_wchar_t_str(&self, ptr: Pointer) -> InterpResult<'tcx, Vec<u32>> {
861 let this = self.eval_context_ref();
862 let wchar_t = if this.tcx.sess.target.os == Os::Windows {
863 this.machine.layouts.u16
865 } else {
866 this.libc_ty_layout("wchar_t")
867 };
868 self.read_c_str_with_char_size(ptr, wchar_t.size, wchar_t.align.abi)
869 }
870
871 fn frame_in_std(&self) -> bool {
872 let this = self.eval_context_ref();
873 let frame = this.frame();
874 let instance: Option<_> = try {
876 let scope = frame.current_source_info()?.scope;
877 let inlined_parent = frame.body().source_scopes[scope].inlined_parent_scope?;
878 let source = &frame.body().source_scopes[inlined_parent];
879 source.inlined.expect("inlined_parent_scope points to scope without inline info").0
880 };
881 let instance = instance.unwrap_or(frame.instance());
883 let frame_crate = this.tcx.def_path(instance.def_id()).krate;
888 let crate_name = this.tcx.crate_name(frame_crate);
889 let crate_name = crate_name.as_str();
890 crate_name == "std"
891 }
892
893 fn mark_immutable(&mut self, mplace: &MPlaceTy<'tcx>) {
895 let this = self.eval_context_mut();
896 let provenance = mplace.ptr().into_pointer_or_addr().unwrap().provenance;
898 this.alloc_mark_immutable(provenance.get_alloc_id().unwrap()).unwrap();
899 }
900
901 fn get_twice_wide_int_ty(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
903 let this = self.eval_context_ref();
904 match ty.kind() {
905 ty::Uint(UintTy::U8) => this.tcx.types.u16,
907 ty::Uint(UintTy::U16) => this.tcx.types.u32,
908 ty::Uint(UintTy::U32) => this.tcx.types.u64,
909 ty::Uint(UintTy::U64) => this.tcx.types.u128,
910 ty::Int(IntTy::I8) => this.tcx.types.i16,
912 ty::Int(IntTy::I16) => this.tcx.types.i32,
913 ty::Int(IntTy::I32) => this.tcx.types.i64,
914 ty::Int(IntTy::I64) => this.tcx.types.i128,
915 _ => span_bug!(this.cur_span(), "unexpected type: {ty:?}"),
916 }
917 }
918
919 fn expect_target_feature_for_intrinsic(
924 &self,
925 intrinsic: Symbol,
926 target_feature: &str,
927 ) -> InterpResult<'tcx, ()> {
928 let this = self.eval_context_ref();
929 if !this.tcx.sess.unstable_target_features.contains(&Symbol::intern(target_feature)) {
930 throw_ub_format!(
931 "attempted to call intrinsic `{intrinsic}` that requires missing target feature {target_feature}"
932 );
933 }
934 interp_ok(())
935 }
936
937 fn lookup_link_section(
940 &mut self,
941 include_name: impl Fn(&str) -> bool,
942 ) -> InterpResult<'tcx, Vec<(ImmTy<'tcx>, Span)>> {
943 let this = self.eval_context_mut();
944 let tcx = this.tcx.tcx;
945
946 let mut array = vec![];
947
948 iter_exported_symbols(tcx, |_cnum, def_id| {
949 let attrs = tcx.codegen_fn_attrs(def_id);
950 let Some(link_section) = attrs.link_section else {
951 return interp_ok(());
952 };
953 if include_name(link_section.as_str()) {
954 let instance = ty::Instance::mono(tcx, def_id);
955 let span = tcx.def_span(def_id);
956 let const_val = this.eval_global(instance).unwrap_or_else(|err| {
957 panic!(
958 "failed to evaluate static in required link_section: {def_id:?}\n{err:?}"
959 )
960 });
961 match const_val.layout.ty.kind() {
962 ty::FnPtr(..) => {
963 array.push((this.read_immediate(&const_val)?, span));
964 }
965 ty::Array(elem_ty, _) if matches!(elem_ty.kind(), ty::FnPtr(..)) => {
966 let mut elems = this.project_array_fields(&const_val)?;
967 while let Some((_idx, elem)) = elems.next(this)? {
968 array.push((this.read_immediate(&elem)?, span));
969 }
970 }
971 _ =>
972 throw_unsup_format!(
973 "only function pointers and arrays of function pointers are supported in well-known linker sections"
974 ),
975 }
976 }
977 interp_ok(())
978 })?;
979
980 interp_ok(array)
981 }
982
983 fn mangle_internal_symbol<'a>(&'a mut self, name: &'static str) -> &'a str
984 where
985 'tcx: 'a,
986 {
987 let this = self.eval_context_mut();
988 let tcx = *this.tcx;
989 this.machine
990 .mangle_internal_symbol_cache
991 .entry(name)
992 .or_insert_with(|| mangle_internal_symbol(tcx, name))
993 }
994}
995
996impl<'tcx> MiriMachine<'tcx> {
997 pub fn current_user_relevant_span(&self) -> Span {
1002 self.threads.active_thread_ref().current_user_relevant_span()
1003 }
1004
1005 pub fn caller_span(&self) -> Span {
1011 let frame_idx = self.top_user_relevant_frame().unwrap();
1014 let frame_idx = cmp::min(frame_idx, self.stack().len().saturating_sub(2));
1015 self.stack()[frame_idx].current_span()
1016 }
1017
1018 fn stack(&self) -> &[Frame<'tcx, Provenance, machine::FrameExtra<'tcx>>] {
1019 self.threads.active_thread_stack()
1020 }
1021
1022 fn top_user_relevant_frame(&self) -> Option<usize> {
1023 self.threads.active_thread_ref().top_user_relevant_frame()
1024 }
1025
1026 pub fn user_relevance(&self, frame: &Frame<'tcx, Provenance>) -> u8 {
1028 if frame.instance().def.requires_caller_location(self.tcx) {
1029 return 0;
1030 }
1031 if self.is_local(frame.instance()) {
1032 u8::MAX
1033 } else {
1034 1
1037 }
1038 }
1039}
1040
1041pub fn isolation_abort_error<'tcx>(name: &str) -> InterpResult<'tcx> {
1042 throw_machine_stop!(TerminationInfo::UnsupportedInIsolation(format!(
1043 "{name} not available when isolation is enabled",
1044 )))
1045}
1046
1047pub(crate) fn bool_to_simd_element(b: bool, size: Size) -> Scalar {
1048 let val = if b { -1 } else { 0 };
1052 Scalar::from_int(val, size)
1053}
1054
1055pub(crate) fn windows_check_buffer_size((success, len): (bool, u64)) -> u32 {
1059 if success {
1060 u32::try_from(len.strict_sub(1)).unwrap()
1063 } else {
1064 u32::try_from(len).unwrap()
1067 }
1068}
1069
1070pub fn is_no_core(tcx: TyCtxt<'_>) -> bool {
1072 rustc_hir::find_attr!(tcx, crate, NoCore)
1073}
1074
1075pub trait ToUsize {
1077 fn to_usize(self) -> usize;
1078}
1079
1080impl ToUsize for u32 {
1081 fn to_usize(self) -> usize {
1082 self.try_into().unwrap()
1083 }
1084}
1085
1086pub trait ToU64 {
1089 fn to_u64(self) -> u64;
1090}
1091
1092impl ToU64 for usize {
1093 fn to_u64(self) -> u64 {
1094 self.try_into().unwrap()
1095 }
1096}
1097
1098#[macro_export]
1104macro_rules! enter_trace_span {
1105 ($($tt:tt)*) => {
1106 rustc_const_eval::enter_trace_span!($crate::MiriMachine<'static>, $($tt)*)
1107 };
1108}