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

Commit 1609011

Browse files
committed
Merge pull request #3976 from mikem8361/readmemoryav
Fix ReadMemory AV (Issue #3916)
2 parents 839d419 + adeb0c8 commit 1609011

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/debug/shared/dbgtransportsession.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,34 @@ DbgTransportSession::Message * DbgTransportSession::RemoveMessageFromSendQueue(D
11261126
#endif
11271127

11281128
#ifndef RIGHT_SIDE_COMPILE
1129+
1130+
#ifdef FEATURE_PAL
1131+
__attribute__((noinline))
1132+
__attribute__((optnone))
1133+
static void
1134+
ProbeMemory(__in_ecount(cbBuffer) volatile PBYTE pbBuffer, DWORD cbBuffer, bool fWriteAccess)
1135+
{
1136+
// Need an throw in this function to fool the C++ runtime into handling the
1137+
// possible h/w exception below.
1138+
if (pbBuffer == NULL)
1139+
{
1140+
throw PAL_SEHException();
1141+
}
1142+
1143+
// Simple one byte at a time probing
1144+
while (cbBuffer > 0)
1145+
{
1146+
volatile BYTE read = *pbBuffer;
1147+
if (fWriteAccess)
1148+
{
1149+
*pbBuffer = read;
1150+
}
1151+
++pbBuffer;
1152+
--cbBuffer;
1153+
}
1154+
}
1155+
#endif // FEATURE_PAL
1156+
11291157
// Check read and optionally write memory access to the specified range of bytes. Used to check
11301158
// ReadProcessMemory and WriteProcessMemory requests.
11311159
HRESULT DbgTransportSession::CheckBufferAccess(__in_ecount(cbBuffer) PBYTE pbBuffer, DWORD cbBuffer, bool fWriteAccess)
@@ -1138,7 +1166,6 @@ HRESULT DbgTransportSession::CheckBufferAccess(__in_ecount(cbBuffer) PBYTE pbBuf
11381166

11391167
// VirtualQuery doesn't know much about memory allocated outside of PAL's VirtualAlloc
11401168
// that's why on Unix we can't rely on in to detect invalid memory reads
1141-
// TODO: We need to find and use appropriate memory map API on other operating systems.
11421169
#ifndef FEATURE_PAL
11431170
do
11441171
{
@@ -1179,11 +1206,24 @@ HRESULT DbgTransportSession::CheckBufferAccess(__in_ecount(cbBuffer) PBYTE pbBuf
11791206
}
11801207
}
11811208
while (cbBuffer > 0);
1209+
#else
1210+
try
1211+
{
1212+
// Need to explicit h/w exception holder so to catch them in ProbeMemory
1213+
CatchHardwareExceptionHolder __catchHardwareException;
1214+
1215+
ProbeMemory(pbBuffer, cbBuffer, fWriteAccess);
1216+
}
1217+
catch(...)
1218+
{
1219+
return HRESULT_FROM_WIN32(ERROR_INVALID_ADDRESS);
1220+
}
11821221
#endif
11831222

11841223
// The specified region has passed all of our checks.
11851224
return S_OK;
11861225
}
1226+
11871227
#endif // !RIGHT_SIDE_COMPILE
11881228

11891229
// Initialize all session state to correct starting values. Used during Init() and on the LS when we

src/pal/src/thread/context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ DWORD CONTEXTGetExceptionCodeForSignal(const siginfo_t *siginfo,
655655
return exceptionCode;
656656
}
657657
}
658-
// fall through
658+
return EXCEPTION_ACCESS_VIOLATION;
659659
}
660660
#endif
661661
default:

0 commit comments

Comments
 (0)