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.io;
|
28 |
|
|
|
29 |
|
|
import ao486.test.TestUnit;
|
30 |
|
|
import ao486.test.layers.FlagsLayer;
|
31 |
|
|
import ao486.test.layers.GeneralRegisterLayer;
|
32 |
|
|
import ao486.test.layers.HandleModeChangeLayer;
|
33 |
|
|
import ao486.test.layers.IOLayer;
|
34 |
|
|
import ao486.test.layers.InstructionLayer;
|
35 |
|
|
import ao486.test.layers.Layer;
|
36 |
|
|
import ao486.test.layers.MemoryLayer;
|
37 |
|
|
import ao486.test.layers.MemoryPatchLayer;
|
38 |
|
|
import ao486.test.layers.OtherLayer;
|
39 |
|
|
import ao486.test.layers.Pair;
|
40 |
|
|
import ao486.test.layers.SegmentLayer;
|
41 |
|
|
import ao486.test.layers.StackLayer;
|
42 |
|
|
import java.io.*;
|
43 |
|
|
import java.util.LinkedList;
|
44 |
|
|
import java.util.Random;
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
public class TestINS_real_rep_interrupt extends TestUnit implements Serializable {
|
48 |
|
|
public static void main(String args[]) throws Exception {
|
49 |
|
|
run_test(TestINS_real_rep_interrupt.class);
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
public TestINS_real_rep_interrupt() {
|
53 |
|
|
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
//--------------------------------------------------------------------------
|
57 |
|
|
@Override
|
58 |
|
|
public int get_test_count() throws Exception {
|
59 |
|
|
return 100;
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
@Override
|
63 |
|
|
public void init() throws Exception {
|
64 |
|
|
|
65 |
|
|
random = new Random(76 + index);
|
66 |
|
|
|
67 |
|
|
String instruction;
|
68 |
|
|
while(true) {
|
69 |
|
|
layers.clear();
|
70 |
|
|
|
71 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
72 |
|
|
|
73 |
|
|
// if false: v8086 mode
|
74 |
|
|
boolean is_real = true;
|
75 |
|
|
|
76 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list, true);
|
77 |
|
|
layers.add(instr);
|
78 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
79 |
|
|
layers.add(stack);
|
80 |
|
|
layers.add(new OtherLayer(is_real ? OtherLayer.Type.REAL : OtherLayer.Type.PROTECTED_OR_V8086, random));
|
81 |
|
|
layers.add(new FlagsLayer(is_real ? FlagsLayer.Type.RANDOM : FlagsLayer.Type.V8086, random));
|
82 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
83 |
|
|
layers.add(new SegmentLayer(random));
|
84 |
|
|
layers.add(new MemoryLayer(random));
|
85 |
|
|
layers.add(new IOLayer(random));
|
86 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
87 |
|
|
getInput("cr0_pe"),
|
88 |
|
|
getInput("vmflag"),
|
89 |
|
|
getInput("cs_rpl"),
|
90 |
|
|
getInput("cs_p"),
|
91 |
|
|
getInput("cs_s"),
|
92 |
|
|
getInput("cs_type")
|
93 |
|
|
));
|
94 |
|
|
|
95 |
|
|
// instruction size
|
96 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
97 |
|
|
|
98 |
|
|
boolean a32 = random.nextBoolean();
|
99 |
|
|
boolean o32 = random.nextBoolean();
|
100 |
|
|
|
101 |
|
|
long cs_limit = getInput("cs_limit");
|
102 |
|
|
|
103 |
|
|
//----------------
|
104 |
|
|
|
105 |
|
|
final Random final_random = random;
|
106 |
|
|
|
107 |
|
|
/* 0 - no rep; no interrupt
|
108 |
|
|
* 1 - no rep; interrupt on 0
|
109 |
|
|
* 2 - no rep; no interrupt; ecx = 0
|
110 |
|
|
* 3 - rep; no interrupt; ecx = 0
|
111 |
|
|
* 4 - rep; interrupt on 0
|
112 |
|
|
* 5 - rep; interrupt on 8
|
113 |
|
|
* 6 - rep; interrupt on 9
|
114 |
|
|
*/
|
115 |
|
|
int type = random.nextInt(6+1);
|
116 |
|
|
|
117 |
|
|
int interrupt_position = -1;
|
118 |
|
|
|
119 |
|
|
if(type == 0) {
|
120 |
|
|
interrupt_position = -1;
|
121 |
|
|
}
|
122 |
|
|
else if(type == 1) {
|
123 |
|
|
interrupt_position = 0;
|
124 |
|
|
}
|
125 |
|
|
else if(type == 2) {
|
126 |
|
|
interrupt_position = -1;
|
127 |
|
|
}
|
128 |
|
|
else if(type == 3) {
|
129 |
|
|
interrupt_position = -1;
|
130 |
|
|
}
|
131 |
|
|
else if(type == 4) {
|
132 |
|
|
interrupt_position = 0;
|
133 |
|
|
}
|
134 |
|
|
else if(type == 5) {
|
135 |
|
|
interrupt_position = 8;
|
136 |
|
|
}
|
137 |
|
|
else if(type == 6) {
|
138 |
|
|
interrupt_position = 9;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
final int interrupt_position_final = interrupt_position;
|
142 |
|
|
|
143 |
|
|
Layer layer = new Layer() {
|
144 |
|
|
long ecx() { return final_random.nextInt(5); }
|
145 |
|
|
|
146 |
|
|
long ds_limit() { return 0xFFFF; }
|
147 |
|
|
long es_limit() { return 0xFFFF; }
|
148 |
|
|
|
149 |
|
|
long esi() {
|
150 |
|
|
return final_random.nextInt(0x1000);
|
151 |
|
|
/*
|
152 |
|
|
int val = final_random.nextInt();
|
153 |
|
|
return ((val % 18) == 0)? 0 :
|
154 |
|
|
((val % 18) == 1)? 1 :
|
155 |
|
|
((val % 18) == 2)? 2 :
|
156 |
|
|
((val % 18) == 3)? 3 :
|
157 |
|
|
((val % 18) == 4)? 4 :
|
158 |
|
|
((val % 18) == 5)? 0xFFFFFFFF :
|
159 |
|
|
((val % 18) == 6)? 0x0000FFFF :
|
160 |
|
|
final_random.nextInt() & ((final_random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
|
161 |
|
|
*/
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
long check_interrupt(long counter) { return counter == interrupt_position_final? 0xAB : 0x100; }
|
165 |
|
|
|
166 |
|
|
long edi() {
|
167 |
|
|
return final_random.nextInt(0x1000);
|
168 |
|
|
/*
|
169 |
|
|
int val = final_random.nextInt();
|
170 |
|
|
return ((val % 18) == 0)? 0 :
|
171 |
|
|
((val % 18) == 1)? 1 :
|
172 |
|
|
((val % 18) == 2)? 2 :
|
173 |
|
|
((val % 18) == 3)? 3 :
|
174 |
|
|
((val % 18) == 4)? 4 :
|
175 |
|
|
((val % 18) == 5)? 0xFFFFFFFF :
|
176 |
|
|
((val % 18) == 6)? 0x0000FFFF :
|
177 |
|
|
final_random.nextInt() & ((final_random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
|
178 |
|
|
*/
|
179 |
|
|
}
|
180 |
|
|
};
|
181 |
|
|
layers.addFirst(layer);
|
182 |
|
|
|
183 |
|
|
//-----------------
|
184 |
|
|
|
185 |
|
|
|
186 |
|
|
final int test_type = (type == 0)? 3 : (type == 1 || type == 2 || type == 3)? 4 : 12;
|
187 |
|
|
final int ecx = (type == 2 || type == 3)? 0 : 10;
|
188 |
|
|
Layer esp_layer = new Layer() {
|
189 |
|
|
long esp() { return 0xF0; }
|
190 |
|
|
long ss_base() { return 0; }
|
191 |
|
|
long iflag() { return 1; }
|
192 |
|
|
long tflag() { return 0; }
|
193 |
|
|
|
194 |
|
|
long cs_d_b() { return 0; }
|
195 |
|
|
|
196 |
|
|
long ecx() { return ecx; }
|
197 |
|
|
|
198 |
|
|
long get_test_type() { return test_type; }
|
199 |
|
|
};
|
200 |
|
|
layers.addFirst(esp_layer);
|
201 |
|
|
|
202 |
|
|
long handler = cs_limit;
|
203 |
|
|
|
204 |
|
|
//0xAB -- interrupt vector = 0xAB*4 = 0x2AC linear
|
205 |
|
|
MemoryPatchLayer int_patch = new MemoryPatchLayer(random, prohibited_list, (int)(0xAB*4), (byte)(handler&0xFF),(byte)((handler>>8)&0xFF), 0x00,0x00);
|
206 |
|
|
layers.addFirst(int_patch);
|
207 |
|
|
|
208 |
|
|
//interrupt handler: simply IRET
|
209 |
|
|
MemoryPatchLayer handler_patch = new MemoryPatchLayer(random, prohibited_list, (int)handler, 0xCF);
|
210 |
|
|
layers.addFirst(handler_patch);
|
211 |
|
|
|
212 |
|
|
// byte or word/double word
|
213 |
|
|
String instr_string = prepare_instr(cs_d_b, a32, o32);
|
214 |
|
|
String rep_string = (type == 0 || type == 1 || type == 2)? "" : random.nextBoolean()? "F2" : "F3";
|
215 |
|
|
|
216 |
|
|
// add instruction
|
217 |
|
|
instruction = rep_string + instr_string + "90909090909090909090909090909090";
|
218 |
|
|
|
219 |
|
|
instr.add_instruction(instruction);
|
220 |
|
|
String total = instruction;
|
221 |
|
|
//-----------------
|
222 |
|
|
|
223 |
|
|
String instruction_string = prepare_instr(cs_d_b, a32, o32);
|
224 |
|
|
|
225 |
|
|
// add instruction
|
226 |
|
|
instruction = instruction_string + instruction_string + "9090900F0F";
|
227 |
|
|
|
228 |
|
|
instr.add_instruction(instruction);
|
229 |
|
|
total += instruction;
|
230 |
|
|
|
231 |
|
|
System.out.println("Instruction: [" + total + "]");
|
232 |
|
|
|
233 |
|
|
// end condition
|
234 |
|
|
break;
|
235 |
|
|
}
|
236 |
|
|
}
|
237 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32) throws Exception {
|
238 |
|
|
int opcodes[] = {
|
239 |
|
|
0x6C,0x6D
|
240 |
|
|
};
|
241 |
|
|
|
242 |
|
|
String prefix = "";
|
243 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
244 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
245 |
|
|
|
246 |
|
|
int opcode = opcodes[random.nextInt(opcodes.length)];
|
247 |
|
|
|
248 |
|
|
int len = 1;
|
249 |
|
|
|
250 |
|
|
byte instr[] = new byte[len];
|
251 |
|
|
instr[0] = (byte)opcode;
|
252 |
|
|
if(len >= 2) instr[1] = (byte)random.nextInt();
|
253 |
|
|
|
254 |
|
|
return prefix + bytesToHex(instr);
|
255 |
|
|
}
|
256 |
|
|
}
|