Commit Graph

440 Commits

Author SHA1 Message Date
rationalcoder
710533975e Fix out-of-band allocations in dynamic arenas 2025-09-16 16:31:10 -05:00
Damian Tarnawski
21fd7c200e Add require_results attr to procs returning an allocator 2025-09-02 13:03:15 +02:00
alessio98888
c6db3cc670 Fix buddy allocator assert
The last address of "data" is not "cast(uintptr)raw_data(data)+cast(uintptr)size" but
"cast(uintptr)raw_data(data)+cast(uintptr)(size-1)".

The original assert would fail when for example the allocation size requested and the buddy allocator allignment were both 64.
2025-08-16 19:50:06 +02:00
gingerBill
7642e0a0e0 Require @(init) and @(fini) to be proc "contextless" () 2025-08-08 12:10:01 +01:00
gingerBill
bb4bc316a4 for in string16; Support string16 across core 2025-08-02 12:20:35 +01:00
gingerBill
a2e7a62a26 Add virtual.new_clone 2025-07-30 23:14:53 +01:00
Feoramund
a0efdf26a6 mem: Clarify Buddy_Allocator requirements 2025-07-22 10:52:55 -04:00
gingerBill
0dd0a3813b Fix very subtle bug in virtual.memory_block_alloc 2025-07-20 15:32:22 +01:00
Feoramund
4d4356e806 mem: Guard against Buddy_Allocator overwriting metadata 2025-06-20 06:44:44 -04:00
Feoramund
bfa0e1110a mem: Don't print Buddy_Allocator.tail
This is always a pointer past the end of the buffer given to
`buddy_allocator_init`, which could be an invalid address. Printing may
result in a segmentation violation.
2025-06-20 06:41:42 -04:00
Feoramund
c2f3d074e3 mem: Forbid construction of Buddy_Allocator with insufficient space
This takes into account eventual alignment.
2025-06-19 19:41:39 -04:00
Feoramund
57e2d8f1dd mem: Fix Buddy_Allocator size calculation to truly include alignment
This didn't take into account the size of the header plus the size of
the allocation itself by virtue of `align_forward_uint`; this could
result in no change if `size` was equal to `b.alignment` because the
number is aligned, and if `actual_size` and `size` ended up being equal,
no additional space would be requested.

This meant that a block would end up being allocated on top of its
buddy's head.

Fixes #3435
2025-06-19 19:41:38 -04:00
Feoramund
4e9f15965a mem: Fix inverted condition in buddy_allocator_alloc_bytes_non_zeroed
This was causing the procedure to find a block, then find one again,
or to not find a block and not try again.
2025-06-19 19:33:51 -04:00
Feoramund
7526549e51 Disable usage of AddressSanitizer (pt. 2) 2025-06-15 15:34:18 -04:00
Feoramund
5e929b596d Merge branch 'master' into fix-2694 2025-06-15 15:30:26 -04:00
Feoramund
efba9a1fb7 Disable usage of AddressSanitizer pending a per-allocator review
It has been discovered that AddressSanitizer does not keep a 1:1 mapping
of which bytes are poisoned and which are not. This can cause issues for
allocations less than 8 bytes and where addresses straddle 8-byte
boundaries.

See the following link for more information:

https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm#mapping
2025-06-15 15:06:28 -04:00
Feoramund
134c39b58b mem: Standardize panic messages 2025-06-15 12:34:16 -04:00
Feoramund
e4258b151e mem: Review all documentation comments 2025-06-15 12:34:16 -04:00
Feoramund
7524762620 mem: Poison memory for Buddy_Allocator 2025-06-15 12:34:16 -04:00
Feoramund
7e77bd425f mem: Guard against size 0 in dynamic_arena_resize_* 2025-06-15 12:34:16 -04:00
Feoramund
78a10b462f mem: Poison memory for Dynamic_Arena 2025-06-15 12:34:16 -04:00
Feoramund
de3c322899 mem: Make dynamic_arena_alloc not private
None of the other `*_alloc` procs are private, and this proc is even
referenced in public documentation comments.
2025-06-15 12:34:16 -04:00
Feoramund
652c0c997d mem: Remove bogus comments
There is no `dynamic_arena_free`; the mode is not implemented
2025-06-15 12:34:15 -04:00
Feoramund
c1e3171178 mem: Remove comment about calling panic
The behavior is codified in the comment as returning `Invalid_Pointer`.
2025-06-15 12:34:15 -04:00
Feoramund
aaeae8bc7d mem: Remove trailing whitespace 2025-06-15 12:34:15 -04:00
Feoramund
47a54dd11a mem: Panic when passing invalid pointers to small stack free/resize
This is consistent with `Stack_Allocator`.
2025-06-15 12:34:15 -04:00
Feoramund
3261896790 mem: Correct wrong error message 2025-06-15 12:34:15 -04:00
Feoramund
aa41a77fc4 mem: Check if alignment matches on Small_Stack resize 2025-06-15 12:34:15 -04:00
Feoramund
3a02918efc mem: Make small_stack_resize* free if size is 0 2025-06-15 12:34:15 -04:00
Feoramund
a4771ce1b5 mem: Clarify what happens when you free out-of-order in a Small_Stack 2025-06-15 12:34:15 -04:00
Feoramund
21a17c1ae6 mem: Replace auto_cast 2025-06-15 12:34:15 -04:00
Feoramund
8f68c46467 mem: Don't unpoison the header of a Small_Stack allocation 2025-06-15 12:34:15 -04:00
Feoramund
5798151a0e mem: Poison unused memory more thoroughly 2025-06-14 13:21:15 -04:00
Feoramund
ae96b276c1 mem: Check if alignment matches on Stack_Allocator resize 2025-06-14 13:21:15 -04:00
Feoramund
837d8cf72c mem: Actually resize when resizing for Stack_Allocator
Changed the check from `bytes` to `err` for safety's sake, too.

This will prevent the potential bug of allocating non-zero memory, then
doing a zeroed resize, which will result in having garbage data in the
initial half.
2025-06-14 13:21:14 -04:00
Feoramund
f701aeffd5 mem: Correct wrong error message 2025-06-14 13:21:14 -04:00
Feoramund
7580dc2dd0 mem: Remove pointless check in Scratch_Allocator
The backup allocator is set at `init` which happens even if `Scratch` is
nil at the head of `scratch_alloc_bytes_non_zeroed`.
2025-06-14 13:21:14 -04:00
Feoramund
c4f6e973d9 mem: Don't change Scratch_Allocator's backup allocator
The backup allocator is set at `init` and must stay the same for the
lifetime of the Scratch allocator, as this allocator is used to free all
`leaked_allocations`. Changing it could lead to a situation where the
wrong allocator is used to free a leaked allocation.
2025-06-14 13:21:14 -04:00
Feoramund
62b0f71768 mem: Fix comment typo 2025-06-14 13:21:14 -04:00
Feoramund
890245c229 mem: Don't unpoison the header of a Stack allocation 2025-06-14 13:21:14 -04:00
Feoramund
79e5ddaa26 mem: Make stack_resize* free if size is 0
This will cause an error if the memory being resized was not the last
allocation, as should be expected according to the description that this
"acts just like stack_free."
2025-06-14 13:21:14 -04:00
Feoramund
179a8559f6 mem: Add guards against buggy allocators overlapping allocations 2025-06-14 13:21:14 -04:00
Feoramund
f627b55cf5 mem: Fix several issues in Scratch_Allocator
1. The size was being adjusted for the alignment which does not make any
   sense without the context of the base pointer. Now we just add the
   `alignment - 1` to the size if needed then adjust the pointer.

2. The root pointer of the last allocation is now stored in order to
   make the free operation more useful (and to cover the right memory
   region for ASan).

3. Resizing now only works on the last allocation instead of any address
   in a valid range, which resulted in overwriting allocations that had
   just been made.

4. `old_memory` is now re-poisoned entirely before the resized range is
   returned with the new range unpoisoned. This will guarantee that
   there are no unpoisoned gaps.

Fixes #2694
2025-06-14 12:35:16 -04:00
Laytan Laats
67a8b035db mem: compat allocator improvements
1. store alignment instead of original pointer
2. implement .Query_Info
3. poison the header and alignment portion of the allocation
4. .Resize uses `max(orig_alignment, new_alignment)` as it's alignment
   now
5. .Free passes along the original alignment
2025-06-12 20:40:22 +02:00
Laytan Laats
0ed6cdc98e mem/tlsf: fix asan reporting poisoning of already poisoned region
free on tlsf poisons the entire block, while alloc might only unpoison a
part of it (cause it's size is aligned up). This causes free to
potentially poison an already poisoned portion, which is a
use-after-poison.

Because this is "fine" and intended, I opted to just
@no_sanitize_address it.
2025-06-12 19:37:24 +02:00
Jeroen van Rijn
0d55764aa7 int 2025-05-27 04:42:12 +02:00
Jeroen van Rijn
c513f035ad Fix example 2025-05-27 04:33:22 +02:00
Jeroen van Rijn
624c176ef3 ptr_sub prose clarification 2025-05-27 04:28:56 +02:00
Jeroen van Rijn
655fab7227 Add core/hyperthread count for Windows and Linux (#5216)
Add core/hyperthread count to `core:sys/info` for Windows and Linux.
TODO: Linux RISCV, Linux ARM, Darwin, and the BSDs.
2025-05-25 19:43:10 +02:00
Lucas Perlind
83bc2d3c4a Add asan support for various allocators 2025-05-06 14:55:50 +10:00