From 7eca4d74c61f6f2b32d12a37e3cfc689682b7c6a Mon Sep 17 00:00:00 2001 From: Aaron Yu Date: Tue, 20 Jun 2023 16:42:04 -0700 Subject: [PATCH] Fix __FrameHandler3::CxxCallCatchBlock crashes at Nearby Connections layer PiperOrigin-RevId: 542085514 --- .../implementation/windows/ble_gatt_server.cc | 14 ++++++++++ .../implementation/windows/ble_medium.cc | 12 +++++++++ .../platform/implementation/windows/ble_v2.cc | 27 +++++++++++++++++++ .../windows/bluetooth_classic_medium.cc | 25 +++++++++++++++++ .../windows/bluetooth_pairing.cc | 14 ++++++++++ .../windows/preferences_repository.cc | 6 +++++ 6 files changed, 98 insertions(+) diff --git a/internal/platform/implementation/windows/ble_gatt_server.cc b/internal/platform/implementation/windows/ble_gatt_server.cc index 99061dfa..ea4ef93d 100644 --- a/internal/platform/implementation/windows/ble_gatt_server.cc +++ b/internal/platform/implementation/windows/ble_gatt_server.cc @@ -210,6 +210,8 @@ void BleGattServer::Stop() { } catch (const winrt::hresult_error& error) { NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } } @@ -344,6 +346,8 @@ bool BleGattServer::InitializeGattServer() { } catch (const winrt::hresult_error& error) { NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } // Clean up. @@ -396,6 +400,8 @@ bool BleGattServer::StartAdvertisement(const ByteArray& service_data, } catch (const winrt::hresult_error& error) { NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } is_advertising_ = false; @@ -434,6 +440,8 @@ bool BleGattServer::StopAdvertisement() { } catch (const winrt::hresult_error& error) { NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } return false; @@ -484,6 +492,8 @@ bool BleGattServer::StopAdvertisement() { } catch (const winrt::hresult_error& error) { NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } deferral.Complete(); @@ -582,6 +592,8 @@ void BleGattServer::Characteristic_SubscribedClientsChanged( } catch (const winrt::hresult_error& error) { NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } } @@ -633,6 +645,8 @@ void BleGattServer::NotifyValueChanged( } catch (const winrt::hresult_error& error) { NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } } diff --git a/internal/platform/implementation/windows/ble_medium.cc b/internal/platform/implementation/windows/ble_medium.cc index 96eb574e..c3fa3eec 100644 --- a/internal/platform/implementation/windows/ble_medium.cc +++ b/internal/platform/implementation/windows/ble_medium.cc @@ -223,6 +223,9 @@ bool BleMedium::StartAdvertising( << ": " << winrt::to_string(ex.message()); return false; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return false; } } @@ -261,6 +264,9 @@ bool BleMedium::StopAdvertising(const std::string& service_id) { << ": " << winrt::to_string(ex.message()); return false; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return false; } } @@ -322,6 +328,9 @@ bool BleMedium::StartScanning( << ": " << winrt::to_string(ex.message()); return false; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return false; } } @@ -362,6 +371,9 @@ bool BleMedium::StopScanning(const std::string& service_id) { << ": " << winrt::to_string(ex.message()); return false; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return false; } } diff --git a/internal/platform/implementation/windows/ble_v2.cc b/internal/platform/implementation/windows/ble_v2.cc index ce1a08aa..64b6fe55 100644 --- a/internal/platform/implementation/windows/ble_v2.cc +++ b/internal/platform/implementation/windows/ble_v2.cc @@ -250,6 +250,9 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid, << ": " << winrt::to_string(ex.message()); return false; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return false; } } @@ -298,6 +301,9 @@ std::unique_ptr BleV2Medium::StartScanning( << ": Exception to start BLE scanning: " << ex.code() << ": " << winrt::to_string(ex.message()); return nullptr; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return nullptr; } } else { NEARBY_LOGS(WARNING) << __func__ << ": BLE Scanning already started."; @@ -359,6 +365,10 @@ std::unique_ptr BleV2Medium::StartScanning( << ": " << winrt::to_string(ex.message()); return absl::InternalError( "Bad status stopping Ble scanning"); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return absl::InternalError( + "Bad status stopping Ble scanning"); } } } @@ -429,6 +439,8 @@ std::unique_ptr BleV2Medium::ConnectToGattServer( } catch (const winrt::hresult_error& error) { NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } return nullptr; @@ -471,6 +483,9 @@ bool BleV2Medium::StopScanning() { << ": " << winrt::to_string(ex.message()); return false; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return false; } } @@ -626,6 +641,9 @@ bool BleV2Medium::StartBleAdvertising( << ": " << winrt::to_string(ex.message()); return false; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return false; } } @@ -662,6 +680,9 @@ bool BleV2Medium::StopBleAdvertising() { << ": " << winrt::to_string(ex.message()); return false; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return false; } } @@ -723,6 +744,9 @@ bool BleV2Medium::StartGattAdvertising( << ": " << winrt::to_string(ex.message()); return false; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return false; } } @@ -761,6 +785,9 @@ bool BleV2Medium::StopGattAdvertising() { << ": " << winrt::to_string(ex.message()); return false; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return false; } } diff --git a/internal/platform/implementation/windows/bluetooth_classic_medium.cc b/internal/platform/implementation/windows/bluetooth_classic_medium.cc index be7d8f18..431400ba 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_medium.cc +++ b/internal/platform/implementation/windows/bluetooth_classic_medium.cc @@ -149,6 +149,9 @@ void BluetoothClassicMedium::OnScanModeChanged( << ": OnScanModeChanged exception: " << ex.code() << ": " << winrt::to_string(ex.message()); return; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return; } } @@ -220,6 +223,8 @@ void BluetoothClassicMedium::InitializeDeviceWatcher() { NEARBY_LOGS(ERROR) << __func__ << ": InitializeDeviceWatcher exception: " << ex.code() << ": " << winrt::to_string(ex.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } } @@ -343,6 +348,9 @@ std::unique_ptr BluetoothClassicMedium::ConnectToService( << ex.code() << ", error message: " << winrt::to_string(ex.message()); return nullptr; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return nullptr; } } @@ -373,6 +381,8 @@ std::unique_ptr BluetoothClassicMedium::CreatePairing( << ": Failed to create pairing. WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } return nullptr; } @@ -806,6 +816,18 @@ bool BluetoothClassicMedium::StartAdvertising(bool radio_discoverable) { rfcomm_provider_ = nullptr; } + return false; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + if (server_socket_ != nullptr) { + server_socket_->Close(); + server_socket_ = nullptr; + } + + if (rfcomm_provider_ != nullptr) { + rfcomm_provider_ = nullptr; + } + return false; } } @@ -836,6 +858,9 @@ bool BluetoothClassicMedium::StopAdvertising() { << ": StopAdvertising exception: " << ex.code() << ": " << winrt::to_string(ex.message()); return false; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return false; } } diff --git a/internal/platform/implementation/windows/bluetooth_pairing.cc b/internal/platform/implementation/windows/bluetooth_pairing.cc index 0dc75c0b..c511439f 100644 --- a/internal/platform/implementation/windows/bluetooth_pairing.cc +++ b/internal/platform/implementation/windows/bluetooth_pairing.cc @@ -99,6 +99,8 @@ bool BluetoothPairing::InitiatePairing( << ": Failed to initiate pairing. WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } return false; } @@ -137,6 +139,8 @@ bool BluetoothPairing::FinishPairing( << ": Failed to finish pairing. WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } return false; } @@ -167,6 +171,8 @@ bool BluetoothPairing::CancelPairing() { << ": Failed to cancel ongoing pairing process. " << "WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } return false; } @@ -195,6 +201,8 @@ bool BluetoothPairing::Unpair() { << ": Failed to unpaired with device. WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } return false; } @@ -212,6 +220,8 @@ bool BluetoothPairing::IsPaired() { << ": Failed to get IsPaired. WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } return false; } @@ -259,6 +269,8 @@ void BluetoothPairing::OnPairingRequested( << __func__ << ": Failed to request to pair with device. WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } pairing_callback_.on_pairing_error_cb(PairingError::kFailed); } @@ -322,6 +334,8 @@ void BluetoothPairing::OnPair(DevicePairingResult& pairing_result) { NEARBY_LOGS(ERROR) << __func__ << ": Failed to get Pairing Result Status." << " WinRT exception: " << error.code() << ": " << winrt::to_string(error.message()); + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; } pairing_callback_.on_pairing_error_cb(PairingError::kFailed); } diff --git a/internal/platform/implementation/windows/preferences_repository.cc b/internal/platform/implementation/windows/preferences_repository.cc index 9f7e1fd2..3a5220e2 100644 --- a/internal/platform/implementation/windows/preferences_repository.cc +++ b/internal/platform/implementation/windows/preferences_repository.cc @@ -100,6 +100,9 @@ bool PreferencesRepository::SavePreferences(json preferences) { } catch (const std::exception& e) { NEARBY_LOGS(ERROR) << "Failed to save preferences file: " << e.what(); return false; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return false; } return true; @@ -130,6 +133,9 @@ std::optional PreferencesRepository::AttemptLoad() { } catch (const std::exception& e) { NEARBY_LOGS(ERROR) << "Exception while loading preferences: " << e.what(); return std::nullopt; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception."; + return std::nullopt; } }