forked from Pusty/Obfuscat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVMRefImpl.java
276 lines (253 loc) · 7.67 KB
/
VMRefImpl.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package re.bytecode.obfuscat.pass.vm;
import static re.bytecode.obfuscat.pass.vm.VMConst.*;
import java.util.HashMap;
import java.util.Random;
import re.bytecode.obfuscat.dsl.api.ExcludeField;
import re.bytecode.obfuscat.dsl.api.ExcludeMethod;
/**
* This is a reference implementation for the VMConst Virtual Machine specs
*/
public class VMRefImpl {
@ExcludeField
private static Random addressRandomiser;
@ExcludeField
private static HashMap<Integer, Object> dataMap;
@ExcludeField
private static HashMap<Object, Integer> reverseMap;
@ExcludeMethod
private static void init() {
if(addressRandomiser != null) return;
addressRandomiser = new Random();
dataMap = new HashMap<Integer, Object>();
reverseMap = new HashMap<Object, Integer>();
}
@ExcludeMethod
private static Object native_int2obj(int a) {
init();
if(dataMap.containsKey(a)) return dataMap.get(a);
return a;
//throw new RuntimeException("Can't convert integer "+a+" to object");
}
@ExcludeMethod
private static int native_obj2int(Object o) {
init();
if(o instanceof Integer) return ((Integer)o).intValue();
if(o instanceof Byte) return ((Byte)o).intValue();
if(o instanceof Short) return ((Short)o).intValue();
if(o instanceof Character) return (int)((Character)o).charValue();
if(o instanceof Boolean) return ((Boolean)o).booleanValue()?1:0;
if(reverseMap.containsKey(o)) {
return reverseMap.get(o);
}else {
int addr = addressRandomiser.nextInt()&0x7FFFFFF;
dataMap.put(addr, o);
reverseMap.put(o, addr);
return addr;
}
}
// memory + 0x100 = mem
public static int process(byte[] program, Object[] appendedData, Object[] pars) {
int[] memory = new int[0x100+0x100];
int pc = 0;
while(true) {
int opcode = program[pc]&0xFF;
int data;
int memslot;
int op1;
int op2;
int op3;
int op4;
short jumpPosition;
int stackslot;
data = (program[pc+1]&0xFF)|((program[pc+2]&0xFF)<<8)|((program[pc+3]&0xFF)<<16)|(program[pc+5]<<24);
op1 = program[pc+1]&0xFF;
op2 = program[pc+2]&0xFF;
op3 = program[pc+3]&0xFF;
op4 = program[pc+5]&0xFF;
jumpPosition = (short) ((program[pc+3]&0xFF) | (program[pc+4]<<8));
memslot = (program[pc+1]&0xFF)|((program[pc+2]&0xFF)<<8);
stackslot = (program[pc+4]&0xFF);
//System.out.println("Opcode: "+Integer.toHexString(opcode)+" @ "+pc);
//System.out.println(Arrays.toString(memory));
switch(opcode) {
case OP_CONST:
memory[stackslot] = data;
break;
case OP_LOAD8:
memory[stackslot] = (byte)(memory[memslot+0x100]);
break;
case OP_LOAD16:
memory[stackslot] = (short)(memory[memslot+0x100]);
break;
case OP_LOAD32:
memory[stackslot] = memory[memslot+0x100];
break;
case OP_LOADP:
memory[stackslot] = memory[memslot+0x100];
break;
case OP_PLOAD8:
memory[stackslot] = (byte)native_obj2int(pars[memslot]);
break;
case OP_PLOAD16:
memory[stackslot] = (short)native_obj2int(pars[memslot]);
break;
case OP_PLOAD32:
memory[stackslot] = native_obj2int(pars[memslot]);
break;
case OP_PLOADP:
memory[stackslot] = native_obj2int(pars[memslot]);
break;
case OP_STORE8:
memory[memslot+0x100] = (memory[stackslot])&0xFF;
break;
case OP_STORE16:
memory[memslot+0x100] = (memory[stackslot])&0xFFFF;
break;
case OP_STORE32:
memory[memslot+0x100] = memory[stackslot];
break;
case OP_STOREP:
memory[memslot+0x100] = memory[stackslot];
break;
case OP_PSTORE8:
pars[memslot] = native_int2obj(memory[stackslot]&0xFF);
break;
case OP_PSTORE16:
pars[memslot] = native_int2obj(memory[stackslot]&0xFFFF);
break;
case OP_PSTORE32:
pars[memslot] = native_int2obj(memory[stackslot]);
break;
case OP_PSTOREP:
pars[memslot] = native_int2obj(memory[stackslot]);
break;
case OP_ALOAD8:
memory[stackslot] = (int)((byte[])native_int2obj(memory[op1]))[(memory[op2])];
break;
case OP_ALOAD16:
memory[stackslot] = (int)((short[])native_int2obj(memory[op1]))[(memory[op2])];
break;
case OP_ALOAD32:
memory[stackslot] = (int)((int[])native_int2obj(memory[op1]))[(memory[op2])];
break;
case OP_ALOADP:
memory[stackslot] = native_obj2int(((Object[])native_int2obj(memory[op1]))[(memory[op2])]);
break;
case OP_ASTORE8:
((byte[])native_int2obj(memory[op1]))[(memory[op2])] = (byte) (memory[stackslot]);
break;
case OP_ASTORE16:
((short[])native_int2obj(memory[op1]))[(memory[op2])] = (short) (memory[stackslot]);
break;
case OP_ASTORE32:
((int[])native_int2obj(memory[op1]))[(memory[op2])] = (int) (memory[stackslot]);
break;
case OP_ASTOREP:
((Object[])native_int2obj(memory[op1]))[(memory[op2])] = native_int2obj(memory[stackslot]);
break;
case OP_NOT:
memory[stackslot] = ~(memory[op1]);
break;
case OP_NEG:
memory[stackslot] = -(memory[op1]);
break;
case OP_NOP:
memory[stackslot] = (memory[op1]);
break;
case OP_ADD:
memory[stackslot] = (memory[op1]) + (memory[op2]);
break;
case OP_SUB:
memory[stackslot] = (memory[op1]) - (memory[op2]);
break;
case OP_MUL:
memory[stackslot] = (memory[op1]) * (memory[op2]);
break;
case OP_DIV:
memory[stackslot] = (memory[op1]) / (memory[op2]);
break;
case OP_MOD:
memory[stackslot] = (memory[op1]) % (memory[op2]);
break;
case OP_AND:
memory[stackslot] = (memory[op1]) & (memory[op2]);
break;
case OP_OR:
memory[stackslot] = (memory[op1]) | (memory[op2]);
break;
case OP_XOR:
memory[stackslot] = (memory[op1]) ^ (memory[op2]);
break;
case OP_SHR:
memory[stackslot] = (memory[op1]) >> (memory[op2]);
break;
case OP_USHR:
memory[stackslot] = (memory[op1]) >>> (memory[op2]);
break;
case OP_SHL:
memory[stackslot] = (memory[op1]) << (memory[op2]);
break;
case OP_COMPARE_EQUAL:
if(memory[op1] == memory[op2])
pc += ((int)jumpPosition-6);
break;
case OP_COMPARE_NOTEQUAL:
if(memory[op1] != memory[op2])
pc += ((int)jumpPosition-6);
break;
case OP_COMPARE_LESSTHAN:
if((memory[op1]) < (memory[op2]))
pc += ((int)jumpPosition-6);
break;
case OP_COMPARE_LESSEQUAL:
if((memory[op1]) <= (memory[op2]))
pc += ((int)jumpPosition-6);
break;
case OP_COMPARE_GREATERTHAN:
if((memory[op1]) > (memory[op2]))
pc += ((int)jumpPosition-6);
break;
case OP_COMPARE_GREATEREQUAL:
if((memory[op1]) >= (memory[op2]))
pc += ((int)jumpPosition-6);
break;
case OP_SWITCH:
int index = pc+6+((memory[op1])*2);
pc += ((short)((program[index]&0xFF) + (program[index+1]<<8)));
break;
case OP_JUMP:
pc += ((int)jumpPosition-6);
break;
case OP_RETURN:
return 0;
case OP_RETURNV:
return memory[op1];
case OP_ALLOC8:
memory[stackslot] = native_obj2int(new byte[memory[op1]]);
break;
case OP_ALLOC16:
memory[stackslot] = native_obj2int(new short[memory[op1]]);
break;
case OP_ALLOC32:
memory[stackslot] = native_obj2int(new int[memory[op1]]);
break;
case OP_ALLOCP:
memory[stackslot] = native_obj2int(new Object[memory[op1]]);
break;
case OP_OCONST:
memory[stackslot] = native_obj2int(appendedData[memory[op1]]);
break;
case OP_CUSTOM_PREPCALL:
memory[stackslot] = native_obj2int(new Object[] {native_int2obj(memory[op1]), native_int2obj(memory[op2]), native_int2obj(memory[op3]), native_int2obj(memory[op4])});
break;
case OP_CUSTOM_CALL:
memory[stackslot] = process(program, appendedData, (Object[])native_int2obj(memory[op1]));
break;
default:
return 0;
// throw new RuntimeException("Illegal Opcode "+opcode+" @ "+pc);
}
pc += 6;
}
}
}