Files
tinygrad/test/external/external_fuzz_tlsf.py
nimlgen c18307e749 AM driver (#6923)
* connect to gpu

* rlc init?

* gfx comp start init

* early init is hardoded, some progress with fw

* gart

* progress, next mqd

* ring setup, still does not execute anything

* ugh write correct reg

* pci2: vm

* pci2: start psp

* vm seems to work

* pci2: gfx start

* pci2: fix psp ring resp

* pci2: try ring

* pci2: mes and some fixes

* pci2: some progress

* pci2: progress

* pci2: mm

* pci2: discovery

* pci2: correct apertures

* pci2: b

* pci2: i

* pci2: l

* pci2: o

* pci2: cmu

* pci2: mes_kiq works

* pci2: mes

* pci2: kcq does not work(

* pci2: unhalt gfx

* ops_am

* minor

* check if amdgpu is there, or we will crash

* bring back graph, it just works

* less prints

* do not init mes (not used)

* remove unused files

* ops_am: start move into core

* ops_am: works

* clcks, but still slower

* faster + no mes_kiq

* vm frags + remove mes

* cleanup fw

* gmc tiny cleanup

* move to ops_amd

* comment out what we dont really need

* driverless

* close in speed

* am clean most of ips

* gmc to ips

* cleaner

* new vm walker

* comment old one

* remove unsued autogens

* last write ups

* remove psp hardcoded values

* more

* add logs

* ih

* p2p and sdma

* vfio hal and interrupts

* smth

* amd dev iface

* minor after rebase

* bind for sdma

* Revert "bind for sdma"

This reverts commit a90766514d.

* tmp

* debug new mm

* ugh, allreduce hangs fixed

* p1

* works

* no pci.py

* cleaner a bit

* smth

* tiny cleanups

* cleaner a bit

* pciiface

* linter

* linter 2

* linter 3

* linter

* pylint

* reverted unrelated changes

* unrelated

* cmp tool

* ugh wrong fw

* clockgating

* unrelated

* alloc smaller chunks

* this

* opt sigs

* collect stat

* ops

* upd

* proclogs

* proclogs2

* vfio

* ruff

* linter pylint

* oops

* mypy p1

* mem fix

* mypy p2

* mypy p3

* mypy p4

* correct

* minor

* more tests

* linter in tests

* pci_regs header

* minor write up

* setup

* do not require libs

---------

Co-authored-by: George Hotz <72895+geohot@users.noreply.github.com>
2024-12-31 23:06:17 +03:00

77 lines
2.5 KiB
Python

import random
from typing import Dict, Optional
from tinygrad.runtime.support.allocator import TLSFAllocator
class AllocatorFuzzer:
def __init__(self, total_size):
self.total_size = total_size
self.alloc_payload = 0
self.mv = memoryview(bytearray(total_size))
self.alloctor = TLSFAllocator(total_size, block_size=16)
self.allocations: Dict[int, tuple[int, int]] = {} # ptr -> (size, pattern)
self.min_alloc_size = 16
self.max_alloc_size = int(total_size * 0.3)
self.alloc_probability = 0.7
def generate_pattern(self, ptr: int, size: int) -> int: return (ptr * 31 + size * 17) & 0xFF
def fill_memory(self, ptr: int, size: int, pattern: int):
for i in range(min(size, 32)):
self.mv[ptr + i] = pattern
self.mv[ptr + (size - 1 - i)] = pattern
def verify_memory(self, ptr: int, size: int, pattern: int) -> bool:
for i in range(min(size, 32)):
assert self.mv[ptr + i] == pattern
assert self.mv[ptr + (size - 1 - i)] == pattern
return True
def random_alloc(self) -> Optional[int]:
size = random.randint(self.min_alloc_size, min(self.max_alloc_size, self.total_size - self.alloc_payload))
try:
ptr = self.alloctor.alloc(size)
except MemoryError:
print(f"Failed to allocate {size} bytes. Payload size is {self.alloc_payload}, so fragmenation is {(size / self.total_size)*100.0:.2f}%")
return None
pattern = self.generate_pattern(ptr, size)
self.fill_memory(ptr, size, pattern)
self.allocations[ptr] = (size, pattern)
self.alloc_payload += size
print(f"Allocated {size} bytes at {ptr:x}, pattern: {pattern:02x}")
return ptr
def random_free(self) -> bool:
if not self.allocations: return False
ptr = random.choice(list(self.allocations.keys()))
size, pattern = self.allocations[ptr]
# Verify pattern before freeing
if not self.verify_memory(ptr, size, pattern):
raise RuntimeError(f"Memory corruption detected at {ptr:x}!")
print(f"Freeing {size} bytes at {ptr:x}, pattern verified: {pattern:02x}")
self.alloc_payload -= size
self.alloctor.free(ptr)
del self.allocations[ptr]
return True
def run(self):
for i in range(10000000):
if (random.random() < self.alloc_probability or not self.allocations): self.random_alloc()
else: self.random_free()
print("\nCleaning up remaining allocations...")
while self.allocations: self.random_free()
print("Fuzzing completed successfully!")
if __name__ == "__main__":
fuzzer = AllocatorFuzzer(1 << 30)
fuzzer.run()