Make development more pleasant on MacOS Arm64

This commit is contained in:
Justine Tunney 2023-11-12 05:42:17 -08:00
parent f7cad70da1
commit 0863427b3a
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
8 changed files with 90 additions and 10 deletions

View file

@ -14,7 +14,8 @@
#define _ASSERT_H
COSMOPOLITAN_C_START_
void __assert_fail(const char *, const char *, int) wontreturn relegated;
void __assert_fail(const char *, const char *, int);
void __unassert_fail(const char *, const char *, int);
#ifdef NDEBUG
#define assert(x) ((void)0)
@ -31,12 +32,14 @@ void __assert_fail(const char *, const char *, int) wontreturn relegated;
#ifndef NDEBUG
#define unassert(x) __assert_macro(x, #x)
#define npassert(x) __assert_macro(x, #x)
#define __assert_macro(x, s) \
({ \
if (__builtin_expect(!(x), 0)) { \
__assert_fail(s, __FILE__, __LINE__); \
} \
(void)0; \
#define __assert_macro(x, s) \
({ \
if (__builtin_expect(!(x), 0)) { \
__unassert_fail(s, __FILE__, __LINE__); \
__asm__("nop"); \
__builtin_unreachable(); \
} \
(void)0; \
})
#else
#define npassert(x) \

View file

@ -31,8 +31,8 @@ void __assert_fail(const char *expr, const char *file, int line) {
char ibuf[12];
sigset_t m = __sig_block();
FormatInt32(ibuf, line);
tinyprint(2, file, ":", ibuf, ": assert(", expr, ") failed (",
program_invocation_short_name, " ",
tinyprint(2, file, ":", ibuf, ": \e[31;1massert(", expr,
") failed\e[0m (cosmoaddr2line ", program_invocation_name, " ",
DescribeBacktrace(__builtin_frame_address(0)), ")\n", NULL);
__sig_unblock(m);
abort();

44
libc/calls/unassertfail.c Normal file
View file

@ -0,0 +1,44 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/describebacktrace.internal.h"
#include "libc/runtime/runtime.h"
/**
* Handles unassert() failure.
*
* Unlike __assert_fail() this doesn't call abort() and instead uses a
* trapping assembly opcode. Standards require that abort() do a great
* number of things. By doing this instead, we guarantee the backtrace
* reported to the signal handler is clean and focused on the problem.
*/
void __unassert_fail(const char *expr, const char *file, int line) {
char ibuf[12];
sigset_t m = __sig_block();
FormatInt32(ibuf, line);
tinyprint(2, file, ":", ibuf, ": \e[31;1munassert(", expr,
") failed\e[0m (cosmoaddr2line ", program_invocation_name, " ",
DescribeBacktrace(__builtin_frame_address(0)), ")\n", NULL);
__sig_unblock(m);
notpossible;
}

View file

@ -601,6 +601,13 @@ void abort(void) wontreturn;
"nop"); \
__builtin_unreachable(); \
} while (0)
#elif defined(__aarch64__)
#define notpossible \
do { \
asm("udf\t#0\n\t" \
"nop"); \
__builtin_unreachable(); \
} while (0)
#else
#define notpossible __builtin_trap()
#endif

View file

@ -20,5 +20,5 @@
// stub version of assert() to keep the build a dag
__attribute__((__weak__)) void __assert_fail(const char *expr, const char *file,
int line) {
__builtin_trap();
notpossible;
}

View file

@ -0,0 +1,24 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
// stub version of unassert() to keep the build a dag
__attribute__((__weak__)) void __unassert_fail(const char *expr,
const char *file, int line) {
notpossible;
}

View file

@ -0,0 +1 @@
aarch64-linux-cosmo-c++filt

View file

@ -0,0 +1 @@
x86_64-linux-cosmo-c++filt