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

Improve traps #2305

Merged
merged 15 commits into from
May 12, 2021
Merged

Improve traps #2305

merged 15 commits into from
May 12, 2021

Conversation

syrusakbary
Copy link
Member

Description

This PR moves the trap code forward so we can start refactoring easily in subsequent PRs:

  • Adding a is_wasm_pc method to the traps/engine (so we only trap Wasm-related signals/...)
  • Making Store generic over TrapInfo (so users can implement their custom trap handlers)
  • Removed Interrupt (non-used) and VMOutOfMemory (moved into RuntimeError) cases in TrapCode, so it's deterministic

Review

  • Add a short description of the change to the CHANGELOG.md file

@syrusakbary
Copy link
Member Author

bors try

bors bot added a commit that referenced this pull request May 11, 2021
@bors
Copy link
Contributor

bors bot commented May 11, 2021

try

Build failed:

@syrusakbary
Copy link
Member Author

bors try

bors bot added a commit that referenced this pull request May 11, 2021
@bors
Copy link
Contributor

bors bot commented May 11, 2021

Copy link
Contributor

@Hywan Hywan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

lib/engine/src/trap/frame_info.rs Show resolved Hide resolved
lib/engine/src/trap/frame_info.rs Outdated Show resolved Hide resolved
lib/vm/src/instance/mod.rs Outdated Show resolved Hide resolved
lib/vm/src/instance/mod.rs Show resolved Hide resolved
lib/vm/src/trap/handlers.c Outdated Show resolved Hide resolved
lib/api/src/store.rs Outdated Show resolved Hide resolved
lib/vm/src/trap/trapcode.rs Outdated Show resolved Hide resolved
lib/vm/src/trap/traphandlers.rs Outdated Show resolved Hide resolved
lib/vm/src/trap/traphandlers.rs Outdated Show resolved Hide resolved
pub struct CallThreadState {
unwind: Cell<UnwindReason>,
pub struct CallThreadState<'a> {
unwind: UnsafeCell<MaybeUninit<UnwindReason>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we replace MaybeUninit with something safer, like Option? That way we can detect an unwind that happened without an unwnd_with, instead of just executing undefined behaviour in unsafe Rust?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I actually tried with Option but it turned to be not trivial (and I couldn't solve it in a few hours, so I went with this).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add an explicit "Poison" state to the UnwindReason and then initialize to that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a pidgin copy of the problem in the rust playground, this code works:

use std::cell::UnsafeCell;
pub struct Foo {
    unwind: UnsafeCell<Option<UnwindReason>>,
}
enum UnwindReason {
    Foo(u8),
    Bar
}
impl Foo {
    fn a(mut self) {
        match unsafe { &*self.unwind.get() } {
            Some(UnwindReason::Foo(_unwind)) => {},
            Some(UnwindReason::Bar) => {},
            None => {}
        }
    }
    fn b(&self, reason: UnwindReason) {
        *(unsafe { &mut *self.unwind.get() }) = Some(reason);
    }
}

lib/vm/src/trap/traphandlers.rs Outdated Show resolved Hide resolved
lib/vm/src/trap/traphandlers.rs Outdated Show resolved Hide resolved
lib/vm/src/trap/traphandlers.rs Show resolved Hide resolved
lib/vm/src/trap/traphandlers.rs Outdated Show resolved Hide resolved
lib/api/src/store.rs Outdated Show resolved Hide resolved
lib/engine/src/trap/frame_info.rs Outdated Show resolved Hide resolved
lib/vm/src/trap/traphandlers.rs Outdated Show resolved Hide resolved
pub struct CallThreadState {
unwind: Cell<UnwindReason>,
pub struct CallThreadState<'a> {
unwind: UnsafeCell<MaybeUninit<UnwindReason>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add an explicit "Poison" state to the UnwindReason and then initialize to that?

lib/vm/src/trap/traphandlers.rs Show resolved Hide resolved
pub struct CallThreadState {
unwind: Cell<UnwindReason>,
pub struct CallThreadState<'a> {
unwind: UnsafeCell<MaybeUninit<UnwindReason>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a pidgin copy of the problem in the rust playground, this code works:

use std::cell::UnsafeCell;
pub struct Foo {
    unwind: UnsafeCell<Option<UnwindReason>>,
}
enum UnwindReason {
    Foo(u8),
    Bar
}
impl Foo {
    fn a(mut self) {
        match unsafe { &*self.unwind.get() } {
            Some(UnwindReason::Foo(_unwind)) => {},
            Some(UnwindReason::Bar) => {},
            None => {}
        }
    }
    fn b(&self, reason: UnwindReason) {
        *(unsafe { &mut *self.unwind.get() }) = Some(reason);
    }
}

@syrusakbary
Copy link
Member Author

bors try

bors bot added a commit that referenced this pull request May 12, 2021
@bors
Copy link
Contributor

bors bot commented May 12, 2021

@syrusakbary
Copy link
Member Author

Merging manually to avoid CI since tests have already passed and there are no new changes since last tests

@syrusakbary syrusakbary merged commit 425846a into master May 12, 2021
@bors bors bot deleted the improve-traps branch May 12, 2021 22:58
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.

4 participants