Repository navigation
core: libxr_fatal_error no longer prints; REQUIRE carries no location - #364
Conversation
All three assertion macros called libxr_fatal_error(__FILE__, __LINE__, in_isr), and that function printed `Fatal error at file:line` before every call of the fatal callback. REQUIRE is always on, so release firmware carried a file name and a line number at each of its REQUIRE sites and the code that formats and prints them. There are two functions now, with the same parameters: - libxr_fatal_error(file, line, in_isr) does not print. It calls the fatal callback in a loop as before. - libxr_fatal_error_and_print(file, line, in_isr) is the same and prints before every call of the callback, as libxr_fatal_error did. ASSERT and DEV_ASSERT call libxr_fatal_error_and_print with __FILE__ and __LINE__. Neither is active in release firmware, so nothing there refers to the printing function and its source file (libxr_fatal_print.cpp) stays out of the link. REQUIRE calls libxr_fatal_error with a null file name and line 0: where a REQUIRE failed is found with a debugger in the call stack. Unchanged: the type of the fatal callback, the loop (callback, 500 ms sleep, never returns) and the interrupt-context path. C code that calls libxr_fatal_error(file, line, in_isr) compiles as before and no longer prints; the callback still gets its file name and line number. For users: - A fatal callback can get a null file name (a failed REQUIRE). - A project that defines its own libxr_fatal_error and uses ASSERT or DEV_ASSERT has to define libxr_fatal_error_and_print as well. Tests: the four assertion-mode programs check which function each macro calls and what location it passes; the case `assert` runs a failing REQUIRE and both functions of the library in child processes and checks the location the callback gets. The topic test confirmed by file name that the failed check is in topic.cpp; those checks are REQUIREs, so it now confirms only that a fatal error happened.
[Medium impact] The change reaches fatal-error reporting across library checks and direct callers. The library changes appear safe to merge, with a non-blocking test isolation fix recommended.
|
| int FatalLocationSeenByCallback(void (*fail)()) | ||
| { | ||
| std::fflush(nullptr); | ||
| const pid_t child = fork(); |
There was a problem hiding this comment.
Child can inherit locked mutex
FatalLocationSeenByCallback now forks, but the assert case still runs after PlatformInit starts the STDIO threads. If a startup log holds write_mutex_ when the fork happens, the child's printing call can wait forever because the thread holding that lock no longer exists. The parent then stays in waitpid until the test times out.
Register this case with isolated=true and platform_init=false, as the runner requires for tests that fork.
Prompt To Fix With AI
This is a comment left during a code review.
Path: test/automatic/core/assert/libxr_assert/test_assert.cpp
Line: 54
Comment:
**Child can inherit locked mutex**
`FatalLocationSeenByCallback` now forks, but the `assert` case still runs after `PlatformInit` starts the STDIO threads. If a startup log holds `write_mutex_` when the fork happens, the child's printing call can wait forever because the thread holding that lock no longer exists. The parent then stays in `waitpid` until the test times out.
Register this case with `isolated=true` and `platform_init=false`, as the runner requires for tests that fork.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.The case forks since it checks the fatal functions of the library in child processes. The runner requires such a case to run in a process of its own without PlatformInit: with the STDIO threads running, a child could inherit the STDIO mutex in the locked state and wait for it forever in libxr_fatal_error_and_print.
src/libxr.cpp held nothing but libxr_fatal_error, under a name that says nothing about it, and the new libxr_fatal_print.cpp sat next to it. Both functions are now in one file in the directory of the assertion support, src/core/assert/fatal_error.cpp, with the loop they share as a local function; the declaration of that loop in diag.hpp is gone again. src/ itself keeps only the umbrella header. The build collects the files of src/core/*/ already, and nothing refers to the old path. libxr_fatal_error_and_print stays the only reference to the printing code. A firmware link that collects unreferenced sections drops it when nothing calls it.
Chinese lines are followed directly by their English counterpart, the @brief of the two functions is one line with the details under @note, and two English continuation lines that had dropped out of the @note indentation are fixed. Source and test files use // comments only; the comment moved with libxr_fatal_error gets its Chinese half. test_topic.cpp no longer includes <cstring>, which only the removed file-name check used.
dev now has the fatal error split (#364) and the shared Topic test cleanup (#363). One conflict, in test/automatic/main.cpp: the USB cases of this branch stay and the case assert takes the registration from dev (isolated, without platform initialization). The CH32 endpoint test programs define libxr_fatal_error themselves; they now define libxr_fatal_error_and_print as well and accept a null file name.
libxr_fatal_errorno longer prints, and a failedREQUIREno longer carries a file name and a line number. The printing moves into a second function that onlyASSERTandDEV_ASSERTcall.Why: all three assertion macros called
libxr_fatal_error(__FILE__, __LINE__, in_isr), and that function printedFatal error at file:linebefore every call of the fatal callback.REQUIREis always on, so release firmware carried a file name and a line number at each of itsREQUIREsites, and the code that formats and prints them. Printing also takes the STDIO mutex with an unbounded wait, so aREQUIREthat fails while that mutex is held never reaches the callback.Two functions with the same parameters:
libxr_fatal_error(file, line, in_isr)REQUIRE,REQUIRE_FROM_CALLBACK(with a null file name and line 0), C codelibxr_fatal_error_and_print(file, line, in_isr)Fatal error at file:line, aslibxr_fatal_errordidASSERT,DEV_ASSERTand their_FROM_CALLBACKforms (with__FILE__and__LINE__)ASSERTandDEV_ASSERTare not active in release firmware, so nothing there refers tolibxr_fatal_error_and_print, and a link that collects unreferenced sections drops it together with the printing code. Both functions are insrc/core/assert/fatal_error.cpp;src/libxr.cpp, which held nothing butlibxr_fatal_error, is gone.Not changed: the type of the fatal callback, what happens after the failure (the callback is called in a loop with 500 ms between the calls and the function does not return) and the interrupt-context path.
Behaviour changes for users:
REQUIREfailed is found with a debugger in the call stack. The fatal callback gets a null file name and line 0 for it, so a callback that uses the file name has to accept null.libxr_fatal_error(file, line, in_isr), such as a stack-overflow hook, compiles as before and no longer prints. The callback still gets its file name and line number.libxr_fatal_errorand usesASSERTorDEV_ASSERThas to definelibxr_fatal_error_and_printas well.Size, measured in an STM32H723 firmware:
LIBXR_DEBUG_BUILDoff (release)LIBXR_DEBUG_BUILDon (Debug)In the release build the flash image contains neither
libxr_fatal_error_and_printnor the stringFatal error at. About 1 KB of the saving is the file names and arguments at theREQUIREsites, the rest is the printing code.Tests:
test_assert_<debug>_<dev>) check which of the two functions each macro calls and what location it passes.assertoflibxr_testruns a failingREQUIREand both functions through the library in child processes and checks the location the callback gets and that neither function returns. Because it forks, it runs in a process of its own without platform initialization, as the test runner requires.topic.cpp. Those checks areREQUIREs, so it now confirms only that a fatal error happened.(no location)for a failedREQUIRE.