| 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.transfer_data;
 | 
      
         | 28 |  |  |  
 | 
      
         | 29 |  |  | import ao486.test.TestUnit;
 | 
      
         | 30 |  |  | import static ao486.test.TestUnit.run_test;
 | 
      
         | 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.OtherLayer;
 | 
      
         | 39 |  |  | import ao486.test.layers.Pair;
 | 
      
         | 40 |  |  | import ao486.test.layers.SegmentLayer;
 | 
      
         | 41 |  |  | import ao486.test.layers.StackLayer;
 | 
      
         | 42 |  |  | import java.io.Serializable;
 | 
      
         | 43 |  |  | import java.util.LinkedList;
 | 
      
         | 44 |  |  | import java.util.Random;
 | 
      
         | 45 |  |  |  
 | 
      
         | 46 |  |  | public class TestSLDT_STR_modregrm extends TestUnit implements Serializable {
 | 
      
         | 47 |  |  |     public static void main(String args[]) throws Exception {
 | 
      
         | 48 |  |  |         run_test(TestSLDT_STR_modregrm.class);
 | 
      
         | 49 |  |  |     }
 | 
      
         | 50 |  |  |  
 | 
      
         | 51 |  |  |     //--------------------------------------------------------------------------
 | 
      
         | 52 |  |  |     @Override
 | 
      
         | 53 |  |  |     public int get_test_count() throws Exception {
 | 
      
         | 54 |  |  |         return 100;
 | 
      
         | 55 |  |  |     }
 | 
      
         | 56 |  |  |  
 | 
      
         | 57 |  |  |     @Override
 | 
      
         | 58 |  |  |     public void init() throws Exception {
 | 
      
         | 59 |  |  |         random = new Random(5 + index);
 | 
      
         | 60 |  |  |  
 | 
      
         | 61 |  |  |         long rpl = random.nextInt(4);
 | 
      
         | 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 |  |  |             layers.add(new StackLayer(random, prohibited_list));
 | 
      
         | 72 |  |  |             layers.add(new OtherLayer(OtherLayer.Type.RANDOM, random));
 | 
      
         | 73 |  |  |             layers.add(new FlagsLayer(FlagsLayer.Type.RANDOM, random));
 | 
      
         | 74 |  |  |             layers.add(new GeneralRegisterLayer(random));
 | 
      
         | 75 |  |  |             layers.add(new SegmentLayer(random));
 | 
      
         | 76 |  |  |             layers.add(new MemoryLayer(random));
 | 
      
         | 77 |  |  |             layers.add(new IOLayer(random));
 | 
      
         | 78 |  |  |             layers.addFirst(new HandleModeChangeLayer(
 | 
      
         | 79 |  |  |                     getInput("cr0_pe"),
 | 
      
         | 80 |  |  |                     getInput("vmflag"),
 | 
      
         | 81 |  |  |                     rpl, //getInput("cs_rpl"),
 | 
      
         | 82 |  |  |                     getInput("cs_p"),
 | 
      
         | 83 |  |  |                     getInput("cs_s"),
 | 
      
         | 84 |  |  |                     getInput("cs_type")
 | 
      
         | 85 |  |  |             ));
 | 
      
         | 86 |  |  |  
 | 
      
         | 87 |  |  |             // instruction size
 | 
      
         | 88 |  |  |             boolean cs_d_b = getInput("cs_d_b") == 1;
 | 
      
         | 89 |  |  |  
 | 
      
         | 90 |  |  |             boolean a32 = random.nextBoolean();
 | 
      
         | 91 |  |  |             boolean o32 = random.nextBoolean();
 | 
      
         | 92 |  |  |  
 | 
      
         | 93 |  |  |             final Random random_final = random;
 | 
      
         | 94 |  |  |             layers.addFirst(new Layer() {
 | 
      
         | 95 |  |  |                 long ldtr() { return random_final.nextInt() & 0xFFFFL; }
 | 
      
         | 96 |  |  |  
 | 
      
         | 97 |  |  |                 long tr() { return random_final.nextInt() & 0xFFFFL; }
 | 
      
         | 98 |  |  |             });
 | 
      
         | 99 |  |  |  
 | 
      
         | 100 |  |  |             // instruction
 | 
      
         | 101 |  |  |             instruction = prepare_instr(cs_d_b, a32, o32, null);
 | 
      
         | 102 |  |  |  
 | 
      
         | 103 |  |  |             instruction += instruction;
 | 
      
         | 104 |  |  |             instruction += "0F0F";
 | 
      
         | 105 |  |  |  
 | 
      
         | 106 |  |  |             // add instruction
 | 
      
         | 107 |  |  |             instr.add_instruction(instruction);
 | 
      
         | 108 |  |  |  
 | 
      
         | 109 |  |  |             // end condition
 | 
      
         | 110 |  |  |             break;
 | 
      
         | 111 |  |  |         }
 | 
      
         | 112 |  |  |  
 | 
      
         | 113 |  |  |         System.out.println("Instruction: [" + instruction + "]");
 | 
      
         | 114 |  |  |     }
 | 
      
         | 115 |  |  |  
 | 
      
         | 116 |  |  |     String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte modregrm_bytes[]) throws Exception {
 | 
      
         | 117 |  |  |  
 | 
      
         | 118 |  |  |         int opcodes[] = {
 | 
      
         | 119 |  |  |             0x00
 | 
      
         | 120 |  |  |         };
 | 
      
         | 121 |  |  |  
 | 
      
         | 122 |  |  |         String prefix = "";
 | 
      
         | 123 |  |  |         if(cs_d_b != o32) { prefix = "66" + prefix; }
 | 
      
         | 124 |  |  |         if(cs_d_b != a32) { prefix = "67" + prefix; }
 | 
      
         | 125 |  |  |  
 | 
      
         | 126 |  |  |         int opcode = opcodes[random.nextInt(opcodes.length)];
 | 
      
         | 127 |  |  |         boolean is_modregrm = true;
 | 
      
         | 128 |  |  |  
 | 
      
         | 129 |  |  |         byte possible_modregrm = (byte)random.nextInt();
 | 
      
         | 130 |  |  |         if(random.nextBoolean())    possible_modregrm = (byte)((possible_modregrm & 0xC7) | (0 << 3));
 | 
      
         | 131 |  |  |         else                        possible_modregrm = (byte)((possible_modregrm & 0xC7) | (1 << 3));
 | 
      
         | 132 |  |  |  
 | 
      
         | 133 |  |  |         byte possible_sib      = (byte)random.nextInt();
 | 
      
         | 134 |  |  |  
 | 
      
         | 135 |  |  |         int len = (is_modregrm == false)? 1 : 1 + modregrm_len(!a32, unsigned(possible_modregrm), unsigned(possible_sib));
 | 
      
         | 136 |  |  |  
 | 
      
         | 137 |  |  |         byte instr[] = new byte[len];
 | 
      
         | 138 |  |  |         instr[0] = (byte)opcode;
 | 
      
         | 139 |  |  |         for(int i=1; i<len; i++) {
 | 
      
         | 140 |  |  |             if(i==1)        instr[1] = possible_modregrm;
 | 
      
         | 141 |  |  |             else if(i==2)   instr[2] = possible_sib;
 | 
      
         | 142 |  |  |             else            instr[i] = (byte)random.nextInt();
 | 
      
         | 143 |  |  |         }
 | 
      
         | 144 |  |  |  
 | 
      
         | 145 |  |  |         return prefix + "0F" + bytesToHex(instr);
 | 
      
         | 146 |  |  |     }
 | 
      
         | 147 |  |  | }
 |