| 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.io;
|
| 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 TestINS_real_rep extends TestUnit implements Serializable {
|
| 47 |
|
|
public static void main(String args[]) throws Exception {
|
| 48 |
|
|
run_test(TestINS_real_rep.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(6 + index);
|
| 61 |
|
|
|
| 62 |
|
|
String instruction;
|
| 63 |
|
|
while(true) {
|
| 64 |
|
|
layers.clear();
|
| 65 |
|
|
|
| 66 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
| 67 |
|
|
|
| 68 |
|
|
// if false: v8086 mode
|
| 69 |
|
|
boolean is_real = true;
|
| 70 |
|
|
|
| 71 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list, true);
|
| 72 |
|
|
layers.add(instr);
|
| 73 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
| 74 |
|
|
layers.add(stack);
|
| 75 |
|
|
layers.add(new OtherLayer(is_real ? OtherLayer.Type.REAL : OtherLayer.Type.PROTECTED_OR_V8086, random));
|
| 76 |
|
|
layers.add(new FlagsLayer(is_real ? FlagsLayer.Type.RANDOM : FlagsLayer.Type.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 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
| 82 |
|
|
getInput("cr0_pe"),
|
| 83 |
|
|
getInput("vmflag"),
|
| 84 |
|
|
getInput("cs_rpl"),
|
| 85 |
|
|
getInput("cs_p"),
|
| 86 |
|
|
getInput("cs_s"),
|
| 87 |
|
|
getInput("cs_type")
|
| 88 |
|
|
));
|
| 89 |
|
|
|
| 90 |
|
|
// instruction size
|
| 91 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
| 92 |
|
|
|
| 93 |
|
|
boolean a32 = random.nextBoolean();
|
| 94 |
|
|
boolean o32 = random.nextBoolean();
|
| 95 |
|
|
|
| 96 |
|
|
long cs_limit = getInput("cs_limit");
|
| 97 |
|
|
|
| 98 |
|
|
//----------------
|
| 99 |
|
|
|
| 100 |
|
|
final Random final_random = random;
|
| 101 |
|
|
|
| 102 |
|
|
Layer layer = new Layer() {
|
| 103 |
|
|
long ecx() { return final_random.nextInt(5); }
|
| 104 |
|
|
|
| 105 |
|
|
long esi() {
|
| 106 |
|
|
int val = final_random.nextInt();
|
| 107 |
|
|
return ((val % 18) == 0)? 0 :
|
| 108 |
|
|
((val % 18) == 1)? 1 :
|
| 109 |
|
|
((val % 18) == 2)? 2 :
|
| 110 |
|
|
((val % 18) == 3)? 3 :
|
| 111 |
|
|
((val % 18) == 4)? 4 :
|
| 112 |
|
|
((val % 18) == 5)? 0xFFFFFFFF :
|
| 113 |
|
|
((val % 18) == 6)? 0x0000FFFF :
|
| 114 |
|
|
final_random.nextInt() & ((final_random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
|
| 115 |
|
|
}
|
| 116 |
|
|
|
| 117 |
|
|
long edi() {
|
| 118 |
|
|
int val = final_random.nextInt();
|
| 119 |
|
|
return ((val % 18) == 0)? 0 :
|
| 120 |
|
|
((val % 18) == 1)? 1 :
|
| 121 |
|
|
((val % 18) == 2)? 2 :
|
| 122 |
|
|
((val % 18) == 3)? 3 :
|
| 123 |
|
|
((val % 18) == 4)? 4 :
|
| 124 |
|
|
((val % 18) == 5)? 0xFFFFFFFF :
|
| 125 |
|
|
((val % 18) == 6)? 0x0000FFFF :
|
| 126 |
|
|
final_random.nextInt() & ((final_random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
|
| 127 |
|
|
}
|
| 128 |
|
|
};
|
| 129 |
|
|
layers.addFirst(layer);
|
| 130 |
|
|
|
| 131 |
|
|
//-----------------
|
| 132 |
|
|
|
| 133 |
|
|
String instruction_string = prepare_instr(cs_d_b, a32, o32);
|
| 134 |
|
|
|
| 135 |
|
|
// add instruction
|
| 136 |
|
|
instruction = instruction_string + instruction_string + "9090900F0F";
|
| 137 |
|
|
|
| 138 |
|
|
instr.add_instruction(instruction);
|
| 139 |
|
|
|
| 140 |
|
|
// end condition
|
| 141 |
|
|
break;
|
| 142 |
|
|
}
|
| 143 |
|
|
|
| 144 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
| 145 |
|
|
}
|
| 146 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32) throws Exception {
|
| 147 |
|
|
int opcodes[] = {
|
| 148 |
|
|
0x6C,0x6D
|
| 149 |
|
|
};
|
| 150 |
|
|
|
| 151 |
|
|
String prefix = "";
|
| 152 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
| 153 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
| 154 |
|
|
if(random.nextBoolean()) { prefix = "F2" + prefix; }
|
| 155 |
|
|
if(random.nextBoolean()) { prefix = "F3" + prefix; }
|
| 156 |
|
|
|
| 157 |
|
|
int opcode = opcodes[random.nextInt(opcodes.length)];
|
| 158 |
|
|
|
| 159 |
|
|
int len = 1;
|
| 160 |
|
|
|
| 161 |
|
|
byte instr[] = new byte[len];
|
| 162 |
|
|
instr[0] = (byte)opcode;
|
| 163 |
|
|
if(len >= 2) instr[1] = (byte)random.nextInt();
|
| 164 |
|
|
|
| 165 |
|
|
return prefix + bytesToHex(instr);
|
| 166 |
|
|
}
|
| 167 |
|
|
}
|