Skip to content

wasm2c: report wide arithmetic instructions as unsupported instead of aborting - #2909

Merged
shravanrn merged 1 commit into
WebAssembly:mainfrom
natify-sh:wasm2c-wide-arithmetic-error
Oct 26, 2026
Merged

shravanrn merged 1 commit into
WebAssembly:mainfrom
natify-sh:wasm2c-wide-arithmetic-error

Conversation

@o-murphy

Copy link
Copy Markdown
Contributor

The wide arithmetic opcodes are read without --enable-wide-arithmetic, but the C
writer has no translation for them, so wasm2c died with SIGABRT and no message:

  • i64.add128 and i64.sub128 (quaternary expressions) reached UNIMPLEMENTED,
    which prints to buffered stdout and calls abort(), so nothing was shown;
  • i64.mul_wide_s and i64.mul_wide_u reached WABT_UNREACHABLE in the switch
    over binary expressions.

The module is accepted by wasm-validate and wasm2wat, so the abort comes only
from the C writer.

This handles the quaternary expressions in their own Write, as is done for the
ternary ones, and reports all four instructions with WABT_FATAL, which names the
instruction and exits with status 1:

error: wasm2c does not support the i64.add128 instruction

It does not add the translation of wide arithmetic; it only makes the failure a
diagnostic.

Testing:

  • New test/wasm2c/wide-arithmetic-unsupported.txt runs wasm2c on a module with
    i64.add128 and expects the message and exit status 1. Before the change the
    process aborted with status 134.
  • i64.sub128, i64.mul_wide_s and i64.mul_wide_u give the same message (checked
    by hand, not in the test).
  • The tests in test/wasm2c/ pass on Linux.

Fixes #2895

… aborting

The wide arithmetic opcodes are read without --enable-wide-arithmetic, but the
C writer has no translation for them: i64.add128 and i64.sub128 reached
UNIMPLEMENTED, which prints to buffered stdout and calls abort(), and
i64.mul_wide_s and i64.mul_wide_u reached WABT_UNREACHABLE in the binary
expression switch. wasm2c died with SIGABRT and no message.

Handle the quaternary expressions in their own Write, as for the ternary ones,
and report all four with WABT_FATAL, which names the instruction and exits
with status 1.
@o-murphy
o-murphy marked this pull request as ready for review October 23, 2026 12:11
Comment thread src/c-writer.cc
case ExprType::CallRef:
case ExprType::ReturnCallRef:
case ExprType::Quaternary:
UNIMPLEMENTED("...");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Isn't this UNIMPLEMENTED message enough? Why is it enough for things like CallRef but not for Quaternary

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think the post said that the unimplemented message stays in the stdout buffer since this path calls an abort instead of exit(1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In that case we should instead fix UNIMPLEMENTED to print to stderr I think

Comment thread src/c-writer.cc
expr.opcode.GetName());

default:
WABT_UNREACHABLE;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we instead give a better error here, so that all unsupported instructions get reported nicely?

@shravanrn
shravanrn enabled auto-merge (rebase) October 23, 2026 20:21
@shravanrn
shravanrn merged commit 1f0c418 into WebAssembly:main Oct 26, 2026
17 checks passed
Comment thread src/c-writer.cc
case Opcode::I64MulWideS:
case Opcode::I64MulWideU:
WABT_FATAL("error: wasm2c does not support the %s instruction\n",
expr.opcode.GetName());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is maybe the wrong fix. Shouldn't we have improved the default: error handler instead of just handling these two instructions?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hmm... sorry, looks like i had automerge enabled on this. I'll look into cleaning this up better when I get a chance.

@o-murphy
o-murphy deleted the wasm2c-wide-arithmetic-error branch October 26, 2026 07: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.

wasm2c aborts (SIGABRT) on wide-arithmetic instructions instead of reporting an error

3 participants