From e9a756e78bad5d97af4287882fd3c62b539f1911 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 22 Feb 2024 11:06:31 -0800 Subject: [PATCH] Make --ftrace more crash proof with foreign code Sometimes we need to interact with code that wasn't compiled using `-fno-omit-frame-pointer`. For example, if a function pointer gets passed and called by a foreign function, linked by cosmo_dlopen(). Function call tracing will now detect backtrace pointer corruption and simply reduce the indentation level back to zero, as a result. --- libc/intrin/describebacktrace.c | 5 +++++ libc/runtime/ftracer.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/libc/intrin/describebacktrace.c b/libc/intrin/describebacktrace.c index 4d1d5fba8..93107f884 100644 --- a/libc/intrin/describebacktrace.c +++ b/libc/intrin/describebacktrace.c @@ -17,6 +17,8 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/describebacktrace.internal.h" +#include "libc/intrin/kprintf.h" +#include "libc/intrin/weaken.h" #include "libc/log/libfatal.internal.h" #include "libc/nexgen32e/stackframe.h" @@ -28,6 +30,9 @@ dontinstrument const char *(DescribeBacktrace)(char buf[N], char *pe = p + N; bool gotsome = false; while (fr) { + if (_weaken(kisdangerous) && _weaken(kisdangerous)(fr)) { + break; + } if (p + 16 + 1 + 1 <= pe) { if (gotsome) { *p++ = ' '; diff --git a/libc/runtime/ftracer.c b/libc/runtime/ftracer.c index a95ac420a..2d54afe92 100644 --- a/libc/runtime/ftracer.c +++ b/libc/runtime/ftracer.c @@ -51,7 +51,7 @@ static struct CosmoFtrace g_ftrace; __funline int GetNestingLevelImpl(struct StackFrame *frame) { int nesting = -2; - while (frame) { + while (frame && !kisdangerous(frame)) { ++nesting; frame = frame->next; }