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.segment;
|
28 |
|
|
|
29 |
|
|
import ao486.test.TestUnit;
|
30 |
|
|
import ao486.test.layers.DescriptorTableLayer;
|
31 |
|
|
import ao486.test.layers.EffectiveAddressLayerFactory;
|
32 |
|
|
import ao486.test.layers.FlagsLayer;
|
33 |
|
|
import ao486.test.layers.GeneralRegisterLayer;
|
34 |
|
|
import ao486.test.layers.HandleModeChangeLayer;
|
35 |
|
|
import ao486.test.layers.IOLayer;
|
36 |
|
|
import ao486.test.layers.InstructionLayer;
|
37 |
|
|
import ao486.test.layers.MemoryLayer;
|
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.Serializable;
|
43 |
|
|
import java.util.LinkedList;
|
44 |
|
|
import java.util.Random;
|
45 |
|
|
|
46 |
|
|
public class TestLAR extends TestUnit implements Serializable {
|
47 |
|
|
public static void main(String args[]) throws Exception {
|
48 |
|
|
run_test(TestLAR.class);
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
//--------------------------------------------------------------------------
|
52 |
|
|
@Override
|
53 |
|
|
public int get_test_count() throws Exception {
|
54 |
|
|
return 100;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
@Override
|
58 |
|
|
public void init() throws Exception {
|
59 |
|
|
|
60 |
|
|
random = new Random(25 + index);
|
61 |
|
|
|
62 |
|
|
/* 0. not protected mode
|
63 |
|
|
* 1. zero selector
|
64 |
|
|
* 2. fetch descriptor failed:
|
65 |
|
|
* - index over limit (GDT,LDT)
|
66 |
|
|
* - LDT not valid
|
67 |
|
|
* 3. descriptor check failed:
|
68 |
|
|
* - descriptor type not valid, privilege not valid,
|
69 |
|
|
* 4. all ok
|
70 |
|
|
*/
|
71 |
|
|
|
72 |
|
|
int type = random.nextInt(4+1);
|
73 |
|
|
|
74 |
|
|
String instruction;
|
75 |
|
|
while(true) {
|
76 |
|
|
layers.clear();
|
77 |
|
|
|
78 |
|
|
long next_cs_rpl;
|
79 |
|
|
boolean descr_seg;
|
80 |
|
|
int descr_type;
|
81 |
|
|
int descr_dpl;
|
82 |
|
|
boolean descr_present;
|
83 |
|
|
long selector_rpl;
|
84 |
|
|
|
85 |
|
|
int cond = (type == 4)? 0 : 1 << (random.nextInt(100000) % 4);
|
86 |
|
|
|
87 |
|
|
System.out.printf("cond: %d, index: %d\n", cond, index);
|
88 |
|
|
|
89 |
|
|
boolean lar_cond_1;
|
90 |
|
|
boolean lar_cond_2;
|
91 |
|
|
boolean lar_cond_3;
|
92 |
|
|
boolean lar_cond_4;
|
93 |
|
|
|
94 |
|
|
do {
|
95 |
|
|
next_cs_rpl = random.nextInt(4);
|
96 |
|
|
descr_seg = random.nextBoolean();
|
97 |
|
|
descr_type = random.nextInt(16);
|
98 |
|
|
descr_dpl = random.nextInt(4);
|
99 |
|
|
descr_present = random.nextBoolean();
|
100 |
|
|
selector_rpl = random.nextInt(4);
|
101 |
|
|
|
102 |
|
|
lar_cond_1 = !descr_seg && (descr_type == 0 || descr_type == 8 || descr_type == 10 || descr_type == 13);
|
103 |
|
|
lar_cond_2 = descr_seg && ( ((descr_type >> 3)&1) == 0 || ((descr_type >> 2)&1) == 0 ) &&
|
104 |
|
|
(descr_dpl < next_cs_rpl || descr_dpl < selector_rpl);
|
105 |
|
|
lar_cond_3 = !descr_seg && (descr_type == 6 || descr_type == 7 || descr_type == 14 || descr_type == 15);
|
106 |
|
|
lar_cond_4 = !descr_seg && (descr_dpl < next_cs_rpl || descr_dpl < selector_rpl);
|
107 |
|
|
}
|
108 |
|
|
while(!isAccepted(cond, lar_cond_1,lar_cond_2,lar_cond_3,lar_cond_4));
|
109 |
|
|
|
110 |
|
|
//0-real; 1-v8086; 2-protected
|
111 |
|
|
int mode = (type == 0)? random.nextInt(2) : 2;
|
112 |
|
|
|
113 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
114 |
|
|
|
115 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list);
|
116 |
|
|
layers.add(instr);
|
117 |
|
|
layers.add(new StackLayer(random, prohibited_list));
|
118 |
|
|
layers.add(new OtherLayer((mode >= 1)? OtherLayer.Type.PROTECTED_OR_V8086 : OtherLayer.Type.REAL, random));
|
119 |
|
|
layers.add(new FlagsLayer((mode == 1)? FlagsLayer.Type.V8086 : (mode == 2)? FlagsLayer.Type.NOT_V8086 : FlagsLayer.Type.RANDOM, random));
|
120 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
121 |
|
|
layers.add(new SegmentLayer(random));
|
122 |
|
|
layers.add(new MemoryLayer(random));
|
123 |
|
|
layers.add(new IOLayer(random));
|
124 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
125 |
|
|
getInput("cr0_pe"),
|
126 |
|
|
getInput("vmflag"),
|
127 |
|
|
next_cs_rpl, //getInput("cs_rpl"),
|
128 |
|
|
getInput("cs_p"),
|
129 |
|
|
getInput("cs_s"),
|
130 |
|
|
getInput("cs_type")
|
131 |
|
|
));
|
132 |
|
|
|
133 |
|
|
// instruction size
|
134 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
135 |
|
|
long cs_rpl = getInput("cs_rpl");
|
136 |
|
|
|
137 |
|
|
boolean a32 = random.nextBoolean();
|
138 |
|
|
boolean o32 = random.nextBoolean();
|
139 |
|
|
|
140 |
|
|
long selector = 0;
|
141 |
|
|
|
142 |
|
|
if(type == 1) {
|
143 |
|
|
selector = random.nextInt(4);
|
144 |
|
|
}
|
145 |
|
|
else if(type == 2) {
|
146 |
|
|
boolean ldtr_valid = random.nextInt(5) != 0;
|
147 |
|
|
|
148 |
|
|
DescriptorTableLayer tables = new DescriptorTableLayer(random, prohibited_list, ldtr_valid);
|
149 |
|
|
|
150 |
|
|
boolean is_ldt = random.nextBoolean();
|
151 |
|
|
|
152 |
|
|
int index = tables.getOutOfBoundsIndex(is_ldt);
|
153 |
|
|
if(index == -1) continue;
|
154 |
|
|
|
155 |
|
|
if(ldtr_valid == false && is_ldt) index = 0;
|
156 |
|
|
|
157 |
|
|
index = index << 3;
|
158 |
|
|
if(is_ldt) index |= 4;
|
159 |
|
|
|
160 |
|
|
index |= random.nextInt(4);
|
161 |
|
|
|
162 |
|
|
selector = index;
|
163 |
|
|
|
164 |
|
|
layers.addFirst(tables);
|
165 |
|
|
}
|
166 |
|
|
else if(type >= 3) {
|
167 |
|
|
Descriptor desc = new Descriptor(
|
168 |
|
|
random.nextInt(), //base
|
169 |
|
|
random.nextInt() & 0xFFFFF, //limit
|
170 |
|
|
descr_type,
|
171 |
|
|
descr_seg,
|
172 |
|
|
descr_present,
|
173 |
|
|
descr_dpl,
|
174 |
|
|
random.nextBoolean(), //d_b
|
175 |
|
|
random.nextBoolean(), //g
|
176 |
|
|
random.nextBoolean(), //l
|
177 |
|
|
random.nextBoolean() //avl
|
178 |
|
|
);
|
179 |
|
|
|
180 |
|
|
DescriptorTableLayer tables = new DescriptorTableLayer(random, prohibited_list, true);
|
181 |
|
|
|
182 |
|
|
boolean is_ldt = random.nextBoolean();
|
183 |
|
|
|
184 |
|
|
int index = tables.addDescriptor(is_ldt, desc);
|
185 |
|
|
if(index == -1) continue;
|
186 |
|
|
|
187 |
|
|
index = index << 3;
|
188 |
|
|
if(is_ldt) index |= 4;
|
189 |
|
|
|
190 |
|
|
index |= selector_rpl;
|
191 |
|
|
|
192 |
|
|
selector = index;
|
193 |
|
|
|
194 |
|
|
layers.addFirst(tables);
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
byte extra_bytes[] = null;
|
198 |
|
|
|
199 |
|
|
byte modregrm_bytes[] = EffectiveAddressLayerFactory.prepare(
|
200 |
|
|
selector,
|
201 |
|
|
0, EffectiveAddressLayerFactory.modregrm_reg_t.RANDOM,
|
202 |
|
|
2, a32,
|
203 |
|
|
layers, random, this, false, false);
|
204 |
|
|
extra_bytes = modregrm_bytes;
|
205 |
|
|
|
206 |
|
|
// instruction
|
207 |
|
|
instruction = prepare_instr(cs_d_b, a32, o32, extra_bytes);
|
208 |
|
|
|
209 |
|
|
instruction += instruction;
|
210 |
|
|
instruction += "0F0F";
|
211 |
|
|
|
212 |
|
|
// add instruction
|
213 |
|
|
instr.add_instruction(instruction);
|
214 |
|
|
|
215 |
|
|
// end condition
|
216 |
|
|
break;
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte modregrm_bytes[]) throws Exception {
|
223 |
|
|
|
224 |
|
|
int opcodes[] = {
|
225 |
|
|
0x02
|
226 |
|
|
};
|
227 |
|
|
|
228 |
|
|
String prefix = "";
|
229 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
230 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
231 |
|
|
|
232 |
|
|
int opcode = opcodes[random.nextInt(opcodes.length)];
|
233 |
|
|
|
234 |
|
|
byte instr[] = new byte[1 + modregrm_bytes.length];
|
235 |
|
|
instr[0] = (byte)opcode;
|
236 |
|
|
System.arraycopy(modregrm_bytes, 0, instr, 1, modregrm_bytes.length);
|
237 |
|
|
|
238 |
|
|
return prefix + "0F" + bytesToHex(instr);
|
239 |
|
|
}
|
240 |
|
|
}
|