def genInstrs(opkind, ropts, limit, ctx): #print opkind, ropts, limit maxretry = options.getOption('maxretry') cnt = 0 retry = 0 inst = [] isa = ctx[options.CTX.ISA] while cnt < limit: gen_it = False if not isa: if limit > 5: r = random.randint(0, 100) if r < options.getOption('per_it'): gen_it = True if gen_it: it_num = random.randint(1, 4) ret = genThumbIT(opkind, ropts, ctx, it_num) else: if opkind: ropkind = random.choice(opkind) else: ropkind = None rcond, rsbit, rsrc_reg, rdst_reg = extOptions(ropts) ret = ctx['rit'].gen_single_inst_with_opkind(isa, ropkind, rcond, rsbit, rsrc_reg, rdst_reg) if ret: inst += ret cnt += len(ret) retry = 0 else: retry += 1 if retry >= maxretry: cnt += 1 retry = 0 return inst
def getBlockLimit(block, ctx): bb_num = len(ctx[CTX.BB].keys()) if bb_num < 10: min_val = options.getOption(CTX.NUM) / bb_num max_val = options.getOption(CTX.INSTLIMIT) + min_val else: min_val = 10 max_val = options.getOption(CTX.INSTLIMIT)/2 + min_val if block.label in ctx[CTX.BB]: if CTX.INSTLIMIT in ctx[CTX.BB][block.label]: max_val = ctx[CTX.BB][block.label][CTX.INSTLIMIT] return random.randint(min_val, max_val)
def main(): options.parseOptions() #init random seed opt_seed = options.getOption('seed') if opt_seed: random.seed(opt_seed) else: random.seed() ctx = options.getInitContext(rb.getRBlock) tname = options.getOption('output') genTest(ctx, tname) buildTest(tname)
def test_vfp_vector(): # gen_instrs(10) options.parseOptions() ctx = options.getInitContext(getFSM, getRBlock) output = options.getOption("output") exit_b = getVFPExitBlock() testgen.genRandomTest(ctx, output, exitblock=exit_b)
def getEpisodeNumFromFilename(filename): d = None for regexp in getOption('episodesRegexps').split('|||'): p = re.compile('.*%s.*' % regexp) if p.match(filename): d = dict(zip( ('s', 'e'), [ t.zfill(2) for t in p.search(filename).groups()] ) ) if d is not None: return "S%(s)sE%(e)s" % d return None
def getRBlock(label, nest): if nest > 0: rb = [RBType.Basic, RBType.ByPass, RBType.Hammock, RBType.Loop, RBType.IrLoop, RBType.Interwork, RBType.CallRet] if options.getOption('excl_block'): rb.append(RBType.Exclusive) if options.getOption('aluwpc_block'): rb.append(RBType.ALUwritePC) if nest > 3: rb.remove(RBType.Basic) if RBType.Exclusive in rb: rb.remove(RBType.Exclusive) rval = random.choice(rb) if rval is RBType.ByPass: return ByPassRBlock(label) elif rval is RBType.Hammock: return HammockRBlock(label) elif rval is RBType.Loop: return LoopRBlock(label) elif rval is RBType.IrLoop: return IrreducibleLoopRBlock(label) elif rval is RBType.Interwork: return InterworkRBlock(label) elif rval is RBType.CallRet: return CallRetRBlock(label) elif rval is RBType.Exclusive: r = random.randint(0, 100) if r < 30: return ExclsiveRBlock(label) else: return BasicRBlock(label) elif rval is RBType.ALUwritePC: r = random.randint(0, 100) if r < 30: return ALUwritePCRBlock(label) else: return BasicRBlock(label) else: return BasicRBlock(label) else: return BasicRBlock(label)
def get_init_fpscr(): fpscr_val = random.randint(0, 15) << 28 stride = random.choice([0, 3]) if options.getOption('vfp_vector'): if stride: len = random.randint(0, 1) else: len = random.randint(0, 3) fpscr_val |= stride << 20 fpscr_val |= len << 16 else: print "vfp vector mode disabled" #print "0x%x" % fpscr_val return fpscr_val
def genLoadInstrs(reg, ropts, limit, ctx): inst = [] isa = ctx[options.CTX.ISA] allow_fix = True m_lo, m_hi = getMemLimit() if reg == arminfo.REGType.R_SP: mem_op = list(arminfo.STACK_OP) allow_fix = False inst += setReg(arminfo.REGType.R_SP, m_lo, m_hi, isa, True) elif reg == arminfo.REGType.R_0_3: mem_op = list(arminfo.LD_OP) allow_fix = False for i in range(0, 4): inst += setReg(i, m_lo, m_hi, isa, True) inst += setReg(arminfo.REGType.R_SP, m_lo, m_hi, isa, True) elif reg == arminfo.REGType.R_4_7: mem_op = list(arminfo.LD_OP) allow_fix = False for i in range(4, 8): inst += setReg(i, m_lo, m_hi, isa, True) inst += setReg(arminfo.REGType.R_SP, m_lo, m_hi, isa, True) else: mem_op = list(arminfo.LD_OP) if allow_fix or isa: dst_regs = list(arminfo.CANDIDATE_REGS) dst_regs.remove(arminfo.REGType.R_PC) else: dst_regs = [arminfo.REGType.R_0_3, arminfo.REGType.R_4_7] if reg in dst_regs: dst_regs.remove(reg) maxretry = options.getOption('maxretry') cnt = 0 retry = 0 while cnt < limit: rcond, rsbit, _, _ = extOptions(ropts) opkind = random.choice(mem_op) dst_reg = random.choice(dst_regs) ret = ctx['rit'].gen_single_inst_with_opkind(isa, opkind, rcond, rsbit, reg, dst_reg, allow_fix) if ret: inst += ret cnt += len(ret) retry = 0 else: retry+= 1 if retry > maxretry: cnt += 1 retry = 0 return inst
def gen_single_inst_with_opkind(self, isARM, opkind, cond, sbit, src_reg, dst_reg, allow_fix=True): if isARM: isa = 0 else: isa = 1 #if not options.getOption('thumb2'): # isa = 2 output = create_string_buffer(1024) if opkind is None: opkind = random.choice(list(arminfo.ALL_OP)) c_str_opkind = c_char_p(opkind) if cond is None: cond = random.randint(0, 32) if cond > 14: cond = 14 if sbit is None: sbit = random.choice([0, 1]) if src_reg is None: src_reg = 16 if dst_reg is None: dst_reg = 16 retry = options.getOption('maxretry') if opkind == 'VFP_OP': inst = self.vfpgen(isARM, cond, src_reg, dst_reg, allow_fix) #print inst return inst #return self.vfpgen(isARM, cond, src_reg, dst_reg, allow_fix) while retry > 0: hexcode = random.randint(0, 2**32-1) seed = random.randint(0, 2**32-1) #print "<<<call py_gen_instr>>>" self.librit.py_gen_instr(output, isa, c_str_opkind, hexcode, cond, sbit, src_reg, dst_reg, seed) #print "<<<finish py_gen_instr>>>" status, inst = parseRIT(output.value, allow_fix) if status: #arg = '_'.join([str(isa), str(opkind), str(hexcode), str(cond), # str(sbit), str(src_reg), str(dst_reg), str(seed)]) #inst.insert(0, (('#%s' % arg, ''))) #print isa, opkind, hexcode, cond, sbit, src_reg, dst_reg, seed #print inst return inst else: retry -= 1 #print "[WARNING] gen instruction failed with options:" #print isa, opkind, cond, sbit, src_reg, dst_reg return None
def downloadEpisode(episode): search_text = "%s %s" % (episode.season.serie.name, episode.num) url = 'http://thepiratebay.org/search/%s/0/7/0' % urllib.quote(search_text) search = urllib2.urlopen( url ).read() torrents = BeautifulSoup(search).findAll('td') if len(torrents) > 1: torrent = torrents[1] link = BeautifulSoup("<html><body>%s</body></html>" % torrent).findAll('a')[1] link = re.search('href="(.*?)"', str(link)).group(1) if link.rstrip(): download = model.Download() download.torrentFile = link download.episode = episode model.Session.add( download ) fp = open( os.path.join( getOption('download.torrent.folder'), os.path.basename(link) ) , 'w') fp.write( urllib2.urlopen( link ).read() ) fp.close()
def gen_single_inst_with_opcode(self, isARM, opcode, cond, sbit, src_reg, dst_reg, allow_fix=True, seed=None, hexcode=None, invalid=False): #if isARM: # isa = 0 #else: # isa = 1 output = create_string_buffer(1024) if opcode is None: #TODO choose opcode from list opcode = "" if cond is None: cond = random.randint(0, 32) if cond > 14: cond = 14 if sbit is None: sbit = random.choice([0, 1]) if src_reg is None: src_reg = 16 if dst_reg is None: dst_reg = 16 retry = options.getOption('maxretry') #return self.vfpgen(isARM, cond, src_reg, dst_reg, allow_fix) while retry > 0: if hexcode is None: hexcode = random.randint(0, 2**32-1) if seed is None: seed = random.randint(0, 2**32-1) #print "<<<call py_gen_instr_with_op>>>" self.librit.py_gen_instr_with_op(output, opcode, hexcode, cond, sbit, src_reg, dst_reg, seed) #print opcode, hexcode, cond, sbit, src_reg, dst_reg, seed #print "<<<finish py_gen_instr_with_op>>>" status, inst = parseRIT(output.value, allow_fix, isARM, invalid) if status: #print isa, opcode, hexcode, cond, sbit, src_reg, dst_reg, seed #print inst return inst else: retry -= 1 #print "[WARNING] gen instruction failed with options:" #print isa, opcode, cond, sbit, src_reg, dst_reg return None
def genMulShiftInstrs(ropts, limit, ctx): rmul_opkind = list(arminfo.MUL_OPKIND) if ctx[options.CTX.ISA]: rshift_op = list(arminfo.ARM_SHIFT_OP) else: rshift_op = list(arminfo.THUMB_SHIFT_OP) maxretry = options.getOption('maxretry') isa = ctx[options.CTX.ISA] inst = [] cnt = 0 retry = 0 while cnt < limit: gen_mul = random.choice([True, False]) rcond, rsbit, rsrc_reg, rdst_reg = extOptions(ropts) if gen_mul: ropkind = random.choice(rmul_opkind) ret = ctx['rit'].gen_single_inst_with_opkind(isa, ropkind, rcond, rsbit, rsrc_reg, rdst_reg) else: ropcode = random.choice(rshift_op) ret = ctx['rit'].gen_single_inst_with_opcode(isa, ropcode, rcond, rsbit, rsrc_reg, rdst_reg) if ret: inst += ret cnt += 1 retry = 0 else: retry += 1 if retry > maxretry: cnt += 1 retry = 0 #reinit regs with boundary value rval = random.randint(0, 100) if rval > 80: fixinst = reinitGeneralRegs(isa, 'bv_general', 512) inst += fixinst cnt += len(fixinst) return inst
def __init__(self): self.basepath = getOption("basepath")
if __name__ == '__main__': from options import getOption from webapp import app app.run(host=getOption('flask.host'), debug=getOption('flask.debug'))
def makeRandomBlock(self, ctx): super(ThumbTBRBlock, self).makeRandomBlock(ctx) if ctx[CTX.ISA]: return blocks = [] tb_max = options.getOption('tb_max') tb_min = options.getOption('tb_min') items = random.randint(tb_min, tb_max) table_label = self.label + '_table' tb_inst = [] is_tbh = random.choice([True, False]) #init Rn and Rm regs = range(16 + 16) #increase freq of Rn == 15 regs.remove(13) Rn = random.choice(regs) if Rn > 15: Rn = 15 regs = range(14) regs.remove(13) if Rn in regs: regs.remove(Rn) Rm = random.choice(regs) tb_inst += ig.setReg(Rm, 0, items-1, False) if not Rn == 15: tb_inst.append(('ldr.w', 'r%d, =%s' % (Rn, table_label))) if is_tbh: tb_inst.append(('tbh', '[r%d, r%d, lsl #1]' % (Rn, Rm))) index_type = '.hword' else: tb_inst.append(('tbb', '[r%d, r%d]' % (Rn, Rm))) index_type = '.byte' self.code.append((CT.INST, tb_inst)) case_code = [] nest = ctx['nest'] - 3 ctx['nest'] = nest for i in range(items): case_block = ctx['getRBlock']("%s_case%d" % (self.label, i), nest) sub_block = case_block.makeRandomBlock(ctx) blocks += sub_block b_end = ig.genBranch(False, ctx, self.label_end, False) case_code.append((CT.BLOCK, case_block)) case_code.append((CT.INST, b_end)) if Rn == 15: self.code.append((CT.LABEL, table_label)) for i in range(items): self.code.append((CT.INST, [(index_type, "((%s_case%d - %s)/2)" % (self.label, i, table_label))])) self.code += case_code else: self.code += case_code self.code.append((CT.LABEL, table_label)) for i in range(items): self.code.append((CT.INST, [(index_type, "((%s_case%d - %s_case0)/2)" % (self.label, i, self.label))])) ctx['nest'] = nest + 2 return blocks
def __init__(self): self.api = API(getOption('betaseries.apikey')) self.details = None