Skip to content

Commit 397fe29

Browse files
committed
fix(android): Run the NDK crash service below API 30
Nothing in the crash service needs API 30. Drop the check in `SentryNdkOutproc.init()`, and pass the database directory to `SentryNdk.createCrashShm()` so that the crash IPC falls back to a file on kernels without memfd. `Application.getProcessName()` needs API 28, so read the process name from `/proc/self/cmdline` on older versions to skip SDK init in the crash service process.
1 parent f532ea3 commit 397fe29

6 files changed

Lines changed: 25 additions & 19 deletions

File tree

‎sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import io.sentry.android.fragment.FragmentLifecycleIntegration;
2121
import io.sentry.android.timber.SentryTimberIntegration;
2222
import io.sentry.util.AutoClosableReentrantLock;
23+
import io.sentry.util.FileUtils;
24+
import java.io.File;
25+
import java.io.IOException;
2326
import java.lang.reflect.InvocationTargetException;
2427
import java.util.ArrayList;
2528
import java.util.List;
@@ -291,13 +294,26 @@ private static void deduplicateIntegrations(
291294
}
292295
}
293296

294-
// the crash service only runs on API 30+
295297
@SuppressLint("NewApi")
296298
private static boolean isNdkCrashServiceProcess() {
297-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
298-
return false;
299-
}
300-
final @Nullable String processName = Application.getProcessName();
299+
final @Nullable String processName =
300+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
301+
? Application.getProcessName()
302+
: readProcessName();
301303
return processName != null && processName.endsWith(NDK_CRASH_SERVICE_PROCESS_SUFFIX);
302304
}
305+
306+
// argv[0] of an app process is its process name
307+
private static @Nullable String readProcessName() {
308+
try {
309+
final @Nullable String cmdline = FileUtils.readText(new File("/proc/self/cmdline"));
310+
if (cmdline == null) {
311+
return null;
312+
}
313+
final int end = cmdline.indexOf('\0');
314+
return end >= 0 ? cmdline.substring(0, end) : cmdline;
315+
} catch (IOException e) {
316+
return null;
317+
}
318+
}
303319
}

‎sentry-android-ndk-outproc/README.md‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,3 @@ configurations.configureEach {
1616
```
1717

1818
Otherwise, the build fails because it contains both native backends.
19-
20-
The crash service needs API 30+. On older versions, native crash reporting is
21-
disabled.

‎sentry-android-ndk-outproc/src/main/java/io/sentry/android/ndk/outproc/SentryNdkCrashService.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ protected boolean onTransact(
140140
return null;
141141
}
142142

143-
final int shmFd = io.sentry.ndk.SentryNdk.createCrashShm();
143+
final int shmFd = io.sentry.ndk.SentryNdk.createCrashShm(nativeDatabaseDir.getPath());
144144
if (shmFd < 0) {
145145
options.getLogger().log(SentryLevel.DEBUG, "Failed to create NDK crash shared memory.");
146146
return null;

‎sentry-android-ndk-outproc/src/main/java/io/sentry/android/ndk/outproc/SentryNdkOutproc.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.annotation.SuppressLint;
44
import android.content.Context;
55
import android.content.ServiceConnection;
6-
import android.os.Build;
76
import io.sentry.SentryIntegrationPackageStorage;
87
import io.sentry.android.core.NdkHandlerStrategy;
98
import io.sentry.android.core.SentryAndroidOptions;
@@ -68,8 +67,8 @@ public static void init(@NotNull final SentryAndroidOptions options) {
6867
public static void init(
6968
@Nullable final Context context, @NotNull final SentryAndroidOptions options) {
7069
// no fork/exec fallback, the native crash daemon only runs in the crash service
71-
if (context == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
72-
throw new IllegalStateException("The NDK crash service needs a context and API 30+");
70+
if (context == null) {
71+
throw new IllegalStateException("The NDK crash service needs a context");
7372
}
7473

7574
addPackage(options.getSdkVersion());

‎sentry-android-ndk-outproc/src/test/java/io/sentry/android/ndk/outproc/NativeBackendTest.kt‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ class NativeBackendTest {
5252
assertFailsWith<IllegalStateException> { SentryNdkOutproc.init(options()) }
5353
}
5454

55-
@Test
56-
@Config(sdk = [29])
57-
fun `fails below API 30`() {
58-
assertFailsWith<IllegalStateException> { SentryNdkOutproc.init(mock<Context>(), options()) }
59-
}
60-
6155
private fun options() =
6256
SentryAndroidOptions().apply {
6357
dsn = "https://key@sentry.io/proj"

‎sentry-android-ndk-outproc/src/test/java/io/sentry/android/ndk/outproc/SentryNdkCrashServiceTest.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ class SentryNdkCrashServiceTest {
223223
fun `init fails without shared memory`() {
224224
configure(true)
225225
Mockito.mockStatic(io.sentry.ndk.SentryNdk::class.java).use { native ->
226-
native.`when`<Int> { io.sentry.ndk.SentryNdk.createCrashShm() }.thenReturn(-1)
226+
native.`when`<Int> { io.sentry.ndk.SentryNdk.createCrashShm(any<String>()) }.thenReturn(-1)
227227

228228
assertFailsWith<IllegalStateException> { SentryNdkOutproc.init(app, options) }
229229

0 commit comments

Comments
 (0)