From 1a0d7ebf30eb5c4f656e34bb843818d94d5ceb33 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 26 Nov 2026 07:15:24 +0000 Subject: [PATCH 1/7] checker: refuse a tuple-returning call passed as a scalar argument A call that returns several values, written straight into an argument whose parameter takes one value (`takes(list.get(xs, 0))`, `spec.expect_str(pair())`, or as a UFCS receiver, `pair().takes()`), passed the checker and failed in the C compiler: "passing '_tuple_ptr_string' to parameter of incompatible type 'const char *'". reject_tuple_argument already refused a NAME bound to the tuple, but left a call to "the callee's own rules", and nothing else looked. A multi-value return is never spread over parameters (`f(pair())` for a two-parameter `f` is an arity error), so the call form is the same mistake. It is now E0200 at the argument, naming the call and the fix (destructure it first), for user functions, module functions and externs alike. A `fn`/wildcard parameter is left alone. Test: tuple_argument_reject gains the call, module-call and UFCS cases, and a destructured list.get_string that still runs; the new cases fail on main (aetherc accepted the program). Co-Authored-By: Claude Opus 5.5 --- compiler/analysis/typechecker.c | 28 ++++++++-- ...10Z-fixed-tuple-call-as-scalar-argument.md | 8 +++ .../test_tuple_argument_reject.sh | 55 ++++++++++++++++++- 3 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 new_changelogs/20261126T071510Z-fixed-tuple-call-as-scalar-argument.md diff --git a/compiler/analysis/typechecker.c b/compiler/analysis/typechecker.c index 59d5230d6..8ebd899ab 100644 --- a/compiler/analysis/typechecker.c +++ b/compiler/analysis/typechecker.c @@ -3273,15 +3273,33 @@ static void reject_tuple_argument(SymbolTable* table, ASTNode* call, ASTNode* ar param_type->kind == TYPE_TUPLE || param_type->kind == TYPE_UNKNOWN) { return; } - /* Only a NAME bound to a tuple. A function passed as a value - * (`via_fn(pair, 2, 3)`) infers to its return type, and a call in - * argument position is typed by the callee's own rules. */ + if (param_type->kind == TYPE_WILDCARD) return; + char spelled[256]; + char emsg[512]; + /* A call that returns several values, written straight into a scalar + * argument (`takes(list.get(xs, 0))`, `spec.expect_str(pair())`). A + * multi-value return is never spread over the parameters, so this is the + * same mistake as the bound name below, and it went through to the C + * compiler the same way ("passing '_tuple_ptr_string' to parameter of + * incompatible type 'const char *'"). */ + if (arg->type == AST_FUNCTION_CALL) { + tuple_type_spell(arg_type, spelled, sizeof(spelled)); + snprintf(emsg, sizeof(emsg), + "Argument %d '%s' of '%s': expected %s, got the %s returned by " + "'%s' — a multi-value return is not spread over parameters; " + "destructure it first: `value, err = %s(...)`", + index, param_name ? param_name : "?", call->value ? call->value : "?", + type_name(param_type), spelled, arg->value ? arg->value : "?", + arg->value ? arg->value : "f"); + type_error(emsg, arg->line, arg->column); + return; + } + /* Otherwise only a NAME bound to a tuple. A function passed as a value + * (`via_fn(pair, 2, 3)`) infers to its return type. */ if (arg->type != AST_IDENTIFIER || !arg->value) return; Symbol* sym = lookup_symbol(table, arg->value); if (!sym || sym->is_function) return; - char spelled[256]; tuple_type_spell(arg_type, spelled, sizeof(spelled)); - char emsg[512]; snprintf(emsg, sizeof(emsg), "Argument %d '%s' of '%s': expected %s, got the %s tuple bound to " "'%s' — a multi-value return bound to one name stays a tuple; " diff --git a/new_changelogs/20261126T071510Z-fixed-tuple-call-as-scalar-argument.md b/new_changelogs/20261126T071510Z-fixed-tuple-call-as-scalar-argument.md new file mode 100644 index 000000000..eead33153 --- /dev/null +++ b/new_changelogs/20261126T071510Z-fixed-tuple-call-as-scalar-argument.md @@ -0,0 +1,8 @@ +- **A call that returns several values, passed where a parameter takes one, + is the checker's error.** `takes(list.get(xs, 0))` or + `spec.expect_str(pair())`, with the multi-value call written straight into + the argument, passed `ae check` and failed in the C compiler ("passing + '_tuple_ptr_string' to parameter of incompatible type 'const char *'"). + The same mistake through a name bound to the tuple was already refused; the + call form, including a UFCS receiver (`pair().takes()`), now is too (E0200), + naming the call and the fix: destructure it first. diff --git a/tests/integration/tuple_argument_reject/test_tuple_argument_reject.sh b/tests/integration/tuple_argument_reject/test_tuple_argument_reject.sh index f4bb98bb3..8237c0d15 100644 --- a/tests/integration/tuple_argument_reject/test_tuple_argument_reject.sh +++ b/tests/integration/tuple_argument_reject/test_tuple_argument_reject.sh @@ -4,7 +4,8 @@ # for argument 1 … argument is of type '_tuple_ptr_string'") instead of an # Aether diagnostic. The front end now refuses it at the call, for user # functions and externs alike, and names the fix (destructure). The -# destructured form still compiles and runs. +# destructured form still compiles and runs. A tuple-returning call written +# straight into the argument (or as a UFCS receiver) is refused the same way. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" @@ -55,15 +56,63 @@ if printf '%s' "$out" | grep -q "_tuple_"; then fail=1 fi +# The same mistake with the call written straight into the argument, no +# name in between: a user function, a module function reached through its +# qualified name, and a method-call (UFCS) receiver. Each used to reach the C +# compiler as "passing '_tuple_ptr_string' to parameter of incompatible type". +cat > "$tmp/call.ae" <<'AE' +import std.list +import std.spec + +pair() -> { + return "a", "" +} +takes_str(s: string) -> int { return 1 } + +main() { + xs = list.new() + list.add(xs, "x") + println("${takes_str(pair())}") + spec.expect_str(list.get(xs, 0)) + pair().takes_str() +} +AE +out="$(AETHER_HOME="$ROOT" "$AETHERC" "$tmp/call.ae" "$tmp/call.c" 2>&1)" +if ! printf '%s' "$out" | grep -q "Argument 1 's' of 'takes_str': expected string, got the (string, string) returned by 'pair'"; then + echo " [FAIL] tuple_argument_reject: a tuple-returning call passed to a user function was not refused" + printf '%s\n' "$out" | head -4 | sed 's/^/ /' + fail=1 +fi +if ! printf '%s' "$out" | grep -q "Argument 1 'v' of 'spec.expect_str': expected string, got the (ptr, string) returned by 'list.get'"; then + echo " [FAIL] tuple_argument_reject: a tuple-returning call passed to a module function was not refused" + printf '%s\n' "$out" | head -4 | sed 's/^/ /' + fail=1 +fi +n="$(printf '%s' "$out" | grep -c "returned by 'pair'")" +if [ "$n" != 2 ]; then + echo " [FAIL] tuple_argument_reject: a tuple-returning UFCS receiver was not refused ($n of 2 reports)" + fail=1 +fi +if printf '%s' "$out" | grep -q "_tuple_"; then + echo " [FAIL] tuple_argument_reject: a generated-C type name leaked into the call report" + fail=1 +fi + cat > "$tmp/good.ae" <<'AE' +import std.list + pair() -> { return 41, "x" } takes_int(n: int) -> int { return n + 1 } +takes_str(s: string) -> int { return 0 } main() { v, _ = pair() - println("${takes_int(v)}") + xs = list.new() + list.add(xs, "x") + s, _ = list.get_string(xs, 0) + println("${takes_int(v) + takes_str(s)}") } AE got="$(AETHER_HOME="$ROOT" "$AE" run "$tmp/good.ae" 2>&1 | tail -1)" @@ -73,6 +122,6 @@ if [ "$got" != "42" ]; then fi if [ "$fail" = 0 ]; then - echo " [PASS] tuple_argument_reject: a tuple-bound name passed as a scalar is refused with the destructuring named" + echo " [PASS] tuple_argument_reject: a tuple, bound to a name or returned by a call, passed as a scalar is refused with the destructuring named" fi exit $fail From a6a32d77b739cd16c88eb69a3c52b06f9b294244 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 26 Nov 2026 07:16:41 +0000 Subject: [PATCH 2/7] checker: a multi-value return must match the result's count check_return_values let a multi-value `return` through whenever its count differed from the result type's (`rt->tuple_count != ret->child_count`, or a result that is not a tuple at all), and codegen then built the value in the shape of the first `return`: pick(ok: int) -> { if ok == 0 { return 0, "" } return 41, "", 3 // third value dropped, C warning only } `return 0` followed by `return 41, 1` returned a tuple from an int function (a C compiler error); `return 0, 0, 0` followed by `return 41, 1` zero-filled the last value. The value-type side of a differently shaped early return (an early `return 0, 0, 0` ahead of a return of 64-bit values) was already the narrowing error (#2782); the count side was not checked at all. Now E0200 at the `return`: "'pick' returns (int, string) (inferred from its first `return`), but this `return` gives 3 values", with a hint to give every `return` the same number of values or write the result type. The same for a written result type. Test: return_type_check gains one-then-two, two-then-three, three-then-two and a written (int, string) returning three; all four were accepted on main. Co-Authored-By: Claude Opus 5.5 --- compiler/analysis/typechecker.c | 36 ++++++++++ ...261126T071641Z-fixed-return-value-count.md | 10 +++ .../test_return_type_check.sh | 70 +++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 new_changelogs/20261126T071641Z-fixed-return-value-count.md diff --git a/compiler/analysis/typechecker.c b/compiler/analysis/typechecker.c index 8ebd899ab..fe1c4e152 100644 --- a/compiler/analysis/typechecker.c +++ b/compiler/analysis/typechecker.c @@ -14935,6 +14935,42 @@ static int check_return_values(ASTNode* ret, Type* rt, const ReturnOwner* own, return check_return_value(ret->children[0], rt->tuple_types[0], &at, table); } if (ret->child_count > 1) { + /* An optional tuple result (`-> (int, int)?`) takes the values as + * its payload; its count is the tuple's. */ + Type* shape = rt; + if (rt->kind == TYPE_OPTIONAL && rt->element_type && + rt->element_type->kind == TYPE_TUPLE) + shape = rt->element_type; + int shape_known = shape->kind == TYPE_TUPLE || shape->kind == TYPE_STRING || + shape->kind == TYPE_PTR || shape->kind == TYPE_BOOL || + shape->kind == TYPE_BYTE || shape->kind == TYPE_FLOAT || + shape->kind == TYPE_FLOAT32 || shape->kind == TYPE_STRUCT || + is_integer_scalar(shape->kind); + if (shape != rt && shape->tuple_count == ret->child_count) return 1; + if (shape_known && + (shape->kind != TYPE_TUPLE || shape->tuple_count != ret->child_count)) { + /* A multi-value `return` whose count is not the result's: one + * value first and then two (`return 0` ... `return v, 1`), or + * two and then three. It was let through, and codegen built + * the result from the first `return`'s shape: a tuple into a + * scalar result reached the C compiler, and a longer tuple + * than the result lost its extra values (only a C warning, + * "excess elements in struct initializer") with no word. */ + char want[200]; + const char* msg = aether_internf( + "Type mismatch in return: %s%s%s returns %s%s, but this `return` gives %d values", + own->fn_name ? "'" : "", own->fn_name ? own->fn_name : "this closure", + own->fn_name ? "'" : "", ret_type_spell(rt, want, sizeof(want)), + own->inferred ? " (inferred from its first `return`)" : "", + ret->child_count); + type_error_hint(msg, + own->inferred + ? "give every `return` the same number of values, or write the " + "result type the function means (`-> (T, U)`)" + : "return as many values as the result type has", + ret->line, ret->column); + return 0; + } if (rt->kind != TYPE_TUPLE || rt->tuple_count != ret->child_count) return 1; int ok = 1; for (int i = 0; i < ret->child_count; i++) { diff --git a/new_changelogs/20261126T071641Z-fixed-return-value-count.md b/new_changelogs/20261126T071641Z-fixed-return-value-count.md new file mode 100644 index 000000000..bd8823161 --- /dev/null +++ b/new_changelogs/20261126T071641Z-fixed-return-value-count.md @@ -0,0 +1,10 @@ +- **A multi-value `return` with a different count than the function's result + is the checker's error.** A function whose first `return` gives one value + and a later one two (`return 0` ... `return v, 1`), or two and then three, + or three and then two, was let through and lowered to the first + `return`'s shape: a tuple returned into an int result reached the C + compiler, a longer tuple silently lost its extra values (a C "excess + elements" warning only), and a shorter one left the rest zero. Each is now + E0200 at the `return`, naming the result type and how many values the + `return` gives; the same for a written result type (`-> (int, string)` + returning three values). diff --git a/tests/integration/return_type_check/test_return_type_check.sh b/tests/integration/return_type_check/test_return_type_check.sh index 5e6ce30f8..0a725f4c4 100644 --- a/tests/integration/return_type_check/test_return_type_check.sh +++ b/tests/integration/return_type_check/test_return_type_check.sh @@ -314,6 +314,76 @@ main() { AE refused tuple_early_return 5:12 "narrowing return from 'capacity': its result type was inferred as int from its first \`return\`, but this \`return\` gives a 64-bit value" +# A multi-value `return` with another count than the result: one value +# first and then two, or two and then three, or three and then two. They +# were let through and lowered to the first `return`'s shape: a tuple into +# an int result reached the C compiler, the extra value of a longer tuple +# was dropped (a C warning only) and a shorter one zero-filled the rest. +cat > "$TMP/one_then_two.ae" <<'AE' +pick(ok: int) -> { + if ok == 0 { return 0 } + return 41, 1 +} + +main() { + x = pick(1) + println("${x}") +} +AE +refused one_then_two 3:5 "'pick' returns int (inferred from its first \`return\`), but this \`return\` gives 2 values" + +cat > "$TMP/two_then_three.ae" <<'AE' +pick(ok: int) -> { + if ok == 0 { return 0, "" } + return 41, "", 3 +} + +main() { + x, e = pick(1) + println("${x} ${e}") +} +AE +refused two_then_three 3:5 "'pick' returns (int, string) (inferred from its first \`return\`), but this \`return\` gives 3 values" + +cat > "$TMP/three_then_two.ae" <<'AE' +pick(ok: int) -> { + if ok == 0 { return 0, 0, 0 } + return 41, 1 +} + +main() { + a, b, c = pick(1) + println("${a} ${b} ${c}") +} +AE +refused three_then_two 3:5 "'pick' returns (int, int, int) (inferred from its first \`return\`), but this \`return\` gives 2 values" + +cat > "$TMP/written_count.ae" <<'AE' +pick() -> (int, string) { + return 41, "", 3 +} + +main() { + x, e = pick() + println("${x} ${e}") +} +AE +refused written_count 2:5 "'pick' returns (int, string), but this \`return\` gives 3 values" + +# An optional tuple result counts its tuple's values; `return n, n + 1` +# into `-> (int, int)?` is tests/regression/test_optional_tuple_return.ae. +cat > "$TMP/optional_count.ae" <<'AE' +pick(n: int) -> (int, int)? { + if n > 0 { return n, n, n } + return none +} + +main() { + println("${pick(1) == none}") +} +AE +refused optional_count 2:16 "'pick' returns (int, int)?, but this \`return\` gives 3 values" + if [ "$fails" -ne 0 ]; then echo " [FAIL] return_type_check: $fails case(s)" exit 1 From a380286a6e0c4f0b6fdb4d7192116a63e680ce93 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 26 Nov 2026 07:16:54 +0000 Subject: [PATCH 3/7] std.os: check os.which candidates and os.getcwd against the sandbox os_which checked `env PATH` and then access(2)'d each `/` on it unchecked, so code in a sandbox with no file-system grant learned where any program on PATH was installed. os_getcwd_raw checked nothing and gave the path of a directory the caller may not read. Every other std call that tells what is at a path is an fs_read (fs.exists, file_stat, realpath, ...; docs/containment-sandbox.md). Each candidate os.which tries (POSIX: the name with a slash, `./name` for an empty PATH entry, `/name`; Windows: every PATH and PATHEXT combination) is now an fs_read of that path, a refused one skipped as if absent. os.getcwd checks fs_read on the directory getcwd(3) gives and returns "" when refused. The containment doc lists both. Behaviour change for sandboxed code: os.which needs fs_read on the directory that holds the program (grant_fs_read("/usr/bin/*")), not only `env PATH`. Test: tests/regression/test_os_which_getcwd_sandbox.ae, which on main fails with "os.which found '/bin/sh' with no fs_read grant". Co-Authored-By: Claude Opus 5.5 --- docs/containment-sandbox.md | 4 +- ...6T071654Z-fixed-os-which-getcwd-sandbox.md | 9 +++ std/os/aether_os.c | 31 +++++++-- .../test_os_which_getcwd_sandbox.ae | 64 +++++++++++++++++++ 4 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 new_changelogs/20261126T071654Z-fixed-os-which-getcwd-sandbox.md create mode 100644 tests/regression/test_os_which_getcwd_sandbox.ae diff --git a/docs/containment-sandbox.md b/docs/containment-sandbox.md index ec792c4fc..68317a071 100644 --- a/docs/containment-sandbox.md +++ b/docs/containment-sandbox.md @@ -403,7 +403,9 @@ it reveals or changes, on the path as it resolves: `mtime`, `is_symlink`, `is_socket`, `readlink`, `realpath`, `statvfs`), which are refused as reads: a stat tells whether something is there, which is what a grant list hides. A refused metadata call reads as absent (0) or - as an error. **A listing is a read of the directory listed**: + as an error. `os.which` checks each candidate it looks at on PATH (a + refused one is skipped, as if it were not there), and `os.getcwd` the + working directory (refused, it gives ""). **A listing is a read of the directory listed**: `fs.list_dir(d)` needs a grant matching `d` itself (`"/data"` or `"/data*"`; `"/data/*"` covers what is in it, not the directory). `fs.glob` checks the directory it starts from, refusing the glob when that is diff --git a/new_changelogs/20261126T071654Z-fixed-os-which-getcwd-sandbox.md b/new_changelogs/20261126T071654Z-fixed-os-which-getcwd-sandbox.md new file mode 100644 index 000000000..79f8a42fe --- /dev/null +++ b/new_changelogs/20261126T071654Z-fixed-os-which-getcwd-sandbox.md @@ -0,0 +1,9 @@ +- **`os.which` and `os.getcwd` are checked against the sandbox.** `os.which` + checked only `env PATH` and then looked at every candidate on it + unchecked, so a sandboxed caller with no file-system grant could learn + where any program was installed; `os.getcwd` checked nothing. Each + candidate `os.which` looks at is now an `fs_read` of its path (a refused + one reads as absent and the search goes on), and `os.getcwd` needs + `fs_read` on the working directory (refused, it gives ""), as + `fs.exists` and `fs.realpath` do. A sandbox that runs a program found + with `os.which` now grants `fs_read` on its directory as well. diff --git a/std/os/aether_os.c b/std/os/aether_os.c index cee4fe361..450bcbe8c 100644 --- a/std/os/aether_os.c +++ b/std/os/aether_os.c @@ -1380,6 +1380,17 @@ int os_execv(const char* prog, void* argv_list) { // that's executable. Return the first hit. // // Caller owns the returned string. +// +// Each candidate is a metadata read of its path (whether an executable is +// there), so it is checked under fs_read as fs.exists is: a candidate the +// sandbox refuses reads as absent, and the search goes on to the next PATH +// entry. Before, only PATH itself was checked, and os.which told a +// sandboxed caller what was installed anywhere on it. +#ifndef _WIN32 +static int which_may_see(const char* candidate) { + return aether_sandbox_check("fs_read", candidate); +} +#endif char* os_which(const char* name) { if (!name || !*name) return NULL; if (!aether_sandbox_check("env", "PATH")) return NULL; @@ -1414,7 +1425,8 @@ char* os_which(const char* name) { // Helper: try a candidate path; return a counted copy of it if the file exists. // Inlined via macro since we use it in both the has-path and PATH-search branches. #define WIN_WHICH_TRY(candidate) do { \ - DWORD attrs = GetFileAttributesA(candidate); \ + DWORD attrs = aether_sandbox_check("fs_read", candidate) \ + ? GetFileAttributesA(candidate) : INVALID_FILE_ATTRIBUTES; \ if (attrs != INVALID_FILE_ATTRIBUTES && !(attrs & FILE_ATTRIBUTE_DIRECTORY)) { \ return (char*)string_new(candidate); \ } \ @@ -1488,7 +1500,7 @@ char* os_which(const char* name) { return NULL; #else if (strchr(name, '/')) { - if (access(name, X_OK) == 0) return (char*)string_new(name); + if (which_may_see(name) && access(name, X_OK) == 0) return (char*)string_new(name); return NULL; } @@ -1511,13 +1523,13 @@ char* os_which(const char* name) { buf[0] = '.'; buf[1] = '/'; memcpy(buf + 2, name, name_len + 1); - if (access(buf, X_OK) == 0) return (char*)string_new(buf); + if (which_may_see(buf) && access(buf, X_OK) == 0) return (char*)string_new(buf); } } else if (dirlen + 1 + name_len < sizeof(buf)) { memcpy(buf, p, dirlen); buf[dirlen] = '/'; memcpy(buf + dirlen + 1, name, name_len + 1); - if (access(buf, X_OK) == 0) return (char*)string_new(buf); + if (which_may_see(buf) && access(buf, X_OK) == 0) return (char*)string_new(buf); } if (!end) break; p = end + 1; @@ -1573,13 +1585,22 @@ int os_chdir_raw(const char* path) { char* os_getcwd_raw(void) { size_t cap = 4096; for (;;) { + /* The directory's path is checked under fs_read, as fs.realpath(".") + * is: a sandbox that may not read the working directory is not told + * where it is. It was not checked at all. */ /* #462: gate the cwd buffer (a deep path is plugin-influenced). * Adopted into the returned string on success; the ERANGE retry * frees through the cap before doubling, so the free size matches * the live `cap`. */ char* buf = (char*)aether_caps_malloc(cap); if (!buf) return NULL; - if (getcwd(buf, cap)) return (char*)string_adopt_caps_buffer(buf, strlen(buf), cap); + if (getcwd(buf, cap)) { + if (!aether_sandbox_check("fs_read", buf)) { + aether_caps_free(buf, cap); + return NULL; + } + return (char*)string_adopt_caps_buffer(buf, strlen(buf), cap); + } aether_caps_free(buf, cap); if (errno != ERANGE) return NULL; if (cap > (size_t)1 << 20) return NULL; /* 1 MiB sanity ceiling */ diff --git a/tests/regression/test_os_which_getcwd_sandbox.ae b/tests/regression/test_os_which_getcwd_sandbox.ae new file mode 100644 index 000000000..b020f39a2 --- /dev/null +++ b/tests/regression/test_os_which_getcwd_sandbox.ae @@ -0,0 +1,64 @@ +// Regression: os.which and os.getcwd are checked against the sandbox, as +// every other std call that tells what is at a path is (fs_read). +// +// os.which checked only `env PATH`, then stat'ed each candidate unchecked, +// so a sandboxed caller with no file-system grant learned where any program +// on PATH was installed. os.getcwd checked nothing, and told the caller the +// path of a directory it was not allowed to read. Now a candidate the +// sandbox refuses reads as absent (the search goes on), and getcwd gives "" +// unless the working directory may be read. With the grants, both give what +// they give outside a sandbox. + +import std.os +import std.path +import std.sandbox + +extern exit(code: int) + +fn fail(msg: string) { + println("FAIL: ${msg}") + exit(1) +} + +main() { + prog = "sh" + found = os.which(prog) + if found == "" { + prog = "cmd" + found = os.which(prog) + } + if found == "" { fail("no sh or cmd on PATH to look up") } + cwd = os.getcwd() + if cwd == "" { fail("os.getcwd gave nothing outside a sandbox") } + bin = path.dirname(found) + + no_fs = sandbox.new("no-fs") { + grant_env("PATH") + grant_env("PATHEXT") + } + w1 = "unset" + c1 = "unset" + sandbox.enforce(no_fs) callback { + w1 = os.which(prog) + c1 = os.getcwd() + } + if w1 != "" { fail("os.which found '${w1}' with no fs_read grant") } + if c1 != "" { fail("os.getcwd told '${c1}' with no fs_read grant") } + + with_fs = sandbox.new("with-fs") { + grant_env("PATH") + grant_env("PATHEXT") + grant_fs_read("${bin}/*") + grant_fs_read(cwd) + } + w2 = "" + c2 = "" + sandbox.enforce(with_fs) callback { + w2 = os.which(prog) + c2 = os.getcwd() + } + if w2 != found { fail("os.which gave '${w2}' with ${bin} readable, not '${found}'") } + if c2 != cwd { fail("os.getcwd gave '${c2}' with the directory readable, not '${cwd}'") } + + println("PASS: os.which and os.getcwd follow the fs_read grants") +} From e828732a7a6b355100eeeb30a6c210aee80f2fd7 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 26 Nov 2026 07:17:24 +0000 Subject: [PATCH 4/7] tests: tcp_deadline_cancel accepts a decoy dial the broken canceller hit The demonstration run (BROKEN_CANCELLER=1, a canceller that closes the connection's descriptor by number) must show the race check catching it, and grepped only for "a decoy was disturbed". On a loaded host the broken close lands on a decoy while it is still dialling, the number already the decoy's, and the round fails as "decoy dial: [connect failed]" first: the check caught it, and the test reported that it had not ("the race check did not catch a canceller that closes by number"). Seen in a 12-way parallel `make test-ae` on macOS; it passes alone. Both forms now count as caught in the demonstration run. The plain run, where any decoy failure is a real one, is unchanged. Co-Authored-By: Claude Opus 5.5 --- .../tcp_deadline_cancel/test_tcp_deadline_cancel.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/integration/tcp_deadline_cancel/test_tcp_deadline_cancel.sh b/tests/integration/tcp_deadline_cancel/test_tcp_deadline_cancel.sh index 605188e2b..271422872 100755 --- a/tests/integration/tcp_deadline_cancel/test_tcp_deadline_cancel.sh +++ b/tests/integration/tcp_deadline_cancel/test_tcp_deadline_cancel.sh @@ -37,7 +37,13 @@ case "$broken_demo:$(uname -s)" in 0:*|*:MINGW*|*:MSYS*|*:CYGWIN*) ;; *) BROKEN_CANCELLER=1 RACE_ROUNDS=400 "$TMP/deadline" >"$TMP/broken" 2>&1 - if grep -q '^FAIL expiry and cancel racing close.*a decoy was disturbed' "$TMP/broken"; then + # The broken canceller's close lands on a decoy either once it is + # connected (its write or read is disturbed) or while it is still + # dialling, when the number is already the decoy's and the dial + # fails ("decoy dial: [connect failed]"). Both are the check + # catching it; which one a run hits is timing, and a loaded host + # hits the second. + if grep -q -E '^FAIL expiry and cancel racing close.*(a decoy was disturbed|decoy dial: )' "$TMP/broken"; then echo " ok: a canceller that closes by number is caught ($(grep -c . "$TMP/broken") lines)" else echo " FAIL: the race check did not catch a canceller that closes by number" From 68cd10e5912d5941801dff7c281e2c25f2690910 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 26 Nov 2026 07:53:24 +0000 Subject: [PATCH 5/7] tests: external_psk lets openssl s_server pick its own port The s_server cases computed a port from the shell's pid and the case name and retried on a bind failure. In a parallel `make test-ae` on FreeBSD one case (-ciphersuites TLS_CHACHA20_POLY1305_SHA256) failed with the client's "connect failed" while s_server had printed ACCEPT and saw no connection; alone it passes. A computed port can be one another test is using: s_server binds the wildcard with SO_REUSEADDR, which FreeBSD lets coexist with another socket's 127.0.0.1 bind of the same port, so the client can reach the other listener instead. s_server now gets `-accept 0`, and the port is read from its "ACCEPT 0.0.0.0:" line: the kernel's choice, free by construction. Checked on macOS (OpenSSL 4.1) and FreeBSD (3.5). Co-Authored-By: Claude Opus 5.5 --- .../crypto_tls13_external_psk/test_external_psk.sh | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/integration/crypto_tls13_external_psk/test_external_psk.sh b/tests/integration/crypto_tls13_external_psk/test_external_psk.sh index 47f99ede5..d18b9979b 100755 --- a/tests/integration/crypto_tls13_external_psk/test_external_psk.sh +++ b/tests/integration/crypto_tls13_external_psk/test_external_psk.sh @@ -217,16 +217,21 @@ if command -v openssl >/dev/null 2>&1 && openssl s_server -help 2>&1 | grep -q - sc_want="$3" shift 3 SP="" + # Port 0: the kernel picks a free port and s_server prints it + # ("ACCEPT 0.0.0.0:54321"). A port computed here could be one another + # test held on 127.0.0.1, which s_server's wildcard bind (it sets + # SO_REUSEADDR) does not refuse on FreeBSD: the client then reached + # the other listener and the case failed as "connect failed". for try in 1 2 3 4 5; do - P=$(( 20000 + ($$ * 7 + try * 1009 + $(printf '%s' "$sc_name" | wc -c)) % 40000 )) - openssl s_server -4 -www -accept "$P" -naccept 1 "$@" >"$TMP/s.log" 2>&1 "$TMP/s.log" 2>&1 /dev/null; do sleep 0.1; w=$((w + 1)); done if server_alive "$spid" && grep -q ACCEPT "$TMP/s.log" 2>/dev/null; then - SP=$P - break + SP=$(sed -n 's/^ACCEPT .*:\([0-9][0-9]*\)[^0-9]*$/\1/p' "$TMP/s.log" | head -1) + [ -n "$SP" ] && [ "$SP" != 0 ] && break + SP="" fi kill_server "$spid" done From b1854d9e55004126d0deee07e2be05254c498c70 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 26 Nov 2026 12:43:17 +0000 Subject: [PATCH 6/7] std.os: getcwd checked under fs_read on Windows too; test frees its sandboxes The Windows os_getcwd_raw is a function of its own and had not been given the fs_read check the POSIX one got in this batch, so a sandbox with no fs_read grant was still told the working directory (Windows/UCRT64, MSYS2, and Wine: "os.getcwd told 'Z:\...' with no fs_read grant"). test_os_which_getcwd_sandbox never freed its two grant lists; the env-only one was reported (LSan exit 23, macOS leaks: 6 leaks, 192 bytes). Checked: macOS leaks 6 -> 0; the Windows build under Wine fails as CI did before and passes after. Co-Authored-By: Claude Opus 5.5 --- std/os/aether_os.c | 6 ++++++ tests/regression/test_os_which_getcwd_sandbox.ae | 2 ++ 2 files changed, 8 insertions(+) diff --git a/std/os/aether_os.c b/std/os/aether_os.c index 450bcbe8c..d73cbaa72 100644 --- a/std/os/aether_os.c +++ b/std/os/aether_os.c @@ -5018,6 +5018,12 @@ char* os_getcwd_raw(void) { out = NULL; } aether_caps_free(w, w_bytes); + /* As the POSIX one: the directory's path is checked under fs_read, and + * a sandbox that may not read the working directory is not told it. */ + if (out && !aether_sandbox_check("fs_read", out->data)) { + string_release(out); + out = NULL; + } return (char*)out; } diff --git a/tests/regression/test_os_which_getcwd_sandbox.ae b/tests/regression/test_os_which_getcwd_sandbox.ae index b020f39a2..5c84ee6ed 100644 --- a/tests/regression/test_os_which_getcwd_sandbox.ae +++ b/tests/regression/test_os_which_getcwd_sandbox.ae @@ -57,6 +57,8 @@ main() { w2 = os.which(prog) c2 = os.getcwd() } + sandbox.free(no_fs) + sandbox.free(with_fs) if w2 != found { fail("os.which gave '${w2}' with ${bin} readable, not '${found}'") } if c2 != cwd { fail("os.getcwd gave '${c2}' with the directory readable, not '${cwd}'") } From 02b47ec4035910c4f82bf68adc616892366cf6b0 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 26 Nov 2026 19:34:55 +0000 Subject: [PATCH 7/7] tests: http_server_slow_clients per-IP round waits for the server to see the close The per-IP round holds two connections against a limit of two, closes the first, slept a fixed 300 ms, and then expected a fourth connection to be served. It failed now and then on the Windows / GCC (MSYS2) job ("after one closed 503"), on main as well as on PRs. The server is not wrong. The address's slot is given back in conn_close, on a worker: the parking lot's poller sees the client's FIN, hands the connection to a pool worker, the worker reads the end and closes. That is two thread hand-offs, and the server never promised them within any time. Measured on the Windows 11 lab VM with the probe run 8 at a time beside 16 busy loops (a loaded runner), the time from the client's close to the server's close was median 47-62 ms, p99 250-282 ms, max 344 and 516 ms: past 300 ms now and then, which is exactly the 503 CI saw. A fourth connection arriving before then is correctly refused. So the round now waits for the observable condition instead of a sleep: on a worker-path server, the on_conn_close hook (which runs after the address's count has gone down); on the proxy event loop, which has no such hook, its live connection count down to the one still held. The wait is bounded at 5 s and the round fails if the close is never seen, so a server that stopped giving slots back still fails it. The note reports how long the close took to be seen. Windows 11 VM, SLOW_PROBE_ONLY=perip under that load: before, 4 of 1200 per-IP rounds failed (0.33%); after, 0 of 1200. Unloaded, 0 of 200 either way. The whole test passes on Windows 11, macOS and FreeBSD 15.1 (ghostbsd), the last two including the event-loop round. Co-Authored-By: Claude Opus 5.5 --- .../http_server_slow_clients/shim.c | 60 ++++++++++++++++--- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/tests/integration/http_server_slow_clients/shim.c b/tests/integration/http_server_slow_clients/shim.c index be8d7ed58..04ac1fa43 100644 --- a/tests/integration/http_server_slow_clients/shim.c +++ b/tests/integration/http_server_slow_clients/shim.c @@ -864,11 +864,39 @@ static client_sock client_connect6(int port) { return s; } +/* Connections a worker-path server has closed, as its on_conn_close hook + * hears them: the hook runs once the address's count has gone down. */ +typedef struct { volatile long closed; } ConnCloses; + +static void conn_closes_hook(int64_t conn_id, void* ud) { + (void)conn_id; +#ifdef _WIN32 + InterlockedIncrement(&((ConnCloses*)ud)->closed); +#else + __atomic_add_fetch(&((ConnCloses*)ud)->closed, 1, __ATOMIC_SEQ_CST); +#endif +} + +static long conn_closes_count(ConnCloses* c) { +#ifdef _WIN32 + return InterlockedCompareExchange(&c->closed, 0, 0); +#else + return __atomic_load_n(&c->closed, __ATOMIC_SEQ_CST); +#endif +} + /* The per-address limit, 2 here, on a server bound to `host`: two * connections from the address, each asking one request and kept, then a * third answered 503; once the first closes, a fourth is served again. On - * ::1 the address counts by its /64. */ -static void round_per_ip_on(HttpServer* server, const char* label, int v6) { + * ::1 the address counts by its /64. + * + * "Once the first closes" is once the server has seen it close, which the + * client cannot know from its own close: a worker-path server says so + * through on_conn_close (`closes`), the event loop through its count of + * live connections (`closes` NULL). The round waited a fixed 300 ms instead, and on a + * loaded Windows runner the server had not always got there: a fourth 503 + * that was the test's guess, not the server's limit. */ +static void round_per_ip_on(HttpServer* server, const char* label, int v6, ConnCloses* closes) { int port = http_server_port(server); static const char req[] = "GET /ping HTTP/1.1\r\nHost: localhost\r\n\r\n"; client_sock held[2]; @@ -886,8 +914,21 @@ static void round_per_ip_on(HttpServer* server, const char* label, int v6) { third = readable(s, 3000) ? read_status(s, NULL, NULL, 0) : -1; client_close(s); } + /* The served connections the server has closed so far: none, the + * refused third never being counted. */ + long closed_before = closes ? conn_closes_count(closes) : 0; if (held[0] != CLIENT_BAD) client_close(held[0]); - sleep_ms(300); /* the server saw it go */ + /* The server saw it go: its close heard, or the loop down to the one + * connection still held. Bounded, so a server that never notices fails + * the round below rather than hanging it. */ + int64_t t_close = now_ms(); + int seen = 0; + while (now_ms() - t_close < CLIENT_WAIT_MS) { + if (closes ? conn_closes_count(closes) > closed_before + : http_server_evloop_active(server) <= 1) { seen = 1; break; } + sleep_ms(5); + } + int64_t close_ms = now_ms() - t_close; s = v6 ? client_connect6(port) : client_connect(port); int fourth = -1; if (s != CLIENT_BAD) { @@ -896,9 +937,10 @@ static void round_per_ip_on(HttpServer* server, const char* label, int v6) { client_close(s); } if (held[1] != CLIENT_BAD) client_close(held[1]); - char note[96]; - snprintf(note, sizeof(note), "%d served, a third %d, after one closed %d", served, third, fourth); - report(label, served == 2 && third == 503 && fourth == 200, 0, third, note); + char note[128]; + snprintf(note, sizeof(note), "%d served, a third %d, after one closed (seen %s %lld ms) %d", + served, third, seen ? "after" : "NOT after", (long long)close_ms, fourth); + report(label, served == 2 && third == 503 && seen && fourth == 200, 0, third, note); } static void round_per_ip(int v6) { @@ -911,6 +953,8 @@ static void round_per_ip(int v6) { if (!*err && *http_server_set_max_connections_per_ip_raw(server, 2, 129) == '\0') err = "a 129 bit prefix was accepted"; if (*err) { report(label, 0, -1, -1, err); http_server_free(server); return; } + ConnCloses closes = { 0 }; + http_server_on_conn_close(server, conn_closes_hook, &closes); int bound = v6 ? http_server_bind_raw(server, "::1", 0) : http_server_bind_raw(server, "127.0.0.1", 0); if (bound < 0 || http_server_start_background_raw(server) < 0) { @@ -919,7 +963,7 @@ static void round_per_ip(int v6) { http_server_free(server); return; } - round_per_ip_on(server, label, v6); + round_per_ip_on(server, label, v6, &closes); http_server_free(server); } @@ -931,7 +975,7 @@ static void round_loop_per_ip(void) { Gateway g = loop_gateway(label, 0, 0); g_loop_per_ip = 0; if (!g.server) return; - round_per_ip_on(g.server, label, 0); + round_per_ip_on(g.server, label, 0, NULL); on_loop(g, label); loop_gateway_free(g); }