omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / osh / host / osh_req.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * osh_req.c -- request record decoder and builder (OSH_PLATFORM_ABI.md sections 3, 7.1, 11). |
| 19 | * The decoder is the only way bytes reach the executor: the record path and the direct-argv path both end here. |
| 20 | */ |
| 21 | #include "osh_host.h" |
| 22 | #include "omega_native.h" |
| 23 | |
| 24 | #include <string.h> |
| 25 | |
| 26 | #define CMD_BASE(i) (OSH_REQ_HDR_CELLS + (size_t)(i) * OSH_CMD_CELLS) |
| 27 | #define ARGV_OFF 4 |
| 28 | #define ASSIGN_OFF (4 + 2 * OSH_MAX_ARGV) |
| 29 | #define REDIR_OFF (ASSIGN_OFF + 4 * OSH_MAX_ASSIGN) |
| 30 | |
| 31 | int osh_name_valid(const char *s) |
| 32 | { |
| 33 | if (!s) return 0; |
| 34 | size_t n = strlen(s); |
| 35 | uint64_t valid = 0; |
| 36 | return omega2_osh_om_name_valid(&valid, (const uint8_t *)s, n, n) == OMEGA_OK && valid; |
| 37 | } |
| 38 | |
| 39 | /* copy out[off..off+len) into the arena as a NUL terminated string; NULL on any violation */ |
| 40 | static const char *take(OshRequest *r, const uint64_t *out, uint64_t used, uint64_t off, uint64_t len, int *nul_ok) |
| 41 | { |
| 42 | (void)nul_ok; |
| 43 | if (off > used || len > used - off) return NULL; |
| 44 | if (r->arena_used + len + 1 > sizeof r->arena) return NULL; |
| 45 | char *d = r->arena + r->arena_used; |
| 46 | for (uint64_t i = 0; i < len; i++) { |
| 47 | uint64_t v = out[off + i]; |
| 48 | if (v == 0 || v > 255) return NULL; /* NUL is never valid in an argument, name, value or path */ |
| 49 | d[i] = (char)v; |
| 50 | } |
| 51 | d[len] = 0; |
| 52 | r->arena_used += len + 1; |
| 53 | return d; |
| 54 | } |
| 55 | |
| 56 | int osh_req_decode(const uint64_t *rec, size_t nrec, const uint64_t *out, size_t nout, OshRequest *r) |
| 57 | { |
| 58 | if (!rec || nrec < OSH_REQ_HDR_CELLS) return OSH_ABI_LENGTH; |
| 59 | uint64_t error = 0; |
| 60 | if (omega2_osh_om_check_req_hdr(&error, rec, OSH_REQ_HDR_CELLS, nrec, nout, out != NULL) != OMEGA_OK) |
| 61 | return OSH_REQ_FIELD; |
| 62 | if (error) return error; |
| 63 | uint64_t ncmds = rec[1], conn = rec[2], used = rec[4]; |
| 64 | |
| 65 | memset(r, 0, sizeof *r); |
| 66 | r->ncmds = (int)ncmds; |
| 67 | r->connector_after = (int)conn; |
| 68 | r->flags = (unsigned)rec[3]; |
| 69 | for (uint64_t c = 0; c < ncmds; c++) { |
| 70 | const uint64_t *b = rec + CMD_BASE(c); |
| 71 | OshCmd *k = &r->cmd[c]; |
| 72 | uint64_t na = b[0], ns = b[1], nr = b[2], bi = b[3]; |
| 73 | if (na > OSH_MAX_ARGV || ns > OSH_MAX_ASSIGN || nr > OSH_MAX_REDIR || bi > OSH_B_EXIT) return OSH_REQ_FIELD; |
| 74 | if (bi != 0 && (na == 0 || (r->flags & OSH_REQ_FLAG_DIRECT))) return OSH_REQ_FIELD; |
| 75 | if (omega2_osh_om_check_cmd_reserved(&error, b, OSH_CMD_CELLS, na, ns, nr) != OMEGA_OK) |
| 76 | return OSH_REQ_FIELD; |
| 77 | if (error) return error; |
| 78 | k->builtin_id = (int)bi; |
| 79 | k->nargv = (int)na; |
| 80 | k->nassign = (int)ns; |
| 81 | k->nredir = (int)nr; |
| 82 | for (uint64_t i = 0; i < na; i++) { |
| 83 | k->argv[i] = take(r, out, used, b[ARGV_OFF + 2 * i], b[ARGV_OFF + 2 * i + 1], NULL); |
| 84 | if (!k->argv[i]) return OSH_REQ_FIELD; |
| 85 | } |
| 86 | k->argv[na] = NULL; |
| 87 | for (uint64_t i = 0; i < ns; i++) { |
| 88 | const uint64_t *e = b + ASSIGN_OFF + 4 * i; |
| 89 | if (e[1] == 0) return OSH_REQ_FIELD; |
| 90 | k->assign[i].name = take(r, out, used, e[0], e[1], NULL); |
| 91 | k->assign[i].value = take(r, out, used, e[2], e[3], NULL); |
| 92 | if (!k->assign[i].name || !k->assign[i].value || !osh_name_valid(k->assign[i].name)) return OSH_REQ_FIELD; |
| 93 | } |
| 94 | for (uint64_t i = 0; i < nr; i++) { |
| 95 | const uint64_t *e = b + REDIR_OFF + 4 * i; |
| 96 | OshRedir *d = &k->redir[i]; |
| 97 | if (e[0] < OSH_R_IN || e[0] > OSH_R_DUP || e[1] > 2) return OSH_REQ_FIELD; |
| 98 | d->kind = (int)e[0]; |
| 99 | d->fd = (int)e[1]; |
| 100 | if (e[0] == OSH_R_DUP) { |
| 101 | if (e[2] > 2 || e[3] != 0) return OSH_REQ_FIELD; |
| 102 | d->src_fd = (int)e[2]; |
| 103 | } else { |
| 104 | d->path = take(r, out, used, e[2], e[3], NULL); |
| 105 | if (!d->path) return OSH_REQ_FIELD; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | /* ---- builder ---- */ |
| 113 | |
| 114 | void osh_rb_init(OshBuilder *b, unsigned flags, int connector_after) |
| 115 | { |
| 116 | memset(b, 0, sizeof *b); |
| 117 | b->rec[2] = (uint64_t)connector_after; |
| 118 | b->rec[3] = flags; |
| 119 | } |
| 120 | |
| 121 | void osh_rb_cmd(OshBuilder *b, int builtin_id) |
| 122 | { |
| 123 | if (b->ncmds >= OSH_MAX_CMDS) { b->err = OSH_REQ_FIELD; return; } |
| 124 | b->rec[CMD_BASE(b->ncmds) + 3] = (uint64_t)builtin_id; |
| 125 | b->ncmds++; |
| 126 | } |
| 127 | |
| 128 | static int put_str(OshBuilder *b, const char *s, uint64_t *off, uint64_t *len) |
| 129 | { |
| 130 | size_t n = strlen(s); |
| 131 | if (b->out_used + n > OSH_OUT_CAP) { b->err = OSH_REQ_FIELD; return -1; } |
| 132 | *off = b->out_used; |
| 133 | *len = n; |
| 134 | for (size_t i = 0; i < n; i++) b->out[b->out_used + i] = (unsigned char)s[i]; |
| 135 | b->out_used += n; |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | void osh_rb_arg(OshBuilder *b, const char *s) |
| 140 | { |
| 141 | if (b->ncmds == 0) { b->err = OSH_REQ_FIELD; return; } |
| 142 | uint64_t *blk = b->rec + CMD_BASE(b->ncmds - 1), off, len; |
| 143 | if (blk[0] >= OSH_MAX_ARGV) { b->err = OSH_REQ_FIELD; return; } |
| 144 | if (put_str(b, s, &off, &len)) return; |
| 145 | blk[ARGV_OFF + 2 * blk[0]] = off; |
| 146 | blk[ARGV_OFF + 2 * blk[0] + 1] = len; |
| 147 | blk[0]++; |
| 148 | } |
| 149 | |
| 150 | void osh_rb_assign(OshBuilder *b, const char *name, const char *value) |
| 151 | { |
| 152 | if (b->ncmds == 0) { b->err = OSH_REQ_FIELD; return; } |
| 153 | uint64_t *blk = b->rec + CMD_BASE(b->ncmds - 1), no, nl, vo, vl; |
| 154 | if (blk[1] >= OSH_MAX_ASSIGN) { b->err = OSH_REQ_FIELD; return; } |
| 155 | if (put_str(b, name, &no, &nl) || put_str(b, value, &vo, &vl)) return; |
| 156 | uint64_t *e = blk + ASSIGN_OFF + 4 * blk[1]; |
| 157 | e[0] = no; e[1] = nl; e[2] = vo; e[3] = vl; |
| 158 | blk[1]++; |
| 159 | } |
| 160 | |
| 161 | void osh_rb_redir(OshBuilder *b, int kind, int fd, const char *path, int src_fd) |
| 162 | { |
| 163 | if (b->ncmds == 0) { b->err = OSH_REQ_FIELD; return; } |
| 164 | uint64_t *blk = b->rec + CMD_BASE(b->ncmds - 1), off = 0, len = 0; |
| 165 | if (blk[2] >= OSH_MAX_REDIR) { b->err = OSH_REQ_FIELD; return; } |
| 166 | if (kind == OSH_R_DUP) off = (uint64_t)src_fd; |
| 167 | else if (!path || put_str(b, path, &off, &len)) { if (!path) b->err = OSH_REQ_FIELD; return; } |
| 168 | uint64_t *e = blk + REDIR_OFF + 4 * blk[2]; |
| 169 | e[0] = (uint64_t)kind; e[1] = (uint64_t)fd; e[2] = off; e[3] = len; |
| 170 | blk[2]++; |
| 171 | } |
| 172 | |
| 173 | size_t osh_rb_cells(const OshBuilder *b) { return OSH_REQ_HDR_CELLS + (size_t)b->ncmds * OSH_CMD_CELLS; } |
| 174 | |
| 175 | void osh_rb_seal(OshBuilder *b) |
| 176 | { |
| 177 | b->rec[0] = OSH_REQ_MAGIC; |
| 178 | b->rec[1] = (uint64_t)b->ncmds; |
| 179 | b->rec[4] = b->out_used; |
| 180 | b->rec[5] = OSH_REQ_VERSION; |
| 181 | } |