Async exports never free the block of indirect parameters
Maintainer antworten meist innerhalb von 1 Tag
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 30/100
Rechercherichtung
Start in crates/core/src/abi.rs at LiftArgsLowerResults, where the GuestDeallocate for indirect parameters is emitted only under sig.indirect_params && !async_; that guard is the source of the leak. Then look at the Rust generator's CallInterface, which the issue proposes should free the block between creating the future and the first .await, and note that C, C# and MoonBit need equivalent frees in their own task-return or async-method code. A linked pull request (#1758) is already open against this, so read it before writing anything. Done looks like the counting-allocator reproduction (nine string parameters, 10 calls, no growth) passing for every generator, including cancelled calls.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Summary
When the parameters of an export flatten to more than 16 core values, the caller writes them into a block of the callee's linear memory. The caller allocates this block with the callee's cabi_realloc, and the callee must free it.
For sync exports, the generated code frees the block after the call. For async exports, the generated code never frees it. Each call of such an async export leaks the block.
Where
crates/core/src/abi.rs, in LiftArgsLowerResults, emits the free only for sync exports:
if sig.indirect_params && !async_ {
// GetArg 0, GuestDeallocate { size, align }
}
No generator frees the block in its own code for async exports.
Effect
- Rust, C, C# and MoonBit: async exports with indirect parameters leak the block on each call. (C++ generates no async exports. I did not check Go.)
- Example: an async export with nine
stringparameters leaks 72 bytes per call. - In Golem, each method call of a Rust agent goes through an async export with such a block. Each call leaks 136 bytes, and linear memory does not shrink.
Why a change in abi.rs alone does not fix it
- A free before the call is too early. The C generator reads the parameters only at the call. The Rust generator reads
optionandresultpayloads only at the call. Both then read freed memory. - A free after the call (remove
&& !async_) is too late, or it is in the wrong function:- Rust: the free comes after
.await. A call that is cancelled while it waits never reaches the free. - C, C# and MoonBit: the code after an async call goes into a different function: the task-return function in C and MoonBit, and the async method in C#. The block pointer is not available there. For C, clang reports
use of undeclared identifier 'arg'.
- Rust: the free comes after
So each generator must free the block at a safe point in its own code: after it reads all parameters, and before the first point where the task can wait.
Possible fix (Rust)
In the Rust generator, CallInterface can free the block between the creation of the future and the first .await:
let call = T_::method(args);
cabi_dealloc(arg0, size, align);
call.await
The creation of the future reads all arguments out of the block, so the free comes after the last read. The free also comes before the first point where the task can wait, so a cancelled call frees the block too. wit-bindgen-core does not change.
I tried this change with a runtime test. The test component counts its live heap bytes. The runner calls each async export 10 times and checks that the count does not change:
- 9 strings, calls that finish: today's code leaks 720 bytes (10 × 72). With this change, nothing leaks.
- An
option<u32>, aresult<u32, string>and 6 strings: a free before the call reads freed memory and traps. With this change, the calls finish and nothing leaks. - A
futureand 8 strings, calls that the runner cancels while they wait: a free after.awaitleaks 680 bytes (10 × 68). With this change, nothing leaks.
This fixes only Rust. C, C# and MoonBit need their own changes.
Reproduction
package my:test;
interface i {
many-arguments: async func(
a: string, b: string, c: string, d: string, e: string,
f: string, g: string, h: string, i: string,
);
}
world test {
export i;
}
A test component with a counting global allocator shows 72 more live bytes after each call of many-arguments.
- Vorherrschende Sprache
- Rust
- Sterne
- 1.5k
- Forks
- 306
- Ø Merge
- 1 T. 4 Std.
- Gemergte PRs (30 T.)
- 21
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Kein Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus bytecodealliance/wit-bindgen
-
gen-rust
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 25/100
bytecodealliance/wit-bindgen#1791 ·
Maintainer antworten meist innerhalb von 1 Tag
-
gen-rust
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 22/100
bytecodealliance/wit-bindgen#1790 ·
Maintainer antworten meist innerhalb von 1 Tag
-
gen-rust
Schwierigkeit 3/5 Ein halber Tag Anfängerfreundlichkeit 55/100
bytecodealliance/wit-bindgen#1787 ·
Maintainer antworten meist innerhalb von 1 Tag
-
gen-c#
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 22/100
bytecodealliance/wit-bindgen#1759 ·
Maintainer antworten meist innerhalb von 1 Tag
-
gen-rust
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 42/100
bytecodealliance/wit-bindgen#1754 · 7 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in bytecodealliance/wit-bindgen
Ähnliche Issues
-
lane:collections todo:ticket
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
Maintainer antworten meist innerhalb von 3 Tagen
-
bugfix
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
storytold/designcraft#3662 ·
Maintainer antworten meist innerhalb von 1 Tag
-
documentation
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 88/100
Maintainer antworten meist innerhalb von 1 Tag
-
good first issue tooling
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Nixitov/Riptide-Native#521 ·
Maintainer antworten meist innerhalb von 1 Tag