Issue 242224
Summary [ORC] ELFDebugObjectPlugin never deregisters JIT debug objects from the GDB JIT interface: removed code leaves jit_code_entry objects whose symfile_addr points at freed memory
Labels new issue
Assignees
Reporter davidnich
    ## Summary

With `enableDebuggerSupport()` (LLJIT) on ELF targets, `ELFDebugObjectPlugin` registers a copy of each linked object with the GDB JIT interface through the `llvm_orc_registerJITLoaderGDBAllocAction` allocation action. Nothing ever deregisters it:

- the allocation action pair is created with an empty deallocation action (`{/* no deregistration */}` in `ELFDebugObjectPlugin::modifyPassConfig()`);
- `ELFDebugObjectPlugin::notifyRemovingResources()` erases the `DebugObject`s of the key, whose destructor deallocates the debug object's target memory, and has `// TODO: Implement unregister notifications.`;
- `JITLoaderGDB.cpp` (TargetProcess) has only `appendJITDebugDescriptor()`; there is no function that unlinks an entry, sets `JIT_UNREGISTER_FN` or deletes the `jit_code_entry` it allocated with `new`.

So when code is removed (`ResourceTracker::remove()`, or the `ExecutionSession` ends), its `jit_code_entry` stays in `__jit_debug_descriptor`'s list:

1. the entry itself leaks (one per linked object, for the life of the process); a JIT that compiles and removes code repeatedly grows the list without bound, and a debugger that attaches has to walk it;
2. the entry's `symfile_addr` points at the debug object's memory, which has been deallocated (and can be reused); a debugger that attaches later (GDB, or LLDB's JITLoaderGDB plugin) reads the list and the symbol files it points at, i.e. freed or reused memory.

The RuntimeDyld-based `GDBRegistrationListener` (`lib/ExecutionEngine/GDBRegistrationListener.cpp`) deregisters (unlink, `relevant_entry`, `JIT_UNREGISTER_FN`, `__jit_debug_register_code()`, delete) in `notifyFreeingObject()`; ORC lost this.

## Reproduction

LLVM 22.1.8, x86_64 Linux, in-process LLJIT with `enableDebuggerSupport(*J)`:

1. add an IR module to a JITDylib with its own `ResourceTracker`, look up a symbol (materialize it);
2. remove the tracker (`RT->remove()`);
3. repeat; after each round, walk `__jit_debug_descriptor.first_entry`: the number of entries grows by one per removed module, and each removed module's entry keeps its `symfile_addr`, whose memory has been deallocated.

(We see it in the Qore language runtime, which now removes the JIT code of deleted programs: 2 entries per program generation.)

## Suggested fix

Give the registration a deregistration counterpart in the target process, e.g. `llvm_orc_deregisterJITLoaderGDBAllocAction(ExecutorAddrRange)` that finds the entry with that `symfile_addr`, unlinks it under the same lock as `appendJITDebugDescriptor()`, sets `relevant_entry` and `JIT_UNREGISTER_FN`, calls `__jit_debug_register_code()` and deletes it; and pass it as the deallocation action of the pair that `ELFDebugObjectPlugin` adds (so the entry is deregistered when the linked object's allocation is deallocated, before or together with the debug object's memory). An EPC-based implementation would register the function as a bootstrap symbol alongside `RegisterJITLoaderGDBAllocActionName`.

## Workaround we use

A client-side `ObjectLinkingLayer::Plugin`, installed after the debugger support plugin, records the entries that each emitted object registered (by `ResourceKey`), follows `notifyTransferringResources()`, and in `notifyRemovingResources()` / `notifyFailed()` unregisters them through the GDB JIT protocol as `GDBRegistrationListener` does. This depends on `appendJITDebugDescriptor()`'s allocation (`new jit_code_entry`) and on all registrations being serialized by the client, so a fix in ORC would be preferable.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to