From 97d71646ddf3814de62573100ed0b224d1588cbc Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Mon, 23 Nov 2020 14:58:15 -0500 Subject: rts/linker: Introduce munmapForLinker Consolidates munmap calls to ensure consistent error handling. --- rts/Linker.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'rts/Linker.c') diff --git a/rts/Linker.c b/rts/Linker.c index 6873b1a9b7..eebde2a799 100644 --- a/rts/Linker.c +++ b/rts/Linker.c @@ -1149,6 +1149,14 @@ mmapAnonForLinker (size_t bytes) return mmapForLinker (bytes, PROT_READ|PROT_WRITE, MAP_ANONYMOUS, -1, 0); } +void munmapForLinker (void *addr, size_t bytes, const char *caller) +{ + int r = munmap(addr, bytes); + if (r == -1) { + // Should we abort here? + sysErrorBelch("munmap: %s", caller); + } +} /* Note [Memory protection in the linker] * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -1235,7 +1243,7 @@ freePreloadObjectFile (ObjectCode *oc) #else if (RTS_LINKER_USE_MMAP && oc->imageMapped) { - munmap(oc->image, oc->fileSize); + munmapForLinker(oc->image, oc->fileSize, "freePreloadObjectFile"); } else { stgFree(oc->image); @@ -1283,8 +1291,10 @@ void freeObjectCode (ObjectCode *oc) switch(oc->sections[i].alloc){ #if RTS_LINKER_USE_MMAP case SECTION_MMAP: - munmap(oc->sections[i].mapped_start, - oc->sections[i].mapped_size); + munmapForLinker( + oc->sections[i].mapped_start, + oc->sections[i].mapped_size, + "freeObjectCode"); break; case SECTION_M32: // Freed by m32_allocator_free @@ -2053,7 +2063,7 @@ void freeSegments (ObjectCode *oc) continue; } else { #if RTS_LINKER_USE_MMAP - CHECKM(0 == munmap(s->start, s->size), "freeSegments: failed to unmap memory"); + munmapForLinker(s->start, s->size, "freeSegments"); #else stgFree(s->start); #endif -- cgit v1.2.1