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.interrupt;
|
28 |
|
|
|
29 |
|
|
import ao486.test.TestUnit;
|
30 |
|
|
import ao486.test.layers.DescriptorTableLayer;
|
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.Layer;
|
37 |
|
|
import ao486.test.layers.MemoryLayer;
|
38 |
|
|
import ao486.test.layers.MemoryPatchLayer;
|
39 |
|
|
import ao486.test.layers.OtherLayer;
|
40 |
|
|
import ao486.test.layers.Pair;
|
41 |
|
|
import ao486.test.layers.SegmentLayer;
|
42 |
|
|
import ao486.test.layers.StackLayer;
|
43 |
|
|
import java.io.*;
|
44 |
|
|
import java.util.LinkedList;
|
45 |
|
|
import java.util.Random;
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
public class TestINT_INT3_INTO_INT1_int_trap_gate_same extends TestUnit implements Serializable {
|
49 |
|
|
public static void main(String args[]) throws Exception {
|
50 |
|
|
run_test(TestINT_INT3_INTO_INT1_int_trap_gate_same.class);
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
//--------------------------------------------------------------------------
|
54 |
|
|
@Override
|
55 |
|
|
public int get_test_count() throws Exception {
|
56 |
|
|
return 100;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
@Override
|
60 |
|
|
public void init() throws Exception {
|
61 |
|
|
|
62 |
|
|
random = new Random(21 + index);
|
63 |
|
|
|
64 |
|
|
String instruction;
|
65 |
|
|
while(true) {
|
66 |
|
|
layers.clear();
|
67 |
|
|
|
68 |
|
|
/*
|
69 |
|
|
* 0 - pre-(interrupt, trap gate) valid check
|
70 |
|
|
* 1 - cs_selector NULL
|
71 |
|
|
* 2 - cs_selector out of bounds
|
72 |
|
|
* 3 - cs_descriptor valid check
|
73 |
|
|
* 4 - cs_descriptor -- v8086 mode active
|
74 |
|
|
* 5 - eip out of bounds
|
75 |
|
|
*
|
76 |
|
|
* 6 - all ok
|
77 |
|
|
*
|
78 |
|
|
* TODO error code push test
|
79 |
|
|
*/
|
80 |
|
|
|
81 |
|
|
int type = random.nextInt(7);
|
82 |
|
|
|
83 |
|
|
boolean is_v8086 = (type == 4)? true : random.nextBoolean();
|
84 |
|
|
boolean is_into = random.nextInt(3) == 0;
|
85 |
|
|
boolean is_ib = random.nextInt(3) == 0;
|
86 |
|
|
|
87 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
88 |
|
|
|
89 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list);
|
90 |
|
|
layers.add(instr);
|
91 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
92 |
|
|
layers.add(stack);
|
93 |
|
|
layers.add(new OtherLayer(OtherLayer.Type.PROTECTED_OR_V8086, random));
|
94 |
|
|
layers.add(new FlagsLayer((is_v8086)? FlagsLayer.Type.V8086 : FlagsLayer.Type.NOT_V8086, random));
|
95 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
96 |
|
|
layers.add(new SegmentLayer(random));
|
97 |
|
|
layers.add(new MemoryLayer(random));
|
98 |
|
|
layers.add(new IOLayer(random));
|
99 |
|
|
|
100 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
101 |
|
|
getInput("cr0_pe"),
|
102 |
|
|
getInput("vmflag"),
|
103 |
|
|
getInput("cs_rpl"),
|
104 |
|
|
getInput("cs_p"),
|
105 |
|
|
getInput("cs_s"),
|
106 |
|
|
getInput("cs_type")
|
107 |
|
|
));
|
108 |
|
|
|
109 |
|
|
// instruction size
|
110 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
111 |
|
|
|
112 |
|
|
boolean a32 = random.nextBoolean();
|
113 |
|
|
boolean o32 = random.nextBoolean();
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
instruction = prepare_instr(cs_d_b, a32, o32, is_into, is_ib);
|
117 |
|
|
instr.add_instruction(instruction);
|
118 |
|
|
|
119 |
|
|
DescriptorTableLayer tables = null;
|
120 |
|
|
|
121 |
|
|
//------------------------------------------------------------------
|
122 |
|
|
//------------------------------------------------------------------
|
123 |
|
|
|
124 |
|
|
// prepare cs descriptor
|
125 |
|
|
boolean is_cs_ldt = (type == 1)? false : random.nextBoolean();
|
126 |
|
|
|
127 |
|
|
boolean conds[] = new boolean[5];
|
128 |
|
|
int cond = 1 << random.nextInt(conds.length);
|
129 |
|
|
if(type >= 5) cond = 0;
|
130 |
|
|
|
131 |
|
|
int new_cs_rpl = 0;
|
132 |
|
|
boolean new_cs_seg = false;
|
133 |
|
|
int new_cs_type = 0;
|
134 |
|
|
int new_cs_dpl = 0;
|
135 |
|
|
boolean new_cs_p = false;
|
136 |
|
|
int old_cs_rpl = 0;
|
137 |
|
|
|
138 |
|
|
if( ((cond >> 4) & 1) == 1 && is_v8086 == false ) continue;
|
139 |
|
|
if( ((cond >> 4) & 1) == 0 && is_v8086 == true ) continue;
|
140 |
|
|
|
141 |
|
|
do {
|
142 |
|
|
do {
|
143 |
|
|
new_cs_seg = random.nextBoolean();
|
144 |
|
|
new_cs_type = random.nextInt(16);
|
145 |
|
|
new_cs_p = random.nextBoolean();
|
146 |
|
|
|
147 |
|
|
new_cs_rpl = random.nextInt(4);
|
148 |
|
|
new_cs_dpl = random.nextInt(4);
|
149 |
|
|
|
150 |
|
|
old_cs_rpl = (is_v8086)? 3 : random.nextInt(4);
|
151 |
|
|
}
|
152 |
|
|
while(((new_cs_type >> 2) & 1) == 0 && new_cs_dpl < old_cs_rpl); //non-conforming
|
153 |
|
|
|
154 |
|
|
conds[0] = new_cs_seg == false;
|
155 |
|
|
conds[1] = ((new_cs_type >> 3) & 1) == 0; //data
|
156 |
|
|
conds[2] = new_cs_dpl > old_cs_rpl;
|
157 |
|
|
conds[3] = new_cs_p == false;
|
158 |
|
|
|
159 |
|
|
conds[4] = is_v8086 && ( ((new_cs_type >> 2) & 1) == 1 || new_cs_dpl != 0 );
|
160 |
|
|
}
|
161 |
|
|
while(!isAccepted(cond, conds[0],conds[1],conds[2],conds[3],conds[4]));
|
162 |
|
|
|
163 |
|
|
System.out.printf("cond cs: %d\n", cond);
|
164 |
|
|
|
165 |
|
|
long new_cs_base, new_cs_limit;
|
166 |
|
|
boolean new_cs_g;
|
167 |
|
|
while(true) {
|
168 |
|
|
new_cs_base = Layer.norm(random.nextInt());
|
169 |
|
|
new_cs_g = random.nextBoolean();
|
170 |
|
|
|
171 |
|
|
new_cs_limit = random.nextInt(new_cs_g? 0xF : 0xFFFF);
|
172 |
|
|
if(new_cs_g) new_cs_limit = (new_cs_limit << 12) | 0xFFF;
|
173 |
|
|
|
174 |
|
|
if( new_cs_base + new_cs_limit < 4294967296L &&
|
175 |
|
|
Layer.collides(prohibited_list, (int)new_cs_base, (int)(new_cs_base + new_cs_limit)) == false
|
176 |
|
|
) break;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
boolean new_cs_d_b = random.nextBoolean();
|
180 |
|
|
boolean new_cs_l = random.nextBoolean();
|
181 |
|
|
boolean new_cs_avl = random.nextBoolean();
|
182 |
|
|
long new_cs_limit_final = new_cs_g? new_cs_limit >> 12 : new_cs_limit;
|
183 |
|
|
Descriptor cs_desc = new Descriptor((int)new_cs_base, (int)new_cs_limit_final, new_cs_type, new_cs_seg, new_cs_p, new_cs_dpl, new_cs_d_b, new_cs_g, new_cs_l, new_cs_avl);
|
184 |
|
|
|
185 |
|
|
System.out.printf("cs_desc: ");
|
186 |
|
|
for(int i=0; i<8; i++) System.out.printf("%02x ", cs_desc.get_byte(i));
|
187 |
|
|
System.out.printf("\n");
|
188 |
|
|
|
189 |
|
|
//-------
|
190 |
|
|
tables = new DescriptorTableLayer(random, prohibited_list, true);
|
191 |
|
|
|
192 |
|
|
int index = -1;
|
193 |
|
|
if(type == 1) {
|
194 |
|
|
index = random.nextInt(4);
|
195 |
|
|
}
|
196 |
|
|
else if(type == 2) {
|
197 |
|
|
index = tables.getOutOfBoundsIndex(is_cs_ldt);
|
198 |
|
|
if(index == -1) continue;
|
199 |
|
|
}
|
200 |
|
|
else {
|
201 |
|
|
index = tables.addDescriptor(is_cs_ldt, cs_desc);
|
202 |
|
|
if(index == -1) continue;
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
if(type != 1) {
|
206 |
|
|
index <<= 3;
|
207 |
|
|
if(is_cs_ldt) index |= 4;
|
208 |
|
|
index |= new_cs_rpl;
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
//--------------------------------------------------------------
|
212 |
|
|
// prepare interrupt trap gate descriptor
|
213 |
|
|
|
214 |
|
|
conds = new boolean[4];
|
215 |
|
|
cond = 1 << random.nextInt(conds.length);
|
216 |
|
|
if(type >= 1) cond = 0;
|
217 |
|
|
|
218 |
|
|
boolean new_gate_seg = false;
|
219 |
|
|
int new_gate_type = 0;
|
220 |
|
|
int new_gate_dpl = 0;
|
221 |
|
|
boolean new_gate_p = false;
|
222 |
|
|
|
223 |
|
|
do {
|
224 |
|
|
new_gate_type = random.nextInt(16); //TASK_GATE: 0x5, 0x6,0x7, 0xE,0xF
|
225 |
|
|
|
226 |
|
|
new_gate_dpl = random.nextInt(4);
|
227 |
|
|
new_gate_p = random.nextBoolean();
|
228 |
|
|
new_gate_seg = random.nextBoolean();
|
229 |
|
|
|
230 |
|
|
if(((cond & 1) == 1) && old_cs_rpl == 0) {
|
231 |
|
|
cond &= 0xFE;
|
232 |
|
|
cond |= 1 << (1 + random.nextInt(conds.length-1));
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
conds[0] = new_gate_dpl < old_cs_rpl;
|
236 |
|
|
conds[1] = new_gate_p == false;
|
237 |
|
|
conds[2] = new_gate_seg == true;
|
238 |
|
|
conds[3] = new_gate_type != 0x5 && new_gate_type != 0x6 && new_gate_type != 0x7 && new_gate_type != 0xE && new_gate_type != 0xF;
|
239 |
|
|
}
|
240 |
|
|
while(!isAccepted(cond, conds[0],conds[1],conds[2],conds[3]));
|
241 |
|
|
|
242 |
|
|
int types[] = { 0x6,0x7,0xE,0xF };
|
243 |
|
|
if(type >= 1) new_gate_type = types[random.nextInt(types.length)];
|
244 |
|
|
|
245 |
|
|
long new_gate_base = index;
|
246 |
|
|
long new_gate_limit = Layer.norm(random.nextInt(0xFFFFF+1));
|
247 |
|
|
boolean new_gate_g = random.nextBoolean();
|
248 |
|
|
|
249 |
|
|
boolean new_gate_d_b = random.nextBoolean();
|
250 |
|
|
boolean new_gate_l = random.nextBoolean();
|
251 |
|
|
boolean new_gate_avl = random.nextBoolean();
|
252 |
|
|
long new_gate_limit_final = new_gate_g? new_gate_limit >> 12 : new_gate_limit;
|
253 |
|
|
Descriptor gate_desc = new Descriptor((int)new_gate_base, (int)new_gate_limit_final, new_gate_type, new_gate_seg, new_gate_p, new_gate_dpl, new_gate_d_b, new_gate_g, new_gate_l, new_gate_avl);
|
254 |
|
|
|
255 |
|
|
System.out.printf("idt_desc: ");
|
256 |
|
|
for(int i=0; i<8; i++) System.out.printf("%02x ", gate_desc.get_byte(i));
|
257 |
|
|
System.out.printf("\n");
|
258 |
|
|
|
259 |
|
|
final int old_cs_rpl_final = old_cs_rpl;
|
260 |
|
|
Layer cs_rpl_layer = new Layer() {
|
261 |
|
|
long cs_rpl() { return old_cs_rpl_final; }
|
262 |
|
|
};
|
263 |
|
|
layers.addFirst(cs_rpl_layer);
|
264 |
|
|
|
265 |
|
|
//---------- prepare IDT and IDTR
|
266 |
|
|
final int idtr_limit = vector * 8 + 7 + 1 + random.nextInt(5);
|
267 |
|
|
Layer idtr_limit_layer = new Layer() {
|
268 |
|
|
long idtr_limit() { return idtr_limit; }
|
269 |
|
|
};
|
270 |
|
|
layers.addFirst(idtr_limit_layer);
|
271 |
|
|
|
272 |
|
|
// set idtr base
|
273 |
|
|
long idtr_base;
|
274 |
|
|
while(true) {
|
275 |
|
|
idtr_base = Layer.norm(random.nextInt());
|
276 |
|
|
|
277 |
|
|
if( idtr_base + idtr_limit < 4294967296L &&
|
278 |
|
|
Layer.collides(prohibited_list, (int)idtr_base, (int)(idtr_base + idtr_limit)) == false
|
279 |
|
|
) break;
|
280 |
|
|
}
|
281 |
|
|
prohibited_list.add(new Pair<>(idtr_base, idtr_base + idtr_limit));
|
282 |
|
|
|
283 |
|
|
final long idtr_base_final = idtr_base;
|
284 |
|
|
Layer idtr_base_layer = new Layer() {
|
285 |
|
|
long idtr_base() { return idtr_base_final; }
|
286 |
|
|
};
|
287 |
|
|
layers.addFirst(idtr_base_layer);
|
288 |
|
|
|
289 |
|
|
|
290 |
|
|
// eip limit
|
291 |
|
|
long eip = 0;
|
292 |
|
|
if(type == 5) {
|
293 |
|
|
while(true) {
|
294 |
|
|
eip = new_cs_limit + 1 + random.nextInt(10);
|
295 |
|
|
|
296 |
|
|
if(new_gate_type < 0xE) eip &= 0xFFFF;
|
297 |
|
|
|
298 |
|
|
if(eip > new_cs_limit) break;
|
299 |
|
|
}
|
300 |
|
|
if(new_gate_type < 0xE) eip |= (random.nextInt() & 0xFFFF0000);
|
301 |
|
|
}
|
302 |
|
|
else {
|
303 |
|
|
while(true) {
|
304 |
|
|
eip = Layer.norm(random.nextInt((int)new_cs_limit+1));
|
305 |
|
|
|
306 |
|
|
if(new_gate_type < 0xE) eip &= 0xFFFF;
|
307 |
|
|
|
308 |
|
|
if(eip <= new_cs_limit) break;
|
309 |
|
|
}
|
310 |
|
|
long dest = new_cs_base + eip;
|
311 |
|
|
// adding always possible
|
312 |
|
|
MemoryPatchLayer patch = new MemoryPatchLayer(random, prohibited_list, (int)dest, 0x0F,0x0F);
|
313 |
|
|
layers.addFirst(patch);
|
314 |
|
|
|
315 |
|
|
if(new_gate_type < 0xE) eip |= (random.nextInt() & 0xFFFF0000);
|
316 |
|
|
}
|
317 |
|
|
gate_desc.set_dest_offset(eip);
|
318 |
|
|
|
319 |
|
|
// idt table entry
|
320 |
|
|
MemoryPatchLayer int_patch = new MemoryPatchLayer(random, prohibited_list, (int)(idtr_base + 8*vector),
|
321 |
|
|
gate_desc.get_byte(0), gate_desc.get_byte(1), gate_desc.get_byte(2), gate_desc.get_byte(3),
|
322 |
|
|
gate_desc.get_byte(4), gate_desc.get_byte(5), gate_desc.get_byte(6), gate_desc.get_byte(7));
|
323 |
|
|
layers.addFirst(int_patch);
|
324 |
|
|
|
325 |
|
|
|
326 |
|
|
System.out.printf("cond idt: %d, is_ib: %b\n", cond, is_ib);
|
327 |
|
|
|
328 |
|
|
layers.addFirst(tables);
|
329 |
|
|
|
330 |
|
|
if(is_into) {
|
331 |
|
|
Layer of_layer = new Layer() {
|
332 |
|
|
long oflag() { return 1; }
|
333 |
|
|
};
|
334 |
|
|
layers.addFirst(of_layer);
|
335 |
|
|
}
|
336 |
|
|
if(is_v8086) {
|
337 |
|
|
Layer iopl_layer = new Layer() {
|
338 |
|
|
long iopl() { return 3; }
|
339 |
|
|
};
|
340 |
|
|
layers.addFirst(iopl_layer);
|
341 |
|
|
}
|
342 |
|
|
|
343 |
|
|
//------------------------------------------------------------------
|
344 |
|
|
//------------------------------------------------------------------
|
345 |
|
|
|
346 |
|
|
|
347 |
|
|
// end condition
|
348 |
|
|
break;
|
349 |
|
|
}
|
350 |
|
|
|
351 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
352 |
|
|
}
|
353 |
|
|
|
354 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, boolean is_into, boolean is_ib) throws Exception {
|
355 |
|
|
int opcodes[] = {
|
356 |
|
|
0xCC,0xF1,0xCD,0xCE
|
357 |
|
|
};
|
358 |
|
|
|
359 |
|
|
String prefix = "";
|
360 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
361 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
362 |
|
|
|
363 |
|
|
int opcode = opcodes[(is_into)? 3 : (is_ib)? 2 : random.nextInt(3)];
|
364 |
|
|
|
365 |
|
|
int len = (opcode == 0xCD)? 2 : 1;
|
366 |
|
|
|
367 |
|
|
byte instr[] = new byte[len];
|
368 |
|
|
instr[0] = (byte)opcode;
|
369 |
|
|
if(len >= 2) instr[1] = (byte)random.nextInt();
|
370 |
|
|
|
371 |
|
|
if(opcode == 0xCC) vector = 3;
|
372 |
|
|
if(opcode == 0xCD) vector = (instr[1] < 0)? instr[1] + 256 : instr[1];
|
373 |
|
|
if(opcode == 0xCE) vector = 4;
|
374 |
|
|
if(opcode == 0xF1) vector = 1;
|
375 |
|
|
|
376 |
|
|
return prefix + bytesToHex(instr);
|
377 |
|
|
}
|
378 |
|
|
int vector;
|
379 |
|
|
|
380 |
|
|
}
|