fix metal uaf (#964)

This commit is contained in:
George Hotz
2023-06-09 21:28:06 -07:00
committed by GitHub
parent c0e558b77c
commit 2c324d0685
2 changed files with 18 additions and 1 deletions
+17
View File
@@ -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
+1 -1
View File
@@ -31,7 +31,7 @@ class RawBufferCopyIn(RawBuffer):
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