Make old C++ demangler asynchronous signal safe

It's now possible to safely print C++ backtraces from signal handlers.
This symbol demangler doesn't need malloc, tls, or even static memory.
Additionally, this change makes it 2x faster and adds test cases. It's
almost as performant and accurate as the libcxxabi implementation now.
This commit is contained in:
Justine Tunney 2024-05-07 03:06:12 -07:00
parent a6ecbb747d
commit 57c0b065c8
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
10 changed files with 42326 additions and 1979 deletions

View file

@ -6,6 +6,8 @@ errno_t cosmo_once(_Atomic(uint32_t) *, void (*)(void));
int systemvpe(const char *, char *const[], char *const[]) libcesque;
char *GetProgramExecutableName(void);
void unleaf(void);
int __demangle(char *, const char *, size_t);
int __is_mangled(const char *);
COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_COSMO_H_ */

View file

@ -29,7 +29,7 @@
* @assume stack memory isn't stored beneath %rsp (-mno-red-zone)
* @deprecated
*/
optimizesize bool32 _isheap(void *p) {
optimizesize bool32 _isheap(const void *p) {
intptr_t x, y;
x = kAutomapStart;
y = x + kAutomapSize;

View file

@ -110,9 +110,9 @@ void *_mapanon(size_t) attributeallocsize((1)) mallocesque;
void *_mapshared(size_t) attributeallocsize((1)) mallocesque;
void CheckForMemoryLeaks(void);
void CheckForFileLeaks(void);
bool32 _isheap(const void *);
void __enable_threads(void);
void __oom_hook(size_t);
bool32 _isheap(void *);
/* code morphing */
void __morph_begin(void);
void __morph_end(void);

File diff suppressed because it is too large Load diff

View file

@ -6,6 +6,7 @@ PKGS += TEST_LIBC_STR
TEST_LIBC_STR_FILES := $(wildcard test/libc/str/*)
TEST_LIBC_STR_SRCS_C = $(filter %.c,$(TEST_LIBC_STR_FILES))
TEST_LIBC_STR_SRCS_CC = $(filter %.cc,$(TEST_LIBC_STR_FILES))
TEST_LIBC_STR_INCS = $(filter %.inc,$(TEST_LIBC_STR_FILES))
TEST_LIBC_STR_SRCS = $(TEST_LIBC_STR_SRCS_C) $(TEST_LIBC_STR_SRCS_CC)
TEST_LIBC_STR_SRCS_TEST_C = $(filter %_test.c,$(TEST_LIBC_STR_FILES))
TEST_LIBC_STR_SRCS_TEST_CC = $(filter %_test.cc,$(TEST_LIBC_STR_FILES))

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,58 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2024 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 "demangle_cases.inc"
#include "libc/cosmo.h"
#include "libc/macros.internal.h"
#include "libc/stdio/internal.h"
#include "libc/str/str.h"
char got[65536];
static void regenerate_cases(void) {
FILE *f = fopen("demangle_cases.inc", "w");
fprintf(f, "const char* demangle_cases[][2] = {\n");
for (int i = 0; i < ARRAYLEN(demangle_cases); ++i) {
const char *input = demangle_cases[i][0];
const char *want = demangle_cases[i][1];
if (__demangle(got, input, sizeof(got)) == -1) {
fprintf(f, "\n // {%`'s,\n // %`'s},\n // got error\n\n", input,
want);
} else if (!strcmp(want, got)) {
fprintf(f, " {%`'s, %`'s},\n", input, got);
} else {
fprintf(f,
"\n // {%`'s,\n // %`'s},\n // %`'s was returned\n\n",
input, want, got);
}
fflush(f);
}
fprintf(f, "};\n");
fclose(f);
}
int main() {
for (int i = 0; i < ARRAYLEN(demangle_cases); ++i) {
const char *input = demangle_cases[i][0];
const char *want = demangle_cases[i][1];
if (__demangle(got, input, sizeof(got)) == -1)
return 1;
if (strcmp(want, got))
return 2;
}
}

View file

@ -33,6 +33,7 @@ TEST_POSIX_DIRECTDEPS = \
LIBC_PROC \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSV \
LIBC_THREAD