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.arithmetic_logic;
|
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.MemoryLayer;
|
36 |
|
|
import ao486.test.layers.OtherLayer;
|
37 |
|
|
import ao486.test.layers.Pair;
|
38 |
|
|
import ao486.test.layers.SegmentLayer;
|
39 |
|
|
import ao486.test.layers.StackLayer;
|
40 |
|
|
import java.io.*;
|
41 |
|
|
import java.util.Arrays;
|
42 |
|
|
import java.util.LinkedList;
|
43 |
|
|
import java.util.Random;
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
public class TestArithLogic extends TestUnit implements Serializable {
|
47 |
|
|
public static void main(String args[]) throws Exception {
|
48 |
|
|
run_test(TestArithLogic.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(2+index);
|
61 |
|
|
|
62 |
|
|
String instruction;
|
63 |
|
|
while(true) {
|
64 |
|
|
layers.clear();
|
65 |
|
|
|
66 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
67 |
|
|
|
68 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list);
|
69 |
|
|
layers.add(instr);
|
70 |
|
|
layers.add(new StackLayer(random, prohibited_list));
|
71 |
|
|
layers.add(new OtherLayer(OtherLayer.Type.RANDOM, random));
|
72 |
|
|
layers.add(new FlagsLayer(FlagsLayer.Type.RANDOM, random));
|
73 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
74 |
|
|
layers.add(new SegmentLayer(random));
|
75 |
|
|
layers.add(new MemoryLayer(random));
|
76 |
|
|
layers.add(new IOLayer(random));
|
77 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
78 |
|
|
getInput("cr0_pe"),
|
79 |
|
|
getInput("vmflag"),
|
80 |
|
|
getInput("cs_rpl"),
|
81 |
|
|
getInput("cs_p"),
|
82 |
|
|
getInput("cs_s"),
|
83 |
|
|
getInput("cs_type")
|
84 |
|
|
));
|
85 |
|
|
|
86 |
|
|
// instruction size
|
87 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
88 |
|
|
|
89 |
|
|
boolean a32 = random.nextBoolean();
|
90 |
|
|
boolean o32 = random.nextBoolean();
|
91 |
|
|
|
92 |
|
|
// instruction
|
93 |
|
|
/*
|
94 |
|
|
byte modregrm_bytes[] = EffectiveAddressLayerFactory.prepare(
|
95 |
|
|
0,
|
96 |
|
|
4, EffectiveAddressLayerFactory.modregrm_reg_t.SET,
|
97 |
|
|
o32? 4 : 2, a32,
|
98 |
|
|
layers, random, this, false, false);
|
99 |
|
|
*/
|
100 |
|
|
|
101 |
|
|
instruction = prepare_instr(cs_d_b, a32, o32, null);
|
102 |
|
|
|
103 |
|
|
instruction += "0F0F";
|
104 |
|
|
|
105 |
|
|
// add instruction
|
106 |
|
|
instr.add_instruction(instruction);
|
107 |
|
|
|
108 |
|
|
// end condition
|
109 |
|
|
break;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
int imm_len(boolean o16, int opcode) {
|
116 |
|
|
int l = opcode & 0x0F;
|
117 |
|
|
int h = (opcode >> 4) & 0x0F;
|
118 |
|
|
|
119 |
|
|
if((h >= 0 && h <= 3) && (l == 0x4 || l == 0xC)) return 1;
|
120 |
|
|
if((h >= 0 && h <= 3) && (l == 0x5 || l == 0xD)) return (o16)? 2 : 4;
|
121 |
|
|
|
122 |
|
|
if(h == 8 && l == 0) return 1;
|
123 |
|
|
if(h == 8 && l == 1) return (o16)? 2 : 4;
|
124 |
|
|
if(h == 8 && l == 3) return 1;
|
125 |
|
|
|
126 |
|
|
return 0;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte modregrm_bytes[]) throws Exception {
|
130 |
|
|
int opcodes[] = {
|
131 |
|
|
0x00,0x01,0x02,0x03,0x04,0x05, 0x08,0x09,0x0A,0x0B,0x0C,0x0D,
|
132 |
|
|
0x10,0x11,0x12,0x13,0x14,0x15, 0x18,0x19,0x1A,0x1B,0x1C,0x1D,
|
133 |
|
|
0x20,0x21,0x22,0x23,0x24,0x25, 0x28,0x29,0x2A,0x2B,0x2C,0x2D,
|
134 |
|
|
0x30,0x31,0x32,0x33,0x34,0x35, 0x38,0x39,0x3A,0x3B,0x3C,0x3D,
|
135 |
|
|
0x80,0x81,0x82,0x83
|
136 |
|
|
};
|
137 |
|
|
int not_modregrm[] = {
|
138 |
|
|
0x04,0x05, 0x0C,0x0D,
|
139 |
|
|
0x14,0x15, 0x1C,0x1D,
|
140 |
|
|
0x24,0x25, 0x2C,0x2D,
|
141 |
|
|
0x34,0x35, 0x3C,0x3D,
|
142 |
|
|
};
|
143 |
|
|
|
144 |
|
|
String prefix = "";
|
145 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
146 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
147 |
|
|
|
148 |
|
|
int opcode = opcodes[random.nextInt(opcodes.length)];
|
149 |
|
|
|
150 |
|
|
boolean is_modregrm = Arrays.binarySearch(not_modregrm, opcode) < 0;
|
151 |
|
|
|
152 |
|
|
byte possible_modregrm = (byte)random.nextInt();
|
153 |
|
|
byte possible_sib = (byte)random.nextInt();
|
154 |
|
|
|
155 |
|
|
int len = (is_modregrm == false)? 1 : 1 + modregrm_len(!a32, unsigned(possible_modregrm), unsigned(possible_sib));
|
156 |
|
|
len += imm_len(!o32, opcode);
|
157 |
|
|
System.out.println("instr len: " + len + ", imm_len: " + imm_len(!o32, opcode));
|
158 |
|
|
|
159 |
|
|
byte instr[] = new byte[len];
|
160 |
|
|
instr[0] = (byte)opcode;
|
161 |
|
|
for(int i=1; i<len; i++) {
|
162 |
|
|
if(i==1) instr[1] = possible_modregrm;
|
163 |
|
|
else if(i==2) instr[2] = possible_sib;
|
164 |
|
|
else instr[i] = (byte)random.nextInt();
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
return prefix + bytesToHex(instr);
|
168 |
|
|
}
|
169 |
|
|
}
|