You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
set memory to undefined in allocator implementations rather than interface #4298
contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.standard libraryThis issue involves writing Zig code for the standard library.
Yes it makes sense to do it in the interface rather than in allocator implementations.
Having looked at the patch and seen this comment:
// Note: can't set shrunk memory to undefined as memory shouldn't be modified on realloc failure
I am now convinced that it should be done in the allocator implementations rather than the interface, where this problem can be solved. My reasoning is that a robust debug allocator will want to do this (handle realloc correctly with regards to defined), and while it wouldn't hurt semantically to have the memory set to undefined twice, this is an area where performance might matter a great deal, and it is better design to have one canonical place where the memory is set to undefined.
The allocator implementations may be able to much more efficiently set the data to undefined for allocations as well.
One concern is that allocator implementations will "forget" to do this. For this I propose that the standard library actually include a robust allocator test, which will work on any std.mem.Allocator API. People writing their own allocators should be able to call std.testing.allocatorInterfaceStressTest(&my_allocator, options); and be reasonably confident their allocator is working correctly, including with regards to setting memory to undefined. The options could include a flag to disable testing whether the memory gets set to undefined in debug/release-safe builds.
Reacted by daurnimator, Michael Dusan, Andrew Hodges, heidezomp, parcel, Joran Dirk Greef, Isaac Freund, Silver, Jan Ph. H., Ali Cheraghi and 11 more
There is another reason:
Setting bytes to undefined in free, hides double free detection.
The second free becomes use after free, when @memset to undefined happens.
I feel like this can easily lead to multiple useless memsets when one wraps multiple allocators on top of each other. There possibility should also be a way to allocate without memset for use-cases where you want to reserve big chunk of memory but not touch it
So the tl;dr as I see it is:
CASE A: Allocator interface handles @memset(_, undefined)
Difficulties undefining in certain cases because interface has limited access to underlying implementation
Memory undefining performed only once and guaranteed by the interface (or is expected to be)
CASE B: Allocator internal implementation handles @memset(_, undefined)
When using allocators that themselves take child/backing allocators, each level of allocator will run redundant @memset() on the same memory region
Allocator implementation author is responsible for ensuring memory undefining, possible to forget
A third option (CASE C) I propose (that would require a breaking change to how some allocators are initialized) would be to have ALL allocators required to be built from a comptime factory function (like GeneralPurposeAllocator), and take a config struct with an option like set_undefined_on_free that tells the implementation whether they should be responsible for their own memory undefining or not.
The downside to this is that instead of the allocator AUTHOR being solely responsible for memory undefining, the allocator USER would also be responsible for making sure they pass the right setting to each level of allocator so that all memory is set to undefined at least once (idealy exactly once). This problem can be mitigated by giving the setting a default value of set_undefined_on_free: bool = true so that by default memory is always undefined, possibly multiple times.
Example code might look something like this:
// PageAllocNoUndef is just the std.heap.page_allocator that doesnt undefine memory on freeconstPageAllocNoUndef: type=std.heap.PageAllocator(.{.set_undefined_on_free=false});
// MyAllocatorUndef will hande all undefining of memoryconstMyAllocUndef: type=MyAllocator(.{.set_undefined_on_free=true});
varbase_alloc: Allocator=PageAllocNoUndef.allocator();
varmy_allocator: MyAllocUndef=MyAllocUndef.init(base_alloc);
Or alternatively
// PageAllocUndef is just the std.heap.page_allocator that DOES undefine memory on freeconstPageAllocUndef: type=std.heap.PageAllocator(.{});
// MyAllocatorNoUndef will assume base allocator handles undefiningconstMyAllocNoUndef: type=MyAllocator(.{.set_undefined_on_free=false});
varbase_alloc: Allocator=PageAllocUndef.allocator();
varmy_allocator: MyAllocNoUndef=MyAllocNoUndef.init(base_alloc);
If user doesn't know what to set, defaults ensure memory always undefined, albeit multiple times
constPageAlloc: type=std.heap.PageAllocator(.{});
constMyAlloc: type=MyAllocator(.{});
varbase_alloc: Allocator=PageAlloc.allocator();
// Freed memory will be `@memset(_, undefined)` twicevarmy_allocator: MyAlloc=MyAlloc.init(base_alloc);
If user explicitly sets all layers to not undefine, it could prevent detection of illegal behavior in Debug mode, but could ALSO be a 'feature', if the user (for some reason) wanted to manually set memory undefined somewhere else external to the allocator itself.
constPageAllocNoUndef: type=std.heap.PageAllocator(.{.set_undefined_on_free=false});
constMyAllocNoUndef: type=MyAllocator(.{.set_undefined_on_free=false});
varbase_alloc: Allocator=PageAllocNoUndef.allocator();
// Freed memory will never be `@memset(_, undefined)`varmy_allocator: MyAlloc=MyAllocNoUndef.init(base_alloc);
// Do some stuff with memory// Eventually free all memory and deinit allocatorssetUsedMemoryAsUndefined(mem: []u8);
After an in-depth investigation I'm reversing this decision.
Mainly:
backing allocators are used in debug mode
memset to undef in the interface allows implementations to bypass the memset to undefined
it's absolutely necessary to do this when allocating many pages of memory
also the implementation may want to rely on mmap returning pre-zeroed memory
Consider the absurdity of taking 2 MiB of zeroes that have not been paged in yet, and do not need to be paged in yet, writing 0xaa to every byte, and then writing 0x00 again to every byte to get back to where you started, except now the CPU cache is obliterated and you have +2M RSS that wasn't supposed to be there.
contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.standard libraryThis issue involves writing Zig code for the standard library.
Activity
dimdin commentedon Apr 28, 2024
There is another reason:
Setting bytes to undefined in free, hides double free detection.
The second free becomes use after free, when
@memsettoundefinedhappens.Cloudef commentedon May 4, 2024
I feel like this can easily lead to multiple useless memsets when one wraps multiple allocators on top of each other. There possibility should also be a way to allocate without memset for use-cases where you want to reserve big chunk of memory but not touch it
gabe-lee commentedon Jun 21, 2024
So the tl;dr as I see it is:
CASE A:
Allocatorinterface handles@memset(_, undefined)CASE B: Allocator internal implementation handles
@memset(_, undefined)@memset()on the same memory regionA third option (CASE C) I propose (that would require a breaking change to how some allocators are initialized) would be to have ALL allocators required to be built from a comptime factory function (like
GeneralPurposeAllocator), and take a config struct with an option likeset_undefined_on_freethat tells the implementation whether they should be responsible for their own memory undefining or not.The downside to this is that instead of the allocator AUTHOR being solely responsible for memory undefining, the allocator USER would also be responsible for making sure they pass the right setting to each level of allocator so that all memory is set to undefined at least once (idealy exactly once). This problem can be mitigated by giving the setting a default value of
set_undefined_on_free: bool = trueso that by default memory is always undefined, possibly multiple times.Example code might look something like this:
Or alternatively
If user doesn't know what to set, defaults ensure memory always undefined, albeit multiple times
If user explicitly sets all layers to not undefine, it could prevent detection of illegal behavior in
Debugmode, but could ALSO be a 'feature', if the user (for some reason) wanted to manually set memory undefined somewhere else external to the allocator itself.std.mem.Allocator: keep the undefined memset
std.mem.Allocator: keep the undefined memset
std.mem.Allocator: keep the undefined memset
std.mem.Allocator: keep the undefined memset
andrewrk commentedon Feb 6, 2025
After an in-depth investigation I'm reversing this decision.
Mainly:
Consider the absurdity of taking 2 MiB of zeroes that have not been paged in yet, and do not need to be paged in yet, writing 0xaa to every byte, and then writing 0x00 again to every byte to get back to where you started, except now the CPU cache is obliterated and you have +2M RSS that wasn't supposed to be there.
full writeup
std.mem.Allocator: keep the undefined memset
std.mem.Allocator: keep the undefined memset