Skip to content

Commit

Permalink
libdyld: use separate mem contexts for each objects
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Aug 5, 2024
1 parent d7183db commit 0c15afd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 11 additions & 4 deletions libdyld/dyld.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,19 +652,22 @@ 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));
if (obj->instance != NULL) {
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));
}

Expand Down Expand Up @@ -790,14 +793,18 @@ 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);
if (ret != 0) {
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) {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions libdyld/dyld_impl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdbool.h>

#include "list.h"
#include "mem.h"
#include "type.h"

struct dyld;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 0c15afd

Please sign in to comment.