Skip to content

Commit 2c52b05

Browse files
committed
Added workaround for Clang breaking NullReferenceException on Linux.
See MochiLibraries/ClangSharp.Pathogen#7
1 parent 84075b6 commit 2c52b05

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Biohazrd/TranslatedLibraryBuilder.cs

+19
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,25 @@ private List<CXUnsavedFile> CreateUnsavedFilesList(SourceFileInternal? indexFile
158158

159159
private void InitializeClang()
160160
{
161+
// Workaround for https://github.com/MochiLibraries/ClangSharp.Pathogen/issues/7
162+
if (OperatingSystem.IsLinux())
163+
{
164+
try
165+
{
166+
unsafe
167+
{
168+
[DllImport("libc")]
169+
static extern int setenv(byte* envname, byte* envval, int overwrite);
170+
171+
fixed (byte* nameP = Encoding.ASCII.GetBytes("LIBCLANG_DISABLE_CRASH_RECOVERY\0"))
172+
fixed (byte* valueP = Encoding.ASCII.GetBytes("1\0"))
173+
{ setenv(nameP, valueP, 1); }
174+
}
175+
}
176+
catch (Exception ex)
177+
{ Console.Error.WriteLine($"Exception while applying ClangSharp.Pathogen#7 workaround: {ex}"); }
178+
}
179+
161180
if (Environment.GetEnvironmentVariable("BIOHAZRD_CUSTOM_LIBCLANG_PATHOGEN_RUNTIME") is string customNativeRuntime)
162181
{
163182
LibClangSharpResolver.OverrideNativeRuntime(customNativeRuntime);

Tests/Biohazrd.Tests/BasicTests.cs

+9
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,14 @@ public void ClangFindsSystemIncludes()
271271
);
272272
library.FindDeclaration<TranslatedFunction>("Test");
273273
}
274+
275+
[Fact]
276+
[RelatedIssue("https://github.com/MochiLibraries/ClangSharp.Pathogen/issues/7")]
277+
public void ClangDoesntBreakNullReferenceException()
278+
{
279+
CreateLibrary("void Hello();");
280+
object o = null!;
281+
Assert.Throws<NullReferenceException>(() => o.GetHashCode());
282+
}
274283
}
275284
}

0 commit comments

Comments
 (0)