-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecompile.py
129 lines (110 loc) · 4.9 KB
/
decompile.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
"""A decompiler for programs targetting a Vector Mapping Machine.
This file is part of a hack distributed under the Hacking License (see HACK.txt)
Copyright (C) 2021 Giacomo Tesio
"""
__author__ = "Giacomo Tesio"
__contact__ = "giacomo@tesio.it"
__copyright__ = "Copyright 2021, Giacomo Tesio"
__date__ = "2021/09/01"
__deprecated__ = False
__email__ = "giacomo@tesio.it"
__license__ = "Hacking License"
__maintainer__ = "Giacomo Tesio"
__status__ = "Proof of Concept"
__version__ = "1.0.0"
from vmm import *
from ef import *
from sources import *
import sys
import bz2
import pickle
from csv import writer
def help() -> None:
hs = """
decompile.py program.bin decompiled.sources.csv
Decompile program.bin into decompiled.sources.csv
"""
print(hs)
sys.exit()
def decompile(binary:VectorMappingMachineExecutable, writer) -> List[Sample]:
for sampleIndex in reversed(range(len(binary.logs))):
log = binary.logs[sampleIndex]
computedOutputs = log.outputs
computedError = log.errors
# easy part: reconstruct sample outputs
sampleOutputs = [computedOutputs[i] + computedError[i] for i in range(len(computedOutputs))]
# now, going backward for each filter
errors = computedError
outputs = computedOutputs
for filterIndex in reversed(range(len(binary.filters))):
inputs = []
currentFilter = binary.filters[filterIndex]
# compute inputs given
# - the weight variations of the first node,
# - errors,
# - output derivative and
# - samples' weight ("learning rate" in "AI/ML" parlance)
firstNodeWeights = currentFilter.reducers[0]
firstNodeOutput = outputs[0]
scaledError = VectorReducer.scaleError(firstNodeOutput, errors[0])
weightVariations = log.sampleNodes[filterIndex]
for weightIndex in range(len(weightVariations)-1): # skip bias
deltaW:float = weightVariations[weightIndex]
if deltaW == 0:
inputs.append(0)
else:
d = scaledError*binary.samplesWeight
inputs.append(deltaW/d)
firstNodeWeights.weights[weightIndex] -= deltaW
firstNodeWeights.weights[weightIndex+1] -= weightVariations[weightIndex+1] # update "bias" weight
# given computed inputs and all outputs
# -> compute weight variations for all other reducers of the filter
# -> apply weight variations to all reducers of the filter
for nodeIndex in range(1, len(currentFilter.reducers)):
currentNode = currentFilter.reducers[nodeIndex]
nodeOutput = outputs[nodeIndex]
scaledError = VectorReducer.scaleError(nodeOutput, errors[nodeIndex])
for weightIndex in range(len(currentNode.weights)):
if weightIndex != len(currentNode.weights) - 1:
deltaW = scaledError*inputs[weightIndex]*binary.samplesWeight
else: # "bias" in AI/ML parlance
deltaW = scaledError*binary.samplesWeight
currentNode.weights[weightIndex] -= deltaW
# so we have the filter in the state it was on backprop of errors
# -> compute the errors for the previous filter
previousFilterErrors = [0] * len(inputs)
for inputIndex in range(len(inputs)):
for nodeIndex in range(len(currentFilter.reducers)):
weights = currentFilter.reducers[nodeIndex].weights
nodeOutput = outputs[nodeIndex]
scaledError = VectorReducer.scaleError(nodeOutput, errors[nodeIndex])
previousFilterErrors[inputIndex] += weights[inputIndex] * scaledError
# the inputs of this filter are the outputs of the previous filter
outputs = inputs
errors = previousFilterErrors
csvRow = rescale(binary, Sample(inputs, sampleOutputs))
writer.writerow(csvRow)
def rescale(binary:VectorMappingMachineExecutable, sample:Sample):
row = []
for i in range(len(sample.inputs)):
stat = binary.encodings.inputs[i]
if stat.minimum == stat.maximum:
row.append(round(stat.minimum))
else:
rescaled = stat.minimum + sample.inputs[i] * (stat.maximum - stat.minimum)
if rescaled < 0:
rescaled *= -1
row.append(round(rescaled))
row.append(binary.encodings.outputs[sample.outputs.index(max(sample.outputs))])
return row
def main(argv:list):
if len(argv) != 3:
help()
with bz2.BZ2File(argv[1], "rb") as f:
program = pickle.load(f)
with open(argv[2], "w", newline='') as f:
csv = writer(f)
decompile(program, csv)
if __name__ == '__main__':
main(sys.argv)