@tybys/wasm-util 0.10.1 → 0.10.3

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.
package/dist/wasm-util.js CHANGED
@@ -312,6 +312,141 @@
312
312
 
313
313
  const CHAR_DOT = 46; /* . */
314
314
  const CHAR_FORWARD_SLASH = 47; /* / */
315
+ const CHAR_BACKWARD_SLASH = 92; /* \ */
316
+ const CHAR_COLON = 58; /* : */
317
+ const CHAR_UPPERCASE_A = 65; /* A */
318
+ const CHAR_UPPERCASE_Z = 90; /* Z */
319
+ const CHAR_LOWERCASE_A = 97; /* a */
320
+ const CHAR_LOWERCASE_Z = 122; /* z */
321
+ function isPathSeparatorWin(code) {
322
+ return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
323
+ }
324
+ function isWindowsDeviceRoot(code) {
325
+ return (code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) ||
326
+ (code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z);
327
+ }
328
+ const _isWin32 = typeof process !== 'undefined' && process.platform === 'win32';
329
+ /**
330
+ * Windows variant of `resolve()`. Mirrors Node's `path.win32.resolve`
331
+ * semantics enough for the WASI shim's needs (drive-letter absolutes
332
+ * + UNC paths + per-drive cwd lookups via `process.env`/`process.cwd`).
333
+ *
334
+ * Why this is needed: on Windows, `FileDescriptor.realPath` is the
335
+ * host realpath returned by `fs.realpathSync(realPath, 'utf8')` — the
336
+ * backslash form `D:\…`. The POSIX-only `resolveImpl` below only
337
+ * treats `/` as a separator, reads `D` as non-`/`, decides realPath
338
+ * is "relative", and produces a garbage joined path. Downstream
339
+ * `fs.openSync(garbage)` then returns `EINVAL` and the WASI caller
340
+ * sees a permanent failure. This function gives us the correct
341
+ * Windows-style resolution without taking a Node-only `path` import
342
+ * (which would break browser bundles of this package).
343
+ *
344
+ * Cribbed from Node's `lib/path.js` `win32.resolve()` (MIT-licensed),
345
+ * trimmed to what WASI realpath resolution actually exercises.
346
+ */
347
+ function resolveWin32(args) {
348
+ let resolvedDevice = '';
349
+ let resolvedTail = '';
350
+ let resolvedAbsolute = false;
351
+ for (let i = args.length - 1; i >= -1; i--) {
352
+ let path;
353
+ if (i >= 0) {
354
+ path = args[i];
355
+ validateString(path, 'path');
356
+ if (path.length === 0)
357
+ continue;
358
+ }
359
+ else if (resolvedDevice.length === 0) {
360
+ path = (typeof process !== 'undefined' && typeof process.cwd === 'function')
361
+ ? process.cwd()
362
+ : '';
363
+ }
364
+ else {
365
+ // Look up per-drive cwd via the `=X:` env var convention; fall back
366
+ // to the global cwd if absent.
367
+ const envKey = `=${resolvedDevice}`;
368
+ const env = (typeof process !== 'undefined') ? process.env : undefined;
369
+ path = (env && typeof env[envKey] === 'string')
370
+ ? env[envKey]
371
+ : (typeof process !== 'undefined' && typeof process.cwd === 'function')
372
+ ? process.cwd()
373
+ : '';
374
+ if (path === undefined ||
375
+ (path.slice(0, 2).toLowerCase() !== resolvedDevice.toLowerCase() &&
376
+ path.charCodeAt(2) === CHAR_BACKWARD_SLASH)) {
377
+ path = `${resolvedDevice}\\`;
378
+ }
379
+ }
380
+ const len = path.length;
381
+ let rootEnd = 0;
382
+ let device = '';
383
+ let isAbsolute = false;
384
+ const code = path.charCodeAt(0);
385
+ if (len === 1) {
386
+ if (isPathSeparatorWin(code)) {
387
+ rootEnd = 1;
388
+ isAbsolute = true;
389
+ }
390
+ }
391
+ else if (isPathSeparatorWin(code)) {
392
+ isAbsolute = true;
393
+ if (isPathSeparatorWin(path.charCodeAt(1))) {
394
+ // UNC path: `\\server\share\…`
395
+ let j = 2;
396
+ let last = j;
397
+ while (j < len && !isPathSeparatorWin(path.charCodeAt(j)))
398
+ j++;
399
+ if (j < len && j !== last) {
400
+ const firstPart = path.slice(last, j);
401
+ last = j;
402
+ while (j < len && isPathSeparatorWin(path.charCodeAt(j)))
403
+ j++;
404
+ if (j < len && j !== last) {
405
+ last = j;
406
+ while (j < len && !isPathSeparatorWin(path.charCodeAt(j)))
407
+ j++;
408
+ if (j === len || j !== last) {
409
+ device = `\\\\${firstPart}\\${path.slice(last, j)}`;
410
+ rootEnd = j;
411
+ }
412
+ }
413
+ }
414
+ }
415
+ else {
416
+ rootEnd = 1;
417
+ }
418
+ }
419
+ else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) {
420
+ device = path.slice(0, 2);
421
+ rootEnd = 2;
422
+ if (len > 2 && isPathSeparatorWin(path.charCodeAt(2))) {
423
+ isAbsolute = true;
424
+ rootEnd = 3;
425
+ }
426
+ }
427
+ if (device.length > 0) {
428
+ if (resolvedDevice.length > 0) {
429
+ if (device.toLowerCase() !== resolvedDevice.toLowerCase())
430
+ continue;
431
+ }
432
+ else {
433
+ resolvedDevice = device;
434
+ }
435
+ }
436
+ if (resolvedAbsolute) {
437
+ if (resolvedDevice.length > 0)
438
+ break;
439
+ }
440
+ else {
441
+ resolvedTail = `${path.slice(rootEnd)}\\${resolvedTail}`;
442
+ resolvedAbsolute = isAbsolute;
443
+ if (isAbsolute && resolvedDevice.length > 0)
444
+ break;
445
+ }
446
+ }
447
+ resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\', isPathSeparatorWin);
448
+ return resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail || '.';
449
+ }
315
450
  function isPosixPathSeparator(code) {
316
451
  return code === CHAR_FORWARD_SLASH;
317
452
  }
@@ -387,6 +522,13 @@
387
522
  return res;
388
523
  }
389
524
  function resolve(...args) {
525
+ // On Windows, host paths are `D:\…` style. The POSIX-only resolver
526
+ // below treats `D` as a non-`/` character and produces garbage; route
527
+ // to the Windows-aware variant. POSIX hosts (Linux/macOS/browser)
528
+ // run the original code path unchanged — `_isWin32` is constant-folded
529
+ // away on those targets.
530
+ if (_isWin32)
531
+ return resolveWin32(args);
390
532
  let resolvedPath = '';
391
533
  let resolvedAbsolute = false;
392
534
  for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
@@ -1023,6 +1165,26 @@
1023
1165
  });
1024
1166
  }
1025
1167
 
1168
+ // Linux fcntl flag bits ↔ Windows libuv flag bits. `pathOpen()` builds
1169
+ // `flagsRes` using Linux constants (O_CREAT=0x40, O_EXCL=0x80,
1170
+ // O_APPEND=0x400). Windows libuv expects different bits (O_CREAT=0x100,
1171
+ // O_EXCL=0x400, O_APPEND=0x8). Without translation,
1172
+ // `fs.openSync(path, 0x241 /* L:WRONLY|CREAT|TRUNC */)` is rejected
1173
+ // with EINVAL because Linux's 0x40 happens to mean O_TEMPORARY on
1174
+ // Windows — and O_TEMPORARY without O_CREAT is invalid.
1175
+ const _isWin32Flags = typeof process !== 'undefined' && process.platform === 'win32';
1176
+ function _toWinOpenFlags(f) {
1177
+ let r = f & 3; // RDONLY/WRONLY/RDWR — same on both
1178
+ if ((f & 0x40) !== 0)
1179
+ r |= 0x100; // O_CREAT: Linux 0x40 -> Windows 0x100
1180
+ if ((f & 0x80) !== 0)
1181
+ r |= 0x400; // O_EXCL: Linux 0x80 -> Windows 0x400
1182
+ if ((f & 0x200) !== 0)
1183
+ r |= 0x200; // O_TRUNC: same value on both
1184
+ if ((f & 0x400) !== 0)
1185
+ r |= 0x8; // O_APPEND: Linux 0x400 -> Windows 0x8
1186
+ return r;
1187
+ }
1026
1188
  function copyMemory(targets, src) {
1027
1189
  if (targets.length === 0 || src.length === 0)
1028
1190
  return 0;
@@ -1045,8 +1207,18 @@
1045
1207
  const _memory = new WeakMap();
1046
1208
  const _wasi = new WeakMap();
1047
1209
  const _fs = new WeakMap();
1048
- function getMemory(wasi) {
1049
- return _memory.get(wasi);
1210
+ function ensureMemoryFor(memory, end) {
1211
+ let buffer = memory.buffer;
1212
+ if (end > buffer.byteLength) {
1213
+ memory.grow(0);
1214
+ buffer = memory.buffer;
1215
+ }
1216
+ return buffer;
1217
+ }
1218
+ function getMemory(wasi, end = 0) {
1219
+ const memory = _memory.get(wasi);
1220
+ ensureMemoryFor(memory, end);
1221
+ return memory;
1050
1222
  }
1051
1223
  function getFs(wasi) {
1052
1224
  const fs = _fs.get(wasi);
@@ -1175,9 +1347,10 @@
1175
1347
  if (argv === 0 || argv_buf === 0) {
1176
1348
  return 28 /* WasiErrno.EINVAL */;
1177
1349
  }
1178
- const { HEAPU8, view } = getMemory(this);
1179
1350
  const wasi = _wasi.get(this);
1180
1351
  const args = wasi.args;
1352
+ const argsSize = encoder.encode(args.join('\0') + '\0').length;
1353
+ const { HEAPU8, view } = getMemory(this, Math.max(argv + args.length * 4, argv_buf + argsSize));
1181
1354
  for (let i = 0; i < args.length; ++i) {
1182
1355
  const arg = args[i];
1183
1356
  view.setInt32(argv, argv_buf, true);
@@ -1194,7 +1367,7 @@
1194
1367
  if (argc === 0 || argv_buf_size === 0) {
1195
1368
  return 28 /* WasiErrno.EINVAL */;
1196
1369
  }
1197
- const { view } = getMemory(this);
1370
+ const { view } = getMemory(this, Math.max(argc + 4, argv_buf_size + 4));
1198
1371
  const wasi = _wasi.get(this);
1199
1372
  const args = wasi.args;
1200
1373
  view.setUint32(argc, args.length, true);
@@ -1207,9 +1380,10 @@
1207
1380
  if (environ === 0 || environ_buf === 0) {
1208
1381
  return 28 /* WasiErrno.EINVAL */;
1209
1382
  }
1210
- const { HEAPU8, view } = getMemory(this);
1211
1383
  const wasi = _wasi.get(this);
1212
1384
  const env = wasi.env;
1385
+ const envSize = encoder.encode(env.join('\0') + '\0').length;
1386
+ const { HEAPU8, view } = getMemory(this, Math.max(environ + env.length * 4, environ_buf + envSize));
1213
1387
  for (let i = 0; i < env.length; ++i) {
1214
1388
  const pair = env[i];
1215
1389
  view.setInt32(environ, environ_buf, true);
@@ -1226,7 +1400,7 @@
1226
1400
  if (len === 0 || buflen === 0) {
1227
1401
  return 28 /* WasiErrno.EINVAL */;
1228
1402
  }
1229
- const { view } = getMemory(this);
1403
+ const { view } = getMemory(this, Math.max(len + 4, buflen + 4));
1230
1404
  const wasi = _wasi.get(this);
1231
1405
  view.setUint32(len, wasi.env.length, true);
1232
1406
  view.setUint32(buflen, encoder.encode(wasi.env.join('\0') + '\0').length, true);
@@ -1237,7 +1411,7 @@
1237
1411
  if (resolution === 0) {
1238
1412
  return 28 /* WasiErrno.EINVAL */;
1239
1413
  }
1240
- const { view } = getMemory(this);
1414
+ const { view } = getMemory(this, resolution + 8);
1241
1415
  switch (id) {
1242
1416
  case 0 /* WasiClockid.REALTIME */:
1243
1417
  view.setBigUint64(resolution, BigInt(1000000), true);
@@ -1255,7 +1429,7 @@
1255
1429
  if (time === 0) {
1256
1430
  return 28 /* WasiErrno.EINVAL */;
1257
1431
  }
1258
- const { view } = getMemory(this);
1432
+ const { view } = getMemory(this, time + 8);
1259
1433
  switch (id) {
1260
1434
  case 0 /* WasiClockid.REALTIME */:
1261
1435
  view.setBigUint64(time, BigInt(Date.now()) * BigInt(1000000), true);
@@ -1283,7 +1457,7 @@
1283
1457
  }
1284
1458
  const wasi = _wasi.get(this);
1285
1459
  const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
1286
- const { view } = getMemory(this);
1460
+ const { view } = getMemory(this, fdstat + 24);
1287
1461
  view.setUint16(fdstat, fileDescriptor.type, true);
1288
1462
  view.setUint16(fdstat + 2, 0, true);
1289
1463
  view.setBigUint64(fdstat + 8, fileDescriptor.rightsBase, true);
@@ -1324,7 +1498,7 @@
1324
1498
  }
1325
1499
  if (fileDescriptor.preopen !== 1)
1326
1500
  return 28 /* WasiErrno.EINVAL */;
1327
- const { view } = getMemory(this);
1501
+ const { view } = getMemory(this, prestat + 8);
1328
1502
  // preopen type is dir(0)
1329
1503
  view.setUint32(prestat, 0, true);
1330
1504
  view.setUint32(prestat + 4, encoder.encode(fileDescriptor.path).length, true);
@@ -1344,7 +1518,7 @@
1344
1518
  const size = buffer.length;
1345
1519
  if (size > path_len)
1346
1520
  return 42 /* WasiErrno.ENOBUFS */;
1347
- const { HEAPU8 } = getMemory(this);
1521
+ const { HEAPU8 } = getMemory(this, path + size);
1348
1522
  HEAPU8.set(buffer, path);
1349
1523
  return 0 /* WasiErrno.ESUCCESS */;
1350
1524
  });
@@ -1358,7 +1532,7 @@
1358
1532
  const wasi = _wasi.get(this);
1359
1533
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SEEK, BigInt(0));
1360
1534
  const r = fileDescriptor.seek(offset, whence);
1361
- const { view } = getMemory(this);
1535
+ const { view } = getMemory(this, newOffset + 8);
1362
1536
  view.setBigUint64(newOffset, r, true);
1363
1537
  return 0 /* WasiErrno.ESUCCESS */;
1364
1538
  });
@@ -1366,7 +1540,7 @@
1366
1540
  const wasi = _wasi.get(this);
1367
1541
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_TELL, BigInt(0));
1368
1542
  const pos = BigInt(fileDescriptor.pos);
1369
- const { view } = getMemory(this);
1543
+ const { view } = getMemory(this, Number(offset) + 8);
1370
1544
  view.setBigUint64(Number(offset), pos, true);
1371
1545
  return 0 /* WasiErrno.ESUCCESS */;
1372
1546
  });
@@ -1379,7 +1553,7 @@
1379
1553
  if (in_ptr === 0 || out_ptr === 0 || nsubscriptions === 0 || nevents === 0) {
1380
1554
  return 28 /* WasiErrno.EINVAL */;
1381
1555
  }
1382
- const { view } = getMemory(this);
1556
+ const { view } = getMemory(this, Math.max(in_ptr + nsubscriptions * 48, out_ptr + nsubscriptions * 32, nevents + 4));
1383
1557
  view.setUint32(nevents, 0, true);
1384
1558
  let i = 0;
1385
1559
  let timer_userdata = BigInt(0);
@@ -1498,7 +1672,7 @@
1498
1672
  return 28 /* WasiErrno.EINVAL */;
1499
1673
  }
1500
1674
  buf_len = Number(buf_len);
1501
- const { HEAPU8, view } = getMemory(this);
1675
+ const { HEAPU8, view } = getMemory(this, buf + buf_len);
1502
1676
  if ((typeof SharedArrayBuffer === 'function' && HEAPU8.buffer instanceof SharedArrayBuffer) ||
1503
1677
  (Object.prototype.toString.call(HEAPU8.buffer) === '[object SharedArrayBuffer]')) {
1504
1678
  for (let i = buf; i < buf + buf_len; ++i) {
@@ -1520,7 +1694,7 @@
1520
1694
  return 28 /* WasiErrno.EINVAL */;
1521
1695
  }
1522
1696
  buf_len = Number(buf_len);
1523
- const { view } = getMemory(this);
1697
+ const { view } = getMemory(this, buf + buf_len);
1524
1698
  for (let i = buf; i < buf + buf_len; ++i) {
1525
1699
  view.setUint8(i, Math.floor(Math.random() * 256));
1526
1700
  }
@@ -1613,7 +1787,7 @@
1613
1787
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
1614
1788
  const fs = getFs(this);
1615
1789
  const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
1616
- const { view } = getMemory(this);
1790
+ const { view } = getMemory(this, buf + 64);
1617
1791
  toFileStat(view, buf, stat);
1618
1792
  return 0 /* WasiErrno.ESUCCESS */;
1619
1793
  }, async function fd_filestat_get(fd, buf) {
@@ -1624,7 +1798,7 @@
1624
1798
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
1625
1799
  const h = fileDescriptor.fd;
1626
1800
  const stat = await h.stat({ bigint: true });
1627
- const { view } = getMemory(this);
1801
+ const { view } = getMemory(this, buf + 64);
1628
1802
  toFileStat(view, buf, stat);
1629
1803
  return 0 /* WasiErrno.ESUCCESS */;
1630
1804
  }, ['i32', 'i32'], ['i32']);
@@ -1675,7 +1849,7 @@
1675
1849
  if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
1676
1850
  return 28 /* WasiErrno.EINVAL */;
1677
1851
  }
1678
- const { HEAPU8, view } = getMemory(this);
1852
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1679
1853
  const wasi = _wasi.get(this);
1680
1854
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
1681
1855
  if (!iovslen) {
@@ -1711,7 +1885,7 @@
1711
1885
  if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
1712
1886
  return 28 /* WasiErrno.EINVAL */;
1713
1887
  }
1714
- const { HEAPU8, view } = getMemory(this);
1888
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1715
1889
  const wasi = _wasi.get(this);
1716
1890
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
1717
1891
  if (!iovslen) {
@@ -1740,7 +1914,7 @@
1740
1914
  if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
1741
1915
  return 28 /* WasiErrno.EINVAL */;
1742
1916
  }
1743
- const { HEAPU8, view } = getMemory(this);
1917
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1744
1918
  const wasi = _wasi.get(this);
1745
1919
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
1746
1920
  if (!iovslen) {
@@ -1763,7 +1937,7 @@
1763
1937
  if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
1764
1938
  return 28 /* WasiErrno.EINVAL */;
1765
1939
  }
1766
- const { HEAPU8, view } = getMemory(this);
1940
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1767
1941
  const wasi = _wasi.get(this);
1768
1942
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
1769
1943
  if (!iovslen) {
@@ -1786,7 +1960,7 @@
1786
1960
  if ((iovs === 0 && iovslen) || size === 0) {
1787
1961
  return 28 /* WasiErrno.EINVAL */;
1788
1962
  }
1789
- const { HEAPU8, view } = getMemory(this);
1963
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1790
1964
  const wasi = _wasi.get(this);
1791
1965
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
1792
1966
  if (!iovslen) {
@@ -1833,7 +2007,7 @@
1833
2007
  if ((iovs === 0 && iovslen) || size === 0) {
1834
2008
  return 28 /* WasiErrno.EINVAL */;
1835
2009
  }
1836
- const { HEAPU8, view } = getMemory(this);
2010
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1837
2011
  const wasi = _wasi.get(this);
1838
2012
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
1839
2013
  if (!iovslen) {
@@ -1877,7 +2051,7 @@
1877
2051
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READDIR, BigInt(0));
1878
2052
  const fs = getFs(this);
1879
2053
  const entries = fs.readdirSync(fileDescriptor.realPath, { withFileTypes: true });
1880
- const { HEAPU8, view } = getMemory(this);
2054
+ const { HEAPU8, view } = getMemory(this, Math.max(buf + buf_len, bufused + 4));
1881
2055
  let bufferUsed = 0;
1882
2056
  for (let i = Number(cookie); i < entries.length; i++) {
1883
2057
  const nameData = encoder.encode(entries[i].name);
@@ -1927,7 +2101,7 @@
1927
2101
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READDIR, BigInt(0));
1928
2102
  const fs = getFs(this);
1929
2103
  const entries = await fs.promises.readdir(fileDescriptor.realPath, { withFileTypes: true });
1930
- const { HEAPU8, view } = getMemory(this);
2104
+ const { HEAPU8, view } = getMemory(this, Math.max(buf + buf_len, bufused + 4));
1931
2105
  let bufferUsed = 0;
1932
2106
  for (let i = Number(cookie); i < entries.length; i++) {
1933
2107
  const nameData = encoder.encode(entries[i].name);
@@ -1995,7 +2169,7 @@
1995
2169
  if ((iovs === 0 && iovslen) || size === 0) {
1996
2170
  return 28 /* WasiErrno.EINVAL */;
1997
2171
  }
1998
- const { HEAPU8, view } = getMemory(this);
2172
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1999
2173
  const wasi = _wasi.get(this);
2000
2174
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
2001
2175
  if (!iovslen) {
@@ -2025,7 +2199,7 @@
2025
2199
  if ((iovs === 0 && iovslen) || size === 0) {
2026
2200
  return 28 /* WasiErrno.EINVAL */;
2027
2201
  }
2028
- const { HEAPU8, view } = getMemory(this);
2202
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
2029
2203
  const wasi = _wasi.get(this);
2030
2204
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
2031
2205
  if (!iovslen) {
@@ -2055,7 +2229,7 @@
2055
2229
  if (path === 0) {
2056
2230
  return 28 /* WasiErrno.EINVAL */;
2057
2231
  }
2058
- const { HEAPU8 } = getMemory(this);
2232
+ const { HEAPU8 } = getMemory(this, path + path_len);
2059
2233
  const wasi = _wasi.get(this);
2060
2234
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
2061
2235
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2069,7 +2243,7 @@
2069
2243
  if (path === 0) {
2070
2244
  return 28 /* WasiErrno.EINVAL */;
2071
2245
  }
2072
- const { HEAPU8 } = getMemory(this);
2246
+ const { HEAPU8 } = getMemory(this, path + path_len);
2073
2247
  const wasi = _wasi.get(this);
2074
2248
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
2075
2249
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2085,7 +2259,7 @@
2085
2259
  if (path === 0 || filestat === 0) {
2086
2260
  return 28 /* WasiErrno.EINVAL */;
2087
2261
  }
2088
- const { HEAPU8, view } = getMemory(this);
2262
+ const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, filestat + 64));
2089
2263
  const wasi = _wasi.get(this);
2090
2264
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
2091
2265
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2107,7 +2281,7 @@
2107
2281
  if (path === 0 || filestat === 0) {
2108
2282
  return 28 /* WasiErrno.EINVAL */;
2109
2283
  }
2110
- const { HEAPU8, view } = getMemory(this);
2284
+ const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, filestat + 64));
2111
2285
  const wasi = _wasi.get(this);
2112
2286
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
2113
2287
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2128,7 +2302,7 @@
2128
2302
  path_len = Number(path_len);
2129
2303
  if (path === 0)
2130
2304
  return 28 /* WasiErrno.EINVAL */;
2131
- const { HEAPU8 } = getMemory(this);
2305
+ const { HEAPU8 } = getMemory(this, path + path_len);
2132
2306
  const wasi = _wasi.get(this);
2133
2307
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
2134
2308
  if (validateFstFlagsOrReturn(fst_flags)) {
@@ -2149,7 +2323,7 @@
2149
2323
  path_len = Number(path_len);
2150
2324
  if (path === 0)
2151
2325
  return 28 /* WasiErrno.EINVAL */;
2152
- const { HEAPU8 } = getMemory(this);
2326
+ const { HEAPU8 } = getMemory(this, path + path_len);
2153
2327
  const wasi = _wasi.get(this);
2154
2328
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
2155
2329
  if (validateFstFlagsOrReturn(fst_flags)) {
@@ -2184,7 +2358,7 @@
2184
2358
  oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE, BigInt(0));
2185
2359
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_LINK_TARGET, BigInt(0));
2186
2360
  }
2187
- const { HEAPU8 } = getMemory(this);
2361
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
2188
2362
  const fs = getFs(this);
2189
2363
  const resolvedOldPath = resolvePathSync(fs, oldWrap, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)), old_flags);
2190
2364
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
@@ -2208,7 +2382,7 @@
2208
2382
  oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE, BigInt(0));
2209
2383
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_LINK_TARGET, BigInt(0));
2210
2384
  }
2211
- const { HEAPU8 } = getMemory(this);
2385
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
2212
2386
  const fs = getFs(this);
2213
2387
  const resolvedOldPath = await resolvePathAsync(fs, oldWrap, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)), old_flags);
2214
2388
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
@@ -2274,12 +2448,12 @@
2274
2448
  const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
2275
2449
  const wasi = _wasi.get(this);
2276
2450
  const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
2277
- const memory = getMemory(this);
2451
+ const memory = getMemory(this, Math.max(path + path_len, fd + 4));
2278
2452
  const HEAPU8 = memory.HEAPU8;
2279
2453
  const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
2280
2454
  const fs = getFs(this);
2281
2455
  const resolved_path = resolvePathSync(fs, fileDescriptor, pathString, dirflags);
2282
- const r = fs.openSync(resolved_path, flagsRes, 0o666);
2456
+ const r = fs.openSync(resolved_path, _isWin32Flags ? _toWinOpenFlags(flagsRes) : flagsRes, 0o666);
2283
2457
  const filetype = wasi.fds.getFileTypeByFd(r);
2284
2458
  if ((filetype !== 3 /* WasiFileType.DIRECTORY */) &&
2285
2459
  ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 ||
@@ -2310,12 +2484,12 @@
2310
2484
  const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
2311
2485
  const wasi = _wasi.get(this);
2312
2486
  const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
2313
- const memory = getMemory(this);
2487
+ const memory = getMemory(this, Math.max(path + path_len, fd + 4));
2314
2488
  const HEAPU8 = memory.HEAPU8;
2315
2489
  const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
2316
2490
  const fs = getFs(this);
2317
2491
  const resolved_path = await resolvePathAsync(fs, fileDescriptor, pathString, dirflags);
2318
- const r = await fs.promises.open(resolved_path, flagsRes, 0o666);
2492
+ const r = await fs.promises.open(resolved_path, _isWin32Flags ? _toWinOpenFlags(flagsRes) : flagsRes, 0o666);
2319
2493
  const filetype = await wasi.fds.getFileTypeByFd(r);
2320
2494
  if ((o_flags & 2 /* WasiFileControlFlag.O_DIRECTORY */) !== 0 && filetype !== 3 /* WasiFileType.DIRECTORY */) {
2321
2495
  return 54 /* WasiErrno.ENOTDIR */;
@@ -2342,7 +2516,7 @@
2342
2516
  if (path === 0 || buf === 0 || bufused === 0) {
2343
2517
  return 28 /* WasiErrno.EINVAL */;
2344
2518
  }
2345
- const { HEAPU8, view } = getMemory(this);
2519
+ const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, buf + buf_len, bufused + 4));
2346
2520
  const wasi = _wasi.get(this);
2347
2521
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
2348
2522
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2366,7 +2540,7 @@
2366
2540
  if (path === 0 || buf === 0 || bufused === 0) {
2367
2541
  return 28 /* WasiErrno.EINVAL */;
2368
2542
  }
2369
- const { HEAPU8, view } = getMemory(this);
2543
+ const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, buf + buf_len, bufused + 4));
2370
2544
  const wasi = _wasi.get(this);
2371
2545
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
2372
2546
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2388,7 +2562,7 @@
2388
2562
  if (path === 0) {
2389
2563
  return 28 /* WasiErrno.EINVAL */;
2390
2564
  }
2391
- const { HEAPU8 } = getMemory(this);
2565
+ const { HEAPU8 } = getMemory(this, path + path_len);
2392
2566
  const wasi = _wasi.get(this);
2393
2567
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
2394
2568
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2402,7 +2576,7 @@
2402
2576
  if (path === 0) {
2403
2577
  return 28 /* WasiErrno.EINVAL */;
2404
2578
  }
2405
- const { HEAPU8 } = getMemory(this);
2579
+ const { HEAPU8 } = getMemory(this, path + path_len);
2406
2580
  const wasi = _wasi.get(this);
2407
2581
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
2408
2582
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2429,7 +2603,7 @@
2429
2603
  oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE, BigInt(0));
2430
2604
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
2431
2605
  }
2432
- const { HEAPU8 } = getMemory(this);
2606
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
2433
2607
  const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)));
2434
2608
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
2435
2609
  const fs = getFs(this);
@@ -2453,7 +2627,7 @@
2453
2627
  oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE, BigInt(0));
2454
2628
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
2455
2629
  }
2456
- const { HEAPU8 } = getMemory(this);
2630
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
2457
2631
  const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)));
2458
2632
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
2459
2633
  const fs = getFs(this);
@@ -2468,7 +2642,7 @@
2468
2642
  if (old_path === 0 || new_path === 0) {
2469
2643
  return 28 /* WasiErrno.EINVAL */;
2470
2644
  }
2471
- const { HEAPU8 } = getMemory(this);
2645
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
2472
2646
  const wasi = _wasi.get(this);
2473
2647
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
2474
2648
  const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
@@ -2488,7 +2662,7 @@
2488
2662
  if (old_path === 0 || new_path === 0) {
2489
2663
  return 28 /* WasiErrno.EINVAL */;
2490
2664
  }
2491
- const { HEAPU8 } = getMemory(this);
2665
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
2492
2666
  const wasi = _wasi.get(this);
2493
2667
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
2494
2668
  const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
@@ -2504,7 +2678,7 @@
2504
2678
  if (path === 0) {
2505
2679
  return 28 /* WasiErrno.EINVAL */;
2506
2680
  }
2507
- const { HEAPU8 } = getMemory(this);
2681
+ const { HEAPU8 } = getMemory(this, path + path_len);
2508
2682
  const wasi = _wasi.get(this);
2509
2683
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
2510
2684
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -2518,7 +2692,7 @@
2518
2692
  if (path === 0) {
2519
2693
  return 28 /* WasiErrno.EINVAL */;
2520
2694
  }
2521
- const { HEAPU8 } = getMemory(this);
2695
+ const { HEAPU8 } = getMemory(this, path + path_len);
2522
2696
  const wasi = _wasi.get(this);
2523
2697
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
2524
2698
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));