Skip to content

Commit 002f244

Browse files
authored
Mitigating VMMethod::id() crash (#359)
1 parent 91ddb64 commit 002f244

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

ddprof-lib/src/main/cpp/safeAccess.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class SafeAccess {
7373

7474
NOINLINE __attribute__((aligned(16)))
7575
static void *loadPtr(void** ptr, void* default_value);
76+
77+
static inline bool isReadable(void* ptr) {
78+
return load32((int32_t*)ptr, 1) != 1 ||
79+
load32((int32_t*)ptr, -1) != -1;
80+
}
7681
};
7782

7883
#endif // _SAFEACCESS_H

ddprof-lib/src/main/cpp/stackWalker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static inline void fillFrame(ASGCT_CallFrame& frame, FrameTypeId type, int bci,
5959
}
6060

6161
static jmethodID getMethodId(VMMethod* method) {
62-
if (!inDeadZone(method) && aligned((uintptr_t)method)) {
62+
if (!inDeadZone(method) && aligned((uintptr_t)method) && SafeAccess::isReadable((void*)method)) {
6363
return method->validatedId();
6464
}
6565
return NULL;

ddprof-lib/src/test/cpp/safefetch_ut.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ TEST_F(SafeFetchTest, invalidAccessPtr) {
105105
EXPECT_EQ(res, bp);
106106
}
107107

108+
TEST_F(SafeFetchTest, isReadable) {
109+
char c = 'x';
110+
EXPECT_TRUE(SafeAccess::isReadable(&c));
111+
EXPECT_FALSE(SafeAccess::isReadable(nullptr));
112+
}
113+
108114
/**
109115
* Tests that safeFetch32 correctly handles mprotected memory.
110116
*

0 commit comments

Comments
 (0)