Skip to content

Commit

Permalink
Added workaround for Clang breaking NullReferenceException on Linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
PathogenDavid committed Mar 9, 2022
1 parent 79c83e9 commit b0c3200
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Biohazrd/TranslatedLibraryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,25 @@ private List<CXUnsavedFile> CreateUnsavedFilesList(SourceFileInternal? indexFile

private void InitializeClang()
{
// Workaround for https://github.com/MochiLibraries/ClangSharp.Pathogen/issues/7
if (OperatingSystem.IsLinux())
{
try
{
unsafe
{
[DllImport("libc")]
static extern int setenv(byte* envname, byte* envval, int overwrite);

fixed (byte* nameP = Encoding.ASCII.GetBytes("LIBCLANG_DISABLE_CRASH_RECOVERY\0"))
fixed (byte* valueP = Encoding.ASCII.GetBytes("1\0"))
{ setenv(nameP, valueP, 1); }
}
}
catch (Exception ex)
{ Console.Error.WriteLine($"Exception while applying ClangSharp.Pathogen#7 workaround: {ex}"); }
}

if (Environment.GetEnvironmentVariable("BIOHAZRD_CUSTOM_LIBCLANG_PATHOGEN_RUNTIME") is string customNativeRuntime)
{
LibClangSharpResolver.OverrideNativeRuntime(customNativeRuntime);
Expand Down
9 changes: 9 additions & 0 deletions Tests/Biohazrd.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,14 @@ public void ClangFindsSystemIncludes()
);
library.FindDeclaration<TranslatedFunction>("Test");
}

[Fact]
[RelatedIssue("https://github.com/MochiLibraries/ClangSharp.Pathogen/issues/7")]
public void ClangDoesntBreakNullReferenceException()
{
CreateLibrary("void Hello();");
object o = null!;
Assert.Throws<NullReferenceException>(() => o.GetHashCode());
}
}
}

0 comments on commit b0c3200

Please sign in to comment.