diff --git a/compiler/analysis/typechecker.c b/compiler/analysis/typechecker.c index 59d5230d6..fe1c4e152 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; " @@ -14917,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/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/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/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/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..d73cbaa72 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 */ @@ -4997,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/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 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); } 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 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" 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 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..5c84ee6ed --- /dev/null +++ b/tests/regression/test_os_which_getcwd_sandbox.ae @@ -0,0 +1,66 @@ +// 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() + } + 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}'") } + + println("PASS: os.which and os.getcwd follow the fs_read grants") +}