source_tree: pointer casts through GHIDRA_CAST (a float holds the pointer's bits); ApplyTypes: zero-length placeholders
MSVC copies pointers through XMM registers, so Ghidra types some as float: those casts now reinterpret the bits (float-to-pointer errors 2415 -> 171). Classes with no known field get a zero-length struct, so the decompiler stops typing 4-byte values with them (applies next build). The hazard count now covers every raw (char *)x + n access: 32689. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bafef76746
commit
2ff2d5d87f
319 files changed
+100751
-100730
No files matched your search
@@ -106,7 +106,7 @@ public class ApplyTypes extends GhidraScript {
|
||||
int typed = 0;
|
||||
for (int pass = 0; pass < 3; pass++) // later passes embed structs (Entity._sprite is an ANM2) built by earlier ones
|
||||
for (String cls : classes) {
|
||||
StructureDataType s = new StructureDataType(cls, 4);
|
||||
StructureDataType s = new StructureDataType(cls, 0); // zero-length until fields are known: only usable behind a pointer
|
||||
addFields(s, cls, 0);
|
||||
if (s.getLength() < size.getOrDefault(cls, 0)) s.growStructure(size.get(cls) - s.getLength()); // so this[1] stops standing in for the tail
|
||||
if (s.getLength() % 4 != 0) s.growStructure(4 - s.getLength() % 4); // MSVC pads to the alignment: arrays of it get the real stride
|
||||
|
||||
+18
-2
@@ -60,6 +60,11 @@ typedef __INTPTR_TYPE__ intptr_t; typedef __UINTPTR_TYPE__ uintptr_t;
|
||||
// (int)x / (uint)x as decompiled: a number stays 32-bit, a pointer keeps its full width (pointer math on 64-bit)
|
||||
template <class T> inline auto GHIDRA_INT(T v) { if constexpr (__is_pointer(T) || __is_class(T)) return (intptr_t)v; else return (int)v; }
|
||||
template <class T> inline auto GHIDRA_UINT(T v) { if constexpr (__is_pointer(T) || __is_class(T)) return (uintptr_t)(intptr_t)v; else return (uint)v; }
|
||||
// A pointer cast as decompiled: a float or double operand holds the pointer's bits (MSVC copies through XMM registers)
|
||||
template <class To, class From> inline To GHIDRA_CAST(From v) {
|
||||
if constexpr (__is_same(From, float) || __is_same(From, double)) { uintptr_t bits = 0; __builtin_memcpy(&bits, &v, sizeof(v) < sizeof(bits) ? sizeof(v) : sizeof(bits)); return (To)bits; }
|
||||
else return (To)v;
|
||||
}
|
||||
// A smart pointer the decompiled code handles as a raw pointer: built from 0 or a pointer, converts back, compares
|
||||
#define GHIDRA_POINTER_LIKE(Self) \
|
||||
void *_ptr; \
|
||||
@@ -228,6 +233,17 @@ def int_casts_rewrite(text): # (int)expr -> GHIDRA_INT(expr), (uint)expr -> GH
|
||||
return ''.join(out) + text[pos:]
|
||||
|
||||
|
||||
def pointer_casts_rewrite(text): # (T *)expr -> GHIDRA_CAST<T *>(expr)
|
||||
out, pos = [], 0
|
||||
for match in re.finditer(r'\(((?:struct )?(?:\w+::)*\w+ ?\*+)\)', text):
|
||||
if match.start() < pos or match[1].startswith(('code', 'void')): continue
|
||||
end = operand_end(text, match.end())
|
||||
if end == match.end(): continue
|
||||
inner = pointer_casts_rewrite(text[match.end():end])
|
||||
out.append(text[pos:match.start()] + f'GHIDRA_CAST<{match[1]}>({inner})'); pos = end
|
||||
return ''.join(out) + text[pos:]
|
||||
|
||||
|
||||
def virtuals_rewrite(text, varTypes): # (**(code **)(*(int *)X + 0x24))(args) -> X->Method(args), X's class from its declaration
|
||||
def slot_call(match):
|
||||
var, offset = match[1], int(match[2], 0)
|
||||
@@ -458,7 +474,7 @@ for cls in [c for c in done if not LIBRARY.match(c)]:
|
||||
varTypes = {m[2]: m[1] for m in re.finditer(r'^\s+((?:\w+::)*\w+)\s*\*\s*(\w+);', funcs[addr][6], re.M)}
|
||||
varTypes.update({p.rsplit('*', 1)[1].strip(): p.rsplit('*', 1)[0].strip() for p in params if '*' in p})
|
||||
varTypes['this'] = cls
|
||||
body = int_casts_rewrite(calls_rewrite(virtuals_rewrite(offsets_rewrite(fields_rewrite(funcs[addr][6])), varTypes), members))
|
||||
body = pointer_casts_rewrite(int_casts_rewrite(calls_rewrite(virtuals_rewrite(offsets_rewrite(fields_rewrite(funcs[addr][6])), varTypes), members)))
|
||||
text = f'\n// {addr:08x}\n{decl.replace(name + "(", cls + "::" + name + "(", 1)}\n{{\n{alias}{body}\n}}\n'
|
||||
spans[cls].append((line + 1, line + text.count('\n'), addr)); line += text.count('\n'); parts.append(text)
|
||||
if len(parts) > 1:
|
||||
@@ -483,7 +499,7 @@ if isCheck: # per method: does its body compile against classes.hpp?
|
||||
total = sum(len(spans[c]) for c in files)
|
||||
clean = sum(1 for c, bad in results for lo, hi, a in spans[c] if not bad.get(a))
|
||||
layout = subprocess.run(CLANG[:1] + ['--target=i686-linux-gnu'] + CLANG[2:] + ['-x', 'c++', f'{outDir}/classes.hpp'], capture_output=True, text=True)
|
||||
hazards = sum(len(re.findall(r'\((?:int|uint|char \*)\)\(?\w+\)? \+ (?:0x[0-9a-f]+|\d+)\)', open(f'{outDir}/{f}').read())) for f in files.values())
|
||||
hazards = sum(len(re.findall(r'GHIDRA_CAST<char \*>\([^;]*?\) \+ (?:0x[0-9a-f]+|\d+)\)', open(f'{outDir}/{f}').read())) for f in files.values())
|
||||
print(f'source_tree --check: {clean}/{total} method bodies compile for x86_64 ({100 * clean / max(total, 1):.1f}%),'
|
||||
f' {hazards} raw offset accesses, 32-bit layout asserts {"hold" if layout.returncode == 0 else "FAIL"}')
|
||||
for message, n in errors.most_common(15): print(f' {n:6d} {message}')
|
||||
Reference in new issue
Block a user