Skip to content

Make PageAllocator use next_mmap_addr_hint on Windows #17377

@squeek502

Description

@squeek502

This is a TODO in the code currently:

zig/lib/std/heap.zig

Lines 31 to 32 in 412d863

/// TODO Utilize this on Windows.
pub var next_mmap_addr_hint: ?[*]align(mem.page_size) u8 = null;

On non-Windows systems, PageAllocator uses this to make the addresses returned always increase to avoid memory addresses being re-used. This is useful/necessary for the GeneralPurposeAllocator to behave as documented:

//! * Do not re-use memory slots, so that memory safety is upheld. For small
//! allocations, this is handled here; for larger ones it is handled in the
//! backing allocator (by default `std.heap.page_allocator`).

I'm converting this into an issue to better track progress towards addressing this on Windows.

Right now, VirtualAlloc is used in PageAllocator, but if the function VirtualAlloc2 was used instead (on >= Windows 10), it would allow the use of next_mmap_addr_hint via LowestStartingAddress

@dweiller has tried to make a binding for VirtualAlloc2 for unrelated reasons but ran into some problems. Relevant Discord links:

I'm having problems trying to cross compile (from linux) a dll using VirtualAlloc2(). My investigations online have pointed to a few issues people have had with the microsoft docs (at least at one point) indicating incorrect dependencies and mingw not exporting it (mingw-w64/mingw-w64#27). I don't really understand what the files in lib/libc/mingw/lib{32,-common} are used for though I gather they define the symbols exported by some libraries and I see that zig only includes VirtualAlloc2FromApp() in them, with VirtualAlloc2() only present in libs/libc/include/any-windows-any/memoryapi.h, which may be one issue (the github link above links to a change to mingw on Jul 28 2023 that adds VirtualAlloc2 to api-ms-win-core-memory-l1-1-6.def). However, attempting to switch to using VirtualAlloc2FromApp results in the same error reported by Zig which is:

error: lld-link: undefined symbol: VirtualAlloc2

This is with -target x86_64-windows-gnu -lkernel32 -ladvapi32 - I'm not 100% confident these linker flags are correct.

My response:

mostly guessing:

looks like the fix for mingw-w64/mingw-w64#27 isn't in the latest tagged mingw release either, so it might take even longer for the fix to make its way into zig

Metadata

Metadata

Assignees

No one assigned

    Labels

    contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.enhancementSolving this issue will likely involve adding new logic or components to the codebase.os-windowsstandard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions