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.string;
|
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.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 TestMOVS_rep_any_interrupt extends TestUnit implements Serializable {
|
49 |
|
|
public static void main(String args[]) throws Exception {
|
50 |
|
|
run_test(TestMOVS_rep_any_interrupt.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(1376 + index);
|
63 |
|
|
|
64 |
|
|
String instruction;
|
65 |
|
|
while(true) {
|
66 |
|
|
layers.clear();
|
67 |
|
|
|
68 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
69 |
|
|
|
70 |
|
|
// if false: v8086 mode
|
71 |
|
|
boolean is_real = true;
|
72 |
|
|
|
73 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list, true);
|
74 |
|
|
layers.add(instr);
|
75 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
76 |
|
|
layers.add(stack);
|
77 |
|
|
layers.add(new OtherLayer(is_real ? OtherLayer.Type.REAL : OtherLayer.Type.PROTECTED_OR_V8086, random));
|
78 |
|
|
layers.add(new FlagsLayer(is_real ? FlagsLayer.Type.RANDOM : FlagsLayer.Type.V8086, random));
|
79 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
80 |
|
|
layers.add(new SegmentLayer(random));
|
81 |
|
|
layers.add(new MemoryLayer(random, random.nextBoolean()));
|
82 |
|
|
layers.add(new IOLayer(random));
|
83 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
84 |
|
|
getInput("cr0_pe"),
|
85 |
|
|
getInput("vmflag"),
|
86 |
|
|
getInput("cs_rpl"),
|
87 |
|
|
getInput("cs_p"),
|
88 |
|
|
getInput("cs_s"),
|
89 |
|
|
getInput("cs_type")
|
90 |
|
|
));
|
91 |
|
|
|
92 |
|
|
// instruction size
|
93 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
94 |
|
|
|
95 |
|
|
boolean a32 = random.nextBoolean();
|
96 |
|
|
boolean o32 = random.nextBoolean();
|
97 |
|
|
|
98 |
|
|
long cs_limit = getInput("cs_limit");
|
99 |
|
|
|
100 |
|
|
|
101 |
|
|
/* 0 - no rep; no interrupt
|
102 |
|
|
* 1 - no rep; interrupt on 0
|
103 |
|
|
* 2 - no rep; no interrupt; ecx = 0
|
104 |
|
|
* 3 - rep; no interrupt; ecx = 0
|
105 |
|
|
* 4 - rep; interrupt on 0
|
106 |
|
|
* 5 - rep; interrupt on 8
|
107 |
|
|
* 6 - rep; interrupt on 9
|
108 |
|
|
*/
|
109 |
|
|
int type = random.nextInt(7);
|
110 |
|
|
|
111 |
|
|
int interrupt_position = -1;
|
112 |
|
|
if(type == 0) {
|
113 |
|
|
interrupt_position = -1;
|
114 |
|
|
}
|
115 |
|
|
else if(type == 1) {
|
116 |
|
|
interrupt_position = 0;
|
117 |
|
|
}
|
118 |
|
|
else if(type == 2) {
|
119 |
|
|
interrupt_position = -1;
|
120 |
|
|
}
|
121 |
|
|
else if(type == 3) {
|
122 |
|
|
interrupt_position = -1;
|
123 |
|
|
}
|
124 |
|
|
else if(type == 4) {
|
125 |
|
|
interrupt_position = 0;
|
126 |
|
|
}
|
127 |
|
|
else if(type == 5) {
|
128 |
|
|
interrupt_position = 8;
|
129 |
|
|
}
|
130 |
|
|
else if(type == 6) {
|
131 |
|
|
interrupt_position = 9;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
final int interrupt_position_final = interrupt_position;
|
135 |
|
|
|
136 |
|
|
final int test_type = (type == 0)? 3 : (type == 1 || type == 2 || type == 3)? 4 : 12;
|
137 |
|
|
final int ecx = (type == 2 || type == 3)? 0 : 10;
|
138 |
|
|
Layer esp_layer = new Layer() {
|
139 |
|
|
long esp() { return 0xF0; }
|
140 |
|
|
long ss_base() { return 0; }
|
141 |
|
|
long iflag() { return 1; }
|
142 |
|
|
long tflag() { return 0; }
|
143 |
|
|
|
144 |
|
|
long cs_d_b() { return 0; }
|
145 |
|
|
|
146 |
|
|
long ecx() { return ecx; }
|
147 |
|
|
|
148 |
|
|
long check_interrupt(long counter) { return counter == interrupt_position_final? 0xAB : 0x100; }
|
149 |
|
|
|
150 |
|
|
long get_test_type() { return test_type; }
|
151 |
|
|
};
|
152 |
|
|
layers.addFirst(esp_layer);
|
153 |
|
|
|
154 |
|
|
long handler = cs_limit;
|
155 |
|
|
|
156 |
|
|
//0xAB -- interrupt vector = 0xAB*4 = 0x2AC linear
|
157 |
|
|
MemoryPatchLayer int_patch = new MemoryPatchLayer(random, prohibited_list, (int)(0xAB*4), (byte)(handler&0xFF),(byte)((handler>>8)&0xFF), 0x00,0x00);
|
158 |
|
|
layers.addFirst(int_patch);
|
159 |
|
|
|
160 |
|
|
//interrupt handler: simply IRET
|
161 |
|
|
MemoryPatchLayer handler_patch = new MemoryPatchLayer(random, prohibited_list, (int)handler, 0xCF);
|
162 |
|
|
layers.addFirst(handler_patch);
|
163 |
|
|
|
164 |
|
|
// byte or word/double word
|
165 |
|
|
String instr_string = random.nextBoolean()? "A4" : "A5"; // MOVS
|
166 |
|
|
String rep_string = (type == 0 || type == 1 || type == 2)? "" : random.nextBoolean()? "F2" : "F3";
|
167 |
|
|
|
168 |
|
|
// add instruction
|
169 |
|
|
instruction = rep_string + instr_string + "90909090909090909090909090909090";
|
170 |
|
|
|
171 |
|
|
instr.add_instruction(instruction);
|
172 |
|
|
|
173 |
|
|
// end condition
|
174 |
|
|
break;
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
178 |
|
|
}
|
179 |
|
|
}
|