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

Subversion Repositories ao486

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

powered by: WebSVN 2.1.0

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