| 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.string;
|
| 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 TestCMPS_rep_any_interrupt extends TestUnit implements Serializable {
|
| 48 |
|
|
public static void main(String args[]) throws Exception {
|
| 49 |
|
|
run_test(TestCMPS_rep_any_interrupt.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(136 + index);
|
| 62 |
|
|
|
| 63 |
|
|
String instruction;
|
| 64 |
|
|
while(true) {
|
| 65 |
|
|
layers.clear();
|
| 66 |
|
|
|
| 67 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
| 68 |
|
|
|
| 69 |
|
|
// if false: v8086 mode
|
| 70 |
|
|
boolean is_real = true;
|
| 71 |
|
|
|
| 72 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list, true);
|
| 73 |
|
|
layers.add(instr);
|
| 74 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
| 75 |
|
|
layers.add(stack);
|
| 76 |
|
|
layers.add(new OtherLayer(is_real ? OtherLayer.Type.REAL : OtherLayer.Type.PROTECTED_OR_V8086, random));
|
| 77 |
|
|
layers.add(new FlagsLayer(is_real ? FlagsLayer.Type.RANDOM : FlagsLayer.Type.V8086, random));
|
| 78 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
| 79 |
|
|
layers.add(new SegmentLayer(random));
|
| 80 |
|
|
layers.add(new MemoryLayer(random, random.nextBoolean()));
|
| 81 |
|
|
layers.add(new IOLayer(random));
|
| 82 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
| 83 |
|
|
getInput("cr0_pe"),
|
| 84 |
|
|
getInput("vmflag"),
|
| 85 |
|
|
getInput("cs_rpl"),
|
| 86 |
|
|
getInput("cs_p"),
|
| 87 |
|
|
getInput("cs_s"),
|
| 88 |
|
|
getInput("cs_type")
|
| 89 |
|
|
));
|
| 90 |
|
|
|
| 91 |
|
|
// instruction size
|
| 92 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
| 93 |
|
|
|
| 94 |
|
|
boolean a32 = random.nextBoolean();
|
| 95 |
|
|
boolean o32 = random.nextBoolean();
|
| 96 |
|
|
|
| 97 |
|
|
long cs_limit = getInput("cs_limit");
|
| 98 |
|
|
|
| 99 |
|
|
|
| 100 |
|
|
/* 0 - no rep; no interrupt
|
| 101 |
|
|
* 1 - no rep; interrupt on 0
|
| 102 |
|
|
* 2 - no rep; no interrupt; ecx = 0
|
| 103 |
|
|
* 3 - rep; no interrupt; ecx = 0
|
| 104 |
|
|
* 4 - rep; interrupt on 0
|
| 105 |
|
|
* 5 - rep; interrupt on 8
|
| 106 |
|
|
* 6 - rep; interrupt on 9
|
| 107 |
|
|
*/
|
| 108 |
|
|
int type = random.nextInt(7);
|
| 109 |
|
|
|
| 110 |
|
|
int interrupt_position = -1;
|
| 111 |
|
|
if(type == 0) {
|
| 112 |
|
|
interrupt_position = -1;
|
| 113 |
|
|
}
|
| 114 |
|
|
else if(type == 1) {
|
| 115 |
|
|
interrupt_position = 0;
|
| 116 |
|
|
}
|
| 117 |
|
|
else if(type == 2) {
|
| 118 |
|
|
interrupt_position = -1;
|
| 119 |
|
|
}
|
| 120 |
|
|
else if(type == 3) {
|
| 121 |
|
|
interrupt_position = -1;
|
| 122 |
|
|
}
|
| 123 |
|
|
else if(type == 4) {
|
| 124 |
|
|
interrupt_position = 0;
|
| 125 |
|
|
}
|
| 126 |
|
|
else if(type == 5) {
|
| 127 |
|
|
interrupt_position = 8;
|
| 128 |
|
|
}
|
| 129 |
|
|
else if(type == 6) {
|
| 130 |
|
|
interrupt_position = 9;
|
| 131 |
|
|
}
|
| 132 |
|
|
|
| 133 |
|
|
final int interrupt_position_final = interrupt_position;
|
| 134 |
|
|
|
| 135 |
|
|
final int test_type = (type == 0)? 3 : (type == 1 || type == 2 || type == 3)? 4 : 12;
|
| 136 |
|
|
final int ecx = (type == 2 || type == 3)? 0 : 10;
|
| 137 |
|
|
Layer esp_layer = new Layer() {
|
| 138 |
|
|
long esp() { return 0xF0; }
|
| 139 |
|
|
long ss_base() { return 0; }
|
| 140 |
|
|
long iflag() { return 1; }
|
| 141 |
|
|
long tflag() { return 0; }
|
| 142 |
|
|
|
| 143 |
|
|
long cs_d_b() { return 0; }
|
| 144 |
|
|
|
| 145 |
|
|
long ecx() { return ecx; }
|
| 146 |
|
|
|
| 147 |
|
|
long check_interrupt(long counter) { return counter == interrupt_position_final? 0xAB : 0x100; }
|
| 148 |
|
|
|
| 149 |
|
|
long get_test_type() { return test_type; }
|
| 150 |
|
|
};
|
| 151 |
|
|
layers.addFirst(esp_layer);
|
| 152 |
|
|
|
| 153 |
|
|
long handler = cs_limit;
|
| 154 |
|
|
|
| 155 |
|
|
//0xAB -- interrupt vector = 0xAB*4 = 0x2AC linear
|
| 156 |
|
|
MemoryPatchLayer int_patch = new MemoryPatchLayer(random, prohibited_list, (int)(0xAB*4), (byte)(handler&0xFF),(byte)((handler>>8)&0xFF), 0x00,0x00);
|
| 157 |
|
|
layers.addFirst(int_patch);
|
| 158 |
|
|
|
| 159 |
|
|
//interrupt handler: simply IRET
|
| 160 |
|
|
MemoryPatchLayer handler_patch = new MemoryPatchLayer(random, prohibited_list, (int)handler, 0xCF);
|
| 161 |
|
|
layers.addFirst(handler_patch);
|
| 162 |
|
|
|
| 163 |
|
|
// byte or word/double word
|
| 164 |
|
|
String instr_string = random.nextBoolean()? "A6" : "A7"; // CMPS
|
| 165 |
|
|
String rep_string = (type == 0 || type == 1 || type == 2)? "" : random.nextBoolean()? "F2" : "F3";
|
| 166 |
|
|
|
| 167 |
|
|
// add instruction
|
| 168 |
|
|
instruction = rep_string + instr_string + "90909090909090909090909090909090";
|
| 169 |
|
|
|
| 170 |
|
|
instr.add_instruction(instruction);
|
| 171 |
|
|
|
| 172 |
|
|
// end condition
|
| 173 |
|
|
break;
|
| 174 |
|
|
}
|
| 175 |
|
|
|
| 176 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
| 177 |
|
|
}
|
| 178 |
|
|
}
|