From 93c0532dd85a0ef7f821836c6fb676461d1a0be0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 9 Mar 2023 08:31:48 +0900 Subject: [PATCH] Call FlushInstructionCache in CFG test Should address https://github.com/dotnet/runtime/pull/82307#discussion_r1111436978. --- .../SmokeTests/ControlFlowGuard/ControlFlowGuard.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/tests/nativeaot/SmokeTests/ControlFlowGuard/ControlFlowGuard.cs b/src/tests/nativeaot/SmokeTests/ControlFlowGuard/ControlFlowGuard.cs index 91575d469bc364..5e9189ac90a572 100644 --- a/src/tests/nativeaot/SmokeTests/ControlFlowGuard/ControlFlowGuard.cs +++ b/src/tests/nativeaot/SmokeTests/ControlFlowGuard/ControlFlowGuard.cs @@ -174,9 +174,11 @@ static IntPtr CreateNewMethod() if (s_armed) flProtect |= 0x40000000 /* TARGETS_INVALID */; + uint allocSize = 4096; + IntPtr address = VirtualAlloc( lpAddress: IntPtr.Zero, - dwSize: 4096, + dwSize: allocSize, flAllocationType: 0x00001000 | 0x00002000 /* COMMIT+RESERVE*/, flProtect: flProtect); @@ -196,6 +198,15 @@ static IntPtr CreateNewMethod() throw new NotSupportedException(); } + [DllImport("kernel32", ExactSpelling = true)] + static extern IntPtr GetCurrentProcess(); + + [DllImport("kernel32", ExactSpelling = true, SetLastError = true)] + static extern int FlushInstructionCache(IntPtr hProcess, IntPtr lpBaseAddress, nuint dwSize); + + if (FlushInstructionCache(GetCurrentProcess(), address, allocSize) == 0) + Console.WriteLine("FlushInstructionCache failed"); + return address; } }