| 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.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 |
|
|
public class TestRET_far_same extends TestUnit implements Serializable {
|
| 48 |
|
|
public static void main(String args[]) throws Exception {
|
| 49 |
|
|
run_test(TestRET_far_same.class);
|
| 50 |
|
|
}
|
| 51 |
|
|
|
| 52 |
|
|
//--------------------------------------------------------------------------
|
| 53 |
|
|
@Override
|
| 54 |
|
|
public int get_test_count() throws Exception {
|
| 55 |
|
|
return 100;
|
| 56 |
|
|
}
|
| 57 |
|
|
|
| 58 |
|
|
@Override
|
| 59 |
|
|
public void init() throws Exception {
|
| 60 |
|
|
|
| 61 |
|
|
random = new Random(9 + index);
|
| 62 |
|
|
|
| 63 |
|
|
String instruction;
|
| 64 |
|
|
while(true) {
|
| 65 |
|
|
layers.clear();
|
| 66 |
|
|
|
| 67 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
| 68 |
|
|
|
| 69 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list);
|
| 70 |
|
|
layers.add(instr);
|
| 71 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
| 72 |
|
|
layers.add(stack);
|
| 73 |
|
|
layers.add(new OtherLayer(random));
|
| 74 |
|
|
layers.add(new FlagsLayer(random));
|
| 75 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
| 76 |
|
|
layers.add(new SegmentLayer(random));
|
| 77 |
|
|
layers.add(new MemoryLayer(random));
|
| 78 |
|
|
layers.add(new IOLayer(random));
|
| 79 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
| 80 |
|
|
getInput("cr0_pe"),
|
| 81 |
|
|
getInput("vmflag"),
|
| 82 |
|
|
getInput("cs_rpl"),
|
| 83 |
|
|
getInput("cs_p"),
|
| 84 |
|
|
getInput("cs_s"),
|
| 85 |
|
|
getInput("cs_type")
|
| 86 |
|
|
));
|
| 87 |
|
|
|
| 88 |
|
|
|
| 89 |
|
|
/*
|
| 90 |
|
|
* 0. cs_selector null
|
| 91 |
|
|
* 1. cs_descriptor out of bounds
|
| 92 |
|
|
* 2. check cs
|
| 93 |
|
|
* 3. same: eip out of bounds
|
| 94 |
|
|
* 4. same: all ok
|
| 95 |
|
|
*/
|
| 96 |
|
|
|
| 97 |
|
|
int test_type = 4; //random.nextInt(5);
|
| 98 |
|
|
|
| 99 |
|
|
// instruction size
|
| 100 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
| 101 |
|
|
boolean cr0_pe = getInput("cr0_pe") == 1;
|
| 102 |
|
|
boolean flag_vm = getInput("vmflag") == 1;
|
| 103 |
|
|
|
| 104 |
|
|
boolean a32 = random.nextBoolean();
|
| 105 |
|
|
boolean o32 = random.nextBoolean();
|
| 106 |
|
|
|
| 107 |
|
|
long cs = random.nextInt(3+1);
|
| 108 |
|
|
long eip = Layer.norm(random.nextInt());
|
| 109 |
|
|
|
| 110 |
|
|
if(test_type == 0) {
|
| 111 |
|
|
// nothing
|
| 112 |
|
|
}
|
| 113 |
|
|
else if(test_type == 1) {
|
| 114 |
|
|
boolean ldtr_valid = random.nextInt(5) != 0;
|
| 115 |
|
|
|
| 116 |
|
|
DescriptorTableLayer tables = new DescriptorTableLayer(random, prohibited_list, ldtr_valid);
|
| 117 |
|
|
|
| 118 |
|
|
boolean is_ldt = (ldtr_valid == false)? false : random.nextBoolean();
|
| 119 |
|
|
|
| 120 |
|
|
int index = tables.getOutOfBoundsIndex(is_ldt);
|
| 121 |
|
|
if(index == -1) continue;
|
| 122 |
|
|
|
| 123 |
|
|
index = index << 3;
|
| 124 |
|
|
if(is_ldt) index |= 4;
|
| 125 |
|
|
|
| 126 |
|
|
// rpl always zero
|
| 127 |
|
|
|
| 128 |
|
|
cs = index;
|
| 129 |
|
|
|
| 130 |
|
|
layers.addFirst(tables);
|
| 131 |
|
|
}
|
| 132 |
|
|
else if(test_type == 2 || test_type == 3 || test_type == 4) {
|
| 133 |
|
|
DescriptorTableLayer tables = new DescriptorTableLayer(random, prohibited_list, true);
|
| 134 |
|
|
|
| 135 |
|
|
boolean is_ldt = random.nextBoolean();
|
| 136 |
|
|
|
| 137 |
|
|
|
| 138 |
|
|
boolean conds[] = new boolean[6];
|
| 139 |
|
|
int cond = 1 << random.nextInt(conds.length);
|
| 140 |
|
|
if(test_type >= 3) cond = 0;
|
| 141 |
|
|
|
| 142 |
|
|
int new_cs_rpl = 0;
|
| 143 |
|
|
int old_cs_rpl = 0;
|
| 144 |
|
|
boolean new_cs_seg = false;
|
| 145 |
|
|
int new_cs_type = 0;
|
| 146 |
|
|
int new_cs_dpl = 0;
|
| 147 |
|
|
boolean new_cs_p = false;
|
| 148 |
|
|
|
| 149 |
|
|
do {
|
| 150 |
|
|
new_cs_rpl = random.nextInt(4);
|
| 151 |
|
|
old_cs_rpl = random.nextInt(4);
|
| 152 |
|
|
new_cs_seg = random.nextBoolean();
|
| 153 |
|
|
new_cs_type = random.nextInt(16);
|
| 154 |
|
|
new_cs_dpl = random.nextInt(4);
|
| 155 |
|
|
new_cs_p = random.nextBoolean();
|
| 156 |
|
|
|
| 157 |
|
|
if(test_type >= 3) old_cs_rpl = new_cs_rpl;
|
| 158 |
|
|
|
| 159 |
|
|
conds[0] = new_cs_rpl < old_cs_rpl;
|
| 160 |
|
|
//check_cs()
|
| 161 |
|
|
conds[1] = new_cs_seg == false;
|
| 162 |
|
|
conds[2] = ((new_cs_type >> 3)&1) == 0; // is data segment
|
| 163 |
|
|
conds[3] = ((new_cs_type >> 3)&1) == 1 && ((new_cs_type >> 2)&1) == 0 && new_cs_dpl != new_cs_rpl; // code non conforming
|
| 164 |
|
|
conds[4] = ((new_cs_type >> 3)&1) == 1 && ((new_cs_type >> 2)&1) == 1 && new_cs_dpl > new_cs_rpl; // code conforming
|
| 165 |
|
|
conds[5] = new_cs_p == false;
|
| 166 |
|
|
}
|
| 167 |
|
|
while(!isAccepted(cond, conds[0],conds[1],conds[2],conds[3],conds[4],conds[5]));
|
| 168 |
|
|
|
| 169 |
|
|
long new_cs_base, new_cs_limit;
|
| 170 |
|
|
boolean new_cs_g;
|
| 171 |
|
|
while(true) {
|
| 172 |
|
|
new_cs_base = Layer.norm(random.nextInt());
|
| 173 |
|
|
new_cs_g = random.nextBoolean();
|
| 174 |
|
|
|
| 175 |
|
|
new_cs_limit = random.nextInt(new_cs_g? 0xF : 0xFFFF);
|
| 176 |
|
|
if(new_cs_g) new_cs_limit = (new_cs_limit << 12) | 0xFFF;
|
| 177 |
|
|
|
| 178 |
|
|
if( new_cs_base + new_cs_limit < 4294967296L &&
|
| 179 |
|
|
Layer.collides(prohibited_list, (int)new_cs_base, (int)(new_cs_base + new_cs_limit)) == false
|
| 180 |
|
|
) break;
|
| 181 |
|
|
}
|
| 182 |
|
|
|
| 183 |
|
|
boolean new_cs_d_b = random.nextBoolean();
|
| 184 |
|
|
boolean new_cs_l = random.nextBoolean();
|
| 185 |
|
|
boolean new_cs_avl = random.nextBoolean();
|
| 186 |
|
|
long new_cs_limit_final = new_cs_g? new_cs_limit >> 12 : new_cs_limit;
|
| 187 |
|
|
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);
|
| 188 |
|
|
|
| 189 |
|
|
final int old_cs_rpl_final = old_cs_rpl;
|
| 190 |
|
|
Layer cs_rpl_layer = new Layer() {
|
| 191 |
|
|
long cs_rpl() { return old_cs_rpl_final; }
|
| 192 |
|
|
};
|
| 193 |
|
|
layers.addFirst(cs_rpl_layer);
|
| 194 |
|
|
|
| 195 |
|
|
int index = tables.addDescriptor(is_ldt, cs_desc);
|
| 196 |
|
|
if(index == -1) continue;
|
| 197 |
|
|
|
| 198 |
|
|
index = index << 3;
|
| 199 |
|
|
if(is_ldt) index |= 4;
|
| 200 |
|
|
|
| 201 |
|
|
index |= new_cs_rpl;
|
| 202 |
|
|
|
| 203 |
|
|
cs = index;
|
| 204 |
|
|
|
| 205 |
|
|
layers.addFirst(tables);
|
| 206 |
|
|
|
| 207 |
|
|
if(test_type == 3) {
|
| 208 |
|
|
while(true) {
|
| 209 |
|
|
eip = Layer.norm(random.nextInt());
|
| 210 |
|
|
|
| 211 |
|
|
if(o32 == false) eip &= 0xFFFF;
|
| 212 |
|
|
|
| 213 |
|
|
if(eip > new_cs_limit) break;
|
| 214 |
|
|
}
|
| 215 |
|
|
if(o32 == false) eip |= (random.nextInt() & 0xFFFF0000);
|
| 216 |
|
|
}
|
| 217 |
|
|
else if(test_type == 4) {
|
| 218 |
|
|
while(true) {
|
| 219 |
|
|
eip = Layer.norm(random.nextInt());
|
| 220 |
|
|
|
| 221 |
|
|
if(o32 == false) eip &= 0xFFFF;
|
| 222 |
|
|
|
| 223 |
|
|
if(eip <= new_cs_limit) break;
|
| 224 |
|
|
}
|
| 225 |
|
|
|
| 226 |
|
|
long dest = new_cs_base + eip;
|
| 227 |
|
|
// adding always possible
|
| 228 |
|
|
MemoryPatchLayer patch = new MemoryPatchLayer(random, prohibited_list, (int)dest, 0x0F,0x0F);
|
| 229 |
|
|
layers.addFirst(patch);
|
| 230 |
|
|
|
| 231 |
|
|
if(o32 == false) eip |= (random.nextInt() & 0xFFFF0000);
|
| 232 |
|
|
System.out.printf("new_cs_base: %08x, new_cs_limit: %08x\n", new_cs_base, new_cs_limit);
|
| 233 |
|
|
}
|
| 234 |
|
|
}
|
| 235 |
|
|
|
| 236 |
|
|
// stack
|
| 237 |
|
|
if(o32) {
|
| 238 |
|
|
stack.push_dword((int)eip); //eip
|
| 239 |
|
|
stack.push_dword((int)cs); //cs
|
| 240 |
|
|
}
|
| 241 |
|
|
else {
|
| 242 |
|
|
stack.push_word((int)eip); //eip
|
| 243 |
|
|
stack.push_word((int)cs); //cs
|
| 244 |
|
|
}
|
| 245 |
|
|
|
| 246 |
|
|
|
| 247 |
|
|
// add instruction
|
| 248 |
|
|
instruction = prepare_instr(cs_d_b, a32, o32);
|
| 249 |
|
|
instr.add_instruction(instruction);
|
| 250 |
|
|
|
| 251 |
|
|
// end condition
|
| 252 |
|
|
if(cr0_pe && flag_vm == false) {
|
| 253 |
|
|
break;
|
| 254 |
|
|
}
|
| 255 |
|
|
}
|
| 256 |
|
|
|
| 257 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
| 258 |
|
|
}
|
| 259 |
|
|
|
| 260 |
|
|
int imm_len(boolean a32, boolean o32, int opcode) {
|
| 261 |
|
|
return opcode == 0xCA ? 2 : 0;
|
| 262 |
|
|
}
|
| 263 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32) throws Exception {
|
| 264 |
|
|
int opcodes[] = {
|
| 265 |
|
|
0xCA,0xCB
|
| 266 |
|
|
};
|
| 267 |
|
|
|
| 268 |
|
|
String prefix = "";
|
| 269 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
| 270 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
| 271 |
|
|
|
| 272 |
|
|
int opcode = opcodes[random.nextInt(opcodes.length)];
|
| 273 |
|
|
boolean is_modregrm = false;
|
| 274 |
|
|
|
| 275 |
|
|
byte possible_modregrm = (byte)random.nextInt();
|
| 276 |
|
|
byte possible_sib = (byte)random.nextInt();
|
| 277 |
|
|
|
| 278 |
|
|
int len = (is_modregrm == false)? 1 : 1 + modregrm_len(!cs_d_b, unsigned(possible_modregrm), unsigned(possible_sib));
|
| 279 |
|
|
len += imm_len(a32, o32, opcode);
|
| 280 |
|
|
|
| 281 |
|
|
|
| 282 |
|
|
byte instr[] = new byte[len];
|
| 283 |
|
|
instr[0] = (byte)opcode;
|
| 284 |
|
|
for(int i=1; i<len; i++) {
|
| 285 |
|
|
if(i==1) instr[1] = possible_modregrm;
|
| 286 |
|
|
else if(i==2) instr[2] = possible_sib;
|
| 287 |
|
|
else instr[i] = (byte)random.nextInt();
|
| 288 |
|
|
}
|
| 289 |
|
|
|
| 290 |
|
|
return prefix + bytesToHex(instr);
|
| 291 |
|
|
}
|
| 292 |
|
|
|
| 293 |
|
|
}
|