修正了 TPC 的一些错误,包括 Test 维护后 TPV 无法重新编译或重编译后越界的部分问题
This commit is contained in:
@@ -8,6 +8,7 @@ import llvmlite
|
||||
import w32.win32file as win32file
|
||||
import w32.win32base as win32base
|
||||
import w32.fileio as fileio
|
||||
import w32.win32memory as win32memory
|
||||
import viperlib
|
||||
import sys
|
||||
import ast
|
||||
@@ -69,6 +70,7 @@ def _load_includes_sha1_set(pool: memhub.MemBuddy | t.CPtr,
|
||||
f: fileio.File | t.CPtr = fileio.File(map_path, fileio.MODE.R)
|
||||
if f.closed:
|
||||
stdio.printf("[StubMerger] _sha1_map.txt 不存在: %s\n", map_path)
|
||||
stdlib.free(map_path)
|
||||
return -1
|
||||
|
||||
# 读取内容(使用 stdlib.malloc 避免 mbuddy 池耗尽)
|
||||
@@ -76,10 +78,13 @@ def _load_includes_sha1_set(pool: memhub.MemBuddy | t.CPtr,
|
||||
content: str = stdlib.malloc(MAP_BUF_SIZE)
|
||||
if content is None:
|
||||
f.close()
|
||||
stdlib.free(map_path)
|
||||
return -1
|
||||
bytes_read: t.CInt64T = f.read_all(content, MAP_BUF_SIZE)
|
||||
f.close()
|
||||
stdlib.free(map_path)
|
||||
if bytes_read <= 0:
|
||||
stdlib.free(content)
|
||||
return -1
|
||||
content[bytes_read] = '\0'
|
||||
|
||||
@@ -112,6 +117,7 @@ def _load_includes_sha1_set(pool: memhub.MemBuddy | t.CPtr,
|
||||
sha1_set[count * 17 + 16] = '\0'
|
||||
count += 1
|
||||
|
||||
stdlib.free(content)
|
||||
return count
|
||||
|
||||
|
||||
@@ -189,6 +195,8 @@ def WriteIncludesSha1Map(mb: memhub.MemBuddy | t.CPtr, temp_dir: str,
|
||||
written_count += 1
|
||||
|
||||
f.close()
|
||||
stdlib.free(map_path)
|
||||
stdlib.free(line_buf)
|
||||
stdio.printf("[Phase1] 已写入 _sha1_map.txt (%d 个 includes)\n", written_count)
|
||||
return 0
|
||||
|
||||
@@ -735,14 +743,32 @@ def _BuildIncludesSha1Map(temp_dir: str, td_len: t.CSizeT,
|
||||
map_buf[map_br] = '\0'
|
||||
else:
|
||||
map_buf[STUB_READ_BUF_SIZE - 1] = '\0'
|
||||
stdio.printf("[BISM] map_br=%lld\n", map_br)
|
||||
stdio.fflush(0)
|
||||
|
||||
# 堆探针:测试堆是否已被之前的操作损坏
|
||||
probe: bytes = stdlib.malloc(17)
|
||||
stdio.printf("[BISM] probe malloc(17)=%p\n", probe)
|
||||
stdio.fflush(0)
|
||||
if probe is not None:
|
||||
stdlib.free(probe)
|
||||
stdio.printf("[BISM] probe free OK\n")
|
||||
stdio.fflush(0)
|
||||
|
||||
count: int = 0
|
||||
pos: t.CSizeT = 0
|
||||
while pos < map_br and count < MAX_INCLUDES:
|
||||
stdio.printf("[BISM] iter=%d pos=%d\n", count, pos)
|
||||
stdio.fflush(0)
|
||||
# 提取 SHA1(16 hex)
|
||||
sha1: t.CChar | t.CPtr = stdlib.malloc(17)
|
||||
stdio.printf("[BISM] malloc(17)=%p\n", sha1)
|
||||
stdio.fflush(0)
|
||||
if sha1 is None:
|
||||
break
|
||||
hv: int = win32memory.HeapValidate(win32memory.GetProcessHeap(), 0, None)
|
||||
stdio.printf("[BISM] HV after malloc(sha1)=%d\n", hv)
|
||||
stdio.fflush(0)
|
||||
string.strncpy(sha1, map_buf + pos, 16)
|
||||
sha1[16] = '\0'
|
||||
pos += 16
|
||||
@@ -755,6 +781,8 @@ def _BuildIncludesSha1Map(temp_dir: str, td_len: t.CSizeT,
|
||||
while pos < map_br and map_buf[pos] != '\n' and map_buf[pos] != '\r' and map_buf[pos] != '\0':
|
||||
pos += 1
|
||||
path_len: t.CSizeT = pos - path_start
|
||||
stdio.printf("[BISM] path_len=%d\n", path_len)
|
||||
stdio.fflush(0)
|
||||
# 跳过 \r\n 或 \n
|
||||
if pos < map_br and map_buf[pos] == '\r':
|
||||
pos += 1
|
||||
@@ -765,25 +793,49 @@ def _BuildIncludesSha1Map(temp_dir: str, td_len: t.CSizeT,
|
||||
# 否则用户文件会被误判为 includes,导致 is_module_imported 模块名不匹配而跳过加载
|
||||
if path_len >= 9 and string.strncmp(map_buf + path_start, "includes/", 9) == 0:
|
||||
path_buf: t.CChar | t.CPtr = stdlib.malloc(path_len + 1)
|
||||
stdio.printf("[BISM] malloc(%d)=%p\n", path_len + 1, path_buf)
|
||||
stdio.fflush(0)
|
||||
if path_buf is not None:
|
||||
string.strncpy(path_buf, map_buf + path_start, path_len)
|
||||
path_buf[path_len] = '\0'
|
||||
hv2: int = win32memory.HeapValidate(win32memory.GetProcessHeap(), 0, None)
|
||||
stdio.printf("[BISM] HV after fill(path_buf)=%d\n", hv2)
|
||||
stdio.fflush(0)
|
||||
mod_name: str = _PathToModuleName(path_buf)
|
||||
stdio.printf("[BISM] _PathToModuleName=%p\n", mod_name)
|
||||
stdio.fflush(0)
|
||||
hv3: int = win32memory.HeapValidate(win32memory.GetProcessHeap(), 0, None)
|
||||
stdio.printf("[BISM] HV after _PathToModuleName=%d\n", hv3)
|
||||
stdio.fflush(0)
|
||||
stdlib.free(path_buf)
|
||||
if mod_name is not None:
|
||||
# 存储 sha1 和 mod_name 到数组中
|
||||
idx: t.CSizeT = t.CSizeT(count) * 17
|
||||
string.strcpy(sha1_list + idx, sha1)
|
||||
hv4: int = win32memory.HeapValidate(win32memory.GetProcessHeap(), 0, None)
|
||||
stdio.printf("[BISM] HV after strcpy(sha1_list)=%d\n", hv4)
|
||||
stdio.fflush(0)
|
||||
idx2: t.CSizeT = t.CSizeT(count) * 64
|
||||
mn_len: t.CSizeT = string.strlen(mod_name)
|
||||
stdio.printf("[BISM] mn_len=%d count=%d\n", mn_len, count)
|
||||
stdio.fflush(0)
|
||||
if mn_len < 64:
|
||||
string.strcpy(mod_list + idx2, mod_name)
|
||||
else:
|
||||
string.strncpy(mod_list + idx2, mod_name, 63)
|
||||
mod_list[idx2 + 63] = '\0'
|
||||
hv5: int = win32memory.HeapValidate(win32memory.GetProcessHeap(), 0, None)
|
||||
stdio.printf("[BISM] HV after strcpy(mod_list)=%d\n", hv5)
|
||||
stdio.fflush(0)
|
||||
stdlib.free(mod_name)
|
||||
stdio.printf("[BISM] free(mod_name) OK\n")
|
||||
stdio.fflush(0)
|
||||
count += 1
|
||||
stdio.printf("[BISM] before free(sha1=%p)\n", sha1)
|
||||
stdio.fflush(0)
|
||||
stdlib.free(sha1)
|
||||
stdio.printf("[BISM] free(sha1) OK\n")
|
||||
stdio.fflush(0)
|
||||
stdlib.free(map_buf)
|
||||
return count
|
||||
|
||||
|
||||
Reference in New Issue
Block a user