Kelyje II · caught · week of
It compiled, and 104 of 211 classes were wrong
In C++, the order you declare virtual methods in is part of the binary contract. Declare them in a different order and every name still matches, the code still compiles, and calls land on the wrong methods.
Kelyje II: the Lithuanian release of Rig'n'Roll, a truck game from 2000. I am rewriting it as C++ a person can read · kelyje2.lt
I test the C++ rewrite by building it as a DLL and loading it into the original game, where each rewritten function takes over from the original's. The first build compiled cleanly. Loaded, it crashed within seconds. This was before it matched the original.
What a wrong slot is
A C++ class with virtual methods keeps a table of them, the vtable, and calls go by position: call whatever is in slot 2. The original binary fixed those positions in 2000 and still calls by them. If the rewrite declares the same methods in a different order, the original asks for slot 2 and gets a different method.
slot original calls rewrite has 0 destructor destructor 1 update draw wrong 2 draw update wrong
That class is made up. In the real one that gave it away, a method sat at offset 0xb0 (slot 44) in the game and 0x64 (slot 25) in my header.
Reading the answer out of the binary
I had no source code showing the original method order, so I wrote a checker. Every constructor writes the address of its class's vtable into the new object. The checker reads that address from each constructor, finds the table in the game's executable, and compares it slot by slot with the table the compiler produces from my headers.
It checked 211 classes and 1,242 slots: 104 classes and 621 slots were wrong. The fix was bookkeeping: give each class its base classes in the right order, make each override really override its base method, and add the missing placeholder slots. Now all 1,242 match.
What else the same runs turned up
None of these stops the compiler. Each showed itself within seconds of running:
- a menu that stopped taking input because its vtable lacked one override
- a function with the wrong calling convention, so both caller and callee popped the stack
- 18 destructors that called themselves
- a function that assigned a truck's body height to itself
- a lookup that compared the wrong column, so the game insisted there was no such vehicle as a Fiat
- an allocator that deadlocked by taking its own lock twice
And the DLL itself first loaded at 0x10000000, on top of memory the game had already mapped.
I'm Ovi. I recompile old Windows games to run in a browser, build a virtual GPU for Windows XP, and write tools for my own machines, with AI since 2020. Projects and the latest log