verify_ember_movie_native.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """Offline ABI verification against a mapped/decrypted image, never an on-disk encrypted EXE.
  2. Usage: python3 tests/verify_ember_movie_native.py path/to/game_image.bin [packages-directory]
  3. """
  4. import re
  5. import struct
  6. import sys
  7. from pathlib import Path
  8. repo = Path(__file__).resolve().parents[1]
  9. data = Path(sys.argv[1]).read_bytes()
  10. def signature(file, name):
  11. source = (repo / file).read_text()
  12. pattern = re.search(r'constexpr auto ' + name + r'\s*=\s*signature<signature_length\("([^"]+)"\)', source)[1]
  13. regex = b''.join(b'.' if token == '?' else re.escape(bytes([int(token, 16)])) for token in pattern.split())
  14. matches = [m.start() for m in re.finditer(regex, data, re.S)]
  15. assert len(matches) == 1, (name, matches)
  16. return matches[0]
  17. def target(base, offset, expected):
  18. assert data[base + offset] == 0xE8
  19. value = base + offset + 5 + struct.unpack_from('<i', data, base + offset + 1)[0]
  20. assert value == expected, (hex(base), hex(offset), hex(value), hex(expected))
  21. path = 'Sunrise/src/client/hooks/ember_movies/resources.cpp'
  22. load = signature(path, 'loadSig')
  23. end = signature(path, 'endSig')
  24. assert load == 0xB46E10 and end == 0xB44020
  25. surface = signature(path, 'surfaceSig')
  26. assert surface == 0x1202B00
  27. assert surface + 13 + struct.unpack_from('<i', data, surface + 9)[0] == 0x2E800B0
  28. target(0xB5F4C5, 9, surface) # native world activation publishes the surface registration stacks
  29. target(0x1184660, 0x37, 0x1202C20) # renderer fetches selected surface definitions
  30. assert data[0x116A070:0x116A077] == bytes.fromhex('48 83 38 00 0f 95 c0') # missing surface => skip GPU upload
  31. ui = signature('Sunrise/src/client/hooks/bootflow/ember_movie_ui.cpp', 'sig')
  32. assert ui == 0x132BD80
  33. target(0x132B890, 0x353, 0x1278FF0) # native movie command is queued before either UI layer
  34. target(0x132B890, 0x3DF, ui)
  35. target(0x132B890, 0x40C, ui)
  36. for offset, expected in [(0x96, 0x4294D0), (0xD1, 0x423EF0), (0x14C, 0x4312D0), (0x157, 0x435AA0)]:
  37. target(load, offset, expected)
  38. for offset, expected in [(0x85, 0x42C650), (0x9F, 0x425310)]:
  39. target(end, offset, expected)
  40. assert data[end + 0x2E:end + 0x31] == bytes.fromhex('48 8B 05')
  41. assert end + 0x35 + struct.unpack_from('<i', data, end + 0x31)[0] == 0x2439C70
  42. # Native tag classifier, including the semantic distinction missed by the old test:
  43. # ordinary tag -> kind 1; shared type-16 tag (type_info & F000 == 2000) -> kind 2.
  44. assert data[0x42694F:0x42696F] == bytes.fromhex(
  45. '8b 45 04 8b cb 48 89 7c 24 30 25 00 f0 00 00 33 ff 3d 00 20 00 00 40 0f 94 c7 45 33 c0 8d 57 01')
  46. target(0x426920, 0x4F, 0x433050)
  47. # Kind 2 is routed to root+10; ordinary metadata belongs in root+20.
  48. assert data[0x4313CD:0x4313E2] == bytes.fromhex(
  49. '83 3f 02 b9 10 00 00 00 8b 57 04 41 b8 20 00 00 00 44 0f 44 c1')
  50. # For stream type_info & 30000 == 10000, the load job maps offset|patch and
  51. # size|C0000000 directly. It bypasses the ordinary allocation/read branch.
  52. assert data[0x3592C6:0x3592EB] == bytes.fromhex(
  53. '8b c3 c1 e8 10 83 e0 03 83 f8 01 75 2f 41 0f b7 4d 20 41 81 cf 00 00 00 c0 8b 55 50 45 8b c7 48 0b d1 8b 4d 48')
  54. target(0x3591B0, 0x13B, 0x351D00)
  55. target(0x41A160, 0x16, 0x3597C0) # native video I/O opens this mapped package/patch
  56. target(0x41A160, 0x2C, 0x357DA0) # then obtains offset and byte length
  57. movie = 'Sunrise/src/client/hooks/ember_movies/ember_movies.cpp'
  58. start, stop, busy = (signature(movie, name) for name in ('startSig', 'stopSig', 'busySig'))
  59. for offset, expected in [(0x72, 0x41B040), (0x7A, 0x41A3C0), (0x8E, 0x41CD20)]:
  60. target(start, offset, expected)
  61. for offset, expected in [(0x18, 0x41D0C0), (0x25, 0x41A980)]:
  62. target(stop, offset, expected)
  63. assert busy == 0x41B420
  64. target(busy, 0x48, 0x41AB70)
  65. if len(sys.argv) > 2:
  66. # Read only container metadata, without unpacking data or starting the game.
  67. latest = {}
  68. for path in Path(sys.argv[2]).glob('*.pkg'):
  69. with path.open('rb') as stream:
  70. header = stream.read(0x170)
  71. package = struct.unpack_from('<H', header, 4)[0]
  72. version = (struct.unpack_from('<Q', header, 0x10)[0],
  73. struct.unpack_from('<I', header, 0x1C)[0],
  74. struct.unpack_from('<H', header, 0x20)[0])
  75. if package not in latest or version > latest[package][0]:
  76. latest[package] = version, path, header
  77. for tag, expected in [(0x80BCA001, 0x80808495), (0x80BCA003, 0x80808495),
  78. (0x80BCA000, 0x80808499), (0x80BCA002, 0x80808499),
  79. (0x80B9EB33, 0x80809A88), (0x80B9EB34, 0x80809A88),
  80. (0x80BCA032, 0x80806B8F),
  81. (0x80BCA022, 0x80806B91), (0x80BCA025, 0x80806B91),
  82. (0x80BCA028, 0x80806B91), (0x80BCA02B, 0x80806B91),
  83. (0x80BCA02E, 0x80806B91), (0x80BCA031, 0x80806B91),
  84. (0x80BCA034, 0xFFFFFFFF), (0x80C7C000, 0xFFFFFFFF)]:
  85. # Tag package IDs include the bank: 80BCAxxx belongs to package 01E5.
  86. package = (tag >> 13) & 0x3FF
  87. _, path, header = latest[package]
  88. table = (struct.unpack_from('<I', header, 0x110)[0] + 0x60 if header[0x1A] == 1
  89. else struct.unpack_from('<I', header, 0xB8)[0])
  90. with path.open('rb') as stream:
  91. stream.seek(table + (tag & 0x1FFF) * 16)
  92. reference, type_info, _ = struct.unpack('<IIQ', stream.read(16))
  93. assert reference == expected, (hex(tag), hex(reference), path)
  94. assert type_info & 0xF000 != 0x2000, (hex(tag), hex(type_info))
  95. if expected == 0xFFFFFFFF:
  96. assert (type_info & 0x30000) == 0x10000 and (type_info >> 6) & 0x3F == 24
  97. print('Installed movie metadata, compact streams, six surface containers and kind-1 package types verified.')
  98. tags = repo / 'build/first-encounter-audit/tags'
  99. catalog = (tags / '80BCA032.bin').read_bytes()
  100. for i, (container, definition) in enumerate(zip(
  101. [0x80BCA022,0x80BCA025,0x80BCA028,0x80BCA02B,0x80BCA02E,0x80BCA031],
  102. [0x80BCA021,0x80BCA024,0x80BCA026,0x80BCA029,0x80BCA02C,0x80BCA02F])):
  103. assert struct.unpack_from('<I', catalog, 0x38 + 16*i)[0] == container
  104. assert (tags / f'{container:08X}.bin').read_bytes() == struct.pack('<I', definition)
  105. assert (tags / f'{definition:08X}.bin').read_bytes()[0] == i + 1
  106. print('Six authored Y/U/V definitions map to the renderer slots 1..6.')
  107. attach = signature('Sunrise/src/client/hooks/bootflow/ember_sunburn.cpp', 'sig')
  108. assert attach == 0x9F2760
  109. # Native attach dereferences the runtime relative template, then passes it to the child factory.
  110. target(attach, 0x68, 0x32BBD0)
  111. target(attach, 0x78, 0x56DE00)
  112. assert data[0x4AE000:0x4AE002] == bytes.fromhex('8B 09') # factory reads resource at request+0
  113. print('Native resource kind selection, request lifecycle, movie playback and sunburn attachment ABI verified.')