Fix OOM edge case in interleaved LoaderHeap (#81602)
authorJan Vorlicek <janvorli@microsoft.com>
Fri, 3 Feb 2023 19:20:55 +0000 (20:20 +0100)
committerGitHub <noreply@github.com>
Fri, 3 Feb 2023 19:20:55 +0000 (20:20 +0100)
commitdfb9604e3b7232ffb7e3dc7ea2d6575804a798d2
tree26aa236b13f706e6e980a8c25aae9a315b3da457
parent043f3a7a8a6420e868a5bc8c45d6b889b4e02c7e
Fix OOM edge case in interleaved LoaderHeap (#81602)

When the `CommitPages` in `GetMoreCommittedPages` fails, which can
happen due to OOM, the `m_pPtrToEndOfCommittedRegion` was already
updated from the end of the code page to the end of the data page.
So if another allocation request comes to the heap, we think there is
one extra page of space left and end up allocating region that crosses
the code / data page boundary. And later we crash when initializing
precodes that were allocated by this request.

This issue was discovered by a Roslyn CI test that loaded assemblies
into non-collectible AssemblyLoadContext instances and ended up jitting
so many methods that the maximum number of mappings on Linux (65535 by
default) got exceeded and mmap failed.

This change fixes it by moving the `m_pPtrToEndOfCommittedRegion` only
in the success case.
src/coreclr/utilcode/loaderheap.cpp