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

Should we put a log::trace! before every unwrap_validated()? #64

Open
wucke13 opened this issue Jul 31, 2024 · 3 comments
Open

Should we put a log::trace! before every unwrap_validated()? #64

wucke13 opened this issue Jul 31, 2024 · 3 comments

Comments

@wucke13
Copy link
Collaborator

wucke13 commented Jul 31, 2024

No description provided.

@wucke13 wucke13 changed the title Should we put a log::trace! before every unwrap_validated() Should we put a log::trace! before every unwrap_validated()? Jul 31, 2024
@florianhartung
Copy link
Collaborator

florianhartung commented Aug 1, 2024

Instead of having a log::trace! before every unwrap_validated(), I think we should do the trace! call inside the unwrap_validated() method. The caller then has to provide a message (or reason) for why the unwrap is safe.

A possible implementiation for unwrap_validated could be:

pub fn unwrap_validated(self, reason: impl Display) {
    log::trace!("Unwrapping Option because of prior validation. Reasoning: {msg}");
    self.unwrap_or_else(||
        panic!("Validation guarantees this to be `Some(_)`, but it is `None`. Reason this should have been safe to unwrap: {reason}")
    )
}

Here is also an example of how this could be called:

GLOBAL_GET => {
    let global_idx = wasm.read_var_u32(
        .unwrap_validated("The GLOBAL_GET instruction should be followed by a 32-bit integer.") as GlobalIdx;
    let global = store.globals.get(global_idx)
        .unwrap_validated("The global index a GLOBAL_GET instruction should be valid");
    stack.push_value(global.value.clone());
}

However, now the naming unwrap_validated does not make sense anymore because usually unwrap methods don't take any arguments.

@wucke13
Copy link
Collaborator Author

wucke13 commented Aug 1, 2024

However, now the naming unwrap_validated does not make sense anymore because usually unwrap methods don't take any arguments.

unwrap_or takes an argument. I think having this particular thing is not an issue at all.

@florianhartung
Copy link
Collaborator

I feel like unwrap_or should not be able to panic. I suggest fn expect_validated(reason: &str) instead.

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