Fix PAL_JitWriteProtect export on Linux (#54190)
authorJan Vorlicek <jan.vorlicek@volny.cz>
Tue, 15 Jun 2021 18:37:30 +0000 (20:37 +0200)
committerGitHub <noreply@github.com>
Tue, 15 Jun 2021 18:37:30 +0000 (20:37 +0200)
* Fix PAL_JitWriteProtect export on Linux

I have not realized that we actually export that symbol on Linux too and
that mscordac then have the dummy function missing.

* Code review feedback

* Disable PAL_JitWriteEnable usage in DBI / DAC

* Fix typo in amd64 version

Co-authored-by: Mike McLaughlin <mikem@microsoft.com>
src/coreclr/debug/di/CMakeLists.txt
src/coreclr/debug/inc/amd64/primitives.h
src/coreclr/debug/inc/arm/primitives.h
src/coreclr/debug/inc/arm64/primitives.h
src/coreclr/debug/inc/i386/primitives.h
src/coreclr/dlls/mscordac/mscordac_unixexports.src
src/coreclr/inc/executableallocator.h
src/coreclr/pal/inc/pal.h
src/coreclr/pal/src/map/virtual.cpp

index 55f34bd..b0272c4 100644 (file)
@@ -1,4 +1,5 @@
 add_definitions(
+    -DDBI_COMPILE
     -DFEATURE_METADATA_CUSTOM_DATA_SOURCE
     -DFEATURE_METADATA_DEBUGGEE_DATA_SOURCE
     -DFEATURE_NO_HOST
index d42dc66..d8d14b2 100644 (file)
@@ -12,7 +12,9 @@
 #ifndef PRIMITIVES_H_
 #define PRIMITIVES_H_
 
+#if !defined(DBI_COMPILE) && !defined(DACCESS_COMPILE)
 #include "executableallocator.h"
+#endif
 
 #ifndef CORDB_ADDRESS_TYPE
 typedef const BYTE                  CORDB_ADDRESS_TYPE;
@@ -189,9 +191,14 @@ inline void CORDbgInsertBreakpoint(UNALIGNED CORDB_ADDRESS_TYPE *address)
 {
     LIMITED_METHOD_CONTRACT;
 
-    ExecutableWriterHolder<void> breakpointWriterHolder((LPVOID)address, CORDbg_BREAK_INSTRUCTION_SIZE);
+#if !defined(DBI_COMPILE) && !defined(DACCESS_COMPILE)
+    ExecutableWriterHolder<CORDB_ADDRESS_TYPE> breakpointWriterHolder(address, CORDbg_BREAK_INSTRUCTION_SIZE);
+    UNALIGNED CORDB_ADDRESS_TYPE* addressRW = breakpointWriterHolder.GetRW();
+#else // !DBI_COMPILE && !DACCESS_COMPILE
+    UNALIGNED CORDB_ADDRESS_TYPE* addressRW = address;
+#endif // !DBI_COMPILE && !DACCESS_COMPILE
 
-    *((unsigned char*)breakpointWriterHolder.GetRW()) = 0xCC; // int 3 (single byte patch)
+    *((unsigned char*)addressRW) = 0xCC; // int 3 (single byte patch)
     FlushInstructionCache(GetCurrentProcess(), address, 1);
 
 }
@@ -202,9 +209,14 @@ inline void CORDbgSetInstruction(UNALIGNED CORDB_ADDRESS_TYPE* address,
     // In a DAC build, this function assumes the input is an host address.
     LIMITED_METHOD_DAC_CONTRACT;
 
-    ExecutableWriterHolder<void> instructionWriterHolder((LPVOID)address, sizeof(unsigned char));
+#if !defined(DBI_COMPILE) && !defined(DACCESS_COMPILE)
+    ExecutableWriterHolder<CORDB_ADDRESS_TYPE> instructionWriterHolder(address, sizeof(unsigned char));
+    UNALIGNED CORDB_ADDRESS_TYPE* addressRW = instructionWriterHolder.GetRW();
+#else // !DBI_COMPILE && !DACCESS_COMPILE
+    UNALIGNED CORDB_ADDRESS_TYPE* addressRW = address;
+#endif // !DBI_COMPILE && !DACCESS_COMPILE
 
-    *((unsigned char*)instructionWriterHolder.GetRW()) =
+    *((unsigned char*)addressRW) =
         (unsigned char) instruction;    // setting one byte is important
     FlushInstructionCache(GetCurrentProcess(), address, 1);
 
index eb29d83..c4e2d28 100644 (file)
@@ -12,7 +12,9 @@
 #ifndef PRIMITIVES_H_
 #define PRIMITIVES_H_
 
+#if !defined(DBI_COMPILE) && !defined(DACCESS_COMPILE)
 #include "executableallocator.h"
+#endif
 
 #ifndef THUMB_CODE
 #define THUMB_CODE 1
@@ -161,9 +163,14 @@ inline void CORDbgSetInstruction(CORDB_ADDRESS_TYPE* address,
     // In a DAC build, this function assumes the input is an host address.
     LIMITED_METHOD_DAC_CONTRACT;
 
-    ExecutableWriterHolder<void> instructionWriterHolder((LPVOID)address, sizeof(PRD_TYPE));
+#if !defined(DBI_COMPILE) && !defined(DACCESS_COMPILE)
+    ExecutableWriterHolder<CORDB_ADDRESS_TYPE> instructionWriterHolder(address, sizeof(PRD_TYPE));
+    CORDB_ADDRESS_TYPE* addressRW = instructionWriterHolder.GetRW();
+#else // !DBI_COMPILE && !DACCESS_COMPILE
+    CORDB_ADDRESS_TYPE* addressRW = address;
+#endif // !DBI_COMPILE && !DACCESS_COMPILE
 
-    CORDB_ADDRESS ptraddr = (CORDB_ADDRESS)instructionWriterHolder.GetRW();
+    CORDB_ADDRESS ptraddr = (CORDB_ADDRESS)addressRW;
     _ASSERTE(ptraddr & THUMB_CODE);
     ptraddr &= ~THUMB_CODE;
 
index 1fb1b9c..4f4c3f7 100644 (file)
@@ -12,7 +12,9 @@
 #ifndef PRIMITIVES_H_
 #define PRIMITIVES_H_
 
+#if !defined(DBI_COMPILE) && !defined(DACCESS_COMPILE)
 #include "executableallocator.h"
+#endif
 
 typedef NEON128                     FPRegister64;
 typedef const BYTE                  CORDB_ADDRESS_TYPE;
@@ -148,9 +150,13 @@ inline void CORDbgSetInstruction(CORDB_ADDRESS_TYPE* address,
     // In a DAC build, this function assumes the input is an host address.
     LIMITED_METHOD_DAC_CONTRACT;
 
+#if !defined(DBI_COMPILE) && !defined(DACCESS_COMPILE)
     ExecutableWriterHolder<void> instructionWriterHolder((LPVOID)address, sizeof(PRD_TYPE));
 
     ULONGLONG ptraddr = dac_cast<ULONGLONG>(instructionWriterHolder.GetRW());
+#else // !DBI_COMPILE && !DACCESS_COMPILE
+    ULONGLONG ptraddr = dac_cast<ULONGLONG>(address);
+#endif // !DBI_COMPILE && !DACCESS_COMPILE
     *(PRD_TYPE *)ptraddr = instruction;
     FlushInstructionCache(GetCurrentProcess(),
                           address,
index 0c1d4bc..313b42c 100644 (file)
@@ -12,7 +12,9 @@
 #ifndef PRIMITIVES_H_
 #define PRIMITIVES_H_
 
+#if !defined(DBI_COMPILE) && !defined(DACCESS_COMPILE)
 #include "executableallocator.h"
+#endif
 
 typedef const BYTE                  CORDB_ADDRESS_TYPE;
 typedef DPTR(CORDB_ADDRESS_TYPE)    PTR_CORDB_ADDRESS_TYPE;
@@ -149,9 +151,14 @@ inline void CORDbgInsertBreakpoint(UNALIGNED CORDB_ADDRESS_TYPE *address)
 {
     LIMITED_METHOD_CONTRACT;
 
-    ExecutableWriterHolder<void> breakpointWriterHolder((LPVOID)address, CORDbg_BREAK_INSTRUCTION_SIZE);
+#if !defined(DBI_COMPILE) && !defined(DACCESS_COMPILE)
+    ExecutableWriterHolder<CORDB_ADDRESS_TYPE> breakpointWriterHolder(address, CORDbg_BREAK_INSTRUCTION_SIZE);
+    UNALIGNED CORDB_ADDRESS_TYPE* addressRW = breakpointWriterHolder.GetRW();
+#else // !DBI_COMPILE && !DACCESS_COMPILE
+    UNALIGNED CORDB_ADDRESS_TYPE* addressRW = address;
+#endif // !DBI_COMPILE && !DACCESS_COMPILE
 
-    *((unsigned char*)breakpointWriterHolder.GetRW()) = 0xCC; // int 3 (single byte patch)
+    *((unsigned char*)addressRW) = 0xCC; // int 3 (single byte patch)
     FlushInstructionCache(GetCurrentProcess(), address, 1);
 }
 
index aa7352b..4fc0af3 100644 (file)
@@ -70,7 +70,6 @@ nativeStringResourceTable_mscorrc
 #PAL__open
 #PAL__pread
 #PAL__close
-#PAL_JitWriteProtect
 
 #_wcsicmp
 #_stricmp
index c71f613..ce0c6c2 100644 (file)
@@ -33,7 +33,7 @@ class ExecutableWriterHolder
         if (m_addressRX != NULL)
         {
             // TODO: mapping / unmapping for targets using double memory mapping  will be added with the double mapped allocator addition 
-#if defined(HOST_OSX) && defined(HOST_ARM64)
+#if defined(HOST_OSX) && defined(HOST_ARM64) && !defined(DACCESS_COMPILE)
             PAL_JitWriteProtect(false);
 #endif
         }
@@ -63,7 +63,7 @@ public:
     {
         m_addressRX = addressRX;
         m_addressRW = addressRX;
-#if defined(HOST_OSX) && defined(HOST_ARM64)
+#if defined(HOST_OSX) && defined(HOST_ARM64) && !defined(DACCESS_COMPILE)
         PAL_JitWriteProtect(true);
 #endif
     }
index 69d9052..8e0553e 100644 (file)
@@ -2760,14 +2760,16 @@ VirtualFree(
         IN SIZE_T dwSize,
         IN DWORD dwFreeType);
 
-#if defined(HOST_OSX)
+
+#if defined(HOST_OSX) && defined(HOST_ARM64)
 
 PALIMPORT
 VOID
 PALAPI
 PAL_JitWriteProtect(bool writeEnable);
 
-#endif // defined(HOST_OSX)
+#endif // defined(HOST_OSX) && defined(HOST_ARM64)
+
 
 PALIMPORT
 BOOL
index 9267f4f..645cf10 100644 (file)
@@ -1760,10 +1760,9 @@ ExitVirtualProtect:
     return bRetVal;
 }
 
-#if defined(HOST_OSX)
+#if defined(HOST_OSX) && defined(HOST_ARM64)
 PALAPI VOID PAL_JitWriteProtect(bool writeEnable)
 {
-#if defined(HOST_ARM64)
     thread_local int enabledCount = 0;
     if (writeEnable)
     {
@@ -1780,9 +1779,8 @@ PALAPI VOID PAL_JitWriteProtect(bool writeEnable)
         }
         _ASSERTE(enabledCount >= 0);
     }
-#endif // HOST_ARM64
 }
-#endif // HOST_OSX
+#endif // HOST_OSX && HOST_ARM64
 
 #if HAVE_VM_ALLOCATE
 //---------------------------------------------------------------------------------------