From 450521df70d1a107674d79add19d6d493d41f3a6 Mon Sep 17 00:00:00 2001 From: Denis Krylov Date: Fri, 20 Nov 2026 15:06:54 +0300 Subject: [PATCH 1/5] Bring up CoreCLR on FreeBSD i386 --- eng/common/cross/build-rootfs.sh | 2 ++ eng/common/cross/toolchain.cmake | 2 ++ eng/native/configurecompiler.cmake | 2 ++ eng/native/configureplatform.cmake | 2 ++ eng/targetingpacks.targets | 2 +- src/coreclr/pal/inc/unixasmmacrosx86.inc | 8 ++++++-- src/coreclr/pal/src/arch/i386/context2.S | 4 +++- src/coreclr/pal/src/configure.cmake | 2 +- src/coreclr/vm/i386/asmhelpers.S | 10 ++++------ src/installer/pkg/projects/netcoreappRIDs.props | 3 +++ .../src/PortableRuntimeIdentifierGraph.json | 6 ++++++ .../Microsoft.NETCore.Platforms/src/runtime.json | 6 ++++++ src/native/libs/System.Native/pal_process.c | 1 + 13 files changed, 39 insertions(+), 11 deletions(-) diff --git a/eng/common/cross/build-rootfs.sh b/eng/common/cross/build-rootfs.sh index 3150ccac6fcd78..16e650314eb64f 100755 --- a/eng/common/cross/build-rootfs.sh +++ b/eng/common/cross/build-rootfs.sh @@ -231,6 +231,8 @@ while :; do __BuildArch=x86 __UbuntuArch=i386 __AlpineArch=x86 + __FreeBSDArch=i386 + __FreeBSDMachineArch=i386 __UbuntuRepo="https://archive.ubuntu.com/ubuntu/" ;; lldb*) diff --git a/eng/common/cross/toolchain.cmake b/eng/common/cross/toolchain.cmake index f65c689f695452..7018e86272a99d 100644 --- a/eng/common/cross/toolchain.cmake +++ b/eng/common/cross/toolchain.cmake @@ -129,6 +129,8 @@ elseif(TARGET_ARCH_NAME STREQUAL "x86") set(CMAKE_SYSTEM_PROCESSOR i686) if(EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/i586-alpine-linux-musl) set(TOOLCHAIN "i586-alpine-linux-musl") + elseif(FREEBSD) + set(TOOLCHAIN "i686-unknown-freebsd14") else() set(TOOLCHAIN "i686-linux-gnu") endif() diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index ede3d895f7fffa..e40f3da03f94f7 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -450,6 +450,8 @@ if (CLR_CMAKE_HOST_UNIX) message("Detected FreeBSD aarch64") elseif(CLR_CMAKE_HOST_UNIX_AMD64) message("Detected FreeBSD amd64") + elseif(CLR_CMAKE_HOST_UNIX_X86) + message("Detected FreeBSD i386") else() message(FATAL_ERROR "Unsupported FreeBSD architecture") endif() diff --git a/eng/native/configureplatform.cmake b/eng/native/configureplatform.cmake index 0154f877e7f72b..ad26da5dfcb79a 100644 --- a/eng/native/configureplatform.cmake +++ b/eng/native/configureplatform.cmake @@ -169,6 +169,8 @@ if(CLR_CMAKE_HOST_OS STREQUAL freebsd) set(CLR_CMAKE_HOST_UNIX_AMD64 1) elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL arm64) set(CLR_CMAKE_HOST_UNIX_ARM64 1) + elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL i386 OR CMAKE_SYSTEM_PROCESSOR STREQUAL i686) + set(CLR_CMAKE_HOST_UNIX_X86 1) else() clr_unknown_arch() endif() diff --git a/eng/targetingpacks.targets b/eng/targetingpacks.targets index 15e62c05e04b4e..89a2bb16b3c5cc 100644 --- a/eng/targetingpacks.targets +++ b/eng/targetingpacks.targets @@ -44,7 +44,7 @@ LatestRuntimeFrameworkVersion="$(ProductVersion)" RuntimeFrameworkName="$(LocalFrameworkOverrideName)" RuntimePackNamePatterns="$(LocalFrameworkOverrideName).Runtime.**RID**" - RuntimePackRuntimeIdentifiers="linux-arm;linux-arm64;linux-musl-arm64;linux-musl-x64;linux-x64;osx-x64;rhel.6-x64;tizen.4.0.0-armel;tizen.5.0.0-armel;win-arm64;win-x64;win-x86;linux-musl-arm;osx-arm64;maccatalyst-x64;maccatalyst-arm64;linux-s390x;linux-bionic-arm;linux-bionic-arm64;linux-bionic-x64;linux-bionic-x86;freebsd-x64;freebsd-arm64;linux-ppc64le;linux-riscv64;linux-musl-riscv64;linux-loongarch64;linux-musl-loongarch64" + RuntimePackRuntimeIdentifiers="linux-arm;linux-arm64;linux-musl-arm64;linux-musl-x64;linux-x64;osx-x64;rhel.6-x64;tizen.4.0.0-armel;tizen.5.0.0-armel;win-arm64;win-x64;win-x86;linux-musl-arm;osx-arm64;maccatalyst-x64;maccatalyst-arm64;linux-s390x;linux-bionic-arm;linux-bionic-arm64;linux-bionic-x64;linux-bionic-x86;freebsd-x64;freebsd-arm64;freebsd-x86;linux-ppc64le;linux-riscv64;linux-musl-riscv64;linux-loongarch64;linux-musl-loongarch64" TargetFramework="$(NetCoreAppCurrent)" TargetingPackName="$(LocalFrameworkOverrideName).Ref" TargetingPackVersion="$(ProductVersion)" diff --git a/src/coreclr/pal/inc/unixasmmacrosx86.inc b/src/coreclr/pal/inc/unixasmmacrosx86.inc index c28b2b170bec5b..a692f24a85d69d 100644 --- a/src/coreclr/pal/inc/unixasmmacrosx86.inc +++ b/src/coreclr/pal/inc/unixasmmacrosx86.inc @@ -139,6 +139,8 @@ C_FUNC(\Name\()_End): PROLOG_END lea \Target, [esp + 4] + // Keep the transition frame at its fixed EBP offset and align native call arguments separately. + and esp, -16 .endm // Inlined version of GetThreadEEAllocContext. @@ -146,6 +148,8 @@ C_FUNC(\Name\()_End): push ecx push eax call C_FUNC(GetThreadEEAllocContext) + // The C helper returns in eax, while allocation helpers expect the context in edx. + mov edx, eax pop eax pop ecx .endm @@ -154,8 +158,8 @@ C_FUNC(\Name\()_End): // restore stack pointer EPILOG_BEG - // skip over alignment (4) + ArgumentRegisters (8) - EPILOG_FREE 12 + // Restore the saved-register area below EBP, skipping any dynamic alignment padding. + lea esp, [ebp - 12] // pop CalleeSavedRegisters EPILOG_POP edi diff --git a/src/coreclr/pal/src/arch/i386/context2.S b/src/coreclr/pal/src/arch/i386/context2.S index 35768ea6232af2..a7c207b6ee75fb 100644 --- a/src/coreclr/pal/src/arch/i386/context2.S +++ b/src/coreclr/pal/src/arch/i386/context2.S @@ -14,6 +14,7 @@ // and is always apply to the current thread. // LEAF_ENTRY CONTEXT_CaptureContext, _TEXT +LOCAL_LABEL(CaptureContext): // Store push eax push ebx @@ -89,7 +90,8 @@ LEAF_ENTRY RtlCaptureContext, _TEXT mov eax, [esp + 8] mov DWORD PTR [eax + CONTEXT_ContextFlags], (CONTEXT_FLOATING_POINT) pop eax - jmp C_FUNC(CONTEXT_CaptureContext) + // Use the local entry to keep this branch position independent in shared libraries. + jmp LOCAL_LABEL(CaptureContext) LEAF_END RtlCaptureContext, _TEXT LEAF_ENTRY RtlRestoreContext, _TEXT diff --git a/src/coreclr/pal/src/configure.cmake b/src/coreclr/pal/src/configure.cmake index 958ad2b82be067..742409b2f73222 100644 --- a/src/coreclr/pal/src/configure.cmake +++ b/src/coreclr/pal/src/configure.cmake @@ -904,7 +904,7 @@ if(CLR_CMAKE_TARGET_APPLE) elseif(CLR_CMAKE_TARGET_FREEBSD) set(DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX 0) set(PAL_PTRACE "ptrace((cmd), (pid), (caddr_t)(addr), (data))") - if (CLR_CMAKE_HOST_ARCH_AMD64) + if(CLR_CMAKE_HOST_ARCH_AMD64 OR CLR_CMAKE_HOST_ARCH_I386) set(BSD_REGS_STYLE "((reg).r_##rr)") elseif(CLR_CMAKE_HOST_ARCH_ARM64) set(BSD_REGS_STYLE "((reg).rr)") diff --git a/src/coreclr/vm/i386/asmhelpers.S b/src/coreclr/vm/i386/asmhelpers.S index 8d578f3243c5a8..b43b6c5a6fd349 100644 --- a/src/coreclr/vm/i386/asmhelpers.S +++ b/src/coreclr/vm/i386/asmhelpers.S @@ -609,9 +609,9 @@ NESTED_ENTRY ResolveWorkerAsmStub, _TEXT, NoHandler mov esi, esp - #define STACK_ALIGN_PADDING 8 - - sub esp, STACK_ALIGN_PADDING + // Keep the transition block at ESI while aligning the three native arguments. + and esp, -16 + sub esp, 4 push [esi + 4*4] // dispatch token push [esi + 5*4] // siteAddrForRegisterIndirect push esi // pTransitionBlock @@ -625,9 +625,7 @@ NESTED_ENTRY ResolveWorkerAsmStub, _TEXT, NoHandler CHECK_STACK_ALIGNMENT call C_FUNC(VSD_ResolveWorker) - add esp, STACK_ALIGN_PADDING - - #undef STACK_ALIGN_PADDING + mov esp, esi // From here on, mustn't trash eax diff --git a/src/installer/pkg/projects/netcoreappRIDs.props b/src/installer/pkg/projects/netcoreappRIDs.props index 6c84841697b420..d0dc897ac57bd2 100644 --- a/src/installer/pkg/projects/netcoreappRIDs.props +++ b/src/installer/pkg/projects/netcoreappRIDs.props @@ -51,6 +51,9 @@ in our runtime.json to enable others to provide them. --> + + x86 + armel diff --git a/src/libraries/Microsoft.NETCore.Platforms/src/PortableRuntimeIdentifierGraph.json b/src/libraries/Microsoft.NETCore.Platforms/src/PortableRuntimeIdentifierGraph.json index 9e104d05ec68af..3693d0474d7686 100644 --- a/src/libraries/Microsoft.NETCore.Platforms/src/PortableRuntimeIdentifierGraph.json +++ b/src/libraries/Microsoft.NETCore.Platforms/src/PortableRuntimeIdentifierGraph.json @@ -64,6 +64,12 @@ "unix-x64" ] }, + "freebsd-x86": { + "#import": [ + "freebsd", + "unix-x86" + ] + }, "haiku": { "#import": [ "unix" diff --git a/src/libraries/Microsoft.NETCore.Platforms/src/runtime.json b/src/libraries/Microsoft.NETCore.Platforms/src/runtime.json index 64e7c5546b529e..7469cd1b81dcdd 100644 --- a/src/libraries/Microsoft.NETCore.Platforms/src/runtime.json +++ b/src/libraries/Microsoft.NETCore.Platforms/src/runtime.json @@ -1683,6 +1683,12 @@ "unix-x64" ] }, + "freebsd-x86": { + "#import": [ + "freebsd", + "unix-x86" + ] + }, "freebsd.12": { "#import": [ "freebsd" diff --git a/src/native/libs/System.Native/pal_process.c b/src/native/libs/System.Native/pal_process.c index ca982ebbf48a23..66ba38752094ce 100644 --- a/src/native/libs/System.Native/pal_process.c +++ b/src/native/libs/System.Native/pal_process.c @@ -581,6 +581,7 @@ static int32_t ConvertRLimitResourcesPalToPlatform(RLimitResources value) unsigned int: UINT_MAX, \ unsigned long: ULONG_MAX, \ long: LONG_MAX, \ + long long: LLONG_MAX, \ unsigned long long: ULLONG_MAX) // Because RLIM_INFINITY is different per-platform, use the max value of a uint64 (which is RLIM_INFINITY on Ubuntu) From c4a836abd64efcf977980a46e8c40541fb423042 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 21 Nov 2026 05:37:08 +0000 Subject: [PATCH 2/5] JIT x86/Unix: don't record caller-pop arg size in GC info header On x86 Unix, caller-pop conventions leave argument cleanup to the caller. Recording lvaParameterStackSize in the JIT32 GC info header made the runtime compute the caller SP too high by that amount. Funclet parent-frame matching (m_csfEHClause) then failed, which showed up as double GC reporting and stale object references. On a FreeBSD i386 box, a minimal repro (async + finally + exception under DOTNET_GCStress=0x1) passes 5/5 with this change and fails with the stock JIT. Co-authored-by: kdenv2604 --- src/coreclr/jit/gcencode.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/coreclr/jit/gcencode.cpp b/src/coreclr/jit/gcencode.cpp index 61310ebe54979a..987d5f9855a062 100644 --- a/src/coreclr/jit/gcencode.cpp +++ b/src/coreclr/jit/gcencode.cpp @@ -1634,6 +1634,16 @@ size_t GCInfo::gcInfoBlockHdrSave( } size_t argCount = compiler->lvaParameterStackSize / REGSIZE_BYTES; +#ifdef UNIX_X86_ABI + // On x86 Unix, caller-pop calling conventions (cdecl-like) leave argument cleanup + // to the caller. Recording lvaParameterStackSize in the header made the runtime compute + // the caller SP too high by that amount. That breaks funclet parent-frame matching + // (m_csfEHClause) and leads to double GC reporting / stale object references. + if (IsCallerPop(compiler->info.compCallConv) && !compiler->info.compIsVarArgs) + { + argCount = 0; + } +#endif // UNIX_X86_ABI assert(argCount <= MAX_USHORT_SIZE_T); header->argCount = static_cast(argCount); From 8bc4533a33dee5915dc405d7fca2982721fd74ca Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 21 Nov 2026 06:00:16 +0000 Subject: [PATCH 3/5] VM x86/Unix: CbStackPop is 0 for caller-pop managed calls TransitionFrame unwinding added the stack arg size to the caller SP, which put the funclet's caller SP 4 bytes off from ExInfo.m_csfEHClause after a prestub call inside a finally funclet. Funclet parent lookup then failed, the parent frame got GC-reported twice (CheckDoubleReporting assert), and a stale slot crashed after compaction. Same root cause as the previous JIT commit. Verified on FreeBSD i386: the Host test under DOTNET_GCStress=0x1 crashes 3/3 stock and 0 fixed, and the ehrepro double-report assert reproduces 0/3 stock and 3/3 fixed. Co-authored-by: kdenv2604 --- src/coreclr/vm/callingconvention.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/coreclr/vm/callingconvention.h b/src/coreclr/vm/callingconvention.h index 3d4abbee5358d4..41c8cb32f16ef0 100644 --- a/src/coreclr/vm/callingconvention.h +++ b/src/coreclr/vm/callingconvention.h @@ -424,10 +424,13 @@ class ArgIteratorTemplate : public ARGITERATOR_BASE { WRAPPER_NO_CONTRACT; - if (this->IsVarArg()) - return 0; - else - return SizeOfArgStack(); +#ifdef UNIX_X86_ABI + // Managed/cdecl calls are caller-pop on Unix x86 (see IsCallerPop in corinfo.h): + // the callee does not pop its stack arguments. + return 0; +#else + if (this->IsVarArg()) return 0; else return SizeOfArgStack(); +#endif // UNIX_X86_ABI } #endif From 182d1d3196fa85691e772e25e54c30d392093963 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 21 Nov 2026 20:15:34 +0000 Subject: [PATCH 4/5] JIT x86/Unix: mark post-import managed calls caller-pop Calls created after import (rationalizer HW-intrinsic user-call fallback, async helpers, special copy helper, tail-call dispatcher) did not get GTF_CALL_POP_ARGS, so the caller never popped stack args. On CPUs without SSE4.2 (Celeron M 420), Vector128.ExtractMostSignificantBits falls back to a call in SpanHelpers.LastIndexOfValueType SimdImpl, leaving 16 bytes on the stack; the epilog then pops the compare mask into ebp and returns to 0. Verified on a physical Celeron M 420 with FreeBSD 14.5 i386: the packer, the Host and the full GUI end-to-end run through the Capsicum worker pass without DOTNET_EnableHWIntrinsic=0, and the regression on the VM is unchanged. Co-authored-by: kdenv2604 --- src/coreclr/jit/async.cpp | 25 +++++++++++++++++++++++++ src/coreclr/jit/gschecks.cpp | 5 +++++ src/coreclr/jit/lower.cpp | 5 +++++ src/coreclr/jit/morph.cpp | 5 +++++ src/coreclr/jit/rationalize.cpp | 5 +++++ 5 files changed, 45 insertions(+) diff --git a/src/coreclr/jit/async.cpp b/src/coreclr/jit/async.cpp index ec92f9b23ca6a7..e7f0989f902e61 100644 --- a/src/coreclr/jit/async.cpp +++ b/src/coreclr/jit/async.cpp @@ -136,6 +136,11 @@ PhaseStatus Compiler::SaveAsyncContexts() CORINFO_ASYNC_INFO* asyncInfo = eeGetAsyncInfo(); GenTreeCall* capture = gtNewCallNode(CT_USER_FUNC, asyncInfo->captureContextsMethHnd, TYP_VOID); +#ifdef UNIX_X86_ABI + // Managed calls on Unix x86 are caller-pop. The importer sets this flag for + // managed calls (impImportCall), but calls created here bypass it. + capture->gtFlags |= GTF_CALL_POP_ARGS; +#endif // UNIX_X86_ABI capture->gtArgs.PushFront(this, NewCallArg::Primitive(gtNewLclAddrNode(syncCtxLclNum, 0))); capture->gtArgs.PushFront(this, NewCallArg::Primitive(gtNewLclAddrNode(execCtxLclNum, 0))); @@ -175,6 +180,11 @@ PhaseStatus Compiler::SaveAsyncContexts() } GenTreeCall* restore = gtNewCallNode(CT_USER_FUNC, asyncInfo->restoreContextsMethHnd, TYP_VOID); +#ifdef UNIX_X86_ABI + // Managed calls on Unix x86 are caller-pop. The importer sets this flag for + // managed calls (impImportCall), but calls created here bypass it. + restore->gtFlags |= GTF_CALL_POP_ARGS; +#endif // UNIX_X86_ABI restore->gtArgs.PushFront(this, NewCallArg::Primitive(gtNewLclVarNode(syncCtxLclNum))); restore->gtArgs.PushFront(this, NewCallArg::Primitive(gtNewLclVarNode(execCtxLclNum))); restore->gtArgs.PushFront(this, NewCallArg::Primitive(gtNewLclVarNode(suspendedLclNum))); @@ -1522,6 +1532,11 @@ void AsyncTransformation::FillInGCPointersOnSuspension(GenTreeCall* GenTree* flagsPlaceholder = m_comp->gtNewZeroConNode(TYP_BYREF); GenTreeCall* captureCall = m_comp->gtNewCallNode(CT_USER_FUNC, m_asyncInfo->captureContinuationContextMethHnd, TYP_VOID); +#ifdef UNIX_X86_ABI + // Managed calls on Unix x86 are caller-pop. The importer sets this flag for + // managed calls (impImportCall), but calls created here bypass it. + captureCall->gtFlags |= GTF_CALL_POP_ARGS; +#endif // UNIX_X86_ABI captureCall->gtArgs.PushFront(m_comp, NewCallArg::Primitive(flagsPlaceholder)); captureCall->gtArgs.PushFront(m_comp, NewCallArg::Primitive(contextElementPlaceholder)); @@ -1572,6 +1587,11 @@ void AsyncTransformation::FillInGCPointersOnSuspension(GenTreeCall* { GenTreeCall* captureExecContext = m_comp->gtNewCallNode(CT_USER_FUNC, m_asyncInfo->captureExecutionContextMethHnd, TYP_REF); +#ifdef UNIX_X86_ABI + // Managed calls on Unix x86 are caller-pop. The importer sets this flag for + // managed calls (impImportCall), but calls created here bypass it. + captureExecContext->gtFlags |= GTF_CALL_POP_ARGS; +#endif // UNIX_X86_ABI m_comp->compCurBB = suspendBB; m_comp->fgMorphTree(captureExecContext); @@ -1866,6 +1886,11 @@ void AsyncTransformation::RestoreFromGCPointersOnResumption(unsigned GenTree* valuePlaceholder = m_comp->gtNewZeroConNode(TYP_REF); GenTreeCall* restoreCall = m_comp->gtNewCallNode(CT_USER_FUNC, m_asyncInfo->restoreExecutionContextMethHnd, TYP_VOID); +#ifdef UNIX_X86_ABI + // Managed calls on Unix x86 are caller-pop. The importer sets this flag for + // managed calls (impImportCall), but calls created here bypass it. + restoreCall->gtFlags |= GTF_CALL_POP_ARGS; +#endif // UNIX_X86_ABI restoreCall->gtArgs.PushFront(m_comp, NewCallArg::Primitive(valuePlaceholder)); m_comp->compCurBB = resumeBB; diff --git a/src/coreclr/jit/gschecks.cpp b/src/coreclr/jit/gschecks.cpp index 4d7a54fcdcc2ff..4d81b31e3c2b21 100644 --- a/src/coreclr/jit/gschecks.cpp +++ b/src/coreclr/jit/gschecks.cpp @@ -519,6 +519,11 @@ void Compiler::gsParamsToShadows() CORINFO_METHOD_HANDLE copyHelper = info.compCompHnd->getSpecialCopyHelper(varDsc->GetLayout()->GetClassHandle()); GenTreeCall* call = gtNewCallNode(CT_USER_FUNC, copyHelper, TYP_VOID); +#ifdef UNIX_X86_ABI + // Managed calls on Unix x86 are caller-pop. The importer sets this flag for + // managed calls (impImportCall), but calls created here bypass it. + call->gtFlags |= GTF_CALL_POP_ARGS; +#endif // UNIX_X86_ABI GenTree* src = gtNewLclVarAddrNode(lclNum); GenTree* dst = gtNewLclVarAddrNode(shadowVarNum); diff --git a/src/coreclr/jit/lower.cpp b/src/coreclr/jit/lower.cpp index 5f8cbd9290058c..04b91aaf011175 100644 --- a/src/coreclr/jit/lower.cpp +++ b/src/coreclr/jit/lower.cpp @@ -2100,6 +2100,11 @@ void Lowering::InsertSpecialCopyArg(GenTreePutArgStk* putArgStk, CORINFO_CLASS_H GenTreeCall* call = comp->gtNewCallNode(CT_USER_FUNC, comp->info.compCompHnd->getSpecialCopyHelper(argType), TYP_VOID); +#ifdef UNIX_X86_ABI + // Managed calls on Unix x86 are caller-pop. The importer sets this flag for + // managed calls (impImportCall), but calls created here bypass it. + call->gtFlags |= GTF_CALL_POP_ARGS; +#endif // UNIX_X86_ABI call->gtArgs.PushBack(comp, NewCallArg::Primitive(destPlaceholder)); call->gtArgs.PushBack(comp, NewCallArg::Primitive(srcPlaceholder)); diff --git a/src/coreclr/jit/morph.cpp b/src/coreclr/jit/morph.cpp index c8f189d71d6344..fd0f7ca34d14ef 100644 --- a/src/coreclr/jit/morph.cpp +++ b/src/coreclr/jit/morph.cpp @@ -5431,6 +5431,11 @@ GenTree* Compiler::fgCreateCallDispatcherAndGetResult(GenTreeCall* orig CORINFO_METHOD_HANDLE dispatcherHnd) { GenTreeCall* callDispatcherNode = gtNewCallNode(CT_USER_FUNC, dispatcherHnd, TYP_VOID, fgMorphStmt->GetDebugInfo()); +#ifdef UNIX_X86_ABI + // Managed calls on Unix x86 are caller-pop. The importer sets this flag for + // managed calls (impImportCall), but calls created here bypass it. + callDispatcherNode->gtFlags |= GTF_CALL_POP_ARGS; +#endif // UNIX_X86_ABI // The dispatcher has signature // void DispatchTailCalls(void* callersRetAddrSlot, void* callTarget, ref byte retValue) diff --git a/src/coreclr/jit/rationalize.cpp b/src/coreclr/jit/rationalize.cpp index 6857379c8b65bf..0dddcc7be24a6e 100644 --- a/src/coreclr/jit/rationalize.cpp +++ b/src/coreclr/jit/rationalize.cpp @@ -42,6 +42,11 @@ void Rationalizer::RewriteNodeAsCall(GenTree** use, // Create the call node GenTreeCall* call = comp->gtNewCallNode(CT_USER_FUNC, callHnd, tree->TypeGet()); +#ifdef UNIX_X86_ABI + // Managed calls on Unix x86 are caller-pop. The importer sets this flag for + // managed calls (impImportCall), but calls created here bypass it. + call->gtFlags |= GTF_CALL_POP_ARGS; +#endif // UNIX_X86_ABI if (isSpecialIntrinsic) { From fec45a4ba168936dd80b5d8145279b3b286fb354 Mon Sep 17 00:00:00 2001 From: Denis Krylov Date: Fri, 20 Nov 2026 16:11:27 +0300 Subject: [PATCH 5/5] Match FreeBSD process info field widths Use the native FreeBSD widths for C long, virtual sizes, and pointers in kinfo_proc and rusage. Keep fixed-width kernel fields and the 64-bit process ABI. (cherry picked from commit 1000e28b829bf801b796ee8652046183c7556ffc) --- .../FreeBSD/Interop.Process.GetProcInfo.cs | 81 +++++++++++-------- 1 file changed, 49 insertions(+), 32 deletions(-) diff --git a/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.GetProcInfo.cs b/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.GetProcInfo.cs index aef8539062b895..6288ef6084b032 100644 --- a/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.GetProcInfo.cs +++ b/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.GetProcInfo.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; #pragma warning disable CA1823 // analyzer incorrectly flags fixed buffer length const (https://github.com/dotnet/roslyn/issues/37593) @@ -21,9 +22,9 @@ internal static partial class Process private const int LOGINCLASSLEN = 17; private const int KI_NGROUPS = 16; - private const int KI_NSPARE_INT = 4; + private const int KI_NSPARE_INT = 2; private const int KI_NSPARE_LONG = 12; - private const int KI_NSPARE_PTR = 6; + private const int KI_NSPARE_PTR = 5; // Constants from sys/sysctl.h private const int CTL_KERN = 1; @@ -69,20 +70,33 @@ internal struct @rusage { public timeval ru_utime; /* user time used */ public timeval ru_stime; /* system time used */ - public long ru_maxrss; /* max resident set size */ - private long ru_ixrss; /* integral shared memory size */ - private long ru_idrss; /* integral unshared data " */ - private long ru_isrss; /* integral unshared stack " */ - private long ru_minflt; /* page reclaims */ - private long ru_majflt; /* page faults */ - private long ru_nswap; /* swaps */ - private long ru_inblock; /* block input operations */ - private long ru_oublock; /* block output operations */ - private long ru_msgsnd; /* messages sent */ - private long ru_msgrcv; /* messages received */ - private long ru_nsignals; /* signals received */ - private long ru_nvcsw; /* voluntary context switches */ - private long ru_nivcsw; /* involuntary " */ + // C long follows the native word size on FreeBSD, including i386. + public nint ru_maxrss; /* max resident set size */ + private nint ru_ixrss; /* integral shared memory size */ + private nint ru_idrss; /* integral unshared data " */ + private nint ru_isrss; /* integral unshared stack " */ + private nint ru_minflt; /* page reclaims */ + private nint ru_majflt; /* page faults */ + private nint ru_nswap; /* swaps */ + private nint ru_inblock; /* block input operations */ + private nint ru_oublock; /* block output operations */ + private nint ru_msgsnd; /* messages sent */ + private nint ru_msgrcv; /* messages received */ + private nint ru_nsignals; /* signals received */ + private nint ru_nvcsw; /* voluntary context switches */ + private nint ru_nivcsw; /* involuntary " */ + } + + [InlineArray(KI_NSPARE_PTR)] + private struct SparePointers + { + private nint _element0; + } + + [InlineArray(KI_NSPARE_LONG)] + private struct SpareLongs + { + private nint _element0; } // From sys/user.h @@ -107,7 +121,7 @@ public unsafe struct @kinfo_proc public int ki_tsid; /* Terminal session ID */ private short ki_jobc; /* job control counter */ private short ki_spare_short1; /* unused (just here for alignment) */ - private int ki_tdev; /* controlling tty dev */ + private uint ki_tdev_freebsd11; /* legacy controlling tty dev */ private sigset_t ki_siglist; /* Signals arrived but not delivered */ private sigset_t ki_sigmask; /* Current signal mask */ private sigset_t ki_sigignore; /* Signals being ignored */ @@ -120,12 +134,12 @@ public unsafe struct @kinfo_proc private short ki_ngroups; /* number of groups */ private short ki_spare_short2; /* unused (just here for alignment) */ private fixed uint ki_groups[KI_NGROUPS]; /* groups */ - public ulong ki_size; /* virtual size */ - public long ki_rssize; /* current resident set size in pages */ - private long ki_swrss; /* resident set size before last swap */ - private long ki_tsize; /* text size (pages) XXX */ - private long ki_dsize; /* data size (pages) XXX */ - private long ki_ssize; /* stack size (pages) */ + public nuint ki_size; /* virtual size */ + public nint ki_rssize; /* current resident set size in pages */ + private nint ki_swrss; /* resident set size before last swap */ + private nint ki_tsize; /* text size (pages) XXX */ + private nint ki_dsize; /* data size (pages) XXX */ + private nint ki_ssize; /* stack size (pages) */ private ushort ki_xstat; /* Exit status for wait & stop signal */ private ushort ki_acflag; /* Accounting flags */ private uint ki_pctcpu; /* %cpu for process during ki_swtime */ @@ -136,11 +150,11 @@ public unsafe struct @kinfo_proc private ulong ki_runtime; /* Real time in microsec */ public timeval ki_start; /* starting time */ private timeval ki_childtime; /* time used by process children */ - private long ki_flag; /* P_* flags */ - private long ki_kiflag; /* KI_* flags (below) */ + private nint ki_flag; /* P_* flags */ + private nint ki_kiflag; /* KI_* flags (below) */ private int ki_traceflag; /* Kernel trace points */ private byte ki_stat; /* S* process status */ - public byte ki_nice; /* Process "nice" value */ + public sbyte ki_nice; /* Process "nice" value */ private byte ki_lock; /* Process lock (prevent swap) count */ private byte ki_rqindex; /* Run queue index */ private byte ki_oncpu_old; /* Which cpu we are on (legacy) */ @@ -152,8 +166,10 @@ public unsafe struct @kinfo_proc public fixed byte ki_comm[COMMLEN + 1]; /* command name */ private fixed byte ki_emul[KI_EMULNAMELEN + 1]; /* emulation name */ private fixed byte ki_loginclass[LOGINCLASSLEN + 1]; /* login class */ - private fixed byte ki_sparestrings[50]; /* spare string space */ + private fixed byte ki_moretdname[COMMLEN - TDNAMLEN + 1]; /* more thread name */ + private fixed byte ki_sparestrings[46]; /* spare string space */ private fixed int ki_spareints[KI_NSPARE_INT]; /* spare room for growth */ + private ulong ki_tdev; /* controlling tty dev */ private int ki_oncpu; /* Which cpu we are on */ private int ki_lastcpu; /* Last cpu we were on */ private int ki_tracer; /* Pid of tracing process */ @@ -172,10 +188,11 @@ public unsafe struct @kinfo_proc private void* ki_udata; /* User convenience pointer */ public void* ki_tdaddr; /* address of thread */ - private fixed long ki_spareptrs[KI_NSPARE_PTR]; /* spare room for growth */ - private fixed long ki_sparelongs[KI_NSPARE_LONG]; /* spare room for growth */ - private long ki_sflag; /* PS_* flags */ - private long ki_tdflags; /* XXXKSE kthread flag */ + private void* ki_pd; /* pointer to process paths info */ + private SparePointers ki_spareptrs; /* spare room for growth */ + private SpareLongs ki_sparelongs; /* spare room for growth */ + private nint ki_sflag; /* PS_* flags */ + private nint ki_tdflags; /* XXXKSE kthread flag */ } /// @@ -196,7 +213,7 @@ public unsafe struct @kinfo_proc kinfo_proc* kinfo = (kinfo_proc*)pBuffer; - Debug.Assert(kinfo->ki_structsize == sizeof(kinfo_proc)); + Debug.Assert(bytesLength == 0 || kinfo->ki_structsize == sizeof(kinfo_proc)); count = (int)bytesLength / sizeof(kinfo_proc);