Skip to content

Commit

Permalink
add nostd test crate
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanisaacs committed Jan 30, 2023
1 parent 92c179e commit b82d5e4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
12 changes: 12 additions & 0 deletions test-crates/mustang-nostd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "mustang-nostd"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# The mustang crate provides the `can_run_this!()` macro.
mustang = { path = "../../mustang", default-features = false, features = ["threads", "default-alloc"] }

[workspace]
15 changes: 15 additions & 0 deletions test-crates/mustang-nostd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
This crate demonstrates the use of mustang with no_std. Specifying `-Zbuild-std=core,alloc` lets you skip compiling std as it will not be used:

```
cargo run --target=x86_64-mustang-linux-gnu -Zbuild-std=core,alloc
```

This line:

```toml
mustang = { path = "../../mustang", default-features = false, features = ["threads", "default-alloc"] }
```

tells cargo to not enable the "std" feature which results in no dependencies on std. c-gull will re-export the no-std version of c-scape. c-scape is still necessary because `dlmalloc` and `unwinding` rely on some libc functionality. `dlmalloc` uses libc for syscalls and needs a pthread implementation for `GlobalAlloc`. `unwinding` uses the libc `dl_iterate_phdr` function, see [comment](https://github.com/sunfishcode/mustang/blob/bf6b53a4c5edd1dec71fa65f468b2c76ff96eb62/mustang/Cargo.toml#L19).

You can use either `#[start]` or replacing the C shim as in the example. See the unstable book for more [details](https://doc.rust-lang.org/unstable-book/language-features/lang-items.html#writing-an-executable-without-stdlib). It may or may not be undefined behavior to call `panic!()` from the function as no catch_unwind has been set, see the start feature [tracking issue](https://github.com/rust-lang/rust/issues/29633) and [following issue](https://github.com/rust-lang/rust/issues/107381).
20 changes: 20 additions & 0 deletions test-crates/mustang-nostd/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![feature(lang_items)]
#![no_std]
#![no_main]

mustang::can_run_this!();

use core::{ffi::c_int, panic::PanicInfo};

#[panic_handler]
fn panic(_panic: &PanicInfo<'_>) -> ! {
loop {}
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

#[no_mangle]
extern "C" fn main(_argc: c_int, _argv: *mut *mut u8, _envp: *mut *mut u8) -> c_int {
0
}

0 comments on commit b82d5e4

Please sign in to comment.