| 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.other;
|
| 28 |
|
|
|
| 29 |
|
|
import ao486.test.TestUnit;
|
| 30 |
|
|
import ao486.test.layers.FlagsLayer;
|
| 31 |
|
|
import ao486.test.layers.GeneralRegisterLayer;
|
| 32 |
|
|
import ao486.test.layers.HandleModeChangeLayer;
|
| 33 |
|
|
import ao486.test.layers.IOLayer;
|
| 34 |
|
|
import ao486.test.layers.InstructionLayer;
|
| 35 |
|
|
import ao486.test.layers.Layer;
|
| 36 |
|
|
import ao486.test.layers.MemoryLayer;
|
| 37 |
|
|
import ao486.test.layers.OtherLayer;
|
| 38 |
|
|
import ao486.test.layers.Pair;
|
| 39 |
|
|
import ao486.test.layers.SegmentLayer;
|
| 40 |
|
|
import ao486.test.layers.StackLayer;
|
| 41 |
|
|
import java.io.*;
|
| 42 |
|
|
import java.util.LinkedList;
|
| 43 |
|
|
import java.util.Random;
|
| 44 |
|
|
|
| 45 |
|
|
|
| 46 |
|
|
public class TestMOV_CRx_store extends TestUnit implements Serializable {
|
| 47 |
|
|
public static void main(String args[]) throws Exception {
|
| 48 |
|
|
run_test(TestMOV_CRx_store.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(5 + index);
|
| 61 |
|
|
|
| 62 |
|
|
String instruction;
|
| 63 |
|
|
while(true) {
|
| 64 |
|
|
layers.clear();
|
| 65 |
|
|
|
| 66 |
|
|
/* 0 - CPL != 0
|
| 67 |
|
|
* 1 - all ok
|
| 68 |
|
|
*/
|
| 69 |
|
|
int type = random.nextInt(2);
|
| 70 |
|
|
|
| 71 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
| 72 |
|
|
|
| 73 |
|
|
// if false: v8086 mode
|
| 74 |
|
|
boolean is_real = (type == 0)? false : random.nextBoolean();
|
| 75 |
|
|
|
| 76 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list);
|
| 77 |
|
|
layers.add(instr);
|
| 78 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
| 79 |
|
|
layers.add(stack);
|
| 80 |
|
|
layers.add(new OtherLayer(is_real ? OtherLayer.Type.REAL : OtherLayer.Type.PROTECTED_OR_V8086, random));
|
| 81 |
|
|
layers.add(new FlagsLayer(FlagsLayer.Type.RANDOM, random));
|
| 82 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
| 83 |
|
|
layers.add(new SegmentLayer(random));
|
| 84 |
|
|
layers.add(new MemoryLayer(random));
|
| 85 |
|
|
layers.add(new IOLayer(random));
|
| 86 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
| 87 |
|
|
getInput("cr0_pe"),
|
| 88 |
|
|
getInput("vmflag"),
|
| 89 |
|
|
getInput("cs_rpl"),
|
| 90 |
|
|
getInput("cs_p"),
|
| 91 |
|
|
getInput("cs_s"),
|
| 92 |
|
|
getInput("cs_type")
|
| 93 |
|
|
));
|
| 94 |
|
|
|
| 95 |
|
|
// instruction size
|
| 96 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
| 97 |
|
|
boolean vmflag = getInput("vmflag") == 1;
|
| 98 |
|
|
|
| 99 |
|
|
boolean a32 = random.nextBoolean();
|
| 100 |
|
|
boolean o32 = random.nextBoolean();
|
| 101 |
|
|
|
| 102 |
|
|
if(type == 0) {
|
| 103 |
|
|
final int cs_rpl = (vmflag)? 3 : 1 + random.nextInt(3);
|
| 104 |
|
|
Layer cs_rpl_layer = new Layer() {
|
| 105 |
|
|
long cs_rpl() { return cs_rpl; }
|
| 106 |
|
|
};
|
| 107 |
|
|
layers.addFirst(cs_rpl_layer);
|
| 108 |
|
|
}
|
| 109 |
|
|
|
| 110 |
|
|
// random CR0 bits
|
| 111 |
|
|
//cr0_pe set above
|
| 112 |
|
|
final boolean cr0_mp = random.nextBoolean();
|
| 113 |
|
|
final boolean cr0_em = random.nextBoolean();
|
| 114 |
|
|
final boolean cr0_ts = random.nextBoolean();
|
| 115 |
|
|
final boolean cr0_ne = random.nextBoolean();
|
| 116 |
|
|
final boolean cr0_wp = random.nextBoolean();
|
| 117 |
|
|
final boolean cr0_am = random.nextBoolean();
|
| 118 |
|
|
final boolean cr0_nw = random.nextBoolean();
|
| 119 |
|
|
final boolean cr0_cd = random.nextBoolean();
|
| 120 |
|
|
|
| 121 |
|
|
final int cr2 = random.nextInt();
|
| 122 |
|
|
final int cr3 = random.nextInt();
|
| 123 |
|
|
|
| 124 |
|
|
Layer cr0_2_3_layer = new Layer() {
|
| 125 |
|
|
long cr0_mp() { return cr0_mp? 1:0; }
|
| 126 |
|
|
long cr0_em() { return cr0_em? 1:0; }
|
| 127 |
|
|
long cr0_ts() { return cr0_ts? 1:0; }
|
| 128 |
|
|
long cr0_ne() { return cr0_ne? 1:0; }
|
| 129 |
|
|
long cr0_wp() { return cr0_wp? 1:0; }
|
| 130 |
|
|
long cr0_am() { return cr0_am? 1:0; }
|
| 131 |
|
|
long cr0_nw() { return cr0_nw? 1:0; }
|
| 132 |
|
|
long cr0_cd() { return cr0_cd? 1:0; }
|
| 133 |
|
|
long cr0_pg() { return 0; }
|
| 134 |
|
|
|
| 135 |
|
|
long cr2() { return cr2; }
|
| 136 |
|
|
long cr3() { return cr3; }
|
| 137 |
|
|
};
|
| 138 |
|
|
layers.addFirst(cr0_2_3_layer);
|
| 139 |
|
|
|
| 140 |
|
|
int idx = random.nextInt(3);
|
| 141 |
|
|
int cr_reg = (random.nextBoolean())? ( (idx == 0)? 0 : (idx == 1)? 2 : 3 ) : random.nextInt(8);
|
| 142 |
|
|
|
| 143 |
|
|
int cr_mod = random.nextInt(4);
|
| 144 |
|
|
int cr_rm = random.nextInt(8);
|
| 145 |
|
|
|
| 146 |
|
|
// instruction
|
| 147 |
|
|
byte modregrm_byte = (byte)((cr_mod << 6) | (cr_reg << 3) | (cr_rm));
|
| 148 |
|
|
|
| 149 |
|
|
instruction = prepare_instr(cs_d_b, a32, o32, modregrm_byte);
|
| 150 |
|
|
|
| 151 |
|
|
instruction += instruction;
|
| 152 |
|
|
instruction += "0F0F";
|
| 153 |
|
|
|
| 154 |
|
|
// add instruction
|
| 155 |
|
|
instr.add_instruction(instruction);
|
| 156 |
|
|
|
| 157 |
|
|
// end condition
|
| 158 |
|
|
break;
|
| 159 |
|
|
}
|
| 160 |
|
|
|
| 161 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
| 162 |
|
|
}
|
| 163 |
|
|
|
| 164 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte modregrm_byte) throws Exception {
|
| 165 |
|
|
int opcodes[] = {
|
| 166 |
|
|
0x20
|
| 167 |
|
|
};
|
| 168 |
|
|
|
| 169 |
|
|
String prefix = "";
|
| 170 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
| 171 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
| 172 |
|
|
|
| 173 |
|
|
prefix += "0F";
|
| 174 |
|
|
int opcode = opcodes[random.nextInt(opcodes.length)];
|
| 175 |
|
|
|
| 176 |
|
|
byte instr[] = new byte[1 + 1];
|
| 177 |
|
|
instr[0] = (byte)opcode;
|
| 178 |
|
|
instr[1] = modregrm_byte;
|
| 179 |
|
|
|
| 180 |
|
|
return prefix + bytesToHex(instr);
|
| 181 |
|
|
}
|
| 182 |
|
|
}
|