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
Use inline assembly
  • Loading branch information
zhengyu123 committed Jul 17, 2025
commit 33261bcefbc38e26d70cd67f33995e1dee5c5fb6
43 changes: 0 additions & 43 deletions ddprof-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -516,19 +516,15 @@ tasks.whenTaskAdded { task ->
onlyIf {
config.active
}

group = 'build'
description = "Link the ${config.name} build of the library"
source = fileTree("$buildDir/obj/main/${config.name}")
source.from(file("${projectDir}/build/obj/main/asm/assembly.o"))

linkedFile = file("$buildDir/lib/main/${config.name}/${osIdentifier()}/${archIdentifier()}/libjavaProfiler.${os().isLinux() ? 'so' : 'dylib'}")
def compileTask = tasks.findByName("compileLib${config.name.capitalize()}".toString())
if (compileTask != null) {
dependsOn compileTask
}
def asmTask = tasks.findByName("asm")
dependsOn asmTask
linkerArgs.addAll(config.linkerArgs)
toolChain = task.toolChain
targetPlatform = task.targetPlatform
Expand Down Expand Up @@ -609,45 +605,6 @@ tasks.register('javadocJar', Jar) {
from javadoc.destinationDir
}

tasks.register('asm', Exec) {
group = 'build'

def arch
if (archIdentifier() == 'x64') {
arch = '-D__x86_64__'
} else {
arch = '-D__aarch64__'
}

def os
if (osIdentifier() == 'macos') {
os = '-D__APPLE__'
} else {
os = '-D__LINUX__'
}
def asmDir = file("${projectDir}/src/main/assembler")
if (!asmDir.exists()) {
// no assembler files found, skipping
return
}

doLast {
// Ensure the output directory exists
file("${projectDir}/build/obj/main/asm").mkdirs()
}

// collect all assembler files and compile them
workingDir asmDir
inputs.files fileTree("${projectDir}/src/main/assembler") {
include '**/*.S'
}
def asmFiles = inputs.files.collect { it.absolutePath }
outputs.dir "${projectDir}/build/obj/main/asm"
// use gcc to compile assembler files
commandLine 'gcc', arch, os, '-c', '-x', 'assembler-with-cpp',
*asmFiles, '-o', "${projectDir}/build/obj/main/asm/assembly.o"
}

tasks.register('scanBuild', Exec) {
workingDir "${projectDir}/src/test/make"
commandLine 'scan-build'
Expand Down
7 changes: 1 addition & 6 deletions ddprof-lib/gtest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,7 @@ tasks.whenTaskAdded { task ->
group = 'verification'
description = "Run all Google Tests for the ${config.name} build of the library"
}
def libProject = project(':ddprof-lib')
def asmTask = libProject.tasks.findByName("asm")

libProject.file("src/test/cpp/").eachFile {
project(':ddprof-lib').file("src/test/cpp/").eachFile {

def testFile = it
def testName = it.name.substring(0, it.name.lastIndexOf('.'))
Expand All @@ -170,7 +167,6 @@ tasks.whenTaskAdded { task ->
group = 'build'
description = "Link the Google Test for the ${config.name} build of the library"
source = fileTree("$buildDir/obj/gtest/${config.name}/${testName}")
source.from(file("${rootDir}/ddprof-lib/build/obj/main/asm/assembly.o"))
linkedFile = binary
if (config.name != 'release') {
// linking the gtests using the minimizing release flags is making gtest unhappy
Expand All @@ -187,7 +183,6 @@ tasks.whenTaskAdded { task ->
libs = task.libs
inputs.files source
outputs.file linkedFile
dependsOn asmTask
}
def gtestExecuteTask = tasks.register("gtest${config.name.capitalize()}_${testName}", Exec) {
onlyIf {
Expand Down
64 changes: 0 additions & 64 deletions ddprof-lib/src/main/assembler/safefetch.S

This file was deleted.

67 changes: 63 additions & 4 deletions ddprof-lib/src/main/cpp/safeAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,74 @@
#endif
#endif

extern "C" char _SafeFetch32_continuation[] __attribute__ ((visibility ("hidden")));
extern "C" char _SafeFetch32_fault[] __attribute__ ((visibility ("hidden")));
#if defined(__x86_64__)
#ifdef __APPLE__
asm(R"(
.globl _safefetch32_impl
.private_extern _safefetch32_impl
_safefetch32_impl:
movl (%rdi), %eax
ret
.globl _safefetch32_continuation
.private_extern _safefetch32_continuation
_safefetch32_continuation:
movl %esi, %eax
ret
)");
#else
asm(R"(
.globl safefetch32_impl
.hidden safefetch32_impl
.type safefetch32_imp, %function
safefetch32_impl:
movl (%rdi), %eax
ret
.globl safefetch32_continuation
.hidden safefetch32_continuation
.type safefetch32_continuation, %function
safefetch32_continuation:
movl %esi, %eax
ret
)");
#endif // __APPLE__
#elif defined(__aarch64__)
#ifdef __APPLE__
asm(R"(
.globl _safefetch32_impl
.private_extern _safefetch32_impl
_safefetch32_impl:
ldr w0, [x0]
ret
.globl _safefetch32_continuation
.private_extern _safefetch32_continuation
_safefetch32_continuation:
mov x0, x1
ret
)");
#else
asm(R"(
.globl safefetch32_impl
.hidden safefetch32_impl
.type safefetch32_impl, %function
safefetch32_impl:
ldr w0, [x0]
ret
.globl safefetch32_continuation
.hidden safefetch32_continuation
.type safefetch32_continuation, %function
safefetch32_continuation:
mov x0, x1
ret
)");
#endif
#endif

bool SafeAccess::handle_safefetch(int sig, void* context) {
ucontext_t* uc = (ucontext_t*)context;
uintptr_t pc = uc->context_pc;
if ((sig == SIGSEGV || sig == SIGBUS) && uc != nullptr) {
if (pc == (uintptr_t)_SafeFetch32_fault) {
uc->context_pc = (uintptr_t)_SafeFetch32_continuation;
if (pc == (uintptr_t)safefetch32_impl) {
uc->context_pc = (uintptr_t)safefetch32_continuation;
return true;
}
}
Expand Down
5 changes: 3 additions & 2 deletions ddprof-lib/src/main/cpp/safeAccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
#include <cassert>
#include <stdint.h>

extern "C" int SafeFetch32_impl(int* adr, int errValue);
extern "C" int safefetch32_impl(int* adr, int errValue);
extern "C" int safefetch32_continuation(int* adr, int errValue);

#ifdef __clang__
#define NOINLINE __attribute__((noinline))
Expand All @@ -35,7 +36,7 @@ class SafeAccess {
public:

static inline int safeFetch32(int* ptr, int errorValue) {
return SafeFetch32_impl(ptr, errorValue);
return safefetch32_impl(ptr, errorValue);
}

static bool handle_safefetch(int sig, void* context);
Expand Down
Loading