From 51b5c3d87d730726ce117f908fe202ece7d156f8 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Thu, 13 Aug 2020 17:28:29 -0700 Subject: [PATCH] ams: avoid UB infinite loops --- exosphere/program/sc7fw/source/sc7fw_main.cpp | 6 ++++-- exosphere/warmboot/source/warmboot_main.cpp | 4 +++- libraries/libexosphere/source/wdt/wdt_api.cpp | 3 ++- libraries/libmesosphere/include/mesosphere/kern_panic.hpp | 2 +- .../source/board/nintendo/nx/kern_k_system_control.cpp | 2 +- .../source/board/nintendo/nx/kern_secure_monitor.cpp | 2 +- libraries/libmesosphere/source/kern_main.cpp | 2 +- .../libstratosphere/source/result/result_on_assertion.cpp | 4 ++-- libraries/libvapours/include/vapours/defines.hpp | 2 ++ 9 files changed, 17 insertions(+), 10 deletions(-) diff --git a/exosphere/program/sc7fw/source/sc7fw_main.cpp b/exosphere/program/sc7fw/source/sc7fw_main.cpp index 0a082586a..24e7553eb 100644 --- a/exosphere/program/sc7fw/source/sc7fw_main.cpp +++ b/exosphere/program/sc7fw/source/sc7fw_main.cpp @@ -90,7 +90,7 @@ namespace ams::sc7fw { reg::ReadWrite(PMC + APBDEV_PMC_DPD_ENABLE, PMC_REG_BITS_ENUM(DPD_ENABLE_ON, ENABLE)); /* Wait forever until we're asleep. */ - while (true) { /* ... */ } + AMS_INFINITE_LOOP(); } } @@ -102,7 +102,9 @@ namespace ams::sc7fw { NORETURN void ExceptionHandler() { /* Write enable to MAIN_RESET. */ reg::Write(PMC + APBDEV_PMC_CNTRL, PMC_REG_BITS_ENUM(CNTRL_MAIN_RESET, ENABLE)); - while (true) { /* ... */ } + + /* Wait forever until we're reset. */ + AMS_INFINITE_LOOP(); } } diff --git a/exosphere/warmboot/source/warmboot_main.cpp b/exosphere/warmboot/source/warmboot_main.cpp index 5042fa8ad..8da3af85f 100644 --- a/exosphere/warmboot/source/warmboot_main.cpp +++ b/exosphere/warmboot/source/warmboot_main.cpp @@ -90,7 +90,9 @@ namespace ams::warmboot { NORETURN void ExceptionHandler() { /* Write enable to MAIN_RESET. */ reg::Write(PMC + APBDEV_PMC_CNTRL, PMC_REG_BITS_ENUM(CNTRL_MAIN_RESET, ENABLE)); - while (true) { /* ... */ } + + /* Wait forever until we're reset. */ + AMS_INFINITE_LOOP(); } } diff --git a/libraries/libexosphere/source/wdt/wdt_api.cpp b/libraries/libexosphere/source/wdt/wdt_api.cpp index cb26f5e05..ef9ad6139 100644 --- a/libraries/libexosphere/source/wdt/wdt_api.cpp +++ b/libraries/libexosphere/source/wdt/wdt_api.cpp @@ -81,7 +81,8 @@ namespace ams::wdt { /* Enable the counters. */ reg::Write(registers + 0x188, 0x1); - while (true) { /* ... */ } + /* Wait forever until the reboot takes. */ + AMS_INFINITE_LOOP(); } #endif diff --git a/libraries/libmesosphere/include/mesosphere/kern_panic.hpp b/libraries/libmesosphere/include/mesosphere/kern_panic.hpp index 3e7eaf72b..4f3b2384b 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_panic.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_panic.hpp @@ -69,7 +69,7 @@ namespace ams::kern { #define MESOSPHERE_UNIMPLEMENTED() MESOSPHERE_PANIC("%s: Unimplemented\n", __PRETTY_FUNCTION__) #define MESOSPHERE_ABORT() MESOSPHERE_PANIC("Abort()\n"); -#define MESOSPHERE_INIT_ABORT() do { /* ... */ } while (true) +#define MESOSPHERE_INIT_ABORT() AMS_INFINITE_LOOP() #define MESOSPHERE_ABORT_UNLESS(expr) \ ({ \ diff --git a/libraries/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp b/libraries/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp index ca6e0f58c..3c73bc298 100644 --- a/libraries/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp +++ b/libraries/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp @@ -517,7 +517,7 @@ namespace ams::kern::board::nintendo::nx { } u32 dummy; smc::init::ReadWriteRegister(std::addressof(dummy), 0x7000E400, 0x10, 0x10); - while (true) { /* ... */ } + AMS_INFINITE_LOOP(); } /* User access. */ diff --git a/libraries/libmesosphere/source/board/nintendo/nx/kern_secure_monitor.cpp b/libraries/libmesosphere/source/board/nintendo/nx/kern_secure_monitor.cpp index 9405d8a67..8c7414c0a 100644 --- a/libraries/libmesosphere/source/board/nintendo/nx/kern_secure_monitor.cpp +++ b/libraries/libmesosphere/source/board/nintendo/nx/kern_secure_monitor.cpp @@ -227,7 +227,7 @@ namespace ams::kern::board::nintendo::nx::smc { void NORETURN Panic(u32 color) { SecureMonitorArguments args = { FunctionId_Panic, color }; CallPrivilegedSecureMonitorFunction(args); - while (true) { /* ... */ } + AMS_INFINITE_LOOP(); } void CallSecureMonitorFromUser(ams::svc::lp64::SecureMonitorArguments *args) { diff --git a/libraries/libmesosphere/source/kern_main.cpp b/libraries/libmesosphere/source/kern_main.cpp index 063e6b301..ecf96e37e 100644 --- a/libraries/libmesosphere/source/kern_main.cpp +++ b/libraries/libmesosphere/source/kern_main.cpp @@ -139,7 +139,7 @@ namespace ams::kern { /* Main() is done, and we should never get to this point. */ MESOSPHERE_PANIC("Main Thread continued after exit."); - while (true) { /* ... */ } + AMS_INFINITE_LOOP(); } } diff --git a/libraries/libstratosphere/source/result/result_on_assertion.cpp b/libraries/libstratosphere/source/result/result_on_assertion.cpp index 7d7d13724..7705c561f 100644 --- a/libraries/libstratosphere/source/result/result_on_assertion.cpp +++ b/libraries/libstratosphere/source/result/result_on_assertion.cpp @@ -33,7 +33,7 @@ namespace ams::result::impl { /* TODO: ams::fatal:: */ fatalThrow(result.GetValue()); - while (true) { /* ... */ } + AMS_INFINITE_LOOP(); } NORETURN WEAK_SYMBOL void OnResultAbort(Result result) { @@ -50,7 +50,7 @@ namespace ams::result::impl { /* TODO: ams::fatal:: */ fatalThrow(result.GetValue()); - while (true) { /* ... */ } + AMS_INFINITE_LOOP(); } NORETURN WEAK_SYMBOL void OnResultAssertion(Result result) { diff --git a/libraries/libvapours/include/vapours/defines.hpp b/libraries/libvapours/include/vapours/defines.hpp index a4be456b7..124508e3c 100644 --- a/libraries/libvapours/include/vapours/defines.hpp +++ b/libraries/libvapours/include/vapours/defines.hpp @@ -65,3 +65,5 @@ #define AMS_ASSUME(expr) do { if (!static_cast((expr))) { __builtin_unreachable(); } } while (0) #define AMS_CURRENT_FUNCTION_NAME __FUNCTION__ + +#define AMS_INFINITE_LOOP() do { __asm__ __volatile__("" ::: "memory"); } while (1)