From 3d21eb612443a15d89999b3781b189c0491f006e Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Fri, 17 May 2024 16:15:03 -0700 Subject: [PATCH] Fixes --- .../tests/Metadata/Decoding/SignatureDecoderTests.cs | 3 +++ .../System/Reflection/MethodBodyTests.cs | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/src/libraries/System.Reflection.Metadata/tests/Metadata/Decoding/SignatureDecoderTests.cs b/src/libraries/System.Reflection.Metadata/tests/Metadata/Decoding/SignatureDecoderTests.cs index 7570404923a7ab..8cbf89e13791bc 100644 --- a/src/libraries/System.Reflection.Metadata/tests/Metadata/Decoding/SignatureDecoderTests.cs +++ b/src/libraries/System.Reflection.Metadata/tests/Metadata/Decoding/SignatureDecoderTests.cs @@ -280,6 +280,9 @@ public static unsafe int DoSomething() byte[] bytes = new byte[] { 1, 2, 3 }; fixed (byte* bytePtr = bytes) { + // Take address of the local variable to prevent it from being optimized out by Roslyn + GC.KeepAlive((nint)(&bytePtr)); + return *bytePtr; } } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBodyTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBodyTests.cs index 146baf023bbfe0..9caa8c5e9abd1d 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBodyTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBodyTests.cs @@ -95,12 +95,17 @@ private static void MethodBodyExample(object arg) catch (Exception ex) { Console.WriteLine(ex.Message); + // Reference local variables to prevent them from being optimized out by Roslyn + GC.KeepAlive(ex); } finally { var1 = 3; var2 = "I am a new string!"; } + // Reference local variables to prevent them from being optimized out by Roslyn + GC.KeepAlive((object)var1); + GC.KeepAlive(var2); } } }