Skip to content

Commit 1c5cc66

Browse files
committed
Fix problem with early-unbound module in different trees
1 parent a920ff8 commit 1c5cc66

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

include/deemon/module.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ struct Dee_module_object {
11661166
#define Dee_MODULE_HASHNX(hs, perturb) (void)((hs) = ((hs) << 2) + (hs) + (perturb) + 1, (perturb) >>= 5) /* This `5' is tunable. */
11671167
#define Dee_MODULE_HASHIT(self, i) (Dee_REQUIRES_OBJECT(DeeModuleObject, self)->mo_bucketv + ((i) & ((DeeModuleObject *)(self))->mo_bucketm))
11681168
DREF DeeModuleObject *const *mo_importv; /* [1..1][const_if(Dee_MODULE_FDIDLOAD)][0..mo_importc][lock(Dee_MODULE_FLOADING)][const_if(Dee_MODULE_FDIDLOAD)][owned] Vector of other modules imported by this one. */
1169-
/* TODO: Make "Module" a variable-length object and inline "mo_globalv" (== one less indirection necessary for access to globals) */
1169+
/* TO-DO: Make "Module" a variable-length object and inline "mo_globalv" (== one less indirection necessary for access to globals) */
11701170
DREF DeeObject **mo_globalv; /* [0..1][lock(mo_lock)][0..mo_globalc][valid_if(Dee_MODULE_FDIDLOAD)][owned] Vector of module-private global variables. */
11711171
DREF struct Dee_code_object *mo_root; /* [0..1][lock(mo_lock)][const_if(Dee_MODULE_FDIDLOAD)] Root code object (Also used as constructor).
11721172
* HINT: Other code objects are addressed through constant/static variables.
@@ -1526,7 +1526,7 @@ DeeModule_OpenSourceStreamString(/*File*/ DeeObject *source_stream,
15261526
* relinquish their right to modify the module's `mo_importv', `mo_globalv',
15271527
* `mo_root', `mo_bucketv', etc... fields.
15281528
* - Interactive code should refrain from starting new threads.
1529-
* TODO: Currently, not abiding by this rule will result in hard undefined
1529+
* TO-DO: Currently, not abiding by this rule will result in hard undefined
15301530
* behavior, potentially resulting in deemon crashing completely.
15311531
* >> This is a bug that must be resolved at some point!
15321532
* The problem are the `mo_globalv', `mo_importv' and `mo_bucketv'

src/deemon/execute/modpath.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,9 @@ DeeModule_OpenDex(/*inherit(always)*/ /*utf-8*/ char *__restrict absname,
14341434
DeeSystem_DlClose(dex_handle);
14351435
return result;
14361436
}
1437+
ASSERT(result->mo_absnode.rb_par != DeeModule_IMPORT_ENOENT);
14371438
module_abstree_removenode(&module_abstree_root, result);
1439+
result->mo_absnode.rb_par = DeeModule_IMPORT_ENOENT;
14381440
}
14391441

14401442
/* Lookup "DEX" descriptor export. */
@@ -1465,6 +1467,7 @@ DeeModule_OpenDex(/*inherit(always)*/ /*utf-8*/ char *__restrict absname,
14651467
return existing_module;
14661468
}
14671469
module_byaddr_removenode(&module_byaddr_tree, existing_module);
1470+
existing_module->mo_adrnode.rb_par = DeeModule_IMPORT_ENOENT;
14681471
}
14691472

14701473
/* Sanity check: does this look like a valid dex object? */
@@ -1585,6 +1588,7 @@ DeeModule_OpenDex(/*inherit(always)*/ /*utf-8*/ char *__restrict absname,
15851588
goto handle_existing_module;
15861589
}
15871590
module_byaddr_removenode(&module_byaddr_tree, existing_module);
1591+
existing_module->mo_adrnode.rb_par = DeeModule_IMPORT_ENOENT;
15881592
}
15891593
Dee_DPRINTF("[RT][dex] Add dex module at %p-%p: %q\n",
15901594
result->mo_minaddr, result->mo_maxaddr, absname);
@@ -1801,7 +1805,8 @@ INTERN void DCALL DeeModule_UnloadAllDexModules(void) {
18011805
INTERN NONNULL((1)) void DCALL
18021806
module_dee_unbind(DeeModuleObject *__restrict self) {
18031807
module_byaddr_lock_write();
1804-
module_byaddr_removenode(&module_byaddr_tree, self);
1808+
if (self->mo_adrnode.rb_par != DeeModule_IMPORT_ENOENT)
1809+
module_byaddr_removenode(&module_byaddr_tree, self);
18051810
module_byaddr_lock_endwrite();
18061811
}
18071812
#endif /* CONFIG_EXPERIMENTAL_MMAP_DEC */
@@ -1934,28 +1939,33 @@ INTERN NONNULL((1)) void DCALL
19341939
module_unbind(DeeModuleObject *__restrict self) {
19351940
ASSERT(self->mo_absname);
19361941
module_abstree_lock_write();
1937-
module_abstree_removenode(&module_abstree_root, self);
1942+
if (self->mo_absnode.rb_par != DeeModule_IMPORT_ENOENT)
1943+
module_abstree_removenode(&module_abstree_root, self);
19381944
module_abstree_lock_endwrite();
19391945
if (self->mo_libname.mle_name) {
19401946
struct Dee_module_libentry *iter = &self->mo_libname;
19411947
module_libtree_lock_write();
1942-
ASSERT(Dee_module_libentry_getmodule(iter) == self);
1943-
module_libtree_removenode(&module_libtree_root, iter);
1944-
while ((iter = iter->mle_next) != NULL) {
1948+
if unlikely(!self->mo_libname.mle_name) {
1949+
module_libtree_lock_endwrite();
1950+
} else {
19451951
ASSERT(Dee_module_libentry_getmodule(iter) == self);
19461952
module_libtree_removenode(&module_libtree_root, iter);
1947-
}
1948-
module_libtree_lock_endwrite();
1949-
iter = &self->mo_libname;
1950-
Dee_Decref(iter->mle_name);
1951-
iter = iter->mle_next;
1952-
while (iter) {
1953-
struct Dee_module_libentry *next;
1954-
ASSERT(Dee_module_libentry_getmodule(iter) == self);
1955-
next = iter->mle_next;
1953+
while ((iter = iter->mle_next) != NULL) {
1954+
ASSERT(Dee_module_libentry_getmodule(iter) == self);
1955+
module_libtree_removenode(&module_libtree_root, iter);
1956+
}
1957+
module_libtree_lock_endwrite();
1958+
iter = &self->mo_libname;
19561959
Dee_Decref(iter->mle_name);
1957-
Dee_module_libentry_free(iter);
1958-
iter = next;
1960+
iter = iter->mle_next;
1961+
while (iter) {
1962+
struct Dee_module_libentry *next;
1963+
ASSERT(Dee_module_libentry_getmodule(iter) == self);
1964+
next = iter->mle_next;
1965+
Dee_Decref(iter->mle_name);
1966+
Dee_module_libentry_free(iter);
1967+
iter = next;
1968+
}
19591969
}
19601970
}
19611971
}
@@ -2797,7 +2807,9 @@ DeeModule_OpenFile_impl2(/*inherit_if(!(flags & _DeeModule_IMPORT_F_NO_INHERIT_F
27972807
Dee_Free(abs_filename);
27982808
return existing;
27992809
}
2810+
ASSERT(existing->mo_absnode.rb_par != DeeModule_IMPORT_ENOENT);
28002811
module_abstree_removenode(&module_abstree_root, existing);
2812+
existing->mo_absnode.rb_par = DeeModule_IMPORT_ENOENT;
28012813
}
28022814
module_abstree_insert(&module_abstree_root, result);
28032815
#ifdef CONFIG_EXPERIMENTAL_MMAP_DEC
@@ -2963,7 +2975,9 @@ do_DeeModule_CreateDirectory(/*inherit(always)*/ /*utf-8*/ char *__restrict abs_
29632975
Dee_Free(abs_dirname);
29642976
return existing_result;
29652977
}
2978+
ASSERT(existing_result->mo_absnode.rb_par != DeeModule_IMPORT_ENOENT);
29662979
module_abstree_removenode(&module_abstree_root, existing_result);
2980+
existing_result->mo_absnode.rb_par = DeeModule_IMPORT_ENOENT;
29672981
}
29682982
module_abstree_insert(&module_abstree_root, result);
29692983
result = DeeGC_TRACK(DeeModuleObject, result);

0 commit comments

Comments
 (0)