Skip to content

Specifying -Z trap-unreachable=no in example-crates/tiny will have no effect #163

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

Closed
nagornin opened this issue May 11, 2025 · 1 comment · Fixed by #166
Closed

Specifying -Z trap-unreachable=no in example-crates/tiny will have no effect #163

nagornin opened this issue May 11, 2025 · 1 comment · Fixed by #166

Comments

@nagornin
Copy link

nagornin commented May 11, 2025

https://github.com/sunfishcode/origin/tree/d0c8ff8c6b4e39b8cb6c2571e2db0a431c2a1f71/example-crates/tiny

Finally, we add a RUSTFLAGS flag with .cargo/config.toml:

rustflags = ["-Z", "trap-unreachable=no"]

This disables the use of trap instructions, such as ud2 on x86-64, at places the compiler thinks should be unreachable, such as after the jmp in _start or after the syscall that calls exit_group, because rustix uses the noreturn inline asm option. Normally this is a very good thing, but it does take a few extra bytes.

This will not prevent an ud2 instruction from being generated after the exit_group syscall on the latest version of rustix due to bytecodealliance/rustix#1444, discussed in bytecodealliance/rustix#1433. Perhaps this should be removed from example-crates/tiny to prevent confusion. Anyone who wants to save space by omitting the ud2 instruction can use the syscalls crate to issue the relevant syscall directly.

As an alternative, bytecodealliance/rustix#1444 can be put behind a feature gate, something like seccomp-bpf-compat, or as an opt-out, no-seccomp-bpf-compat.

@sunfishcode
Copy link
Owner

Interesting. trap-unreachable=no still does prevent a ud2 in places the compiler thinks should be unreachable. Without it, and with bytecodealliance/rustix#1444, we now get two ud2s. trap-unreachable=no makes it only have one ud2. Agreed, we should update the README.md to mention that.

I'm not super concerned about implementing every last optimization; the README.md already describes a few others that aren't implemented as well. And at some point, if people really want the smallest possible binary where every possible byte is optimized out, they'll need to do this instead anyway.

sunfishcode added a commit that referenced this issue May 14, 2025
Update the documentation for the tiny example to explain why the
code now has a ud2 in it.

Fixes #163.
sunfishcode added a commit that referenced this issue May 15, 2025
Update the documentation for the tiny example to explain why the
code now has a ud2 in it.

Fixes #163.
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 a pull request may close this issue.

2 participants