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;
|
28 |
|
|
|
29 |
|
|
import static ao486.test.TestUnit.run_test;
|
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.FileInputStream;
|
42 |
|
|
import java.io.FileOutputStream;
|
43 |
|
|
import java.io.Serializable;
|
44 |
|
|
import java.nio.ByteBuffer;
|
45 |
|
|
import java.nio.channels.FileChannel;
|
46 |
|
|
import java.util.LinkedList;
|
47 |
|
|
import java.util.Properties;
|
48 |
|
|
import java.util.Random;
|
49 |
|
|
|
50 |
|
|
public class TestFromTrace extends TestUnit implements Serializable {
|
51 |
|
|
public static void main(String args[]) throws Exception {
|
52 |
|
|
Properties props = new Properties();
|
53 |
|
|
props.load(new FileInputStream("run.properties"));
|
54 |
|
|
file_offset = Long.parseLong(props.getProperty("file_offset"));
|
55 |
|
|
|
56 |
|
|
run_test(TestFromTrace.class);
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
//--------------------------------------------------------------------------
|
60 |
|
|
@Override
|
61 |
|
|
public int get_test_count() throws Exception {
|
62 |
|
|
return 100000000;
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
@Override
|
66 |
|
|
public void init() throws Exception {
|
67 |
|
|
|
68 |
|
|
random = new Random(4+index);
|
69 |
|
|
|
70 |
|
|
String instruction;
|
71 |
|
|
while(true) {
|
72 |
|
|
layers.clear();
|
73 |
|
|
|
74 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
75 |
|
|
|
76 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list);
|
77 |
|
|
layers.add(instr);
|
78 |
|
|
layers.add(new StackLayer(random, prohibited_list));
|
79 |
|
|
layers.add(new OtherLayer(OtherLayer.Type.RANDOM, random));
|
80 |
|
|
layers.add(new FlagsLayer(FlagsLayer.Type.RANDOM, random));
|
81 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
82 |
|
|
layers.add(new SegmentLayer(random));
|
83 |
|
|
layers.add(new MemoryLayer(random));
|
84 |
|
|
layers.add(new IOLayer(random));
|
85 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
86 |
|
|
getInput("cr0_pe"),
|
87 |
|
|
getInput("vmflag"),
|
88 |
|
|
getInput("cs_rpl"),
|
89 |
|
|
getInput("cs_p"),
|
90 |
|
|
getInput("cs_s"),
|
91 |
|
|
getInput("cs_type")
|
92 |
|
|
));
|
93 |
|
|
|
94 |
|
|
Layer debug_layer = new Layer() {
|
95 |
|
|
long get_test_type() { return 4; }
|
96 |
|
|
};
|
97 |
|
|
layers.addFirst(debug_layer);
|
98 |
|
|
|
99 |
|
|
// instruction size
|
100 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
101 |
|
|
|
102 |
|
|
boolean a32 = random.nextBoolean();
|
103 |
|
|
boolean o32 = random.nextBoolean();
|
104 |
|
|
|
105 |
|
|
// instruction
|
106 |
|
|
instruction = prepare_instr(cs_d_b, a32, o32, null);
|
107 |
|
|
|
108 |
|
|
instruction += "0F0F";
|
109 |
|
|
|
110 |
|
|
// add instruction
|
111 |
|
|
instr.add_instruction(instruction);
|
112 |
|
|
|
113 |
|
|
// end condition
|
114 |
|
|
break;
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte modregrm_bytes[]) throws Exception {
|
121 |
|
|
|
122 |
|
|
String prefix = "";
|
123 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
124 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
125 |
|
|
|
126 |
|
|
FileInputStream fis = new FileInputStream("/home/alek/temp/bochs-run/dbg_win311_boot.out");
|
127 |
|
|
FileChannel ch = fis.getChannel();
|
128 |
|
|
|
129 |
|
|
ch.position(file_offset);
|
130 |
|
|
|
131 |
|
|
ByteBuffer bb = ByteBuffer.allocate(8192);
|
132 |
|
|
ch.read(bb);
|
133 |
|
|
String str = new String(bb.array());
|
134 |
|
|
|
135 |
|
|
int index1 = str.indexOf("; ");
|
136 |
|
|
int index2 = str.indexOf("\n", index1);
|
137 |
|
|
int index3 = str.indexOf("; ", index2);
|
138 |
|
|
int index4 = str.indexOf("\n", index3);
|
139 |
|
|
|
140 |
|
|
if(index1 == -1 || index2 == -1 || index3 == -1 || index4 == -1) throw new Exception("index == -1: " + index1 + ", " + index2 + ", " + index3 + ", " + index4);
|
141 |
|
|
|
142 |
|
|
String s1 = str.substring(index1+1, index2);
|
143 |
|
|
String s2 = str.substring(index3+1, index4);
|
144 |
|
|
|
145 |
|
|
s1 = s1.trim();
|
146 |
|
|
s2 = s2.trim();
|
147 |
|
|
|
148 |
|
|
System.out.println("file_offset: " + file_offset + ", file_number: " + file_number);
|
149 |
|
|
if((file_number & 1L) == 1) file_offset += index2;
|
150 |
|
|
file_number++;
|
151 |
|
|
if(file_number==2) {
|
152 |
|
|
file_number = 0;
|
153 |
|
|
|
154 |
|
|
//Properties props = new Properties();
|
155 |
|
|
//props.load(new FileInputStream("run.properties"));
|
156 |
|
|
//props.setProperty("file_offset", "" + file_offset);
|
157 |
|
|
//props.store(new FileOutputStream("run.properties"), null);
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
return prefix + s1 + s2 + "909090909090909090909090909090909090909090909090909090909090";
|
161 |
|
|
}
|
162 |
|
|
static long file_number = 0;
|
163 |
|
|
static long file_offset = 301186382;
|
164 |
|
|
}
|