start static2 caching, shouldn't be deleted on exit

This commit is contained in:
George Hotz 2015-09-04 00:02:24 -07:00
parent 7c0621ac4b
commit 1336a6e1f8
2 changed files with 58 additions and 7 deletions

View File

@ -20,6 +20,7 @@ WITH_BAP = False
# TODO: make this true in v3 # TODO: make this true in v3
WITH_STATIC = False WITH_STATIC = False
STATIC_ENGINE = "builtin" STATIC_ENGINE = "builtin"
STATIC_CACHE_BASE = "/tmp/qira_static_cache/"
WEBSOCKET_DEBUG = False WEBSOCKET_DEBUG = False

View File

@ -32,26 +32,29 @@
import collections import collections
import os, sys import os, sys
import re import re
import pickle
import atexit
from hashlib import sha1
sys.path.append("../middleware") sys.path.append("../middleware")
import qira_config import qira_config
from model import * from model import *
# debugging
try:
from hexdump import hexdump
except:
pass
# the new interface for all things static # the new interface for all things static
# will only support radare2 for now # will only support radare2 for now
# mostly tags, except for names and functions # mostly tags, except for names and functions
class Static: class Static:
def __init__(self, path, debug=0, static_engine=None): def __init__(self, path, debug=0, static_engine=None):
# create the static cache dir
try:
os.mkdir(qira_config.STATIC_CACHE_BASE)
except:
pass
self.tags = {} self.tags = {}
self.path = path self.path = path
self.scf = qira_config.STATIC_CACHE_BASE + sha1(open(self.path, "rb").read()).hexdigest()
self.r2core = None self.r2core = None
self.debug = debug self.debug = debug
@ -107,6 +110,53 @@ class Static:
if self.debug >= 1: if self.debug >= 1:
print "*** elf loaded" print "*** elf loaded"
"""
# check the cache
if os.path.isfile(self.scf):
# cache is global_tags + tags
with open(self.scf) as f:
try:
dd = pickle.load(f)
print "*** read %d bytes from static cache" % f.tell()
except:
dd = None
print "*** static cache corrupt, ignoring"
if dd != None:
self.deserialize(dd)
pass
# register cache writing
def write_cache():
with open(self.scf, "wb") as f:
dat = self.serialize()
pickle.dump(dat, f)
print "*** wrote %d bytes to static cache" % f.tell()
atexit.register(write_cache)
"""
def serialize(self):
def blacklist(d):
ret = {}
for k in d:
#if k == "instruction":
if k != "name":
continue
ret[k] = d[k]
return ret
kk = self.tags.keys()
vv = map(lambda x: blacklist(self.tags[x].backing), kk)
return self.global_tags.backing, kk, vv
def deserialize(self, dat):
gt, kk, vv = dat
for k in gt:
self[k] = gt[k]
for address, dd in zip(kk, vv):
for k in dd:
self[address][k] = dd[k]
# this should be replaced with a # this should be replaced with a
def set_name(self, address, name): def set_name(self, address, name):
if name not in self.rnames: if name not in self.rnames: