jyao1 / rust-uefi-std-stub

2 stars 0 forks source link

investigate a solution not use prelude #3

Open jyao1 opened 2 years ago

jyao1 commented 2 years ago

use target.json to indicate a std lib ?

REF: https://zhuanlan.zhihu.com/p/406785047

xiaoyuxlu commented 2 years ago

Problem

Our target x86_64-unknown-uefi doesn't support std library.So we must use #![no_std] to build for that target. Here is platform-support list.

the following questions is for those not support std target.

A. No panic_handler B. No heap alloc C. A lot of crates in crate.io do not support no_std (eg. rustls).

A and B easy to solve. For A we can simply add #[panic_handler] For B we can reuse alloc crate and provide a #[global_allocator] and a alloc_error_handler. This also need enable unstable features #![feature(alloc_error_handler)]

But for C eh... Modifying the crate's source code might be work #![no_std]. (use prelude or add support no_std). But there is a lot of maintenance work and upstream work.

So we need a way to add std support for C.

Here are discussions in the rust community https://github.com/rust-lang/wg-cargo-std-aware

One way (case 2 or 3) is use build-std feature. see: https://github.com/rust-lang/wg-cargo-std-aware#mvp-implementation

Another way (case 1) is add modify rust source code and add support for x86_64-unknown-uefi target

Results

  1. rustls (unmodified) build with host target, into host exe -- success basic feature that we need guarantee.

  2. rustls (unmodified) build with modified rust (add uefi support for library/std), build with cargo-build -- success build modified rust https://github.com/xiaoyuxlu/rust/tree/b/uefi_std and build payload(https://github.com/jyao1/rust-td/tree/master/rust-rustls-payload) with unmodified rustls

    ./x.py build -i
    ./x.py install -i
    ./x.py install -i --target x86_64-unknown-uefi library/std --stage 2 -v
    ## change dir to rust-rustls-payload
    cargo build --target x86_64-unknown-uefi --release
  3. rustls ( + std::prelude): build with cargo-xbuild / cargo build -Zbuild-std -- success ref: https://github.com/jyao1/rust-uefi-std-stub ref: https://github.com/jyao1/rustls/tree/uefi_support

  4. rustls (unmodified): build with __CARGO_TESTS_ONLY_SRC_ROOT=<modified rust source code tree path> cargo build -Z build-std -- success ref: https://github.com/jyao1/rust-td/tree/master/rust-rustls-payload

cargo build -Zbuild-std=core,std,compiler_builtins,alloc,panic_abort -Zbuild-std-features=compiler-builtins-mem,panic_immediate_abort --target x86_64-unknown-uefi --release

If we use rust_std (in 1 and 3), we need resolve problem (similar to no_std in 2): A. rust_std panic for UEFI B. rust_std alloc for UEFI

Question for 1 and 3: A: Payload can register a customer panic_handler by using std::panic::set_hook. B: Payload can use #[global_allocator] to specify a allocator.

Summary

Case Rustls modification Rust modification Rebuild rust Target Build method Status(rustls-payload with std) (if use #![no_std] NO else YES)
0 NO NO NO host cargo build YES
1 NO YES YES x86_64-unknown-uefi cargo build YES
2 YES with std_stub NO NO x86_64-unknown-uefi cargo build -Zbuild-std NO
3 NO YES NO x86_64-unknown-uefi __CARGO_TESTS_ONLY_SRC_ROOT=[path-to-rust-src]/rust cargo build -Zbuild-std YES
xiaoyuxlu commented 2 years ago

SGX std support

sgx need add extern sgx_tstd as std in bottom of crate. and add use std::prelude::v1::*

need maintain another crate branch for sgx

https://github.com/mesalock-linux/

xiaoyuxlu commented 2 years ago

https://github.com/rust-lang/wg-cargo-std-aware/issues/3