]> git.lizzy.rs Git - rust.git/commitdiff
Ignore integers
authorChristian Poveda <christianpoveda@protonmail.com>
Mon, 23 Sep 2019 02:39:17 +0000 (21:39 -0500)
committerChristian Poveda <christianpoveda@protonmail.com>
Mon, 23 Sep 2019 02:39:17 +0000 (21:39 -0500)
src/shims/mod.rs
tests/run-pass/integer_align_offset.rs [deleted file]

index 0b5bd7ae5cf0dba98450ad355d34042453db0327..4ccdbdc0d7c31b772498b61fda9d4fd777431429 100644 (file)
@@ -1,10 +1,10 @@
+pub mod dlsym;
+pub mod env;
 pub mod foreign_items;
 pub mod intrinsics;
 pub mod tls;
-pub mod dlsym;
-pub mod env;
 
-use rustc::{ty, mir};
+use rustc::{mir, ty};
 
 use crate::*;
 
@@ -18,7 +18,11 @@ fn find_fn(
         ret: Option<mir::BasicBlock>,
     ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
         let this = self.eval_context_mut();
-        trace!("eval_fn_call: {:#?}, {:?}", instance, dest.map(|place| *place));
+        trace!(
+            "eval_fn_call: {:#?}, {:?}",
+            instance,
+            dest.map(|place| *place)
+        );
 
         // First, run the common hooks also supported by CTFE.
         if this.hook_fn(instance, args, dest)? {
@@ -28,7 +32,9 @@ fn find_fn(
         // There are some more lang items we want to hook that CTFE does not hook (yet).
         if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
             let dest = dest.unwrap();
-            let n = this.align_offset(args[0], args[1], dest.layout)?;
+            let n = this
+                .align_offset(args[0], args[1])?
+                .unwrap_or_else(|| this.truncate(u128::max_value(), dest.layout));
             this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;
             this.goto_block(ret)?;
             return Ok(None);
@@ -51,13 +57,12 @@ fn align_offset(
         &mut self,
         ptr_op: OpTy<'tcx, Tag>,
         align_op: OpTy<'tcx, Tag>,
-        layout: ty::layout::TyLayout<'tcx>,
-    ) -> InterpResult<'tcx, u128> {
+    ) -> InterpResult<'tcx, Option<u128>> {
         let this = self.eval_context_mut();
 
         let req_align = this.force_bits(
             this.read_scalar(align_op)?.not_undef()?,
-            this.pointer_size()
+            this.pointer_size(),
         )? as usize;
 
         // FIXME: This should actually panic in the interpreted program
@@ -67,18 +72,19 @@ fn align_offset(
 
         let ptr_scalar = this.read_scalar(ptr_op)?.not_undef()?;
 
-        if let Scalar::Ptr(ptr) = ptr_scalar {
+        if let Ok(ptr) = this.force_ptr(ptr_scalar) {
             let cur_align = this.memory().get(ptr.alloc_id)?.align.bytes() as usize;
-            if cur_align < req_align {
-                return Ok(this.truncate(u128::max_value(), layout));
+            if cur_align >= req_align {
+                // if the allocation alignment is at least the required alignment we use the
+                // libcore implementation
+                return Ok(Some(
+                    (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
+                        .align_offset(req_align) as u128,
+                ));
             }
         }
-
-        // if the allocation alignment is at least the required alignment or if the pointer is an
-        // integer, we use the libcore implementation
-        Ok(
-            (this.force_bits(ptr_scalar, this.pointer_size())? as *const i8)
-            .align_offset(req_align) as u128
-        )
+        // If the allocation alignment is smaller than then required alignment or the pointer was
+        // actually an integer, we return `None`
+        Ok(None)
     }
 }
diff --git a/tests/run-pass/integer_align_offset.rs b/tests/run-pass/integer_align_offset.rs
deleted file mode 100644 (file)
index 971c19b..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-fn main() {
-    assert_eq!(2, (2 as *const i8).align_offset(4));
-}