[Fix, Test] (MG_State/GLState, MG_Test/Wire): answer tracked write faults reported as SEGV_MAPERR (#25)

- Adreno 619 kernels report a write to a page the tracker protected as
  SEGV_MAPERR, which was chained to the JVM and killed the process
This commit is contained in:
swung0x48 committed 2026-12-02 07:56:38 -04:00
1 parent c2b6105461
commit 45e343a71f
3 files changed
+56 -20

No files matched your search

@@ -66,8 +66,8 @@ namespace MobileGL::MG_Record {
// CHAINING IS LOAD-BEARING: the JVM uses SIGSEGV for implicit null checks,
// so a fault outside every tracked range MUST reach the previous handler or
// the first NPE after this installs is a process kill. Ownership is three
// tests, applied in the handler below: kernel-raised permission fault
// (SEGV_ACCERR - never a userspace raise, never an unmapped page), inside a
// tests, applied in the handler below: kernel-raised fault (SEGV_ACCERR or
// SEGV_MAPERR - never a userspace raise), inside a
// live tracked range, and not an immediate same-instruction refault (a write
// we unprotected succeeds, so an instant refault is a non-write access and
// must crash honestly). Everything failing any test is chained verbatim.
@@ -125,9 +125,9 @@ namespace MobileGL::MG_Record {
// one of these three tests is chained to the previous owner untouched, so a
// genuinely broken memory access crashes exactly as it would without us:
//
// (1) KERNEL-RAISED PERMISSION FAULT ONLY: si_code must be SEGV_ACCERR. A fault
// on an unmapped page (SEGV_MAPERR) is a wild pointer, and anything raised
// from userspace (raise/kill/tgkill/sigqueue) carries si_code <= 0 - this
// (1) Kernel-raised faults only: si_code SEGV_ACCERR, or SEGV_MAPERR, which Adreno 619
// (SM6375) kernels report for a write to a page this tracker protected. Anything
// raised from userspace (raise/kill/tgkill/sigqueue) carries si_code <= 0 - this
// tracker never raises SIGSEGV itself, so a user-raised one is always foreign.
// (2) INSIDE A LIVE TRACKED RANGE: a fault anywhere else is the application's.
// (3) NOT A REFAULT WITHOUT RE-ARM: a write to a page this handler just
@@ -194,6 +194,16 @@ namespace MobileGL::MG_Record {
return true;
}
// Ownership test (1), shared by the handler and the unit test.
Bool FaultCodeIsKernelRaised(int code) {
#if MGL_TRACKER_MPROTECT_ARM
return code == SEGV_ACCERR || code == SEGV_MAPERR;
#else
(void)code;
return false;
#endif
}
// THE DECLINE RECORD. A fault this handler chains away is, for a page the tracker
// itself protected, a process kill with no evidence anywhere: debuggerd's tombstone
// names the application's memcpy and says nothing about who took the page's write
@@ -349,7 +359,7 @@ namespace MobileGL::MG_Record {
}
void PersistentWriteFaultHandler(int sig, siginfo_t* info, void* ucontext) {
if (info != nullptr && info->si_code == SEGV_ACCERR) {
if (info != nullptr && FaultCodeIsKernelRaised(info->si_code)) {
const uintptr_t address = reinterpret_cast<uintptr_t>(info->si_addr);
// EVERY overlapping slot gets its bit, not just the first: unprotecting
// after only the first mark would leave a second owner's block permanently
@@ -392,21 +402,25 @@ namespace MobileGL::MG_Record {
// epoch is guaranteed to see the bit that goes with it.
g_faultEpoch.fetch_add(1, std::memory_order_release);
// Un-arm is a raw syscall, deliberately not the libc wrapper's
// bookkeeping: the handler's whole job is to get out of the way.
mprotect(reinterpret_cast<void*>(UntagAddress(address) & ~(kPageBytes - 1)), kPageBytes,
PROT_READ | PROT_WRITE);
return;
// bookkeeping: the handler's whole job is to get out of the way. A page
// that really is unmapped fails the mprotect, and its fault is chained.
if (mprotect(reinterpret_cast<void*>(UntagAddress(address) & ~(kPageBytes - 1)), kPageBytes,
PROT_READ | PROT_WRITE) == 0) {
return;
}
} else if (info->si_code == SEGV_ACCERR) {
// A SEGV_ACCERR this handler does not own. Legitimate (the JVM's implicit
// null checks land here every time), so it is recorded and chained, never
// refused - but recorded, because the one case where it is not legitimate
// is a page this module protected and then failed to recognize, and that
// case is otherwise a tombstone with no mention of the tracker at all.
// Foreign SEGV_MAPERRs are not recorded: every wild or null access is one.
g_declinedFaults.fetch_add(1, std::memory_order_relaxed);
g_declinedFaultAddress.store(address, std::memory_order_relaxed);
g_declinedFaultNearestBase.store(nearestBase, std::memory_order_relaxed);
g_declinedFaultNearestEnd.store(nearestEnd, std::memory_order_relaxed);
if (nearestBase != 0) AnnounceDeclinedFault(address, nearestBase, nearestEnd);
}
// A SEGV_ACCERR this handler does not own. Legitimate (the JVM's implicit
// null checks land here every time), so it is recorded and chained, never
// refused - but recorded, because the ONE case where it is not legitimate
// is a page this module protected and then failed to recognize, and that
// case is otherwise a tombstone with no mention of the tracker at all.
g_declinedFaults.fetch_add(1, std::memory_order_relaxed);
g_declinedFaultAddress.store(address, std::memory_order_relaxed);
g_declinedFaultNearestBase.store(nearestBase, std::memory_order_relaxed);
g_declinedFaultNearestEnd.store(nearestEnd, std::memory_order_relaxed);
if (nearestBase != 0) AnnounceDeclinedFault(address, nearestBase, nearestEnd);
}
ChainToPreviousSegvHandler(sig, info, ucontext);
}
@@ -837,6 +851,10 @@ namespace MobileGL::MG_Record {
return UntagAddress(address);
}
Bool PersistentMapTracker::ClaimsFaultCodeForTest(int siCode) {
return FaultCodeIsKernelRaised(siCode);
}
// The ownership row against a slot the caller composed, WITHOUT protecting anything: an
// x86-64 host has no top byte of its own, so the only way to drive the arm64 defect on
// the gate that has to catch it is to hand the real predicate the two addresses the
@@ -184,6 +184,8 @@ namespace MobileGL::MG_Record {
static uintptr_t UntagAddressForTest(uintptr_t address);
static Bool OwnershipProbeForTest(uintptr_t slotBase, uintptr_t slotEnd, uintptr_t faultAddress,
SizeT* pageIndexOut);
// Unit tests only: whether the handler considers a fault with this si_code at all.
static Bool ClaimsFaultCodeForTest(int siCode);
// Unit tests and device triage: how many SEGV_ACCERRs the handler chained away as
// foreign, and the last one's si_addr beside the first live tracked span. Zero on a
// healthy client - the JVM's implicit null checks do land here, so a NON-zero count
@@ -144,6 +144,22 @@ TEST(PersistentMapFaultOwnership, UntagAddressClearsTheTopByteAndNothingBelowIt)
EXPECT_EQ(PersistentMapTracker::UntagAddressForTest(real), real);
}
// An Adreno 619 (SM6375) kernel reported the application's write to a page the tracker had
// protected as SEGV_MAPERR, with si_addr on that page. The handler has to consider both kernel
// codes, and still never a fault raised from userspace.
TEST(PersistentMapFaultOwnership, BothKernelFaultCodesAreConsideredAndUserRaisedOnesAreNot) {
#if defined(__linux__) || defined(__ANDROID__)
EXPECT_TRUE(PersistentMapTracker::ClaimsFaultCodeForTest(SEGV_ACCERR));
EXPECT_TRUE(PersistentMapTracker::ClaimsFaultCodeForTest(SEGV_MAPERR))
<< "a write to a protected page reported as SEGV_MAPERR was chained, which killed the process";
EXPECT_FALSE(PersistentMapTracker::ClaimsFaultCodeForTest(SI_USER));
EXPECT_FALSE(PersistentMapTracker::ClaimsFaultCodeForTest(SI_QUEUE));
EXPECT_FALSE(PersistentMapTracker::ClaimsFaultCodeForTest(SI_TKILL));
#else
GTEST_SKIP() << "no mprotect arm on this platform";
#endif
}
// =====================================================================================
// 2. A MAP HANDS BACK A RANGE THE APPLICATION CAN WRITE
// =====================================================================================