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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [branch/] [TestTaskSwitch.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.Layer;
32
import ao486.test.layers.MemoryPatchLayer;
33
import ao486.test.layers.Pair;
34
import ao486.test.layers.TSSCurrentLayer;
35
import java.util.LinkedList;
36
import java.util.Random;
37
 
38
public class TestTaskSwitch {
39
 
40
    public static enum Source {
41
        FROM_IRET,
42
        FROM_CALL,
43
        FROM_JUMP,
44
        FROM_INT
45
    }
46
 
47
   /*
48
    * 3 - new TSS limit (not in CALL_task_gate)
49
    * 4 - old TSS limit
50
    *
51
    * 5 - LDT TI
52
    * 6 - LDT out of index
53
    * 7 - invalid LDT descriptor
54
    *
55
    * 8 - v8086 mode, eip invalid
56
    * 9 - v8086 mode, eip ok
57
    *
58
    * 10 - SS null
59
    * 11 - SS out of bounds
60
    * 12 - SS descriptor invalid
61
    *
62
    * 13 - DS out of bounds
63
    * 14 - DS descriptor invalid
64
    *
65
    * 15 - ES out of bounds
66
    * 16 - ES descriptor invalid
67
    *
68
    * 17 - FS out of bounds
69
    * 18 - FS descriptor invalid
70
    *
71
    * 19 - GS out of bounds
72
    * 20 - GS descriptor invalid
73
    *
74
    * 21 - CS null
75
    * 22 - CS out of bounds
76
    * 23 - CS invalid descriptor
77
    *
78
    * 24 - eip out of bounds
79
    *
80
    * 25 - all ok
81
    *
82
    * 26 - task debug
83
    */
84
 
85
 
86
    public static int new_tss_selector;
87
    public static int old_tss_limit;
88
    public static DescriptorTableLayer tables;
89
 
90
    public static boolean test(Random random, TestUnit test, LinkedList<Pair<Long, Long>> prohibited_list, Source source,
91
            TestUnit.Descriptor tss_desc,
92
            int new_tss_rpl,
93
            DescriptorTableLayer tables,
94
            int test_type) throws Exception {
95
 
96
//TODO: test translate_linear
97
//TODO: test T trap bit  
98
//TODO: push error
99
 
100
        int type = (test_type == -1)? 3+ random.nextInt(25+1-3) : test_type;
101
 
102
System.out.println("Task type: " + type);
103
 
104
        old_tss_limit = 0xFFFF;
105
        TestTaskSwitch.tables = tables;
106
 
107
        TSSCurrentLayer.Type old_tss_type = random.nextBoolean()? TSSCurrentLayer.Type.BUSY_286 : TSSCurrentLayer.Type.BUSY_386;
108
 
109
        if(type == 3) {
110
 
111
            tss_desc.g = false;
112
 
113
            tss_desc.limit =
114
                       (tss_desc.type == (source == Source.FROM_IRET? 0x3 : 0x1))?  0x2B - 3 + random.nextInt(3) :
115
                                                                                    0x67 - 3 + random.nextInt(3);
116
 
117
            if(tables == null) {
118
                tables = new DescriptorTableLayer(random, prohibited_list, true);
119
                TestTaskSwitch.tables = tables;
120
                new_tss_selector = tables.addDescriptor(false, tss_desc);
121
                if(new_tss_selector == -1) return false;
122
 
123
                //copy
124
                new_tss_selector <<= 3;
125
                new_tss_selector |= new_tss_rpl;
126
System.out.printf("[task_switch: new_tss_selector: %x\n", new_tss_selector);
127
            }
128
        }
129
        else if(type >= 4) {
130
 
131
            int     new_tss_type    = tss_desc.type;
132
            long    new_tss_base    = Layer.norm(tss_desc.base);
133
 
134
            boolean conds[]         = new boolean[0];
135
            int     cond            = 0;
136
 
137
            if(tables == null) {
138
                tables = new DescriptorTableLayer(random, prohibited_list, true);
139
                TestTaskSwitch.tables = tables;
140
                new_tss_selector = tables.addDescriptor(false, tss_desc);
141
                if(new_tss_selector == -1) return false;
142
 
143
                //copy
144
                new_tss_selector <<= 3;
145
                new_tss_selector |= new_tss_rpl;
146
            }
147
 
148
            if(type == 4) {
149
                old_tss_limit =
150
                        (old_tss_type == TSSCurrentLayer.Type.BUSY_286)?    0x29 - 3 + random.nextInt(3) :
151
                                                                            0x5F - 3 + random.nextInt(3);
152
            }
153
            else if(type == 5) {
154
                long address = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? 42 : 96);
155
 
156
                int ldt_selector = random.nextInt(65536);
157
                ldt_selector |= 4;
158
 
159
                MemoryPatchLayer ldt_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)address, (ldt_selector & 0xFF), ((ldt_selector>>8) & 0xFF));
160
                test.layers.addFirst(ldt_selector_patch);
161
            }
162
            else if(type == 6) {
163
                long address = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? 42 : 96);
164
 
165
                int ldt_selector = tables.getOutOfBoundsIndex(false);
166
                if(ldt_selector == -1) return false;
167
 
168
                ldt_selector <<= 3;
169
                ldt_selector |= random.nextInt(0x8);
170
                ldt_selector &= 0xFFFB;
171
 
172
                MemoryPatchLayer ldt_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)address, (ldt_selector & 0xFF), ((ldt_selector>>8) & 0xFF));
173
                test.layers.addFirst(ldt_selector_patch);
174
            }
175
            else if(type >= 7) {
176
 
177
                boolean is_new_ldt_null = random.nextInt(4) == 0 && type >= 8;
178
                int ldt_selector = random.nextInt(4);
179
                long new_ldt_base = 0, new_ldt_limit = 0;
180
 
181
                if(is_new_ldt_null == false) {
182
                    conds= new boolean[3];
183
                    cond = (type == 7)? 1 << random.nextInt(conds.length) : 0;
184
 
185
                    boolean new_ldt_seg  = false;
186
                    int     new_ldt_type = 0;
187
                    boolean new_ldt_p    = false;
188
 
189
                    int     new_ldt_dpl  = random.nextInt(4);
190
                    boolean new_ldt_d_b  = random.nextBoolean();
191
                    boolean new_ldt_l    = random.nextBoolean();
192
                    boolean new_ldt_avl  = random.nextBoolean();
193
                    int     new_ldt_rpl  = random.nextInt(4);
194
 
195
                    do {
196
                        new_ldt_seg = random.nextBoolean();
197
                        new_ldt_type= random.nextInt(16);
198
                        new_ldt_p   = random.nextBoolean();
199
 
200
                        conds[0] = new_ldt_seg;
201
                        conds[1] = new_ldt_type != 0x2; // ldt
202
                        conds[2] = new_ldt_p == false;
203
                    }
204
                    while(!test.isAccepted(cond, conds[0],conds[1],conds[2]));
205
 
206
                    //---------
207
                    boolean new_ldt_g;
208
                    while(true) {
209
                        new_ldt_base = Layer.norm(random.nextInt());
210
                        new_ldt_g    = false;
211
 
212
                        new_ldt_limit = random.nextInt(new_ldt_g? 0xF+1 : 0xFFFF + 1);
213
                        if(new_ldt_g) new_ldt_limit = (new_ldt_limit << 12) | 0xFFF;
214
 
215
                        if(new_ldt_g) new_ldt_limit = (new_ldt_limit << 12) | 0xFFF;
216
 
217
                        if( new_ldt_base + new_ldt_limit < 4294967296L &&
218
                            Layer.collides(prohibited_list, (int)new_ldt_base, (int)(new_ldt_base + new_ldt_limit)) == false)
219
                        {
220
                            prohibited_list.add(new Pair<>(new_ldt_base, new_ldt_base + new_ldt_limit));
221
                            break;
222
                        }
223
                    }
224
 
225
                    long new_ldt_limit_final = new_ldt_g? new_ldt_limit >> 12 : new_ldt_limit;
226
 
227
                    TestUnit.Descriptor ldt_desc = new TestUnit.Descriptor((int)new_ldt_base, (int)new_ldt_limit_final, new_ldt_type, new_ldt_seg, new_ldt_p, new_ldt_dpl, new_ldt_d_b, new_ldt_g, new_ldt_l, new_ldt_avl);
228
 
229
                    ldt_selector = tables.addDescriptor(false, ldt_desc);
230
                    if(ldt_selector == -1) return false;
231
 
232
                    ldt_selector <<= 3;
233
                    ldt_selector |= new_ldt_rpl;
234
                }
235
 
236
                long ldt_offset = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? 42 : 96);
237
 
238
                MemoryPatchLayer ldt_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)ldt_offset, (ldt_selector & 0xFF), ((ldt_selector>>8) & 0xFF));
239
                test.layers.addFirst(ldt_selector_patch);
240
 
241
                if(type == 7) {
242
                    // nothing
243
                }
244
                else if(type >= 8) {
245
 
246
                    if(new_tss_type == (source == Source.FROM_IRET? 0xB : 0x9)) {
247
                        // set trap to zero
248
                        long trap_offset = new_tss_base + 100;
249
                        MemoryPatchLayer trap_patch = new MemoryPatchLayer(random, prohibited_list, (int)trap_offset,
250
                                0,0);
251
                        test.layers.addFirst(trap_patch);
252
                    }
253
 
254
                    // set eflags
255
                    long eflags_offset = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? 16 : 36);
256
 
257
                    long eflags = Layer.norm(random.nextInt());
258
                    // set vm flag
259
                    if(type == 8 || type == 9)  eflags |= 0x20000;
260
                    else                        eflags &= 0xFFFDFFFF;
261
 
262
                    if(new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1)) {
263
                        MemoryPatchLayer elfags_patch = new MemoryPatchLayer(random, prohibited_list, (int)eflags_offset,
264
                                (int)(eflags & 0xFF), (int)((eflags>>8) & 0xFF));
265
                        test.layers.addFirst(elfags_patch);
266
                    }
267
                    else {
268
                        MemoryPatchLayer elfags_patch = new MemoryPatchLayer(random, prohibited_list, (int)eflags_offset,
269
                                (int)(eflags & 0xFF), (int)((eflags>>8) & 0xFF), (int)((eflags>>16) & 0xFF), (int)((eflags>>24) & 0xFF));
270
                        test.layers.addFirst(elfags_patch);
271
                    }
272
 
273
                    if(type == 8 || type == 9) {
274
                        // always new tss is 386
275
 
276
                        // set cs
277
                        long cs_offset = new_tss_base + 76;
278
 
279
                        long new_cs_base, new_cs_limit;
280
                        while(true) {
281
                            new_cs_base = Layer.norm(random.nextInt(65536));
282
                            new_cs_base <<= 4;
283
                            new_cs_limit = 0xFFFF;
284
 
285
                            if( new_cs_base + new_cs_limit < 4294967296L &&
286
                                Layer.collides(prohibited_list, (int)new_cs_base, (int)(new_cs_base + new_cs_limit)) == false)
287
                            {
288
                                prohibited_list.add(new Pair<>(new_cs_base, new_cs_base + new_cs_limit));
289
                                break;
290
                            }
291
                        }
292
                        MemoryPatchLayer cs_patch = new MemoryPatchLayer(random, prohibited_list, (int)cs_offset,
293
                                (int)((new_cs_base >> 4) & 0xFF), (int)((new_cs_base>>12) & 0xFF));
294
                        test.layers.addFirst(cs_patch);
295
 
296
                        // set eip
297
                        long eip_offset = new_tss_base + 32;
298
 
299
                        long eip = (type == 9)? random.nextInt(0xFFFF) : 0xFFFF + 1 + random.nextInt(5);
300
 
301
                        MemoryPatchLayer eip_patch = new MemoryPatchLayer(random, prohibited_list, (int)eip_offset,
302
                                (int)(eip & 0xFF), (int)((eip>>8) & 0xFF), (int)((eip>>16) & 0xFF), (int)((eip>>24) & 0xFF));
303
                        test.layers.addFirst(eip_patch);
304
 
305
                        if(type == 9) {
306
                            MemoryPatchLayer instr_patch = new MemoryPatchLayer(random, prohibited_list, (int)(new_cs_base + eip),
307
                                0x0F, 0x0F);
308
                            test.layers.addFirst(instr_patch);
309
                        }
310
                    }
311
                    else if(type == 10) {
312
                        long address = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? 38 : 80);
313
 
314
                        int ss_selector = random.nextInt(4);
315
 
316
                        MemoryPatchLayer ss_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)address, (ss_selector & 0xFF), ((ss_selector>>8) & 0xFF));
317
                        test.layers.addFirst(ss_selector_patch);
318
                    }
319
                    else if(type == 11) {
320
                        boolean is_ss_ldt = is_new_ldt_null == false && random.nextBoolean();
321
 
322
                        if(is_ss_ldt) {
323
                            tables.setup_new_ldt((int)new_ldt_base, (int)new_ldt_limit);
324
                        }
325
 
326
                        int new_ss_selector = tables.getOutOfBoundsIndex(is_ss_ldt);
327
                        if(new_ss_selector == -1) return false;
328
 
329
                        new_ss_selector <<= 3;
330
                        new_ss_selector |= (is_ss_ldt)? 4 : 0;
331
                        new_ss_selector |= random.nextInt(4);
332
 
333
                        long address = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? 38 : 80);
334
 
335
                        MemoryPatchLayer ss_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)address, (new_ss_selector & 0xFF), ((new_ss_selector>>8) & 0xFF));
336
                        test.layers.addFirst(ss_selector_patch);
337
                    }
338
                    else if(type >= 12) {
339
                        boolean is_ss_ldt = is_new_ldt_null == false && random.nextBoolean();
340
 
341
                        if(is_ss_ldt) {
342
                            tables.setup_new_ldt((int)new_ldt_base, (int)new_ldt_limit);
343
                        }
344
 
345
                        int new_cs_rpl = random.nextInt(4);
346
 
347
                        conds= new boolean[6];
348
                        cond = (type == 12)? 1 << random.nextInt(conds.length) : 0;
349
 
350
                        boolean new_ss_seg  = false;
351
                        int     new_ss_type = 0;
352
                        boolean new_ss_p    = false;
353
                        int     new_ss_dpl  = 0;
354
                        int     new_ss_rpl  = 0;
355
 
356
                        boolean new_ss_d_b  = random.nextBoolean();
357
                        boolean new_ss_l    = random.nextBoolean();
358
                        boolean new_ss_avl  = random.nextBoolean();
359
                        int     new_ss_limit= random.nextInt(65536);
360
                        int     new_ss_base = random.nextInt(65536);
361
                        boolean new_ss_g    = random.nextBoolean();
362
 
363
                        do {
364
                            new_ss_seg = random.nextBoolean();
365
                            new_ss_type= random.nextInt(16);
366
                            new_ss_p   = random.nextBoolean();
367
                            new_ss_dpl = random.nextInt(4);
368
                            new_ss_rpl = random.nextInt(4);
369
 
370
                            conds[0] = new_ss_seg == false;
371
                            conds[1] = (new_ss_type >> 3) == 1; // code segment
372
                            conds[2] = (new_ss_type >> 3) == 0 && ((new_ss_type >> 1)&1) == 0; // data segment and not writable
373
                            conds[3] = new_ss_p == false;
374
                            conds[4] = new_ss_dpl != new_cs_rpl;
375
                            conds[5] = new_ss_dpl != new_ss_rpl;
376
                        }
377
                        while(!test.isAccepted(cond, conds[0],conds[1],conds[2],conds[3],conds[4],conds[5]));
378
 
379
                        TestUnit.Descriptor ss_desc = new TestUnit.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);
380
 
381
                        int ss_selector = tables.addDescriptor(is_ss_ldt, ss_desc);
382
                        if(ss_selector == -1) return false;
383
 
384
                        ss_selector <<= 3;
385
                        ss_selector |= new_ss_rpl;
386
                        if(is_ss_ldt) ss_selector |= 4;
387
 
388
                        long ss_address = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? 38 : 80);
389
 
390
                        MemoryPatchLayer ss_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)ss_address, (ss_selector & 0xFF), ((ss_selector>>8) & 0xFF));
391
                        test.layers.addFirst(ss_selector_patch);
392
 
393
                        // cs selector
394
                        // set cs
395
                        long cs_offset = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? 36 : 76);
396
 
397
                        int new_cs_selector = (type >= 17 && type <= 21)? 0 : random.nextInt(65536);
398
                        new_cs_selector &= 0xFFFC;
399
                        new_cs_selector |= new_cs_rpl;
400
 
401
                        MemoryPatchLayer cs_patch = new MemoryPatchLayer(random, prohibited_list, (int)cs_offset,
402
                                (int)((new_cs_selector >> 0) & 0xFF), (int)((new_cs_selector>>8) & 0xFF));
403
                        test.layers.addFirst(cs_patch);
404
 
405
                        if(type == 12) {
406
                            // nothing
407
                        }
408
                        else if(type == 13) {
409
                            boolean is_ds_ldt = is_new_ldt_null == false && random.nextBoolean();
410
 
411
                            if(is_ds_ldt) {
412
                                tables.setup_new_ldt((int)new_ldt_base, (int)new_ldt_limit);
413
                            }
414
 
415
                            long ds_offset = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? 40 : 84);
416
 
417
                            int new_ds_selector = tables.getOutOfBoundsIndex(is_ds_ldt);
418
                            if(new_ds_selector == -1) return false;
419
 
420
                            new_ds_selector <<= 3;
421
                            new_ds_selector |= (is_ds_ldt)? 4 : 0;
422
                            new_ds_selector |= random.nextInt(4);
423
 
424
                            MemoryPatchLayer ds_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)ds_offset, (new_ds_selector & 0xFF), ((new_ds_selector>>8) & 0xFF));
425
                            test.layers.addFirst(ds_selector_patch);
426
                        }
427
                        else if(type >= 14) {
428
                            boolean is_ds_null = random.nextInt(10) == 0;
429
                            int ds_selector = random.nextInt(4);
430
 
431
                            if(is_ds_null == false) {
432
                                boolean is_ds_ldt = is_new_ldt_null == false && random.nextBoolean();
433
 
434
                                if(is_ds_ldt) {
435
                                    tables.setup_new_ldt((int)new_ldt_base, (int)new_ldt_limit);
436
                                }
437
 
438
                                conds= new boolean[4];
439
                                cond = (type == 14)? 1 << random.nextInt(conds.length) : 0;
440
 
441
                                boolean new_ds_seg  = false;
442
                                int     new_ds_type = 0;
443
                                boolean new_ds_p    = false;
444
                                int     new_ds_dpl  = 0;
445
                                int     new_ds_rpl  = 0;
446
 
447
                                boolean new_ds_d_b  = random.nextBoolean();
448
                                boolean new_ds_l    = random.nextBoolean();
449
                                boolean new_ds_avl  = random.nextBoolean();
450
                                int     new_ds_limit= random.nextInt(65536);
451
                                int     new_ds_base = random.nextInt(65536);
452
                                boolean new_ds_g    = random.nextBoolean();
453
 
454
                                do {
455
                                    new_ds_seg = random.nextBoolean();
456
                                    new_ds_type= random.nextInt(16);
457
                                    new_ds_p   = random.nextBoolean();
458
                                    new_ds_dpl = random.nextInt(4);
459
                                    new_ds_rpl = random.nextInt(4);
460
 
461
                                    conds[0] = new_ds_seg == false;
462
                                    conds[1] = (new_ds_type >> 3) == 1 && ((new_ds_type >> 1)&1) == 0; // code segment and not readable
463
                                    conds[2] = ((new_ds_type >> 3) == 0 || ((new_ds_type >> 2)&1) == 0) && (new_ds_rpl > new_ds_dpl || new_cs_rpl > new_ds_dpl); // (data segment or code non conforming)
464
                                    conds[3] = new_ds_p == false;
465
                                }
466
                                while(!test.isAccepted(cond, conds[0],conds[1],conds[2],conds[3]));
467
 
468
                                TestUnit.Descriptor ds_desc = new TestUnit.Descriptor((int)new_ds_base, (int)new_ds_limit, new_ds_type, new_ds_seg, new_ds_p, new_ds_dpl, new_ds_d_b, new_ds_g, new_ds_l, new_ds_avl);
469
 
470
                                ds_selector = tables.addDescriptor(is_ds_ldt, ds_desc);
471
                                if(ds_selector == -1) return false;
472
 
473
                                ds_selector <<= 3;
474
                                ds_selector |= new_ds_rpl;
475
                                if(is_ds_ldt) ds_selector |= 4;
476
                            }
477
 
478
                            long ds_offset = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? 40 : 84);
479
 
480
                            MemoryPatchLayer ds_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)ds_offset, (ds_selector & 0xFF), ((ds_selector>>8) & 0xFF));
481
                            test.layers.addFirst(ds_selector_patch);
482
 
483
                            if(type == 15) {
484
                                boolean is_es_ldt = is_new_ldt_null == false && random.nextBoolean();
485
 
486
                                if(is_es_ldt) {
487
                                    tables.setup_new_ldt((int)new_ldt_base, (int)new_ldt_limit);
488
                                }
489
 
490
                                long es_offset = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? 34 : 72);
491
 
492
                                int new_es_selector = tables.getOutOfBoundsIndex(is_es_ldt);
493
                                if(new_es_selector == -1) return false;
494
 
495
                                new_es_selector <<= 3;
496
                                new_es_selector |= (is_es_ldt)? 4 : 0;
497
                                new_es_selector |= random.nextInt(4);
498
 
499
                                MemoryPatchLayer es_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)es_offset, (new_es_selector & 0xFF), ((new_es_selector>>8) & 0xFF));
500
                                test.layers.addFirst(es_selector_patch);
501
                            }
502
                            else if(type >= 16) {
503
                                boolean is_es_null = random.nextInt(10) == 0;
504
                                int es_selector = random.nextInt(4);
505
 
506
                                if(is_es_null == false) {
507
                                    boolean is_es_ldt = is_new_ldt_null == false && random.nextBoolean();
508
 
509
                                    if(is_es_ldt) {
510
                                        tables.setup_new_ldt((int)new_ldt_base, (int)new_ldt_limit);
511
                                    }
512
 
513
                                    conds= new boolean[4];
514
                                    cond = (type == 16)? 1 << random.nextInt(conds.length) : 0;
515
 
516
                                    boolean new_es_seg  = false;
517
                                    int     new_es_type = 0;
518
                                    boolean new_es_p    = false;
519
                                    int     new_es_dpl  = 0;
520
                                    int     new_es_rpl  = 0;
521
 
522
                                    boolean new_es_d_b  = random.nextBoolean();
523
                                    boolean new_es_l    = random.nextBoolean();
524
                                    boolean new_es_avl  = random.nextBoolean();
525
                                    int     new_es_limit= random.nextInt(65536);
526
                                    int     new_es_base = random.nextInt(65536);
527
                                    boolean new_es_g    = random.nextBoolean();
528
 
529
                                    do {
530
                                        new_es_seg = random.nextBoolean();
531
                                        new_es_type= random.nextInt(16);
532
                                        new_es_p   = random.nextBoolean();
533
                                        new_es_dpl = random.nextInt(4);
534
                                        new_es_rpl = random.nextInt(4);
535
 
536
                                        conds[0] = new_es_seg == false;
537
                                        conds[1] = (new_es_type >> 3) == 1 && ((new_es_type >> 1)&1) == 0; // code segment and not readable
538
                                        conds[2] = ((new_es_type >> 3) == 0 || ((new_es_type >> 2)&1) == 0) && (new_es_rpl > new_es_dpl || new_cs_rpl > new_es_dpl); // (data segment or code non conforming)
539
                                        conds[3] = new_es_p == false;
540
                                    }
541
                                    while(!test.isAccepted(cond, conds[0],conds[1],conds[2],conds[3]));
542
 
543
                                    TestUnit.Descriptor es_desc = new TestUnit.Descriptor((int)new_es_base, (int)new_es_limit, new_es_type, new_es_seg, new_es_p, new_es_dpl, new_es_d_b, new_es_g, new_es_l, new_es_avl);
544
 
545
                                    es_selector = tables.addDescriptor(is_es_ldt, es_desc);
546
                                    if(es_selector == -1) return false;
547
 
548
                                    es_selector <<= 3;
549
                                    es_selector |= new_es_rpl;
550
                                    if(is_es_ldt) es_selector |= 4;
551
                                }
552
 
553
                                long es_offset = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? 34 : 72);
554
 
555
                                MemoryPatchLayer es_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)es_offset, (es_selector & 0xFF), ((es_selector>>8) & 0xFF));
556
                                test.layers.addFirst(es_selector_patch);
557
 
558
                                if(type == 17) {
559
                                    if(new_tss_type == (source == Source.FROM_IRET? 0xB : 0x9)) {
560
                                        boolean is_fs_ldt = is_new_ldt_null == false && random.nextBoolean();
561
 
562
                                        if(is_fs_ldt) {
563
                                            tables.setup_new_ldt((int)new_ldt_base, (int)new_ldt_limit);
564
                                        }
565
 
566
                                        long fs_offset = new_tss_base + 88;
567
 
568
                                        int new_fs_selector = tables.getOutOfBoundsIndex(is_fs_ldt);
569
                                        if(new_fs_selector == -1) return false;
570
 
571
                                        new_fs_selector <<= 3;
572
                                        new_fs_selector |= (is_fs_ldt)? 4 : 0;
573
                                        new_fs_selector |= random.nextInt(4);
574
 
575
                                        MemoryPatchLayer fs_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)fs_offset, (new_fs_selector & 0xFF), ((new_fs_selector>>8) & 0xFF));
576
                                        test.layers.addFirst(fs_selector_patch);
577
                                    }
578
                                }
579
                                else if(type >= 18) {
580
                                    boolean is_fs_null = random.nextInt(10) == 0;
581
                                    int fs_selector = random.nextInt(4);
582
 
583
                                    if(is_fs_null == false && new_tss_type == (source == Source.FROM_IRET? 0xB : 0x9)) {
584
                                        boolean is_fs_ldt = is_new_ldt_null == false && random.nextBoolean();
585
 
586
                                        if(is_fs_ldt) {
587
                                            tables.setup_new_ldt((int)new_ldt_base, (int)new_ldt_limit);
588
                                        }
589
 
590
                                        conds= new boolean[4];
591
                                        cond = (type == 18)? 1 << random.nextInt(conds.length) : 0;
592
 
593
                                        boolean new_fs_seg  = false;
594
                                        int     new_fs_type = 0;
595
                                        boolean new_fs_p    = false;
596
                                        int     new_fs_dpl  = 0;
597
                                        int     new_fs_rpl  = 0;
598
 
599
                                        boolean new_fs_d_b  = random.nextBoolean();
600
                                        boolean new_fs_l    = random.nextBoolean();
601
                                        boolean new_fs_avl  = random.nextBoolean();
602
                                        int     new_fs_limit= random.nextInt(65536);
603
                                        int     new_fs_base = random.nextInt(65536);
604
                                        boolean new_fs_g    = random.nextBoolean();
605
 
606
                                        do {
607
                                            new_fs_seg = random.nextBoolean();
608
                                            new_fs_type= random.nextInt(16);
609
                                            new_fs_p   = random.nextBoolean();
610
                                            new_fs_dpl = random.nextInt(4);
611
                                            new_fs_rpl = random.nextInt(4);
612
 
613
                                            conds[0] = new_fs_seg == false;
614
                                            conds[1] = (new_fs_type >> 3) == 1 && ((new_fs_type >> 1)&1) == 0; // code segment and not readable
615
                                            conds[2] = ((new_fs_type >> 3) == 0 || ((new_fs_type >> 2)&1) == 0) && (new_fs_rpl > new_fs_dpl || new_cs_rpl > new_fs_dpl); // (data segment or code non conforming)
616
                                            conds[3] = new_fs_p == false;
617
                                        }
618
                                        while(!test.isAccepted(cond, conds[0],conds[1],conds[2],conds[3]));
619
 
620
                                        TestUnit.Descriptor fs_desc = new TestUnit.Descriptor((int)new_fs_base, (int)new_fs_limit, new_fs_type, new_fs_seg, new_fs_p, new_fs_dpl, new_fs_d_b, new_fs_g, new_fs_l, new_fs_avl);
621
 
622
                                        fs_selector = tables.addDescriptor(is_fs_ldt, fs_desc);
623
                                        if(fs_selector == -1) return false;
624
 
625
                                        fs_selector <<= 3;
626
                                        fs_selector |= new_fs_rpl;
627
                                        if(is_fs_ldt) fs_selector |= 4;
628
                                    }
629
 
630
                                    long fs_offset = new_tss_base + 88;
631
 
632
                                    MemoryPatchLayer fs_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)fs_offset, (fs_selector & 0xFF), ((fs_selector>>8) & 0xFF));
633
                                    test.layers.addFirst(fs_selector_patch);
634
 
635
                                    if(type == 19) {
636
                                        if(new_tss_type == (source == Source.FROM_IRET? 0xB : 0x9)) {
637
                                            boolean is_gs_ldt = is_new_ldt_null == false && random.nextBoolean();
638
 
639
                                            if(is_gs_ldt) {
640
                                                tables.setup_new_ldt((int)new_ldt_base, (int)new_ldt_limit);
641
                                            }
642
 
643
                                            long gs_offset = new_tss_base + 92;
644
 
645
                                            int new_gs_selector = tables.getOutOfBoundsIndex(is_gs_ldt);
646
                                            if(new_gs_selector == -1) return false;
647
 
648
                                            new_gs_selector <<= 3;
649
                                            new_gs_selector |= (is_gs_ldt)? 4 : 0;
650
                                            new_gs_selector |= random.nextInt(4);
651
 
652
                                            MemoryPatchLayer gs_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)gs_offset, (new_gs_selector & 0xFF), ((new_gs_selector>>8) & 0xFF));
653
                                            test.layers.addFirst(gs_selector_patch);
654
                                        }
655
                                    }
656
                                    else if(type >= 20) {
657
                                        boolean is_gs_null = random.nextInt(10) == 0;
658
                                        int gs_selector = random.nextInt(4);
659
 
660
                                        if(is_gs_null == false && new_tss_type == (source == Source.FROM_IRET? 0xB : 0x9)) {
661
                                            boolean is_gs_ldt = is_new_ldt_null == false && random.nextBoolean();
662
 
663
                                            if(is_gs_ldt) {
664
                                                tables.setup_new_ldt((int)new_ldt_base, (int)new_ldt_limit);
665
                                            }
666
 
667
                                            conds= new boolean[4];
668
                                            cond = (type == 20)? 1 << random.nextInt(conds.length) : 0;
669
 
670
                                            boolean new_gs_seg  = false;
671
                                            int     new_gs_type = 0;
672
                                            boolean new_gs_p    = false;
673
                                            int     new_gs_dpl  = 0;
674
                                            int     new_gs_rpl  = 0;
675
 
676
                                            boolean new_gs_d_b  = random.nextBoolean();
677
                                            boolean new_gs_l    = random.nextBoolean();
678
                                            boolean new_gs_avl  = random.nextBoolean();
679
                                            int     new_gs_limit= random.nextInt(65536);
680
                                            int     new_gs_base = random.nextInt(65536);
681
                                            boolean new_gs_g    = random.nextBoolean();
682
 
683
                                            do {
684
                                                new_gs_seg = random.nextBoolean();
685
                                                new_gs_type= random.nextInt(16);
686
                                                new_gs_p   = random.nextBoolean();
687
                                                new_gs_dpl = random.nextInt(4);
688
                                                new_gs_rpl = random.nextInt(4);
689
 
690
                                                conds[0] = new_gs_seg == false;
691
                                                conds[1] = (new_gs_type >> 3) == 1 && ((new_gs_type >> 1)&1) == 0; // code segment and not readable
692
                                                conds[2] = ((new_gs_type >> 3) == 0 || ((new_gs_type >> 2)&1) == 0) && (new_gs_rpl > new_gs_dpl || new_cs_rpl > new_gs_dpl); // (data segment or code non conforming)
693
                                                conds[3] = new_gs_p == false;
694
                                            }
695
                                            while(!test.isAccepted(cond, conds[0],conds[1],conds[2],conds[3]));
696
 
697
                                            TestUnit.Descriptor gs_desc = new TestUnit.Descriptor((int)new_gs_base, (int)new_gs_limit, new_gs_type, new_gs_seg, new_gs_p, new_gs_dpl, new_gs_d_b, new_gs_g, new_gs_l, new_gs_avl);
698
 
699
                                            gs_selector = tables.addDescriptor(is_gs_ldt, gs_desc);
700
                                            if(gs_selector == -1) return false;
701
 
702
                                            gs_selector <<= 3;
703
                                            gs_selector |= new_gs_rpl;
704
                                            if(is_gs_ldt) gs_selector |= 4;
705
                                        }
706
 
707
                                        long gs_offset = new_tss_base + 92;
708
 
709
                                        MemoryPatchLayer gs_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)gs_offset, (gs_selector & 0xFF), ((gs_selector>>8) & 0xFF));
710
                                        test.layers.addFirst(gs_selector_patch);
711
 
712
                                        if(type == 22) {
713
                                            boolean is_cs_ldt = is_new_ldt_null == false && random.nextBoolean();
714
 
715
                                            if(is_cs_ldt) {
716
                                                tables.setup_new_ldt((int)new_ldt_base, (int)new_ldt_limit);
717
                                            }
718
 
719
                                            new_cs_selector = tables.getOutOfBoundsIndex(is_cs_ldt);
720
                                            if(new_cs_selector == -1) return false;
721
 
722
                                            new_cs_selector <<= 3;
723
                                            new_cs_selector |= (is_cs_ldt)? 4 : 0;
724
                                            new_cs_selector |= new_cs_rpl;
725
 
726
                                            MemoryPatchLayer cs_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)cs_offset, (new_cs_selector & 0xFF), ((new_cs_selector>>8) & 0xFF));
727
                                            test.layers.addFirst(cs_selector_patch);
728
                                        }
729
                                        else if(type >= 23) {
730
                                            boolean is_cs_ldt = is_new_ldt_null == false && random.nextBoolean();
731
 
732
                                            if(is_cs_ldt) {
733
                                                tables.setup_new_ldt((int)new_ldt_base, (int)new_ldt_limit);
734
                                            }
735
 
736
                                            conds= new boolean[5];
737
                                            cond = (type == 23)? 1 << random.nextInt(conds.length) : 0;
738
 
739
                                            boolean new_cs_seg  = false;
740
                                            int     new_cs_type = 0;
741
                                            boolean new_cs_p    = false;
742
                                            int     new_cs_dpl  = 0;
743
 
744
                                            boolean new_cs_d_b  = random.nextBoolean();
745
                                            boolean new_cs_l    = random.nextBoolean();
746
                                            boolean new_cs_avl  = random.nextBoolean();
747
 
748
                                            if((cond & 8) != 0 && new_cs_rpl == 3) return false;
749
 
750
                                            do {
751
                                                new_cs_seg = random.nextBoolean();
752
                                                new_cs_type= random.nextInt(16);
753
                                                new_cs_p   = random.nextBoolean();
754
                                                new_cs_dpl = random.nextInt(4);
755
 
756
                                                conds[0] = new_cs_seg == false;
757
                                                conds[1] = (new_cs_type >> 3) == 0; // data segment
758
                                                conds[2] = (new_cs_type >> 3) == 1 && ((new_cs_type >> 2)&1) == 0 && new_cs_rpl != new_cs_dpl; // (code non conforming)
759
                                                conds[3] = (new_cs_type >> 3) == 1 && ((new_cs_type >> 2)&1) == 1 && new_cs_dpl > new_cs_rpl;  // (code conforming)
760
                                                conds[4] = new_cs_p == false;
761
                                            }
762
                                            while(!test.isAccepted(cond, conds[0],conds[1],conds[2],conds[3],conds[4]));
763
 
764
                                            long new_cs_base, new_cs_limit;
765
                                            boolean new_cs_g;
766
                                            while(true) {
767
                                                new_cs_base = Layer.norm(random.nextInt());
768
                                                new_cs_g    = random.nextBoolean();
769
 
770
                                                new_cs_limit = random.nextInt(new_cs_g? 0xF+1 : 0xFFFF + 1);
771
                                                if(new_cs_g) new_cs_limit = (new_cs_limit << 12) | 0xFFF;
772
 
773
                                                if( new_cs_base + new_cs_limit < 4294967296L &&
774
                                                    Layer.collides(prohibited_list, (int)new_cs_base, (int)(new_cs_base + new_cs_limit)) == false )
775
                                                {
776
                                                    prohibited_list.add(new Pair<>(new_cs_base, new_cs_base + new_cs_limit));
777
                                                    break;
778
                                                }
779
                                            }
780
                                            long new_cs_limit_final = new_cs_g? new_cs_limit >> 12 : new_cs_limit;
781
 
782
 
783
                                            TestUnit.Descriptor cs_desc = new TestUnit.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);
784
 
785
                                            int cs_selector = tables.addDescriptor(is_cs_ldt, cs_desc);
786
                                            if(cs_selector == -1) return false;
787
 
788
                                            cs_selector <<= 3;
789
                                            cs_selector |= new_cs_rpl;
790
                                            if(is_cs_ldt) cs_selector |= 4;
791
 
792
                                            MemoryPatchLayer cs_selector_patch = new MemoryPatchLayer(random, prohibited_list, (int)cs_offset, (cs_selector & 0xFF), ((cs_selector>>8) & 0xFF));
793
                                            test.layers.addFirst(cs_selector_patch);
794
 
795
                                            if(type >= 24) {
796
                                                // set eip
797
                                                long eip_offset = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? 14 : 32);
798
 
799
                                                long eip = random.nextInt((int)new_cs_limit_final+1);
800
                                                if(eip >= 65536 && new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1)) eip = random.nextInt(65536);
801
 
802
                                                if(type == 24) eip = new_cs_limit_final + 1 + random.nextInt(5);
803
 
804
                                                if(new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1)) {
805
                                                    MemoryPatchLayer eip_patch = new MemoryPatchLayer(random, prohibited_list, (int)eip_offset,
806
                                                            (int)(eip & 0xFF), (int)((eip>>8) & 0xFF));
807
                                                    test.layers.addFirst(eip_patch);
808
                                                }
809
                                                else {
810
                                                    MemoryPatchLayer eip_patch = new MemoryPatchLayer(random, prohibited_list, (int)eip_offset,
811
                                                            (int)(eip & 0xFF), (int)((eip>>8) & 0xFF), (int)((eip>>16) & 0xFF), (int)((eip>>24) & 0xFF));
812
                                                    test.layers.addFirst(eip_patch);
813
                                                }
814
 
815
                                                MemoryPatchLayer instr_patch = new MemoryPatchLayer(random, prohibited_list, (int)(new_cs_base + eip),
816
                                                    0x0F, 0x0F, 0x0F);
817
                                                test.layers.addFirst(instr_patch);
818
 
819
                                                int debug_flag = (type == 26)? 1 : 0;
820
 
821
                                                long debug_offset = new_tss_base + ((new_tss_type == (source == Source.FROM_IRET? 0x3 : 0x1))? -1 : 100);
822
                                                if(debug_offset >= 0) {
823
                                                    MemoryPatchLayer trap_patch = new MemoryPatchLayer(random, prohibited_list, (int)debug_offset,
824
                                                            (int)(debug_flag & 0xFF), (int)((debug_flag>>8) & 0xFF));
825
                                                    test.layers.addFirst(trap_patch);
826
                                                }
827
 
828
                                            }
829
                                        }
830
                                    }
831
                                }
832
                            }
833
                        }
834
                        else throw new Exception("Invalid type");
835
 
836
                    }
837
                    else throw new Exception("Invalid type");
838
                }
839
                else throw new Exception("Invalid type");
840
            }
841
            else throw new Exception("Invalid type");
842
        }
843
        return true;
844
    }
845
}

powered by: WebSVN 2.1.0

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