Skip to content

Commit c469dfd

Browse files
committed
fix: macOS ARM64 PDF 导出崩溃 (JSC JIT W^X 冲突)
PdfNativeLibraryBootstrapper.cs: 通过 libc.setenv 在程序集加载前设置 JSC_useJIT=0 - [ModuleInitializer] 确保最早执行时机 (net5.0+) - 静态构造函数作为 netstandard2.0 fallback - 仅在 macOS ARM64 上执行,其他平台完全无影响 - 用户零感知,不需要设置任何环境变量
1 parent edd4ded commit c469dfd

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

src/Magicodes.ExporterAndImporter.Pdf/PdfNativeLibraryBootstrapper.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,50 @@ internal static class PdfNativeLibraryBootstrapper
1919
private static readonly Lazy<PdfEnvironmentInfo> CachedResult =
2020
new Lazy<PdfEnvironmentInfo>(CheckEnvironmentCore);
2121

22+
/// <summary>
23+
/// macOS ARM64: Qt WebKit 的 JSC JIT 使用 W^X 内存映射,
24+
/// 与 macOS ARM64 的 W^X 策略冲突导致 SIGBUS。
25+
/// 在 native 库加载前通过 libc.setenv 禁用 JSC JIT。
26+
/// 此方法仅在 macOS ARM64 上执行,对用户完全透明。
27+
/// </summary>
28+
[DllImport("libc", EntryPoint = "setenv", SetLastError = true)]
29+
private static extern int setenv(string name, string value, int overwrite);
30+
31+
private static void DisableJscJitIfNeeded()
32+
{
33+
try
34+
{
35+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) &&
36+
RuntimeInformation.OSArchitecture == Architecture.Arm64)
37+
{
38+
setenv("JSC_useJIT", "0", 1);
39+
}
40+
}
41+
catch
42+
{
43+
// setenv 在某些平台上不可用,忽略
44+
}
45+
}
46+
47+
#if NET5_0_OR_GREATER
48+
/// <summary>
49+
/// 程序集模块初始化器:在 Pdf 程序集加载时第一时间执行。
50+
/// </summary>
51+
[System.Runtime.CompilerServices.ModuleInitializer]
52+
internal static void InitializeJscJitWorkaround()
53+
{
54+
DisableJscJitIfNeeded();
55+
}
56+
#endif
57+
2258
/// <summary>
2359
/// 静态构造函数:首次访问类型时注册 DllImportResolver(不加载 native 库)。
2460
/// 确保 Haukcode 的 P/Invoke 能找到我们提供的 arm64 dylib。
61+
/// 同时作为 netstandard2.0 的 fallback(无 ModuleInitializer 时)。
2562
/// </summary>
2663
static PdfNativeLibraryBootstrapper()
2764
{
65+
DisableJscJitIfNeeded();
2866
#if NET5_0_OR_GREATER
2967
RegisterDllImportResolver();
3068
#endif

0 commit comments

Comments
 (0)