Skip to content

[mono][interp] Avoid leaving around pages without access#119754

Merged
BrzVlad merged 2 commits intodotnet:mainfrom
BrzVlad:fix-interp-instruments
Sep 19, 2025
Merged

[mono][interp] Avoid leaving around pages without access#119754
BrzVlad merged 2 commits intodotnet:mainfrom
BrzVlad:fix-interp-instruments

Conversation

@BrzVlad
Copy link
Member

@BrzVlad BrzVlad commented Sep 16, 2025

Otherwise it would crash instruments when trying to access it. This impacts mostly interpreter because we have the memory barrier call in sgen_client_scan_thread_data when interpreter is enabled.

Fixes #90394

Otherwise it would crash instruments when trying to access it.
@BrzVlad BrzVlad requested a review from steveisok as a code owner September 16, 2025 11:05
Copilot AI review requested due to automatic review settings September 16, 2025 11:05
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a crash in instruments by ensuring memory pages don't remain without access permissions after a memory barrier operation. The change reorders memory protection calls to avoid leaving pages in an inaccessible state.

  • Moves the memory protection restoration call to after the no-access protection call
  • Ensures the helper page is accessible after the memory barrier operation completes

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Sep 16, 2025
@BrzVlad
Copy link
Member Author

BrzVlad commented Sep 16, 2025

Original fix found thanks to @rolfbjarne in #90394 (comment)

@BrzVlad
Copy link
Member Author

BrzVlad commented Sep 16, 2025

CoreCLR has a completely different way of achieving a process wide memory barrier. This commit avoids any drastic changes to the mechanism behind. Also I'm not certain whether there are official documentations about this guaranteeing the required memory barrier.

@BrzVlad BrzVlad requested a review from lateralusX September 16, 2025 11:10
@BrzVlad BrzVlad added area-VM-meta-mono and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Sep 16, 2025
@lateralusX
Copy link
Member

lateralusX commented Sep 16, 2025

Looks like CoreCLR implements a similar approach,

int status = pthread_mutex_lock(&g_flushProcessWriteBuffersMutex);
, but it's using mmap instead and locks the page. On Apple platforms CoreCLR does a completely different thing thought. I guess going from read/write to none back to read/write should have the same effects on TLBs and buffers as we have in our initial assumption but maybe is not too bad of an idea to try to use CoreCLR's approach, at least in main since their approach on Apple platforms is different.

@rolfbjarne
Copy link
Member

On Apple platforms CoreCLR does a completely different thing thought. I guess going from read/write to none back to read/write should have the same effects on TLBs and buffers as we have in our initial assumption but maybe is not too bad of an idea to try to use CoreCLR's approach, at least in main since their approach on Apple platforms is different.

CoreCLR's implementation was introduced here: #44670

and it seems there was a discussion with Apple beforehand: #41993

@lateralusX
Copy link
Member

minipal_initialize_memory_barrier_process_wide and minipal_memory_barrier_process_wide is in minipal so we should be able to use the same implementation on Mono since minipal is a shared library.

@BrzVlad
Copy link
Member Author

BrzVlad commented Sep 16, 2025

I just stumbled across that PR as well but there doesn't seem to be any significant explanation about it. Maybe @janvorli would have some more context.

I wanted to backport this change to .net10, potentially .net9 and wouldn't want to introduce any risky changes. I believe we could move to the minipal one with a follow up PR for .net 11.

@BrzVlad
Copy link
Member Author

BrzVlad commented Sep 19, 2025

/ba-g wasm build tests known error

@BrzVlad BrzVlad merged commit 9031045 into dotnet:main Sep 19, 2025
68 of 70 checks passed
@BrzVlad
Copy link
Member Author

BrzVlad commented Sep 19, 2025

/backport to release/10.0

@github-actions
Copy link
Contributor

xtqqczze pushed a commit to xtqqczze/dotnet-runtime that referenced this pull request Sep 20, 2025
* [mono][interp] Avoid leaving around pages without access

Otherwise it would crash instruments when trying to access it.

* add comment
BrzVlad added a commit to BrzVlad/runtime that referenced this pull request Sep 24, 2025
BrzVlad added a commit that referenced this pull request Sep 25, 2025
…struments (#120060)

* Revert "[mono][interp] Avoid leaving around pages without access (#119754)"

This reverts commit 9031045.

* Allocate helper page with mono_valloc so it doesn't confuse instruments

mono_valloc uses mmap behind the scene.
@github-actions github-actions bot locked and limited conversation to collaborators Oct 20, 2025
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.

Mono crashes in mono_reflection_get_custom_attrs_data_checked when running under Instruments Leaks profiler

5 participants