Call FlushInstructionCache in CFG test (#83168)
authorMichal Strehovský <MichalStrehovsky@users.noreply.github.com>
Thu, 9 Mar 2023 03:25:21 +0000 (12:25 +0900)
committerGitHub <noreply@github.com>
Thu, 9 Mar 2023 03:25:21 +0000 (12:25 +0900)
Should address https://github.com/dotnet/runtime/pull/82307#discussion_r1111436978.

src/tests/nativeaot/SmokeTests/ControlFlowGuard/ControlFlowGuard.cs

index 91575d4..5e9189a 100644 (file)
@@ -174,9 +174,11 @@ unsafe class ControlFlowGuardTests
         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 @@ unsafe class ControlFlowGuardTests
                 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;
     }
 }