| 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 static ao486.test.TestUnit.run_test;
|
| 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.OtherLayer;
|
| 39 |
|
|
import ao486.test.layers.Pair;
|
| 40 |
|
|
import ao486.test.layers.SegmentLayer;
|
| 41 |
|
|
import ao486.test.layers.StackLayer;
|
| 42 |
|
|
import java.io.*;
|
| 43 |
|
|
import java.util.LinkedList;
|
| 44 |
|
|
import java.util.Random;
|
| 45 |
|
|
|
| 46 |
|
|
public class TestAddressingModes extends TestUnit implements Serializable {
|
| 47 |
|
|
public static void main(String args[]) throws Exception {
|
| 48 |
|
|
run_test(TestAddressingModes.class);
|
| 49 |
|
|
}
|
| 50 |
|
|
|
| 51 |
|
|
//--------------------------------------------------------------------------
|
| 52 |
|
|
|
| 53 |
|
|
@Override
|
| 54 |
|
|
public int get_test_count() throws Exception {
|
| 55 |
|
|
//16-bit: 256
|
| 56 |
|
|
//32-bit: 256-8*3 + 8*3*256 = 256 + 8*3*255
|
| 57 |
|
|
|
| 58 |
|
|
return REPEAT * (256 + 256-8*3 + 8*3*256);
|
| 59 |
|
|
}
|
| 60 |
|
|
|
| 61 |
|
|
|
| 62 |
|
|
static final int REPEAT = 1;
|
| 63 |
|
|
|
| 64 |
|
|
static int current_repeat = 0;
|
| 65 |
|
|
static int current_16bit = 0;
|
| 66 |
|
|
static int current_32bit = 0;
|
| 67 |
|
|
static int current_32bit_sib = 0;
|
| 68 |
|
|
|
| 69 |
|
|
static int current_run = 0;
|
| 70 |
|
|
|
| 71 |
|
|
void update() {
|
| 72 |
|
|
if(current_repeat == REPEAT-1) {
|
| 73 |
|
|
current_repeat = 0;
|
| 74 |
|
|
|
| 75 |
|
|
if(current_16bit == 256) {
|
| 76 |
|
|
|
| 77 |
|
|
if(current_32bit == 256-1) { System.out.println("[TestAddressingModes]: tested all."); }
|
| 78 |
|
|
else {
|
| 79 |
|
|
boolean sib = is_sib();
|
| 80 |
|
|
|
| 81 |
|
|
if(sib && current_32bit_sib == 256-1) {
|
| 82 |
|
|
current_32bit_sib = 0;
|
| 83 |
|
|
current_32bit++;
|
| 84 |
|
|
}
|
| 85 |
|
|
else if(sib) {
|
| 86 |
|
|
current_32bit_sib++;
|
| 87 |
|
|
}
|
| 88 |
|
|
else {
|
| 89 |
|
|
current_32bit++;
|
| 90 |
|
|
}
|
| 91 |
|
|
}
|
| 92 |
|
|
}
|
| 93 |
|
|
else current_16bit++;
|
| 94 |
|
|
}
|
| 95 |
|
|
else current_repeat++;
|
| 96 |
|
|
}
|
| 97 |
|
|
|
| 98 |
|
|
boolean is_sib() {
|
| 99 |
|
|
return current_16bit == 256 && ((current_32bit >> 6) & 0x3) != 3 && (current_32bit & 0x7) == 4;
|
| 100 |
|
|
}
|
| 101 |
|
|
|
| 102 |
|
|
int modregrm_len() {
|
| 103 |
|
|
boolean a16 = current_16bit < 256;
|
| 104 |
|
|
int modregrm = (a16)? current_16bit : current_32bit;
|
| 105 |
|
|
int mod = (modregrm >> 6) & 3;
|
| 106 |
|
|
int rm = modregrm & 7;
|
| 107 |
|
|
int base = current_32bit_sib & 7;
|
| 108 |
|
|
|
| 109 |
|
|
// d_CRx_DRx_condition ignored
|
| 110 |
|
|
|
| 111 |
|
|
if(a16 && mod == 0 && rm == 6) return 3;
|
| 112 |
|
|
if(a16 && mod == 1) return 2;
|
| 113 |
|
|
if(a16 && mod == 2) return 3;
|
| 114 |
|
|
if(a16) return 1;
|
| 115 |
|
|
|
| 116 |
|
|
if(mod == 0 && rm == 5) return 5;
|
| 117 |
|
|
if(mod == 0 && rm == 4 && base == 5) return 6;
|
| 118 |
|
|
if(mod == 0 && rm == 4) return 2;
|
| 119 |
|
|
if(mod == 1 && rm == 4) return 3;
|
| 120 |
|
|
if(mod == 1) return 2;
|
| 121 |
|
|
if(mod == 2 && rm == 4) return 6;
|
| 122 |
|
|
if(mod == 2) return 5;
|
| 123 |
|
|
|
| 124 |
|
|
return 1;
|
| 125 |
|
|
}
|
| 126 |
|
|
|
| 127 |
|
|
@Override
|
| 128 |
|
|
public void init() throws Exception {
|
| 129 |
|
|
|
| 130 |
|
|
random = new Random(10 + index);
|
| 131 |
|
|
|
| 132 |
|
|
System.out.println("[current_repeat (" + current_repeat + ") " +
|
| 133 |
|
|
"[current_16bit (" + current_16bit + ") " +
|
| 134 |
|
|
"[current_32bit (" + current_32bit + ") " +
|
| 135 |
|
|
"[current_32bit_sib(" + current_32bit_sib + ")]");
|
| 136 |
|
|
|
| 137 |
|
|
String instruction;
|
| 138 |
|
|
layers.clear();
|
| 139 |
|
|
|
| 140 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
| 141 |
|
|
|
| 142 |
|
|
// if false: v8086 mode
|
| 143 |
|
|
boolean is_real = true;
|
| 144 |
|
|
|
| 145 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list, true);
|
| 146 |
|
|
layers.add(instr);
|
| 147 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
| 148 |
|
|
layers.add(stack);
|
| 149 |
|
|
layers.add(new OtherLayer(is_real ? OtherLayer.Type.REAL : OtherLayer.Type.PROTECTED_OR_V8086, random));
|
| 150 |
|
|
layers.add(new FlagsLayer(is_real ? FlagsLayer.Type.RANDOM : FlagsLayer.Type.V8086, random));
|
| 151 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
| 152 |
|
|
layers.add(new SegmentLayer(random));
|
| 153 |
|
|
layers.add(new MemoryLayer(random));
|
| 154 |
|
|
layers.add(new IOLayer(random));
|
| 155 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
| 156 |
|
|
getInput("cr0_pe"),
|
| 157 |
|
|
getInput("vmflag"),
|
| 158 |
|
|
getInput("cs_rpl"),
|
| 159 |
|
|
getInput("cs_p"),
|
| 160 |
|
|
getInput("cs_s"),
|
| 161 |
|
|
getInput("cs_type")
|
| 162 |
|
|
));
|
| 163 |
|
|
|
| 164 |
|
|
int d_b = 0;
|
| 165 |
|
|
if(current_16bit == 256) d_b = 1;
|
| 166 |
|
|
final int d_b_final = d_b;
|
| 167 |
|
|
|
| 168 |
|
|
Layer layer = new Layer() {
|
| 169 |
|
|
long cs_d_b() { return d_b_final; }
|
| 170 |
|
|
};
|
| 171 |
|
|
layers.addFirst(layer);
|
| 172 |
|
|
|
| 173 |
|
|
byte bytes[] = hexToBytes("000000000000000000000000000000");
|
| 174 |
|
|
|
| 175 |
|
|
if(current_16bit < 256) bytes[1] = (byte)current_16bit;
|
| 176 |
|
|
else bytes[1] = (byte)current_32bit;
|
| 177 |
|
|
|
| 178 |
|
|
if(is_sib()) {
|
| 179 |
|
|
bytes[2] = (byte)current_32bit_sib;
|
| 180 |
|
|
for(int i=0; i<modregrm_len()-2; i++) bytes[3+i] = (byte)random.nextInt();
|
| 181 |
|
|
}
|
| 182 |
|
|
else {
|
| 183 |
|
|
for(int i=0; i<modregrm_len()-1; i++) bytes[2+i] = (byte)random.nextInt();
|
| 184 |
|
|
}
|
| 185 |
|
|
|
| 186 |
|
|
int final_size = modregrm_len()+1;
|
| 187 |
|
|
byte final_bytes[] = new byte[final_size];
|
| 188 |
|
|
System.arraycopy(bytes, 0, final_bytes, 0, final_size);
|
| 189 |
|
|
|
| 190 |
|
|
instruction = bytesToHex(final_bytes) + "0F0F0F0F0F0F0F";
|
| 191 |
|
|
|
| 192 |
|
|
instr.add_instruction(instruction);
|
| 193 |
|
|
|
| 194 |
|
|
System.out.println("[get_instructions: " + instruction + "]");
|
| 195 |
|
|
|
| 196 |
|
|
//---------------------------------------------------------------------- update
|
| 197 |
|
|
current_run++;
|
| 198 |
|
|
if(current_run == 2) {
|
| 199 |
|
|
update();
|
| 200 |
|
|
current_run = 0;
|
| 201 |
|
|
}
|
| 202 |
|
|
}
|
| 203 |
|
|
}
|