Skip to content
Open

demo #124

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
40 changes: 40 additions & 0 deletions test/shared_library_unwind/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
load("@rules_cc//cc:defs.bzl", "cc_test")
load("@rules_rs//rs:rust_shared_library.bzl", "rust_shared_library")
load("@rules_shell//shell:sh_test.bzl", "sh_test")

_LINUX_ONLY = select({
"@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
})

rust_shared_library(
name = "filewatcher_jni",
srcs = ["filewatcher_jni.rs"],
crate_name = "filewatcher_jni",
edition = "2021",
)

sh_test(
name = "filewatcher_jni_unwind_link_test",
srcs = ["unwind_link_test.sh"],
args = [
"$(location :filewatcher_jni)",
"$(location @llvm//tools:llvm-nm)",
"$(location @llvm//tools:llvm-readelf)",
],
data = [
":filewatcher_jni",
"@llvm//tools:llvm-nm",
"@llvm//tools:llvm-readelf",
],
target_compatible_with = _LINUX_ONLY,
)

cc_test(
name = "filewatcher_jni_dlopen_test",
srcs = ["dlopen_test.c"],
args = ["$(location :filewatcher_jni)"],
data = [":filewatcher_jni"],
linkopts = ["-ldl"],
target_compatible_with = _LINUX_ONLY,
)
24 changes: 24 additions & 0 deletions test/shared_library_unwind/dlopen_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <dlfcn.h>
#include <stdio.h>

int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s <shared-library>\n", argv[0]);
return 2;
}

void *handle = dlopen(argv[1], RTLD_NOW | RTLD_LOCAL);
if (!handle) {
fprintf(stderr, "dlopen failed: %s\n", dlerror());
return 1;
}

if (!dlsym(handle, "Java_com_example_FileWatcher_nativeInit")) {
fprintf(stderr, "dlsym failed: %s\n", dlerror());
dlclose(handle);
return 1;
}

dlclose(handle);
return 0;
}
13 changes: 13 additions & 0 deletions test/shared_library_unwind/filewatcher_jni.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::ffi::c_void;

unsafe extern "C" {
fn _Unwind_GetIP(ctx: *mut c_void) -> usize;
fn _Unwind_GetIPInfo(ctx: *mut c_void, ip_before_insn: *mut i32) -> usize;
}

#[no_mangle]
pub extern "C" fn Java_com_example_FileWatcher_nativeInit() -> usize {
let get_ip = _Unwind_GetIP as usize;
let get_ip_info = _Unwind_GetIPInfo as usize;
get_ip ^ get_ip_info
}
14 changes: 14 additions & 0 deletions test/shared_library_unwind/unwind_link_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail

lib="$1"
llvm_nm="$2"
llvm_readelf="$3"

undefined="$("$llvm_nm" -m -u "$lib")"
echo "$undefined" | grep '_Unwind_GetIP$'
echo "$undefined" | grep '_Unwind_GetIPInfo$'

dynamic="$("$llvm_readelf" --dynamic "$lib")"
echo "$dynamic" | grep 'Shared library: \[libunwind\.so'
echo "$dynamic" | grep 'RUNPATH'
Loading