Skip to content

Commit 4843129

Browse files
authored
Document why the tiny example gets a ud2 now. (#166)
Update the documentation for the tiny example to explain why the code now has a ud2 in it. Fixes #163.
1 parent 00eef83 commit 4843129

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

example-crates/tiny/README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,27 @@ does take a few extra bytes.
181181
With all these optimizations, the generated code looks like this:
182182

183183
```asm
184-
00000000004000b0 <.text>:
185-
4000b0: 48 89 e7 mov %rsp,%rdi
186-
4000b3: 55 push %rbp
187-
4000b4: e9 00 00 00 00 jmp 0x4000b9
188-
4000b9: 6a 2a push $0x2a
189-
4000bb: 5f pop %rdi
190-
4000bc: b8 e7 00 00 00 mov $0xe7,%eax
191-
4000c1: 0f 05 syscall
184+
00000000002000cc <.text>:
185+
2000cc: 48 89 e7 mov %rsp,%rdi
186+
2000cf: 55 push %rbp
187+
2000d0: e9 00 00 00 00 jmp 0x2000d5
188+
2000d5: 6a 2a push $0x2a
189+
2000d7: 5f pop %rdi
190+
2000d8: b8 e7 00 00 00 mov $0xe7,%eax
191+
2000dd: 0f 05 syscall
192+
2000df: 0f 0b ud2
192193
```
193194

194195
Those first 3 instructions are origin's `_start` function. The next 5
195196
instructions are `origin::program::entry` and everything, including the user
196197
`origin_main` function and the `exit_group` syscall inlined into it.
197198

199+
Even though we added "trap-unreachable=no", we still have a ud2 instruction
200+
after the syscall. It's added by rustix because in theory users could run
201+
the program under a seccomp configuration in which `exit_group` does return,
202+
and rustix needs to be completely sure that execution won't fall through into
203+
whatever instructions happen to appear next in memory.
204+
198205
## Optimizations not done
199206

200207
In theory this code be made even smaller.
@@ -233,6 +240,11 @@ saving 2 bytes. In theory origin could have a feature to enable this, however
233240
it's a very minor optimization, and it would introduce undefined behavior if
234241
somehow some thread got created outside of origin, so I chose not to add it.
235242

243+
We could also add an option to rustix to have it omit the `ud2` after the
244+
`exit_group` syscall for users willing to promise that they won't run the
245+
program under a pathological seccomp configuration, however it'd only save
246+
2 bytes in an uncommon situation.
247+
236248
## Sources
237249

238250
Many of these optimizations came from the following websites:

0 commit comments

Comments
 (0)