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.layers;
|
28 |
|
|
|
29 |
|
|
import ao486.test.TestUnit;
|
30 |
|
|
import java.util.HashMap;
|
31 |
|
|
import java.util.LinkedList;
|
32 |
|
|
import java.util.Random;
|
33 |
|
|
|
34 |
|
|
/**
|
35 |
|
|
* base ------ eip(offset) ------- base + limit
|
36 |
|
|
*
|
37 |
|
|
*/
|
38 |
|
|
public class InstructionLayer extends Layer {
|
39 |
|
|
public InstructionLayer(Random random, LinkedList<Pair<Long, Long>> prohibited_list, boolean is_real) {
|
40 |
|
|
this.random = random;
|
41 |
|
|
|
42 |
|
|
while(true) {
|
43 |
|
|
cs_base = norm(is_real? random.nextInt(0xFFFF+1) : random.nextInt());
|
44 |
|
|
if(is_real) cs_base &= 0xFFFFFFF0;
|
45 |
|
|
|
46 |
|
|
cs_limit = random.nextInt(is_real? 0xFFFF + 1 : 0xFFFFF + 1);
|
47 |
|
|
|
48 |
|
|
if( cs_base + cs_limit < 4294967296L &&
|
49 |
|
|
collides(prohibited_list, (int)cs_base, (int)(cs_base + cs_limit)) == false
|
50 |
|
|
) break;
|
51 |
|
|
}
|
52 |
|
|
if(is_real) { cs_selector = (cs_base >> 4); }
|
53 |
|
|
|
54 |
|
|
eip = random.nextInt((int)cs_limit + 1);
|
55 |
|
|
cs_index = cs_base + eip;
|
56 |
|
|
|
57 |
|
|
prohibited_list.add(new Pair<>(cs_base, cs_base+cs_limit));
|
58 |
|
|
}
|
59 |
|
|
public InstructionLayer(Random random, LinkedList<Pair<Long, Long>> prohibited_list) {
|
60 |
|
|
this.random = random;
|
61 |
|
|
|
62 |
|
|
while(true) {
|
63 |
|
|
cs_base = norm(random.nextInt());
|
64 |
|
|
|
65 |
|
|
cs_limit = random.nextInt(0xFFFFF + 1);
|
66 |
|
|
|
67 |
|
|
if( cs_base + cs_limit < 4294967296L &&
|
68 |
|
|
collides(prohibited_list, (int)cs_base, (int)(cs_base + cs_limit)) == false
|
69 |
|
|
) break;
|
70 |
|
|
}
|
71 |
|
|
eip = random.nextInt((int)cs_limit + 1);
|
72 |
|
|
cs_index = cs_base + eip;
|
73 |
|
|
|
74 |
|
|
prohibited_list.add(new Pair<>(cs_base, cs_base+cs_limit));
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
public void add_instruction(String instruction) throws Exception {
|
78 |
|
|
byte bytes[] = TestUnit.hexToBytes(instruction);
|
79 |
|
|
|
80 |
|
|
for(byte b : bytes) {
|
81 |
|
|
if(cs_index <= cs_base + cs_limit) {
|
82 |
|
|
cs_map.put(cs_index, b);
|
83 |
|
|
//System.out.printf("INSTR: %08x <- %x\n", cs_index, b);
|
84 |
|
|
cs_index++;
|
85 |
|
|
}
|
86 |
|
|
}
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
//-----------
|
90 |
|
|
public long cs() {
|
91 |
|
|
return cs_selector;
|
92 |
|
|
}
|
93 |
|
|
public long eip() {
|
94 |
|
|
return eip;
|
95 |
|
|
}
|
96 |
|
|
public long cs_base() {
|
97 |
|
|
return cs_base;
|
98 |
|
|
}
|
99 |
|
|
public long cs_limit() {
|
100 |
|
|
return cs_limit;
|
101 |
|
|
}
|
102 |
|
|
public boolean is_memory_not_random(long address) { return cs_map.containsKey(address); }
|
103 |
|
|
|
104 |
|
|
public Byte get_memory(long address) {
|
105 |
|
|
|
106 |
|
|
//System.out.printf("instr get_memory: %08x, in map: %b\n", address, cs_map.containsKey(address));
|
107 |
|
|
if(address < cs_base || address > cs_base + cs_limit) return null;
|
108 |
|
|
|
109 |
|
|
if(cs_map.containsKey(address)) return cs_map.get(address);
|
110 |
|
|
|
111 |
|
|
return (byte)random.nextInt();
|
112 |
|
|
}
|
113 |
|
|
HashMap<Long, Byte> cs_map = new HashMap<>();
|
114 |
|
|
long cs_selector;
|
115 |
|
|
|
116 |
|
|
long cs_base, cs_limit, cs_index;
|
117 |
|
|
long eip;
|
118 |
|
|
}
|