Skip to content

Commit a3ad1d6

Browse files
committed
Make sure we use cabi_realloc() in favor of a malloc()-based realloc if the former is provided.
This allows TinyGo programs to take control over reallocation if they desire, elevating an explicit API over the heuristic identification of `malloc()`. It turns out it was already doing this, through the call to `realloc_to_import_into_adapter()`, which returns an (even more specific) `cabi_realloc_adapter()` if there is one and otherwise `cabi_realloc`.
1 parent a5eb511 commit a3ad1d6

File tree

1 file changed

+4
-3
lines changed
  • crates/wit-component/src/encoding

1 file changed

+4
-3
lines changed

crates/wit-component/src/encoding/world.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,11 @@ impl<'a> ComponentWorld<'a> {
8888
/// returns the realloc scheme we should use.
8989
fn fallback_realloc_scheme(&self) -> ReallocScheme {
9090
if self.encoder.module_is_produced_by_tiny_go {
91-
// TODO: Also check for cabi_realloc and bail if it's there.
9291
// If it appears the module was emitted by TinyGo, we delegate to
9392
// its `malloc()` function. (TinyGo assumes its GC has rein over the
9493
// whole memory and quickly overwrites the adapter's
95-
// `memory.grow`-allocated State struct unless we inform TinyGo's GC
96-
// of the memory we use.)
94+
// `memory.grow`-allocated State struct, causing a crash. So we use
95+
// `malloc()` to inform TinyGo's GC of the memory we use.)
9796
ReallocScheme::Malloc("malloc")
9897
} else {
9998
// If it's not TinyGo, use `memory.grow` instead.
@@ -182,6 +181,8 @@ impl<'a> ComponentWorld<'a> {
182181
}
183182

184183
let realloc = if self.encoder.realloc_via_memory_grow {
184+
// User explicitly requested memory-grow-based realloc. We
185+
// give them that unless it would definitely crash.
185186
self.fallback_realloc_scheme()
186187
} else {
187188
match self.info.exports.realloc_to_import_into_adapter() {

0 commit comments

Comments
 (0)