+ }
+ None
+ })
+}
+
+pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
+ /// Gets an instance for a path.
+ fn resolve_path(&self, path: &[&str]) -> ty::Instance<'tcx> {
+ let did = try_resolve_did(self.eval_context_ref().tcx.tcx, path)
+ .unwrap_or_else(|| panic!("failed to find required Rust item: {:?}", path));
+ ty::Instance::mono(self.eval_context_ref().tcx.tcx, did)
+ }
+
+ /// Evaluates the scalar at the specified path. Returns Some(val)
+ /// if the path could be resolved, and None otherwise
+ fn eval_path_scalar(
+ &mut self,
+ path: &[&str],
+ ) -> InterpResult<'tcx, ScalarMaybeUndef<Tag>> {
+ let this = self.eval_context_mut();
+ let instance = this.resolve_path(path);
+ let cid = GlobalId { instance, promoted: None };
+ let const_val = this.const_eval_raw(cid)?;
+ let const_val = this.read_scalar(const_val.into())?;
+ return Ok(const_val);
+ }
+
+ /// Helper function to get a `libc` constant as a `Scalar`.
+ fn eval_libc(&mut self, name: &str) -> InterpResult<'tcx, Scalar<Tag>> {
+ self.eval_context_mut()
+ .eval_path_scalar(&["libc", name])?
+ .not_undef()
+ }
+
+ /// Helper function to get a `libc` constant as an `i32`.
+ fn eval_libc_i32(&mut self, name: &str) -> InterpResult<'tcx, i32> {
+ self.eval_libc(name)?.to_i32()
+ }
+
+ /// Helper function to get the `TyLayout` of a `libc` type
+ fn libc_ty_layout(&mut self, name: &str) -> InterpResult<'tcx, TyLayout<'tcx>> {
+ let this = self.eval_context_mut();
+ let ty = this.resolve_path(&["libc", name]).monomorphic_ty(*this.tcx);
+ this.layout_of(ty)