Repository navigation
Fix GC bugs with embedded fields and stop tracing constants - #7085
Open
SeanTAllen wants to merge 1 commit into
Open
SeanTAllen wants to merge 1 commit into
SeanTAllen wants to merge 1 commit into
Conversation
An embedded field can share its address with the object that holds it, and the GC traced each address as a single type. Because of that, the GC could free a struct's other fields when the struct's first field was embedded, could trace forever around a reference cycle through embedded objects, and could collect an actor while another actor held one of its embedded fields. With a Pointer to the start of a struct, made through FFI, the GC could also free the struct's fields. The runtime now traces a shared address once for each type at it, finds the actor that owns an embedded field, and traces Pointer fields as opaque and with no type. To find the actor that owns an embedded field, the runtime needs to know where the compiler's type descriptors are, so the compiler now places them in a section of their own. It also places its constants in a second section. Nothing writes to a constant at run time, so the GC now skips constants, which makes collection much faster for actors that hold many references to string literals. Closes #6693 Closes #6694 Closes #6695
SeanTAllen
force-pushed
the
embed-gc-timers
branch
from
December 4, 2026 06:55
8ce61f8 to
67a5332
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An embedded field lives inside its holder's memory with no allocation of its own, and at the start of a struct it has the struct's address. This PR fixes four garbage collector bugs in how the runtime traces embedded fields and addresses that two types share. Three involve embedded fields, and the fourth involves a
Pointerto the start of a struct. Across the four bugs, the runtime freed memory that was still in use, traced forever, or collected an actor while another actor still held its field. I changed how the runtime traces addresses that can be shared, and the compiler now puts the type descriptors in a section of their own so the runtime can find the actor that owns an embedded field. With the same section machinery, the compiler also puts its constants in a section of their own, and the GC skips them. The constant section isn't needed for the fixes, but it makes GC of actors that hold lots of string literals a good deal faster.What it fixes
A struct whose first field is embedded could lose its other fields (#6693). A struct has no object header, so it has the same address as its first field when that field is embedded. If a program held that field apart from the struct, the runtime could trace the address only as the field's type and never as the struct. It then freed the struct's other fields while the struct still pointed at them. This happened in heap GC and in the trace of a sent message.
A reference cycle through embedded objects made the trace run forever (#6694). The simplest case is an embedded object that references itself. The runtime hung on three forms of the cycle, in GC and in message tracing: an embedded field in a class object on the heap, an embedded field of an actor, and an embedded
valfield of an actor that the actor sends.An actor could be collected while another actor held its embedded field. If an actor sent an embedded
valfield to another actor and then had no more work, the runtime could collect the sender and reuse its memory for actors created later, while the receiver still held the field.A
Pointerto the start of a struct could get the struct's fields freed. A class can hold aPointerto a struct's start in a field declared before the field that holds the struct, so a mark visits thePointerfirst. The GC could then free a field of the struct while the class still held the struct. When an actor sent such a class to another actor, the runtime didn't count the struct's field for the receiver, so the sender freed the field while the receiver still held it. You need aPointermade through FFI to get here; a program with no FFI isn't affected. A related problem with aPointerinto an actor's data stays as it is on main (see Follow-up issues).#6696 already fixed #6695, "GC segfaults after another actor acquires an embedded struct", on main. I added regression tests for it, which is why this PR closes it.
Closes #6693
Closes #6694
Closes #6695
How it works
The struct that loses its fields comes down to two types at one address. The compiler now identifies those types as routed. From
pony.h:The compiler emits routed traces (
pony_traceknown_routed) for routed types and plain traces (pony_traceknown) for every other type. The runtime handles a plain trace on the same fast path as main. When a struct's first field is embedded and has a non-zero size, the runtime allocates the struct with a "shared start" bit set. If the runtime visits that allocation through a routed trace as the field at the start, it doesn't mark the allocation. It marks the allocation only on a visit as the struct's own type (ponyint_heap_mark_routed), so a visit as the field can no longer cut off the trace of the struct's other fields.For the cycles through embedded objects, the fix differs between the heap mark and the message trace. In the heap mark, the runtime records the (address, type) pairs it has already traced and stops when it comes back around to one. It doesn't trace an embedded field of the actor running the mark as a separate object, because it traces the actor's embedded fields in place when it traces the actor. In the message trace, the runtime records each address in its object map and traces that address again only for an enclosing type: a type that contains the one already traced at that address, such as the struct after its first field.
For the actor that got collected while another actor held its embedded field, the runtime now finds the owning actor when it traces the field and counts that actor as referenced. To do that, the compiler puts the type descriptors in a section of their own. It also puts its constants in another section; from
sections.h, that section holds "the objects the compiler emits as constants: string literals, constant array literals and primitive instances". The runtime reads the bounds of both sections at start-up:gc/pony_sections.ld).For an object with no heap chunk, the runtime tries three places in order to find the owning actor: the actor the pagemap records for the address, then the actor running the trace (for its own fields), then the pool block that holds the object. For the pool block, the runtime checks whether the word at the block's start points into the type descriptor section. That's how it recognizes an actor there (
ponyint_actor_in_pool, throughponyint_pool_base). The pagemap holds the actors the pool lookup can't find.The lookup doesn't need the constant section. Without it, the pool lookup still finds no actor for a constant, and the GC traces the constant as main does. The constant section is a speed-up that uses the same machinery as the descriptor section (the linker script, the bounds on each platform, the read at start-up), and I included it in this PR. The GC skips any object it finds in the constant section. From
sections.h: "Nothing writes an object there at run time, so the GC traces neither the object nor what it points to."The
Pointerto the start of a struct got its fix in how the compiler emits aPointer's trace. Fromgentrace.c:Divergences from main
-ddidn't. The cost is about 32 bytes per duplicated literal per package.--static --nopicprograms, because a static musl binary gets no RELRO. Elsewhere these sections are read-only after relocation. I've accepted the writable sections in static musl programs.Known costs
.textis about 15.7 KB larger. A small program's.textgoes from 186,274 to 201,986 bytes (+8.4%)..bssholds a new region map for the pool: 128 KB of virtual memory on 64-bit targets, 8 bytes on 32-bit. The map is a bitmap with one bit per pool region, so the runtime can tell whether an address is in a region the pool created without walking the region list. The actor lookup above uses it.The routed trace and the checks for objects with no heap chunk are on the mark path, so I ran an A/B to check that programs that gain nothing from this change don't slow down. It ran on perf-tester (AMD Ryzen 9 5950X): b184c0a against its base, main at 7e46db9. b184c0a has the same changes as this PR, on that main rather than the newer main this PR is on. Both are release builds built the same way, pinned to one CPU with one scheduler thread. Numbers are medians of 5 alternating 60-second rounds, in collections per second unless noted.
valarrays of 100 Strings owned by another actorvaltree back and forth (messages/s)The first A/B ran on the branch rebased onto 7e46db9, before the two changes below, against 7e46db9. It had markvalroot 5.1% slower. Hardware counters showed the same instruction count with 3.9% more cycles. The cause was the fix for the
Pointerto the start of a struct. With that fix, the runtime tracesPointerfields with no type and as opaque. That mademarkobjectbig enough that the compiler keptmutabilityon the stack, with 14 reloads on the hot path. On every untyped visit, the objectmap'sgetorputalso stored NULL over NULL. I made two changes to the runtime: I moved the path ofmarkobjectfor objects with no heap chunk into a separate non-inlined function, and I madegetorputskip the store when the type is NULL. With those two changes, markvalroot went from -5.1% in the first A/B to +0.7% in the final one.Speed-up
Because the GC skips objects in the constant section, an actor that holds a lot of references to string literals and other compiler constants collects much faster. Same A/B run as above:
The memory high-water mark on both dropped from about 17.2 MB to 11.5 MB.
Testing
The change is covered by 31 new full programs and 83 new C++ unit tests, plus new cases in the noalloc-link, output-file-links and link-handoff tests. A new runtime test program,
test/libponyrt/noscript, is linked without the linker script and checks that the bounds stay 0.The branch is one commit on main. On Linux x86-64, with the branch on main at e412ae8, the build succeeds and nothing fails. ci-core in debug codegen passes 19/19. stdlib, full-programs and inherit-programs pass in release codegen. The tool suites pass 7/7, and the ci-weekly mutation tests pass 11/11.
I ran the macOS and Windows tests on the old base, 35075af:
Since those runs, I rebased the branch three times and made the two runtime changes from the markvalroot fix under Known costs. The last rebase came after the Linux runs. It put the branch on main at a484f69, which updates the vendored LLVM to 23.1.7. Only the release notes conflicted, and the branch's source, test and cmake files are unchanged from the commit I tested on Linux. I haven't rebuilt LLVM 23.1.7 locally, and I haven't run macOS or Windows on this PR's code. PR CI covers both platforms and the branch on LLVM 23.1.7.
On an ASan + UBSan debug build on perf-tester at b184c0a (the A/B commit), 219 libponyrt.tests pass. 33 embed, gc-pointer and constant programs, each in both codegen modes, pass all 66 runs with no sanitizer report. That build used the memalign pool, because
pool.hrefuses ASan with the arena pool. So I haven't run the arena's region map or the lookup through the type descriptor section under the sanitizers.For 32-bit, GCC on armhf (Raspberry Pi) compiled the runtime; I skipped the rest of the Pi run. An i386 GCC container compiled every runtime file and every changed compiler and test file with the project's warning flags. I didn't run tier 3.
Follow-up issues
I'll file two issues once this PR is open and link them here:
Pointerinto an actor's data, such as one to an embedded field made through FFI, doesn't keep the actor alive and can dangle, as on main. I've left that as it is in this PR: