This is an archive of the discontinued LLVM Phabricator instance.

[WebAssembly] Report error for inline assembly with unsupported opcodes
ClosedPublic

Authored by mojingran on Jul 7 2023, 11:27 AM.

Details

Summary

For inline WebAssembly, passing a numeric operand to global.get is unsupported. This causes encodeInstruction to reach an llvm_unreachable block, leading to undefined behaviors. This patch fixes the issue for this invalid instruction encoding, making it report an error by adding an MCContext field in class WebAssemblyMCCodeEmitter.

Diff Detail

Event Timeline

mojingran created this revision.Jul 7 2023, 11:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 7 2023, 11:27 AM
mojingran requested review of this revision.Jul 7 2023, 11:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 7 2023, 11:27 AM
sbc100 accepted this revision.Jul 7 2023, 11:32 AM

Nice! Can we get a test for this?

This revision is now accepted and ready to land.Jul 7 2023, 11:32 AM
mojingran added a comment.EditedJul 7 2023, 11:36 AM

Nice! Can we get a test for this?

I just added you as a reviewer in the test patch :)

sbc100 added inline comments.Jul 7 2023, 11:41 AM
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp
129

Is this error specific to inline assembly?

Looking at global_set_expected_expression_operand in llvm/test/MC/WebAssembly/type-checker-errors.s I see a different error.. so I guess maybe it fails before it gets here in the case to non-inline assembly?

I wonder if we shouldn't just allow this.. even though its kind odd to want to do it..

Nice! Can we get a test for this?

I just added you as a reviewer in the test patch :)

Thanks. Wouldn't it be better to add the test in the same patch as the change its testing?

mojingran added a comment.EditedJul 7 2023, 12:08 PM

Thanks. Wouldn't it be better to add the test in the same patch as the change its testing?

I followed this guide. I thought it might be better style.

llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp
129

Noted, I tried llvm-mc on the assembly generated from my testcase, and it is also giving a different error. I am not sure why it is behaving differently if we split up the 2 stages.

dschuff added inline comments.Jul 7 2023, 1:35 PM
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp
129

I wonder if we shouldn't just allow this.. even though its kind odd to want to do it..

In other architectures there are use cases for hardcoding register names as well as using symbols and letting the regalloc choose. Some of them are for constraints in particular instructions (e.g. instruction that always leaves 2 outputs in rax/rcx), or for ABIs that specify register use. Wasm doesn't really have either one of those. So I think I'd be fine having this limitation until someone proposes a use case for it.

mojingran updated this revision to Diff 538264.EditedJul 7 2023, 2:17 PM

Added the testcase to check for an appropriate error message when an unsupported inline WebAssembly is provided.

bryanpkc added inline comments.Jul 10 2023, 7:58 AM
llvm/test/CodeGen/WebAssembly/inline-asm-failure.ll
3

NIT: Can you add a space between the semi-colon and RUN:? Same thing for the ;CHECK: line below.

4

It might be useful to rename this to "rust-issue-111471.c".

30

Can you remove !0 and !1, as well as !llvm.module.flags and !llvm.ident? These metadata are irrelevant to the test case. Rename !2 to !0.

mojingran updated this revision to Diff 538672.Jul 10 2023, 8:56 AM
mojingran marked 3 inline comments as done.

Abandoned irrelevant metadata for the testcase, and reformatted MCTargetDesc.h.

Fixing testcase

bryanpkc accepted this revision.Jul 10 2023, 10:05 AM

@mojingran Thanks. I think we can land this, unless @sbc100 has more comments.

sbc100 accepted this revision.Jul 10 2023, 10:07 AM

Missing WebAssemblyMCTargetDesc.cpp file added.