From 6b7566fed8607c48e8dba3c8be781193fe3707df Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Mon, 8 Jun 2020 23:21:22 +0200 Subject: [PATCH] Fixed error in opcode parameter that only shows itself in mono under 6.x --- .../Mono/BoringTLSVerifyFlagsPatch.cs | 12 ++++++---- src/Sonarr.RuntimePatches/RuntimePatchBase.cs | 22 ++++++++++++++++--- src/Sonarr.RuntimePatches/RuntimePatcher.cs | 21 ++++++++++++++++-- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/Sonarr.RuntimePatches/Mono/BoringTLSVerifyFlagsPatch.cs b/src/Sonarr.RuntimePatches/Mono/BoringTLSVerifyFlagsPatch.cs index a7eec5b0f..5e14dc6b7 100644 --- a/src/Sonarr.RuntimePatches/Mono/BoringTLSVerifyFlagsPatch.cs +++ b/src/Sonarr.RuntimePatches/Mono/BoringTLSVerifyFlagsPatch.cs @@ -46,10 +46,12 @@ namespace NzbDrone.RuntimePatches.Mono var patchable = codes.Matches(OpCodes.Ldstr, OpCodes.Ldc_I4_1, OpCodes.Call, OpCodes.Ret); + Instance.DebugOpcodes("Before", codes); + var targetType = method.DeclaringType; - var copyMethod = targetType.GetMethod("Copy"); - var disposeMethod = targetType.GetMethod("Dispose"); - var setFlagsMethod = targetType.GetMethod("SetFlags"); + var copyMethod = targetType.GetMethod("Copy", new Type[0]); + var disposeMethod = targetType.GetMethod("Dispose", new Type[0]); + var setFlagsMethod = targetType.GetMethod("SetFlags", new[] { typeof(ulong) }); if (patchable && copyMethod != null && disposeMethod != null && setFlagsMethod != null) { @@ -64,10 +66,12 @@ namespace NzbDrone.RuntimePatches.Mono codes.Add(new CodeInstruction(OpCodes.Callvirt, disposeMethod)); // Dispose the original codes.Add(new CodeInstruction(OpCodes.Ldloc, copy)); codes.Add(new CodeInstruction(OpCodes.Dup)); - codes.Add(new CodeInstruction(OpCodes.Ldc_I4, 0x8000)); // X509_V_FLAG_TRUSTED_FIRST + codes.Add(new CodeInstruction(OpCodes.Ldc_I8, 0x8000L)); // X509_V_FLAG_TRUSTED_FIRST codes.Add(new CodeInstruction(OpCodes.Call, setFlagsMethod)); // SetFlags is an or-operation codes.Add(new CodeInstruction(OpCodes.Ret)); + Instance.DebugOpcodes("After", codes); + Instance.Debug($"Patch applied to method {method.GetSimplifiedName()}"); } else diff --git a/src/Sonarr.RuntimePatches/RuntimePatchBase.cs b/src/Sonarr.RuntimePatches/RuntimePatchBase.cs index 3f8cc13e3..76bd63e0b 100644 --- a/src/Sonarr.RuntimePatches/RuntimePatchBase.cs +++ b/src/Sonarr.RuntimePatches/RuntimePatchBase.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Reflection; using HarmonyLib; @@ -8,6 +9,8 @@ namespace NzbDrone.RuntimePatches { private Harmony _harmony; + internal static bool IsDebug; + public virtual bool ShouldPatch() => true; protected abstract void Patch(); @@ -101,11 +104,24 @@ namespace NzbDrone.RuntimePatches return null; } + protected void DebugOpcodes(string prefix, List codes) + { + if (IsDebug) + { + Log($"Opcodes {prefix}:"); + foreach (var code in codes) + { + Console.WriteLine($" {code}"); + } + } + } + protected void Debug(string log) { -#if DEBUG - Log(log); -#endif + if (IsDebug) + { + Log(log); + } } protected void Error(string log) diff --git a/src/Sonarr.RuntimePatches/RuntimePatcher.cs b/src/Sonarr.RuntimePatches/RuntimePatcher.cs index 06d7cde64..d0c251671 100644 --- a/src/Sonarr.RuntimePatches/RuntimePatcher.cs +++ b/src/Sonarr.RuntimePatches/RuntimePatcher.cs @@ -9,9 +9,26 @@ namespace NzbDrone.RuntimePatches { public static void Initialize() { - var env = Environment.GetEnvironmentVariable("DISABLE_RUNTIMEPATCHES"); - if (env != "1") + var envDisableRuntimePatches = Environment.GetEnvironmentVariable("DISABLE_RUNTIMEPATCHES"); + var envDebugRuntimePatches = Environment.GetEnvironmentVariable("DEBUG_RUNTIMEPATCHES"); + + if (envDisableRuntimePatches != "1") { + if (envDebugRuntimePatches == "1") + { + RuntimePatchBase.IsDebug = true; + } + else if (envDebugRuntimePatches == "0") + { + RuntimePatchBase.IsDebug = false; + } + else + { +#if DEBUG + RuntimePatchBase.IsDebug = true; +#endif + } + try { ApplyPatches();