Skip to content

Commit e9a756e

Browse files
committed
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.
1 parent 38af54a commit e9a756e

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

libc/intrin/describebacktrace.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/intrin/describebacktrace.internal.h"
20+
#include "libc/intrin/kprintf.h"
21+
#include "libc/intrin/weaken.h"
2022
#include "libc/log/libfatal.internal.h"
2123
#include "libc/nexgen32e/stackframe.h"
2224

@@ -28,6 +30,9 @@ dontinstrument const char *(DescribeBacktrace)(char buf[N],
2830
char *pe = p + N;
2931
bool gotsome = false;
3032
while (fr) {
33+
if (_weaken(kisdangerous) && _weaken(kisdangerous)(fr)) {
34+
break;
35+
}
3136
if (p + 16 + 1 + 1 <= pe) {
3237
if (gotsome) {
3338
*p++ = ' ';

libc/runtime/ftracer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static struct CosmoFtrace g_ftrace;
5151

5252
__funline int GetNestingLevelImpl(struct StackFrame *frame) {
5353
int nesting = -2;
54-
while (frame) {
54+
while (frame && !kisdangerous(frame)) {
5555
++nesting;
5656
frame = frame->next;
5757
}

0 commit comments

Comments
 (0)