Skip to content
Open
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
17 changes: 15 additions & 2 deletions .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ jobs:
fail-fast: false
matrix:
include:
- name: E2E Test (Android API 21, NDK 23)
ANDROID_API: 21
ANDROID_NDK_VERSION: 23.2.8568313
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
- name: E2E Test (Android API 26, NDK 27)
ANDROID_API: 26
ANDROID_NDK_VERSION: 27.3.13750724
- name: E2E Test (Android API 31, NDK 27)
ANDROID_API: 31
ANDROID_NDK_VERSION: 27.3.13750724
- name: E2E Test (Android API 35, NDK 29)
ANDROID_API: 35
ANDROID_NDK_VERSION: 29.0.14206865
- name: E2E Test (Android API 36, NDK 30)
ANDROID_API: 36
ANDROID_NDK_VERSION: 30.0.16138531
Expand All @@ -99,7 +111,8 @@ jobs:
repository: getsentry/sentry-java
# first release with sentry-android-ndk-outproc, bump only when
# sentry-native-ndk-outproc changes need matching sentry-java changes
ref: 8.64.0
# ref: 8.64.0
ref: jpnurmi/fix/native-android-21 # TODO: pin release
path: sentry-java

- name: Set up Python
Expand Down Expand Up @@ -136,7 +149,7 @@ jobs:
api-level: ${{ env.ANDROID_API }}
ndk: ${{ env.ANDROID_NDK_VERSION }}
arch: ${{ env.ANDROID_ARCH }}
target: google_apis
target: default
emulator-boot-timeout: 1200
script: |
export ANDROID_NDK="$ANDROID_HOME/ndk/$ANDROID_NDK_VERSION"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

**Fixes**:

- Native/Android: support API 21+ (previously API 30+) with a file fallback for kernels without memfd. ([#2301](https://github.com/getsentry/sentry-native/pull/2301))
- Native: retain crash envelopes on OOM in the crash daemon. ([#2320](https://github.com/getsentry/sentry-native/pull/2320))
- Native: omit crash metadata in minidump-only mode. ([#2324](https://github.com/getsentry/sentry-native/pull/2324))
- Native: recover interrupted crash reports. ([#2330](https://github.com/getsentry/sentry-native/pull/2330))
Expand Down
1 change: 1 addition & 0 deletions ndk/lib/api/sentry-native-ndk.api
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public final class io/sentry/ndk/NdkOptions {
public final class io/sentry/ndk/SentryNdk {
public static fun close ()V
public static fun createCrashShm ()I
public static fun createCrashShm (Ljava/lang/String;)I
public static fun dupCrashShmFd ()I
public static fun init (Lio/sentry/ndk/NdkOptions;)V
public static fun initCrashDaemon (IIII)V
Expand Down
14 changes: 11 additions & 3 deletions ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private SentryNdk() {}
*/
private static native int initSentryNative(@NotNull final NdkOptions options);

private static native int nativeCreateCrashShm();
private static native int nativeCreateCrashShm(@Nullable String databasePath);

private static native void nativeInitCrashDaemon(
int shmFd, int notifyFd, int readyFd, int messageFd);
Expand Down Expand Up @@ -75,10 +75,18 @@ public static void init(@NotNull final NdkOptions options) {
}
}

/** Creates the shared memory of the crash IPC, or returns -1 if that fails. Needs API 30+. */
/** Creates the shared memory of the crash IPC, or returns -1 if that fails. Needs memfd. */
public static int createCrashShm() {
return createCrashShm(null);
}

/**
* Creates the shared memory of the crash IPC, or returns -1 if that fails. Falls back to a file
* in the database directory on kernels without memfd.
*/
public static int createCrashShm(@Nullable String databasePath) {
loadNativeLibraries();
return nativeCreateCrashShm();
return nativeCreateCrashShm(databasePath);
}

public static void initCrashDaemon(int shmFd, int notifyFd, int readyFd, int messageFd) {
Expand Down
22 changes: 17 additions & 5 deletions ndk/lib/src/main/jni/sentry.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

#include "sentry_http.h"

extern int sentry_android_crash_daemon_create_shm(void) __attribute__((weak));
extern int sentry_android_crash_daemon_create_shm(const char *database_path)
__attribute__((weak));
extern void sentry_android_crash_daemon_init(int shm_fd, int notify_fd,
int ready_fd, int message_fd) __attribute__((weak));
typedef struct sentry_crash_daemon_s sentry_crash_daemon_t;
Expand Down Expand Up @@ -568,10 +569,21 @@ Java_io_sentry_ndk_SentryNdk_initSentryNative(
}

JNIEXPORT jint JNICALL
Java_io_sentry_ndk_SentryNdk_nativeCreateCrashShm(JNIEnv *env, jclass cls) {
return sentry_android_crash_daemon_create_shm
? (jint) sentry_android_crash_daemon_create_shm()
: -1;
Java_io_sentry_ndk_SentryNdk_nativeCreateCrashShm(
JNIEnv *env,
jclass cls,
jstring database_path) {
if (!sentry_android_crash_daemon_create_shm) {
return -1;
}
const char *path_chars = database_path
? (*env)->GetStringUTFChars(env, database_path, 0)
: NULL;
int fd = sentry_android_crash_daemon_create_shm(path_chars);
if (path_chars) {
(*env)->ReleaseStringUTFChars(env, database_path, path_chars);
}
return (jint) fd;
}

JNIEXPORT void JNICALL
Expand Down
41 changes: 38 additions & 3 deletions src/backends/native/sentry_crash_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,17 @@ sentry__crash_ipc_message_decode(
# include <fcntl.h>
# include <pthread.h>
# include <sched.h>
# include <stdlib.h>
# include <sys/file.h>
# include <sys/socket.h>
# include <sys/stat.h>
# include <sys/syscall.h>
# include <unistd.h>

# if defined(SENTRY_PLATFORM_ANDROID)
# include <android/api-level.h>
# endif

# ifndef MFD_CLOEXEC
# define MFD_CLOEXEC 0x0001U
# endif
Expand Down Expand Up @@ -221,6 +226,28 @@ init_unlock(sentry_crash_ipc_t *ipc)
sentry__mutex_unlock(ipc->init_mutex);
}
}

int
sentry__crash_ipc_create_shm_file(const char *dir)
{
char path[SENTRY_CRASH_MAX_PATH];
int len = snprintf(path, sizeof(path), "%s/.sentry-shm-XXXXXX", dir);
if (len < 0 || (size_t)len >= sizeof(path)) {
return -1;
}
int fd = mkstemp(path);
if (fd < 0) {
return -1;
}
unlink(path);
// a sparse file raises SIGBUS on write when the disk is full
if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0
|| posix_fallocate(fd, 0, SENTRY_CRASH_SHM_SIZE) != 0) {
close(fd);
return -1;
}
return fd;
}
# else
static int
shm_create(sentry_crash_ipc_t *ipc)
Expand Down Expand Up @@ -443,6 +470,8 @@ sentry__crash_ipc_init_app_with_fds(int shm_fd, int notify_fd, int ready_fd,
ipc->init_mutex = init_mutex;
// the crash service attaches after the app has started
ipc->awaiting_ready = 1;
// below API 29, the app receives the service connection on its main thread
ipc->attach_on_main_thread = android_get_device_api_level() < 29;

return init_app(ipc, shm_fd, notify_fd, ready_fd, message_fd);
}
Expand Down Expand Up @@ -1807,10 +1836,9 @@ daemon_ready(sentry_crash_ipc_t *ipc)
{
#if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)
if (sentry__atomic_fetch(&ipc->awaiting_ready)) {
// no read, so that concurrent callers all see the signal
struct pollfd pfd = { ipc->ready_fd, POLLIN, 0 };
uint64_t val;
if (poll(&pfd, 1, 0) != 1 || !(pfd.revents & POLLIN)
|| read(ipc->ready_fd, &val, sizeof(val)) != sizeof(val)) {
if (poll(&pfd, 1, 0) != 1 || !(pfd.revents & POLLIN)) {
return false;
}
sentry__atomic_store(&ipc->awaiting_ready, 0);
Expand Down Expand Up @@ -1910,6 +1938,13 @@ sentry__crash_ipc_notify(sentry_crash_ipc_t *ipc)
if (!ipc) {
return false;
}
#if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)
// a crashed main thread can't attach the daemon
if (ipc->attach_on_main_thread && syscall(SYS_gettid) == getpid()
&& !daemon_ready(ipc)) {
return false;
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
#endif
if (sentry__atomic_fetch(&ipc->shmem->state)
== SENTRY_CRASH_STATE_CRASHING) {
return notify_fallback(ipc);
Expand Down
10 changes: 10 additions & 0 deletions src/backends/native/sentry_crash_ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ typedef struct {
volatile long stalled;
// set until a daemon that attaches later signals ready
volatile long awaiting_ready;
// a daemon that attaches later does so through the app's main thread
bool attach_on_main_thread;
uint64_t sequence;
// frame with an unsent tail, owned by the writer guard
char *torn_frame;
Expand Down Expand Up @@ -215,6 +217,14 @@ sentry_crash_ipc_t *sentry__crash_ipc_init_app(void);
int sentry__crash_ipc_create_memfd(void);
#endif

#if defined(SENTRY_PLATFORM_ANDROID)
/**
* Creates shared memory backed by an unlinked, preallocated file in `dir`, for
* kernels without memfd. Returns -1 on failure.
*/
int sentry__crash_ipc_create_shm_file(const char *dir);
#endif

/**
* Initialize IPC for daemon process.
* Attaches to existing shared memory created by app.
Expand Down
9 changes: 7 additions & 2 deletions src/backends/sentry_backend_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,14 @@ static android_crash_daemon_config_t g_android_crash_daemon_config
= { false, -1, -1, -1, -1 };

__attribute__((visibility("default"))) int
sentry_android_crash_daemon_create_shm(void)
sentry_android_crash_daemon_create_shm(const char *database_path)
{
return sentry__crash_ipc_create_memfd();
int fd = sentry__crash_ipc_create_memfd();
if (fd < 0 && database_path) {
// no memfd before kernel 3.17, e.g. below API 26
fd = sentry__crash_ipc_create_shm_file(database_path);
}
return fd;
}

__attribute__((visibility("default"))) void
Expand Down
4 changes: 1 addition & 3 deletions tests/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@
# Native backend works on all platforms (lightweight, no external dependencies)
# It's always available - tests explicitly set SENTRY_BACKEND: native in cmake
# On macOS ASAN, the signal handling conflicts with ASAN's memory interception
has_native = not (is_asan and sys.platform == "darwin") and (
not is_android or is_android >= 23
)
has_native = not (is_asan and sys.platform == "darwin")

has_sccache = os.environ.get("USE_SCCACHE") and shutil.which("sccache")
2 changes: 1 addition & 1 deletion tests/fixtures/android_e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pytest -s -v tests/test_e2e_sentry.py

Run these commands from the sentry-native root. Testing needs a running emulator
or device, `ANDROID_API`, and the `SENTRY_E2E_*` credentials documented in the test
module. The crash service needs API 30+; tombstone merging needs API 31+.
module. Tombstone merging needs API 31+.

The setup script builds this checkout's native NDK package and the Java SDK
into an isolated Maven repository, overriding the Java SDK's pinned NDK dependency.
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/android_e2e/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ android {

defaultConfig {
applicationId = "io.sentry.e2e"
minSdk = 30
minSdk = 21
targetSdk = project.property("targetSdk").toInteger()
ndk { abiFilters.add(System.getenv("ANDROID_ARCH")) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import io.sentry.Sentry;

public class CrashReceiver extends BroadcastReceiver {
Expand All @@ -19,6 +17,6 @@ public void onReceive(Context context, Intent intent) {
}
// keep the process active until the crash
goAsync();
new Handler(Looper.getMainLooper()).postDelayed(MainActivity::triggerNativeCrash, 1000);
MainActivity.crashLater(intent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import io.sentry.ISentryExecutorService;
import io.sentry.Sentry;
import io.sentry.android.core.SentryAndroid;
import io.sentry.protocol.User;
Expand All @@ -17,7 +19,7 @@ public void onCreate(Bundle state) {
super.onCreate(state);
init(getApplicationContext(), getIntent());
if (getIntent().getBooleanExtra("crash", true)) {
new Handler(getMainLooper()).postDelayed(MainActivity::triggerNativeCrash, 1000);
crashLater(getIntent());
}
}

Expand All @@ -43,6 +45,25 @@ static void init(Context context, Intent intent) {
});
}

static void crashLater(Intent intent) {
Handler handler = new Handler(Looper.getMainLooper());
int delay = intent.getIntExtra("crash_delay", 1000);
// the SDK executor syncs the scope to native first
ISentryExecutorService executor = Sentry.getCurrentScopes().getOptions().getExecutorService();
if (delay == 0) {
// below API 29, the main looper connects the crash service, so crash before
// returning to it
try {
executor.submit(() -> {}).get();
} catch (Exception e) {
throw new RuntimeException(e);
}
triggerNativeCrash();
return;
}
executor.submit(() -> handler.postDelayed(MainActivity::triggerNativeCrash, delay));
}

private static void callNativeCrash() {
crash();
}
Expand Down
Loading
Loading