| 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 static ao486.test.TestUnit.run_test;
|
| 31 |
|
|
import ao486.test.layers.DescriptorTableLayer;
|
| 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 TestPOP_seg_protected extends TestUnit implements Serializable {
|
| 47 |
|
|
public static void main(String args[]) throws Exception {
|
| 48 |
|
|
run_test(TestPOP_seg_protected.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(353 + index);
|
| 61 |
|
|
|
| 62 |
|
|
/* 0. zero selector
|
| 63 |
|
|
* 1. fetch descriptor failed:
|
| 64 |
|
|
* - index over limit (GDT,LDT)
|
| 65 |
|
|
* - LDT not valid
|
| 66 |
|
|
* 2. descriptor check failed:
|
| 67 |
|
|
* - descriptor type not valid, privilege not valid,
|
| 68 |
|
|
* 3. all ok
|
| 69 |
|
|
*/
|
| 70 |
|
|
|
| 71 |
|
|
int type = random.nextInt(4);
|
| 72 |
|
|
|
| 73 |
|
|
String instruction;
|
| 74 |
|
|
while(true) {
|
| 75 |
|
|
layers.clear();
|
| 76 |
|
|
|
| 77 |
|
|
long next_cs_rpl;
|
| 78 |
|
|
boolean descr_seg;
|
| 79 |
|
|
int descr_type;
|
| 80 |
|
|
int descr_dpl;
|
| 81 |
|
|
boolean descr_present;
|
| 82 |
|
|
long selector_rpl;
|
| 83 |
|
|
|
| 84 |
|
|
should_be_ss = random.nextInt(3) == 0;
|
| 85 |
|
|
|
| 86 |
|
|
if(should_be_ss == false) {
|
| 87 |
|
|
int cond = (type == 3)? 0 : 1 << (random.nextInt(100000) % 4);
|
| 88 |
|
|
|
| 89 |
|
|
boolean xs_cond_1;
|
| 90 |
|
|
boolean xs_cond_2;
|
| 91 |
|
|
boolean xs_cond_3;
|
| 92 |
|
|
boolean xs_cond_4;
|
| 93 |
|
|
|
| 94 |
|
|
do {
|
| 95 |
|
|
descr_seg = random.nextBoolean();
|
| 96 |
|
|
descr_type = random.nextInt(16);
|
| 97 |
|
|
descr_dpl = random.nextInt(4);
|
| 98 |
|
|
next_cs_rpl = random.nextInt(4);
|
| 99 |
|
|
descr_present = random.nextBoolean();
|
| 100 |
|
|
selector_rpl = random.nextInt(4);
|
| 101 |
|
|
|
| 102 |
|
|
xs_cond_1 = descr_seg == false;
|
| 103 |
|
|
xs_cond_2 = (descr_type & 8) != 0 && (descr_type & 2) == 0; // code and not readable
|
| 104 |
|
|
xs_cond_3 = ( (descr_type & 8) == 0 || ((descr_type & 8) != 0 && (descr_type & 2) != 0 && (descr_type & 4) == 0) ) &&
|
| 105 |
|
|
( selector_rpl > descr_dpl || next_cs_rpl > descr_dpl );
|
| 106 |
|
|
xs_cond_4 = descr_present == false;
|
| 107 |
|
|
}
|
| 108 |
|
|
while(!isAccepted(cond, xs_cond_1,xs_cond_2,xs_cond_3,xs_cond_4));
|
| 109 |
|
|
|
| 110 |
|
|
System.out.printf("/xs cond: %d/\n", cond);
|
| 111 |
|
|
}
|
| 112 |
|
|
else {
|
| 113 |
|
|
int cond = (type == 3)? 0 : 1 << (random.nextInt(100000) % 6);
|
| 114 |
|
|
|
| 115 |
|
|
boolean ss_cond_1;
|
| 116 |
|
|
boolean ss_cond_2;
|
| 117 |
|
|
boolean ss_cond_3;
|
| 118 |
|
|
boolean ss_cond_4;
|
| 119 |
|
|
boolean ss_cond_5;
|
| 120 |
|
|
boolean ss_cond_6;
|
| 121 |
|
|
|
| 122 |
|
|
do {
|
| 123 |
|
|
next_cs_rpl = random.nextInt(4);
|
| 124 |
|
|
descr_seg = random.nextBoolean();
|
| 125 |
|
|
descr_type = random.nextInt(16);
|
| 126 |
|
|
descr_dpl = random.nextInt(4);
|
| 127 |
|
|
descr_present = random.nextBoolean();
|
| 128 |
|
|
selector_rpl = random.nextInt(4);
|
| 129 |
|
|
|
| 130 |
|
|
ss_cond_1 = selector_rpl != next_cs_rpl;
|
| 131 |
|
|
ss_cond_2 = descr_seg == false;
|
| 132 |
|
|
ss_cond_3 = (descr_type & 8) != 0;
|
| 133 |
|
|
ss_cond_4 = (descr_type & 2) == 0;
|
| 134 |
|
|
ss_cond_5 = descr_dpl != next_cs_rpl;
|
| 135 |
|
|
ss_cond_6 = descr_present == false;
|
| 136 |
|
|
}
|
| 137 |
|
|
while(!isAccepted(cond, ss_cond_1,ss_cond_2,ss_cond_3,ss_cond_4,ss_cond_5,ss_cond_6));
|
| 138 |
|
|
|
| 139 |
|
|
System.out.printf("/ss cond: %d/\n", cond);
|
| 140 |
|
|
}
|
| 141 |
|
|
|
| 142 |
|
|
//0-real; 1-v8086; 2-protected
|
| 143 |
|
|
int mode = 2;
|
| 144 |
|
|
|
| 145 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
| 146 |
|
|
|
| 147 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list);
|
| 148 |
|
|
layers.add(instr);
|
| 149 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
| 150 |
|
|
layers.add(stack);
|
| 151 |
|
|
layers.add(new OtherLayer((mode >= 1)? OtherLayer.Type.PROTECTED_OR_V8086 : OtherLayer.Type.REAL, random));
|
| 152 |
|
|
layers.add(new FlagsLayer((mode == 1)? FlagsLayer.Type.V8086 : (mode == 2)? FlagsLayer.Type.NOT_V8086 : FlagsLayer.Type.RANDOM, random));
|
| 153 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
| 154 |
|
|
layers.add(new SegmentLayer(random));
|
| 155 |
|
|
layers.add(new MemoryLayer(random));
|
| 156 |
|
|
layers.add(new IOLayer(random));
|
| 157 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
| 158 |
|
|
getInput("cr0_pe"),
|
| 159 |
|
|
getInput("vmflag"),
|
| 160 |
|
|
next_cs_rpl, //getInput("cs_rpl"),
|
| 161 |
|
|
getInput("cs_p"),
|
| 162 |
|
|
getInput("cs_s"),
|
| 163 |
|
|
getInput("cs_type")
|
| 164 |
|
|
));
|
| 165 |
|
|
|
| 166 |
|
|
// instruction size
|
| 167 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
| 168 |
|
|
long cs_rpl = getInput("cs_rpl");
|
| 169 |
|
|
|
| 170 |
|
|
boolean a32 = random.nextBoolean();
|
| 171 |
|
|
boolean o32 = random.nextBoolean();
|
| 172 |
|
|
|
| 173 |
|
|
long selector = 0;
|
| 174 |
|
|
|
| 175 |
|
|
if(type == 0) {
|
| 176 |
|
|
selector = random.nextInt(4);
|
| 177 |
|
|
}
|
| 178 |
|
|
else if(type == 1) {
|
| 179 |
|
|
boolean ldtr_valid = random.nextInt(5) != 0;
|
| 180 |
|
|
|
| 181 |
|
|
DescriptorTableLayer tables = new DescriptorTableLayer(random, prohibited_list, ldtr_valid);
|
| 182 |
|
|
|
| 183 |
|
|
boolean is_ldt = random.nextBoolean();
|
| 184 |
|
|
|
| 185 |
|
|
int index = tables.getOutOfBoundsIndex(is_ldt);
|
| 186 |
|
|
if(index == -1) continue;
|
| 187 |
|
|
|
| 188 |
|
|
if(ldtr_valid == false && is_ldt) index = 0;
|
| 189 |
|
|
|
| 190 |
|
|
index = index << 3;
|
| 191 |
|
|
if(is_ldt) index |= 4;
|
| 192 |
|
|
|
| 193 |
|
|
index |= random.nextInt(4);
|
| 194 |
|
|
|
| 195 |
|
|
selector = index;
|
| 196 |
|
|
|
| 197 |
|
|
layers.addFirst(tables);
|
| 198 |
|
|
}
|
| 199 |
|
|
else if(type >= 2) {
|
| 200 |
|
|
TestUnit.Descriptor desc = new TestUnit.Descriptor(
|
| 201 |
|
|
random.nextInt(), //base
|
| 202 |
|
|
random.nextInt() & 0xFFFFF, //limit
|
| 203 |
|
|
descr_type,
|
| 204 |
|
|
descr_seg,
|
| 205 |
|
|
descr_present,
|
| 206 |
|
|
descr_dpl,
|
| 207 |
|
|
random.nextBoolean(), //d_b
|
| 208 |
|
|
random.nextBoolean(), //g
|
| 209 |
|
|
random.nextBoolean(), //l
|
| 210 |
|
|
random.nextBoolean() //avl
|
| 211 |
|
|
);
|
| 212 |
|
|
|
| 213 |
|
|
DescriptorTableLayer tables = new DescriptorTableLayer(random, prohibited_list, true);
|
| 214 |
|
|
|
| 215 |
|
|
boolean is_ldt = random.nextBoolean();
|
| 216 |
|
|
|
| 217 |
|
|
int index = tables.addDescriptor(is_ldt, desc);
|
| 218 |
|
|
if(index == -1) continue;
|
| 219 |
|
|
|
| 220 |
|
|
index = index << 3;
|
| 221 |
|
|
if(is_ldt) index |= 4;
|
| 222 |
|
|
|
| 223 |
|
|
index |= selector_rpl;
|
| 224 |
|
|
|
| 225 |
|
|
selector = index;
|
| 226 |
|
|
|
| 227 |
|
|
layers.addFirst(tables);
|
| 228 |
|
|
}
|
| 229 |
|
|
|
| 230 |
|
|
System.out.printf("selector: %x\n", selector);
|
| 231 |
|
|
|
| 232 |
|
|
stack.push_word((int)selector);
|
| 233 |
|
|
stack.push_word((int)selector);
|
| 234 |
|
|
|
| 235 |
|
|
// instruction
|
| 236 |
|
|
instruction = prepare_instr(cs_d_b, a32, o32);
|
| 237 |
|
|
|
| 238 |
|
|
instruction += instruction;
|
| 239 |
|
|
instruction += "0F0F";
|
| 240 |
|
|
|
| 241 |
|
|
// add instruction
|
| 242 |
|
|
instr.add_instruction(instruction);
|
| 243 |
|
|
|
| 244 |
|
|
// end condition
|
| 245 |
|
|
break;
|
| 246 |
|
|
}
|
| 247 |
|
|
|
| 248 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
| 249 |
|
|
}
|
| 250 |
|
|
|
| 251 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32) throws Exception {
|
| 252 |
|
|
|
| 253 |
|
|
int opcodes[] = {
|
| 254 |
|
|
0x07,0x17,0x1F,0xA1,0xA9
|
| 255 |
|
|
};
|
| 256 |
|
|
|
| 257 |
|
|
String prefix = "";
|
| 258 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
| 259 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
| 260 |
|
|
|
| 261 |
|
|
int opcode = opcodes[random.nextInt(opcodes.length)];
|
| 262 |
|
|
|
| 263 |
|
|
if(should_be_ss) opcode = 0x17;
|
| 264 |
|
|
|
| 265 |
|
|
byte instr[] = new byte[1];
|
| 266 |
|
|
instr[0] = (byte)opcode;
|
| 267 |
|
|
|
| 268 |
|
|
return prefix + ((opcode == 0xA1 || opcode == 0xA9)? "0F" : "") + bytesToHex(instr);
|
| 269 |
|
|
}
|
| 270 |
|
|
|
| 271 |
|
|
boolean should_be_ss;
|
| 272 |
|
|
}
|