Handle read-only /tmp and missing NUMA/robust-mutex on HarmonyOS - #132827
Handle read-only /tmp and missing NUMA/robust-mutex on HarmonyOS#132827springmin wants to merge 5 commits into
Conversation
HarmonyOS (OpenHarmony) app sandboxes differ from a plain Linux environment in three ways that break .NET at startup or at runtime: 1. get_mempolicy/mbind are blocked by the seccomp policy, so the GC's NUMA probe SIGSYS-crashes the process. Compile the NUMA syscalls out for TARGET_OHOS; the GC falls back to single-node, which is correct for phones. 2. /tmp is mounted read-only. Shared-memory files (named mutexes, memory-mapped files) now live under Path.GetTempPath() instead of the hardcoded /tmp/, honoring TMPDIR like the rest of the runtime already does. 3. The sysroot's pthread lacks robust-mutex support, so NamedMutex falls back to the shared-memory-file implementation (already the path for OpenBSD/Haiku). Adds internal OperatingSystem.IsOhos() (compile-time TARGET_OHOS, mirroring IsHaiku()) and the TARGET_OHOS managed define. All changes are no-ops on existing platforms.
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @dotnet/area-system-io |
|
@dotnet-policy-service agree |
CI failure classification (run 2)The latest push fixes the only PR-caused failures: the four Three legs still fail, all unrelated to this PR:
Note This comment was drafted with AI assistance (Copilot/agent tooling) under the repository owner's direction. |
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tracking issue for the overall OpenHarmony (HarmonyOS) porting effort: #132866 Note This comment was drafted with AI assistance (Copilot/agent tooling) under the repository owner's direction. |
|
This PR is ready for review. It is the first PR of a series adding OpenHarmony (HarmonyOS) support to the runtime (tracking issue: #132866). The changes are guarded by The PR spans three areas, so I'm pinging the respective owners:
CI status: the only PR-caused failures (four Happy to split the GC change into a separate PR if preferred. Note This comment was drafted with AI assistance (Copilot/agent tooling) under the repository owner's direction. |
|
Is HarmonyOS only used in tablet/mobile/IOT scenarios or is it used in desktop scenarios as well? Would it be reasonable to use the in-process-only NamedMutex implementation like we do for Android, iOS, and MacCatalyst? |
|
Good questions. HarmonyOS NEXT is used across phones, tablets, and IoT devices, but it also targets desktop-class devices (HarmonyOS NEXT for PC), so cross-process synchronization is a real scenario for applications ported from Linux/Windows. Now,I am porting the dotnet runtime to harmonyos by AI in HarmonyOS PC.
If the maintainers prefer the in-process-only Note This comment was drafted with AI assistance (Copilot/agent tooling) under the repository owner's direction. |
Reflects jkotas' TargetsLinuxOhos -> TargetsOhos rename (already applied in 602a5b1/955126211cc) and records the reviewer feedback from dotnet#132827 (jkotas rename + jkoritzinsky TMPDIR scoping).
602a5b1 renamed the property in 7 files but missed the three most critical ones: RuntimeIdentifier.props (property definition + TargetsLinuxGlibc exclusion), Subsets.props (DefaultSubsets + _BuildAnyCrossArch), and liveBuilds.targets (CoreCLRArtifactsPath). Without these, TargetsOhos would be defined but never consumed. Renames complete the jkotas feedback from dotnet#132827.
Rebases the remaining OHOS porting work (30 files) into 3 PRs after the sandbox-fix PR dotnet#132827 (tracking issue dotnet#132866). Includes exact file inventories per PR, the TargetsOhos naming convention (jkotas feedback), no-op guarantees, validation checklist, and A→C→B submission rationale.
|
There are no named event wait handles on non-Windows, your AI assessment is incorrect there. Also, it looks like HarmonyOS is shifting to its own kernel instead of Linux. Can we change the RID to be ohos instead of linux-ohos since it's not guaranteed to be Linux based? |
|
Thanks for the feedback — you're right on both points. I've verified in the code that there are no named event wait handles or named semaphores on non-Windows (both throw PlatformNotSupportedException on Unix), so shared memory files are used exclusively by the cross-process NamedMutex path — my earlier statement was incorrect. On the RID: HarmonyOS PC currently ships with two kernel variants — linux-ohos (Linux kernel) and harmony-ohos (the HarmonyOS native kernel) — across both arm and x86. Since the kernel is not guaranteed to be Linux-based, I agree ohos is the right RID going forward. The property and define names in this PR (TargetsOhos / TARGET_OHOS) are already kernel-agnostic, and the follow-up build-infrastructure PR will use the ohos RID. On NamedMutex: The primary target for this port right now is HarmonyOS PC — running the .NET runtime on desktop-class devices to develop .NET applications for Harmony devices. The current design references Apple's macOS implementation: cross-process named mutexes backed by shared memory files, with the shared directory resolved via TMPDIR because the HarmonyOS sandbox mounts /tmp read-only. Inspired by this discussion, I've refined the design to also cover non-PC devices that don't need cross-process mutexes: the cross-process path becomes selectable per device class through the existing build configuration (FeatureCrossProcessMutex, the same mechanism that gates iOS/Android). PC builds keep the macOS-style shared-memory-file implementation; builds for non-PC devices simply leave the feature disabled, which automatically excludes the shared-memory files code from compilation and falls back to the in-process named-mutex implementation — exactly like iOS and Android. Concretely, this is a one-line condition on FeatureCrossProcessMutex in System.Private.CoreLib.Shared.projitems plus a TargetsOhosMobile property set by the build for non-PC targets (e.g. the ohos-mobile-arm64 RID). No other mobile behaviors (such as AssemblyDependencyResolver or PosixSignalRegistration) are affected, and the current PR itself stays unchanged — it provides the PC path. Does this design sound reasonable? If you'd prefer not to have this variability, I can fall back to disabling the cross-process path for OHOS entirely (the simpler, iOS-aligned approach). |
|
You can ask your AI model to analyze the merge commits of OpenBSD PRs, which we have recently ported: https://github.com/dotnet/runtime/pulls?q=is:pr+label:os-openbsd. Then ask it to start porting the code. Once you have full set of changes in a branch; cross building, infra, coreclr, tools, r2r, aot, libraries, corehost, installer, you can create smaller branches with substantial work then upstream them sequentially like infra+coreclr changes in one PR, each library changes in a separate PR, dotnet/arcade upstreamed there and so on. This way reviewer can make sense of what's going on, we can test what's being upstreamed etc. which is better than starting off of a random point and using undefined stuff like |
|
Thanks for the guidance — the OpenBSD port's pattern (e.g. #130761 CI leg, #129906 NativeAOT stubs, #130478 exepath — small, focused PRs) is exactly the model we intend to follow, and it's how this effort is already structured. The full OHOS integration is staged on a dedicated branch (43 files in the runtime repo + 19 in the SDK repo) and split into sequential upstream PRs, tracked in #132866:
On the RID: both OpenHarmony and HarmonyOS currently ship Linux-based ( On "undefined Note This comment was drafted with AI assistance (Copilot/agent tooling) under the repository owner's direction. |
|
The point was this is not the good
You could use stacked PR approach for this port, it would help the future platforms port: https://docs.github.com/pull-requests/how-tos/stacked-pull-requests When we started OpenBSD port few months ago, stacked PR concept didn't exist. I'd have definitely opted for it. |
| #include <minipal/utils.h> | ||
|
|
||
| #if defined(TARGET_LINUX) && !defined(TARGET_ANDROID) | ||
| #if defined(TARGET_LINUX) && !defined(TARGET_ANDROID) && !defined(TARGET_OHOS) |
There was a problem hiding this comment.
TARGET_OPENHARMONY is more readable and it's 16 chars. We have a 16 chars precedence with an architecture macro TARGET_LOONGARCH64, so it shouldn't be a problem.
There was a problem hiding this comment.
Done — renamed to TARGET_OPENHARMONY in the latest push (cc9ccb3).
| /// Indicates whether the current application is running on HarmonyOS (OpenHarmony). | ||
| /// </summary> | ||
| [NonVersionable] | ||
| internal static bool IsOhos() => |
There was a problem hiding this comment.
Done — renamed to IsOpenHarmony in the latest push (cc9ccb3).
| <DefineConstants Condition="'$(Targetsillumos)' == 'true'">$(DefineConstants);TARGET_ILLUMOS</DefineConstants> | ||
| <DefineConstants Condition="'$(TargetsSolaris)' == 'true'">$(DefineConstants);TARGET_SOLARIS</DefineConstants> | ||
| <DefineConstants Condition="'$(TargetsHaiku)' == 'true'">$(DefineConstants);TARGET_HAIKU</DefineConstants> | ||
| <DefineConstants Condition="'$(TargetsOhos)' == 'true'">$(DefineConstants);TARGET_OHOS</DefineConstants> |
There was a problem hiding this comment.
This would become TargetOpenHarmony for MSBuild scripts.
There was a problem hiding this comment.
Done — renamed to TargetOpenHarmony in the latest push (cc9ccb3).
|
Thanks — I've applied all three renames in the latest push (cc9ccb3): On the ordering: agreed. The build infrastructure PR is now up as #132953 ( Since this is a fork-based contribution, the PRs are submitted sequentially (each against Note This comment was drafted with AI assistance (Copilot/agent tooling) under the repository owner's direction. |
Summary
HarmonyOS (OpenHarmony) app sandboxes differ from a plain Linux environment in
three ways that break .NET at startup or at runtime. This PR adds the
TARGET_OPENHARMONYplatform guard and the three runtime fixes required to run onHarmonyOS. All changes are no-ops on existing platforms (see "Impact" below).
This is the second PR of the series adding
linux-ohos(HarmonyOS) support tothe runtime (tracking issue: #132866). It depends on the build infrastructure
PR #132953, which defines
TargetOpenHarmony/TARGET_OPENHARMONY; until thatlands, this PR is a compile-time no-op on every platform. The split keeps the
sandbox fixes reviewable independently of the build infrastructure.
Changes
1. Skip the GC NUMA probe on HarmonyOS (
numasupport.cpp)get_mempolicy/mbindare blocked by the HarmonyOS seccomp policy, so the NUMAprobe SIGSYS-crashes the process at startup. The syscalls are compiled out for
TARGET_OPENHARMONY; the GC falls back to single-node, which is correct for phones.2. Honor TMPDIR for shared-memory files on HarmonyOS (
SharedMemoryManager.Unix.cs)/tmpis mounted read-only in the HarmonyOS app sandbox, so shared-memory files(named mutexes, memory-mapped files) must not be placed under a hardcoded
/tmp/. OnTARGET_OPENHARMONYthe shared-memory files directory is now derived fromPath.GetTempPath(), which honorsTMPDIRand matches how the rest of theruntime resolves the temp directory. All other platforms keep the existing
hardcoded
/tmp/behavior unchanged.3. Fall back from pthread mutexes for
NamedMutex(NamedMutex.Unix.cs)The HarmonyOS sysroot's pthread lacks robust-mutex support.
NamedMutexfallsback to the shared-memory-file implementation (already the path for
OpenBSD/Haiku).
Supporting:
OperatingSystem.IsOpenHarmony()+TARGET_OPENHARMONYOperatingSystem.IsOpenHarmony()—internal, compile-timeTARGET_OPENHARMONY, mirrorsIsHaiku(). No public API change in this PR.System.Private.CoreLib.Shared.projitems—TARGET_OPENHARMONYdefine constant fromTargetOpenHarmony(mirroringTargetsAndroid/TARGET_ANDROID).Tests (
MutexTests.cs)The
NamedMutex_*shared-memory tests derive the global shared-memory directorythe same way the runtime does (
{SharedFilesPath}/.dotnet/shm/global), mirroringthe platform-conditional selection in change 2, so they stay in sync with the
runtime on every platform.
Impact on existing platforms
None:
TARGET_OPENHARMONYis only defined whenTargetsLinuxOhos == 'true', which noexisting build sets.
IsOpenHarmony()returnsfalseon every existing platform.numasupport.cppguards are additive (&& !defined(TARGET_OPENHARMONY)); withTARGET_OPENHARMONYundefined the behavior is byte-identical.(
/tmp/); theTMPDIR-honoring path is compiled in only forTARGET_OPENHARMONY.Validation
clr.native+libs+host+packs -os linux-ohos -arch arm64 --crosscross-buildsucceeds:
0 Warning(s) 0 Error(s).libcoreclr.soforlinux-ohos-arm64contains zero references toget_mempolicy/mbind(verified viaobjdump).IntermediatesDirpath fix verified in the full build.MutexTests.NamedMutex_*on the Unix legs because theshared-memory path change was not yet scoped to
TARGET_OPENHARMONY; that is fixed bythe latest push (the runtime path on non-HarmonyOS platforms is
/tmp/again,which the tests target). On the re-run, all previously failing Unix legs pass.
The remaining failures are unrelated to this PR:
CompositeMLDsa*on theWindows legs (the Helix Windows queues' CNG provider reports "The requested
operation is not supported" for Composite ML-DSA; the same tests pass on the
osx-arm64-NativeAOT leg in this run) and a browser-wasm
WasmTestOnChrometime-out on the LibraryTests_EAT leg (an infrastructure flake — a different
test work item failed on each run).
Notes for reviewers
-os linux-ohos, RID graph, NDK toolchainplumbing) is intentionally not in this PR — it will follow so each PR is
independently reviewable. Without it,
TARGET_OPENHARMONYis simply never defined.linux-ohoscross legs mirroringlinux-bionic.Note
This PR was authored with AI assistance (Copilot/agent tooling) under the
repository owner's direction.