summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2020-11-23 14:58:15 -0500
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-12-01 19:58:17 -0500
commit97d71646ddf3814de62573100ed0b224d1588cbc (patch)
treeef0db7a064554e585528ed9f76d799a77157c9bb
parentadd0aeaefd4d823d31315564e924ce8c018fb69e (diff)
downloadhaskell-97d71646ddf3814de62573100ed0b224d1588cbc.tar.gz
rts/linker: Introduce munmapForLinker
Consolidates munmap calls to ensure consistent error handling.
-rw-r--r--rts/Linker.c18
-rw-r--r--rts/LinkerInternals.h1
-rw-r--r--rts/linker/LoadArchive.c2
-rw-r--r--rts/linker/M32Alloc.c25
-rw-r--r--rts/linker/MachO.c2
-rw-r--r--rts/linker/SymbolExtras.c2
-rw-r--r--rts/linker/elf_got.c2
7 files changed, 22 insertions, 30 deletions
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
diff --git a/rts/LinkerInternals.h b/rts/LinkerInternals.h
index 954c36764e..84a67753bd 100644
--- a/rts/LinkerInternals.h
+++ b/rts/LinkerInternals.h
@@ -365,6 +365,7 @@ SymbolAddr* loadSymbol(SymbolName *lbl, RtsSymbolInfo *pinfo);
void *mmapAnonForLinker (size_t bytes);
void *mmapForLinker (size_t bytes, uint32_t prot, uint32_t flags, int fd, int offset);
void mmapForLinkerMarkExecutable (void *start, size_t len);
+void munmapForLinker (void *addr, size_t bytes, const char *caller);
void addProddableBlock ( ObjectCode* oc, void* start, int size );
void checkProddableBlock (ObjectCode *oc, void *addr, size_t size );
diff --git a/rts/linker/LoadArchive.c b/rts/linker/LoadArchive.c
index 9dda0e99e8..041ebef4b6 100644
--- a/rts/linker/LoadArchive.c
+++ b/rts/linker/LoadArchive.c
@@ -613,7 +613,7 @@ fail:
stgFree(fileName);
if (gnuFileIndex != NULL) {
#if RTS_LINKER_USE_MMAP
- munmap(gnuFileIndex, gnuFileIndexSize + 1);
+ munmapForLinker(gnuFileIndex, gnuFileIndexSize + 1, "loadArchive_");
#else
stgFree(gnuFileIndex);
#endif
diff --git a/rts/linker/M32Alloc.c b/rts/linker/M32Alloc.c
index f1440e093c..678df95485 100644
--- a/rts/linker/M32Alloc.c
+++ b/rts/linker/M32Alloc.c
@@ -217,25 +217,6 @@ unsigned int m32_free_page_pool_size = 0;
// TODO
/**
- * Wrapper for `unmap` that handles error cases.
- * This is the real implementation. There is another dummy implementation below.
- * See the note titled "Compile Time Trickery" at the top of this file.
- */
-static void
-munmapForLinker (void * addr, size_t size)
-{
- IF_DEBUG(linker,
- debugBelch("m32_alloc: Unmapping %zu bytes at %p\n",
- size, addr));
-
- int r = munmap(addr,size);
- if (r == -1) {
- // Should we abort here?
- sysErrorBelch("munmap");
- }
-}
-
-/**
* Free a page or, if possible, place it in the free page pool.
*/
static void
@@ -246,7 +227,7 @@ m32_release_page(struct m32_page_t *page)
m32_free_page_pool = page;
m32_free_page_pool_size ++;
} else {
- munmapForLinker((void *) page, getPageSize());
+ munmapForLinker((void *) page, getPageSize(), "m32_release_page");
}
}
@@ -309,7 +290,7 @@ m32_allocator_unmap_list(struct m32_page_t *head)
{
while (head != NULL) {
struct m32_page_t *next = m32_filled_page_get_next(head);
- munmapForLinker((void *) head, head->filled_page.size);
+ munmapForLinker((void *) head, head->filled_page.size, "m32_allocator_unmap_list");
head = next;
}
}
@@ -327,7 +308,7 @@ void m32_allocator_free(m32_allocator *alloc)
const size_t pgsz = getPageSize();
for (int i=0; i < M32_MAX_PAGES; i++) {
if (alloc->pages[i]) {
- munmapForLinker(alloc->pages[i], pgsz);
+ munmapForLinker(alloc->pages[i], pgsz, "m32_allocator_free");
}
}
diff --git a/rts/linker/MachO.c b/rts/linker/MachO.c
index e39379fd7e..56e31c486b 100644
--- a/rts/linker/MachO.c
+++ b/rts/linker/MachO.c
@@ -525,7 +525,7 @@ makeGot(ObjectCode * oc) {
void
freeGot(ObjectCode * oc) {
- munmap(oc->info->got_start, oc->info->got_size);
+ munmapForLinker(oc->info->got_start, oc->info->got_size, "freeGot");
oc->info->got_start = NULL;
oc->info->got_size = 0;
}
diff --git a/rts/linker/SymbolExtras.c b/rts/linker/SymbolExtras.c
index 9d4eb89400..7df5d7d3d6 100644
--- a/rts/linker/SymbolExtras.c
+++ b/rts/linker/SymbolExtras.c
@@ -85,7 +85,7 @@ int ocAllocateExtras(ObjectCode* oc, int count, int first, int bssSize)
if (new) {
memcpy(new, oc->image, oc->fileSize);
if (oc->imageMapped) {
- munmap(oc->image, n);
+ munmapForLinker(oc->image, n, "ocAllocateExtras");
}
oc->image = new;
oc->imageMapped = true;
diff --git a/rts/linker/elf_got.c b/rts/linker/elf_got.c
index 4b014dac50..b3b8c1a554 100644
--- a/rts/linker/elf_got.c
+++ b/rts/linker/elf_got.c
@@ -144,7 +144,7 @@ verifyGot(ObjectCode * oc) {
void
freeGot(ObjectCode * oc) {
-// munmap(oc->info->got_start, oc->info->got_size);
+// munmapForLinker(oc->info->got_start, oc->info->got_size, "freeGot);
oc->info->got_start = 0x0;
oc->info->got_size = 0;
}