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.transfer_data;
|
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.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.Serializable;
|
42 |
|
|
import java.util.LinkedList;
|
43 |
|
|
import java.util.Random;
|
44 |
|
|
|
45 |
|
|
public class TestMOV_modregrm extends TestUnit implements Serializable {
|
46 |
|
|
public static void main(String args[]) throws Exception {
|
47 |
|
|
run_test(TestMOV_modregrm.class);
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
//--------------------------------------------------------------------------
|
51 |
|
|
@Override
|
52 |
|
|
public int get_test_count() throws Exception {
|
53 |
|
|
return 100;
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
@Override
|
57 |
|
|
public void init() throws Exception {
|
58 |
|
|
random = new Random(5 + index);
|
59 |
|
|
|
60 |
|
|
long rpl = random.nextInt(4);
|
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 |
|
|
rpl, //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 |
|
|
instruction = prepare_instr(cs_d_b, a32, o32, null);
|
94 |
|
|
|
95 |
|
|
instruction += instruction;
|
96 |
|
|
instruction += "0F0F";
|
97 |
|
|
|
98 |
|
|
// add instruction
|
99 |
|
|
instr.add_instruction(instruction);
|
100 |
|
|
|
101 |
|
|
// end condition
|
102 |
|
|
break;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte modregrm_bytes[]) throws Exception {
|
109 |
|
|
|
110 |
|
|
int opcodes[] = {
|
111 |
|
|
0x88,0x89,0x8A,0x8B
|
112 |
|
|
};
|
113 |
|
|
|
114 |
|
|
String prefix = "";
|
115 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
116 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
117 |
|
|
|
118 |
|
|
int opcode = opcodes[random.nextInt(opcodes.length)];
|
119 |
|
|
boolean is_modregrm = true;
|
120 |
|
|
|
121 |
|
|
byte possible_modregrm = (byte)random.nextInt();
|
122 |
|
|
byte possible_sib = (byte)random.nextInt();
|
123 |
|
|
|
124 |
|
|
int len = (is_modregrm == false)? 1 : 1 + modregrm_len(!a32, unsigned(possible_modregrm), unsigned(possible_sib));
|
125 |
|
|
|
126 |
|
|
byte instr[] = new byte[len];
|
127 |
|
|
instr[0] = (byte)opcode;
|
128 |
|
|
for(int i=1; i<len; i++) {
|
129 |
|
|
if(i==1) instr[1] = possible_modregrm;
|
130 |
|
|
else if(i==2) instr[2] = possible_sib;
|
131 |
|
|
else instr[i] = (byte)random.nextInt();
|
132 |
|
|
}
|
133 |
|
|
return prefix + bytesToHex(instr);
|
134 |
|
|
}
|
135 |
|
|
}
|