From The Old New Thing · Raymond Chen · · 1m 40s
Why x86's undefined instruction is called ud2
Two rival byte sequences, one broken processor upgrade, and the reason Intel skipped straight to number two.
In brief
Two rival byte sequences, one broken processor upgrade, and the reason Intel skipped straight to number two. ud2 exists because programmers had already standardised on two unofficial crash opcodes, so Intel made a third one official and renamed the old ones ud0 and ud1. Originally reported by The Old New Thing.
The crash on purpose
ud2 is an x86 instruction guaranteed to raise an invalid opcode exception. Compilers drop it into unreachable code so a wrong turn crashes loudly instead of running whatever bytes come next.
“an architecturally undefined instruction, guaranteed to raise an "invalid opcode" exception”
Two rival byte sequences
Before Intel blessed anything, programmers hunted for bytes that reliably faulted. One camp found 0F FF, another found 0F B9, and both worked well enough that neither side won.
“you now had two factions, the 0F FF believers and the 0F B9 adherents”
The next chip broke it
Intel changed something on a later processor and 0F FF stopped faulting. Software started failing, and the investigation traced it back to programs depending on an opcode nobody had promised anything about.
Hyrum's Law in silicon
With enough users, every observable behaviour becomes someone's dependency. An accidental crash became a contract Intel had to honour, and 0F B9 turned out to have the same problem.
“With a sufficient number of users, all observable behaviors will be depended upon by somebody.”
Why the name starts at 2
Intel made the behaviour official and retroactively named the old sequences ud0 and ud1. That left ud2 as the recommended one, a clean two-byte instruction with no operands to decode.
Phantom operands bite back
ud0 and ud1 decode a register and a memory operand they never use. If that decoding runs off into a missing page, you get an access violation instead of the invalid opcode you wanted.
“even though the processor doesn't use it, it still decodes it”
The main idea
ud2 exists because programmers had already standardised on two unofficial crash opcodes, so Intel made a third one official and renamed the old ones ud0 and ud1.
