Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/glibc/assert/__libc_assert_fail.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,30 @@
#include <array_length.h>
#include <intprops.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef WASM_DEBUG_LIBC_FATAL
// TODO: For some reason, glibc's libc_assert_fail is not printing the error message when a fatal error is triggered.
// As a temporary workaround, we manually forward the message to Wasmtime and let Wasmtime handle the printing.
// Ideally, we should fix the issue and restore proper handling via glibc's native libc_assert_fail in the future.
void __imported__libc_assert_fail(const char *assertion, const char *file, unsigned int line, const char *function) __attribute__((
__import_module__("debug"),
__import_name__("libc_assert_fail")
));
#endif

void
__libc_assert_fail (const char *assertion, const char *file, unsigned int line,
const char *function)
{
char linebuf[INT_BUFSIZE_BOUND (unsigned int)];
array_end (linebuf)[-1] = '\0';
char *linestr = _itoa_word (line, array_end (linebuf) - 1, 10, 0);
__libc_message ("Fatal glibc error: %s:%s (%s): assertion failed: %s\n",
file, linestr, function, assertion);
}
#ifdef WASM_DEBUG_LIBC_FATAL
__imported__libc_assert_fail(assertion, file, line, function);
abort();
#else
char linebuf[INT_BUFSIZE_BOUND (unsigned int)];
array_end (linebuf)[-1] = '\0';
char *linestr = _itoa_word (line, array_end (linebuf) - 1, 10, 0);
__libc_message ("Fatal glibc error: %s:%s (%s): assertion failed: %s\n",
file, linestr, function, assertion);
#endif
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line

17 changes: 15 additions & 2 deletions src/glibc/malloc/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5766,15 +5766,28 @@ libc_hidden_def (__libc_mallopt)

extern char **__libc_argv attribute_hidden;

#ifdef WASM_DEBUG_MALLOC_ERR
// TODO: malloc_printerr is currently unable to print messages to the console correctly.
// This is a temporary workaround to ensure that error messages are printed.
void __imported__malloc_printerr(const char *str) __attribute__((
__import_module__("debug"),
__import_name__("malloc_printerr")
));
#endif

static void
malloc_printerr (const char *str)
{
#ifdef WASM_DEBUG_MALLOC_ERR
__imported__malloc_printerr(str);
__builtin_unreachable ();
#else
#if IS_IN (libc)
__libc_message ("%s\n", str);
#else
__libc_fatal (str);
#endif
__builtin_unreachable ();
#endif
}

#if IS_IN (libc)
Expand Down Expand Up @@ -6047,4 +6060,4 @@ compat_symbol (libc, __libc_free, cfree, GLIBC_2_0);
* Local variables:
* c-basic-offset: 2
* End:
*/
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line

27 changes: 27 additions & 0 deletions src/wasmtime/crates/lind-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,33 @@ pub fn add_to_linker<
wasmtime_lind_multi_process::signal::signal_handler(&mut caller);
},
)?;

// a temporary solution to have libc_assert_fail correctly working
linker.func_wrap(
"debug",
"libc_assert_fail",
move |mut caller: Caller<'_, T>, assertion: i32, file: i32, line: i32, function: i32| {
let mem_base = get_memory_base(&caller);
let assertion = rawposix::interface::get_cstr(mem_base + assertion as u64).unwrap();
let file = rawposix::interface::get_cstr(mem_base + file as u64).unwrap();
let function = rawposix::interface::get_cstr(mem_base + function as u64).unwrap();
eprintln!(
"Fatal glibc error: {}:{} ({}): assertion failed: {}\n",
assertion, file, line, function
);
},
)?;

// a temporary solution to have malloc_printerr correctly working
linker.func_wrap(
"debug",
"malloc_printerr",
move |mut caller: Caller<'_, T>, msg: i32| {
let mem_base = get_memory_base(&caller);
let msg = rawposix::interface::get_cstr(mem_base + msg as u64).unwrap();
eprintln!("malloc_printerr: {}", msg);
},
)?;

Ok(())
}
Loading