Skip to content

Commit a109994

Browse files
committed
Auto merge of rust-lang#2481 - RalfJung:shim-pattern, r=oli-obk
document general shim pattern r? `@oli-obk`
2 parents 6e306f9 + daaa81f commit a109994

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

src/shims/foreign_items.rs

+32
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,38 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
369369
) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
370370
let this = self.eval_context_mut();
371371

372+
// When adding a new shim, you should follow the following pattern:
373+
// ```
374+
// "shim_name" => {
375+
// let [arg1, arg2, arg3] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
376+
// let result = this.shim_name(arg1, arg2, arg3)?;
377+
// this.write_scalar(result, dest)?;
378+
// }
379+
// ```
380+
// and then define `shim_name` as a helper function in an extension trait in a suitable file
381+
// (see e.g. `unix/fs.rs`):
382+
// ```
383+
// fn shim_name(
384+
// &mut self,
385+
// arg1: &OpTy<'tcx, Provenance>,
386+
// arg2: &OpTy<'tcx, Provenance>,
387+
// arg3: &OpTy<'tcx, Provenance>)
388+
// -> InterpResult<'tcx, Scalar<Provenance>> {
389+
// let this = self.eval_context_mut();
390+
//
391+
// // First thing: load all the arguments. Details depend on the shim.
392+
// let arg1 = this.read_scalar(arg1)?.to_u32()?;
393+
// let arg2 = this.read_pointer(arg2)?; // when you need to work with the pointer directly
394+
// let arg3 = this.deref_operand(arg3)?; // when you want to load/store through the pointer at its declared type
395+
//
396+
// // ...
397+
//
398+
// Ok(Scalar::from_u32(42))
399+
// }
400+
// ```
401+
// You might find existing shims not following this pattern, most
402+
// likely because they predate it or because for some reason they cannot be made to fit.
403+
372404
// Here we dispatch all the shims for foreign functions. If you have a platform specific
373405
// shim, add it to the corresponding submodule.
374406
match link_name.as_str() {

src/shims/unix/foreign_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2424
) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
2525
let this = self.eval_context_mut();
2626

27+
// See `fn emulate_foreign_item_by_name` in `shims/foreign_items.rs` for the general pattern.
2728
#[rustfmt::skip]
2829
match link_name.as_str() {
2930
// Environment related shims

src/shims/unix/linux/foreign_items.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
1919
) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
2020
let this = self.eval_context_mut();
2121

22+
// See `fn emulate_foreign_item_by_name` in `shims/foreign_items.rs` for the general pattern.
23+
2224
match link_name.as_str() {
2325
// errno
2426
"__errno_location" => {

src/shims/unix/macos/foreign_items.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
1717
) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
1818
let this = self.eval_context_mut();
1919

20+
// See `fn emulate_foreign_item_by_name` in `shims/foreign_items.rs` for the general pattern.
21+
2022
match link_name.as_str() {
2123
// errno
2224
"__error" => {

src/shims/windows/foreign_items.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2323
) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
2424
let this = self.eval_context_mut();
2525

26+
// See `fn emulate_foreign_item_by_name` in `shims/foreign_items.rs` for the general pattern.
27+
2628
// Windows API stubs.
2729
// HANDLE = isize
2830
// NTSTATUS = LONH = i32

0 commit comments

Comments
 (0)