Skip to content

Commit 21f3287

Browse files
committed
v3
1 parent 35576dc commit 21f3287

6 files changed

Lines changed: 36 additions & 33 deletions

File tree

ddprof-lib/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ plugins {
33
id 'java'
44
id 'maven-publish'
55
id 'signing'
6-
id 'assembler'
76

87
id 'com.github.ben-manes.versions' version '0.27.0'
98
id 'de.undercouch.download' version '4.1.1'
@@ -621,8 +620,7 @@ tasks.register('asmTask', Exec) {
621620
} else {
622621
os = '-D__LINUX__'
623622
}
624-
625-
commandLine 'gcc', arch, os, 'src/main/assembler/safefetch.S', '-c', 'build/obj/safefetch.o'
623+
commandLine 'gcc', arch, os, 'src/main/assembler/safefetch.S', '-c', 'safefetch.o'
626624
}
627625

628626
tasks.register('scanBuild', Exec) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,13 @@ bool Profiler::crashHandler(int signo, siginfo_t *siginfo, void *ucontext) {
878878
return false;
879879
}
880880

881+
if (SafeAccess::handle_safefetch(signo, pc, ucontext)) {
882+
if (thrd != nullptr) {
883+
thrd->exitCrashHandler();
884+
}
885+
return true;
886+
}
887+
881888
uintptr_t length = SafeAccess::skipLoad(pc);
882889
if (length > 0) {
883890
// Skip the fault instruction, as if it successfully loaded NULL

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,30 @@
1616

1717

1818
#include "safeAccess.h"
19+
#include <ucontext.h>
1920

20-
SafeAccess::SafeFetch32 SafeAccess::_safeFetch32Func = nullptr;
21-
22-
void SafeAccess::initSafeFetch(CodeCache* libjvm) {
23-
// Hotspot JVM's safefetch implementation appears better, e.g. it actually returns errorValue,
24-
// when the address is invalid. let's use it whenever possible.
25-
// When the methods are not available, fallback to SafeAccess::load32() implementation.
26-
_safeFetch32Func = (SafeFetch32)libjvm->findSymbol("SafeFetch32_impl");
27-
if (_safeFetch32Func == nullptr && !WX_MEMORY) {
28-
// jdk11 stub implementation other than Macosx/aarch64
29-
void** entry = (void**)libjvm->findSymbol("_ZN12StubRoutines18_safefetch32_entryE");
30-
if (entry != nullptr && *entry != nullptr) {
31-
_safeFetch32Func = (SafeFetch32)*entry;
21+
22+
#ifdef __APPLE__
23+
24+
#if defined(__x86_64)
25+
#define context_pc context_rip
26+
#elif defined(__aarch64__)
27+
#define DU3_PREFIX(s, m) __ ## s.__ ## m
28+
#define context_pc uc_mcontext->DU3_PREFIX(ss,pc)
29+
#endif
30+
31+
#endif
32+
33+
extern "C" char _SafeFetch32_continuation[] __attribute__ ((visibility ("hidden")));
34+
extern "C" char _SafeFetch32_fault[] __attribute__ ((visibility ("hidden")));
35+
36+
bool SafeAccess::handle_safefetch(int sig, uintptr_t pc, void* context) {
37+
ucontext_t* uc = (ucontext_t*)context;
38+
if ((sig == SIGSEGV || sig == SIGBUS) && uc != nullptr) {
39+
if (pc == (uintptr_t)_SafeFetch32_fault) {
40+
uc->context_pc == (uintptr_t)_SafeFetch32_continuation;
41+
return true;
3242
}
3343
}
34-
// Fallback
35-
if (_safeFetch32Func == nullptr) {
36-
_safeFetch32Func = (SafeFetch32)load32;
37-
}
44+
return false;
3845
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <cassert>
2323
#include <stdint.h>
2424

25+
extern "C" int SafeFetch32_impl(int* adr, int errValue);
26+
2527
#ifdef __clang__
2628
#define NOINLINE __attribute__((noinline))
2729
#else
@@ -30,18 +32,14 @@
3032
#define NOADDRSANITIZE __attribute__((no_sanitize("address")))
3133

3234
class SafeAccess {
33-
private:
34-
typedef int (*SafeFetch32)(int* ptr, int errorValue);
35-
static SafeFetch32 _safeFetch32Func;
36-
3735
public:
38-
static void initSafeFetch(CodeCache* libjvm);
3936

4037
static inline int safeFetch32(int* ptr, int errorValue) {
41-
assert(_safeFetch32Func != nullptr);
42-
return _safeFetch32Func(ptr, errorValue);
38+
return SafeFetch32_impl(ptr, errorValue);
4339
}
4440

41+
static bool handle_safefetch(int sig, uintptr_t pc, void* context);
42+
4543
NOINLINE NOADDRSANITIZE __attribute__((aligned(16))) static void *load(void **ptr) {
4644
return *ptr;
4745
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ namespace ddprof {
4444
initOffsets();
4545
initJvmFunctions();
4646
initUnsafeFunctions();
47-
initSafeFetch(libjvm);
4847
}
4948

5049
void VMStructs_::initOffsets() {
@@ -99,10 +98,6 @@ namespace ddprof {
9998
}
10099
}
101100

102-
void VMStructs_::initSafeFetch(CodeCache* libjvm) {
103-
SafeAccess::initSafeFetch(libjvm);
104-
}
105-
106101
const void *VMStructs_::findHeapUsageFunc() {
107102
if (VM::hotspot_version() < 17) {
108103
// For JDK 11 it is really unreliable to find the memory_usage function -

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ namespace ddprof {
5252
static void initOffsets();
5353
static void initJvmFunctions();
5454
static void initUnsafeFunctions();
55-
// We need safe access for all jdk versions
56-
static void initSafeFetch(CodeCache* libjvm);
5755

5856
static void checkNativeBinding(jvmtiEnv *jvmti, JNIEnv *jni, jmethodID method,
5957
void *address);

0 commit comments

Comments
 (0)