ladybird/Userland/Libraries/LibC/assert.cpp
Jean-Baptiste Boric 8043fcd466 LibC: Don't format strings when asserting with an unstable heap
If we hit an assertion while the heap isn't in a stable state, we can't
rely on dynamic memory allocation because the malloc mutex is already
held and the heap is most likely corrupted. Instead, we need to bail
out fast before we make the situation even worse.
2021-09-18 01:35:11 +00:00

43 lines
842 B
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Format.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/internals.h>
#include <syscall.h>
#include <unistd.h>
extern "C" {
extern bool __stdio_is_initialized;
#ifndef NDEBUG
void __assertion_failed(const char* msg)
{
if (__heap_is_stable) {
dbgln("ASSERTION FAILED: {}", msg);
if (__stdio_is_initialized)
warnln("ASSERTION FAILED: {}", msg);
}
Syscall::SC_set_coredump_metadata_params params {
{ "assertion", strlen("assertion") },
{ msg, strlen(msg) },
};
syscall(SC_set_coredump_metadata, &params);
abort();
}
#endif
}
void _abort()
{
asm volatile("ud2");
__builtin_unreachable();
}