def one_iteration(self,i): m,sego,s,b = rnd_bool(),rnd_bool(),rnd_bool(),rnd_bool() aop = self.random_aop() print "%d %s:" % (i,aop), opnd = self.rog.gen(aop,(m,sego,s,b)) print "%r %s" % (opnd,opnd)
def one_iteration(self): # Choose base register, index register and scale factor, and displacement. b = rnd_reg32() if rnd_bool() else None sr,sf = (rnd_reg32_noesp(),rnd_scale()) if rnd_bool() else (None,0) d = rnd_displ(not (b == None and sr == None),0xFFFFFFFF) if self.verbose: print "Testing base = %s, scale register = %s, scale factor = %s, displacement = %s" % (b,sr,sf,d) # Make a ModRM32, encode our random choices by parts, and then call # Interpret to get back the constitutent components. m = ModRM32() m.EncodeFromParts(b,sr,sf,d) nb,nsr,nsf,nd,_ = m.Interpret() bFailed = False if nb is not None: self.assertIsInstance(nb,R32Elt,"%s(%r) should have been a 32-bit register, not type %s" % (nb,nb,type(nb))) bFailed |= not(isinstance(nb,R32Elt)) self.assertEqual(b, nb, "Chose base register %s, retrieved %s" % (b, nb)) bFailed |= b != nb if sr is not None: self.assertIsInstance(sr,R32Elt,"%s(%r) should have been a 32-bit register, not type %s" % (sr,sr,type(sr))) bFailed |= not(isinstance(nsr,R32Elt)) self.assertEqual(sr,nsr,"Chose index register %s, retrieved %s" % (sr,nsr)) bFailed |= sr != nsr if sf is not None or sf != 0: self.assertIsInstance(sf,(int,long),"%s(%r) should have been an integer, not type %s" % (sf,sf,type(sf))) bFailed |= not(isinstance(sf,(int,long))) self.assertEqual(sf,nsf,"Chose scale factor %s, retrieved %s" % (sf,nsf)) bFailed |= sf != nsf if d is not None: if nd is None: self.assertTrue(d==0,"Chose displacement %#lx, retrieved None" % d) else: self.assertEqual(d,nd,"Chose displacement %#lx, retrieved %#lx" % (d,nd)) elif nd is not None: self.assertIsInstance(nd,(int,long),"%s(%r) should have been an integer, not type %s" % (nd,nd,type(nd))) bFailed |= not(isinstance(nd,(int,long))) self.assertTrue(nd == 0,"Chose no displacement, retrieved %#lx" % nd) bFailed |= nd != 0 # GGG must be set in order to encode into bytes. m.GGG = 7 bytes = m.Encode() # Make a new ModRM32, and populate it by decoding the encoded version of # the previous one. dm = ModRM32() dm.Decode(StreamObj(bytes)) db,dsr,dsf,dd,_ = dm.Interpret() # Ensure that we got back something roughly equal. if db is not None: self.assertIsInstance(db,R32Elt,"Interpret: %s(%r) should have been a 32-bit register, not type %s" % (db,db,type(db))) bFailed |= not(isinstance(db,R32Elt)) self.assertEqual(db,nb, "Decoded base register %s, encoded %s" % (b, nb)) bFailed |= db != nb if dsr is not None: self.assertIsInstance(dsr,R32Elt,"Interpret: %s(%r) should have been a 32-bit register, not type %s" % (dsr,dsr,type(dsr))) bFailed |= not(isinstance(dsr,R32Elt)) self.assertEqual(dsr,nsr,"Decoded index register %s, encoded %s" % (sr,nsr)) bFailed |= dsr != nsr if nsf is not None or nsf != 0: self.assertIsInstance(nsf,(int,long),"Interpret: %s(%r) should have been an integer, not type %s" % (nsf,nsf,type(nsf))) self.assertEqual(sf,nsf, "Decoded scale factor %s, retrieved %s" % (sf,nsf)) if dd is not None: if nd is None: self.assertTrue(dd==0,"Decoded displacement %#lx, encoded None" % dd) bFailed |= dd != 0 else: self.assertEqual(dd,nd,"Decoded displacement %#lx, encoded %#lx" % (dd,nd)) bFailed |= dd != nd elif nd is not None: self.assertIsInstance(nd,(int,long),"Interpret: %s(%r) should have been an integer, not type %s" % (nd,nd,type(nd))) bFailed |= not(isinstance(nd,(int,long))) self.assertTrue(nd == 0,"Chose no displacement, retrieved %#lx" % nd) bFailed |= nd != 0 if self.verbose and not bFailed: print "TestModRM32: iteration [%s+%s*%d+%s] passed!" % (b,sr,(1<<sf),d)
def one_iteration(self): m, s, a = rnd_bool(), rnd_bool(), rnd_bool() aop = AOTElt(random.randint(0, X86_INTERNAL_OPERAND_LAST)) opnd = self.rog.gen(aop, (m, s, a)) res = self.tc.check(aop, opnd) self.do_test(aop, opnd, res, (m, s, a))
def one_iteration(self): # Choose base and scale registers, and displacement b, sr = random.choice(modrm_16) if rnd_bool() else (None, None) d = rnd_displ(not (b == None and sr == None), 0xFFFF) # Make a ModRM16, encode our random choices by parts, and then call # Interpret to get back the constitutent components. m = ModRM16() m.EncodeFromParts(b, sr, d) nb, nsr, nd, _ = m.Interpret() # Test to make sure we got back roughly what we put in. if nb is not None: self.assertIsInstance( nb, R16Elt, "%s(%r) should have been a 16-bit register, not type %s" % (nb, nb, type(nb))) self.assertEqual(b, nb, "Chose base register %s, retrieved %s" % (b, nb)) if sr is not None: self.assertIsInstance( sr, R16Elt, "%s(%r) should have been a 16-bit register, not type %s" % (sr, sr, type(sr))) self.assertEqual(sr, nsr, "Chose index register %s, retrieved %s" % (sr, nsr)) if d is not None: if nd is None: self.assertTrue(d == 0, "Chose displacement %#lx, retrieved None" % d) else: self.assertEqual( d, nd, "Chose displacement %#lx, retrieved %#lx" % (d, nd)) elif nd is not None: self.assertIsInstance( nd, (int, long), "%s(%r) should have been an integer, not type %s" % (nd, nd, type(nd))) self.assertTrue(nd == 0, "Chose no displacement, retrieved %#lx" % nd) # GGG must be set in order to encode into bytes. m.GGG = 7 bytes = m.Encode() # Make a new ModRM16, and populate it by decoding the encoded version of # the previous one. m = ModRM16() m.Decode(StreamObj(bytes)) db, dsr, dd, _ = m.Interpret() # Ensure that we got back something roughly equal. if db is not None: self.assertIsInstance( db, R16Elt, "Interpret: %s(%r) should have been a 16-bit register, not type %s" % (db, db, type(db))) self.assertEqual(db, nb, "Decoded base register %s, encoded %s" % (b, nb)) if dsr is not None: self.assertIsInstance( dsr, R16Elt, "Interpret: %s(%r) should have been a 32-bit register, not type %s" % (dsr, dsr, type(dsr))) self.assertEqual(dsr, nsr, "Decoded index register %s, encoded %s" % (sr, nsr)) if dd is not None: if nd is None: self.assertTrue(dd == 0, "Decoded displacement %#lx, encoded None" % dd) else: self.assertEqual( dd, nd, "Decoded displacement %#lx, encoded %#lx" % (dd, nd)) elif nd is not None: self.assertIsInstance( nd, (int, long), "Interpret: %s(%r) should have been an integer, not type %s" % (nd, nd, type(nd))) self.assertTrue(nd == 0, "Chose no displacement, retrieved %#lx" % nd)