Skip to content
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

Possibly wrong argc/argv #72

Closed
blckngm opened this issue Sep 28, 2022 · 3 comments
Closed

Possibly wrong argc/argv #72

blckngm opened this issue Sep 28, 2022 · 3 comments

Comments

@blckngm
Copy link
Collaborator

blckngm commented Sep 28, 2022

Rust contracts built by capsule build (esp. in non-release build) may not get the correct argc and argv arguments because the compiler may insert function prologue before our assembly code in _start and change the sp register.

One possible solution is to mark the _start function #[naked].

Or the _start function can be written in pure assembly and linked with rust code.

(By the way the program_entry function should use extern "C" so that it has a defined calling convention.)

@blckngm
Copy link
Collaborator Author

blckngm commented Sep 28, 2022

#[no_mangle]
#[naked]
pub unsafe extern "C" fn _start() -> ! {
    core::arch::asm!(
        "lw a0, 0(sp)",
        "addi a1, sp, 8",
        "call main",
        "li a7, 93",
        "ecall",
        options(noreturn)
    );
}

#[no_mangle]
extern "C" fn main(_argc: u64, _argv: *const *const u8) -> i8 {
    _argc as i8
}

@XuJiandong
Copy link
Collaborator

@blckngm
Copy link
Collaborator Author

blckngm commented Sep 28, 2022

Global asm also works, and does not require any nightly features:

core::arch::global_asm!(
    ".global _start",
    "_start:",
    "lw a0, 0(sp)",
    "addi a1, sp, 8",
    "call main",
    "li a7, 93",
    "ecall",
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants