From 9d5a600d6b698f3e96c46701cb2457656eefc566 Mon Sep 17 00:00:00 2001 From: Patryk Gensch <43010113+patryk025@users.noreply.github.com> Date: Sat, 24 Oct 2026 20:30:36 +0200 Subject: [PATCH] feat(extractor): properties per script type along the constructor chain type_fields walks each type's factory constructor through its base constructors, base first, so unnamed classes get their properties (VECTOR VALUE/SIZE, FILTER ACTION, PATH NODES, MOVIE FILENAME/POSITION) and INTEGER/STRING inherit CMC_Variable's TOINI/NETNOTIFY. Property names come only from pushed or register-loaded literals: a vftable store whose bytes look printable produced BlooMoo's X5/Gs field names. Schema 7. Co-Authored-By: Claude Opus 5.5 --- ghidra_scripts/extract_engine_surface.py | 124 ++++++++++++++++++----- 1 file changed, 96 insertions(+), 28 deletions(-) diff --git a/ghidra_scripts/extract_engine_surface.py b/ghidra_scripts/extract_engine_surface.py index 7e6df5f..5466d7b 100644 --- a/ghidra_scripts/extract_engine_surface.py +++ b/ghidra_scripts/extract_engine_surface.py @@ -839,15 +839,48 @@ def _prop_type(getter): return "string" # plain getPropertyValue -def extract_script_fields(program): - """The script-visible named fields each type exposes (FILENAME, FPS, PRELOAD, VISIBLE, ...). +def _property_getter(program, addr): + """The CMElement::getPropertyValue called at `addr` (any folded alias), or None.""" + for _, name in _labels(program, addr): + if name.startswith("getProperty"): + return name + return None - The CMC_* constructor reads them from the element: it builds the property-name literal and - calls CMElement::getPropertyValue(...). We anchor on that getter call, take the preceding - string literal as the field name, and derive the field type from the getter. Returns a flat - list deduped per (owner, name).""" + +def _string_operand(program, instr): + """A name literal an instruction pushes or loads into a register. A store into memory is + never one: installing a vftable writes an address whose bytes may happen to be printable + (BlooMoo read "X5"/"vN" instead of VALUE that way).""" + if instr.getMnemonicString() == "MOV" and "[" in instr.getDefaultOperandRepresentation(0): + return None + s = _imm_string(program, instr) + return s if s is not None and _is_method_name(s) else None + + +def _fields_read(program, func): + """[(property, value type)] a constructor body reads, in order: the literal pushed or + loaded right before each CMElement::getPropertyValue call names the property.""" + out = [] + last = None + ins = program.getListing().getInstructions(resolve_thunk(func).getBody(), True) + while ins.hasNext(): + instr = ins.next() + s = _string_operand(program, instr) + if s is not None: + last = s + if instr.getMnemonicString() == "CALL" and len(instr.getFlows()) == 1 and last is not None: + getter = _property_getter(program, instr.getFlows()[0]) + if getter is not None: + out.append((last, _prop_type(getter))) + last = None + return out + + +def extract_script_fields(program): + """The script-visible named fields each named CMC_* constructor reads (FILENAME, FPS, + PRELOAD, VISIBLE, ...), typed by the CMElement getter. Flat list deduped per (owner, name); + `type_fields` covers unnamed classes and inheritance per script type.""" fm = program.getFunctionManager() - listing = program.getListing() by_key = {} # (owner, name) -> record next_order = {} # owner -> next order index it = fm.getFunctions(True) @@ -856,27 +889,60 @@ def extract_script_fields(program): if not _is_cmc_ctor(f): continue owner = f.getParentNamespace().getName() - last_string = None - ins = listing.getInstructions(resolve_thunk(f).getBody(), True) - while ins.hasNext(): - instr = ins.next() - s = _imm_string(program, instr) - if s is not None and _is_method_name(s): - last_string = s - if instr.getMnemonicString() == "CALL": - cf = call_target(program, instr) - cname = cf.getName() if cf is not None else None - if cname is not None and cname.startswith("getProperty") and last_string is not None: - key = (owner, last_string) - if key not in by_key: - o = next_order.get(owner, 0) - by_key[key] = {"owner": owner, "name": last_string, - "type": _prop_type(cname), "order": o} - next_order[owner] = o + 1 - last_string = None + for name, ty in _fields_read(program, f): + key = (owner, name) + if key not in by_key: + o = next_order.get(owner, 0) + by_key[key] = {"owner": owner, "name": name, "type": ty, "order": o} + next_order[owner] = o + 1 return sorted(by_key.values(), key=lambda r: (r["owner"], r["order"])) +def _constructor_chain(program, ctor, limit=6): + """`ctor` and the base constructors it delegates to, most-derived first.""" + chain = [] + func = ctor + while func is not None and len(chain) < limit: + chain.append(func) + nxt = None + it = program.getListing().getInstructions(resolve_thunk(func).getBody(), True) + while it.hasNext(): + instr = it.next() + flows = instr.getFlows() if instr.getMnemonicString() == "CALL" else [] + if len(flows) == 1 and _is_base_ctor_call(program, flows[0]): + nxt = program.getFunctionManager().getFunctionAt(flows[0]) + break + func = nxt + return chain + + +def extract_type_fields(program, types): + """Per script type, the properties its constructor chain reads, base first: reaches + unnamed classes (VECTOR's VALUE/SIZE, FILTER's ACTION, PATH's NODES) and inherited ones + (INTEGER gets CMC_Variable's TOINI/NETNOTIFY). A property read through several getters + lists every value type.""" + out = [] + seen = set() + for t in types: + ctor = _function_at(program, t.get("ctor_addr")) + if ctor is None or t["script_name"] in seen: + continue + seen.add(t["script_name"]) + records = {} + for func in reversed(_constructor_chain(program, ctor)): + classes = sorted(ns for ns, name in _labels(program, func.getEntryPoint()) if ns == name) + for name, ty in _fields_read(program, func): + rec = records.get(name) + if rec is None: + rec = records[name] = {"type": t["script_name"], "name": name, "value_types": [], + "order": len(records), + "declared_in": classes[0] if classes else None} + if ty not in rec["value_types"]: + rec["value_types"].append(ty) + out.extend(sorted(records.values(), key=lambda r: r["order"])) + return out + + def extract_struct_layout(program): """Bonus/fuzzy axis: per CMC_* constructor, recover the this-relative C++ field stores (offset, size, is_vtable) and the base-class link. Offsets come from decompiler P-code, so @@ -1896,9 +1962,10 @@ def run(): annotate_method_facts(program, method_dispatch, run_addrs, runner_map, types) annotate_type_vftables(program, types, method_dispatch) type_events = extract_type_events(program, types) + type_fields = extract_type_fields(program, types) snapshot = { - "schema_version": 6, + "schema_version": 7, "binary": { "name": program.getName(), "sha256": sha256_of(program), @@ -1916,6 +1983,7 @@ def run(): "method_dispatch": method_dispatch, "runner_map": runner_map, "type_events": type_events, + "type_fields": type_fields, } args = getScriptArgs() @@ -1926,9 +1994,9 @@ def run(): finally: fh.close() - print("[+] %s [%s/%s]: %d types, %d methods, %d events (%d per type), %d fields (%d layout, %d dispatch, %d runners) -> %s" % ( + print("[+] %s [%s/%s]: %d types, %d methods, %d events (%d per type), %d fields (%d per type, %d layout, %d dispatch, %d runners) -> %s" % ( program.getName(), engine, compiler, len(types), len(methods), - len(events), len(type_events), len(fields), len(struct_layout), len(method_dispatch), + len(events), len(type_events), len(fields), len(type_fields), len(struct_layout), len(method_dispatch), len((runner_map or {}).get("runners", [])), out_path))