Skip to content

Commit

Permalink
fix metal uaf (tinygrad#964)
Browse files Browse the repository at this point in the history
  • Loading branch information
geohot authored Jun 10, 2023
1 parent c0e558b commit 2c324d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions test/external/external_metal_uaf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import weakref
import numpy as np
from tinygrad.tensor import Tensor, Device
Device.DEFAULT = "METAL"

if __name__ == "__main__":
t = Tensor.zeros(3).realize()
wt = weakref.ref(t.lazydata.realized)
n = t.numpy()
t += 1
n2 = t.numpy()
print(wt)
del t
print(wt)
print(n, n.base, n.base.base)
print(n2, n2.base, n2.base.base)
assert wt() is not None
2 changes: 1 addition & 1 deletion tinygrad/runtime/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def fromCPU(cls, x:np.ndarray, **kwargs):

class RawBufferMapped(RawBufferCopyIn):
def _buffer(self) -> memoryview: raise NotImplementedError("must be implemented")
def toCPU(self) -> np.ndarray: return np.frombuffer(self._buffer(), dtype=self.dtype.np)
def toCPU(self) -> np.ndarray: return np.frombuffer(self._buffer(), dtype=np.dtype(self.dtype.np, metadata={"backing": self})) # type: ignore
def _copyin(self, x:np.ndarray) -> None: np.copyto(self.toCPU(), x.reshape(-1))

# this one is simple enough that i moved it out of the runtimes
Expand Down

0 comments on commit 2c324d0

Please sign in to comment.