From 5075fb617a384392bc0113b55a2b5ce1c0923df3 Mon Sep 17 00:00:00 2001 From: Sylvain Defresne Date: Mon, 12 Feb 2024 16:23:37 +0100 Subject: [PATCH] Honor ios_is_app_extension chromium build flag If building for chromium, honor the ios_is_app_extension gn variable that is set per toolchain. When it is defined, the code is built for an application extension (i.e. -fapplication-extension is passed to the compiler). Use CRASHPAD_IS_IOS_APP_EXTENSION build guard to not compile code that use unavailable extension when ios_is_app_extension is set. If the variable is not set, then check at runtime whether the API can be used or not (if the crashpad client uses the same toolchain for the main application and its application extensions). This is required to pass -fapplication-extension to the compiler when building application extensions (which allow catching API that is not available to application extensions). Bug: 40120082 Change-Id: I28d545fcfd0f8662430c40ff202b79b0c2b2ff8b Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5286216 Reviewed-by: Justin Cohen Commit-Queue: Sylvain Defresne --- build/BUILD.gn | 13 +++++++++++++ util/BUILD.gn | 4 ++++ util/ios/ios_system_data_collector.mm | 14 ++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/build/BUILD.gn b/build/BUILD.gn index 7d7e08bb..664e4982 100644 --- a/build/BUILD.gn +++ b/build/BUILD.gn @@ -72,4 +72,17 @@ if (crashpad_is_ios) { ] } } + + if (crashpad_is_in_chromium) { + import("//build/config/ios/ios_sdk.gni") + crashpad_is_ios_app_extension = ios_is_app_extension + } else { + crashpad_is_ios_app_extension = false + } + + config("crashpad_is_ios_app_extension") { + if (crashpad_is_ios_app_extension) { + defines = [ "CRASHPAD_IS_IOS_APP_EXTENSION" ] + } + } } diff --git a/util/BUILD.gn b/util/BUILD.gn index 93b9d544..e7ff4a8a 100644 --- a/util/BUILD.gn +++ b/util/BUILD.gn @@ -597,6 +597,10 @@ crashpad_static_library("util") { ] } + if (crashpad_is_ios) { + configs += [ "../build:crashpad_is_ios_app_extension" ] + } + if (crashpad_is_win) { libs = [ "user32.lib", diff --git a/util/ios/ios_system_data_collector.mm b/util/ios/ios_system_data_collector.mm index 2ec6de59..bcadba6e 100644 --- a/util/ios/ios_system_data_collector.mm +++ b/util/ios/ios_system_data_collector.mm @@ -22,6 +22,7 @@ #import #include "base/apple/mach_logging.h" +#include "base/notreached.h" #include "base/numerics/safe_conversions.h" #include "base/strings/stringprintf.h" #include "base/strings/sys_string_conversions.h" @@ -99,7 +100,14 @@ IOSSystemDataCollector::IOSSystemDataCollector() build_ = ReadStringSysctlByName("kern.osversion"); bundle_identifier_ = base::SysNSStringToUTF8([[NSBundle mainBundle] bundleIdentifier]); +// If CRASHPAD_IS_IOS_APP_EXTENSION is defined, then the code is compiled with +// -fapplication-extension and can only be used in an app extension. Otherwise +// check at runtime whether the code is executing in an app extension or not. +#if defined(CRASHPAD_IS_IOS_APP_EXTENSION) + is_extension_ = true; +#else is_extension_ = [[NSBundle mainBundle].bundlePath hasSuffix:@"appex"]; +#endif #if defined(ARCH_CPU_X86_64) cpu_vendor_ = ReadStringSysctlByName("machdep.cpu.vendor"); @@ -172,6 +180,7 @@ void IOSSystemDataCollector::InstallHandlers() { (__bridge CFStringRef)UIDeviceOrientationDidChangeNotification, this); OrientationDidChangeNotification(); +#if !defined(CRASHPAD_IS_IOS_APP_EXTENSION) // Foreground/Background. Extensions shouldn't use UIApplication*. if (!is_extension_) { AddObserver< @@ -185,6 +194,7 @@ void IOSSystemDataCollector::InstallHandlers() { this); ApplicationDidChangeActiveNotification(); } +#endif } void IOSSystemDataCollector::SystemTimeZoneDidChangeNotification() { @@ -228,6 +238,9 @@ void IOSSystemDataCollector::OrientationDidChangeNotification() { } void IOSSystemDataCollector::ApplicationDidChangeActiveNotification() { +#if defined(CRASHPAD_IS_IOS_APP_EXTENSION) + NOTREACHED_NORETURN(); +#else dispatch_assert_queue_debug(dispatch_get_main_queue()); bool old_active = active_; active_ = [UIApplication sharedApplication].applicationState == @@ -235,6 +248,7 @@ void IOSSystemDataCollector::ApplicationDidChangeActiveNotification() { if (active_ != old_active && active_application_callback_) { active_application_callback_(active_); } +#endif } } // namespace internal