Repository navigation
Set DOTNET_EnableWriteXorExecute=0 for OpenDebugAD7 when the file size limit is below 2 TiB #14886
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
Ming (MaxonT)
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
MaxonT:contrib/13768-auto
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+89
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* -------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All Rights Reserved. | ||
| * See 'LICENSE' in the project root for license information. | ||
| * ------------------------------------------------------------------------------------------ */ | ||
|
|
||
| // This file must not import 'vscode' so that it can be unit tested. | ||
|
|
||
| // On Linux, the .NET runtime (W^X) creates a memfd called "doublemapper" and truncates it to 2 TiB. | ||
| // When the "Max file size" resource limit (ulimit -f) is lower than that, ftruncate fails with EFBIG, | ||
| // the process gets SIGXFSZ, and OpenDebugAD7 crashes at startup. | ||
| // Setting DOTNET_EnableWriteXorExecute=0 makes the runtime skip the double mapping. | ||
| const dotnetMemfdSize: bigint = 2n ** 41n; // 2 TiB | ||
|
|
||
| /** | ||
| * Returns true if the 'Max file size' row of /proc/<pid>/limits shows a soft limit below 2 TiB. | ||
| * Returns false for 'unlimited', for a missing row, and for anything that cannot be parsed. | ||
| */ | ||
| export function isFileSizeLimitBelowDotnetMemfd(procLimits: string): boolean { | ||
| const match: RegExpMatchArray | null = /^Max file size\s+(\S+)/m.exec(procLimits); | ||
| // /proc/<pid>/limits reports 'Max file size' in bytes (the Units column is 'bytes'). | ||
| if (!match || !/^\d+$/.test(match[1])) { | ||
| return false; | ||
| } | ||
| return BigInt(match[1]) < dotnetMemfdSize; | ||
| } | ||
|
|
||
| /** | ||
| * Environment overrides for the OpenDebugAD7 process, or undefined when none are needed. | ||
| * An existing DOTNET_EnableWriteXorExecute value set by the user is never overridden. | ||
| */ | ||
| export function getDebugAdapterEnvironment(platform: string, processEnv: NodeJS.ProcessEnv, procLimits: string | undefined): { [key: string]: string } | undefined { | ||
| if (platform !== 'linux' || processEnv.DOTNET_EnableWriteXorExecute !== undefined || procLimits === undefined) { | ||
| return undefined; | ||
| } | ||
| return isFileSizeLimitBelowDotnetMemfd(procLimits) ? { DOTNET_EnableWriteXorExecute: '0' } : undefined; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* -------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All Rights Reserved. | ||
| * See 'LICENSE' in the project root for license information. | ||
| * ------------------------------------------------------------------------------------------ */ | ||
|
|
||
| import { describe, it } from 'mocha'; | ||
| import { deepStrictEqual, strictEqual } from 'node:assert'; | ||
| import { getDebugAdapterEnvironment, isFileSizeLimitBelowDotnetMemfd } from '../../src/Debugger/debugAdapterEnvironment'; | ||
|
|
||
| const limits = (maxFileSize: string): string => | ||
| 'Limit Soft Limit Hard Limit Units \n' + | ||
| 'Max cpu time unlimited unlimited seconds \n' + | ||
| `Max file size ${maxFileSize} ${maxFileSize} bytes \n` + | ||
| 'Max data size unlimited unlimited bytes \n'; | ||
|
|
||
| describe('Debug adapter environment', () => { | ||
| it('detects a file size limit below 2 TiB', () => { | ||
| strictEqual(isFileSizeLimitBelowDotnetMemfd(limits('25769803776')), true); // ulimit -f 25165824 (KiB) | ||
| strictEqual(isFileSizeLimitBelowDotnetMemfd(limits('2199023255551')), true); | ||
| }); | ||
|
|
||
| it('does not trigger for unlimited, 2 TiB or more, or unparsable input', () => { | ||
| strictEqual(isFileSizeLimitBelowDotnetMemfd(limits('unlimited')), false); | ||
| strictEqual(isFileSizeLimitBelowDotnetMemfd(limits('2199023255552')), false); | ||
| strictEqual(isFileSizeLimitBelowDotnetMemfd(''), false); | ||
| strictEqual(isFileSizeLimitBelowDotnetMemfd('Max cpu time unlimited unlimited seconds'), false); | ||
| }); | ||
|
|
||
| it('disables the .NET W^X double mapping only on Linux with a low limit', () => { | ||
| deepStrictEqual(getDebugAdapterEnvironment('linux', {}, limits('25769803776')), { DOTNET_EnableWriteXorExecute: '0' }); | ||
| strictEqual(getDebugAdapterEnvironment('linux', {}, limits('unlimited')), undefined); | ||
| strictEqual(getDebugAdapterEnvironment('linux', {}, undefined), undefined); | ||
| strictEqual(getDebugAdapterEnvironment('darwin', {}, limits('25769803776')), undefined); | ||
| strictEqual(getDebugAdapterEnvironment('win32', {}, limits('25769803776')), undefined); | ||
| }); | ||
|
|
||
| it('keeps a value the user already set', () => { | ||
| strictEqual(getDebugAdapterEnvironment('linux', { DOTNET_EnableWriteXorExecute: '1' }, limits('25769803776')), undefined); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨Copilot (devbox7-wsl/agent61): [Moderate] .NET also accepts
COMPlus_EnableWriteXorExecute, and checksDOTNET_beforeCOMPlus_. With a low file-size limit andCOMPlus_EnableWriteXorExecute=1but noDOTNET_value, this addsDOTNET_EnableWriteXorExecute=0, silently disabling W^X despite the user's explicit setting. Could this preserve theCOMPlus_value too and cover that case in a test?