1 |
2 |
alfik |
/*
|
2 |
|
|
* Copyright (c) 2014, Aleksander Osman
|
3 |
|
|
* All rights reserved.
|
4 |
|
|
*
|
5 |
|
|
* Redistribution and use in source and binary forms, with or without
|
6 |
|
|
* modification, are permitted provided that the following conditions are met:
|
7 |
|
|
*
|
8 |
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
9 |
|
|
* list of conditions and the following disclaimer.
|
10 |
|
|
*
|
11 |
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
12 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
13 |
|
|
* and/or other materials provided with the distribution.
|
14 |
|
|
*
|
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18 |
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
21 |
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
22 |
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
23 |
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
24 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
package ao486.test.arithmetic_logic;
|
28 |
|
|
|
29 |
|
|
import ao486.test.TestUnit;
|
30 |
|
|
import static ao486.test.TestUnit.run_test;
|
31 |
|
|
import ao486.test.layers.FlagsLayer;
|
32 |
|
|
import ao486.test.layers.GeneralRegisterLayer;
|
33 |
|
|
import ao486.test.layers.HandleModeChangeLayer;
|
34 |
|
|
import ao486.test.layers.IOLayer;
|
35 |
|
|
import ao486.test.layers.InstructionLayer;
|
36 |
|
|
import ao486.test.layers.MemoryLayer;
|
37 |
|
|
import ao486.test.layers.OtherLayer;
|
38 |
|
|
import ao486.test.layers.Pair;
|
39 |
|
|
import ao486.test.layers.SegmentLayer;
|
40 |
|
|
import ao486.test.layers.StackLayer;
|
41 |
|
|
import java.io.Serializable;
|
42 |
|
|
import java.util.LinkedList;
|
43 |
|
|
import java.util.Random;
|
44 |
|
|
|
45 |
|
|
public class TestShiftDouble extends TestUnit implements Serializable {
|
46 |
|
|
public static void main(String args[]) throws Exception {
|
47 |
|
|
run_test(TestShiftDouble.class);
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
//--------------------------------------------------------------------------
|
51 |
|
|
@Override
|
52 |
|
|
public int get_test_count() throws Exception {
|
53 |
|
|
return 100;
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
@Override
|
57 |
|
|
public void init() throws Exception {
|
58 |
|
|
|
59 |
|
|
random = new Random(3+index);
|
60 |
|
|
|
61 |
|
|
String instruction;
|
62 |
|
|
while(true) {
|
63 |
|
|
layers.clear();
|
64 |
|
|
|
65 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
66 |
|
|
|
67 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list);
|
68 |
|
|
layers.add(instr);
|
69 |
|
|
layers.add(new StackLayer(random, prohibited_list));
|
70 |
|
|
layers.add(new OtherLayer(OtherLayer.Type.RANDOM, random));
|
71 |
|
|
layers.add(new FlagsLayer(FlagsLayer.Type.RANDOM, random));
|
72 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
73 |
|
|
layers.add(new SegmentLayer(random));
|
74 |
|
|
layers.add(new MemoryLayer(random));
|
75 |
|
|
layers.add(new IOLayer(random));
|
76 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
77 |
|
|
getInput("cr0_pe"),
|
78 |
|
|
getInput("vmflag"),
|
79 |
|
|
getInput("cs_rpl"),
|
80 |
|
|
getInput("cs_p"),
|
81 |
|
|
getInput("cs_s"),
|
82 |
|
|
getInput("cs_type")
|
83 |
|
|
));
|
84 |
|
|
|
85 |
|
|
// instruction size
|
86 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
87 |
|
|
|
88 |
|
|
boolean a32 = random.nextBoolean();
|
89 |
|
|
boolean o32 = random.nextBoolean();
|
90 |
|
|
|
91 |
|
|
// instruction
|
92 |
|
|
instruction = prepare_instr(cs_d_b, a32, o32, null);
|
93 |
|
|
|
94 |
|
|
instruction += "0F0F";
|
95 |
|
|
|
96 |
|
|
// add instruction
|
97 |
|
|
instr.add_instruction(instruction);
|
98 |
|
|
|
99 |
|
|
// end condition
|
100 |
|
|
break;
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
int imm_len(boolean o16, int opcode) {
|
107 |
|
|
if(opcode == 0xA4 || opcode == 0xAC) return 1;
|
108 |
|
|
|
109 |
|
|
return 0;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte modregrm_bytes[]) throws Exception {
|
113 |
|
|
|
114 |
|
|
int opcodes[] = {
|
115 |
|
|
0xA4,0xA5,0xAC,0xAD
|
116 |
|
|
};
|
117 |
|
|
|
118 |
|
|
String prefix = "";
|
119 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
120 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
121 |
|
|
|
122 |
|
|
int opcode = opcodes[random.nextInt(opcodes.length)];
|
123 |
|
|
boolean is_modregrm = true;
|
124 |
|
|
|
125 |
|
|
byte possible_modregrm = (byte)random.nextInt();
|
126 |
|
|
byte possible_sib = (byte)random.nextInt();
|
127 |
|
|
|
128 |
|
|
int len = (is_modregrm == false)? 1 : 1 + modregrm_len(!a32, unsigned(possible_modregrm), unsigned(possible_sib));
|
129 |
|
|
len += imm_len(!o32, opcode);
|
130 |
|
|
System.out.println("[len final: " + len + "]");
|
131 |
|
|
|
132 |
|
|
byte instr[] = new byte[len];
|
133 |
|
|
instr[0] = (byte)opcode;
|
134 |
|
|
for(int i=1; i<len; i++) {
|
135 |
|
|
if(i==1) instr[1] = possible_modregrm;
|
136 |
|
|
else if(i==2) instr[2] = possible_sib;
|
137 |
|
|
else instr[i] = (byte)random.nextInt();
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
return prefix + "0F" + bytesToHex(instr);
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
/*
|
145 |
|
|
public class TestShiftDouble {
|
146 |
|
|
public static void main(String args[]) throws Exception {
|
147 |
|
|
TestManager manager = new TestManager();
|
148 |
|
|
|
149 |
|
|
TestShiftDoubleSerializable test = new TestShiftDoubleSerializable();
|
150 |
|
|
|
151 |
|
|
if(false) {
|
152 |
|
|
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.obj"));
|
153 |
|
|
test = (TestShiftDoubleSerializable)ois.readObject();
|
154 |
|
|
ois.close();
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
for(; test.index<test.get_test_count(); test.index++) {
|
158 |
|
|
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Running test " + (test.index+1) + "/" + test.get_test_count());
|
159 |
|
|
|
160 |
|
|
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.obj"));
|
161 |
|
|
oos.writeObject(test);
|
162 |
|
|
oos.close();
|
163 |
|
|
|
164 |
|
|
boolean passed = manager.run_test_and_print_result(test);
|
165 |
|
|
if(passed == false) break;
|
166 |
|
|
}
|
167 |
|
|
}
|
168 |
|
|
}
|
169 |
|
|
class TestShiftDoubleSerializable extends TestBase implements Test, Serializable {
|
170 |
|
|
TestShiftDoubleSerializable() {
|
171 |
|
|
random = new Random(1);
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
Random random;
|
175 |
|
|
int index;
|
176 |
|
|
|
177 |
|
|
boolean d_b;
|
178 |
|
|
|
179 |
|
|
//--------------------------------------------------------------------------
|
180 |
|
|
@Override
|
181 |
|
|
public int get_test_count() throws Exception {
|
182 |
|
|
return 200;
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
@Override
|
186 |
|
|
public void init() throws Exception {
|
187 |
|
|
d_b = random.nextBoolean();
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
@Override
|
191 |
|
|
public boolean fini() throws Exception {
|
192 |
|
|
return index < get_test_count();
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
int imm_len(boolean o16, int opcode) {
|
196 |
|
|
if(opcode == 0xA4 || opcode == 0xAC) return 1;
|
197 |
|
|
|
198 |
|
|
return 0;
|
199 |
|
|
}
|
200 |
|
|
String prepare_instr() throws Exception {
|
201 |
|
|
int opcodes[] = {
|
202 |
|
|
0xA4,0xA5,0xAC,0xAD
|
203 |
|
|
};
|
204 |
|
|
|
205 |
|
|
boolean a16 = !d_b;
|
206 |
|
|
boolean o16 = !d_b;
|
207 |
|
|
String prefix = "";
|
208 |
|
|
if(random.nextBoolean()) { prefix = "66" + prefix; o16 = !o16; }
|
209 |
|
|
if(random.nextBoolean()) { prefix = "67" + prefix; a16 = !a16; }
|
210 |
|
|
|
211 |
|
|
int opcode = opcodes[random.nextInt(opcodes.length)];
|
212 |
|
|
boolean is_modregrm = true;
|
213 |
|
|
|
214 |
|
|
byte possible_modregrm = (byte)random.nextInt();
|
215 |
|
|
byte possible_sib = (byte)random.nextInt();
|
216 |
|
|
|
217 |
|
|
int len = (is_modregrm == false)? 1 : 1 + modregrm_len(a16, unsigned(possible_modregrm), unsigned(possible_sib));
|
218 |
|
|
System.out.printf("[len: %d, d_b: %b, modregrm: %02x, sib: %02x]\n", len, d_b, unsigned(possible_modregrm), unsigned(possible_sib));
|
219 |
|
|
len += imm_len(o16, opcode);
|
220 |
|
|
System.out.println("[len final: " + len + "]");
|
221 |
|
|
|
222 |
|
|
byte instr[] = new byte[len];
|
223 |
|
|
instr[0] = (byte)opcode;
|
224 |
|
|
for(int i=1; i<len; i++) {
|
225 |
|
|
if(i==1) instr[1] = possible_modregrm;
|
226 |
|
|
else if(i==2) instr[2] = possible_sib;
|
227 |
|
|
else instr[i] = (byte)random.nextInt();
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
return prefix + "0F" + bytesToHex(instr);
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
public String get_instructions() throws Exception {
|
234 |
|
|
String instr = "";
|
235 |
|
|
|
236 |
|
|
while(instr.length() < 2*15) {
|
237 |
|
|
instr += prepare_instr();
|
238 |
|
|
}
|
239 |
|
|
instr = instr.substring(0, 2*15);
|
240 |
|
|
|
241 |
|
|
System.out.println("[get_instructions: " + instr + "]");
|
242 |
|
|
return instr;
|
243 |
|
|
}
|
244 |
|
|
@Override
|
245 |
|
|
public byte get_memory(int address) throws Exception {
|
246 |
|
|
return (byte)random.nextInt();
|
247 |
|
|
}
|
248 |
|
|
@Override
|
249 |
|
|
public int eax() throws Exception {
|
250 |
|
|
return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
|
251 |
|
|
}
|
252 |
|
|
@Override
|
253 |
|
|
public int get_ebx() throws Exception {
|
254 |
|
|
return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
|
255 |
|
|
}
|
256 |
|
|
@Override
|
257 |
|
|
public int get_ecx() throws Exception {
|
258 |
|
|
return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
|
259 |
|
|
}
|
260 |
|
|
@Override
|
261 |
|
|
public int get_edx() throws Exception {
|
262 |
|
|
return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
|
263 |
|
|
}
|
264 |
|
|
@Override
|
265 |
|
|
public int get_esi() throws Exception {
|
266 |
|
|
return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
|
267 |
|
|
}
|
268 |
|
|
@Override
|
269 |
|
|
public int get_edi() throws Exception {
|
270 |
|
|
return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
|
271 |
|
|
}
|
272 |
|
|
@Override
|
273 |
|
|
public int get_ebp() throws Exception {
|
274 |
|
|
return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
|
275 |
|
|
}
|
276 |
|
|
@Override
|
277 |
|
|
public int get_esp() throws Exception {
|
278 |
|
|
return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
|
279 |
|
|
}
|
280 |
|
|
@Override
|
281 |
|
|
public boolean get_cf() throws Exception {
|
282 |
|
|
return random.nextBoolean();
|
283 |
|
|
}
|
284 |
|
|
@Override
|
285 |
|
|
public boolean get_pf() throws Exception {
|
286 |
|
|
return random.nextBoolean();
|
287 |
|
|
}
|
288 |
|
|
@Override
|
289 |
|
|
public boolean get_af() throws Exception {
|
290 |
|
|
return random.nextBoolean();
|
291 |
|
|
}
|
292 |
|
|
@Override
|
293 |
|
|
public boolean get_zf() throws Exception {
|
294 |
|
|
return random.nextBoolean();
|
295 |
|
|
}
|
296 |
|
|
@Override
|
297 |
|
|
public boolean get_sf() throws Exception {
|
298 |
|
|
return random.nextBoolean();
|
299 |
|
|
}
|
300 |
|
|
@Override
|
301 |
|
|
public boolean get_tf() throws Exception {
|
302 |
|
|
return random.nextBoolean();
|
303 |
|
|
}
|
304 |
|
|
@Override
|
305 |
|
|
public boolean get_if() throws Exception {
|
306 |
|
|
return random.nextBoolean();
|
307 |
|
|
}
|
308 |
|
|
@Override
|
309 |
|
|
public boolean get_df() throws Exception {
|
310 |
|
|
return random.nextBoolean();
|
311 |
|
|
}
|
312 |
|
|
@Override
|
313 |
|
|
public boolean get_of() throws Exception {
|
314 |
|
|
return random.nextBoolean();
|
315 |
|
|
}
|
316 |
|
|
@Override
|
317 |
|
|
public int get_iopl() throws Exception {
|
318 |
|
|
return random.nextInt(4);
|
319 |
|
|
}
|
320 |
|
|
@Override
|
321 |
|
|
public boolean get_nt() throws Exception {
|
322 |
|
|
return random.nextBoolean();
|
323 |
|
|
}
|
324 |
|
|
@Override
|
325 |
|
|
public boolean get_rf() throws Exception {
|
326 |
|
|
return random.nextBoolean();
|
327 |
|
|
}
|
328 |
|
|
@Override
|
329 |
|
|
public boolean get_vm() throws Exception {
|
330 |
|
|
return random.nextBoolean();
|
331 |
|
|
}
|
332 |
|
|
@Override
|
333 |
|
|
public boolean get_ac() throws Exception {
|
334 |
|
|
return random.nextBoolean();
|
335 |
|
|
}
|
336 |
|
|
@Override
|
337 |
|
|
public boolean get_id() throws Exception {
|
338 |
|
|
return random.nextBoolean();
|
339 |
|
|
}
|
340 |
|
|
@Override
|
341 |
|
|
public int get_cs_base() throws Exception {
|
342 |
|
|
return 0;
|
343 |
|
|
}
|
344 |
|
|
@Override
|
345 |
|
|
public int get_cs_limit() throws Exception {
|
346 |
|
|
return 0x000FFFFF;
|
347 |
|
|
}
|
348 |
|
|
@Override
|
349 |
|
|
public boolean get_cs_d_b() throws Exception {
|
350 |
|
|
return d_b;
|
351 |
|
|
}
|
352 |
|
|
@Override
|
353 |
|
|
public int get_ds_base() throws Exception {
|
354 |
|
|
return 0;
|
355 |
|
|
}
|
356 |
|
|
@Override
|
357 |
|
|
public int get_ds_limit() throws Exception {
|
358 |
|
|
return 0x000FFFFF;
|
359 |
|
|
}
|
360 |
|
|
@Override
|
361 |
|
|
public int get_es_base() throws Exception {
|
362 |
|
|
return 0;
|
363 |
|
|
}
|
364 |
|
|
@Override
|
365 |
|
|
public int get_es_limit() throws Exception {
|
366 |
|
|
return 0x000FFFFF;
|
367 |
|
|
}
|
368 |
|
|
@Override
|
369 |
|
|
public int get_fs_base() throws Exception {
|
370 |
|
|
return 0;
|
371 |
|
|
}
|
372 |
|
|
@Override
|
373 |
|
|
public int get_fs_limit() throws Exception {
|
374 |
|
|
return 0x000FFFFF;
|
375 |
|
|
}
|
376 |
|
|
@Override
|
377 |
|
|
public int get_gs_base() throws Exception {
|
378 |
|
|
return 0;
|
379 |
|
|
}
|
380 |
|
|
@Override
|
381 |
|
|
public int get_gs_limit() throws Exception {
|
382 |
|
|
return 0x000FFFFF;
|
383 |
|
|
}
|
384 |
|
|
@Override
|
385 |
|
|
public int get_ss_base() throws Exception {
|
386 |
|
|
return 0;
|
387 |
|
|
}
|
388 |
|
|
@Override
|
389 |
|
|
public int get_ss_limit() throws Exception {
|
390 |
|
|
return 0x000FFFFF;
|
391 |
|
|
}
|
392 |
|
|
}
|
393 |
|
|
*/
|