Re:Why isn't memset() async-signal-safe?
by trentnelson on Wednesday January 04, 2017 @08:19PM
(#13323870)
> This is probably not such a better method.
It actually really, really is. At least from a technical perspective on x64. Having language-level lexical scoping of system-level exceptions is incredibly useful and once you've grokked how NT's trap handler, PE .xdata sections, prologues and epilogues all work in concert, signals just seem barbaric.
Here's an example trapping an access violation:
//
// Prefault the page.
//
TRY_MAPPED_MEMORY_OP {
TraceStore->Rtl->PrefaultPages(PrefaultMemoryMap->NextAddress, 1);
} CATCH_EXCEPTION_ACCESS_VIOLATION {
//
// This will happen if servicing the prefault off-core has taken longer
// for the originating core (the one that submitted the prefault work)
// to consume the entire memory map, then *another* memory map, which
// will retire the memory map backing this prefault address, which
// results in the address being invalidated, which results in an access
// violation when we try and read/prefault it from the thread pool.
//
TraceStore->Stats->AccessViolationsEncounteredDuringAsyncPrefault++;
}
Or a page fault that has occurred against a memory map backed file (because, say, the underlying network drive has been disconnected):
TRY_MAPPED_MEMORY_OP {
//
// Copy the caller's address range structure over.
//
__movsq((PDWORD64)NewAddressRange,
(PDWORD64)AddressRange,
sizeof(*NewAddressRange) >> 3);
//
// If there's an existing address range set, update its ValidTo
// timestamp.
//
if (TraceStore->AddressRange) {
TraceStore->AddressRange->Timestamp.ValidTo.QuadPart = (
Timestamp.QuadPart
);
}
//
// Update the trace store's address range pointer.
//
TraceStore->AddressRange = NewAddressRange;
} CATCH_STATUS_IN_PAGE_ERROR {
//
// We'll leak the address range we just allocated here, but a copy
// failure is indicative of much bigger issues (drive full, network
// map disappearing) than leaking ~32 bytes, so we don't attempt to
// roll back the allocation.
//
return FALSE;
}
It actually really, really is. At least from a technical perspective on x64. Having language-level lexical scoping of system-level exceptions is incredibly useful and once you've grokked how NT's trap handler, PE .xdata sections, prologues and epilogues all work in concert, signals just seem barbaric.
Here's an example trapping an access violation:
Or an alignment fault: Or an illegal instruction: Or a page fault that has occurred against a memory map backed file (because, say, the underlying network drive has been disconnected): Relevant macro definitions: