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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [branch/] [TestJMP_near_Ev.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.EffectiveAddressLayerFactory;
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 TestJMP_near_Ev extends TestUnit implements Serializable {
49
    public static void main(String args[]) throws Exception {
50
        run_test(TestJMP_near_Ev.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(3 + 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(OtherLayer.Type.RANDOM, random));
75
            layers.add(new FlagsLayer(FlagsLayer.Type.RANDOM, 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
            /* 0 - eip out of bounds
90
             *
91
             * 1 - all ok
92
             */
93
 
94
            int type = 1;
95
 
96
            // instruction size
97
            boolean cs_d_b = getInput("cs_d_b") == 1;
98
 
99
            boolean a32 = random.nextBoolean();
100
            boolean o32 = random.nextBoolean();
101
 
102
            // destination
103
            long cs_limit = getInput("cs_limit");
104
            long cs_base  = Layer.norm(getInput("cs_base"));
105
            long new_eip  = random.nextInt((int)cs_limit);
106
 
107
            if(o32 == false) new_eip &= 0xFFFF;
108
 
109
            if(type == 0 && o32 == false && cs_limit <= 0xFFFF) new_eip = 0xFFFF - 5 + random.nextInt(5);
110
            if(type == 0 && o32 == true)                        new_eip = cs_limit + random.nextInt(5);
111
 
112
            byte modregrm_bytes[] = EffectiveAddressLayerFactory.prepare(
113
                    new_eip,
114
                    4, EffectiveAddressLayerFactory.modregrm_reg_t.SET,
115
                    o32? 4 : 2, a32,
116
                    layers, random, this, false, false);
117
 
118
            long dest = cs_base + new_eip;
119
 
120
            MemoryPatchLayer patch = new MemoryPatchLayer(random, prohibited_list, (int)dest, 0x0F,0x0F);
121
            layers.addFirst(patch);
122
 
123
            // add instruction
124
 
125
            instruction = prepare_instr(cs_d_b, a32, o32, modregrm_bytes);
126
            instr.add_instruction(instruction);
127
System.out.printf("a32: %b, o32: %b, cs_d_b: %b\n", a32,o32,cs_d_b);
128
System.out.printf("dest:     %08x\n", dest);
129
System.out.printf("new_eip:  %08x\n", new_eip);
130
System.out.printf("cs_base:  %08x\n", cs_base);
131
System.out.printf("cs_limit: %08x\n", cs_limit);
132
            // end condition
133
            break;
134
        }
135
 
136
        System.out.println("Instruction: [" + instruction + "]");
137
    }
138
 
139
    String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte modregrm_bytes[]) throws Exception {
140
        int opcodes[] = {
141
            0xFF
142
        };
143
 
144
        String prefix = "";
145
        if(cs_d_b != o32) { prefix = "66" + prefix; }
146
        if(cs_d_b != a32) { prefix = "67" + prefix; }
147
 
148
        int opcode = opcodes[random.nextInt(opcodes.length)];
149
 
150
        byte instr[] = new byte[1 + modregrm_bytes.length];
151
        instr[0] = (byte)opcode;
152
        System.arraycopy(modregrm_bytes, 0, instr, 1, modregrm_bytes.length);
153
 
154
        return prefix + bytesToHex(instr);
155
    }
156
 
157
}

powered by: WebSVN 2.1.0

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