Skip to content

Fix Linux madvise() advice values incorrectly combined via bitwise OR in GCToOSInterface::VirtualReset - #126966

Merged
janvorli merged 6 commits into
mainfrom
copilot/fix-madvise-advice-values
Apr 20, 2026
Merged

janvorli merged 6 commits into
mainfrom
copilot/fix-madvise-advice-values

Conversation

Copilot AI commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor

main PR

Description

VirtualReset was combining MADV_FREE (8) and MADV_DONTDUMP (16) via bitwise OR and passing the result (24) to a single madvise() call. madvise() takes a single advice constant — not a bitmask — so this was either rejected as EINVAL (kernels < 5.18) or silently matched MADV_DONTNEED_LOCKED (kernels ≥ 5.18), a completely different operation.

Fix: Issue two separate madvise() calls, and use posix_madvise(POSIX_MADV_DONTNEED) as a proper fallback only when MADV_FREE is not available:

// Before (broken)
int madviseFlags = 0;
madviseFlags |= MADV_DONTDUMP;  // 16
madviseFlags |= MADV_FREE;      // 8
st = madvise(address, size, madviseFlags);  // passes 24 — invalid

// After (fixed)
#ifdef MADV_DONTDUMP
st = madvise(address, size, MADV_DONTDUMP);  // coredump hint (independent)
#endif

#ifdef MADV_FREE
st = madvise(address, size, MADV_FREE);      // primary: page reclaim
#elif defined(HAVE_POSIX_MADVISE)
st = posix_madvise(address, size, POSIX_MADV_DONTNEED);  // fallback when MADV_FREE unavailable
#endif

The posix_madvise(POSIX_MADV_DONTNEED) path is an #elif chained to #ifdef MADV_FREE, making it a proper fallback that only executes when MADV_FREE is unavailable. The guard uses MADV_FREE directly (the kernel header constant) rather than HAVE_MADV_FREE, which was never defined by the GC unix CMake build system. The previous #if defined(HAVE_POSIX_MADVISE) && !defined(MADV_DONTDUMP) condition was semantically unrelated to that fallback decision. This matches the pattern already used by VirtualReserveInner and VirtualCommitInner in the same file. Introduced by #95643.

Customer Impact

On all Linux builds where both MADV_DONTDUMP and MADV_FREE are defined (i.e., all modern glibc/x86-64 Linux):

  • Kernels < 5.18: GC never successfully resets pages to the OS — memory pressure behavior is broken and MADV_DONTDUMP is never applied (reset memory appears in coredumps).
  • Kernels ≥ 5.18: MADV_DONTNEED_LOCKED is invoked instead — immediately discarding pages including locked pages, which is semantically incorrect and potentially dangerous.

Additionally, because HAVE_MADV_FREE was never defined by the build system, the MADV_FREE reclaim path was silently compiled out on all platforms, falling through to posix_madvise(POSIX_MADV_DONTNEED) instead. Using MADV_FREE directly as the preprocessor guard restores the intended behavior.

Regression

Yes — introduced in .NET 9 by #95643 (an optimization to reduce syscall count).

Testing

The bug is a straightforward misuse of a syscall interface. The fix has been manually verified against the Linux madvise(2) man page and kernel headers. No existing automated test infrastructure covers madvise() advice values directly.

Risk

Low. The change is a minimal, targeted fix: replace one combined madvise() call with two separate calls, restructure the posix_madvise fallback as a proper #elif, and use the MADV_FREE kernel constant directly as the preprocessor guard. No logic changes, no new APIs, no structural changes.

Package authoring no longer needed in .NET 9

IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.

Copilot AI requested review from Copilot and removed request for Copilot April 15, 2026 18:46
Copilot AI requested review from Copilot and removed request for Copilot April 15, 2026 18:47
Copilot AI requested review from Copilot and removed request for Copilot April 15, 2026 18:50
Copilot AI changed the title [WIP] Fix madvise() advice values combined via bitwise OR Fix Linux madvise() advice values incorrectly combined via bitwise OR in GCToOSInterface::VirtualReset Apr 15, 2026
Copilot AI requested a review from janvorli April 15, 2026 18:52
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.

Comment thread src/coreclr/gc/unix/gcenv.unix.cpp Resolved
Copilot AI requested review from Copilot and removed request for Copilot April 15, 2026 22:32
Copilot AI requested a review from janvorli April 15, 2026 22:33
@janvorli
janvorli requested a review from jkotas April 15, 2026 22:35

@janvorli janvorli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@jkotas
jkotas marked this pull request as ready for review April 15, 2026 23:23
Copilot AI review requested due to automatic review settings April 15, 2026 23:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes Linux GCToOSInterface::VirtualReset to avoid passing an invalid/incorrectly-combined advice value to madvise(), ensuring the GC’s “reset pages” hint behaves correctly across kernel versions.

Changes:

  • Replace the single madvise() call (with OR-combined advice values) with separate calls for MADV_DONTDUMP and the reclaim hint (MADV_FREE or POSIX fallback).
  • Restructure the POSIX fallback as a proper #elif so it is only used when the MADV_FREE path is unavailable.

Comment thread src/coreclr/gc/unix/gcenv.unix.cpp Resolved
Copilot AI requested a review from janvorli April 16, 2026 08:51
@janvorli

Copy link
Copy Markdown
Member

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

@janvorli backporting to release/10.0 failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Creating an empty commit: Initial plan
Applying: Fix madvise() calls in VirtualReset to use separate calls instead of bitwise OR
Using index info to reconstruct a base tree...
M	src/coreclr/gc/unix/gcenv.unix.cpp
Falling back to patching base and 3-way merge...
Auto-merging src/coreclr/gc/unix/gcenv.unix.cpp
CONFLICT (content): Merge conflict in src/coreclr/gc/unix/gcenv.unix.cpp
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0002 Fix madvise() calls in VirtualReset to use separate calls instead of bitwise OR
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

JulieLeeMSFT pushed a commit that referenced this pull request May 27, 2026
Backport of #126966 to release/10.0

## Customer Impact

- [x] Customer reported
- [ ] Found internally

`VirtualReset` in GC was combining `MADV_FREE` (8) and `MADV_DONTDUMP`
(16) via bitwise OR and passing the result (24) to a single `madvise`
call. `madvise` takes a single advice constant — not a bitmask — so this
was either rejected as `EINVAL` (kernels < 5.18) or silently matched
`MADV_DONTNEED_LOCKED` (kernels ≥ 5.18), a completely different
operation.

## Regression
- [x] Yes - introduced in .NET 9 by #95643 (an optimization to reduce
syscall count).
- [ ] No

## Testing
CI tests, local testing using targeted repro app from the customer, GC
tests

## Risk
Low. It is a simple targeted fix that just makes two calls to `madvise`
with correct arguments where we were making one call with a combination
of those values that had no effect or was outright wrong.
@github-actions github-actions Bot locked and limited conversation to collaborators May 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linux madvise() advice values incorrectly combined via bitwise OR in GCToOSInterface::VirtualReset

4 participants