Commit 77e25cb6 authored by Marius Wachtler's avatar Marius Wachtler

When loading a shared library let the GC know about the static variables

parent 6ab26e7f
......@@ -142,6 +142,11 @@ void deregisterPermanentRoot(void* obj) {
roots.erase(obj);
}
static std::vector<std::pair<void*, void*>> potential_root_ranges;
void registerPotentialRootRange(void* start, void* end) {
potential_root_ranges.push_back(std::make_pair(start, end));
}
extern "C" PyObject* PyGC_AddRoot(PyObject* obj) noexcept {
if (obj) {
// Allow duplicates from CAPI code since they shouldn't have to know
......@@ -268,6 +273,10 @@ void markPhase() {
visitor.visit(h->value);
}
for (auto& e : potential_root_ranges) {
visitor.visitPotentialRange((void* const*)e.first, (void* const*)e.second);
}
// if (VERBOSITY()) printf("Found %d roots\n", stack.size());
while (void* p = stack.pop()) {
assert(((intptr_t)p) % 8 == 0);
......
......@@ -33,6 +33,8 @@ void deregisterPermanentRoot(void* root_obj);
// even if they are not heap allocated.
void registerNonheapRootObject(void* obj);
void registerPotentialRootRange(void* start, void* end);
// If you want to have a static root "location" where multiple values could be stored, use this:
class GCRootHandle {
public:
......
......@@ -1290,6 +1290,15 @@ BoxedModule* importCExtension(const std::string& full_name, const std::string& l
assert(init);
// Let the GC know about the static variables.
uintptr_t bss_start = (uintptr_t)dlsym(handle, "__bss_start");
uintptr_t bss_end = (uintptr_t)dlsym(handle, "_end");
RELEASE_ASSERT(bss_end - bss_start < 100000, "Large BSS section detected - there maybe something wrong");
// only track void* aligned memory
bss_start = (bss_start + (sizeof(void*) - 1)) & ~(sizeof(void*) - 1);
bss_end -= bss_end % sizeof(void*);
gc::registerPotentialRootRange((void*)bss_start, (void*)bss_end);
char* packagecontext = strdup(full_name.c_str());
char* oldcontext = _Py_PackageContext;
_Py_PackageContext = packagecontext;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment