Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 4a2a446

Browse files
Konstantin BaladurinRussKeldorph
authored andcommitted
PEImageLayout: clear instruction cache after relocations
It fixes crashes on arm when using AOT images.
1 parent 2023be1 commit 4a2a446

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/vm/peimagelayout.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ void PEImageLayout::ApplyBaseRelocations()
149149
SIZE_T cbWriteableRegion = 0;
150150
DWORD dwOldProtection = 0;
151151

152+
BOOL bRelocDone = FALSE;
153+
152154
COUNT_T dirPos = 0;
153155
while (dirPos < dirSize)
154156
{
@@ -175,10 +177,20 @@ void PEImageLayout::ApplyBaseRelocations()
175177
// Restore the protection
176178
if (dwOldProtection != 0)
177179
{
180+
BOOL bExecRegion = (dwOldProtection & (PAGE_EXECUTE | PAGE_EXECUTE_READ |
181+
PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)) != 0;
182+
178183
if (!ClrVirtualProtect(pWriteableRegion, cbWriteableRegion,
179184
dwOldProtection, &dwOldProtection))
180185
ThrowLastError();
181186

187+
if (bRelocDone && bExecRegion)
188+
{
189+
ClrFlushInstructionCache(pWriteableRegion, cbWriteableRegion);
190+
}
191+
192+
bRelocDone = FALSE;
193+
182194
dwOldProtection = 0;
183195
}
184196

@@ -221,11 +233,13 @@ void PEImageLayout::ApplyBaseRelocations()
221233
{
222234
case IMAGE_REL_BASED_PTR:
223235
*(TADDR *)address += delta;
236+
bRelocDone = TRUE;
224237
break;
225238

226239
#ifdef _TARGET_ARM_
227240
case IMAGE_REL_BASED_THUMB_MOV32:
228241
PutThumb2Mov32((UINT16 *)address, GetThumb2Mov32((UINT16 *)address) + delta);
242+
bRelocDone = TRUE;
229243
break;
230244
#endif
231245

@@ -245,10 +259,18 @@ void PEImageLayout::ApplyBaseRelocations()
245259
#ifndef CROSSGEN_COMPILE
246260
if (dwOldProtection != 0)
247261
{
262+
BOOL bExecRegion = (dwOldProtection & (PAGE_EXECUTE | PAGE_EXECUTE_READ |
263+
PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)) != 0;
264+
248265
// Restore the protection
249266
if (!ClrVirtualProtect(pWriteableRegion, cbWriteableRegion,
250267
dwOldProtection, &dwOldProtection))
251268
ThrowLastError();
269+
270+
if (bRelocDone && bExecRegion)
271+
{
272+
ClrFlushInstructionCache(pWriteableRegion, cbWriteableRegion);
273+
}
252274
}
253275
#endif // CROSSGEN_COMPILE
254276
}

0 commit comments

Comments
 (0)