Skip to content

Commit

Permalink
WASM handles stdout and stderr (at last). (AztecProtocol/barretenberg…
Browse files Browse the repository at this point in the history
  • Loading branch information
charlielye authored Jun 12, 2023
1 parent 8e38ca3 commit 9610ecf
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
11 changes: 10 additions & 1 deletion circuits/cpp/barretenberg/cpp/src/barretenberg/common/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@ WASM_EXPORT void test_abort()

#endif

WASM_EXPORT void test_stdout_stderr()
{
fprintf(stdout, "c: hello stdout!");
fflush(stdout);
fprintf(stderr, "c: hello stderr!");
std::cout << "c++: hello stdout!" << std::flush;
std::cerr << "c++: hello stderr!";
}

WASM_EXPORT void common_init_slab_allocator(uint32_t const* circuit_size)
{
barretenberg::init_slab_allocator(ntohl(*circuit_size));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@

WASM_EXPORT void test_threads(uint32_t const* threads, uint32_t const* iterations, uint32_t* out);

WASM_EXPORT void test_thread_abort();

WASM_EXPORT void test_abort();

WASM_EXPORT void common_init_slab_allocator(uint32_t const* circuit_size);
WASM_EXPORT void common_init_slab_allocator(uint32_t const* circuit_size);
33 changes: 27 additions & 6 deletions circuits/cpp/barretenberg/cpp/src/barretenberg/wasi/wasi_stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstdint>
#include <cstdlib>
#include <barretenberg/common/log.hpp>
#include <string.h>

extern "C" {

Expand All @@ -23,10 +24,24 @@ int32_t __imported_wasi_snapshot_preview1_poll_oneoff(int32_t, int32_t, int32_t,
// abort();
// }

int32_t __imported_wasi_snapshot_preview1_fd_write(int32_t, int32_t, int32_t, int32_t)
struct iovs_struct {
char* data;
size_t len;
};

int32_t __imported_wasi_snapshot_preview1_fd_write(int32_t fd, iovs_struct* iovs_ptr, size_t iovs_len, size_t* ret_ptr)
{
info("fd_write not implemented.");
abort();
if (fd != 1 && fd != 2) {
info("fd_write to unsupported file descriptor: ", fd);
abort();
}
std::string str;
for (size_t i = 0; i < iovs_len; ++i) {
auto iovs = iovs_ptr[i];
str += std::string(iovs.data, iovs.len);
}
logstr(str.c_str());
*ret_ptr = str.length();
return 0;
}

Expand Down Expand Up @@ -65,10 +80,16 @@ int32_t __imported_wasi_snapshot_preview1_environ_sizes_get(int32_t, int32_t)
// return 0;
// }

int32_t __imported_wasi_snapshot_preview1_fd_fdstat_get(int32_t, int32_t)
int32_t __imported_wasi_snapshot_preview1_fd_fdstat_get(int32_t fd, void* buf)
{
info("fd_fdstat_get not implemented.");
abort();
// info("fd_fdstat_get not implemented.");
// abort();
if (fd != 1 && fd != 2) {
info("fd_fdstat_get with unsupported file descriptor: ", fd);
abort();
}
memset(buf, 0, 20);
*(uint8_t*)buf = (uint8_t)fd;
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ describe('barretenberg wasm', () => {
it('test abort', () => {
expect(() => wasm.call('test_abort')).toThrow();
});

it('test c/c++ stdout/stderr', () => {
// We're checking we don't crash, but you can manually confirm you see log lines handled by logstr.
wasm.call('test_stdout_stderr');
});
});

describe('barretenberg wasm worker', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class BarretenbergWasm {
view.setBigUint64(out, ts, true);
},
proc_exit: () => {
this.logger('HUNG: proc_exit was called. This is caused by unstable experimental wasi pthreads. Try again.');
this.logger('PANIC: proc_exit was called. This is maybe caused by "joining" with unstable wasi pthreads.');
this.logger(new Error().stack!);
killSelf();
},
Expand Down

0 comments on commit 9610ecf

Please sign in to comment.