Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions ddprof-lib/src/main/cpp/hotspot/hotspotSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1264,11 +1264,10 @@ static void patchClassLoaderData(JNIEnv* jni, jclass klass) {
int method_count = vmklass->methodCount();
if (method_count > 0) {
VMClassLoaderData *cld = vmklass->classLoaderData();
cld->lock();
VMClassLoaderDataMutexLocker locker(cld->mutex());
for (int i = 0; i < method_count; i += MethodList::SIZE) {
*cld->methodList() = new MethodList(*cld->methodList());
*cld->methodList() = new MethodList(cld->methodList());
}
Comment on lines +1267 to 1270
cld->unlock();
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions ddprof-lib/src/main/cpp/hotspot/vmStructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,3 +1165,15 @@ HeapUsage HeapUsage::get(bool allow_jmx) {
}
return usage;
}

VMClassLoaderDataMutexLocker::VMClassLoaderDataMutexLocker(void* mutex) : _mutex(mutex) {
if (_mutex != nullptr) {
_lock_func(_mutex);
}
}

VMClassLoaderDataMutexLocker::~VMClassLoaderDataMutexLocker() {
if (_mutex != nullptr) {
_unlock_func(_mutex);
}
}
Comment on lines +1169 to +1179
41 changes: 15 additions & 26 deletions ddprof-lib/src/main/cpp/hotspot/vmStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,18 +449,11 @@ class VMStructs {
static void checkNativeBinding(jvmtiEnv *jvmti, JNIEnv *jni, jmethodID method, void *address);
static const void *findHeapUsageFunc();

const char* at(int offset) {
const char* ptr = (const char*)this + offset;
assert(crashProtectionActive() || SafeAccess::isReadable(ptr));
// Poison only the returned pointer; the assert above sees the real ptr.
return INJECT_FAULT_ADDRESS_RARE(ptr);
}
const char* at(int offset);
const char* at(int offset) const;

const char* at(int offset) const {
const char* ptr = (const char*)this + offset;
assert(crashProtectionActive() || SafeAccess::isReadable(ptr));
return INJECT_FAULT_ADDRESS_RARE(ptr);
}
template <typename T, bool safe = false>
T load_at_offset(int offset) const;

static bool goodPtr(const void* ptr) {
return (uintptr_t)ptr >= 0x1000 && ((uintptr_t)ptr & (sizeof(uintptr_t) - 1)) == 0;
Expand Down Expand Up @@ -635,24 +628,20 @@ DECLARE(VMSymbol)
static int bodyOffset() { return _symbol_body_offset; }
DECLARE_END

DECLARE(VMClassLoaderData)
class VMClassLoaderDataMutexLocker : protected VMStructs {
private:
void* mutex() {
return *(void**) at(sizeof(uintptr_t) * 3);
}

void* _mutex;
public:
void lock() {
_lock_func(mutex());
}

void unlock() {
_unlock_func(mutex());
}
VMClassLoaderDataMutexLocker(void* mutex);
Comment on lines +631 to +635
~VMClassLoaderDataMutexLocker();
};

MethodList** methodList() {
return (MethodList**) at(sizeof(uintptr_t) * 6 + 8);
}
// VMClassLoaderData is used outside of protected (longjmp) context,
// so both methods are protected by SafeAccess
DECLARE(VMClassLoaderData)
public:
inline void* mutex();
inline MethodList* methodList();
DECLARE_END

DECLARE(VMKlass)
Expand Down
32 changes: 32 additions & 0 deletions ddprof-lib/src/main/cpp/hotspot/vmStructs.inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ inline T* cast_to(const void* ptr) {
return reinterpret_cast<T*>(const_cast<void*>(ptr));
}

inline const char* VMStructs::at(int offset) {
const char* ptr = (const char*)this + offset;
assert(crashProtectionActive() || SafeAccess::isReadable(ptr));
// Poison only the returned pointer; the assert above sees the real ptr.
return INJECT_FAULT_ADDRESS_RARE(ptr);
}

inline const char* VMStructs::at(int offset) const {
const char* ptr = (const char*)this + offset;
assert(crashProtectionActive() || SafeAccess::isReadable(ptr));
return INJECT_FAULT_ADDRESS_RARE(ptr);
}

template <typename T, bool safe>
T VMStructs::load_at_offset(int offset) const {
const char* ptr = at(offset);
if (safe) {
return (T)SafeAccess::loadPtr((void**)ptr, nullptr);
} else {
return *((T*)ptr);
}
}

VMThread* VMThread::current() {
assert(VM::isHotspot());
return VMThread::cast(JVMThread::current());
Expand Down Expand Up @@ -188,5 +211,14 @@ u16 VMConstMethod::signatureIndex() const {
return *(u16*)at(_constmethod_sig_index_offset);
}

// ClassLoaderData accesses are unprotected
void* VMClassLoaderData::mutex() {
return load_at_offset<void*, true /*safe*/>(sizeof(uintptr_t) * 3);
}

MethodList* VMClassLoaderData::methodList() {
return load_at_offset<MethodList*, true /*safe*/>(sizeof(uintptr_t) * 6 + 8);
}
Comment on lines +214 to +221


#endif // _HOTSPOT_VMSTRUCTS_INLINE_H
Loading