From f273a7de20969768d0afab442715204f8f1a1250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Istv=C3=A1n=20B=C3=ADr=C3=B3?= Date: Wed, 25 Nov 2026 00:00:00 +0000 Subject: [PATCH 1/7] Build for WASI --- builtin.c | 2 +- io.c | 4 ++-- main.c | 3 +++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/builtin.c b/builtin.c index 57576a9c6..568aa2655 100644 --- a/builtin.c +++ b/builtin.c @@ -3381,7 +3381,7 @@ do_mkbool(int nargs) int gawk_system(const char *command) { -#if defined(VMS) || defined(__MINGW32__) +#if defined(VMS) || defined(__MINGW32__) || defined(__wasi__) return system(command); #else /* ! (defined(VMS) || defined(__MINGW32__)) */ pid_t childpid; diff --git a/io.c b/io.c index b48f14ead..0116f1466 100644 --- a/io.c +++ b/io.c @@ -4702,7 +4702,7 @@ save_pipe(int fd, FILE *fp, pid_t pid) static FILE * gawk_popen_write(const char *cmd) { -#if defined(VMS) || defined(__MINGW32__) +#if defined(VMS) || defined(__MINGW32__) || defined(__wasi__) return popen(cmd, binmode("w")); #else pid_t childpid; @@ -4763,7 +4763,7 @@ gawk_popen_write(const char *cmd) static int gawk_popen_write_close(FILE *fp) { -#if defined(VMS) || defined(__MINGW32__) +#if defined(VMS) || defined(__MINGW32__) || defined(__wasi__) return pclose(fp); #else write_pipe *wp; diff --git a/main.c b/main.c index d8f7750d1..9ab2f8c8d 100644 --- a/main.c +++ b/main.c @@ -1611,6 +1611,9 @@ parse_args(int argc, char **argv) break; case 'D': +#ifdef __wasi__ + fatal(_("-D/--debug: the debugger is not supported in this build")); +#endif do_flags |= DO_DEBUG; if (optarg != NULL && optarg[0] != '\0') command_file = optarg; From 17cec9040b017135b3c3c29d24a14f86ccc70ed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Istv=C3=A1n=20B=C3=ADr=C3=B3?= Date: Wed, 25 Nov 2026 00:00:00 +0000 Subject: [PATCH 2/7] Check integer format range before casting --- printf.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/printf.c b/printf.c index 02646928c..b9af7716b 100644 --- a/printf.c +++ b/printf.c @@ -925,9 +925,11 @@ format_integer_digits(NODE *arg, struct flags *flags, bool *used_float) // octal or hex or unsigned decimal chbuf = (flags->format == 'X' ? Uchbuf : lchbuf); + /* Converting an out-of-range double is undefined, so check the range first. */ if (tmpval < 0) { - uval = (uintmax_t) (intmax_t) tmpval; - if ((AWKNUM)(intmax_t)uval != double_to_int(tmpval)) { + if (tmpval < -0x1p63 + || (uval = (uintmax_t) (intmax_t) tmpval, + (AWKNUM)(intmax_t)uval != double_to_int(tmpval))) { flags->format = 'g'; free((void *) buf); *used_float = true; @@ -935,8 +937,9 @@ format_integer_digits(NODE *arg, struct flags *flags, bool *used_float) return format_float(arg, flags); } } else { - uval = (uintmax_t) tmpval; - if ((AWKNUM)uval != double_to_int(tmpval)) { + if (tmpval >= 0x1p64 + || (uval = (uintmax_t) tmpval, + (AWKNUM)uval != double_to_int(tmpval))) { flags->format = 'g'; free((void *) buf); *used_float = true; From a67551d7665affa76bfe2d84dc69502a9205ea2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Istv=C3=A1n=20B=C3=ADr=C3=B3?= Date: Wed, 25 Nov 2026 00:00:00 +0000 Subject: [PATCH 3/7] Two way pipes and pipe status for WASI --- io.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/io.c b/io.c index 0116f1466..c7ad55f42 100644 --- a/io.c +++ b/io.c @@ -2525,6 +2525,31 @@ two_way_open(const char *str, struct redirect *rp, int extfd) return true; } +#elif defined(__wasi__) + /* case 4: two way pipe to a command started as a Golem tool call */ + { + int rfd, wfd; + + if (golem_gawk_coprocess(str, & rfd, & wfd) != 0) + return false; + rp->pid = -1; + rp->iop = iop_alloc(rfd, str, 0); + find_input_parser(rp->iop); + iop_finish(rp->iop); + rp->ifp = fdopen(rfd, binmode("r")); + rp->output.fp = fdopen(wfd, binmode("w")); + if (! rp->iop->valid || rp->ifp == NULL || rp->output.fp == NULL) { + if (rp->iop->valid && rp->iop->errcode != 0) + update_ERRNO_int(rp->iop->errcode); + iop_close(rp->iop); + rp->iop = NULL; + return false; + } + rp->output.mode = "w"; + rp->output.name = str; + find_output_wrapper(& rp->output); + return true; + } #else /*PIPES_SIMULATED*/ fatal(_("`|&' not supported")); @@ -2824,6 +2849,11 @@ gawk_pclose(struct redirect *rp) rp->iop = NULL; aval = pclose(rp->ifp); rp->ifp = NULL; +#ifdef __wasi__ + /* the callers expect a sanitized status, as from the real-pipe version */ + if (aval >= 0) + aval = sanitize_exit_status(aval); +#endif return (rval < 0 ? rval : aval); } From b7aa7a27b96fd10c4e68c597e549e83f6e9d4844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Istv=C3=A1n=20B=C3=ADr=C3=B3?= Date: Wed, 25 Nov 2026 00:00:00 +0000 Subject: [PATCH 4/7] Refuse inet special files on WASI --- io.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/io.c b/io.c index c7ad55f42..628b111ba 100644 --- a/io.c +++ b/io.c @@ -1839,6 +1839,18 @@ devopen_simple(const char *name, const char *mode, bool try_real_open) return openfd; } +#ifdef __wasi__ +/* refused_inet_name --- true for a /inet special file name; this build has no network access */ + +static bool +refused_inet_name(const char *name) +{ + return strncmp(name, "/inet/", 6) == 0 + || strncmp(name, "/inet4/", 7) == 0 + || strncmp(name, "/inet6/", 7) == 0; +} +#endif + /* devopen --- handle /dev/std{in,out,err}, /dev/fd/N, /inet, regular files */ /* @@ -1862,6 +1874,11 @@ devopen(const char *name, const char *mode) if (do_traditional) { goto strictopen; +#ifdef __wasi__ + } else if (refused_inet_name(name)) { + errno = EAFNOSUPPORT; + return INVALID_HANDLE; +#endif } else if (inetfile(name, strlen(name), & isi)) { #ifdef HAVE_SOCKETS #define DEFAULT_RETRIES 20 @@ -2530,6 +2547,11 @@ two_way_open(const char *str, struct redirect *rp, int extfd) { int rfd, wfd; + if (refused_inet_name(str)) { + errno = EAFNOSUPPORT; + return false; + } + if (golem_gawk_coprocess(str, & rfd, & wfd) != 0) return false; rp->pid = -1; From 2c9f7ef139fce150677f8dd2f0f9b98c31b65576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Istv=C3=A1n=20B=C3=ADr=C3=B3?= Date: Wed, 25 Nov 2026 00:00:00 +0000 Subject: [PATCH 5/7] Guard pclose of closed two way read end on WASI --- io.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/io.c b/io.c index 628b111ba..6815540eb 100644 --- a/io.c +++ b/io.c @@ -2862,7 +2862,16 @@ gawk_popen(const char *cmd, struct redirect *rp) static int gawk_pclose(struct redirect *rp) { +#ifdef __wasi__ + int rval = 0, aval, fd; + + /* the read end of a two-way pipe can be closed before close(cmd) closes the rest */ + if (rp->iop == NULL && rp->ifp == NULL) + return rp->status; + fd = rp->iop != NULL ? rp->iop->public.fd : INVALID_HANDLE; +#else int rval, aval, fd = rp->iop->public.fd; +#endif if (rp->iop != NULL) { rp->iop->public.fd = dup(fd); /* kludge to allow close() + pclose() */ @@ -2875,6 +2884,7 @@ gawk_pclose(struct redirect *rp) /* the callers expect a sanitized status, as from the real-pipe version */ if (aval >= 0) aval = sanitize_exit_status(aval); + rp->status = (rval < 0 ? rval : aval); #endif return (rval < 0 ? rval : aval); } From 6d26eda6710799303c22c24335bd34a02e6b3385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Istv=C3=A1n=20B=C3=ADr=C3=B3?= Date: Wed, 25 Nov 2026 00:00:00 +0000 Subject: [PATCH 6/7] Refuse -M and --persist on WASI --- main.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main.c b/main.c index 9ab2f8c8d..fc3232522 100644 --- a/main.c +++ b/main.c @@ -1708,6 +1708,9 @@ parse_args(int argc, char **argv) break; case 'M': +#ifdef __wasi__ + fatal(_("-M/--bignum: arbitrary precision arithmetic is not supported in this build")); +#endif #ifdef HAVE_MPFR do_flags |= DO_MPFR; #else @@ -1734,6 +1737,9 @@ parse_args(int argc, char **argv) break; case 'T': // --persist[=file] +#ifdef __wasi__ + fatal(_("--persist: persistent memory is not supported in this build")); +#endif #ifdef USE_PERSISTENT_MALLOC if (optarg == NULL) optarg = "/some/file"; From bd9b55e4e729508b629d5799c2055f39b2a9b17c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Istv=C3=A1n=20B=C3=ADr=C3=B3?= Date: Tue, 1 Dec 2026 11:17:57 +0000 Subject: [PATCH 7/7] Close a failed two way pipe once on WASI --- io.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/io.c b/io.c index 6815540eb..50d93f6e7 100644 --- a/io.c +++ b/io.c @@ -2558,13 +2558,31 @@ two_way_open(const char *str, struct redirect *rp, int extfd) rp->iop = iop_alloc(rfd, str, 0); find_input_parser(rp->iop); iop_finish(rp->iop); - rp->ifp = fdopen(rfd, binmode("r")); - rp->output.fp = fdopen(wfd, binmode("w")); - if (! rp->iop->valid || rp->ifp == NULL || rp->output.fp == NULL) { - if (rp->iop->valid && rp->iop->errcode != 0) - update_ERRNO_int(rp->iop->errcode); - iop_close(rp->iop); + rp->ifp = NULL; + rp->output.fp = NULL; + if (rp->iop->valid) { + rp->ifp = fdopen(rfd, binmode("r")); + if (rp->ifp != NULL) + rp->output.fp = fdopen(wfd, binmode("w")); + } + if (rp->ifp == NULL || rp->output.fp == NULL) { + /* + * The command starts only at the first read or the close, so nothing + * has run: close each descriptor once and keep the setup's errno. + * rp->ifp owns rfd when it exists; otherwise iop_close closes rfd + * unless iop_finish already did. + */ + int save_errno = rp->iop->valid ? errno : rp->iop->errcode; + + if (rp->ifp != NULL) { + (void) fclose(rp->ifp); + rp->ifp = NULL; + rp->iop->public.fd = INVALID_HANDLE; + } + (void) iop_close(rp->iop); rp->iop = NULL; + (void) close(wfd); + errno = save_errno; return false; } rp->output.mode = "w";