Repository navigation
Expand file tree
/
Copy pathmnem_tracker.lua
More file actions
85 lines (67 loc) · 2.46 KB
/
Copy pathmnem_tracker.lua
File metadata and controls
85 lines (67 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-- mnemonic tracker
-- tracks specific instruction mnemonics using explicit callback registration
local mnemonic_counts = {}
local matched_instructions = 0
local unique_sites = {}
local arch_targets = {}
local default_mnemonics = {
arm64 = {"B*", "BL*", "BR*", "BLR*", "RET*"},
x64 = {"CALL*", "JMP*", "RET*", "TAI*"},
x86 = {"CALL*", "JMP*", "RET*", "TAI*"}
}
local tracer = {}
local function parse_mnemonic(vm)
local disasm = w1.inst.disasm(vm) or ""
return disasm:match("^%s*(%S+)") or ""
end
local function register_mnemonic(pattern)
w1.on(w1.event.INSTRUCTION_PRE, function(vm, gpr, fpr)
local address = w1.reg.pc(gpr) or 0
local actual_mnemonic = parse_mnemonic(vm)
matched_instructions = matched_instructions + 1
if not unique_sites[address] then
unique_sites[address] = true
end
mnemonic_counts[pattern] = (mnemonic_counts[pattern] or 0) + 1
w1.log.debug(string.format("mnemonic matched: %s (address=0x%016x, actual=%s)",
pattern, address, actual_mnemonic))
return w1.enum.vm_action.CONTINUE
end, {
mnemonic = pattern
})
end
function tracer.init()
local arch = w1.util.architecture()
w1.log.info("mnemonic tracker initialized for " .. arch)
arch_targets = default_mnemonics[arch] or {}
for _, mnemonic in ipairs(arch_targets) do
mnemonic_counts[mnemonic] = 0
register_mnemonic(mnemonic)
end
w1.log.info("registered " .. #arch_targets .. " mnemonic callbacks")
end
function tracer.shutdown()
local unique_count = 0
for _ in pairs(unique_sites) do
unique_count = unique_count + 1
end
w1.log.info("mnemonic summary:")
w1.log.info(" matched instructions: " .. matched_instructions)
w1.log.info(" unique sites: " .. unique_count)
w1.log.info(" target patterns: " .. #arch_targets)
local sorted = {}
for mnemonic, count in pairs(mnemonic_counts) do
if count > 0 then
table.insert(sorted, {mnemonic = mnemonic, count = count})
end
end
table.sort(sorted, function(a, b) return a.count > b.count end)
if #sorted > 0 then
w1.log.info(" breakdown by pattern:")
for _, entry in ipairs(sorted) do
local pct = matched_instructions > 0 and (entry.count / matched_instructions * 100) or 0
w1.log.info(string.format(" %s: %d (%.1f%%)", entry.mnemonic, entry.count, pct))
end
end
end
return tracer