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

Subversion Repositories ao486

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

powered by: WebSVN 2.1.0

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