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 java.util.HashMap;
|
30 |
|
|
import java.util.LinkedList;
|
31 |
|
|
import java.util.Random;
|
32 |
|
|
|
33 |
|
|
public class StackLayer extends Layer {
|
34 |
|
|
public StackLayer(Random random, LinkedList<Pair<Long, Long>> prohibited_list) {
|
35 |
|
|
this.random = random;
|
36 |
|
|
|
37 |
|
|
ss_d_b = random.nextBoolean();
|
38 |
|
|
|
39 |
|
|
while(true) {
|
40 |
|
|
ss_base = norm(random.nextInt());
|
41 |
|
|
|
42 |
|
|
ss_limit = random.nextInt(ss_d_b? 0xFFFFF + 1 : 0xFFFF + 1);
|
43 |
|
|
|
44 |
|
|
if( ss_base + ss_limit < 4294967296L &&
|
45 |
|
|
collides(prohibited_list, ss_base, ss_base + ss_limit) == false
|
46 |
|
|
) break;
|
47 |
|
|
}
|
48 |
|
|
esp = random.nextInt((int)ss_limit + 1);
|
49 |
|
|
ss_index = ss_base + esp;
|
50 |
|
|
|
51 |
|
|
if(ss_d_b == false) {
|
52 |
|
|
esp |= random.nextInt() & 0xFFFF0000;
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
prohibited_list.add(new Pair<>(ss_base, ss_base+ss_limit));
|
56 |
|
|
}
|
57 |
|
|
public void push_byte(int value) {
|
58 |
|
|
//System.out.printf("push_byte: %08x :: %08x\n", ss_index, value);
|
59 |
|
|
if(ss_index > ss_base + ss_limit) return;
|
60 |
|
|
ss_map.put(ss_index, (byte)(value & 0xFF));
|
61 |
|
|
ss_index++;
|
62 |
|
|
}
|
63 |
|
|
public void push_word(int value) {
|
64 |
|
|
//System.out.printf("push_word: %08x :: %08x\n", ss_index, value);
|
65 |
|
|
if(ss_index > ss_base + ss_limit) return;
|
66 |
|
|
ss_map.put(ss_index, (byte)(value & 0xFF));
|
67 |
|
|
ss_index++;
|
68 |
|
|
|
69 |
|
|
if(ss_index > ss_base + ss_limit) return;
|
70 |
|
|
ss_map.put(ss_index, (byte)((value >> 8) & 0xFF));
|
71 |
|
|
ss_index++;
|
72 |
|
|
}
|
73 |
|
|
public void push_dword(int value) {
|
74 |
|
|
//System.out.printf("push_dword: %08x :: %08x\n", ss_index, value);
|
75 |
|
|
if(ss_index > ss_base + ss_limit) return;
|
76 |
|
|
ss_map.put(ss_index, (byte)(value & 0xFF));
|
77 |
|
|
ss_index++;
|
78 |
|
|
|
79 |
|
|
if(ss_index > ss_base + ss_limit) return;
|
80 |
|
|
ss_map.put(ss_index, (byte)((value >> 8) & 0xFF));
|
81 |
|
|
ss_index++;
|
82 |
|
|
|
83 |
|
|
if(ss_index > ss_base + ss_limit) return;
|
84 |
|
|
ss_map.put(ss_index, (byte)((value >> 16) & 0xFF));
|
85 |
|
|
ss_index++;
|
86 |
|
|
|
87 |
|
|
if(ss_index > ss_base + ss_limit) return;
|
88 |
|
|
ss_map.put(ss_index, (byte)((value >> 24) & 0xFF));
|
89 |
|
|
ss_index++;
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
//-----------
|
93 |
|
|
|
94 |
|
|
public long esp() {
|
95 |
|
|
return esp;
|
96 |
|
|
}
|
97 |
|
|
public long ss_base() {
|
98 |
|
|
return ss_base;
|
99 |
|
|
}
|
100 |
|
|
public long ss_limit() {
|
101 |
|
|
return ss_limit;
|
102 |
|
|
}
|
103 |
|
|
public long ss_d_b() {
|
104 |
|
|
return ss_d_b? 1 : 0;
|
105 |
|
|
}
|
106 |
|
|
public boolean is_memory_not_random(long address) { return ss_map.containsKey(address); }
|
107 |
|
|
|
108 |
|
|
public Byte get_memory(long address) {
|
109 |
|
|
if(address < ss_base || address > ss_base + ss_limit) return null;
|
110 |
|
|
|
111 |
|
|
if(ss_map.containsKey(address)) return ss_map.get(address);
|
112 |
|
|
|
113 |
|
|
return (byte)random.nextInt();
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
HashMap<Long, Byte> ss_map = new HashMap<>();
|
117 |
|
|
boolean ss_d_b;
|
118 |
|
|
long ss_base, ss_limit, ss_index;
|
119 |
|
|
long esp;
|
120 |
|
|
}
|