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.
This commit is contained in:
Justine Tunney 2024-02-22 11:06:31 -08:00
parent 38af54a2dd
commit e9a756e78b
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
2 changed files with 6 additions and 1 deletions

View file

@ -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++ = ' ';

View file

@ -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;
}