@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.
@@ -9339,6 +9339,12 @@ function read(view, at, type) {
9339
9339
  if (type === "f32") return view.getFloat32(at, true);
9340
9340
  return view.getFloat64(at, true);
9341
9341
  }
9342
+ function readSome(view, at, end, type) {
9343
+ if (at + SIZE2[type] <= end) return read(view, at, type);
9344
+ const some = new DataView(new ArrayBuffer(8));
9345
+ for (let i = 0; at + i < end; i++) some.setUint8(i, view.getUint8(at + i));
9346
+ return read(some, 0, type);
9347
+ }
9342
9348
  function write(view, at, type, value2) {
9343
9349
  if (type === "i32") view.setInt32(at, Number(value2), true);
9344
9350
  else if (type === "i64") view.setBigInt64(at, BigInt(value2), true);
@@ -9349,7 +9355,8 @@ function scalars(types) {
9349
9355
  if (types.includes("other")) throw new WasiError("EINVAL");
9350
9356
  return types;
9351
9357
  }
9352
- function callDynamic(ctx, id, values, valuesLen, results, resultsLen, strict) {
9358
+ function callDynamic(ctx, ...operands) {
9359
+ const [id, values, valuesLen, results, resultsLen, strict] = operands.map((n) => n >>> 0);
9353
9360
  const table = ctx.table();
9354
9361
  if (!table) throw new WasiError("ENOTSUP");
9355
9362
  const fn = id < table.length ? table.get(id) : null;
@@ -9357,33 +9364,31 @@ function callDynamic(ctx, id, values, valuesLen, results, resultsLen, strict) {
9357
9364
  if (!fn || !sig) throw new WasiError("EINVAL");
9358
9365
  const params = scalars(sig.params);
9359
9366
  const outs = scalars(sig.results);
9367
+ const size = (types) => types.reduce((n, type) => n + SIZE2[type], 0);
9368
+ if (strict && (size(params) !== valuesLen || size(outs) !== resultsLen)) {
9369
+ throw new WasiError("EINVAL");
9370
+ }
9360
9371
  const view = ctx.mem.view();
9372
+ if (values + valuesLen > view.byteLength || results + resultsLen > view.byteLength) {
9373
+ throw new WasiError("EFAULT");
9374
+ }
9361
9375
  const end = values + valuesLen;
9362
9376
  let at = values;
9363
9377
  const args = params.map((type) => {
9364
- if (at + SIZE2[type] > end) {
9365
- if (strict) throw new WasiError("EINVAL");
9366
- return type === "i64" ? 0n : 0;
9367
- }
9368
- const v = read(view, at, type);
9378
+ const v = readSome(view, at, end, type);
9369
9379
  at += SIZE2[type];
9370
9380
  return v;
9371
9381
  });
9372
- if (strict && at !== end) throw new WasiError("EINVAL");
9373
9382
  const returned = fn(...args);
9374
9383
  const got = outs.length === 1 ? [returned] : returned ?? [];
9375
9384
  const after = ctx.mem.view();
9376
9385
  const stop = results + resultsLen;
9377
9386
  let out = results;
9378
9387
  outs.forEach((type, i) => {
9379
- if (out + SIZE2[type] > stop) {
9380
- if (strict) throw new WasiError("EINVAL");
9381
- return;
9382
- }
9388
+ if (out + SIZE2[type] > stop) return;
9383
9389
  write(after, out, type, got[i]);
9384
9390
  out += SIZE2[type];
9385
9391
  });
9386
- if (strict && out !== stop) throw new WasiError("EINVAL");
9387
9392
  }
9388
9393
  var SIZE2;
9389
9394
  var init_wasix_ffi = __esm({
@@ -9900,12 +9905,22 @@ var init_wasix_linker = __esm({
9900
9905
  if (this.mainPath === void 0) return false;
9901
9906
  if (path === this.mainPath) return true;
9902
9907
  const real = this.host.real;
9903
- return real !== void 0 && real(path) === real(this.mainPath);
9908
+ if (real === void 0) return false;
9909
+ try {
9910
+ return real(path) === real(this.mainPath);
9911
+ } catch {
9912
+ return false;
9913
+ }
9904
9914
  }
9905
9915
  open(name2, cwd, ldPath) {
9906
9916
  const path = this.locate(name2, cwd, ldPath);
9907
9917
  if (this.isMain(path)) return 0;
9908
- return this.load(path, cwd, ldPath).handle;
9918
+ this.indices?.lock();
9919
+ try {
9920
+ return this.load(path, cwd, ldPath).handle;
9921
+ } finally {
9922
+ this.indices?.unlock();
9923
+ }
9909
9924
  }
9910
9925
  symbol(handle, name2) {
9911
9926
  const found = handle === 0 ? this.find(name2) : this.findIn(this.module(handle), name2, /* @__PURE__ */ new Set());
@@ -9927,15 +9942,17 @@ var init_wasix_linker = __esm({
9927
9942
  allocateClosure() {
9928
9943
  const index2 = this.indices.allocate(this.table.length);
9929
9944
  growTable(this.table, index2 + 1);
9945
+ this.live.add(index2);
9930
9946
  return index2;
9931
9947
  }
9932
9948
  freeClosure(index2) {
9933
9949
  this.indices?.free(index2);
9950
+ this.live.delete(index2);
9934
9951
  this.table.set(index2, null);
9935
9952
  }
9953
+ live = /* @__PURE__ */ new Set();
9936
9954
  prepareClosure(record) {
9937
- if (record.index >= this.table.length)
9938
- throw new DlError(`closure ${record.index}: no such slot`);
9955
+ if (!this.live.has(record.index)) throw new DlError(`closure ${record.index}: not allocated`);
9939
9956
  const full = { kind: "closure-prepare", ...record };
9940
9957
  this.install(full);
9941
9958
  this.publisher?.(full);
@@ -9952,15 +9969,23 @@ var init_wasix_linker = __esm({
9952
9969
  this.table.set(record.index, fn);
9953
9970
  this.types.set(fn, sig);
9954
9971
  }
9972
+ reserveLoad(size) {
9973
+ return this.indices?.load(this.table.length, size) ?? {
9974
+ base: this.table.length,
9975
+ handle: this.nextHandle++
9976
+ };
9977
+ }
9955
9978
  reserve(size) {
9956
9979
  return this.indices?.reserve(this.table.length, size) ?? this.table.length;
9957
9980
  }
9958
9981
  replay(record) {
9959
9982
  if (record.kind === "closure") {
9960
9983
  growTable(this.table, record.index + 1);
9984
+ this.live.add(record.index);
9961
9985
  return;
9962
9986
  }
9963
9987
  if (record.kind === "closure-free") {
9988
+ this.live.delete(record.index);
9964
9989
  if (record.index < this.table.length) this.table.set(record.index, null);
9965
9990
  return;
9966
9991
  }
@@ -9970,7 +9995,8 @@ var init_wasix_linker = __esm({
9970
9995
  }
9971
9996
  if (record.kind === "load") {
9972
9997
  if (this.modules.has(record.handle)) return;
9973
- this.instantiate(record.path, this.moduleAt(record.path), record, false);
9998
+ const linked = this.instantiate(record.path, this.moduleAt(record.path), record, false);
9999
+ linked.needed.push(...record.needed ?? []);
9974
10000
  this.nextHandle = Math.max(this.nextHandle, record.handle + 1);
9975
10001
  return;
9976
10002
  }
@@ -10020,13 +10046,14 @@ var init_wasix_linker = __esm({
10020
10046
  (n) => this.load(this.locate(n, cwd, search), cwd, ldPath).handle
10021
10047
  );
10022
10048
  const memoryBase = this.allocate(info.memorySize, 2 ** info.memoryAlign);
10023
- const tableBase = this.reserve(info.tableSize);
10049
+ const { base: tableBase, handle } = this.reserveLoad(info.tableSize);
10024
10050
  const record = {
10025
10051
  kind: "load",
10026
- handle: this.nextHandle++,
10052
+ handle,
10027
10053
  path,
10028
10054
  memoryBase,
10029
- tableBase
10055
+ tableBase,
10056
+ ...needed.length > 0 ? { needed } : {}
10030
10057
  };
10031
10058
  const linked = this.instantiate(path, module, record, true);
10032
10059
  linked.needed.push(...needed);
@@ -10084,7 +10111,8 @@ var init_wasix_linker = __esm({
10084
10111
  linked.instance = new WebAssembly.Instance(module, imports);
10085
10112
  this.types.register(() => this.host.read(path), linked.instance, this.table, at.tableBase);
10086
10113
  this.modules.set(at.handle, linked);
10087
- this.byPath.set(path, linked);
10114
+ const first = this.byPath.get(path);
10115
+ if (!first || at.handle < first.handle) this.byPath.set(path, linked);
10088
10116
  for (const resolve of pending) resolve();
10089
10117
  if (init) {
10090
10118
  call(linked.instance, "__wasm_apply_data_relocs");
@@ -10764,6 +10792,7 @@ var init_wasix_host = __esm({
10764
10792
  return {
10765
10793
  dlopen: (p, pl, _flags, buf, len, lp, ll, out) => {
10766
10794
  if (p === 0) {
10795
+ if (!this.linker) return failed4("not a dynamically-linked program", buf, len);
10767
10796
  mem.view().setUint32(out, MAIN_HANDLE, true);
10768
10797
  return E.SUCCESS;
10769
10798
  }
@@ -10780,7 +10809,7 @@ var init_wasix_host = __esm({
10780
10809
  (l) => l.symbol(handle === MAIN_HANDLE ? 0 : handle, this.str(s, sl)),
10781
10810
  (v) => void mem.view().setUint32(out, v, true)
10782
10811
  ),
10783
- dl_invalid_handle: (handle) => handle === MAIN_HANDLE || handle !== 0 && this.linker && !this.linker.invalid(handle) ? E.SUCCESS : E.NOEXEC
10812
+ dl_invalid_handle: (handle) => this.linker && (handle === MAIN_HANDLE || handle !== 0 && !this.linker.invalid(handle)) ? E.SUCCESS : E.NOEXEC
10784
10813
  };
10785
10814
  }
10786
10815
  threadImports() {
@@ -10945,6 +10974,7 @@ var init_wasm_names = __esm({
10945
10974
  var wasi_runtime_exports = {};
10946
10975
  __export(wasi_runtime_exports, {
10947
10976
  FALLBACK_MAXIMUM_PAGES: () => FALLBACK_MAXIMUM_PAGES,
10977
+ LinkSync: () => LinkSync,
10948
10978
  captureBacktraces: () => captureBacktraces,
10949
10979
  createImportedMemory: () => createImportedMemory,
10950
10980
  runWasiProcess: () => runWasiProcess,
@@ -11117,7 +11147,7 @@ async function instantiate(host, module, memory, threads, {
11117
11147
  thread
11118
11148
  ),
11119
11149
  (req) => host.o.kernel.call(req),
11120
- threads?.ids
11150
+ threads
11121
11151
  ) : void 0;
11122
11152
  if (sync) {
11123
11153
  ({ preview1, wasix } = sync.guard(preview1, wasix));
@@ -11429,32 +11459,59 @@ var init_wasi_runtime = __esm({
11429
11459
  linker;
11430
11460
  call;
11431
11461
  ids;
11432
- constructor(linker, call2, ids) {
11462
+ owner;
11463
+ constructor(linker, call2, threads) {
11433
11464
  this.linker = linker;
11434
11465
  this.call = call2;
11435
- this.ids = ids;
11466
+ this.ids = threads?.ids;
11467
+ this.owner = threads?.tid ?? 0;
11436
11468
  linker.publisher = (record) => this.publish(record);
11437
11469
  linker.indices = {
11438
11470
  reserve: (floor, size) => this.call({ op: "dl-reserve", floor, size }),
11471
+ load: (floor, size) => this.call({ op: "dl-load-reserve", floor, size }),
11472
+ lock: () => {
11473
+ this.call({ op: "dl-lock", owner: this.owner });
11474
+ try {
11475
+ this.catchUp();
11476
+ } catch (e) {
11477
+ this.call({ op: "dl-unlock", owner: this.owner });
11478
+ throw e;
11479
+ }
11480
+ },
11481
+ unlock: () => this.call({ op: "dl-unlock", owner: this.owner }),
11439
11482
  allocate: (floor) => {
11440
11483
  const index2 = this.call({ op: "dl-closure", floor });
11441
11484
  this.catchUp();
11485
+ this.bump();
11442
11486
  return index2;
11443
11487
  },
11444
- free: (index2) => void this.call({ op: "dl-closure-free", index: index2 })
11488
+ free: (index2) => {
11489
+ this.call({ op: "dl-closure-free", index: index2 });
11490
+ this.bump();
11491
+ }
11445
11492
  };
11446
11493
  }
11494
+ bump() {
11495
+ if (this.ids) Atomics.add(this.ids, DL_GEN, 1);
11496
+ }
11447
11497
  publish(record) {
11448
11498
  const since = this.call({ op: "dl-log", append: record, from: this.count });
11449
11499
  for (const r of since.slice(0, -1)) this.linker.replay(r);
11450
11500
  this.count += since.length;
11451
- if (this.ids) this.gen = Atomics.add(this.ids, DL_GEN, 1) + 1;
11501
+ this.bump();
11502
+ this.catchUp();
11452
11503
  }
11453
11504
  catchUp() {
11454
- const since = this.call({ op: "dl-log", from: this.count });
11455
- for (const r of since) this.linker.replay(r);
11456
- this.count += since.length;
11457
- if (this.ids) this.gen = Atomics.load(this.ids, DL_GEN);
11505
+ for (; ; ) {
11506
+ const gen = this.ids ? Atomics.load(this.ids, DL_GEN) : 0;
11507
+ const since = this.call({ op: "dl-log", from: this.count });
11508
+ for (const r of since) this.linker.replay(r);
11509
+ this.count += since.length;
11510
+ if (!this.ids || Atomics.load(this.ids, DL_GEN) === gen) {
11511
+ this.gen = gen;
11512
+ return;
11513
+ }
11514
+ }
11458
11515
  }
11459
11516
  guard(preview1, wasix) {
11460
11517
  const ids = this.ids;