Selaa lähdekoodia

Only create mmap once per binary

We keep the context manager in play just to avoid refactoring all
of the references to that.
nosoop 11 kuukautta sitten
vanhempi
commit
243c23f315
1 muutettua tiedostoa jossa 4 lisäystä ja 10 poistoa
  1. 4 10
      src/smgdc/validate.py

+ 4 - 10
src/smgdc/validate.py

@@ -52,6 +52,7 @@ class BaseBinary:
     path: pathlib.Path
     angr: angr.Project
     _file: io.IOBase
+    _mm: mmap.mmap
 
     def __init__(self, path: pathlib.Path, cache_path: pathlib.Path | None = None):
         self.path = path
@@ -64,22 +65,15 @@ class BaseBinary:
             cached_proj.write_bytes(pickle.dumps(self.angr))
         else:
             self.angr = pickle.loads(cached_proj.read_bytes())
+        self._mm = mmap.mmap(self._file.fileno(), 0, access=mmap.ACCESS_READ)
 
     @contextlib.contextmanager
     def mmap(self):
-        mm = mmap.mmap(self._file.fileno(), 0, access=mmap.ACCESS_READ)
-        yield mm
-
-        # FIXME: We currently don't close this because an error gets thrown at
-        #        ``LocationEntry.calculate_phys_address()`` due to exported pointers.  It's not
-        #        clear to me if the issue stems from ``ByteSignature.expr.finditer()`` or
-        #        something else at this point.
-        # mm.close()
+        yield self._mm
 
     def read(self, address, size) -> bytes:
         # shorthand to read a value from a physical file
-        with self.mmap() as memory:
-            return memory[address : address + size]
+        return self._mm[address : address + size]
 
 
 class WindowsBinary(BaseBinary):