Skip to content

Comments

Some fixes for try_call#10593

Merged
cfallin merged 6 commits intobytecodealliance:mainfrom
bjorn3:fix_try_call
Apr 17, 2025
Merged

Some fixes for try_call#10593
cfallin merged 6 commits intobytecodealliance:mainfrom
bjorn3:fix_try_call

Conversation

@bjorn3
Copy link
Contributor

@bjorn3 bjorn3 commented Apr 16, 2025

Together with a change to allow the system_v call conv, this is enough to pass most of cg_clif's test suite (including panicking tests). In addition rustc's compiletest now works just fine in panic=unwind mode. Not all rustc tests pass yet, but the test failures seem to be caused by either cg_clif or bugs in the tests themself.

The second commit is not strictly necessary for correctness, but does make the optimizer work better with try_call.

@bjorn3 bjorn3 requested a review from a team as a code owner April 16, 2025 12:15
@bjorn3 bjorn3 requested review from fitzgen and removed request for a team April 16, 2025 12:15
@github-actions github-actions bot added the cranelift Issues related to the Cranelift code generator label Apr 16, 2025
@bjorn3
Copy link
Contributor Author

bjorn3 commented Apr 16, 2025

Fixed miscompilation

The following gets miscompiled even with this PR in release mode (will fix in a future PR, but leaving my debugging notes here):

use std::ffi::CString;

fn main() {
    let s = CString::new(b"ar");
    panic!("{s:?}");
}

Reduced so far to:

pub struct MyCString {
    inner: Vec<u8>,
}

#[inline(never)]
fn push(v: &mut Vec<u8>) {
    v.push(0);
}

impl MyCString {
    #[inline(never)]
    pub fn new() -> MyCString {
        let mut buffer = const { MyCString { inner: Vec::new() } };
        push(&mut buffer.inner);
        buffer
    }
}

fn main() {
    let s = MyCString::new();
    panic!("{:?}", s.inner); // nul-terminator gets lost
}

Edit: Pushed a commit to fix this miscompilation.

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

LGTM, but we should have a test for the first commit before this merges. Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

This pass is used by cg_clif, I assume? We should probably have filetests for these changes, but I see that we don't have the infrastructure to run this pass on filetests at the moment, and I don't want to necessarily block on that. But longer term, we should either gain that functionality, so that we can hold it to our usual code standards, or else this pass should move into cg_clif (since I don't believe it relies on anything that isn't publicly exported).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This pass is part of the normal optimization pipeline of Cranelift and runs unconditionally.

Copy link
Member

Choose a reason for hiding this comment

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

Huh. rust-analyzer wasn't finding uses of it for me.

Do we exercise it under the test optimize filetest mode? If so, can you add a filetest for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Context::optimize contains an unconditional call to self.eliminate_unreachable_code(isa)?; which in turn calls this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As for testing, I'm not sure how to do that given that the clif ir printing doesn't show a list of all exception tables, but rather only shows them as part of a try_call instruction.

Copy link
Member

Choose a reason for hiding this comment

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

I think that's correct -- I can't see a way to test for dangling exception tables either unless we change the print format. I think this is probably fine to merge as-is -- the other direction (avoiding false-removals of actually used exception tables) is the more important one and is implicitly tested.

Copy link
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

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

LGTM on the additional commits -- just one thought on the verifier change below. I'm excited that you found the memory-fence issue and have things working with cg_clif now!

bjorn3 added 3 commits April 17, 2025 15:51
… used

This way other consumers of Cranelift don't have to pay the cost of the
way Wasmtime will implement unwinding on exceptions.
@cfallin
Copy link
Member

cfallin commented Apr 17, 2025

Looked over all commits and this looks good -- I'm going to do a short followup based on @bjorn3's cg_clif unwinding enabling work, so I'll go ahead and merge this now.

@cfallin cfallin added this pull request to the merge queue Apr 17, 2025
Merged via the queue into bytecodealliance:main with commit 3932e8f Apr 17, 2025
71 checks passed
@bjorn3 bjorn3 deleted the fix_try_call branch April 17, 2025 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants