]> git.lizzy.rs Git - rust.git/commitdiff
implement proper panicking for other MIR assertions
authorRalf Jung <post@ralfj.de>
Fri, 29 Nov 2019 10:17:44 +0000 (11:17 +0100)
committerRalf Jung <post@ralfj.de>
Mon, 2 Dec 2019 15:04:31 +0000 (16:04 +0100)
Requires generalizing the call_function helper to arbitrary Immediate arguments

src/eval.rs
src/helpers.rs
src/machine.rs
src/shims/env.rs
src/shims/foreign_items.rs
src/shims/panic.rs
src/shims/tls.rs

index dbb14b7bb189a2ebd72cbf31cdad430b664bd883..f13ef89ff7fe5aa42243ea7c9e5ace479a633bed 100644 (file)
@@ -103,7 +103,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
         let argvs_place = ecx.allocate(argvs_layout, MiriMemoryKind::Env.into());
         for (idx, arg) in argvs.into_iter().enumerate() {
             let place = ecx.mplace_field(argvs_place, idx as u64)?;
-            ecx.write_scalar(Scalar::Ptr(arg), place.into())?;
+            ecx.write_scalar(arg, place.into())?;
         }
         ecx.memory
             .mark_immutable(argvs_place.ptr.assert_ptr().alloc_id)?;
@@ -149,7 +149,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
     // Call start function.
     ecx.call_function(
         start_instance,
-        &[main_ptr.into(), argc, argv],
+        &[main_ptr.into(), argc.into(), argv.into()],
         Some(ret_place.into()),
         StackPopCleanup::None { cleanup: true },
     )?;
index 28c31a07ef5796d49705ce7091892dfe6b2cc27f..68ce014c30689e41fe9475bb5f3af82578bbe6e7 100644 (file)
@@ -124,7 +124,7 @@ fn gen_random(
     fn call_function(
         &mut self,
         f: ty::Instance<'tcx>,
-        args: &[Scalar<Tag>],
+        args: &[Immediate<Tag>],
         dest: Option<PlaceTy<'tcx, Tag>>,
         stack_pop: StackPopCleanup,
     ) -> InterpResult<'tcx> {
@@ -146,7 +146,7 @@ fn call_function(
             let callee_arg = this.local_place(
                 callee_args.next().expect("callee has fewer arguments than expected")
             )?;
-            this.write_scalar(*arg, callee_arg)?;
+            this.write_immediate(*arg, callee_arg)?;
         }
         callee_args.next().expect_none("callee has more arguments than expected");
 
index 07864ac4ee468e90bb3da312c85a645a139dcc50..980c6115e91cdb044292c7016e370ead22c14378 100644 (file)
@@ -253,7 +253,7 @@ fn box_alloc(
         let malloc = ty::Instance::mono(ecx.tcx.tcx, malloc);
         ecx.call_function(
             malloc,
-            &[size, align],
+            &[size.into(), align.into()],
             Some(dest),
             // Don't do anything when we are done. The `statement()` function will increment
             // the old stack frame's stmt counter to the next statement, which means that when
index 44896fd9bbd563a22d33293fa7058f9a517e3e4e..ae8dae0313f1bfc42c25560f20fac4e05178b6e9 100644 (file)
@@ -57,7 +57,7 @@ fn getenv(&mut self, name_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, Scalar<Tag>
         Ok(match this.machine.env_vars.map.get(name) {
             // The offset is used to strip the "{name}=" part of the string.
             Some(var_ptr) => {
-                Scalar::Ptr(var_ptr.offset(Size::from_bytes(name.len() as u64 + 1), this)?)
+                Scalar::from(var_ptr.offset(Size::from_bytes(name.len() as u64 + 1), this)?)
             }
             None => Scalar::ptr_null(&*this.tcx),
         })
index 114931e435a4c6e982fbb116b387a6117ff409b3..f27b4fb333527df8ee5ebb09a1a12c1764a3ce19 100644 (file)
@@ -205,7 +205,7 @@ fn emulate_foreign_item(
                         Align::from_bytes(align).unwrap(),
                         MiriMemoryKind::C.into(),
                     );
-                    this.write_scalar(Scalar::Ptr(ptr), ret.into())?;
+                    this.write_scalar(ptr, ret.into())?;
                 }
                 this.write_null(dest)?;
             }
@@ -234,7 +234,7 @@ fn emulate_foreign_item(
                     Align::from_bytes(align).unwrap(),
                     MiriMemoryKind::Rust.into(),
                 );
-                this.write_scalar(Scalar::Ptr(ptr), dest)?;
+                this.write_scalar(ptr, dest)?;
             }
             "__rust_alloc_zeroed" => {
                 let size = this.read_scalar(args[0])?.to_machine_usize(this)?;
@@ -254,7 +254,7 @@ fn emulate_foreign_item(
                 this.memory
                     .write_bytes(ptr.into(), iter::repeat(0u8).take(size as usize))
                     .unwrap();
-                this.write_scalar(Scalar::Ptr(ptr), dest)?;
+                this.write_scalar(ptr, dest)?;
             }
             "__rust_dealloc" => {
                 let ptr = this.read_scalar(args[0])?.not_undef()?;
@@ -295,7 +295,7 @@ fn emulate_foreign_item(
                     align,
                     MiriMemoryKind::Rust.into(),
                 )?;
-                this.write_scalar(Scalar::Ptr(new_ptr), dest)?;
+                this.write_scalar(new_ptr, dest)?;
             }
 
             "syscall" => {
index 4b9cb6b41955dec3b0509b17b4db7df054a9a9d3..7cdd81d935c2eb976c7e08749af6ec26a4878618 100644 (file)
@@ -86,7 +86,7 @@ fn handle_catch_panic(
             MPlaceTy::dangling(this.layout_of(tcx.mk_unit())?, this).into();
         this.call_function(
             f_instance,
-            &[f_arg],
+            &[f_arg.into()],
             Some(ret_place),
             // Directly return to caller.
             StackPopCleanup::Goto { ret: Some(ret), unwind: None },
@@ -163,6 +163,8 @@ fn assert_panic(
 
         match msg {
             BoundsCheck { ref index, ref len } => {
+                // Forward to `panic_bounds_check` lang item.
+
                 // First arg: Caller location.
                 let location = this.alloc_caller_location_for_span(span)?;
                 // Second arg: index.
@@ -175,12 +177,31 @@ fn assert_panic(
                 let panic_bounds_check = ty::Instance::mono(this.tcx.tcx, panic_bounds_check);
                 this.call_function(
                     panic_bounds_check,
-                    &[location.ptr, index.not_undef()?, len.not_undef()?],
+                    &[location.ptr.into(), index.into(), len.into()],
+                    None,
+                    StackPopCleanup::Goto { ret: None, unwind },
+                )?;
+            }
+            _ => {
+                // Forward everything else to `panic` lang item.
+
+                // First arg: Message.
+                let msg = msg.description();
+                let msg = this.allocate_str(msg, MiriMemoryKind::Static.into());
+
+                // Second arg: Caller location.
+                let location = this.alloc_caller_location_for_span(span)?;
+
+                // Call the lang item.
+                let panic = this.tcx.lang_items().panic_fn().unwrap();
+                let panic = ty::Instance::mono(this.tcx.tcx, panic);
+                this.call_function(
+                    panic,
+                    &[msg.to_ref(), location.ptr.into()],
                     None,
                     StackPopCleanup::Goto { ret: None, unwind },
                 )?;
             }
-            _ => unimplemented!()
         }
         Ok(())
     }
index ea8b0df230f2041d90c5cad55e99675d226d97c7..420fd63a6181e22d57b88f30357555151a555838 100644 (file)
@@ -150,7 +150,7 @@ fn run_tls_dtors(&mut self) -> InterpResult<'tcx> {
             let ret_place = MPlaceTy::dangling(this.layout_of(this.tcx.mk_unit())?, this).into();
             this.call_function(
                 instance,
-                &[ptr],
+                &[ptr.into()],
                 Some(ret_place),
                 StackPopCleanup::None { cleanup: true },
             )?;