-
Notifications
You must be signed in to change notification settings - Fork 389
Add ABI check for shims #1670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add ABI check for shims #1670
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,14 @@ use log::trace; | |
|
||
use rustc_hir::def_id::DefId; | ||
use rustc_middle::mir; | ||
use rustc_target::{abi::{Align, Size}, spec::PanicStrategy}; | ||
use rustc_target::{abi::{Align, Size}, spec::{PanicStrategy, abi::Abi}}; | ||
use rustc_middle::ty; | ||
use rustc_apfloat::Float; | ||
use rustc_span::symbol::sym; | ||
|
||
use crate::*; | ||
use super::backtrace::EvalContextExt as _; | ||
use helpers::check_arg_count; | ||
use helpers::{check_abi, check_arg_count}; | ||
|
||
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {} | ||
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> { | ||
|
@@ -112,6 +112,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
fn emulate_foreign_item( | ||
&mut self, | ||
def_id: DefId, | ||
abi: Abi, | ||
args: &[OpTy<'tcx, Tag>], | ||
ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>, | ||
unwind: Option<mir::BasicBlock>, | ||
|
@@ -130,25 +131,29 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
let (dest, ret) = match ret { | ||
None => match link_name { | ||
"miri_start_panic" => { | ||
check_abi(abi, Abi::Rust)?; | ||
this.handle_miri_start_panic(args, unwind)?; | ||
return Ok(None); | ||
} | ||
// This matches calls to the foreign item `panic_impl`. | ||
// The implementation is provided by the function with the `#[panic_handler]` attribute. | ||
"panic_impl" => { | ||
check_abi(abi, Abi::Rust)?; | ||
let panic_impl_id = tcx.lang_items().panic_impl().unwrap(); | ||
let panic_impl_instance = ty::Instance::mono(tcx, panic_impl_id); | ||
return Ok(Some(&*this.load_mir(panic_impl_instance.def, None)?)); | ||
} | ||
| "exit" | ||
| "ExitProcess" | ||
=> { | ||
check_abi(abi, if link_name == "exit" { Abi::C } else { Abi::System })?; | ||
let &[code] = check_arg_count(args)?; | ||
// it's really u32 for ExitProcess, but we have to put it into the `Exit` variant anyway | ||
let code = this.read_scalar(code)?.to_i32()?; | ||
throw_machine_stop!(TerminationInfo::Exit(code.into())); | ||
} | ||
"abort" => { | ||
check_abi(abi, Abi::C)?; | ||
throw_machine_stop!(TerminationInfo::Abort("the program aborted execution".to_owned())) | ||
} | ||
_ => throw_unsup_format!("can't call (diverging) foreign function: {}", link_name), | ||
|
@@ -165,6 +170,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
// 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_panic_cleanup" => { | ||
check_abi(abi, Abi::C)?; | ||
// This replicates some of the logic in `inject_panic_runtime`. | ||
// FIXME: is there a way to reuse that logic? | ||
let panic_runtime = match this.tcx.sess.panic_strategy() { | ||
|
@@ -179,7 +185,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
} | ||
|
||
// Third: functions that return. | ||
if this.emulate_foreign_item_by_name(link_name, args, dest, ret)? { | ||
if this.emulate_foreign_item_by_name(link_name, abi, args, dest, ret)? { | ||
trace!("{:?}", this.dump_place(*dest)); | ||
this.go_to_block(ret); | ||
} | ||
|
@@ -193,6 +199,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
fn emulate_foreign_item_by_name( | ||
&mut self, | ||
link_name: &str, | ||
abi: Abi, | ||
args: &[OpTy<'tcx, Tag>], | ||
dest: PlaceTy<'tcx, Tag>, | ||
ret: mir::BasicBlock, | ||
|
@@ -204,6 +211,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
match link_name { | ||
// Miri-specific extern functions | ||
"miri_static_root" => { | ||
check_abi(abi, Abi::Rust)?; | ||
let &[ptr] = check_arg_count(args)?; | ||
let ptr = this.read_scalar(ptr)?.check_init()?; | ||
let ptr = this.force_ptr(ptr)?; | ||
|
@@ -215,23 +223,27 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
|
||
// Obtains a Miri backtrace. See the README for details. | ||
"miri_get_backtrace" => { | ||
check_abi(abi, Abi::Rust)?; | ||
this.handle_miri_get_backtrace(args, dest)?; | ||
} | ||
|
||
// Resolves a Miri backtrace frame. See the README for details. | ||
"miri_resolve_frame" => { | ||
check_abi(abi, Abi::Rust)?; | ||
this.handle_miri_resolve_frame(args, dest)?; | ||
} | ||
|
||
|
||
// Standard C allocation | ||
"malloc" => { | ||
check_abi(abi, Abi::C)?; | ||
let &[size] = check_arg_count(args)?; | ||
let size = this.read_scalar(size)?.to_machine_usize(this)?; | ||
let res = this.malloc(size, /*zero_init:*/ false, MiriMemoryKind::C); | ||
this.write_scalar(res, dest)?; | ||
} | ||
"calloc" => { | ||
check_abi(abi, Abi::C)?; | ||
let &[items, len] = check_arg_count(args)?; | ||
let items = this.read_scalar(items)?.to_machine_usize(this)?; | ||
let len = this.read_scalar(len)?.to_machine_usize(this)?; | ||
|
@@ -241,11 +253,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
this.write_scalar(res, dest)?; | ||
} | ||
"free" => { | ||
check_abi(abi, Abi::C)?; | ||
let &[ptr] = check_arg_count(args)?; | ||
let ptr = this.read_scalar(ptr)?.check_init()?; | ||
this.free(ptr, MiriMemoryKind::C)?; | ||
} | ||
"realloc" => { | ||
check_abi(abi, Abi::C)?; | ||
let &[old_ptr, new_size] = check_arg_count(args)?; | ||
let old_ptr = this.read_scalar(old_ptr)?.check_init()?; | ||
let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?; | ||
|
@@ -257,6 +271,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
// (Usually these would be forwarded to to `#[global_allocator]`; we instead implement a generic | ||
// allocation that also checks that all conditions are met, such as not permitting zero-sized allocations.) | ||
"__rust_alloc" => { | ||
check_abi(abi, Abi::Rust)?; | ||
let &[size, align] = check_arg_count(args)?; | ||
let size = this.read_scalar(size)?.to_machine_usize(this)?; | ||
let align = this.read_scalar(align)?.to_machine_usize(this)?; | ||
|
@@ -269,6 +284,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
this.write_scalar(ptr, dest)?; | ||
} | ||
"__rust_alloc_zeroed" => { | ||
check_abi(abi, Abi::Rust)?; | ||
let &[size, align] = check_arg_count(args)?; | ||
let size = this.read_scalar(size)?.to_machine_usize(this)?; | ||
let align = this.read_scalar(align)?.to_machine_usize(this)?; | ||
|
@@ -283,6 +299,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
this.write_scalar(ptr, dest)?; | ||
} | ||
"__rust_dealloc" => { | ||
check_abi(abi, Abi::Rust)?; | ||
let &[ptr, old_size, align] = check_arg_count(args)?; | ||
let ptr = this.read_scalar(ptr)?.check_init()?; | ||
let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?; | ||
|
@@ -296,6 +313,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
)?; | ||
} | ||
"__rust_realloc" => { | ||
check_abi(abi, Abi::Rust)?; | ||
let &[ptr, old_size, align, new_size] = check_arg_count(args)?; | ||
let ptr = this.force_ptr(this.read_scalar(ptr)?.check_init()?)?; | ||
let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?; | ||
|
@@ -316,6 +334,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
|
||
// C memory handling functions | ||
"memcmp" => { | ||
check_abi(abi, Abi::C)?; | ||
let &[left, right, n] = check_arg_count(args)?; | ||
let left = this.read_scalar(left)?.check_init()?; | ||
let right = this.read_scalar(right)?.check_init()?; | ||
|
@@ -336,6 +355,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
this.write_scalar(Scalar::from_i32(result), dest)?; | ||
} | ||
"memrchr" => { | ||
check_abi(abi, Abi::C)?; | ||
let &[ptr, val, num] = check_arg_count(args)?; | ||
let ptr = this.read_scalar(ptr)?.check_init()?; | ||
let val = this.read_scalar(val)?.to_i32()? as u8; | ||
|
@@ -354,6 +374,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
} | ||
} | ||
"memchr" => { | ||
check_abi(abi, Abi::C)?; | ||
let &[ptr, val, num] = check_arg_count(args)?; | ||
let ptr = this.read_scalar(ptr)?.check_init()?; | ||
let val = this.read_scalar(val)?.to_i32()? as u8; | ||
|
@@ -371,6 +392,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
} | ||
} | ||
"strlen" => { | ||
check_abi(abi, Abi::C)?; | ||
let &[ptr] = check_arg_count(args)?; | ||
let ptr = this.read_scalar(ptr)?.check_init()?; | ||
let n = this.memory.read_c_str(ptr)?.len(); | ||
|
@@ -386,6 +408,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
| "asinf" | ||
| "atanf" | ||
=> { | ||
check_abi(abi, Abi::C)?; | ||
let &[f] = check_arg_count(args)?; | ||
// FIXME: Using host floats. | ||
let f = f32::from_bits(this.read_scalar(f)?.to_u32()?); | ||
|
@@ -405,6 +428,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
| "hypotf" | ||
| "atan2f" | ||
=> { | ||
check_abi(abi, Abi::C)?; | ||
let &[f1, f2] = check_arg_count(args)?; | ||
// underscore case for windows, here and below | ||
// (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019) | ||
|
@@ -426,6 +450,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
| "asin" | ||
| "atan" | ||
=> { | ||
check_abi(abi, Abi::C)?; | ||
let &[f] = check_arg_count(args)?; | ||
// FIXME: Using host floats. | ||
let f = f64::from_bits(this.read_scalar(f)?.to_u64()?); | ||
|
@@ -445,6 +470,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
| "hypot" | ||
| "atan2" | ||
=> { | ||
check_abi(abi, Abi::C)?; | ||
let &[f1, f2] = check_arg_count(args)?; | ||
// FIXME: Using host floats. | ||
let f1 = f64::from_bits(this.read_scalar(f1)?.to_u64()?); | ||
|
@@ -460,6 +486,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
| "ldexp" | ||
| "scalbn" | ||
=> { | ||
check_abi(abi, Abi::C)?; | ||
let &[x, exp] = check_arg_count(args)?; | ||
// For radix-2 (binary) systems, `ldexp` and `scalbn` are the same. | ||
let x = this.read_scalar(x)?.to_f64()?; | ||
|
@@ -481,10 +508,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
|
||
// Architecture-specific shims | ||
"llvm.x86.sse2.pause" if this.tcx.sess.target.arch == "x86" || this.tcx.sess.target.arch == "x86_64" => { | ||
check_abi(abi, Abi::C)?; | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let &[] = check_arg_count(args)?; | ||
this.yield_active_thread(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just added another architecture-specific function here; can you rebase and also add the |
||
} | ||
"llvm.aarch64.hint" if this.tcx.sess.target.arch == "aarch64" => { | ||
check_abi(abi, Abi::C)?; | ||
let &[hint] = check_arg_count(args)?; | ||
let hint = this.read_scalar(hint)?.to_i32()?; | ||
match hint { | ||
|
@@ -499,8 +528,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx | |
|
||
// Platform-specific shims | ||
_ => match this.tcx.sess.target.os.as_str() { | ||
"linux" | "macos" => return shims::posix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest, ret), | ||
"windows" => return shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest, ret), | ||
"linux" | "macos" => return shims::posix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret), | ||
"windows" => return shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret), | ||
target => throw_unsup_format!("the target `{}` is not supported", target), | ||
} | ||
}; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.