Skip to content

Commit a4607ef

Browse files
committed
document general shim pattern
1 parent 403b3f9 commit a4607ef

File tree

5 files changed

+20
-0
lines changed

5 files changed

+20
-0
lines changed

src/shims/foreign_items.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
366366
) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
367367
let this = self.eval_context_mut();
368368

369+
// When adding a new shim, you should follow the following pattern:
370+
// ```
371+
// "shim_name" => {
372+
// let [arg1, arg2, arg3] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
373+
// let result = this.shim_name(arg1, arg2, arg3)?;
374+
// this.write_scalar(result, dest)?;
375+
// }
376+
// ```
377+
// and then define `shim_name` as a helper function in an extension trait in a suitable file
378+
// (see e.g. `unix/fs.rs`). You might find existing shims not following this pattern, most
379+
// likely because they predate it or because for some reason they cannot be made to fit.
380+
369381
// Here we dispatch all the shims for foreign functions. If you have a platform specific
370382
// shim, add it to the corresponding submodule.
371383
match link_name.as_str() {

src/shims/unix/foreign_items.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ 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.
28+
2729
match link_name.as_str() {
2830
// Environment related shims
2931
"getenv" => {

src/shims/unix/linux/foreign_items.rs

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2020
) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
2121
let this = self.eval_context_mut();
2222

23+
// See `fn emulate_foreign_item_by_name` in `shims/foreign_items.rs` for the general pattern.
24+
2325
// Windows API stubs.
2426
// HANDLE = isize
2527
// NTSTATUS = LONH = i32

0 commit comments

Comments
 (0)