Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.sentry.SentryLevel;
import io.sentry.time.Deadline;
import io.sentry.time.MonotonicTicker;
import io.sentry.util.ExceptionUtils;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -174,9 +175,16 @@ private boolean isProcessNotResponding() {
if (am != null) {
List<ActivityManager.ProcessErrorStateInfo> processesInErrorState = null;
try {
// It can throw RuntimeException or OutOfMemoryError
processesInErrorState = am.getProcessesInErrorState();
} catch (OutOfMemoryError e) {
// Unparceling the reply, which lists every process in an error state, can fail to
// allocate even when the app itself has memory left, so it's not treated as fatal here
// (#2114). The process is treated as responding normally, so no ANR is reported.
logger.log(SentryLevel.ERROR, "Error getting ActivityManager#getProcessesInErrorState.", e);
} catch (Throwable e) {
// A Binder call into the activity manager, which can rethrow any runtime exception from
// the other side. The process is treated as responding normally, so no ANR is reported.
ExceptionUtils.rethrowIfFatal(e);
logger.log(SentryLevel.ERROR, "Error getting ActivityManager#getProcessesInErrorState.", e);
}
// if list is null, there's no process in ANR state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.sentry.protocol.SentryThread;
import io.sentry.transport.CurrentDateProvider;
import io.sentry.transport.ICurrentDateProvider;
import io.sentry.util.ExceptionUtils;
import io.sentry.util.HintUtils;
import io.sentry.util.Objects;
import java.io.BufferedReader;
Expand Down Expand Up @@ -229,6 +230,10 @@ public void markReported(final long timestamp) {
}
return new ParseResult(ParseResult.Type.DUMP, dump, threads, debugImages, artContext);
} catch (Throwable e) {
// The dump is written by ART in a format that changes between Android versions and
// vendors, so we can't know how the parser fails on unexpected input. The ANR is still
// reported, with a message asking to attach the raw dump.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.WARNING, "Failed to parse ANR thread dump", e);
return new ParseResult(ParseResult.Type.ERROR, dump);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.sentry.android.core.internal.util.Debouncer;
import io.sentry.android.core.internal.util.DeviceOrientations;
import io.sentry.protocol.Device;
import io.sentry.util.ExceptionUtils;
import io.sentry.util.Objects;
import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -66,6 +67,10 @@ public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions
.log(SentryLevel.DEBUG, "AppComponentsBreadcrumbsIntegration installed.");
addIntegrationToSdkVersion("AppComponentsBreadcrumbs");
} catch (Throwable e) {
// The context may be the app's own subclass, or a ContextImpl that doesn't support
// component callbacks on older versions, so we can't know what this throws. App
// component breadcrumbs are disabled.
ExceptionUtils.rethrowIfFatal(e);
this.options.setEnableAppComponentBreadcrumbs(false);
options.getLogger().log(SentryLevel.INFO, e, "ComponentCallbacks2 is not available.");
}
Expand All @@ -77,12 +82,15 @@ public void close() throws IOException {
try {
// if its a ContextImpl, unregisterComponentCallbacks can't be used
context.unregisterComponentCallbacks(this);
} catch (Throwable ignored) {
// fine, might throw on older versions
} catch (Throwable e) {
// The context may be the app's own subclass, or a ContextImpl that doesn't support
// component callbacks on older versions, so we can't know what this throws. The callbacks
// may stay registered until the process ends.
ExceptionUtils.rethrowIfFatal(e);
if (options != null) {
options
.getLogger()
.log(SentryLevel.DEBUG, ignored, "It was not possible to unregisterComponentCallbacks");
.log(SentryLevel.DEBUG, e, "It was not possible to unregisterComponentCallbacks");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import io.sentry.protocol.SentryTransaction;
import io.sentry.protocol.User;
import io.sentry.protocol.profiling.SentryProfile;
import io.sentry.util.ExceptionUtils;
import io.sentry.util.HintUtils;
import io.sentry.util.SentryRandom;
import java.io.File;
Expand Down Expand Up @@ -447,6 +448,9 @@ private void setApp(final @NotNull SentryBaseEvent event) {
}
}
} catch (Throwable e) {
// The first DeviceInfoUtil.getInstance call queries the package manager over Binder, which
// can rethrow any runtime exception from the other side. The split APK info is left out.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.ERROR, "Error getting split apks info.", e);
}

Expand Down Expand Up @@ -716,6 +720,9 @@ private void setSideLoadedInfo(final @NotNull SentryBaseEvent event) {
}
}
} catch (Throwable e) {
// The first DeviceInfoUtil.getInstance call queries the package manager over Binder, which
// can rethrow any runtime exception from the other side. The side-loaded tags are left out.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.ERROR, "Error getting side loaded info.", e);
}
}
Expand Down Expand Up @@ -975,6 +982,9 @@ private void applyAnrProfile(
options.getLogger().log(SentryLevel.DEBUG, "No ANR profile file found");
}
} catch (Throwable t) {
// The profile file was written by the previous process, possibly while it was being
// killed, so we can't know how reading it fails. The ANR is sent without a profile.
ExceptionUtils.rethrowIfFatal(t);
options.getLogger().log(SentryLevel.INFO, "Could not retrieve ANR profile", t);
} finally {
if (!AnrProfileRotationHelper.deleteLastFile(cacheDir)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public int getSdkInfoVersion() {
|| Build.PRODUCT.contains("vbox86p")
|| Build.PRODUCT.contains("emulator")
|| Build.PRODUCT.contains("simulator");
} catch (Throwable e) {
} catch (NullPointerException e) {
// Build fields are only null off-device, e.g. in unit tests against the android.jar stubs
logger.log(
SentryLevel.ERROR, "Error checking whether application is running in an emulator.", e);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.sentry.SentryOptions;
import io.sentry.android.core.util.AndroidLazyEvaluator;
import io.sentry.protocol.App;
import io.sentry.util.ExceptionUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
Expand Down Expand Up @@ -104,6 +105,9 @@ private ContextUtils() {}
.getPackageManager()
.getPackageInfo(context.getPackageName(), PackageManager.PackageInfoFlags.of(0));
} catch (Throwable e) {
// A Binder call into the package manager, which can rethrow any runtime exception from
// the other side. The package info is left out.
ExceptionUtils.rethrowIfFatal(e);
return null;
}
});
Expand All @@ -113,6 +117,9 @@ private ContextUtils() {}
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
} catch (Throwable e) {
// A Binder call into the package manager, which can rethrow any runtime exception from
// the other side. The package info is left out.
ExceptionUtils.rethrowIfFatal(e);
return null;
}
});
Expand All @@ -131,6 +138,9 @@ private ContextUtils() {}
return context.getString(stringId);
}
} catch (Throwable e) {
// getApplicationLabel is a Binder call into the package manager, and the context may be
// the app's own subclass, so we can't know what this throws. The app name is left out.
ExceptionUtils.rethrowIfFatal(e);
return null;
}
});
Expand All @@ -150,6 +160,9 @@ private ContextUtils() {}
context.getPackageName(),
PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA));
} catch (Throwable e) {
// A Binder call into the package manager, which can rethrow any runtime exception from
// the other side. The application info is left out.
ExceptionUtils.rethrowIfFatal(e);
return null;
}
});
Expand All @@ -161,6 +174,9 @@ private ContextUtils() {}
.getPackageManager()
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
} catch (Throwable e) {
// A Binder call into the package manager, which can rethrow any runtime exception from
// the other side. The application info is left out.
ExceptionUtils.rethrowIfFatal(e);
return null;
}
});
Expand Down Expand Up @@ -202,6 +218,9 @@ static PackageInfo getPackageInfo(
return context.getPackageManager().getPackageInfo(context.getPackageName(), flags);
}
} catch (Throwable e) {
// A Binder call into the package manager, which can rethrow any runtime exception from the
// other side. The package info is left out.
ExceptionUtils.rethrowIfFatal(e);
logger.log(SentryLevel.ERROR, "Error getting package info.", e);
return null;
}
Expand Down Expand Up @@ -269,8 +288,10 @@ public static boolean isForegroundImportance() {
new ActivityManager.RunningAppProcessInfo();
ActivityManager.getMyMemoryState(appProcessInfo);
return appProcessInfo.importance == IMPORTANCE_FOREGROUND;
} catch (Throwable ignored) {
// should never happen
} catch (Throwable e) {
// A Binder call into the activity manager, which can rethrow any runtime exception from the
// other side. The process is treated as not in the foreground.
ExceptionUtils.rethrowIfFatal(e);
}
return false;
}
Expand Down Expand Up @@ -299,7 +320,9 @@ public static boolean appIsLibraryForComposePreview(final @NotNull Context conte
}
}
} catch (Throwable t) {
// ignored
// getAppTasks is a Binder call into the activity manager, which can rethrow any runtime
// exception from the other side. The app is treated as not running a Compose Preview.
ExceptionUtils.rethrowIfFatal(t);
}
}
return false;
Expand Down Expand Up @@ -398,6 +421,9 @@ public static boolean appIsLibraryForComposePreview(final @NotNull Context conte
try {
return context.getResources().getDisplayMetrics();
} catch (Throwable e) {
// The context may be the app's own subclass that overrides getResources(), so we can't know
// what it throws. The display metrics are left out.
ExceptionUtils.rethrowIfFatal(e);
logger.log(SentryLevel.ERROR, "Error getting DisplayMetrics.", e);
return null;
}
Expand All @@ -412,7 +438,8 @@ public static boolean appIsLibraryForComposePreview(final @NotNull Context conte
static @Nullable String getFamily(final @NotNull ILogger logger) {
try {
return Build.MODEL.split(" ", -1)[0];
} catch (Throwable e) {
} catch (NullPointerException e) {
// Build fields are only null off-device, e.g. in unit tests against the android.jar stubs
logger.log(SentryLevel.ERROR, "Error getting device family.", e);
return null;
}
Expand Down Expand Up @@ -440,6 +467,9 @@ public static boolean appIsLibraryForComposePreview(final @NotNull Context conte
logger.log(SentryLevel.INFO, "Error getting MemoryInfo.");
return null;
} catch (Throwable e) {
// A Binder call into the activity manager, which can rethrow any runtime exception from the
// other side. The memory info is left out.
ExceptionUtils.rethrowIfFatal(e);
logger.log(SentryLevel.ERROR, "Error getting MemoryInfo.", e);
return null;
}
Expand Down Expand Up @@ -506,16 +536,12 @@ static void setAppPackageInfo(
app.setPermissions(permissions);

if (deviceInfoUtil != null) {
try {
final ContextUtils.SplitApksInfo splitApksInfo = deviceInfoUtil.getSplitApksInfo();
if (splitApksInfo != null) {
app.setSplitApks(splitApksInfo.isSplitApks());
if (splitApksInfo.getSplitNames() != null) {
app.setSplitNames(Arrays.asList(splitApksInfo.getSplitNames()));
}
final ContextUtils.SplitApksInfo splitApksInfo = deviceInfoUtil.getSplitApksInfo();
if (splitApksInfo != null) {
app.setSplitApks(splitApksInfo.isSplitApks());
if (splitApksInfo.getSplitNames() != null) {
app.setSplitNames(Arrays.asList(splitApksInfo.getSplitNames()));
}
} catch (Throwable e) {
// Split APK info is optional, the app context is still complete without it.
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
import io.sentry.protocol.SentryThread;
import io.sentry.protocol.SentryTransaction;
import io.sentry.protocol.User;
import io.sentry.util.ExceptionUtils;
import io.sentry.util.HintUtils;
import io.sentry.util.Objects;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -193,6 +195,9 @@ private void setDevice(
.getContexts()
.setDevice(deviceInfoUtil.get().collectDeviceInformation(errorEvent, applyScopeData));
} catch (Throwable e) {
// Collecting the device info makes Binder calls into several system services, which can
// rethrow any runtime exception from the other side. The device context is left out.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve device info", e);
}
} else {
Expand All @@ -210,7 +215,10 @@ private void mergeOS(final @NotNull SentryBaseEvent event) {
final OperatingSystem androidOS = deviceInfoUtil.get().getOperatingSystem();
// make Android OS the main OS using the 'os' key
event.getContexts().setOperatingSystem(androidOS);
} catch (Throwable e) {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve os system", e);
} catch (ExecutionException e) {
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve os system", e);
}
} else {
Expand Down Expand Up @@ -310,7 +318,10 @@ private void setPackageInfo(final @NotNull SentryBaseEvent event, final @NotNull
if (this.deviceInfoUtil != null) {
try {
deviceInfoUtil = this.deviceInfoUtil.get();
} catch (Throwable e) {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve device info", e);
} catch (ExecutionException e) {
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve device info", e);
}
} else {
Expand Down Expand Up @@ -367,7 +378,10 @@ private void setSideLoadedInfo(final @NotNull SentryBaseEvent event) {
event.setTag(entry.getKey(), entry.getValue());
}
}
} catch (Throwable e) {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
options.getLogger().log(SentryLevel.ERROR, "Error getting side loaded info.", e);
} catch (ExecutionException e) {
options.getLogger().log(SentryLevel.ERROR, "Error getting side loaded info.", e);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.sentry.android.core.internal.util.RootChecker;
import io.sentry.protocol.Device;
import io.sentry.protocol.OperatingSystem;
import io.sentry.util.ExceptionUtils;
import java.io.File;
import java.util.Calendar;
import java.util.Collections;
Expand Down Expand Up @@ -303,6 +304,9 @@ public static Float getBatteryLevel(

return ((float) level / (float) scale) * percentMultiplier;
} catch (Throwable e) {
// The extras were parceled by system_server and are unparceled on first access, which can
// throw any runtime exception on malformed data. The battery level is left out.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.ERROR, "Error getting device battery level.", e);
return null;
}
Expand All @@ -321,6 +325,9 @@ public static Boolean isCharging(
return plugged == BatteryManager.BATTERY_PLUGGED_AC
|| plugged == BatteryManager.BATTERY_PLUGGED_USB;
} catch (Throwable e) {
// The extras were parceled by system_server and are unparceled on first access, which can
// throw any runtime exception on malformed data. The charging state is left out.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.ERROR, "Error getting device charging state.", e);
return null;
}
Expand All @@ -334,6 +341,9 @@ private Float getBatteryTemperature(final @NotNull Intent batteryIntent) {
return ((float) temperature) / 10; // celsius
}
} catch (Throwable e) {
// The extras were parceled by system_server and are unparceled on first access, which can
// throw any runtime exception on malformed data. The temperature is left out.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.ERROR, "Error getting battery temperature.", e);
}
return null;
Expand All @@ -359,6 +369,9 @@ private Device.DeviceOrientation getOrientation() {
return null;
}
} catch (Throwable e) {
// The context may be the app's own subclass that overrides getResources(), so we can't know
// what it throws. The orientation is left out.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.ERROR, "Error getting device orientation.", e);
}
return deviceOrientation;
Expand Down Expand Up @@ -394,6 +407,9 @@ private StatFs getExternalStorageStat(final @Nullable File internalStorage) {
return new StatFs(path.getPath());
}
} catch (Throwable e) {
// getExternalFilesDirs asks the storage service over Binder to create the directories, so
// we can't know what it throws. The external storage sizes are left out.
ExceptionUtils.rethrowIfFatal(e);
options.getLogger().log(SentryLevel.INFO, "Not possible to read external files directory");
}
return null;
Expand Down
Loading
Loading