From b9a0a7143727a84910992e29d2d345e1e9869f85 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sun, 5 Feb 2023 22:37:01 +0100 Subject: [PATCH] feat: remove asserts and return an error as thread exit code --- src/Inject/NosSmooth.Inject/nossmooth.cpp | 14 ++++-- .../Errors/InjectionFailedError.cs | 3 +- .../NosSmooth.Injector/InjectionResult.cs | 41 ++++++++++++++++++ src/Inject/NosSmooth.Injector/NosInjector.cs | 6 ++- .../NosSmooth.Injector/NosSmooth.Inject.dll | Bin 53248 -> 53248 bytes .../NosSmooth.Injector.csproj | 6 +-- 6 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 src/Inject/NosSmooth.Injector/InjectionResult.cs diff --git a/src/Inject/NosSmooth.Inject/nossmooth.cpp b/src/Inject/NosSmooth.Inject/nossmooth.cpp index e9b6f247f3b686819df6470224ce8b1462c863fa..9f07919df862da1626e26094b6cfe6fb98058656 100644 --- a/src/Inject/NosSmooth.Inject/nossmooth.cpp +++ b/src/Inject/NosSmooth.Inject/nossmooth.cpp @@ -105,13 +105,15 @@ int LoadAndCallMethod(LoadParams* params) { if (!load_hostfxr()) { - assert(false && "Failure: load_hostfxr()"); - return 0; + return 2; } load_assembly_and_get_function_pointer_fn load_assembly_and_get_function_pointer = nullptr; load_assembly_and_get_function_pointer = get_dotnet_load_assembly(params->runtimeConfigPath); - assert(load_assembly_and_get_function_pointer != nullptr && "Failure: get_dotnet_load_assembly()"); + if (load_assembly_and_get_function_pointer == nullptr) + { + return 3; + } typedef void (CORECLR_DELEGATE_CALLTYPE* main_entry_point_fn)(); main_entry_point_fn main = nullptr; @@ -122,7 +124,11 @@ int LoadAndCallMethod(LoadParams* params) UNMANAGEDCALLERSONLY_METHOD, nullptr, (void**)&main); - assert(rc == 0 && main != nullptr && "Failure: load_assembly_and_get_function_pointer()"); + if (rc != 0 || main == nullptr) + { + return 4; + } + main(); return 1; } \ No newline at end of file diff --git a/src/Inject/NosSmooth.Injector/Errors/InjectionFailedError.cs b/src/Inject/NosSmooth.Injector/Errors/InjectionFailedError.cs index 3ea5fc5b111b19508013535a71e8efaf9ea08c69..6e8831ead45ea9d57d8cb46366a94a1765a19b27 100644 --- a/src/Inject/NosSmooth.Injector/Errors/InjectionFailedError.cs +++ b/src/Inject/NosSmooth.Injector/Errors/InjectionFailedError.cs @@ -12,4 +12,5 @@ namespace NosSmooth.Injector.Errors; /// The injection could not be finished successfully. /// /// The path to the dll. -public record InjectionFailedError(string DllPath, string Reason) : ResultError($"Could not inject {DllPath} dll into the process. {Reason}"); \ No newline at end of file +public record InjectionFailedError(string DllPath, string Reason, InjectionResult? Result = null) : ResultError + ($"Could not inject {DllPath} dll into the process. {Reason}, {Result}"); \ No newline at end of file diff --git a/src/Inject/NosSmooth.Injector/InjectionResult.cs b/src/Inject/NosSmooth.Injector/InjectionResult.cs new file mode 100644 index 0000000000000000000000000000000000000000..d5402250208029f25179992731c1be45aec58595 --- /dev/null +++ b/src/Inject/NosSmooth.Injector/InjectionResult.cs @@ -0,0 +1,41 @@ +// +// InjectionResult.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace NosSmooth.Injector; + +/// +/// A result obtained from NosSmooth.Inject. +/// +public enum InjectionResult +{ + /// + /// Successful result. + /// + Ok = 1, + + /// + /// Hostfxr.dll was not found, is .NET installed? It should be as this is a .NET application injecting the dll... + /// + HostfxrNotFound = 2, + + /// + /// A runtimeconfig.json of the assembly to be injected was not found. + /// + /// + /// Be sure to include true + /// in a library that will be injected. + /// + RuntimeConfigNotFound = 3, + + /// + /// The specified class or type was not found. + /// + /// + /// Be sure to put it in this format: "namespace.type.method, assembly", + /// see samples. + /// + ClassOrMethodNotFound = 4 +} \ No newline at end of file diff --git a/src/Inject/NosSmooth.Injector/NosInjector.cs b/src/Inject/NosSmooth.Injector/NosInjector.cs index 6ad39b9e4a605165f1107c52564be7235b34f6b9..7869e06dffc9cb205f0a70773b99dad5bf876d36 100644 --- a/src/Inject/NosSmooth.Injector/NosInjector.cs +++ b/src/Inject/NosSmooth.Injector/NosInjector.cs @@ -150,12 +150,16 @@ public class NosInjector } var functionResult = injector.CallFunction(nosSmoothInjectPath, "LoadAndCallMethod", loadParams); + + injector.Eject(nosSmoothInjectPath); + if (functionResult != 1) { return new InjectionFailedError ( dllPath, - $"Couldn't initialize the nethost or call the main function, did you specify the class and method correctly? Result: {functionResult}" + $"Couldn't initialize the nethost or call the main function, did you specify the class and method correctly? Result: {functionResult}", + (InjectionResult)functionResult ); } diff --git a/src/Inject/NosSmooth.Injector/NosSmooth.Inject.dll b/src/Inject/NosSmooth.Injector/NosSmooth.Inject.dll index ff72338c5afeb9e39dab480a386dddd6934faa82..1744de8e554239910e6f58329de1da29134d8672 100644 GIT binary patch delta 303 zcmZozz}&EadBP87WyuEe+MH214B^v*??Zr2Re)bF9M$a{~r+*9~a&2`^Wg~ zi;gG%|2H0a!N9;^{pQ8wr~m(VKaD-iFV6td^#Y{+|Ns9WL%--S3gjkc<}sXsfyukI zL??0V+I&%wJ!|udh6JX~7JW;&n2pqKPQE`w5=3wQJtLGwaNl`{2M-#TI&TkMdTfz@ r_FSgTiF23pFfwfBT;(puxMyG delta 942 zcmZozz}&EadBP9o7fPEZ{+`MCf`x(M#xh0*i;0Rdn|Cr6%GDbhA1GDp=H)oRDA0V6 zx%*3ChQbU+fs7k783q1}{<+8~(0qi)`pXOPXaD~jA9#6&fq~(;>lX$F28M1|j&2W@ zz!x%4|Nn13!qVybrMdPCLkZ^&omq?mtw@G)!VQg>g=*-br~euMPmWN~uAlt$|NlPlo;F?5*acX zau`Y(iWpKEtQZs+av1U%5*bn$;u$g+@)?R5N*K}@Dj13wG#E4)lt6)pg544`b4rU+ ztrXHzOX5@VOY%}n;&bv7Q{oegi&Jxxaw;`6Ve0FVj80@I20J8|A&DV}p^_n0PJdBx}Iaj&MG2WOQcw(`Vp^c4=qm8>wjZK@) gM4M?gb8KGPe75;#!)(iGt7jWyn_;{8*NInb09RTZR{#J2 diff --git a/src/Inject/NosSmooth.Injector/NosSmooth.Injector.csproj b/src/Inject/NosSmooth.Injector/NosSmooth.Injector.csproj index a84c58e6c0a19c0efa1d77eff584b2aed3b1c4b4..8a44b1042e131c59660aadb3b4dfce1a2ee4f1a8 100644 --- a/src/Inject/NosSmooth.Injector/NosSmooth.Injector.csproj +++ b/src/Inject/NosSmooth.Injector/NosSmooth.Injector.csproj @@ -5,8 +5,8 @@ $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb;.dll enable enable - 1.0.3 - Support relative path when injecting. Inluce NosSmooth.Inject.dll in package and copy it to output. + 1.1.0 + Remove asserts from Inject and obtain errors from thread exit code. @@ -21,7 +21,7 @@ - Always + PreserveNewest