Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zephyr/wrapper.c: dereference NULL in POSIX for better k_panic() trace #8886

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions zephyr/wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,32 @@ int poll_for_register_delay(uint32_t reg, uint32_t mask,
return 0;
}

/* Mutable, volatile and not static to escape optimizers and static
* analyzers.
*/
volatile int *_sof_fatal_null = NULL;

void k_sys_fatal_error_handler(unsigned int reason,
const z_arch_esf_t *esf)
{
ARG_UNUSED(esf);

/* flush and switch to immediate mode */
LOG_PANIC();

ipc_send_panic_notification();

#if defined(CONFIG_ARCH_POSIX) || defined(CONFIG_ZEPHYR_POSIX)
LOG_ERR("Halting emulation");

/* In emulation we want to stop _immediately_ and print a useful stack
* trace; not a useless pointer to some signal handler or Zephyr
* cleanup routine. See Zephyr POSIX limitations discussed in:
* https://github.com/zephyrproject-rtos/zephyr/pull/68494
*/
*_sof_fatal_null = 42;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually do it with something as simple as *(int *)NULL = 0; Any reason this wouldn't be enough? Never failed me until now, except on some particular hardware platforms like #6093

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimizers and modern code flow analysis tend to make a mess of code that does undefined things. Putting it in an extern guarantees (at least until LTO lands) that the compiler can't "fix" this. The fundamental problem is that the desired behavior here isn't in fact portable: there's no way under the C standard to "crash in a way that ASAN detects before the OS".

#else
LOG_ERR("Halting system");
#endif
k_fatal_halt(reason);
}