diff --git a/core/runtime/ctx.cpp b/core/runtime/ctx.cpp index abc5443f..5ed2fca2 100644 --- a/core/runtime/ctx.cpp +++ b/core/runtime/ctx.cpp @@ -313,6 +313,15 @@ int Ctx::bind_ops() { p.n_bands = (int32_t)oi.bands.size(); p.plan_off = (int32_t)issue_table_.size(); p.n_opd_schema = oi.schema ? (int32_t)oi.schema->n_operands : -1; + p.n_opd_required = -1; + if (oi.schema) { + /* One past the last required operand: everything after it is optional and may be + * omitted at issue. An optional operand before a required one is not omittable -- + * that would shift positions, which is what the arity check guards. */ + for (int o = 0; o < oi.schema->n_operands; ++o) + if (!oi.schema->operands[o].optional) p.n_opd_required = o + 1; + if (p.n_opd_required < 0) p.n_opd_required = 0; /* an all-optional schema */ + } /* Matched by name once here, not on the issue path; see OpPlan::billed. */ p.billed = (oi.op == "all_reduce"); p.collective = p.billed; diff --git a/core/runtime/ctx.h b/core/runtime/ctx.h index 9ef3a898..417f5c7e 100644 --- a/core/runtime/ctx.h +++ b/core/runtime/ctx.h @@ -76,8 +76,14 @@ struct OpPlan { int32_t n_bands = 0; int32_t plan_off = 0; /* into Ctx::issue_table_, band * RAD_N_DOMAINS + domain */ /* The schema's operand count, cached so the arity check avoids a cold OpInfo. -1 when the op - * has no schema. */ - int32_t n_opd_schema = -1; + * has no schema. The arity check also stores here the count with the schema's *trailing* + * optional operands dropped (rad_abi.h: "an absent optional operand is a null + * RadTensor.data"): an issue may stop at n_opd_required and the shims -- r4d_opt(), + * rad_args_have(), libref's finish() -- already branch on exactly that. Only trailing + * optionals; an omitted optional before a required operand shifts positions and is the + * miscount the check exists to catch. */ + int32_t n_opd_schema = -1; + int32_t n_opd_required = -1; bool issued = false; /* did the run phase ever issue it (spec ยง16's graph dump) */ /* Whether issuing it puts bytes on the interconnect. Set at bind from the resolved rows * (rad_kernel_collective) and the conventional all_reduce. The weight stager's copies share diff --git a/core/runtime/issue.cpp b/core/runtime/issue.cpp index 05f33e02..410b7fa4 100644 --- a/core/runtime/issue.cpp +++ b/core/runtime/issue.cpp @@ -549,13 +549,23 @@ int Ctx::issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n) { return abort_step_msg(h, RAD_E_INVAL, fmt("issued with %d operands; the runtime carries at most %d", n_opd, MAX_OPERANDS)); - if (p.n_opd_schema >= 0 && n_opd != p.n_opd_schema) + /* A count below the schema is legal only when every dropped slot is a *trailing optional* + * operand (rad_abi.h: "an absent optional operand is a null RadTensor.data" -- the shims + * branch on exactly that: r4d_opt(), rad_args_have(), libref's finish()). A count below the + * required floor shifts positions or drops a required operand, and operands are positional, + * so a miscount is silent numerical garbage. */ + if (p.n_opd_schema >= 0 && (n_opd > p.n_opd_schema || n_opd < p.n_opd_required)) return abort_step_msg(h, RAD_E_SCHEMA, - fmt("issued with %d operands; the op's schema declares %d. Operands " - "are positional, so a miscount is silent numerical garbage", - n_opd, (int)p.n_opd_schema)); + fmt("issued with %d operands; the op's schema declares %d " + "(%d required, only trailing optional operands may be omitted). " + "Operands are positional, so a miscount is silent numerical " + "garbage", + n_opd, (int)p.n_opd_schema, (int)p.n_opd_required)); RadTensor* t = tbuf_; + /* The schema slots the issue stopped short of: null tensors, never stale ones. Nothing is + * supposed to read past args.n_t -- this keeps a stray read a null deref, not old bytes. */ + for (int i = n_opd; i < p.n_opd_schema; ++i) t[i] = RadTensor{}; bool touches_host = false; /* a host-domain buffer among the operands; see host_touch_ev_ */ for (int i = 0; i < n_opd; ++i) {