Files
xboxrecomp/tools/recomp/test_lifter_bit_string.py
sp00nz 5633c132f0 lifter: bt/bts on memory is a bit string, not one dword
With a memory bit base, x86 does not mask the offset. The operand addresses
the byte holding bit 0 and the offset then runs over the whole string, so the
hardware reads the dword at base + (offset/32)*4 and takes bit offset%32.
Masking to 31 is right only for a register bit base, where the offset really
is taken modulo the operand size -- applied to memory it folds the entire
string onto its first dword.

MSVC builds strpbrk, strspn and strcspn out of exactly this shape: eight zero
dwords pushed as a 256-bit character map, "bts [esp], eax" per character of
the set, "bt [esp], eax" per character of the string. Folded onto one dword
the map aliases mod 32, and '?' (0x3F) sets the same bit that '_' (0x5F)
tests.

Half-Life 2's CRT stats every path for wildcards before opening it, and
rejects one that has them. So every path containing an underscore came back
"contains a wildcard" and failed with ENOENT, while paths without one opened
normally -- which is why it looked like a content problem rather than a string
one. Its archives are zip0_xbox.xzp and zip0_xbox_english.xzp: the pack scan
found nothing, no search path ever gained a pack file, and the engine ran its
whole main loop with no materials to draw.

Both halves are fixed: the standalone lift and the fused bt+jcc, since the
loop that does the testing is "bt [esp], eax" fused with the jae after it. An
immediate offset is genuinely limited to 0..31 of the addressed dword, so it
keeps the simple form.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BuW5WGcXFFrLAYEfvCNGs1
2026-09-04 20:43:13 -07:00

73 lines
2.9 KiB
Python

"""bt/bts with a memory bit base address a bit string, not one dword.
MSVC builds strpbrk, strspn and strcspn out of a 256-bit character map pushed
onto the stack: eight zero dwords, "bts [esp], eax" for each character of the
set, "bt [esp], eax" for each character of the string. Masking the offset to 31
folds all eight dwords onto the first, and the map aliases mod 32 -- '?' (0x3F)
sets the bit '_' (0x5F) then tests.
Half-Life 2's CRT stats a path for wildcards before opening it, so every path
containing an underscore was rejected as if it held a '?'. Its archives are
zip0_xbox.xzp and zip0_xbox_english.xzp, so the engine found no content at all
while paths without an underscore opened normally.
"""
import unittest
from .disasm import BasicBlock, Instruction, Operand
from .lifter import Lifter, lift_basic_block
def _mem_esp():
return Operand(type="mem", mem_base="esp", mem_index=None, mem_scale=1,
mem_disp=0, mem_size=4)
def _lift(mnemonic, bit_operand):
insn = Instruction(0, 4, mnemonic, "dword ptr [esp], eax", "0fab0424")
insn.operands = [_mem_esp(), bit_operand]
lifter = Lifter()
lifter.needs_cf = True
lifted, _ = lift_basic_block(
lifter, BasicBlock(start=0, instructions=[insn]))
return chr(10).join(lifted)
class BitStringTest(unittest.TestCase):
def test_bts_selects_the_dword_by_offset(self):
generated = _lift("bts", Operand(type="reg", reg="eax"))
self.assertIn("(((int32_t)(eax) >> 5) * 4)", generated)
self.assertIn("(eax) & 31", generated)
def test_bt_selects_the_dword_by_offset(self):
generated = _lift("bt", Operand(type="reg", reg="eax"))
self.assertIn("(((int32_t)(eax) >> 5) * 4)", generated)
def test_immediate_offset_stays_within_the_addressed_dword(self):
# An immediate bit offset really is limited to 0..31 of the dword the
# operand names, so it must NOT be turned into a string index.
generated = _lift("bts", Operand(type="imm", imm=7))
self.assertNotIn(">> 5", generated)
def test_register_bit_base_still_masks(self):
insn = Instruction(0, 3, "bts", "edx, eax", "0fabc2")
insn.operands = [Operand(type="reg", reg="edx"),
Operand(type="reg", reg="eax")]
lifter = Lifter()
lifter.needs_cf = True
lifted, _ = lift_basic_block(
lifter, BasicBlock(start=0, instructions=[insn]))
generated = chr(10).join(lifted)
self.assertIn("eax & 31", generated)
self.assertNotIn(">> 5", generated)
class StrpbrkMapTest(unittest.TestCase):
"""The aliasing the bug produced, stated as arithmetic."""
def test_question_mark_and_underscore_alias_when_folded(self):
self.assertEqual(ord("?") & 31, ord("_") & 31)
def test_they_do_not_alias_as_a_bit_string(self):
self.assertNotEqual((ord("?") >> 5, ord("?") & 31),
(ord("_") >> 5, ord("_") & 31))