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.EffectiveAddressLayerFactory;
|
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 |
|
|
|
47 |
|
|
public class TestSMSW extends TestUnit implements Serializable {
|
48 |
|
|
public static void main(String args[]) throws Exception {
|
49 |
|
|
run_test(TestSMSW.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(1 + index);
|
62 |
|
|
|
63 |
|
|
String instruction;
|
64 |
|
|
while(true) {
|
65 |
|
|
layers.clear();
|
66 |
|
|
|
67 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
68 |
|
|
|
69 |
|
|
// if false: v8086 mode
|
70 |
|
|
boolean is_real = random.nextBoolean();
|
71 |
|
|
|
72 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list);
|
73 |
|
|
layers.add(instr);
|
74 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
75 |
|
|
layers.add(stack);
|
76 |
|
|
layers.add(new OtherLayer(is_real ? OtherLayer.Type.REAL : OtherLayer.Type.PROTECTED_OR_V8086, random));
|
77 |
|
|
layers.add(new FlagsLayer(is_real ? FlagsLayer.Type.RANDOM : FlagsLayer.Type.V8086, random));
|
78 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
79 |
|
|
layers.add(new SegmentLayer(random));
|
80 |
|
|
layers.add(new MemoryLayer(random));
|
81 |
|
|
layers.add(new IOLayer(random));
|
82 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
83 |
|
|
getInput("cr0_pe"),
|
84 |
|
|
getInput("vmflag"),
|
85 |
|
|
getInput("cs_rpl"),
|
86 |
|
|
getInput("cs_p"),
|
87 |
|
|
getInput("cs_s"),
|
88 |
|
|
getInput("cs_type")
|
89 |
|
|
));
|
90 |
|
|
|
91 |
|
|
// instruction size
|
92 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
93 |
|
|
|
94 |
|
|
boolean a32 = random.nextBoolean();
|
95 |
|
|
boolean o32 = random.nextBoolean();
|
96 |
|
|
|
97 |
|
|
// random CR0 bits
|
98 |
|
|
//cr0_pe set above
|
99 |
|
|
final boolean cr0_mp = random.nextBoolean();
|
100 |
|
|
final boolean cr0_em = random.nextBoolean();
|
101 |
|
|
final boolean cr0_ts = random.nextBoolean();
|
102 |
|
|
final boolean cr0_ne = random.nextBoolean();
|
103 |
|
|
|
104 |
|
|
Layer cr0_layer = new Layer() {
|
105 |
|
|
long cr0_mp() { return cr0_mp? 1:0; }
|
106 |
|
|
long cr0_em() { return cr0_em? 1:0; }
|
107 |
|
|
long cr0_ts() { return cr0_ts? 1:0; }
|
108 |
|
|
long cr0_ne() { return cr0_ne? 1:0; }
|
109 |
|
|
long cr0_wp() { return 0; }
|
110 |
|
|
long cr0_am() { return 0; }
|
111 |
|
|
long cr0_nw() { return 1; }
|
112 |
|
|
long cr0_cd() { return 1; }
|
113 |
|
|
long cr0_pg() { return 0; }
|
114 |
|
|
};
|
115 |
|
|
layers.addFirst(cr0_layer);
|
116 |
|
|
|
117 |
|
|
// instruction
|
118 |
|
|
|
119 |
|
|
byte modregrm_bytes[] = EffectiveAddressLayerFactory.prepare(
|
120 |
|
|
0,
|
121 |
|
|
4, EffectiveAddressLayerFactory.modregrm_reg_t.SET,
|
122 |
|
|
o32? 4 : 2, a32,
|
123 |
|
|
layers, random, this, false, false);
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
instruction = prepare_instr(cs_d_b, a32, o32, modregrm_bytes);
|
127 |
|
|
|
128 |
|
|
instruction += instruction;
|
129 |
|
|
instruction += "0F0F";
|
130 |
|
|
|
131 |
|
|
// add instruction
|
132 |
|
|
instr.add_instruction(instruction);
|
133 |
|
|
|
134 |
|
|
// end condition
|
135 |
|
|
break;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte modregrm_bytes[]) throws Exception {
|
142 |
|
|
int opcodes[] = {
|
143 |
|
|
0x01
|
144 |
|
|
};
|
145 |
|
|
|
146 |
|
|
String prefix = "";
|
147 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
148 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
149 |
|
|
|
150 |
|
|
prefix += "0F";
|
151 |
|
|
int opcode = opcodes[random.nextInt(opcodes.length)];
|
152 |
|
|
|
153 |
|
|
byte instr[] = new byte[1 + modregrm_bytes.length];
|
154 |
|
|
instr[0] = (byte)opcode;
|
155 |
|
|
System.arraycopy(modregrm_bytes, 0, instr, 1, modregrm_bytes.length);
|
156 |
|
|
|
157 |
|
|
return prefix + bytesToHex(instr);
|
158 |
|
|
}
|
159 |
|
|
}
|