Skip to content

Add from_raw and into_raw methods to RestoreState. - #64

Open
reitermarkus wants to merge 1 commit into
rust-embedded:mainfrom
reitermarkus:raw-conversion
Open

Add from_raw and into_raw methods to RestoreState.#64
reitermarkus wants to merge 1 commit into
rust-embedded:mainfrom
reitermarkus:raw-conversion

Conversation

@reitermarkus

Copy link
Copy Markdown
Member

I have a use case where I need to re-expose _critical_section_1_0_acquire/_critical_section_1_0_release to a shared library that is dynamically loaded.

Specifically, using the elf_loader crate, I need to do this:

unsafe fn _critical_section_1_0_acquire() -> RawRestoreState {
  unsafe { critical_section::acquire().into_raw() }
}

unsafe fn _critical_section_1_0_release(restore_state: RawRestoreState) {
  unsafe { critical_section::release(RestoreState::from_raw(restore_state)) };
}


 SyntheticModule::new(
  "__host",
  [
    SyntheticSymbol::function(
      "_critical_section_1_0_acquire",
      _critical_section_1_0_acquire as *const (),
    ),
    SyntheticSymbol::function(
      "_critical_section_1_0_release",
      _critical_section_1_0_release as *const (),
    ),
  ],
)

@reitermarkus
reitermarkus requested a review from a team as a code owner July 27, 2026 07:11
@jannic

jannic commented Aug 11, 2026

Copy link
Copy Markdown
Member

In your use case, doesn't this cause an infinite recursion?

unsafe fn _critical_section_1_0_acquire() -> RawRestoreState {
  unsafe { critical_section::acquire().into_raw() }
}

critical_section::acquire() is implemented as:

pub unsafe fn acquire() -> RestoreState {
    extern "Rust" {
        fn _critical_section_1_0_acquire() -> RawRestoreState;
    }

    #[allow(clippy::unit_arg)]
    RestoreState(_critical_section_1_0_acquire())
}

Ignoring that I don't understand your use case, the PR itself looks sound.

@reitermarkus

Copy link
Copy Markdown
Member Author

In your use case, doesn't this cause an infinite recursion?

No, the unsafe fn _critical_section_1_0_acquire isn't marked no_mangle, and could actually have any name. Only the string given to SyntheticSymbol::function is used for the lookup table exposed to the shared library.

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

Successfully merging this pull request may close these issues.

2 participants