]> git.lizzy.rs Git - rust.git/blobdiff - src/shims/foreign_items.rs
avoid using unchecked casts or arithmetic
[rust.git] / src / shims / foreign_items.rs
index 86c227121540c1cca63a3489f13d2a4f978d0c5e..deabbdd6081933a22d5daad07a3a86f228f4634c 100644 (file)
@@ -1,7 +1,7 @@
 mod windows;
 mod posix;
 
-use std::{convert::TryInto, iter};
+use std::{convert::{TryInto, TryFrom}, iter};
 
 use rustc_hir::def_id::DefId;
 use rustc::mir;
@@ -150,12 +150,13 @@ fn emulate_foreign_item(
 
         // Second: some functions that we forward to MIR implementations.
         match link_name {
-            // This matches calls to the *foreign* item `__rust_start_panic*, that is,
-            // calls to `extern "Rust" { fn __rust_start_panic(...) }`.
+            // This matches calls to the foreign item `__rust_start_panic`, that is,
+            // calls to `extern "Rust" { fn __rust_start_panic(...) }`
+            // (and `__rust_panic_cleanup`, respectively).
             // We forward this to the underlying *implementation* in the panic runtime crate.
             // Normally, this will be either `libpanic_unwind` or `libpanic_abort`, but it could
             // also be a custom user-provided implementation via `#![feature(panic_runtime)]`
-            "__rust_start_panic" => {
+            "__rust_start_panic" | "__rust_panic_cleanup"=> {
                 // FIXME we might want to cache this... but it's not really performance-critical.
                 let panic_runtime = tcx
                     .crates()
@@ -164,7 +165,7 @@ fn emulate_foreign_item(
                     .expect("No panic runtime found!");
                 let panic_runtime = tcx.crate_name(*panic_runtime);
                 let start_panic_instance =
-                    this.resolve_path(&[&*panic_runtime.as_str(), "__rust_start_panic"])?;
+                    this.resolve_path(&[&*panic_runtime.as_str(), link_name])?;
                 return Ok(Some(&*this.load_mir(start_panic_instance.def, None)?));
             }
             _ => {}
@@ -249,7 +250,7 @@ fn emulate_foreign_item_by_name(
                     MiriMemoryKind::Rust.into(),
                 );
                 // We just allocated this, the access is definitely in-bounds.
-                this.memory.write_bytes(ptr.into(), iter::repeat(0u8).take(size as usize)).unwrap();
+                this.memory.write_bytes(ptr.into(), iter::repeat(0u8).take(usize::try_from(size).unwrap())).unwrap();
                 this.write_scalar(ptr, dest)?;
             }
             "__rust_dealloc" => {
@@ -291,11 +292,6 @@ fn emulate_foreign_item_by_name(
                 this.write_scalar(new_ptr, dest)?;
             }
 
-            "__rust_maybe_catch_panic" => {
-                this.handle_catch_panic(args, dest, ret)?;
-                return Ok(false);
-            }
-
             "memcmp" => {
                 let left = this.read_scalar(args[0])?.not_undef()?;
                 let right = this.read_scalar(args[1])?.not_undef()?;
@@ -354,7 +350,7 @@ fn emulate_foreign_item_by_name(
             "strlen" => {
                 let ptr = this.read_scalar(args[0])?.not_undef()?;
                 let n = this.memory.read_c_str(ptr)?.len();
-                this.write_scalar(Scalar::from_uint(n as u64, dest.layout.size), dest)?;
+                this.write_scalar(Scalar::from_uint(u64::try_from(n).unwrap(), dest.layout.size), dest)?;
             }
 
             // math functions
@@ -444,9 +440,9 @@ fn emulate_foreign_item_by_name(
 
                 // Saturating cast to i16. Even those are outside the valid exponent range to
                 // `scalbn` below will do its over/underflow handling.
-                let exp = if exp > i16::MAX as i32 {
+                let exp = if exp > i32::from(i16::MAX) {
                     i16::MAX
-                } else if exp < i16::MIN as i32 {
+                } else if exp < i32::from(i16::MIN) {
                     i16::MIN
                 } else {
                     exp.try_into().unwrap()