mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-22 21:15:57 +00:00
Merge pull request #1349 head
This commit is contained in:
commit
7949484a40
2 files changed
+69
-2
No files matched your search
@@ -10,6 +10,8 @@ import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from unittest import mock
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT / 'tools'))
|
||||
@@ -106,6 +108,65 @@ class PackTest(unittest.TestCase):
|
||||
n, _, _, _ = psc.pack(self.cache)
|
||||
self.assertEqual(n, 4)
|
||||
|
||||
def bounded_read_pack(self, path):
|
||||
"""Record real file reads; fail before a corrupt count can request a large allocation, even before the fix."""
|
||||
requests = []
|
||||
with path.open('rb') as f:
|
||||
read = f.read
|
||||
|
||||
def bounded(n):
|
||||
requests.append(n)
|
||||
self.assertLessEqual(n, 1024, 'corrupt index count reached the allocating read')
|
||||
return read(n)
|
||||
|
||||
with mock.patch.object(psc, 'open', return_value=f, create=True), \
|
||||
mock.patch.object(f, 'read', side_effect=bounded):
|
||||
result = psc.read_pack(path)
|
||||
return result, requests
|
||||
|
||||
def test_truncated_index_is_rejected_before_reading_it(self):
|
||||
path = self.cache / psc.PACK
|
||||
for count in (1, 0xffffffff):
|
||||
with self.subTest(count=count):
|
||||
path.write_bytes(psc.MAGIC + struct.pack('<II', count, 0))
|
||||
result, requests = self.bounded_read_pack(path)
|
||||
self.assertEqual(result, {})
|
||||
self.assertEqual(requests, [16])
|
||||
|
||||
def test_runtime_index_limit_is_checked_before_reading_it(self):
|
||||
path = self.cache / psc.PACK
|
||||
count = (1 << 24) + 1 # gfx_shader.c pack_open rejects more than 2**24 entries
|
||||
path.write_bytes(psc.MAGIC + struct.pack('<II', count, 0))
|
||||
with mock.patch.object(psc.os, 'fstat', return_value=SimpleNamespace(st_size=16 + 48 * count)):
|
||||
result, requests = self.bounded_read_pack(path)
|
||||
self.assertEqual(result, {})
|
||||
self.assertEqual(requests, [16])
|
||||
|
||||
def test_short_read_after_the_size_check_is_still_rejected(self):
|
||||
path = self.cache / psc.PACK
|
||||
path.write_bytes(psc.MAGIC + struct.pack('<II', 1, 0) + bytes(24))
|
||||
with mock.patch.object(psc.os, 'fstat', return_value=SimpleNamespace(st_size=64)):
|
||||
result, requests = self.bounded_read_pack(path)
|
||||
self.assertEqual(result, {})
|
||||
self.assertEqual(requests, [16, 48])
|
||||
|
||||
def test_a_truncated_pack_is_rebuilt_from_the_loose_files(self):
|
||||
key = self.shader('a')
|
||||
(self.cache / psc.PACK).write_bytes(psc.MAGIC + struct.pack('<II', 4096, 0))
|
||||
n, new, skipped, size = psc.pack(self.cache)
|
||||
self.assertEqual((n, new, skipped), (4, 4, 0))
|
||||
self.assertEqual(size, (self.cache / psc.PACK).stat().st_size)
|
||||
self.assertEqual(read_pack(self.cache / psc.PACK)[(1, key)], b'\x03\x02\x23\x07spirva')
|
||||
|
||||
def test_a_valid_empty_pack_is_not_rewritten(self):
|
||||
path = self.cache / psc.PACK
|
||||
data = psc.MAGIC + struct.pack('<II', 0, 0)
|
||||
path.write_bytes(data)
|
||||
before = path.stat().st_mtime_ns
|
||||
self.assertEqual(psc.pack(self.cache), (0, 0, 0, len(data)))
|
||||
self.assertEqual(path.read_bytes(), data)
|
||||
self.assertEqual(path.stat().st_mtime_ns, before)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -26,6 +26,7 @@ PACK = 'shader-cache.pack'
|
||||
KINDS = {'.spv': 1, '.res.json': 2, '.use': 3, '.in': 4}
|
||||
NAME = re.compile(r'([0-9a-f]{64})(\.spv|\.res\.json|\.use|\.in)')
|
||||
USE_SIZE = 20 # gfx_shader.c use_store: magic, version, three masks
|
||||
MAX_ENTRIES = 1 << 24 # gfx_shader.c pack_open's index limit
|
||||
|
||||
|
||||
def loose_files(cache):
|
||||
@@ -58,8 +59,13 @@ def read_pack(path):
|
||||
print(f'{path}: not a cache pack, rebuilt from the loose files only', flush=True)
|
||||
return {}
|
||||
count = struct.unpack_from('<I', head, 8)[0]
|
||||
raw = f.read(48 * count)
|
||||
if len(raw) != 48 * count:
|
||||
index_size = 48 * count
|
||||
# Check the count before read() allocates its buffer: a damaged header can request hundreds of GiB.
|
||||
if count > MAX_ENTRIES or index_size > size - 16:
|
||||
print(f'{path}: invalid index size, rebuilt from the loose files only', flush=True)
|
||||
return {}
|
||||
raw = f.read(index_size)
|
||||
if len(raw) != index_size:
|
||||
print(f'{path}: truncated index, rebuilt from the loose files only', flush=True)
|
||||
return {}
|
||||
out = {}
|
||||
|
||||
Reference in new issue
Block a user