| 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.branch;
|
| 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.Layer;
|
| 38 |
|
|
import ao486.test.layers.MemoryLayer;
|
| 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 ao486.test.layers.TSSCurrentLayer;
|
| 44 |
|
|
import java.io.*;
|
| 45 |
|
|
import java.util.LinkedList;
|
| 46 |
|
|
import java.util.Random;
|
| 47 |
|
|
|
| 48 |
|
|
|
| 49 |
|
|
public class TestCALL_task_gate extends TestUnit implements Serializable {
|
| 50 |
|
|
public static void main(String args[]) throws Exception {
|
| 51 |
|
|
run_test(TestCALL_task_gate.class);
|
| 52 |
|
|
}
|
| 53 |
|
|
|
| 54 |
|
|
//--------------------------------------------------------------------------
|
| 55 |
|
|
@Override
|
| 56 |
|
|
public int get_test_count() throws Exception {
|
| 57 |
|
|
return 100;
|
| 58 |
|
|
}
|
| 59 |
|
|
|
| 60 |
|
|
@Override
|
| 61 |
|
|
public void init() throws Exception {
|
| 62 |
|
|
|
| 63 |
|
|
random = new Random(128+index);
|
| 64 |
|
|
|
| 65 |
|
|
String instruction;
|
| 66 |
|
|
while(true) {
|
| 67 |
|
|
layers.clear();
|
| 68 |
|
|
|
| 69 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
| 70 |
|
|
|
| 71 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list);
|
| 72 |
|
|
layers.add(instr);
|
| 73 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
| 74 |
|
|
layers.add(stack);
|
| 75 |
|
|
layers.add(new OtherLayer(OtherLayer.Type.PROTECTED_OR_V8086, random));
|
| 76 |
|
|
layers.add(new FlagsLayer(FlagsLayer.Type.NOT_V8086, random));
|
| 77 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
| 78 |
|
|
layers.add(new SegmentLayer(random));
|
| 79 |
|
|
layers.add(new MemoryLayer(random));
|
| 80 |
|
|
layers.add(new IOLayer(random));
|
| 81 |
|
|
|
| 82 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
| 83 |
|
|
getInput("cr0_pe"),
|
| 84 |
|
|
getInput("vmflag"),
|
| 85 |
|
|
getInput("cs_rpl"),
|
| 86 |
|
|
getInput("cs_p"),
|
| 87 |
|
|
getInput("cs_s"),
|
| 88 |
|
|
getInput("cs_type")
|
| 89 |
|
|
));
|
| 90 |
|
|
|
| 91 |
|
|
// instruction size
|
| 92 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
| 93 |
|
|
|
| 94 |
|
|
boolean a32 = random.nextBoolean();
|
| 95 |
|
|
boolean o32 = random.nextBoolean();
|
| 96 |
|
|
|
| 97 |
|
|
/* null check, selector limit checked in: TestCALL_protected_seg
|
| 98 |
|
|
*
|
| 99 |
|
|
* 0 - pre-(task gate) valid check
|
| 100 |
|
|
* 1 - tss_selector TI
|
| 101 |
|
|
* 2 - tss_descriptor out of bounds
|
| 102 |
|
|
* 3 - tss_descriptor valid check
|
| 103 |
|
|
*
|
| 104 |
|
|
* >=4 - task switch tests
|
| 105 |
|
|
*/
|
| 106 |
|
|
|
| 107 |
|
|
int type = random.nextInt(5);
|
| 108 |
|
|
System.out.println("Running test type: " + type);
|
| 109 |
|
|
int task_switch_type = -1; //26;
|
| 110 |
|
|
|
| 111 |
|
|
DescriptorTableLayer tables = null;
|
| 112 |
|
|
int new_tss_selector = random.nextInt(4);
|
| 113 |
|
|
int old_tss_limit = 0xFFFF;
|
| 114 |
|
|
|
| 115 |
|
|
TSSCurrentLayer.Type old_tss_type = random.nextBoolean()? TSSCurrentLayer.Type.BUSY_286 : TSSCurrentLayer.Type.BUSY_386;
|
| 116 |
|
|
|
| 117 |
|
|
//------------------------------------------------------------------
|
| 118 |
|
|
//------------------------------------------------------------------
|
| 119 |
|
|
|
| 120 |
|
|
if(type >= 0) {
|
| 121 |
|
|
// prepare tss descriptor
|
| 122 |
|
|
boolean is_tss_ldt = (type == 1)? true : false;
|
| 123 |
|
|
|
| 124 |
|
|
boolean conds[] = new boolean[3];
|
| 125 |
|
|
int cond = 1 << random.nextInt(conds.length);
|
| 126 |
|
|
if(type >= 4) cond = 0;
|
| 127 |
|
|
|
| 128 |
|
|
int new_tss_rpl = 0;
|
| 129 |
|
|
boolean new_tss_seg = false;
|
| 130 |
|
|
int new_tss_type = 0;
|
| 131 |
|
|
int new_tss_dpl = 0;
|
| 132 |
|
|
boolean new_tss_p = false;
|
| 133 |
|
|
|
| 134 |
|
|
do {
|
| 135 |
|
|
new_tss_seg = random.nextBoolean();
|
| 136 |
|
|
new_tss_type = random.nextInt(16);
|
| 137 |
|
|
new_tss_p = random.nextBoolean();
|
| 138 |
|
|
|
| 139 |
|
|
new_tss_rpl = random.nextInt(4);
|
| 140 |
|
|
new_tss_dpl = random.nextInt(4);
|
| 141 |
|
|
|
| 142 |
|
|
|
| 143 |
|
|
conds[0] = new_tss_seg;
|
| 144 |
|
|
conds[1] = new_tss_type != 0x1 && new_tss_type != 0x9; //AVAIL_TSS_286,386
|
| 145 |
|
|
conds[2] = new_tss_p == false;
|
| 146 |
|
|
}
|
| 147 |
|
|
while(!isAccepted(cond, conds[0],conds[1],conds[2]));
|
| 148 |
|
|
|
| 149 |
|
|
long new_tss_base, new_tss_limit;
|
| 150 |
|
|
boolean new_tss_g;
|
| 151 |
|
|
while(true) {
|
| 152 |
|
|
new_tss_base = Layer.norm(random.nextInt());
|
| 153 |
|
|
new_tss_g = random.nextBoolean();
|
| 154 |
|
|
|
| 155 |
|
|
new_tss_limit = random.nextInt(new_tss_g? 0xF : 0xFFFF);
|
| 156 |
|
|
if(new_tss_g) new_tss_limit = (new_tss_limit << 12) | 0xFFF;
|
| 157 |
|
|
|
| 158 |
|
|
if( new_tss_base + new_tss_limit < 4294967296L &&
|
| 159 |
|
|
Layer.collides(prohibited_list, (int)new_tss_base, (int)(new_tss_base + new_tss_limit)) == false
|
| 160 |
|
|
) break;
|
| 161 |
|
|
}
|
| 162 |
|
|
|
| 163 |
|
|
boolean new_tss_d_b = random.nextBoolean();
|
| 164 |
|
|
boolean new_tss_l = random.nextBoolean();
|
| 165 |
|
|
boolean new_tss_avl = random.nextBoolean();
|
| 166 |
|
|
long new_tss_limit_final = new_tss_g? new_tss_limit >> 12 : new_tss_limit;
|
| 167 |
|
|
Descriptor tss_desc = new Descriptor((int)new_tss_base, (int)new_tss_limit_final, new_tss_type, new_tss_seg, new_tss_p, new_tss_dpl, new_tss_d_b, new_tss_g, new_tss_l, new_tss_avl);
|
| 168 |
|
|
|
| 169 |
|
|
System.out.printf("tss_desc: ");
|
| 170 |
|
|
for(int i=0; i<8; i++) System.out.printf("%02x ", tss_desc.get_byte(i));
|
| 171 |
|
|
System.out.printf("\n");
|
| 172 |
|
|
|
| 173 |
|
|
tables = new DescriptorTableLayer(random, prohibited_list, true);
|
| 174 |
|
|
|
| 175 |
|
|
int index = -1;
|
| 176 |
|
|
if(type != 2) {
|
| 177 |
|
|
index = tables.addDescriptor(is_tss_ldt, tss_desc);
|
| 178 |
|
|
if(index == -1) continue;
|
| 179 |
|
|
}
|
| 180 |
|
|
else {
|
| 181 |
|
|
index = tables.getOutOfBoundsIndex(is_tss_ldt);
|
| 182 |
|
|
if(index == -1) continue;
|
| 183 |
|
|
}
|
| 184 |
|
|
|
| 185 |
|
|
index <<= 3;
|
| 186 |
|
|
if(is_tss_ldt) index |= 4;
|
| 187 |
|
|
index |= new_tss_rpl;
|
| 188 |
|
|
|
| 189 |
|
|
|
| 190 |
|
|
// prepare task gate descriptor
|
| 191 |
|
|
|
| 192 |
|
|
|
| 193 |
|
|
boolean is_ldt = random.nextBoolean();
|
| 194 |
|
|
|
| 195 |
|
|
conds = new boolean[3];
|
| 196 |
|
|
cond = 1 << random.nextInt(conds.length);
|
| 197 |
|
|
if(type >= 1) cond = 0;
|
| 198 |
|
|
|
| 199 |
|
|
int new_cs_rpl = 0;
|
| 200 |
|
|
int old_cs_rpl = 0;
|
| 201 |
|
|
boolean new_cs_seg = false;
|
| 202 |
|
|
int new_cs_type = 0;
|
| 203 |
|
|
int new_cs_dpl = 0;
|
| 204 |
|
|
boolean new_cs_p = false;
|
| 205 |
|
|
|
| 206 |
|
|
do {
|
| 207 |
|
|
new_cs_seg = false;
|
| 208 |
|
|
new_cs_type = 0x5; //TASK_GATE
|
| 209 |
|
|
|
| 210 |
|
|
new_cs_rpl = random.nextInt(4);
|
| 211 |
|
|
old_cs_rpl = random.nextInt(4);
|
| 212 |
|
|
new_cs_dpl = random.nextInt(4);
|
| 213 |
|
|
new_cs_p = random.nextBoolean();
|
| 214 |
|
|
is_ldt = random.nextBoolean();
|
| 215 |
|
|
|
| 216 |
|
|
conds[0] = new_cs_dpl < old_cs_rpl;
|
| 217 |
|
|
conds[1] = new_cs_dpl < new_cs_rpl;
|
| 218 |
|
|
conds[2] = new_cs_p == false;
|
| 219 |
|
|
}
|
| 220 |
|
|
while(!isAccepted(cond, conds[0],conds[1],conds[2]));
|
| 221 |
|
|
|
| 222 |
|
|
long new_cs_base = index;
|
| 223 |
|
|
long new_cs_limit = Layer.norm(random.nextInt(0xFFFFF+1));
|
| 224 |
|
|
boolean new_cs_g = random.nextBoolean();
|
| 225 |
|
|
|
| 226 |
|
|
boolean new_cs_d_b = random.nextBoolean();
|
| 227 |
|
|
boolean new_cs_l = random.nextBoolean();
|
| 228 |
|
|
boolean new_cs_avl = random.nextBoolean();
|
| 229 |
|
|
long new_cs_limit_final = new_cs_g? new_cs_limit >> 12 : new_cs_limit;
|
| 230 |
|
|
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);
|
| 231 |
|
|
|
| 232 |
|
|
System.out.printf("cs_desc: ");
|
| 233 |
|
|
for(int i=0; i<8; i++) System.out.printf("%02x ", cs_desc.get_byte(i));
|
| 234 |
|
|
System.out.printf("\n");
|
| 235 |
|
|
|
| 236 |
|
|
final int old_cs_rpl_final = old_cs_rpl;
|
| 237 |
|
|
Layer cs_rpl_layer = new Layer() {
|
| 238 |
|
|
long cs_rpl() { return old_cs_rpl_final; }
|
| 239 |
|
|
};
|
| 240 |
|
|
layers.addFirst(cs_rpl_layer);
|
| 241 |
|
|
|
| 242 |
|
|
//----------
|
| 243 |
|
|
index = tables.addDescriptor(is_ldt, cs_desc);
|
| 244 |
|
|
if(index == -1) continue;
|
| 245 |
|
|
|
| 246 |
|
|
index = index << 3;
|
| 247 |
|
|
if(is_ldt) index |= 4;
|
| 248 |
|
|
index |= new_cs_rpl;
|
| 249 |
|
|
|
| 250 |
|
|
new_tss_selector = index;
|
| 251 |
|
|
TestTaskSwitch.new_tss_selector = new_tss_selector;
|
| 252 |
|
|
|
| 253 |
|
|
if(type == 0) {
|
| 254 |
|
|
layers.addFirst(tables);
|
| 255 |
|
|
}
|
| 256 |
|
|
|
| 257 |
|
|
System.out.printf("cond: %d\n", cond);
|
| 258 |
|
|
|
| 259 |
|
|
if(type >= 4) {
|
| 260 |
|
|
boolean is_ok = TestTaskSwitch.test(random, this, prohibited_list, TestTaskSwitch.Source.FROM_CALL, tss_desc, new_cs_rpl, tables, task_switch_type);
|
| 261 |
|
|
if(is_ok == false) continue;
|
| 262 |
|
|
|
| 263 |
|
|
tables = TestTaskSwitch.tables;
|
| 264 |
|
|
new_tss_selector = TestTaskSwitch.new_tss_selector;
|
| 265 |
|
|
old_tss_limit = TestTaskSwitch.old_tss_limit;
|
| 266 |
|
|
}
|
| 267 |
|
|
}
|
| 268 |
|
|
|
| 269 |
|
|
//------------------------------------------------------------------
|
| 270 |
|
|
//------------------------------------------------------------------
|
| 271 |
|
|
|
| 272 |
|
|
long new_eip = 0;
|
| 273 |
|
|
long new_cs = new_tss_selector;
|
| 274 |
|
|
|
| 275 |
|
|
if(type >= 1) {
|
| 276 |
|
|
TSSCurrentLayer old_tss = new TSSCurrentLayer(random, old_tss_type, old_tss_limit, new_tss_selector, prohibited_list);
|
| 277 |
|
|
layers.addFirst(old_tss);
|
| 278 |
|
|
|
| 279 |
|
|
layers.addFirst(tables);
|
| 280 |
|
|
}
|
| 281 |
|
|
|
| 282 |
|
|
// instruction
|
| 283 |
|
|
byte extra_bytes[] = null;
|
| 284 |
|
|
|
| 285 |
|
|
boolean is_Ep = random.nextBoolean();
|
| 286 |
|
|
|
| 287 |
|
|
if(is_Ep) {
|
| 288 |
|
|
byte modregrm_bytes[] = EffectiveAddressLayerFactory.prepare(
|
| 289 |
|
|
o32? (((new_cs & 0xFFFF) << 32) | (new_eip & 0xFFFFFFFF)) : (((new_cs & 0xFFFF) << 16) | (new_eip & 0xFFFF)),
|
| 290 |
|
|
3, EffectiveAddressLayerFactory.modregrm_reg_t.SET,
|
| 291 |
|
|
o32? 6 : 4, a32,
|
| 292 |
|
|
layers, random, this, true, false);
|
| 293 |
|
|
extra_bytes = modregrm_bytes;
|
| 294 |
|
|
}
|
| 295 |
|
|
else {
|
| 296 |
|
|
long immediate = o32? (((new_cs & 0xFFFF) << 32) | (new_eip & 0xFFFFFFFF)) : (((new_cs & 0xFFFF) << 16) | (new_eip & 0xFFFF));
|
| 297 |
|
|
|
| 298 |
|
|
byte imm_bytes[] = new byte[o32? 6 : 4];
|
| 299 |
|
|
for(int i=0; i<imm_bytes.length; i++) {
|
| 300 |
|
|
imm_bytes[i] = (byte)(immediate & 0xFF);
|
| 301 |
|
|
immediate >>= 8;
|
| 302 |
|
|
}
|
| 303 |
|
|
extra_bytes = imm_bytes;
|
| 304 |
|
|
}
|
| 305 |
|
|
|
| 306 |
|
|
instruction = prepare_instr(cs_d_b, a32, o32, extra_bytes, is_Ep);
|
| 307 |
|
|
instr.add_instruction(instruction);
|
| 308 |
|
|
|
| 309 |
|
|
// end condition
|
| 310 |
|
|
break;
|
| 311 |
|
|
}
|
| 312 |
|
|
|
| 313 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
| 314 |
|
|
}
|
| 315 |
|
|
|
| 316 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte extra_bytes[], boolean is_Ep) throws Exception {
|
| 317 |
|
|
int opcodes[] = {
|
| 318 |
|
|
0xFF, 0x9A
|
| 319 |
|
|
};
|
| 320 |
|
|
|
| 321 |
|
|
String prefix = "";
|
| 322 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
| 323 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
| 324 |
|
|
|
| 325 |
|
|
int opcode = opcodes[is_Ep? 0 : 1];
|
| 326 |
|
|
|
| 327 |
|
|
byte instr[] = new byte[1 + extra_bytes.length];
|
| 328 |
|
|
instr[0] = (byte)opcode;
|
| 329 |
|
|
System.arraycopy(extra_bytes, 0, instr, 1, extra_bytes.length);
|
| 330 |
|
|
|
| 331 |
|
|
return prefix + bytesToHex(instr);
|
| 332 |
|
|
}
|
| 333 |
|
|
|
| 334 |
|
|
}
|