Skip to content

Fix GC bugs with embedded fields and stop tracing constants - #7085

Open
SeanTAllen wants to merge 1 commit into
mainfrom
embed-gc-timers
Open

SeanTAllen wants to merge 1 commit into
mainfrom
embed-gc-timers

Conversation

@SeanTAllen

Copy link
Copy Markdown
Member

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 Pointer to 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 val field 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 val field 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 Pointer to the start of a struct could get the struct's fields freed. A class can hold a Pointer to a struct's start in a field declared before the field that holds the struct, so a mark visits the Pointer first. 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 a Pointer made through FFI to get here; a program with no FFI isn't affected. A related problem with a Pointer into 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:

A routed type is one whose objects can share an address with an object of another type: a type embedded at the start of a struct, a struct that starts with an embedded field, a type with no size that is embedded somewhere (such a field has its holder's address), or a class or struct that embeds a field with no size. Whether a type is routed depends on the whole program it is compiled into, …

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:

  • ELF: ponyc passes a linker script to the linker (gc/pony_sections.ld).
  • Mach-O: the runtime uses section bound symbols.
  • Windows: the runtime uses marker objects placed at the start and end of the sections.

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, through ponyint_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 Pointer to the start of a struct got its fix in how the compiler emits a Pointer's trace. From gentrace.c:

A Pointer can point at the start of an object of another type, such as a struct, and has no trace function. A Pointer is traced as opaque and with no type. A non-opaque visit would mark that object's allocation as traced without tracing its fields, and a visit with a type would leave the Pointer's type in the object map as the type of the object at that address.

Divergences from main

  • Identical constant array literals are no longer merged into one object. The compiler now puts constant array literals and string literals in ponyc's constant section, and LLVM and the linker don't merge identical ones there.
  • For the same reason, string literals in different packages are no longer one object. On main, fat LTO merged them; thin LTO and -d didn't. The cost is about 32 bytes per duplicated literal per package.
  • With musl, both new sections stay writable in --static --nopic programs, 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.
  • An embedded struct with no size now has the address of the object that holds it. Before, it got the address where it would sit in its holder, which for a field at the end is one past the end of the object and can be the address of a different object. The new address is no worse than main's, but Pony promises no particular address for an embedded struct with no size. I'll file an issue about giving it storage of its own (see Follow-up issues).

Known costs

  • Every program's .text is about 15.7 KB larger. A small program's .text goes from 186,274 to 201,986 bytes (+8.4%).
  • .bss holds 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.
  • In an ELF program linked by hand without ponyc's linker script, the bounds of both sections stay 0. The program still works, but the GC traces constants the way main does, and the runtime records each actor type with an embedded field in the pagemap and gives each such actor at least 1 KB. The runtime always takes that pagemap path with the memalign pool.
  • The GC runs two more instructions on each visit to a constant. On a benchmark that sends only literals, that's +3.2% instructions with the same wall time.
  • Compiling the stdlib tests went from 211.9 s to 212.9 s (+0.5%, mean of 3 alternating compiles), and the binary is 575 KB (+0.49%) larger.

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.

Benchmark What it does main branch Change
markplain class objects pointing to class objects, no embedded fields 326.2 329.4 +1.0%
markstructs structs pointing to class objects, no embedded fields 322.7 329.8 +2.2%
markvalroot an actor holding 2,000 val arrays of 100 Strings owned by another actor 40.22 40.50 +0.7%
msgobjects two actors passing a val tree back and forth (messages/s) 124,563 125,858 +1.0%

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 Pointer to the start of a struct. With that fix, the runtime traces Pointer fields with no type and as opaque. That made markobject big enough that the compiler kept mutability on the stack, with 14 reloads on the hot path. On every untyped visit, the objectmap's getorput also stored NULL over NULL. I made two changes to the runtime: I moved the path of markobject for objects with no heap chunk into a separate non-inlined function, and I made getorput skip 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:

Benchmark What it does main branch Change
marklits an actor holding many references to string literals 193.5 904.0 +367%
marklitsstr the same, in a program where an actor embeds a String, so the GC must also check whether an actor holds each string it visits 197.2 840.4 +326%

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:

  • macOS (Apple silicon): libponyrt.tests 262, libponyc.tests 2681 and 64 program runs pass.
  • Windows: libponyrt.tests 243, libponyc.tests 2661 and 64 program runs pass.

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.h refuses 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:

  • A Pointer into 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:
  • Giving an embedded struct with no size storage of its own:

@ponylang-main ponylang-main added the discuss during sync Should be discussed during an upcoming sync label Dec 4, 2026
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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

discuss during sync Should be discussed during an upcoming sync

Projects

None yet

2 participants