Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
v1
  • Loading branch information
zhengyu123 committed Jul 21, 2025
commit ae754ea899e9ace4f700e164415a60e835cce0ba
8 changes: 4 additions & 4 deletions ddprof-lib/src/main/cpp/safeAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <ucontext.h>

extern "C" int safefetch32_cont(int* adr, int errValue);
extern "C" int safefetch64_cont(int64_t* adr, int64_t errValue);
extern "C" int64_t safefetch64_cont(int64_t* adr, int64_t errValue);

#ifdef __APPLE__
#if defined(__x86_64__)
Expand Down Expand Up @@ -74,7 +74,7 @@ extern "C" int safefetch64_cont(int64_t* adr, int64_t errValue);
.globl safefetch32_impl
.hidden safefetch32_impl
.type safefetch32_imp, %function
safefetch32_impl:
safefetch64_impl:
movl (%rdi), %eax
ret
.globl safefetch32_cont
Expand All @@ -86,7 +86,7 @@ extern "C" int safefetch64_cont(int64_t* adr, int64_t errValue);
.globl safefetch64_impl
.hidden safefetch64_impl
.type safefetch64_imp, %function
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a typo ? This might be confusing for linkers / debuggers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, will fix it. Thanks!

safefetch32_impl:
safefetch64_impl:
movq (%rdi), %rax
ret
.globl safefetch64_cont
Expand Down Expand Up @@ -160,7 +160,7 @@ bool SafeAccess::handle_safefetch(int sig, void* context) {
uc->current_pc = (uintptr_t)safefetch32_cont;
return true;
} else if (pc == (uintptr_t)safefetch64_impl) {
uc->current_pc = (uintptr_t)safefetch32_cont;
uc->current_pc = (uintptr_t)safefetch64_cont;
return true;
}
}
Expand Down
10 changes: 5 additions & 5 deletions ddprof-lib/src/main/cpp/safeAccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <stdint.h>

extern "C" int safefetch32_impl(int* adr, int errValue);
extern "C" int safefetch64_impl(int64_t* adr, int64_t errValue);
extern "C" int64_t safefetch64_impl(int64_t* adr, int64_t errValue);

#ifdef __clang__
#define NOINLINE __attribute__((noinline))
Expand All @@ -39,7 +39,7 @@ class SafeAccess {
return safefetch32_impl(ptr, errorValue);
}

static inline int safeFetch64(int64_t* ptr, int64_t errorValue) {
static inline int64_t safeFetch64(int64_t* ptr, int64_t errorValue) {
return safefetch64_impl(ptr, errorValue);
}

Expand All @@ -54,10 +54,10 @@ class SafeAccess {
return static_cast<u32>(res);
}

static inline void *loadPtr(void **ptr, void *default_value) {
static inline void *loadPtr(void** ptr, void* default_value) {
#if defined(__x86_64__) || defined(__aarch64__)
int64_t res = safefetch64_impl((int64_t*)ptr, (int64_t)default_value);
return (void*)res;
int64_t res = safefetch64_impl((int64_t*)ptr, (int64_t)reinterpret_cast<uintptr_t>(default_value));
return (void*)static_cast<uintptr_t>(res);
#elif defined(__i386__) || defined(__arm__) || defined(__thumb__)
int res = safefetch32_impl((int*)ptr, (int)default_value);
return (void*)res;
Expand Down
32 changes: 32 additions & 0 deletions ddprof-lib/src/test/cpp/safefetch_ut.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <climits>
#include <signal.h>

#include "safeAccess.h"
Expand Down Expand Up @@ -39,6 +40,12 @@ TEST_F(SafeFetchTest, validAccess32) {
int* p = &i;
int res = SafeAccess::safeFetch32(p, -1);
EXPECT_EQ(res, i);
i = INT_MAX;
res = SafeAccess::safeFetch32(p, -1);
EXPECT_EQ(res, i);
i = INT_MIN;
res = SafeAccess::safeFetch32(p, 0);
EXPECT_EQ(res, i);
}


Expand All @@ -55,6 +62,12 @@ TEST_F(SafeFetchTest, validAccess64) {
int64_t* p = &i;
int64_t res = SafeAccess::safeFetch64(p, -1);
EXPECT_EQ(res, i);
i = LONG_MIN;
res = SafeAccess::safeFetch64(p, -19);
EXPECT_EQ(res, i);
i = LONG_MAX;
res = SafeAccess::safeFetch64(p, -1);
EXPECT_EQ(res, i);
}

TEST_F(SafeFetchTest, invalidAccess64) {
Expand All @@ -64,3 +77,22 @@ TEST_F(SafeFetchTest, invalidAccess64) {
res = SafeAccess::safeFetch64(p, -2);
EXPECT_EQ(res, -2);
}

TEST_F(SafeFetchTest, validAccessPtr) {
char c = 'a';
void* p = (void*)&c;
void** pp = &p;
void* res = SafeAccess::loadPtr(pp, nullptr);
EXPECT_EQ(res, p);
}

TEST_F(SafeFetchTest, invalidAccessPtr) {
int a, b;
void* ap = (void*)&a;
void* bp = (void*)&b;
void** pp = nullptr;
void* res = SafeAccess::loadPtr(pp, ap);
EXPECT_EQ(res, ap);
res = SafeAccess::loadPtr(pp, bp);
EXPECT_EQ(res, bp);
}