Skip to content

Remove VirtualAlloc & VirtualFree from standard library + update std.heap.PageAllocator #23154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ziggoon
Copy link
Contributor

@ziggoon ziggoon commented Mar 9, 2025

This commit removes the VirtualAlloc and VirtualFree definitions from the standard library as outlined in #22846.

Additionally, a slight refactor has been made to the Windows implementation of std.heap.PageAllocator to check for page_size_min as follow-up to #23097.

) callconv(.winapi) ?LPVOID;

// TODO: Wrapper around NtFreeVirtualMemory.
// If the return value is .INVALID_PAGE_PROTECTION, calls RtlFlushSecureMemoryCache and try again.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems worth it to do, just to make sure we don't lose any functionality here. VirtualFree() is basically (without error handling minutiae) this:

BOOL __stdcall VirtualFree(LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType)
{
    PVOID BaseAddress = lpAddress;
    ULONG_PTR RegionSize = dwSize;
    
    NTSTATUS Status = NtFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &RegionSize, dwFreeType);
    if (Status == STATUS_INVALID_PAGE_PROTECTION) {
        if (!RtlFlushSecureMemoryCache(BaseAddress, RegionSize)) {
            return FALSE;
        }

        Status = NtFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &RegionSize, dwFreeType);
    }

    return NT_SUCCESS(Status);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants