From 0c15afd9d0d87d1cff59dc2adb0afd5e1b6bdf72 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 5 Aug 2024 20:35:25 +0900 Subject: [PATCH] libdyld: use separate mem contexts for each objects --- libdyld/dyld.c | 15 +++++++++++---- libdyld/dyld_impl.h | 4 ++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/libdyld/dyld.c b/libdyld/dyld.c index 8483cebe..72cce966 100644 --- a/libdyld/dyld.c +++ b/libdyld/dyld.c @@ -652,7 +652,8 @@ dyld_object_destroy(struct dyld_object *obj) struct dyld *d = obj->dyld; struct mem_context *mctx = d->mctx; if (obj->local_import_obj != NULL) { - import_object_destroy(mctx, obj->local_import_obj); + import_object_destroy(&obj->instance_mctx, + obj->local_import_obj); } mem_free(mctx, obj->gots, obj->ngots * sizeof(*obj->gots)); mem_free(mctx, obj->plts, obj->nplts * sizeof(*obj->plts)); @@ -660,11 +661,13 @@ dyld_object_destroy(struct dyld_object *obj) instance_destroy(obj->instance); } if (obj->module != NULL) { - module_destroy(mctx, obj->module); + module_destroy(&obj->module_mctx, obj->module); } if (obj->bin != NULL) { unmap_file((void *)obj->bin, obj->binsz); } + mem_context_clear(&obj->module_mctx); + mem_context_clear(&obj->instance_mctx); mem_free(mctx, obj, sizeof(*obj)); } @@ -790,6 +793,10 @@ dyld_load_object_from_file(struct dyld *d, const struct name *name, ret = ENOMEM; goto fail; } + mem_context_init(&obj->module_mctx); + obj->module_mctx.parent = d->mctx; + mem_context_init(&obj->instance_mctx); + obj->instance_mctx.parent = d->mctx; obj->dyld = d; obj->name = name; ret = map_file(filename, (void *)&obj->bin, &obj->binsz); @@ -797,7 +804,7 @@ dyld_load_object_from_file(struct dyld *d, const struct name *name, goto fail; } struct load_context lctx; - load_context_init(&lctx, d->mctx); + load_context_init(&lctx, &obj->module_mctx); ret = module_create(&obj->module, obj->bin, obj->bin + obj->binsz, &lctx); if (ret != 0) { @@ -862,7 +869,7 @@ dyld_load_object_from_file(struct dyld *d, const struct name *name, obj->local_import_obj->next = d->shared_import_obj; struct report report; report_init(&report); - ret = instance_create(d->mctx, obj->module, &obj->instance, + ret = instance_create(&obj->instance_mctx, obj->module, &obj->instance, obj->local_import_obj, &report); if (ret != 0) { xlog_error("instance_create failed with %d: %s", ret, diff --git a/libdyld/dyld_impl.h b/libdyld/dyld_impl.h index 75f428d8..2ea5afe6 100644 --- a/libdyld/dyld_impl.h +++ b/libdyld/dyld_impl.h @@ -1,6 +1,7 @@ #include #include "list.h" +#include "mem.h" #include "type.h" struct dyld; @@ -40,6 +41,9 @@ struct dyld_object { struct module *module; struct instance *instance; + struct mem_context module_mctx; + struct mem_context instance_mctx; + struct dyld *dyld; LIST_ENTRY(struct dyld_object) q;