[docs]classLoaderSerializer:""" Serialize/unserialize a CLE Loader object into/from an angr DB. """backend2name={v:kfork,vincle.ALL_BACKENDS.items()}
[docs]@staticmethoddefdump(session,loader):forobjinloader.all_objects:ifisinstance(obj,(cle.ExternObject,cle.TLSObject,cle.KernelObject,),):# skip dynamically created objectscontinue# does the object exist?exists=session.query(DbObject.id).filter_by(path=obj.binary).scalar()isnotNoneifexists:# it exists. skip.continuetry:content=obj.cached_contentifhasattr(obj,"cached_content")elseNoneifcontentisNone:# fall back to loading the file again from diskwithopen(obj.binary,"rb")asthe_file:content=the_file.read()exceptOSErrorasex:raiseAngrDBError(f"Failed to load content for file {obj.binary}.")fromex# save the objecto=DbObject(main_object=loader.main_objectisobj,path=obj.binary,content=content,backend=LoaderSerializer.backend2name.get(obj.__class__),backend_args="",# TODO: We will need support from CLE to store loader arguments)session.add(o)
[docs]@staticmethoddefload(session):all_objects={}# path to objectmain_object=Nonedb_objects:List[DbObject]=session.query(DbObject)fordb_oindb_objects:all_objects[db_o.path]=db_oifdb_o.main_object:main_object=db_oifmain_objectisNone:raiseAngrCorruptDBError("Corrupt database: No main object.")# build params# FIXME: Load other objectsloader=cle.Loader(BytesIO(main_object.content),)# fix the binary name of the main binaryloader._main_binary_path=main_object.pathloader.main_object.binary=main_object.pathreturnloader