diff --git a/docs/docs/guides/ipc.md b/docs/docs/guides/ipc.md index ee2e0bf25..8a73d9da3 100644 --- a/docs/docs/guides/ipc.md +++ b/docs/docs/guides/ipc.md @@ -326,8 +326,25 @@ The IPC layer is also accessible from C, enabling you to embed Rayforce clients | `ray_ipc_send(handle, msg)` | Synchronous send: serialize `msg`, send, wait for response, return deserialized result. | | `ray_ipc_send_async(handle, msg)` | Fire-and-forget send: serialize and send `msg` without waiting for a response. | | `ray_ipc_close(handle)` | Close the connection and free the handle. | +| `ray_poll_set_restricted(poll, restricted)` | Make connections created afterward evaluate in restricted (read-only) mode. Call before servicing an embedded listener. | | `ray_ipc_listen(poll, port)` | Register a server socket on the given poll loop. Used internally by `-p` mode. | +An embedded read-only server can use only the public, pointer-based API: + +```c +ray_poll_t* poll = ray_poll_create(); +ray_poll_set_restricted(poll, true); +ray_runtime_set_poll(poll); + +ray_t* listener = ray_eval_str("(.sys.listen 7701)"); +/* Check listener for an error, then service IPC from the host loop. */ +ray_poll_run_for(poll, 200); +ray_release(listener); +``` + +The restriction applies to connections created after the setter call; +existing connections keep their original evaluation mode. + ### Example: C Client ```c diff --git a/include/rayforce.h b/include/rayforce.h index efbfffc2b..cf8aa5d37 100644 --- a/include/rayforce.h +++ b/include/rayforce.h @@ -687,6 +687,12 @@ typedef struct ray_poll ray_poll_t; ray_poll_t* ray_poll_create(void); void ray_poll_destroy(ray_poll_t* poll); +/* Mark IPC evaluations on connections subsequently created by poll as + * restricted (read-only). Existing connections keep their original mode. + * Call from the thread that owns poll before servicing the listener. + * A NULL poll is ignored. */ +void ray_poll_set_restricted(ray_poll_t* poll, bool restricted); + /* Run until ray_poll_exit is called. */ int64_t ray_poll_run(ray_poll_t* poll); diff --git a/src/app/main.c b/src/app/main.c index 501f84c8f..fc9485431 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -218,7 +218,7 @@ int main(int argc, char** argv) { pw_len = sizeof(poll->auth_secret) - 1; memcpy(poll->auth_secret, auth_pw, pw_len); poll->auth_secret[pw_len] = '\0'; - poll->restricted = auth_restricted; + ray_poll_set_restricted(poll, auth_restricted); } /* Open journal BEFORE the IPC listener accepts any connection. diff --git a/src/core/poll.c b/src/core/poll.c index 4d43bf5ed..df7eddadf 100644 --- a/src/core/poll.c +++ b/src/core/poll.c @@ -31,6 +31,11 @@ void ray_poll_exit(ray_poll_t* poll, int64_t code) if (poll) poll->code = code; } +void ray_poll_set_restricted(ray_poll_t* poll, bool restricted) +{ + if (poll) poll->restricted = restricted; +} + ray_selector_t* ray_poll_get(ray_poll_t* poll, int64_t id) { if (!poll || id < 0 || (uint32_t)id >= poll->n_sels) diff --git a/src/core/poll.h b/src/core/poll.h index 19fa57282..543d90be5 100644 --- a/src/core/poll.h +++ b/src/core/poll.h @@ -104,6 +104,7 @@ struct ray_poll { ray_poll_t* ray_poll_create(void); void ray_poll_destroy(ray_poll_t* poll); +void ray_poll_set_restricted(ray_poll_t* poll, bool restricted); int64_t ray_poll_register(ray_poll_t* poll, ray_poll_reg_t* reg); void ray_poll_deregister(ray_poll_t* poll, int64_t id); int64_t ray_poll_run(ray_poll_t* poll); diff --git a/src/ops/group.c b/src/ops/group.c index f482e08f1..629dd81fc 100644 --- a/src/ops/group.c +++ b/src/ops/group.c @@ -145,7 +145,7 @@ static inline bool sym_lex_gt(struct ray_sym_domain_s* dom, return sym_lex_lt(dom, b, a); } -/* ── Wide-element (STR/GUID) min/max/first/last ────────────────────────── +/* ── Row-gathered min/max/first/last ───────────────────────────────────── * STR (a 16-byte ray_str_t: pool pointer + length) and GUID (16 raw bytes) * do not fit the 8-byte integer reduce accumulators, so the int64 fast paths * silently truncate them to a single byte. These helpers instead track the @@ -158,6 +158,20 @@ static inline bool agg_is_wide_type(int8_t t) { return t == RAY_STR || t == RAY_GUID; } +/* Aggregates whose result must be materialised by gathering the winning + * source row instead of packing the input into an 8-byte numeric accumulator. + * + * LIST columns are boxed ray_t* cells. FIRST/LAST are well-defined for them, + * but the numeric emitters cannot construct a RAY_LIST with ray_vec_new (LIST + * is the special type tag 0) and must not reinterpret child pointers as i64. + * MIN/MAX remain limited to comparable STR/GUID elements. */ +static inline bool agg_needs_row_gather(int8_t t, uint16_t op) { + if (t == RAY_LIST) + return op == OP_FIRST || op == OP_LAST; + return agg_is_wide_type(t) && + (op == OP_MIN || op == OP_MAX || op == OP_FIRST || op == OP_LAST); +} + /* Group key/agg output columns are filled by copying ray_str_t descriptors * (by source row index). Share the source column's string pool so pooled * (>12 B) descriptors resolve against it (inline ≤12 B are self-contained). */ @@ -185,14 +199,24 @@ static int64_t wide_winner_row(ray_t* input, uint16_t op, if (op == OP_FIRST) { for (int64_t i = 0; i < scan_n; i++) { int64_t row = sel ? sel[i] : i; - if (!has_nulls || !ray_vec_is_null(input, row)) return row; + if (input->type == RAY_LIST) { + ray_t* e = ((ray_t**)ray_data(input))[row]; + if (e && !RAY_ATOM_IS_NULL(e)) return row; + } else if (!has_nulls || !ray_vec_is_null(input, row)) { + return row; + } } return -1; } if (op == OP_LAST) { for (int64_t i = scan_n - 1; i >= 0; i--) { int64_t row = sel ? sel[i] : i; - if (!has_nulls || !ray_vec_is_null(input, row)) return row; + if (input->type == RAY_LIST) { + ray_t* e = ((ray_t**)ray_data(input))[row]; + if (e && !RAY_ATOM_IS_NULL(e)) return row; + } else if (!has_nulls || !ray_vec_is_null(input, row)) { + return row; + } } return -1; } @@ -224,14 +248,21 @@ static int64_t wide_winner_row(ray_t* input, uint16_t op, } return best; } -/* Whole-table (optionally selected) min/max/first/last over a wide column. */ +/* Whole-table (optionally selected) min/max/first/last over a row-gathered + * column, including boxed LIST cells. */ static ray_t* agg_wide_reduce(ray_t* input, uint16_t op, const int64_t* sel, int64_t scan_n, bool has_nulls) { int64_t best = wide_winner_row(input, op, sel, scan_n, has_nulls); - if (best < 0) return ray_typed_null(-input->type); + if (best < 0) + return input->type == RAY_LIST + ? ray_typed_null(-RAY_I64) + : ray_typed_null(-input->type); int alloc; - return collection_elem(input, best, &alloc); + ray_t* result = collection_elem(input, best, &alloc); + /* collection_elem borrows boxed LIST cells; reductions return ownership. */ + if (result && !alloc) ray_retain(result); + return result; } /* Integer reduction loop — reads native type T, accumulates as i64. @@ -2521,12 +2552,13 @@ ray_t* ray_topk_per_group_buf(ray_t* src, /* ─── ray_wide_minmax_per_group_buf ─────────────────────────────────────── * - * Per-group min/max/first/last for wide element types (STR/GUID) that don't - * fit the 8-byte integer accumulators. Same idx_buf/offsets/grp_cnt layout - * as the median/topk kernels — produced by exec_group's group-contiguous row - * gather — but instead of a numeric quickselect it finds the winning row per - * group (lexicographic for STR, byte order for GUID; positional for - * first/last) and materialises that element into a typed result column. + * Per-group min/max/first/last for elements that cannot flow through the + * 8-byte integer accumulators. Same idx_buf/offsets/grp_cnt layout as the + * median/topk kernels — produced by exec_group's group-contiguous row gather + * — but instead of a numeric quickselect it finds the winning row per group + * (lexicographic for STR, byte order for GUID; positional for first/last) and + * materialises that element into a typed result column. LIST elements retain + * the selected boxed child into a new owning list. * * Runs SERIAL: ray_str_vec_set COW-mutates the result vector and its shared * string pool, so concurrent group writers would corrupt the pool. Wide @@ -2537,17 +2569,28 @@ ray_t* ray_wide_minmax_per_group_buf(ray_t* src, uint16_t op, const int64_t* grp_cnt, int64_t n_groups) { if (!src || RAY_IS_ERR(src) || n_groups < 0) return NULL; - if (!agg_is_wide_type(src->type)) return NULL; /* caller falls back */ + if (!agg_needs_row_gather(src->type, op)) return NULL; /* caller falls back */ bool has_nulls = (src->attrs & RAY_ATTR_HAS_NULLS) != 0; - ray_t* out = col_vec_new(src, n_groups); + ray_t* out = src->type == RAY_LIST + ? ray_list_new(n_groups) + : col_vec_new(src, n_groups); if (!out || RAY_IS_ERR(out)) return out ? out : ray_error("oom", NULL); - out->len = n_groups; + /* LIST length advances with initialized child slots so every error path + * releases exactly the children whose ownership has been retained. */ + out->len = src->type == RAY_LIST ? 0 : n_groups; for (int64_t g = 0; g < n_groups; g++) { int64_t cnt = grp_cnt[g]; int64_t off = offsets[g]; int64_t best = wide_winner_row(src, op, &idx_buf[off], cnt, has_nulls); + if (src->type == RAY_LIST) { + ray_t* e = best < 0 ? NULL : ((ray_t**)ray_data(src))[best]; + if (e) ray_retain(e); + ((ray_t**)ray_data(out))[g] = e; + out->len = g + 1; + continue; + } if (best < 0) { ray_vec_set_null(out, g, true); continue; } int alloc; ray_t* e = collection_elem(src, best, &alloc); @@ -2671,12 +2714,10 @@ ray_t* exec_reduction(ray_graph_t* g, ray_op_t* op, ray_t* input) { } } - /* Wide element types (STR/GUID) overflow the 8-byte reduce - * accumulators; resolve min/max/first/last by materialising the + /* Wide elements and boxed LIST cells cannot use the 8-byte reduce + * accumulators; resolve their supported operations by materialising the * winning row instead. COUNT keeps the generic length-based path. */ - if (agg_is_wide_type(in_type) && - (op->opcode == OP_MIN || op->opcode == OP_MAX || - op->opcode == OP_FIRST || op->opcode == OP_LAST)) { + if (agg_needs_row_gather(in_type, op->opcode)) { ray_t* r = agg_wide_reduce(input, op->opcode, sel_idx, scan_n, has_nulls); if (sel_idx_block) ray_release(sel_idx_block); return r; @@ -2974,14 +3015,12 @@ bool ght_compute_layout(ght_layout_t* out, uint32_t n_keys, uint32_t n_aggs, * packed into entries or HT rows. A post-radix pass over * row_gid+grp_cnt gathers per-group slices and runs the matching * per-group kernel. */ - /* Wide-element (STR/GUID) min/max/first/last also reserve no - * row-layout slot: the 8-byte accumulators can't hold a 16-byte - * GUID or a pooled string, so they are resolved by the same + /* Wide-element STR/GUID aggregates and boxed LIST first/last also + * reserve no row-layout slot: the 8-byte accumulators cannot hold + * their cells, so they are resolved by the same * post-radix per-group pass via ray_wide_minmax_per_group_buf. */ bool wide_mm = agg_ops && agg_vecs[a] && - agg_is_wide_type(agg_vecs[a]->type) && - (agg_ops[a] == OP_MIN || agg_ops[a] == OP_MAX || - agg_ops[a] == OP_FIRST || agg_ops[a] == OP_LAST); + agg_needs_row_gather(agg_vecs[a]->type, agg_ops[a]); bool holistic = agg_ops && (agg_ops[a] == OP_MEDIAN || agg_ops[a] == OP_QUANTILE || agg_ops[a] == OP_MODE || @@ -5200,7 +5239,12 @@ static void emit_agg_columns(ray_t** result, ray_graph_t* g, const ray_op_ext_t* out_type = is_f64 ? RAY_F64 : (agg_col ? agg_col->type : RAY_I64); break; } - ray_t* new_col = ray_vec_new(out_type, (int64_t)grp_count); + /* LIST is the boxed collection tag (0), not a ray_vec_new element + * type. The scalar no-key path fills this placeholder below via the + * row-gather override; keyed LIST aggregates never reach this emitter. */ + ray_t* new_col = out_type == RAY_LIST + ? ray_list_new((int64_t)grp_count) + : ray_vec_new(out_type, (int64_t)grp_count); if (!new_col || RAY_IS_ERR(new_col)) continue; /* SYM MIN/MAX/FIRST/LAST: the emitted values are RAW cell ids * accumulated from ONE source column — the output resolves over @@ -5208,7 +5252,7 @@ static void emit_agg_columns(ray_t** result, ray_graph_t* g, const ray_op_ext_t* if (out_type == RAY_SYM) ray_sym_vec_adopt_domain(new_col, sym_domain_rep(agg_col)); new_col->len = (int64_t)grp_count; - for (uint32_t gi = 0; gi < grp_count; gi++) { + for (uint32_t gi = 0; out_type != RAY_LIST && gi < grp_count; gi++) { size_t idx = (size_t)gi * n_aggs + a; /* nn_counts[idx] == 0 means the group is all-null for this * agg column — null-aware operators (MIN/MAX/PROD/FIRST/LAST/ @@ -9534,15 +9578,14 @@ static ray_t* exec_group_run(ray_graph_t* g, ray_op_t* op, ray_t* tbl, m->count, agg_affine, agg_prod, m->sumsq_f64, m->nn_count, NULL, NULL, NULL); - /* Wide-element (STR/GUID) min/max/first/last overflow emit_agg_columns' - * fixed-width slots (it truncated them to 1 byte above). Recompute - * those 1-row columns by materialising the winning row — mirroring + /* Row-gathered min/max/first/last cannot be represented in + * emit_agg_columns' fixed-width accumulator slots. Recompute those + * 1-row columns by materialising the winning row — mirroring * exec_reduction — and override them in the result table. */ for (uint32_t a = 0; a < n_aggs; a++) { uint16_t aop = ext->agg_ops[a]; - if (!(agg_vecs[a] && agg_is_wide_type(agg_vecs[a]->type) && - (aop == OP_MIN || aop == OP_MAX || - aop == OP_FIRST || aop == OP_LAST))) + if (!(agg_vecs[a] && + agg_needs_row_gather(agg_vecs[a]->type, aop))) continue; ray_t* wsel_blk = NULL; const int64_t* wsel = NULL; int64_t wscan = nrows; if (g->selection) { @@ -9556,11 +9599,18 @@ static ray_t* exec_group_run(ray_graph_t* g, ray_op_t* op, ray_t* tbl, bool hn = (agg_vecs[a]->attrs & RAY_ATTR_HAS_NULLS) != 0; ray_t* atom = agg_wide_reduce(agg_vecs[a], aop, wsel, wscan, hn); if (wsel_blk) ray_release(wsel_blk); - ray_t* col = col_vec_new(agg_vecs[a], 1); + ray_t* col = agg_vecs[a]->type == RAY_LIST + ? ray_list_new(1) + : col_vec_new(agg_vecs[a], 1); if (col && !RAY_IS_ERR(col)) { col->len = 1; if (RAY_ATOM_IS_NULL(atom)) { ray_vec_set_null(col, 0, true); + } else if (agg_vecs[a]->type == RAY_LIST) { + /* agg_wide_reduce returned an owned child reference; + * transfer it directly into the owning output list. */ + ((ray_t**)ray_data(col))[0] = atom; + atom = NULL; } else if (agg_vecs[a]->type == RAY_STR) { ray_t* nv = ray_str_vec_set(col, 0, ray_str_ptr(atom), ray_str_len(atom)); if (nv && !RAY_IS_ERR(nv)) col = nv; @@ -9570,7 +9620,7 @@ static ray_t* exec_group_run(ray_graph_t* g, ray_op_t* op, ray_t* tbl, ray_table_set_col_idx(result, a, col); ray_release(col); } - ray_release(atom); + if (atom) ray_release(atom); } /* Whole-table holistic aggregates have no n_keys==0 accumulator, so emit_agg_columns @@ -9690,11 +9740,10 @@ da_path:; if (aop == OP_MODE) da_eligible = false; if (aop == OP_TOP_N) da_eligible = false; if (aop == OP_BOT_N) da_eligible = false; - /* Wide-element (STR/GUID) min/max/first/last need the holistic - * post-fill; the DA emit (emit_agg_columns) would truncate them. */ - if (agg_vecs[a] && agg_is_wide_type(agg_vecs[a]->type) && - (aop == OP_MIN || aop == OP_MAX || - aop == OP_FIRST || aop == OP_LAST)) + /* Row-gathered aggregates need the holistic post-fill; the DA + * emitter cannot represent their source cells. */ + if (agg_vecs[a] && + agg_needs_row_gather(agg_vecs[a]->type, aop)) da_eligible = false; } for (uint32_t k = 0; k < n_keys && da_eligible; k++) { @@ -11645,7 +11694,9 @@ v2_emit:; out_type = is_f64 ? RAY_F64 : (agg_col ? agg_col->type : RAY_I64); break; } - ray_t* new_col = ray_vec_new(out_type, (int64_t)total_grps); + ray_t* new_col = out_type == RAY_LIST + ? ray_list_new((int64_t)total_grps) + : ray_vec_new(out_type, (int64_t)total_grps); if (!new_col || RAY_IS_ERR(new_col)) { agg_cols[a] = NULL; memset(&agg_outs[a], 0, sizeof(agg_outs[a])); @@ -12276,9 +12327,9 @@ sequential_fallback:; : (agg_col ? agg_col->type : RAY_I64); break; } ray_t* new_col; - /* Drive off the layout bitmask, not the op literal: wide-element - * (STR/GUID) min/max/first/last are holistic too and their column - * lives in med_out[a], not the truncating row-layout read below. */ + /* Drive off the layout bitmask, not the op literal: row-gathered + * STR/GUID/LIST aggregates are holistic too and their column lives + * in med_out[a], not the truncating row-layout read below. */ bool is_holistic = (ly->agg_flags[a] & GHT_AF_HOLISTIC) != 0; if (is_holistic && med_out && med_out[a] && !RAY_IS_ERR(med_out[a])) { diff --git a/test/rfl/system/part.rfl b/test/rfl/system/part.rfl index 4357729c4..9d67478d9 100644 --- a/test/rfl/system/part.rfl +++ b/test/rfl/system/part.rfl @@ -374,5 +374,24 @@ (count (at Pac 'part)) -- 4 (.sys.exec "rm -rf /tmp/rfl_part_atcol") +;; ────────────── grouped FIRST/LAST on persisted nested columns (#373) ──────── +;; LIST columns loaded from a parted table contain boxed ray_t* cells. Their +;; aggregate result must be gathered as an owned child object; sending them +;; through the fixed-width numeric emitter used to omit the column entirely +;; and shift every following output name. +(.sys.exec "rm -rf /tmp/rfl_part_nested") +(set PN-A (table [g nested c1 c2] (list [a a b] (list (dict (as 'i64 (list 0)) (as 'f64 (list 5))) (dict (as 'i64 (list 0 250000)) (as 'f64 (list 15 10))) (dict (as 'i64 (list 0)) (as 'f64 (list 7)))) (as 'i64 (list 1 2 3)) (as 'i64 (list 4 5 6))))) +(set PN-B (table [g nested c1 c2] (list [a b] (list (dict (as 'i64 (list 0)) (as 'f64 (list 20))) (dict (as 'i64 (list 0)) (as 'f64 (list 9)))) (as 'i64 (list 8 9)) (as 'i64 (list 10 11))))) +(.db.splayed.set "/tmp/rfl_part_nested/2024.01.01/t/" PN-A) +(.db.splayed.set "/tmp/rfl_part_nested/2024.01.02/t/" PN-B) +(set Pnested (.db.parted.get "/tmp/rfl_part_nested/" 't)) +(set Rnested (select {n: (last nested) c1: (last c1) c2: (last c2) from: Pnested by: g})) +(key Rnested) -- [g n c1 c2] +(at Rnested 'c1) -- [8 9] +(at Rnested 'c2) -- [10 11] +(at (at (at Rnested 'n) 0) 0) -- 20.0 +(at (at (at Rnested 'n) 1) 0) -- 9.0 +(.sys.exec "rm -rf /tmp/rfl_part_nested") + ;; ────────────── teardown ────────────── (.sys.exec "rm -rf /tmp/rfl_part_date /tmp/rfl_part_int /tmp/rfl_part_sym /tmp/rfl_part_single /tmp/rfl_part_empty /tmp/rfl_part_missing /tmp/rfl_part_three /tmp/rfl_part_cd /tmp/rfl_part_cd_sym /tmp/rfl_part_minute /tmp/rfl_part_like /tmp/rfl_part_mc /tmp/rfl_part_mb") diff --git a/test/rfl/table/select.rfl b/test/rfl/table/select.rfl index 5bfaafe7a..2b57cc332 100644 --- a/test/rfl/table/select.rfl +++ b/test/rfl/table/select.rfl @@ -198,3 +198,53 @@ (first (at aapl 'mx)) -- 151.0 (first (at aapl 'mn)) -- 149.5 (first (at aapl 'av)) -- 150.16666666666666 + +;; ── grouped FIRST/LAST preserve boxed nested columns (issue #373) +;; A LIST aggregate between scalar aggregates used to be omitted by the numeric +;; emitter; positional renaming then shifted every later column left. +(set nested-dicts (table [g nested c1 c2] (list [a a b] (list (dict (as 'i64 (list 0)) (as 'f64 (list 5))) (dict (as 'i64 (list 0 250000)) (as 'f64 (list 15 10))) (dict (as 'i64 (list 0)) (as 'f64 (list 7)))) (as 'i64 (list 1 2 3)) (as 'i64 (list 4 5 6))))) +(set nested-dicts-last (select {n: (last nested) c1: (last c1) c2: (last c2) from: nested-dicts by: g})) +(key nested-dicts-last) -- [g n c1 c2] +(at nested-dicts-last 'c1) -- [2 3] +(at nested-dicts-last 'c2) -- [5 6] +(at (at (at nested-dicts-last 'n) 0) 0) -- 15.0 +(at (at (at nested-dicts-last 'n) 0) 250000) -- 10.0 +(at (at (at nested-dicts-last 'n) 1) 0) -- 7.0 + +(set nested-dicts-first (select {n: (first nested) c1: (first c1) c2: (first c2) from: nested-dicts by: g})) +(key nested-dicts-first) -- [g n c1 c2] +(at nested-dicts-first 'c1) -- [1 3] +(at nested-dicts-first 'c2) -- [4 6] +(at (at (at nested-dicts-first 'n) 0) 0) -- 5.0 +(at (at (at nested-dicts-first 'n) 1) 0) -- 7.0 + +;; The no-key aggregate emitter uses the same output-schema contract. +(set nested-dicts-all (select {f: (first nested) l: (last nested) c: (last c1) from: nested-dicts})) +(key nested-dicts-all) -- [f l c] +(at (at (at nested-dicts-all 'f) 0) 0) -- 5.0 +(at (at (at nested-dicts-all 'l) 0) 0) -- 7.0 +(at nested-dicts-all 'c) -- [3] + +;; Vector-valued LIST cells use the same boxed row-gather path. Multiple +;; nested aggregates in one projection must retain independent child refs. +(set nested-vecs (table [g nested tail] (list [a a b] (list [10 11] [20 21] [30 31]) (as 'i64 (list 1 2 3))))) +(set nested-vecs-grouped (select {f: (first nested) l: (last nested) tail: (last tail) from: nested-vecs by: g})) +(key nested-vecs-grouped) -- [g f l tail] +(at (at nested-vecs-grouped 'f) 0) -- [10 11] +(at (at nested-vecs-grouped 'l) 0) -- [20 21] +(at (at nested-vecs-grouped 'f) 1) -- [30 31] +(at (at nested-vecs-grouped 'l) 1) -- [30 31] +(at nested-vecs-grouped 'tail) -- [2 3] + +;; Cross RAY_PARALLEL_THRESHOLD (65536 rows) with a sparse key range so the +;; radix hash path also has to replace its temporary aggregate slot with the +;; gathered LIST result. +(set nested-radix (table [g nested tail] (list (* 100000 (% (til 70000) 2)) (take (list [10] [20] [30] [40]) 70000) (til 70000)))) +(set nested-radix-grouped (select {f: (first nested) l: (last nested) tail: (last tail) from: nested-radix by: g})) +(key nested-radix-grouped) -- [g f l tail] +(at nested-radix-grouped 'g) -- [0 100000] +(at (at nested-radix-grouped 'f) 0) -- [10] +(at (at nested-radix-grouped 'f) 1) -- [20] +(at (at nested-radix-grouped 'l) 0) -- [30] +(at (at nested-radix-grouped 'l) 1) -- [40] +(at nested-radix-grouped 'tail) -- [69998 69999] diff --git a/test/test_ipc.c b/test/test_ipc.c index 3aa9236d7..1d7c7dd97 100644 --- a/test/test_ipc.c +++ b/test/test_ipc.c @@ -754,6 +754,65 @@ static test_result_t test_ipc_poll_based_listen(void) { PASS(); } +/* ---- test_ipc_poll_public_restricted ----------------------------------- */ +/* + * An embedder can make `.sys.listen` connections read-only without + * including the private poll definition. Exercise the public setter and + * verify that the accepted connection evaluates in restricted mode, + * including inside compiled lambda bytecode. + */ +static test_result_t test_ipc_poll_public_restricted(void) { + ray_poll_t* poll = ray_poll_create(); + TEST_ASSERT_NOT_NULL(poll); + + ray_poll_set_restricted(NULL, true); /* documented no-op */ + ray_poll_set_restricted(poll, true); + + int64_t listener_id = ray_ipc_listen(poll, 0); + TEST_ASSERT((listener_id) >= (0), "listener_id >= 0"); + + ray_selector_t* listener_sel = ray_poll_get(poll, listener_id); + TEST_ASSERT_NOT_NULL(listener_sel); + uint16_t port = get_listen_port((ray_sock_t)listener_sel->fd); + TEST_ASSERT((port) > (0), "port > 0"); + + ray_vm_t* srv_vm = make_server_vm(); + TEST_ASSERT_NOT_NULL(srv_vm); + + poll_thread_ctx_t pctx = { .poll = poll, .vm = srv_vm, .running = 1 }; + ray_thread_t tid; + ray_thread_create(&tid, (void(*)(void*))poll_server_thread_fn, &pctx); + sleep_ms(20); + + int64_t h = ray_ipc_connect("127.0.0.1", port, NULL, NULL, 0); + TEST_ASSERT((h) >= (0), "restricted poll client h >= 0"); + + ray_t* arithmetic = ray_str("(+ 3 4)", 7); + ray_t* allowed = ray_ipc_send(h, arithmetic); + ray_release(arithmetic); + TEST_ASSERT_NOT_NULL(allowed); + TEST_ASSERT_FALSE(RAY_IS_ERR(allowed)); + TEST_ASSERT_EQ_I(allowed->i64, 7); + ray_release(allowed); + + const char* mutation = + "((fn [x] (set '__rf_issue_371_ipc_injected 1)) 0)"; + ray_t* mutating = ray_str(mutation, strlen(mutation)); + ray_t* denied = ray_ipc_send(h, mutating); + ray_release(mutating); + TEST_ASSERT_NOT_NULL(denied); + TEST_ASSERT_TRUE(RAY_IS_ERR(denied)); + TEST_ASSERT_STR_EQ(ray_err_code(denied), "access"); + ray_release(denied); + + ray_ipc_close(h); + poll_stop(poll, port); + ray_thread_join(tid); + ray_poll_destroy(poll); + ray_sys_free(srv_vm); + PASS(); +} + /* ---- test_ipc_poll_auth_creds_path ------------------------------------- */ /* * Poll-based auth happy path — exercises ipc_read_creds (lines 503-541) and @@ -2072,6 +2131,7 @@ const test_entry_t ipc_entries[] = { { "ipc/send_invalid_handle", test_ipc_send_invalid_handle, ipc_setup, ipc_teardown }, { "ipc/send_async_invalid_handle", test_ipc_send_async_invalid_handle, ipc_setup, ipc_teardown }, { "ipc/poll_based_listen", test_ipc_poll_based_listen, ipc_setup, ipc_teardown }, + { "ipc/poll_public_restricted", test_ipc_poll_public_restricted, ipc_setup, ipc_teardown }, { "ipc/poll_auth_creds_path", test_ipc_poll_auth_creds_path, ipc_setup, ipc_teardown }, { "ipc/poll_auth_reject", test_ipc_poll_auth_reject, ipc_setup, ipc_teardown }, { "ipc/poll_handshake_version_mismatch", test_ipc_poll_handshake_version_mismatch, ipc_setup, ipc_teardown }, diff --git a/test/test_public_api.c b/test/test_public_api.c index 5c937960d..13a40768c 100644 --- a/test/test_public_api.c +++ b/test/test_public_api.c @@ -57,6 +57,7 @@ static test_result_t test_public_ipc_client_symbols(void) { static test_result_t test_public_poll_symbols(void) { ray_poll_t* (*create_fn)(void) = ray_poll_create; void (*destroy_fn)(ray_poll_t*) = ray_poll_destroy; + void (*restricted_fn)(ray_poll_t*, bool) = ray_poll_set_restricted; int64_t (*run_fn)(ray_poll_t*) = ray_poll_run; int64_t (*run_for_fn)(ray_poll_t*, int) = ray_poll_run_for; void (*exit_fn)(ray_poll_t*, int64_t) = ray_poll_exit; @@ -65,6 +66,7 @@ static test_result_t test_public_poll_symbols(void) { TEST_ASSERT_NOT_NULL((void*)create_fn); TEST_ASSERT_NOT_NULL((void*)destroy_fn); + TEST_ASSERT_NOT_NULL((void*)restricted_fn); TEST_ASSERT_NOT_NULL((void*)run_fn); TEST_ASSERT_NOT_NULL((void*)run_for_fn); TEST_ASSERT_NOT_NULL((void*)exit_fn);