OpenCores
URL https://opencores.org/ocsvn/ao486/ao486/trunk

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [branch/] [TestJMP_protected_seg.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
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.branch;
28
 
29
import ao486.test.TestUnit;
30
import ao486.test.layers.DescriptorTableLayer;
31
import ao486.test.layers.EffectiveAddressLayerFactory;
32
import ao486.test.layers.FlagsLayer;
33
import ao486.test.layers.GeneralRegisterLayer;
34
import ao486.test.layers.HandleModeChangeLayer;
35
import ao486.test.layers.IOLayer;
36
import ao486.test.layers.InstructionLayer;
37
import ao486.test.layers.Layer;
38
import ao486.test.layers.MemoryLayer;
39
import ao486.test.layers.MemoryPatchLayer;
40
import ao486.test.layers.OtherLayer;
41
import ao486.test.layers.Pair;
42
import ao486.test.layers.SegmentLayer;
43
import ao486.test.layers.StackLayer;
44
import java.io.*;
45
import java.util.LinkedList;
46
import java.util.Random;
47
 
48
 
49
public class TestJMP_protected_seg extends TestUnit implements Serializable {
50
    public static void main(String args[]) throws Exception {
51
        run_test(TestJMP_protected_seg.class);
52
    }
53
 
54
    //--------------------------------------------------------------------------
55
    @Override
56
    public int get_test_count() throws Exception {
57
        return 100;
58
    }
59
 
60
    @Override
61
    public void init() throws Exception {
62
 
63
        random = new Random(84 + index);
64
 
65
        String instruction;
66
        while(true) {
67
            layers.clear();
68
 
69
            LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
70
 
71
            InstructionLayer instr = new InstructionLayer(random, prohibited_list);
72
            layers.add(instr);
73
            StackLayer stack = new StackLayer(random, prohibited_list);
74
            layers.add(stack);
75
            layers.add(new OtherLayer(OtherLayer.Type.PROTECTED_OR_V8086, random));
76
            layers.add(new FlagsLayer(FlagsLayer.Type.NOT_V8086, random));
77
            layers.add(new GeneralRegisterLayer(random));
78
            layers.add(new SegmentLayer(random));
79
            layers.add(new MemoryLayer(random));
80
            layers.add(new IOLayer(random));
81
            layers.addFirst(new HandleModeChangeLayer(
82
                    getInput("cr0_pe"),
83
                    getInput("vmflag"),
84
                    getInput("cs_rpl"),
85
                    getInput("cs_p"),
86
                    getInput("cs_s"),
87
                    getInput("cs_type")
88
            ));
89
 
90
            // instruction size
91
            boolean cs_d_b = getInput("cs_d_b") == 1;
92
 
93
            boolean a32 = random.nextBoolean();
94
            boolean o32 = random.nextBoolean();
95
 
96
 
97
            long new_eip = 0;
98
            long new_cs  = random.nextInt(4);
99
 
100
            /*
101
            * 0. cs_selector null
102
            * 1. cs_descriptor out of bounds
103
            * 2. valid and check_cs
104
            * 3. eip not in limits
105
            * 4. all ok
106
            */
107
 
108
            int test_type = random.nextInt(5);
109
 
110
            if(test_type == 0) {
111
                // nothing
112
            }
113
            else if(test_type == 1) {
114
                boolean ldtr_valid = random.nextInt(5) != 0;
115
 
116
                DescriptorTableLayer tables = new DescriptorTableLayer(random, prohibited_list, ldtr_valid);
117
 
118
                boolean is_ldt = (ldtr_valid == false)? false : random.nextBoolean();
119
 
120
                int index = tables.getOutOfBoundsIndex(is_ldt);
121
                if(index == -1) continue;
122
 
123
                index = index << 3;
124
                if(is_ldt) index |= 4;
125
 
126
                index |= random.nextInt(4);
127
 
128
                new_cs = index;
129
 
130
                layers.addFirst(tables);
131
            }
132
            else if(test_type >= 2) {
133
                DescriptorTableLayer tables = new DescriptorTableLayer(random, prohibited_list, true);
134
 
135
                boolean is_ldt = random.nextBoolean();
136
 
137
                boolean conds[] = new boolean[6];
138
                int cond = 1 << random.nextInt(conds.length);
139
                if(test_type >= 3) cond = 0;
140
 
141
                int     new_cs_rpl  = 0;
142
                int     old_cs_rpl  = 0;
143
                boolean new_cs_seg  = false;
144
                int     new_cs_type = 0;
145
                int     new_cs_dpl  = 0;
146
                boolean new_cs_p    = false;
147
 
148
                do {
149
                    new_cs_rpl  = random.nextInt(4);
150
                    old_cs_rpl  = random.nextInt(4);
151
                    new_cs_seg  = random.nextBoolean();
152
                    new_cs_type = random.nextInt(16);
153
                    new_cs_dpl  = random.nextInt(4);
154
                    new_cs_p    = random.nextBoolean();
155
 
156
                    //not valid
157
                    conds[0] = new_cs_seg == false && (new_cs_type == 0 || new_cs_type == 8 || new_cs_type == 10 || new_cs_type == 13);
158
                    //check_cs()
159
                    //segment always set --- new_cs_seg == false;
160
                    conds[1] = ((new_cs_type >> 3)&1) == 0; // is data segment
161
                    conds[2] = ((new_cs_type >> 3)&1) == 1 && ((new_cs_type >> 2)&1) == 0 && new_cs_dpl != old_cs_rpl; // code non conforming 1
162
                    conds[3] = ((new_cs_type >> 3)&1) == 1 && ((new_cs_type >> 2)&1) == 0 && new_cs_rpl > old_cs_rpl; // code non conforming 2
163
                    conds[4] = ((new_cs_type >> 3)&1) == 1 && ((new_cs_type >> 2)&1) == 1 && new_cs_dpl > old_cs_rpl;  // code conforming
164
                    conds[5] = new_cs_p == false;
165
                }
166
                while(!isAccepted(cond, conds[0],conds[1],conds[2],conds[3],conds[4],conds[5]));
167
 
168
                if(new_cs_seg == false && new_cs_type != 0 && new_cs_type != 8 && new_cs_type != 10 && new_cs_type != 13) new_cs_seg = true;
169
 
170
                long new_cs_base, new_cs_limit;
171
                boolean new_cs_g;
172
                while(true) {
173
                    new_cs_base = Layer.norm(random.nextInt());
174
                    new_cs_g    = random.nextBoolean();
175
 
176
                    new_cs_limit = random.nextInt(new_cs_g? 0xF : 0xFFFF);
177
                    if(new_cs_g) new_cs_limit = (new_cs_limit << 12) | 0xFFF;
178
 
179
                    if( new_cs_base + new_cs_limit < 4294967296L &&
180
                        Layer.collides(prohibited_list, (int)new_cs_base, (int)(new_cs_base + new_cs_limit)) == false
181
                    ) break;
182
                }
183
 
184
                boolean new_cs_d_b = random.nextBoolean();
185
                boolean new_cs_l   = random.nextBoolean();
186
                boolean new_cs_avl = random.nextBoolean();
187
                long new_cs_limit_final = new_cs_g? new_cs_limit >> 12 : new_cs_limit;
188
                Descriptor cs_desc = new Descriptor((int)new_cs_base, (int)new_cs_limit_final, new_cs_type, new_cs_seg, new_cs_p, new_cs_dpl, new_cs_d_b, new_cs_g, new_cs_l, new_cs_avl);
189
 
190
System.out.printf("cs_desc: ");
191
for(int i=0; i<8; i++) System.out.printf("%02x ", cs_desc.get_byte(i));
192
System.out.printf("\n");
193
 
194
                final int old_cs_rpl_final = old_cs_rpl;
195
                Layer cs_rpl_layer = new Layer() {
196
                    long cs_rpl() { return old_cs_rpl_final; }
197
                };
198
                layers.addFirst(cs_rpl_layer);
199
 
200
                int index = tables.addDescriptor(is_ldt, cs_desc);
201
                if(index == -1) continue;
202
 
203
                index = index << 3;
204
                if(is_ldt) index |= 4;
205
 
206
                index |= new_cs_rpl;
207
 
208
                new_cs = index;
209
 
210
                layers.addFirst(tables);
211
 
212
System.out.printf("cond: %d\n", cond);
213
 
214
                if(test_type == 3) {
215
                    while(true) {
216
                        new_eip = new_cs_limit + 1 + random.nextInt(10);
217
 
218
                        if(o32 == false) new_eip &= 0xFFFF;
219
 
220
                        if(new_eip > new_cs_limit) break;
221
                    }
222
                    if(o32 == false) new_eip |= (random.nextInt() & 0xFFFF0000);
223
                }
224
                else if(test_type == 4) {
225
                    while(true) {
226
                        new_eip = Layer.norm(random.nextInt((int)new_cs_limit+1));
227
 
228
                        if(o32 == false) new_eip &= 0xFFFF;
229
 
230
                        if(new_eip <= new_cs_limit) break;
231
                    }
232
 
233
                    long dest = new_cs_base + new_eip;
234
                    // adding always possible
235
                    MemoryPatchLayer patch = new MemoryPatchLayer(random, prohibited_list, (int)dest, 0x0F,0x0F);
236
                    layers.addFirst(patch);
237
 
238
                    if(o32 == false) new_eip |= (random.nextInt() & 0xFFFF0000);
239
System.out.printf("new_cs_base: %08x, new_cs_limit: %08x\n", new_cs_base, new_cs_limit);
240
                }
241
 
242
            }
243
 
244
            // instruction
245
            byte extra_bytes[] = null;
246
 
247
            boolean is_Ep = random.nextBoolean();
248
 
249
            if(is_Ep) {
250
                byte modregrm_bytes[] = EffectiveAddressLayerFactory.prepare(
251
                        o32? (((new_cs & 0xFFFF) << 32) | (new_eip & 0xFFFFFFFF)) : (((new_cs & 0xFFFF) << 16) | (new_eip & 0xFFFF)),
252
                        5, EffectiveAddressLayerFactory.modregrm_reg_t.SET,
253
                        o32? 6 : 4, a32,
254
                        layers, random, this, true, false);
255
                extra_bytes = modregrm_bytes;
256
            }
257
            else {
258
                long immediate = o32? (((new_cs & 0xFFFF) << 32) | (new_eip & 0xFFFFFFFF)) : (((new_cs & 0xFFFF) << 16) | (new_eip & 0xFFFF));
259
 
260
                byte imm_bytes[] = new byte[o32? 6 : 4];
261
                for(int i=0; i<imm_bytes.length; i++) {
262
                    imm_bytes[i] = (byte)(immediate & 0xFF);
263
                    immediate >>= 8;
264
                }
265
                extra_bytes = imm_bytes;
266
            }
267
 
268
            instruction = prepare_instr(cs_d_b, a32, o32, extra_bytes, is_Ep);
269
            instr.add_instruction(instruction);
270
 
271
            // end condition
272
            break;
273
        }
274
 
275
        System.out.println("Instruction: [" + instruction + "]");
276
    }
277
 
278
    String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte extra_bytes[], boolean is_Ep) throws Exception {
279
        int opcodes[] = {
280
            0xFF, 0xEA
281
        };
282
 
283
        String prefix = "";
284
        if(cs_d_b != o32) { prefix = "66" + prefix; }
285
        if(cs_d_b != a32) { prefix = "67" + prefix; }
286
 
287
        int opcode = opcodes[is_Ep? 0 : 1];
288
 
289
        byte instr[] = new byte[1 + extra_bytes.length];
290
        instr[0] = (byte)opcode;
291
        System.arraycopy(extra_bytes, 0, instr, 1, extra_bytes.length);
292
 
293
        return prefix + bytesToHex(instr);
294
    }
295
 
296
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.