Add base:sanitizer package

This commit is contained in:
Lucas Perlind
2025-04-24 20:28:32 +10:00
parent 7502e7f2bc
commit 5c117bde6d
2 changed files with 73 additions and 70 deletions
+23 -22
View File
@@ -2,6 +2,7 @@ package mem
import "base:intrinsics"
import "base:runtime"
import "base:sanitizer"
/*
Nil allocator.
@@ -138,7 +139,7 @@ arena_init :: proc(a: ^Arena, data: []byte) {
a.offset = 0
a.peak_used = 0
a.temp_count = 0
runtime.asan_poison(a.data)
sanitizer.address_poison(a.data)
}
/*
@@ -226,7 +227,7 @@ arena_alloc_bytes_non_zeroed :: proc(
a.offset += total_size
a.peak_used = max(a.peak_used, a.offset)
result := byte_slice(ptr, size)
runtime.asan_unpoison(result)
sanitizer.address_unpoison(result)
return result, nil
}
@@ -235,7 +236,7 @@ Free all memory to an arena.
*/
arena_free_all :: proc(a: ^Arena) {
a.offset = 0
runtime.asan_poison(a.data)
sanitizer.address_poison(a.data)
}
arena_allocator_proc :: proc(
@@ -313,7 +314,7 @@ allocations *inside* the temporary memory region will be freed to the arena.
end_arena_temp_memory :: proc(tmp: Arena_Temp_Memory) {
assert(tmp.arena.offset >= tmp.prev_offset)
assert(tmp.arena.temp_count > 0)
runtime.asan_poison(tmp.arena.data[tmp.prev_offset:tmp.arena.offset])
sanitizer.address_poison(tmp.arena.data[tmp.prev_offset:tmp.arena.offset])
tmp.arena.offset = tmp.prev_offset
tmp.arena.temp_count -= 1
}
@@ -368,7 +369,7 @@ scratch_init :: proc(s: ^Scratch, size: int, backup_allocator := context.allocat
s.prev_allocation = nil
s.backup_allocator = backup_allocator
s.leaked_allocations.allocator = backup_allocator
runtime.asan_poison(s.data)
sanitizer.address_poison(s.data)
return nil
}
@@ -383,7 +384,7 @@ scratch_destroy :: proc(s: ^Scratch) {
free_bytes(ptr, s.backup_allocator)
}
delete(s.leaked_allocations)
runtime.asan_unpoison(s.data)
sanitizer.address_unpoison(s.data)
delete(s.data, s.backup_allocator)
s^ = {}
}
@@ -480,7 +481,7 @@ scratch_alloc_bytes_non_zeroed :: proc(
s.prev_allocation = rawptr(ptr)
s.curr_offset = int(offset) + size
result := byte_slice(rawptr(ptr), size)
runtime.asan_unpoison(result)
sanitizer.address_unpoison(result)
return result, nil
} else {
a := s.backup_allocator
@@ -525,7 +526,7 @@ scratch_free :: proc(s: ^Scratch, ptr: rawptr, loc := #caller_location) -> Alloc
old_ptr := uintptr(ptr)
if s.prev_allocation == ptr {
s.curr_offset = int(uintptr(s.prev_allocation) - start)
runtime.asan_poison(s.data[s.curr_offset:])
sanitizer.address_poison(s.data[s.curr_offset:])
s.prev_allocation = nil
return nil
}
@@ -556,7 +557,7 @@ scratch_free_all :: proc(s: ^Scratch, loc := #caller_location) {
free_bytes(ptr, s.backup_allocator, loc)
}
clear(&s.leaked_allocations)
runtime.asan_poison(s.data)
sanitizer.address_poison(s.data)
}
/*
@@ -687,7 +688,7 @@ scratch_resize_bytes_non_zeroed :: proc(
if begin <= old_ptr && old_ptr < end && old_ptr+uintptr(size) < end {
s.curr_offset = int(old_ptr-begin)+size
result := byte_slice(old_memory, size)
runtime.asan_unpoison(result)
sanitizer.address_unpoison(result)
return result, nil
}
data, err := scratch_alloc_bytes_non_zeroed(s, size, alignment, loc)
@@ -789,7 +790,7 @@ stack_init :: proc(s: ^Stack, data: []byte) {
s.prev_offset = 0
s.curr_offset = 0
s.peak_used = 0
runtime.asan_poison(data)
sanitizer.address_poison(data)
}
/*
@@ -880,13 +881,13 @@ stack_alloc_bytes_non_zeroed :: proc(
s.curr_offset += padding
next_addr := curr_addr + uintptr(padding)
header := (^Stack_Allocation_Header)(next_addr - size_of(Stack_Allocation_Header))
runtime.asan_unpoison(header)
sanitizer.address_unpoison(header)
header.padding = padding
header.prev_offset = old_offset
s.curr_offset += size
s.peak_used = max(s.peak_used, s.curr_offset)
result := byte_slice(rawptr(next_addr), size)
runtime.asan_unpoison(result)
sanitizer.address_unpoison(result)
return result, nil
}
@@ -926,7 +927,7 @@ stack_free :: proc(
}
s.prev_offset = header.prev_offset
runtime.asan_poison(s.data[old_offset:s.curr_offset])
sanitizer.address_poison(s.data[old_offset:s.curr_offset])
s.curr_offset = old_offset
return nil
@@ -938,7 +939,7 @@ Free all allocations to the stack.
stack_free_all :: proc(s: ^Stack, loc := #caller_location) {
s.prev_offset = 0
s.curr_offset = 0
runtime.asan_poison(s.data)
sanitizer.address_poison(s.data)
}
/*
@@ -1099,7 +1100,7 @@ stack_resize_bytes_non_zeroed :: proc(
zero(rawptr(curr_addr + uintptr(diff)), diff)
}
result := byte_slice(old_memory, size)
runtime.asan_unpoison(result)
sanitizer.address_unpoison(result)
return result, nil
}
@@ -1168,7 +1169,7 @@ small_stack_init :: proc(s: ^Small_Stack, data: []byte) {
s.data = data
s.offset = 0
s.peak_used = 0
runtime.asan_poison(data)
sanitizer.address_poison(data)
}
/*
@@ -1277,12 +1278,12 @@ small_stack_alloc_bytes_non_zeroed :: proc(
s.offset += padding
next_addr := curr_addr + uintptr(padding)
header := (^Small_Stack_Allocation_Header)(next_addr - size_of(Small_Stack_Allocation_Header))
runtime.asan_unpoison(header)
sanitizer.address_unpoison(header)
header.padding = auto_cast padding
s.offset += size
s.peak_used = max(s.peak_used, s.offset)
result := byte_slice(rawptr(next_addr), size)
runtime.asan_unpoison(result)
sanitizer.address_unpoison(result)
return result, nil
}
@@ -1317,7 +1318,7 @@ small_stack_free :: proc(
}
header := (^Small_Stack_Allocation_Header)(curr_addr - size_of(Small_Stack_Allocation_Header))
old_offset := int(curr_addr - uintptr(header.padding) - uintptr(raw_data(s.data)))
runtime.asan_poison(s.data[old_offset:s.offset])
sanitizer.address_poison(s.data[old_offset:s.offset])
s.offset = old_offset
return nil
}
@@ -1327,7 +1328,7 @@ Free all memory to small stack.
*/
small_stack_free_all :: proc(s: ^Small_Stack) {
s.offset = 0
runtime.asan_poison(s.data)
sanitizer.address_poison(s.data)
}
/*
@@ -1473,7 +1474,7 @@ small_stack_resize_bytes_non_zeroed :: proc(
}
if old_size == size {
result := byte_slice(old_memory, size)
runtime.asan_unpoison(result)
sanitizer.address_unpoison(result)
return result, nil
}
data, err := small_stack_alloc_bytes_non_zeroed(s, size, alignment, loc)