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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [branch/] [TestJMP_near_Jv.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.FlagsLayer;
31
import ao486.test.layers.GeneralRegisterLayer;
32
import ao486.test.layers.HandleModeChangeLayer;
33
import ao486.test.layers.IOLayer;
34
import ao486.test.layers.InstructionLayer;
35
import ao486.test.layers.Layer;
36
import ao486.test.layers.MemoryLayer;
37
import ao486.test.layers.MemoryPatchLayer;
38
import ao486.test.layers.OtherLayer;
39
import ao486.test.layers.Pair;
40
import ao486.test.layers.SegmentLayer;
41
import ao486.test.layers.StackLayer;
42
import java.io.*;
43
import java.util.LinkedList;
44
import java.util.Random;
45
 
46
 
47
public class TestJMP_near_Jv extends TestUnit implements Serializable {
48
    public static void main(String args[]) throws Exception {
49
        run_test(TestJMP_near_Jv.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(1 + 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.RANDOM, random));
74
            layers.add(new FlagsLayer(FlagsLayer.Type.RANDOM, 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
            /* 0 - eip out of bounds
89
             * 1 - all ok
90
             */
91
 
92
            //int type = 0;
93
 
94
            // instruction size
95
            boolean cs_d_b = getInput("cs_d_b") == 1;
96
 
97
            boolean a32 = random.nextBoolean();
98
            boolean o32 = random.nextBoolean();
99
 
100
            // destination
101
            long cs_limit = getInput("cs_limit");
102
            long cs_base  = Layer.norm(getInput("cs_base"));
103
            long eip      = Layer.norm(getInput("eip"));
104
 
105
            boolean is8bit = random.nextBoolean();
106
 
107
            long min = (is8bit == false)? (o32 == false)? eip - 32768 + 5 : 0 : eip - 128 + 5;
108
            if(min < 0) min = 0;
109
 
110
            long max = (is8bit == false)? (o32 == false)? eip + 32767 - 5 : cs_limit : eip + 127 - 5;
111
            if(max > cs_limit) max = cs_limit;
112
 
113
            long new_eip  = min + random.nextInt((int)(max - min + 1));
114
 
115
            //if(type == 0 && max == cs_limit) new_eip = cs_limit + random.nextInt(5);
116
 
117
            if(o32 == false) new_eip &= 0xFFFF;
118
            if(o32 == false) eip &= 0xFFFF;
119
 
120
            long eip_diff = new_eip - (eip + ((is8bit)? 2 : (o32)? 5 : 3) + ((cs_d_b != o32)? 1 : 0) + ((cs_d_b != a32)? 1 : 0));
121
            if(o32 == false) eip_diff &= 0xFFFF;
122
 
123
            long eip_diff_abs = (eip_diff < 0)? -eip_diff : eip_diff;
124
            if(eip_diff_abs < 10) continue;
125
 
126
            // instruction after call
127
            long dest = cs_base + new_eip;
128
 
129
            MemoryPatchLayer patch = new MemoryPatchLayer(random, prohibited_list, (int)dest, 0x0F,0x0F);
130
            layers.addFirst(patch);
131
 
132
            // add instruction
133
 
134
            instruction = prepare_instr(cs_d_b, a32, o32, is8bit, (int)eip_diff);
135
            instr.add_instruction(instruction);
136
System.out.printf("a32: %b, o32: %b, cs_d_b: %b, is8bit: %b\n", a32,o32,cs_d_b,is8bit);
137
System.out.printf("dist(dec):%d\n", eip_diff);
138
System.out.printf("min:      %08x\n", min);
139
System.out.printf("max:      %08x\n", max);
140
System.out.printf("eip:      %08x\n", eip);
141
System.out.printf("eip_orig: %08x\n", Layer.norm(getInput("eip")));
142
System.out.printf("dest:     %08x\n", dest);
143
System.out.printf("new_eip:  %08x\n", new_eip);
144
System.out.printf("cs_base:  %08x\n", cs_base);
145
System.out.printf("cs_limit: %08x\n", cs_limit);
146
            // end condition
147
            break;
148
        }
149
 
150
        System.out.println("Instruction: [" + instruction + "]");
151
    }
152
 
153
    String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, boolean is8bit, int offset) throws Exception {
154
        int opcodes[] = {
155
            0xE9,0xEB
156
        };
157
 
158
        String prefix = "";
159
        if(cs_d_b != o32) { prefix = "66" + prefix; }
160
        if(cs_d_b != a32) { prefix = "67" + prefix; }
161
 
162
        int opcode = opcodes[is8bit? 1 : 0];
163
 
164
        byte instr[] = new byte[1 + ((is8bit)? 1 : (o32)? 4 : 2)];
165
        instr[0] = (byte)opcode;
166
        for(int i=1; i<instr.length; i++) {
167
            instr[i] = (byte)(offset & 0xFF);
168
            offset >>= 8;
169
        }
170
 
171
        return prefix + bytesToHex(instr);
172
    }
173
 
174
}

powered by: WebSVN 2.1.0

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