From 85ecba341a6f724dfd85e540e522016be52c428d Mon Sep 17 00:00:00 2001 From: Vicente Eduardo Ferrer Garcia Date: Wed, 1 Feb 2023 20:38:57 +0100 Subject: [PATCH] Solve all issues in cs loader. --- .../loaders/cs_loader/netcore/CMakeLists.txt | 4 +++ .../netcore/source/MetacallEntryPoint.cs | 1 - .../netcore/source/Providers/LoaderBase.cs | 32 +++++++++---------- .../source/metacall_cs_test.cpp | 2 -- source/tests/sanitizer/lsan.supp | 2 ++ 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/source/loaders/cs_loader/netcore/CMakeLists.txt b/source/loaders/cs_loader/netcore/CMakeLists.txt index e35f82399..a7c4c2b95 100644 --- a/source/loaders/cs_loader/netcore/CMakeLists.txt +++ b/source/loaders/cs_loader/netcore/CMakeLists.txt @@ -63,24 +63,28 @@ if(OPTION_BUILD_GUIX) # Build without internet access add_custom_target(${target} ALL + COMMAND ${CMAKE_COMMAND} -E $,remove,rm> ${PROJECT_OUTPUT_DIR}/CSLoader.dll COMMAND ${DOTNET_COMMAND} restore ${DOTNET_CORE_PATH_SOURCE} ${DOTNET_ADDITIONAL_PACKAGES_SOURCE} ${DOTNET_SOURCE} ${CMAKE_CURRENT_SOURCE_DIR}/source/project.csproj COMMAND ${DOTNET_COMMAND} publish ${DOTNET_CORE_PATH_SOURCE} ${DOTNET_ADDITIONAL_PACKAGES_SOURCE} ${DOTNET_SOURCE} ${CMAKE_CURRENT_SOURCE_DIR}/source/project.csproj -o ${CMAKE_BINARY_DIR} ) else() if(DOTNET_VERSION VERSION_EQUAL "2.0" OR DOTNET_VERSION VERSION_GREATER "2.0") add_custom_target(${target} ALL + COMMAND ${CMAKE_COMMAND} -E $,remove,rm> ${PROJECT_OUTPUT_DIR}/CSLoader.dll COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} restore ${CMAKE_CURRENT_SOURCE_DIR}/source/project.csproj COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} publish ${CMAKE_CURRENT_SOURCE_DIR}/source/project.csproj -o ${PROJECT_OUTPUT_DIR} ) else() if(DOTNET_MIGRATE) add_custom_target(${target} ALL + COMMAND ${CMAKE_COMMAND} -E $,remove,rm> ${PROJECT_OUTPUT_DIR}/CSLoader.dll COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} migrate ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} restore ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} publish ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json -o ${PROJECT_OUTPUT_DIR} ) else() add_custom_target(${target} ALL + COMMAND ${CMAKE_COMMAND} -E $,remove,rm> ${PROJECT_OUTPUT_DIR}/CSLoader.dll COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} restore ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json COMMAND ${DOTNET_ENV_VAR} ${DOTNET_COMMAND} publish ${CMAKE_CURRENT_SOURCE_DIR}/source/project.json -o ${PROJECT_OUTPUT_DIR} ) diff --git a/source/loaders/cs_loader/netcore/source/MetacallEntryPoint.cs b/source/loaders/cs_loader/netcore/source/MetacallEntryPoint.cs index e8429151f..2433024b2 100644 --- a/source/loaders/cs_loader/netcore/source/MetacallEntryPoint.cs +++ b/source/loaders/cs_loader/netcore/source/MetacallEntryPoint.cs @@ -7,7 +7,6 @@ using Microsoft.CodeAnalysis.CSharp; using System.IO; using Microsoft.CodeAnalysis.Emit; -using System.Runtime.Loader; using System.Runtime.InteropServices; using static CSLoader.MetacallDef; using System.Collections.Immutable; diff --git a/source/loaders/cs_loader/netcore/source/Providers/LoaderBase.cs b/source/loaders/cs_loader/netcore/source/Providers/LoaderBase.cs index 6ca495797..e9bb39fb3 100644 --- a/source/loaders/cs_loader/netcore/source/Providers/LoaderBase.cs +++ b/source/loaders/cs_loader/netcore/source/Providers/LoaderBase.cs @@ -133,16 +133,24 @@ public bool LoadFromFileFunctions(string[] files) return LoadFromSourceFunctions(sources.ToArray()); } - private void PrintDiagnostics(ImmutableArray diagnostics) + private int PrintDiagnostics(ImmutableArray diagnostics) { - IEnumerable failures = diagnostics.Where(diagnostic => - diagnostic.IsWarningAsError || - diagnostic.Severity == DiagnosticSeverity.Error); + int errorCount = 0; - foreach (Diagnostic diagnostic in failures) + foreach (Diagnostic diagnostic in diagnostics) { - this.log.Error("CSLoader compilation error: " + diagnostic.GetMessage()); + if (diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error) + { + this.log.Error("CSLoader compilation error: " + diagnostic.ToString()); + ++errorCount; + } + else + { + this.log.Error("CSLoader compilation warning: " + diagnostic.ToString()); + } } + + return errorCount; } public bool LoadFromSourceFunctions(string[] source) @@ -185,21 +193,13 @@ public bool LoadFromSourceFunctions(string[] source) ) ); - ImmutableArray compilationErrors = compilation.GetDiagnostics(); - - if (compilationErrors.Count() > 0) + if (PrintDiagnostics(compilation.GetDiagnostics()) > 0) { - PrintDiagnostics(compilationErrors); - return false; } - ImmutableArray declarationErrors = compilation.GetDeclarationDiagnostics(); - - if (compilationErrors.Count() > 0) + if (PrintDiagnostics(compilation.GetDeclarationDiagnostics()) > 0) { - PrintDiagnostics(declarationErrors); - return false; } diff --git a/source/tests/metacall_cs_test/source/metacall_cs_test.cpp b/source/tests/metacall_cs_test/source/metacall_cs_test.cpp index 1525a0c7d..723fa8d3a 100644 --- a/source/tests/metacall_cs_test/source/metacall_cs_test.cpp +++ b/source/tests/metacall_cs_test/source/metacall_cs_test.cpp @@ -86,7 +86,6 @@ TEST_F(metacall_cs_test, Concat) metacall_value_destroy(ret); } -#if !defined(__ADDRESS_SANITIZER__) /* TODO: C# Loader leaks when fails to load a script */ TEST_F(metacall_cs_test, Fail) { /* This is a Python script on purpose, in order to test C# when it fails */ @@ -99,7 +98,6 @@ TEST_F(metacall_cs_test, Fail) EXPECT_EQ((int)1, (int)metacall_load_from_memory("cs", buffer, sizeof(buffer), NULL)); } -#endif TEST_F(metacall_cs_test, FailRelativePath) { diff --git a/source/tests/sanitizer/lsan.supp b/source/tests/sanitizer/lsan.supp index bbacb3857..8550a5116 100644 --- a/source/tests/sanitizer/lsan.supp +++ b/source/tests/sanitizer/lsan.supp @@ -34,6 +34,8 @@ leak:libruby* # leak:libcoreclr* leak:libicuuc* +# TODO: Implement assembly unloading with loader context +leak:System.Private.CoreLib.dll # # Rust #