fix: lock crash report ring buffer (SMP race) and guard crashpad crash loop

This commit is contained in:
daniel committed 2026-06-19 19:36:21 +02:00
1 parent 925f28068f
commit c86650d662
5 files changed
+72 -28

No files matched your search

+22
View File
@@ -5,6 +5,7 @@
*/
#include "CrashReport.hpp"
#include <CppLib/Spinlock.hpp>
namespace CrashReport {
@@ -12,10 +13,31 @@ Report g_reports[MaxReports];
int g_reportHead = 0;
int g_reportCount = 0;
// Serializes ring-buffer access. Interrupt-disabling: the writer runs inside
// the exception handler (IF=0) while readers come from syscall context.
static kcp::Spinlock g_lock;
void AddReport(const Report& report) {
g_lock.Acquire();
g_reports[g_reportHead] = report;
g_reportHead = (g_reportHead + 1) % MaxReports;
if (g_reportCount < MaxReports) g_reportCount++;
g_lock.Release();
}
int TakeOldest(Report& out) {
g_lock.Acquire();
int had = g_reportCount;
if (had == 0) {
g_lock.Release();
return 0;
}
int oldestIdx = (g_reportHead - g_reportCount + MaxReports) % MaxReports;
out = g_reports[oldestIdx];
g_reports[oldestIdx].valid = false;
g_reportCount--;
g_lock.Release();
return had;
}
} // namespace CrashReport
+10 -2
View File
@@ -39,12 +39,20 @@ struct Report {
bool valid;
};
// Kernel-side ring buffer (written by exception handlers, read by SYS_CRASH_REPORT)
// Kernel-side ring buffer (written by exception handlers, read by SYS_CRASH_REPORT).
// All access is serialized by an internal interrupt-disabling spinlock: the
// writer (AddReport) runs in exception/interrupt context and the reader
// (TakeOldest) runs in syscall context, possibly on different CPUs.
extern Report g_reports[MaxReports];
extern int g_reportHead;
extern int g_reportCount;
// Add a report to the kernel ring buffer
// Add a report to the kernel ring buffer (called from exception handlers).
void AddReport(const Report& report);
// Pop the oldest report into `out`. Returns the number of reports that were
// present before this call (0 if the buffer was empty, in which case `out` is
// left untouched). Safe against concurrent AddReport / TakeOldest.
int TakeOldest(Report& out);
} // namespace CrashReport
+12
View File
@@ -10,6 +10,7 @@
#include <Memory/Paging.hpp>
#include <Memory/HHDM.hpp>
#include <Libraries/Memory.hpp>
#include <Libraries/String.hpp>
#include <Terminal/Terminal.hpp>
#include <CppLib/Stream.hpp>
#include <CppLib/Spinlock.hpp>
@@ -1399,6 +1400,17 @@ namespace Sched {
// handle to that same out-stream so it can surface a textual crash
// report to the console user when no desktop is running.
Process* crasher = GetProcessByPid(crasherPid);
// Crash-loop guard: never spawn a crashpad to report on the crashpad
// itself. If crashpad.elf faults (startup bug, OOM, etc.) we would
// otherwise spawn another crashpad, which can fault again, ad infinitum
// -- a fork bomb that also exhausts process slots and report buffers.
if (crasher && Lib::strcmp(crasher->name, crashpadPath) == 0) {
Kt::KernelLogStream(Kt::ERROR, "Sched")
<< "crashpad faulted; not spawning a nested crashpad";
return -1;
}
int crasherSlot = Ipc::SlotForPid(crasherPid);
bool inheritRedir = crasher && crasher->redirected &&
crasher->ioOutHandle >= 0 && crasherSlot >= 0;