Skip to content

Commit 67c7d74

Browse files
authored
Fixes for cabi_realloc when new_size = 0 (#465)
* gen-guest-c: Fix cabi_realloc behavior when new_size is zero Match the behavior of the Rust guest cabi_realloc by returning `align` if new_size == 0. * guest-rust: Add an assert to cabi_realloc The code generator is expected not to emit calls that might have `old_size == 0 && new_size == 0`, but if that condition is not met the resulting call to `alloc::realloc` is documented as having undefined behavior. * Add test exercising cabi_realloc new_size = 0
1 parent 26bc5dd commit 67c7d74

File tree

5 files changed

+12
-4
lines changed

5 files changed

+12
-4
lines changed

crates/gen-guest-c/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,14 +418,15 @@ impl C {
418418
// Note that these intrinsics are declared as `weak` so they can be
419419
// overridden from some other symbol.
420420
self.src.c_fns(
421-
"
422-
__attribute__((weak, export_name(\"cabi_realloc\")))
423-
void *cabi_realloc(void *ptr, size_t orig_size, size_t org_align, size_t new_size) {
421+
r#"
422+
__attribute__((weak, export_name("cabi_realloc")))
423+
void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) {
424+
if (new_size == 0) return (void*) align;
424425
void *ret = realloc(ptr, new_size);
425426
if (!ret) abort();
426427
return ret;
427428
}
428-
",
429+
"#,
429430
);
430431
}
431432
}

crates/guest-rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub mod rt {
3232
layout = Layout::from_size_align_unchecked(new_len, align);
3333
alloc::alloc(layout)
3434
} else {
35+
debug_assert_ne!(new_len, 0, "non-zero old_len requires non-zero new_len!");
3536
layout = Layout::from_size_align_unchecked(old_len, align);
3637
alloc::realloc(old_ptr, layout, new_len)
3738
};

tests/runtime/strings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ fn run() -> Result<()> {
2929

3030
fn run_test(exports: Strings, store: &mut Store<crate::Wasi<MyImports>>) -> Result<()> {
3131
exports.call_test_imports(&mut *store)?;
32+
assert_eq!(exports.call_return_empty(&mut *store)?, "");
3233
assert_eq!(exports.call_roundtrip(&mut *store, "str")?, "str");
3334
assert_eq!(
3435
exports.call_roundtrip(&mut *store, "🚀🚀🚀 𠈄𓀀")?,

tests/runtime/strings/wasm_utf16.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ void strings_test_imports() {
2525
strings_string_free(&str2);
2626
}
2727

28+
void strings_return_empty(strings_string_t *ret) {
29+
strings_string_dup(ret, u""); // Exercise cabi_realloc new_size = 0
30+
}
31+
2832
void strings_roundtrip(strings_string_t *str, strings_string_t *ret) {
2933
assert(str->len > 0);
3034
ret->len = str->len;

tests/runtime/strings/world.wit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ default world strings {
77
import imports: self.imports
88

99
export test-imports: func()
10+
export return-empty: func() -> string
1011
export roundtrip: func(s: string) -> string
1112
}

0 commit comments

Comments
 (0)