@ai-ecoverse/slicc-kernel 1.54.3 → 1.54.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7426,6 +7426,12 @@ function read(view, at, type) {
7426
7426
  if (type === "f32") return view.getFloat32(at, true);
7427
7427
  return view.getFloat64(at, true);
7428
7428
  }
7429
+ function readSome(view, at, end, type) {
7430
+ if (at + SIZE2[type] <= end) return read(view, at, type);
7431
+ const some = new DataView(new ArrayBuffer(8));
7432
+ for (let i = 0; at + i < end; i++) some.setUint8(i, view.getUint8(at + i));
7433
+ return read(some, 0, type);
7434
+ }
7429
7435
  function write(view, at, type, value2) {
7430
7436
  if (type === "i32") view.setInt32(at, Number(value2), true);
7431
7437
  else if (type === "i64") view.setBigInt64(at, BigInt(value2), true);
@@ -7436,7 +7442,8 @@ function scalars(types) {
7436
7442
  if (types.includes("other")) throw new WasiError("EINVAL");
7437
7443
  return types;
7438
7444
  }
7439
- function callDynamic(ctx, id, values, valuesLen, results, resultsLen, strict) {
7445
+ function callDynamic(ctx, ...operands) {
7446
+ const [id, values, valuesLen, results, resultsLen, strict] = operands.map((n) => n >>> 0);
7440
7447
  const table = ctx.table();
7441
7448
  if (!table) throw new WasiError("ENOTSUP");
7442
7449
  const fn = id < table.length ? table.get(id) : null;
@@ -7444,33 +7451,31 @@ function callDynamic(ctx, id, values, valuesLen, results, resultsLen, strict) {
7444
7451
  if (!fn || !sig) throw new WasiError("EINVAL");
7445
7452
  const params = scalars(sig.params);
7446
7453
  const outs = scalars(sig.results);
7454
+ const size = (types) => types.reduce((n, type) => n + SIZE2[type], 0);
7455
+ if (strict && (size(params) !== valuesLen || size(outs) !== resultsLen)) {
7456
+ throw new WasiError("EINVAL");
7457
+ }
7447
7458
  const view = ctx.mem.view();
7459
+ if (values + valuesLen > view.byteLength || results + resultsLen > view.byteLength) {
7460
+ throw new WasiError("EFAULT");
7461
+ }
7448
7462
  const end = values + valuesLen;
7449
7463
  let at = values;
7450
7464
  const args = params.map((type) => {
7451
- if (at + SIZE2[type] > end) {
7452
- if (strict) throw new WasiError("EINVAL");
7453
- return type === "i64" ? 0n : 0;
7454
- }
7455
- const v = read(view, at, type);
7465
+ const v = readSome(view, at, end, type);
7456
7466
  at += SIZE2[type];
7457
7467
  return v;
7458
7468
  });
7459
- if (strict && at !== end) throw new WasiError("EINVAL");
7460
7469
  const returned = fn(...args);
7461
7470
  const got = outs.length === 1 ? [returned] : returned ?? [];
7462
7471
  const after = ctx.mem.view();
7463
7472
  const stop = results + resultsLen;
7464
7473
  let out = results;
7465
7474
  outs.forEach((type, i) => {
7466
- if (out + SIZE2[type] > stop) {
7467
- if (strict) throw new WasiError("EINVAL");
7468
- return;
7469
- }
7475
+ if (out + SIZE2[type] > stop) return;
7470
7476
  write(after, out, type, got[i]);
7471
7477
  out += SIZE2[type];
7472
7478
  });
7473
- if (strict && out !== stop) throw new WasiError("EINVAL");
7474
7479
  }
7475
7480
  var SIZE2;
7476
7481
  var init_wasix_ffi = __esm({
@@ -7987,12 +7992,22 @@ var init_wasix_linker = __esm({
7987
7992
  if (this.mainPath === void 0) return false;
7988
7993
  if (path === this.mainPath) return true;
7989
7994
  const real = this.host.real;
7990
- return real !== void 0 && real(path) === real(this.mainPath);
7995
+ if (real === void 0) return false;
7996
+ try {
7997
+ return real(path) === real(this.mainPath);
7998
+ } catch {
7999
+ return false;
8000
+ }
7991
8001
  }
7992
8002
  open(name2, cwd, ldPath) {
7993
8003
  const path = this.locate(name2, cwd, ldPath);
7994
8004
  if (this.isMain(path)) return 0;
7995
- return this.load(path, cwd, ldPath).handle;
8005
+ this.indices?.lock();
8006
+ try {
8007
+ return this.load(path, cwd, ldPath).handle;
8008
+ } finally {
8009
+ this.indices?.unlock();
8010
+ }
7996
8011
  }
7997
8012
  symbol(handle, name2) {
7998
8013
  const found = handle === 0 ? this.find(name2) : this.findIn(this.module(handle), name2, /* @__PURE__ */ new Set());
@@ -8014,15 +8029,17 @@ var init_wasix_linker = __esm({
8014
8029
  allocateClosure() {
8015
8030
  const index2 = this.indices.allocate(this.table.length);
8016
8031
  growTable(this.table, index2 + 1);
8032
+ this.live.add(index2);
8017
8033
  return index2;
8018
8034
  }
8019
8035
  freeClosure(index2) {
8020
8036
  this.indices?.free(index2);
8037
+ this.live.delete(index2);
8021
8038
  this.table.set(index2, null);
8022
8039
  }
8040
+ live = /* @__PURE__ */ new Set();
8023
8041
  prepareClosure(record) {
8024
- if (record.index >= this.table.length)
8025
- throw new DlError(`closure ${record.index}: no such slot`);
8042
+ if (!this.live.has(record.index)) throw new DlError(`closure ${record.index}: not allocated`);
8026
8043
  const full = { kind: "closure-prepare", ...record };
8027
8044
  this.install(full);
8028
8045
  this.publisher?.(full);
@@ -8039,15 +8056,23 @@ var init_wasix_linker = __esm({
8039
8056
  this.table.set(record.index, fn);
8040
8057
  this.types.set(fn, sig);
8041
8058
  }
8059
+ reserveLoad(size) {
8060
+ return this.indices?.load(this.table.length, size) ?? {
8061
+ base: this.table.length,
8062
+ handle: this.nextHandle++
8063
+ };
8064
+ }
8042
8065
  reserve(size) {
8043
8066
  return this.indices?.reserve(this.table.length, size) ?? this.table.length;
8044
8067
  }
8045
8068
  replay(record) {
8046
8069
  if (record.kind === "closure") {
8047
8070
  growTable(this.table, record.index + 1);
8071
+ this.live.add(record.index);
8048
8072
  return;
8049
8073
  }
8050
8074
  if (record.kind === "closure-free") {
8075
+ this.live.delete(record.index);
8051
8076
  if (record.index < this.table.length) this.table.set(record.index, null);
8052
8077
  return;
8053
8078
  }
@@ -8057,7 +8082,8 @@ var init_wasix_linker = __esm({
8057
8082
  }
8058
8083
  if (record.kind === "load") {
8059
8084
  if (this.modules.has(record.handle)) return;
8060
- this.instantiate(record.path, this.moduleAt(record.path), record, false);
8085
+ const linked = this.instantiate(record.path, this.moduleAt(record.path), record, false);
8086
+ linked.needed.push(...record.needed ?? []);
8061
8087
  this.nextHandle = Math.max(this.nextHandle, record.handle + 1);
8062
8088
  return;
8063
8089
  }
@@ -8107,13 +8133,14 @@ var init_wasix_linker = __esm({
8107
8133
  (n) => this.load(this.locate(n, cwd, search), cwd, ldPath).handle
8108
8134
  );
8109
8135
  const memoryBase = this.allocate(info.memorySize, 2 ** info.memoryAlign);
8110
- const tableBase = this.reserve(info.tableSize);
8136
+ const { base: tableBase, handle } = this.reserveLoad(info.tableSize);
8111
8137
  const record = {
8112
8138
  kind: "load",
8113
- handle: this.nextHandle++,
8139
+ handle,
8114
8140
  path,
8115
8141
  memoryBase,
8116
- tableBase
8142
+ tableBase,
8143
+ ...needed.length > 0 ? { needed } : {}
8117
8144
  };
8118
8145
  const linked = this.instantiate(path, module, record, true);
8119
8146
  linked.needed.push(...needed);
@@ -8171,7 +8198,8 @@ var init_wasix_linker = __esm({
8171
8198
  linked.instance = new WebAssembly.Instance(module, imports);
8172
8199
  this.types.register(() => this.host.read(path), linked.instance, this.table, at.tableBase);
8173
8200
  this.modules.set(at.handle, linked);
8174
- this.byPath.set(path, linked);
8201
+ const first = this.byPath.get(path);
8202
+ if (!first || at.handle < first.handle) this.byPath.set(path, linked);
8175
8203
  for (const resolve of pending) resolve();
8176
8204
  if (init) {
8177
8205
  call(linked.instance, "__wasm_apply_data_relocs");
@@ -8851,6 +8879,7 @@ var init_wasix_host = __esm({
8851
8879
  return {
8852
8880
  dlopen: (p, pl, _flags, buf, len, lp, ll, out) => {
8853
8881
  if (p === 0) {
8882
+ if (!this.linker) return failed4("not a dynamically-linked program", buf, len);
8854
8883
  mem.view().setUint32(out, MAIN_HANDLE, true);
8855
8884
  return E.SUCCESS;
8856
8885
  }
@@ -8867,7 +8896,7 @@ var init_wasix_host = __esm({
8867
8896
  (l) => l.symbol(handle === MAIN_HANDLE ? 0 : handle, this.str(s, sl)),
8868
8897
  (v) => void mem.view().setUint32(out, v, true)
8869
8898
  ),
8870
- dl_invalid_handle: (handle) => handle === MAIN_HANDLE || handle !== 0 && this.linker && !this.linker.invalid(handle) ? E.SUCCESS : E.NOEXEC
8899
+ dl_invalid_handle: (handle) => this.linker && (handle === MAIN_HANDLE || handle !== 0 && !this.linker.invalid(handle)) ? E.SUCCESS : E.NOEXEC
8871
8900
  };
8872
8901
  }
8873
8902
  threadImports() {
@@ -9032,6 +9061,7 @@ var init_wasm_names = __esm({
9032
9061
  var wasi_runtime_exports = {};
9033
9062
  __export(wasi_runtime_exports, {
9034
9063
  FALLBACK_MAXIMUM_PAGES: () => FALLBACK_MAXIMUM_PAGES,
9064
+ LinkSync: () => LinkSync,
9035
9065
  captureBacktraces: () => captureBacktraces,
9036
9066
  createImportedMemory: () => createImportedMemory,
9037
9067
  runWasiProcess: () => runWasiProcess,
@@ -9204,7 +9234,7 @@ async function instantiate(host, module, memory, threads, {
9204
9234
  thread
9205
9235
  ),
9206
9236
  (req) => host.o.kernel.call(req),
9207
- threads?.ids
9237
+ threads
9208
9238
  ) : void 0;
9209
9239
  if (sync) {
9210
9240
  ({ preview1, wasix } = sync.guard(preview1, wasix));
@@ -9516,32 +9546,59 @@ var init_wasi_runtime = __esm({
9516
9546
  linker;
9517
9547
  call;
9518
9548
  ids;
9519
- constructor(linker, call2, ids) {
9549
+ owner;
9550
+ constructor(linker, call2, threads) {
9520
9551
  this.linker = linker;
9521
9552
  this.call = call2;
9522
- this.ids = ids;
9553
+ this.ids = threads?.ids;
9554
+ this.owner = threads?.tid ?? 0;
9523
9555
  linker.publisher = (record) => this.publish(record);
9524
9556
  linker.indices = {
9525
9557
  reserve: (floor, size) => this.call({ op: "dl-reserve", floor, size }),
9558
+ load: (floor, size) => this.call({ op: "dl-load-reserve", floor, size }),
9559
+ lock: () => {
9560
+ this.call({ op: "dl-lock", owner: this.owner });
9561
+ try {
9562
+ this.catchUp();
9563
+ } catch (e) {
9564
+ this.call({ op: "dl-unlock", owner: this.owner });
9565
+ throw e;
9566
+ }
9567
+ },
9568
+ unlock: () => this.call({ op: "dl-unlock", owner: this.owner }),
9526
9569
  allocate: (floor) => {
9527
9570
  const index2 = this.call({ op: "dl-closure", floor });
9528
9571
  this.catchUp();
9572
+ this.bump();
9529
9573
  return index2;
9530
9574
  },
9531
- free: (index2) => void this.call({ op: "dl-closure-free", index: index2 })
9575
+ free: (index2) => {
9576
+ this.call({ op: "dl-closure-free", index: index2 });
9577
+ this.bump();
9578
+ }
9532
9579
  };
9533
9580
  }
9581
+ bump() {
9582
+ if (this.ids) Atomics.add(this.ids, DL_GEN, 1);
9583
+ }
9534
9584
  publish(record) {
9535
9585
  const since = this.call({ op: "dl-log", append: record, from: this.count });
9536
9586
  for (const r of since.slice(0, -1)) this.linker.replay(r);
9537
9587
  this.count += since.length;
9538
- if (this.ids) this.gen = Atomics.add(this.ids, DL_GEN, 1) + 1;
9588
+ this.bump();
9589
+ this.catchUp();
9539
9590
  }
9540
9591
  catchUp() {
9541
- const since = this.call({ op: "dl-log", from: this.count });
9542
- for (const r of since) this.linker.replay(r);
9543
- this.count += since.length;
9544
- if (this.ids) this.gen = Atomics.load(this.ids, DL_GEN);
9592
+ for (; ; ) {
9593
+ const gen = this.ids ? Atomics.load(this.ids, DL_GEN) : 0;
9594
+ const since = this.call({ op: "dl-log", from: this.count });
9595
+ for (const r of since) this.linker.replay(r);
9596
+ this.count += since.length;
9597
+ if (!this.ids || Atomics.load(this.ids, DL_GEN) === gen) {
9598
+ this.gen = gen;
9599
+ return;
9600
+ }
9601
+ }
9545
9602
  }
9546
9603
  guard(preview1, wasix) {
9547
9604
  const ids = this.ids;