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

Hide global_allocator and panic_handler behind a macro #335

Open
greenhat opened this issue Oct 8, 2024 · 3 comments
Open

Hide global_allocator and panic_handler behind a macro #335

greenhat opened this issue Oct 8, 2024 · 3 comments
Milestone

Comments

@greenhat
Copy link
Contributor

greenhat commented Oct 8, 2024

In the new project template:

// Global allocator to use heap memory in no-std environment
#[global_allocator]
static ALLOC: BumpAlloc = miden_sdk::BumpAlloc::new();

// Required for no-std crates
#[panic_handler]
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}

in
https://github.com/0xPolygonMiden/rust-templates/blob/3aa6c9dc5fceb8fc0975270c12b98267e3abbde6/account/template/src/lib.rs#L10-L19

The macro could be called miden_setup. My only concern is that we might be insulating the developer too much from being in the no_std land.
The other thing, is that they are not intended to be written by the developer and provided in the new project template. So we're not helping the developer to write them but hiding them for the optics.

@greenhat greenhat added this to the Beta 1 milestone Oct 8, 2024
@bobbinth
Copy link
Contributor

bobbinth commented Oct 8, 2024

I think if we document the macro well enough, it should be fine.

Quick question: what does setting panic handler to an infinite loop actually do? (I'm assuming it does not result in infinite loops on panic). Maybe worth documenting this.

@greenhat
Copy link
Contributor Author

greenhat commented Oct 9, 2024

Quick question: what does setting panic handler to an infinite loop actually do? (I'm assuming it does not result in infinite loops on panic). Maybe worth documenting this.

#[panic_handler] is used to define the behavior of panic! in #![no_std] environment. It should have the following signature fn my_panic(_info: &core::panic::PanicInfo) -> ! so it should never return (! is never type). In the no_std environment, there is no operating system to return to, so I suppose the looping forever is the only thing that goes nowhere else in the program in a platform independent way. In Wasm, there is unreachable instruction that is a trap, so

#[panic_handler]
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
    core::arch::wasm32::unreachable()
}

is more appropriate for Wasm targets since it compiles to its unreachable instruction. However, it is not that much of a difference in our case since we're re-building core, alloc libraries with effectively ripped out panic infrastructure (see)

@bobbinth
Copy link
Contributor

bobbinth commented Oct 9, 2024

Makes sense! Thank you for the explanation.

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