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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [branch/] [TestRET_far_outer.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.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.Layer;
37
import ao486.test.layers.MemoryLayer;
38
import ao486.test.layers.MemoryPatchLayer;
39
import ao486.test.layers.OtherLayer;
40
import ao486.test.layers.Pair;
41
import ao486.test.layers.SegmentLayer;
42
import ao486.test.layers.StackLayer;
43
import java.io.*;
44
import java.util.LinkedList;
45
import java.util.Random;
46
 
47
 
48
public class TestRET_far_outer extends TestUnit implements Serializable {
49
    public static void main(String args[]) throws Exception {
50
        run_test(TestRET_far_outer.class);
51
    }
52
 
53
    //--------------------------------------------------------------------------
54
    @Override
55
    public int get_test_count() throws Exception {
56
        return 100;
57
    }
58
 
59
    @Override
60
    public void init() throws Exception {
61
 
62
        random = new Random(8 + index);
63
 
64
        String instruction;
65
        while(true) {
66
            layers.clear();
67
 
68
            LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
69
 
70
            InstructionLayer instr  = new InstructionLayer(random, prohibited_list);
71
            layers.add(instr);
72
            StackLayer stack        = new StackLayer(random, prohibited_list);
73
            layers.add(stack);
74
            layers.add(new OtherLayer(random));
75
            layers.add(new FlagsLayer(random));
76
            layers.add(new GeneralRegisterLayer(random));
77
            layers.add(new SegmentLayer(random));
78
            layers.add(new MemoryLayer(random));
79
            layers.add(new IOLayer(random));
80
            layers.addFirst(new HandleModeChangeLayer(
81
                    getInput("cr0_pe"),
82
                    getInput("vmflag"),
83
                    getInput("cs_rpl"),
84
                    getInput("cs_p"),
85
                    getInput("cs_s"),
86
                    getInput("cs_type")
87
            ));
88
 
89
            // instruction size
90
            boolean cs_d_b  = getInput("cs_d_b") == 1;
91
            boolean cr0_pe  = getInput("cr0_pe") == 1;
92
            boolean flag_vm = getInput("vmflag") == 1;
93
 
94
            boolean a32     = random.nextBoolean();
95
            boolean o32     = random.nextBoolean();
96
 
97
            long cs  = 0;
98
            long eip = 0;
99
 
100
            //-------------------------- prepare CS
101
 
102
            DescriptorTableLayer tables = new DescriptorTableLayer(random, prohibited_list, true);
103
 
104
            boolean is_ldt = random.nextBoolean();
105
 
106
            boolean conds[] = new boolean[6];
107
            int cond = 0;
108
 
109
            int     new_cs_rpl  = 0;
110
            int     old_cs_rpl  = 0;
111
            boolean new_cs_seg  = false;
112
            int     new_cs_type = 0;
113
            int     new_cs_dpl  = 0;
114
            boolean new_cs_p    = false;
115
 
116
            do {
117
                old_cs_rpl  = random.nextInt(3);
118
                new_cs_seg  = random.nextBoolean();
119
                new_cs_type = random.nextInt(16);
120
                new_cs_dpl  = random.nextInt(4);
121
                new_cs_p    = random.nextBoolean();
122
 
123
                new_cs_rpl = old_cs_rpl+1+random.nextInt(3-old_cs_rpl);
124
 
125
                conds[0] = new_cs_rpl < old_cs_rpl;
126
                //check_cs()
127
                conds[1] = new_cs_seg == false;
128
                conds[2] = ((new_cs_type >> 3)&1) == 0; // is data segment
129
                conds[3] = ((new_cs_type >> 3)&1) == 1 && ((new_cs_type >> 2)&1) == 0 && new_cs_dpl != new_cs_rpl; // code non conforming
130
                conds[4] = ((new_cs_type >> 3)&1) == 1 && ((new_cs_type >> 2)&1) == 1 && new_cs_dpl > new_cs_rpl;  // code conforming
131
                conds[5] = new_cs_p == false;
132
            }
133
            while(!isAccepted(cond, conds[0],conds[1],conds[2],conds[3],conds[4],conds[5]));
134
 
135
            long new_cs_base, new_cs_limit;
136
            boolean new_cs_g;
137
            while(true) {
138
                new_cs_base = Layer.norm(random.nextInt());
139
                new_cs_g    = random.nextBoolean();
140
 
141
                new_cs_limit = random.nextInt(new_cs_g? 2 : 0xFFFF + 1);
142
                if(new_cs_g) new_cs_limit = (new_cs_limit << 12) | 0xFFF;
143
 
144
                if( new_cs_base + new_cs_limit < 4294967296L &&
145
                    Layer.collides(prohibited_list, (int)new_cs_base, (int)(new_cs_base + new_cs_limit)) == false
146
                ) break;
147
            }
148
 
149
            boolean new_cs_d_b = random.nextBoolean();
150
            boolean new_cs_l   = random.nextBoolean();
151
            boolean new_cs_avl = random.nextBoolean();
152
            long new_cs_limit_final = new_cs_g? new_cs_limit >> 12 : new_cs_limit;
153
            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);
154
 
155
            final int old_cs_rpl_final = old_cs_rpl;
156
            Layer cs_rpl_layer = new Layer() {
157
                long cs_rpl() { return old_cs_rpl_final; }
158
            };
159
            layers.addFirst(cs_rpl_layer);
160
 
161
            int index = tables.addDescriptor(is_ldt, cs_desc);
162
            if(index == -1) continue;
163
 
164
            index = index << 3;
165
            if(is_ldt) index |= 4;
166
 
167
            index |= new_cs_rpl;
168
 
169
            cs = index;
170
 
171
 
172
            /* all outer
173
            *
174
            * 0. ss_selector null
175
            * 1. ss_descriptor out of bounds
176
            * 2. check ss
177
            * 3. eip out of bounds
178
            * 4. all ok
179
            */
180
            int test_type = 4; //random.nextInt(5);
181
 
182
            if(test_type == 3) {
183
                while(true) {
184
                    eip = Layer.norm(random.nextInt());
185
 
186
                    if(o32 == false) eip &= 0xFFFF;
187
 
188
                    if(eip > new_cs_limit) break;
189
                }
190
                if(o32 == false) eip |= (random.nextInt() & 0xFFFF0000);
191
            }
192
            else {
193
                while(true) {
194
                    eip = Layer.norm(random.nextInt());
195
 
196
                    if(o32 == false) eip &= 0xFFFF;
197
 
198
                    if(eip <= new_cs_limit) break;
199
                }
200
 
201
                long dest = new_cs_base + eip;
202
                // adding always possible
203
                MemoryPatchLayer patch = new MemoryPatchLayer(random, prohibited_list, (int)dest, 0x0F,0x0F);
204
                layers.addFirst(patch);
205
 
206
                if(o32 == false) eip |= (random.nextInt() & 0xFFFF0000);
207
            }
208
 
209
 
210
 
211
            //-------------------------- prepare SS
212
            int pop_bytes = random.nextBoolean()? random.nextInt(0xFF) : 0;
213
 
214
            long ss  = random.nextInt(4);
215
            long esp = Layer.norm(random.nextInt());
216
 
217
 
218
            if(test_type == 0) {
219
                // nothing
220
            }
221
            else if(test_type == 1) {
222
                is_ldt = random.nextBoolean();
223
 
224
                index = tables.getOutOfBoundsIndex(is_ldt);
225
                if(index == -1) continue;
226
 
227
                index = index << 3;
228
                if(is_ldt) index |= 4;
229
 
230
                // rpl always zero
231
 
232
                ss = index;
233
            }
234
            else if(test_type == 2 || test_type == 3 || test_type == 4) {
235
                is_ldt = random.nextBoolean();
236
 
237
                conds = new boolean[6];
238
                cond = 1 << random.nextInt(conds.length);
239
                if(test_type >= 3) cond = 0;
240
 
241
                int     new_ss_rpl  = 0;
242
                boolean new_ss_seg  = false;
243
                int     new_ss_type = 0;
244
                int     new_ss_dpl  = 0;
245
                boolean new_ss_p    = false;
246
 
247
                do {
248
                    new_ss_rpl  = random.nextInt(4);
249
                    new_ss_seg  = random.nextBoolean();
250
                    new_ss_type = random.nextInt(16);
251
                    new_ss_dpl  = random.nextInt(4);
252
                    new_ss_p    = random.nextBoolean();
253
 
254
                    conds[0] = new_ss_rpl != new_cs_rpl;
255
                    conds[1] = new_ss_seg == false;
256
                    conds[2] = ((new_ss_type >> 3)&1) == 1; // is code segment
257
                    conds[3] = ((new_ss_type >> 3)&1) == 0 && ((new_ss_type >> 1)&1) == 0; // data not writable
258
                    conds[4] = new_ss_dpl != new_cs_rpl;
259
                    conds[5] = new_ss_p == false;
260
                }
261
                while(!isAccepted(cond, conds[0],conds[1],conds[2],conds[3],conds[4],conds[5]));
262
 
263
                long new_ss_base, new_ss_limit;
264
                while(true) {
265
                    new_ss_base = Layer.norm(random.nextInt());
266
 
267
                    new_ss_limit = random.nextInt(0xFFFF + 1);
268
 
269
                    if( new_ss_base + new_ss_limit < 4294967296L &&
270
                        Layer.collides(prohibited_list, (int)new_ss_base, (int)(new_ss_base + new_ss_limit)) == false
271
                    ) break;
272
                }
273
 
274
                boolean new_ss_d_b = random.nextBoolean();
275
                boolean new_ss_g   = random.nextBoolean();
276
                boolean new_ss_l   = random.nextBoolean();
277
                boolean new_ss_avl = random.nextBoolean();
278
                Descriptor ss_desc = new Descriptor((int)new_ss_base, (int)new_ss_limit, new_ss_type, new_ss_seg, new_ss_p, new_ss_dpl, new_ss_d_b, new_ss_g, new_ss_l, new_ss_avl);
279
 
280
 
281
                index = tables.addDescriptor(is_ldt, ss_desc);
282
                if(index == -1) continue;
283
 
284
                index = index << 3;
285
                if(is_ldt) index |= 4;
286
 
287
                index |= new_ss_rpl;
288
 
289
                ss = index;
290
            }
291
 
292
            // entry stack
293
            if(o32) {
294
                stack.push_dword((int)eip); //eip
295
                stack.push_dword((int)cs);  //cs
296
 
297
                for(int i=0; i<pop_bytes; i++) stack.push_byte(random.nextInt() & 0xFF);
298
 
299
                stack.push_dword((int)esp); // esp
300
                stack.push_dword((int)ss);  // ss
301
            }
302
            else {
303
                stack.push_word((int)eip); //eip
304
                stack.push_word((int)cs);  //cs
305
 
306
                for(int i=0; i<pop_bytes; i++) stack.push_byte(random.nextInt() & 0xFF);
307
 
308
                stack.push_word((int)esp); // esp
309
                stack.push_word((int)ss);  // ss
310
            }
311
 
312
            layers.addFirst(tables);
313
 
314
            // add instruction
315
            instruction = prepare_instr(cs_d_b, a32, o32, pop_bytes);
316
            instr.add_instruction(instruction);
317
 
318
 
319
 
320
            // end condition
321
            if(cr0_pe && flag_vm == false) {
322
                break;
323
            }
324
        }
325
 
326
        System.out.println("Instruction: [" + instruction + "]");
327
    }
328
 
329
    int imm_len(boolean a32, boolean o32, int opcode) {
330
        return opcode == 0xCA ? 2 : 0;
331
    }
332
    String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, int pop_bytes) throws Exception {
333
        int opcodes[] = {
334
            0xCA,0xCB
335
        };
336
 
337
        String prefix = "";
338
        if(cs_d_b != o32) { prefix = "66" + prefix; }
339
        if(cs_d_b != a32) { prefix = "67" + prefix; }
340
 
341
        int     opcode      = opcodes[random.nextInt(opcodes.length)];
342
        if(pop_bytes != 0) opcode = 0xCA;
343
        boolean is_modregrm = false;
344
 
345
        byte possible_modregrm = (byte)random.nextInt();
346
        byte possible_sib      = (byte)random.nextInt();
347
 
348
        int len = (is_modregrm == false)? 1 : 1 + modregrm_len(!cs_d_b, unsigned(possible_modregrm), unsigned(possible_sib));
349
        len += imm_len(a32, o32, opcode);
350
 
351
 
352
        byte instr[] = new byte[len];
353
        instr[0] = (byte)opcode;
354
        if(len > 1) instr[1] = (byte)((pop_bytes >> 0) & 0xFF);
355
        if(len > 2) instr[2] = (byte)((pop_bytes >> 8) & 0xFF);
356
 
357
        return prefix + bytesToHex(instr);
358
    }
359
 
360
}

powered by: WebSVN 2.1.0

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