On this page
The oracle: asking the original for the answer
The surest way to know what a function of the original returns is to call it. The oracle does that: it loads one of the original program files into a small process of its own, calls a single function with inputs it made up, and compares the answer with what the port's Rust gives for the same inputs. No game runs, no window opens, nothing is rendered, and the comparison is of bits.
Why it exists#
Reading a function tells you what it does as you understood it. The port was written that way, and reading is where its errors come from: an operand order, a rounding, a branch on a NaN. A test written from the same reading repeats the same error. The oracle takes the reader out of the loop: the expected value comes from the original's own instructions.
The host#
A small 32-bit program (tools/retail_oracle/oracle_host.c):
- maps one original image (an executable or a library) per process, at its link base when that range is free, otherwise elsewhere with its relocations applied;
- runs nothing of the image by itself: no entry point, no library initialiser, no static constructor;
- gives the image the C runtime it was built against, and makes every other import a trap: a function that reaches for the file system, the network or the graphics device fails its case instead of doing anything;
- turns an access violation or a trap inside a call into a result ("fault at this address") and goes on with the next call.
The images are read from your own install and staged in a scratch folder; the install is never written.
A case#
A case (tools/retail_oracle/oracle_cases.yaml) names the function's address, how to call it, the input vectors, and the Rust expression that should agree. The calling convention is read from the listing:
| The listing shows | So the case passes |
|---|---|
the function reads ecx before writing it | this in ecx (a method) |
it reads [ebp + 8], [ebp + 0xc] .. | stack words in that order |
it reads edx, esi or edi before writing them | those registers: conventions the linker invented for this one function |
it ends by loading its first stack word into eax | a buffer for a struct returned by value |
| it leaves a value on the x87 stack | a float or double result |
Arguments can be numbers, buffers that are dumped after the call, structs with fields at offsets, a std::string in the compiler's own layout (28 bytes: the text or a pointer to it, the size, the capacity), vectors, and objects whose virtual tables point at host stubs that record how they were called.
Precision is part of the case#
Every case says which x87 precision it runs at: 24 bits for client code (the clients set single precision, as Direct3D asks) and 53 bits for the dedicated servers. The Rust side takes the same setting. A function the client and the server both have is two cases, and they can differ: float precision has what that costs.
Comparison is exact by default: the float's bits, with NaN equal to NaN. A few Heroes cases allow two units in the last place, where its newer compiler sums products in another order than the 2142 code the shared Rust follows; the case says so.
What the plain host cannot call#
Because no static initialiser runs, a function that reads a table filled at start-up sees zeros, and a method that calls through a manager's virtual table has no manager. Three answers:
- Run the initialiser first, in the same case, with its callees hooked to record what they are handed.
- The emulator. One statement or one block of a function runs in a CPU emulator over generated inputs, once per precision, and the results are stored as rows that a Rust test must reproduce. Such a test holds that statement and says the function is not held whole. Client-only code that needs a device (the death camera, parts of the HUD) is checked this way: 6,000 frames of the client's camera code, for instance.
- Inside the running original. For Heroes, the same case list can be run inside the original program after it has started, on this machine and joined to nothing: the functions then see real managers and a loaded level. Results of that route are kept, so the plain cases can be rerun without it.
Older oracles, same idea#
Before the common host, each subsystem built its own: generators under oracle/ that call the original's functions on thousands of random inputs and write fixtures, and Rust tests tests/*_oracle.rs that replay them. Most of the "verified" marks on this wiki's physics, vehicle, animation and audio pages are those:
| Subsystem | Size of the check |
|---|---|
| the engine's update (classic engines) | 10,000 ticks at the server's precision, 6,000 each for three more builds and precisions |
| vehicle component nodes | original bodies on original terrain, 200 ticks a case |
| animation sampling | 139,176 cases a precision |
| projectile ricochet and penetration | 20,000 cases a function and precision |
| soldier control on the client | 36,000 ticks at 24 bits, 18,000 at 53 |
Generated cases#
A generator sorts every ported Heroes function by whether it can be called blind: auto (pure: reads no writable global, makes no virtual call, takes numbers and arrays), hand (pure, but the Rust side needs a hand-written call), stateful (needs the running program). When it was first run, of 6,265 ported functions 6 were auto, 899 hand and 5,340 stateful: most of a game is methods on objects. For each callable function it makes vectors from the edges (0, -0, 1, -1, NaN, the infinities, the largest floats, denormals, the integer limits) and seeded random values, runs both sides, and keeps the cases that agree; the disagreements are written out with both outputs.
What it found#
The oracle earns its keep on the cases where reading failed:
- a date formatter: the original's C runtime accepts times from half a day before 1970 to the end of the year 3000 plus 21 hours, not the range one would assume; a date of -1 prints as the last second of 1969;
- number formatting: the original's
%fprints 17 significant digits and then zeros for a huge float, and spells NaN and infinity in the old Microsoft way; the port printed the exact decimal; - a clamp built from conditional moves returns the lower bound for NaN where the port returned NaN;
- a sum of three squares added in another order than the port's.
And it is itself tested by mutation: with one entry of a rank table changed from 600 to 601 the case failed and printed both tables.
In the count#
A function with a passing oracle case is tested in the strict sense: class a1 of the parity count, the same as a test written from its address. The counter reads the oracle's result file.
How it is tested#
The oracle is itself tested: its generator has tests beside it (tools/retail_oracle), a case whose original call faults is a recorded result and not a crash, and every case's inputs go to the original and to the port alike. A function counts as strictly tested through the oracle only while its case passes (parity). What the oracle cannot tell is whether the inputs are the ones a game produces; that is what scenarios and the differentials add.
In the code#
| What | Where |
|---|---|
| the host and its runner | tools/retail_oracle/oracle_host.c, tools/retail_oracle/retail_oracle.py |
| the cases | tools/retail_oracle/oracle_cases.yaml, oracle_cases_gen.yaml |
| the Rust shims | crates/retail-oracle-rs |
| the generator | tools/retail_oracle/oracle_gen.py |
| the older per-subsystem oracles | oracle/ and crates/*/tests/*_oracle.rs |
python tools\retail_oracle\retail_oracle.py list
python tools\retail_oracle\retail_oracle.py run --case <id> -v
cargo test -p retail-oracle-rsNotes: notes/retail_oracle.md, notes/tracks/validation-strategy.md. See also the test rigs and how a port is made.
docs/dev/testing/oracle.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.