@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.
@@ -1,5 +1,25 @@
1
1
  import { _WebAssembly } from "../webassembly.mjs";
2
2
  import { resolve } from "./path.mjs";
3
+ // Linux fcntl flag bits ↔ Windows libuv flag bits. `pathOpen()` builds
4
+ // `flagsRes` using Linux constants (O_CREAT=0x40, O_EXCL=0x80,
5
+ // O_APPEND=0x400). Windows libuv expects different bits (O_CREAT=0x100,
6
+ // O_EXCL=0x400, O_APPEND=0x8). Without translation,
7
+ // `fs.openSync(path, 0x241 /* L:WRONLY|CREAT|TRUNC */)` is rejected
8
+ // with EINVAL because Linux's 0x40 happens to mean O_TEMPORARY on
9
+ // Windows — and O_TEMPORARY without O_CREAT is invalid.
10
+ const _isWin32Flags = typeof process !== 'undefined' && process.platform === 'win32';
11
+ function _toWinOpenFlags(f) {
12
+ let r = f & 3; // RDONLY/WRONLY/RDWR — same on both
13
+ if ((f & 0x40) !== 0)
14
+ r |= 0x100; // O_CREAT: Linux 0x40 -> Windows 0x100
15
+ if ((f & 0x80) !== 0)
16
+ r |= 0x400; // O_EXCL: Linux 0x80 -> Windows 0x400
17
+ if ((f & 0x200) !== 0)
18
+ r |= 0x200; // O_TRUNC: same value on both
19
+ if ((f & 0x400) !== 0)
20
+ r |= 0x8; // O_APPEND: Linux 0x400 -> Windows 0x8
21
+ return r;
22
+ }
3
23
  import { WasiErrno, WasiRights, FileControlFlag, WasiFileControlFlag, WasiFdFlag, WasiFileType, WasiClockid, WasiFstFlag, WasiEventType, WasiSubclockflags } from "./types.mjs";
4
24
  import { concatBuffer, toFileStat, AsyncTable, SyncTable } from "./fd.mjs";
5
25
  import { WasiError } from "./error.mjs";
@@ -29,8 +49,18 @@ function copyMemory(targets, src) {
29
49
  const _memory = new WeakMap();
30
50
  const _wasi = new WeakMap();
31
51
  const _fs = new WeakMap();
32
- function getMemory(wasi) {
33
- return _memory.get(wasi);
52
+ export function ensureMemoryFor(memory, end) {
53
+ let buffer = memory.buffer;
54
+ if (end > buffer.byteLength) {
55
+ memory.grow(0);
56
+ buffer = memory.buffer;
57
+ }
58
+ return buffer;
59
+ }
60
+ function getMemory(wasi, end = 0) {
61
+ const memory = _memory.get(wasi);
62
+ ensureMemoryFor(memory, end);
63
+ return memory;
34
64
  }
35
65
  function getFs(wasi) {
36
66
  const fs = _fs.get(wasi);
@@ -159,9 +189,10 @@ export class WASI {
159
189
  if (argv === 0 || argv_buf === 0) {
160
190
  return WasiErrno.EINVAL;
161
191
  }
162
- const { HEAPU8, view } = getMemory(this);
163
192
  const wasi = _wasi.get(this);
164
193
  const args = wasi.args;
194
+ const argsSize = encoder.encode(args.join('\0') + '\0').length;
195
+ const { HEAPU8, view } = getMemory(this, Math.max(argv + args.length * 4, argv_buf + argsSize));
165
196
  for (let i = 0; i < args.length; ++i) {
166
197
  const arg = args[i];
167
198
  view.setInt32(argv, argv_buf, true);
@@ -178,7 +209,7 @@ export class WASI {
178
209
  if (argc === 0 || argv_buf_size === 0) {
179
210
  return WasiErrno.EINVAL;
180
211
  }
181
- const { view } = getMemory(this);
212
+ const { view } = getMemory(this, Math.max(argc + 4, argv_buf_size + 4));
182
213
  const wasi = _wasi.get(this);
183
214
  const args = wasi.args;
184
215
  view.setUint32(argc, args.length, true);
@@ -191,9 +222,10 @@ export class WASI {
191
222
  if (environ === 0 || environ_buf === 0) {
192
223
  return WasiErrno.EINVAL;
193
224
  }
194
- const { HEAPU8, view } = getMemory(this);
195
225
  const wasi = _wasi.get(this);
196
226
  const env = wasi.env;
227
+ const envSize = encoder.encode(env.join('\0') + '\0').length;
228
+ const { HEAPU8, view } = getMemory(this, Math.max(environ + env.length * 4, environ_buf + envSize));
197
229
  for (let i = 0; i < env.length; ++i) {
198
230
  const pair = env[i];
199
231
  view.setInt32(environ, environ_buf, true);
@@ -210,7 +242,7 @@ export class WASI {
210
242
  if (len === 0 || buflen === 0) {
211
243
  return WasiErrno.EINVAL;
212
244
  }
213
- const { view } = getMemory(this);
245
+ const { view } = getMemory(this, Math.max(len + 4, buflen + 4));
214
246
  const wasi = _wasi.get(this);
215
247
  view.setUint32(len, wasi.env.length, true);
216
248
  view.setUint32(buflen, encoder.encode(wasi.env.join('\0') + '\0').length, true);
@@ -221,7 +253,7 @@ export class WASI {
221
253
  if (resolution === 0) {
222
254
  return WasiErrno.EINVAL;
223
255
  }
224
- const { view } = getMemory(this);
256
+ const { view } = getMemory(this, resolution + 8);
225
257
  switch (id) {
226
258
  case WasiClockid.REALTIME:
227
259
  view.setBigUint64(resolution, BigInt(1000000), true);
@@ -239,7 +271,7 @@ export class WASI {
239
271
  if (time === 0) {
240
272
  return WasiErrno.EINVAL;
241
273
  }
242
- const { view } = getMemory(this);
274
+ const { view } = getMemory(this, time + 8);
243
275
  switch (id) {
244
276
  case WasiClockid.REALTIME:
245
277
  view.setBigUint64(time, BigInt(Date.now()) * BigInt(1000000), true);
@@ -267,7 +299,7 @@ export class WASI {
267
299
  }
268
300
  const wasi = _wasi.get(this);
269
301
  const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
270
- const { view } = getMemory(this);
302
+ const { view } = getMemory(this, fdstat + 24);
271
303
  view.setUint16(fdstat, fileDescriptor.type, true);
272
304
  view.setUint16(fdstat + 2, 0, true);
273
305
  view.setBigUint64(fdstat + 8, fileDescriptor.rightsBase, true);
@@ -308,7 +340,7 @@ export class WASI {
308
340
  }
309
341
  if (fileDescriptor.preopen !== 1)
310
342
  return WasiErrno.EINVAL;
311
- const { view } = getMemory(this);
343
+ const { view } = getMemory(this, prestat + 8);
312
344
  // preopen type is dir(0)
313
345
  view.setUint32(prestat, 0, true);
314
346
  view.setUint32(prestat + 4, encoder.encode(fileDescriptor.path).length, true);
@@ -328,7 +360,7 @@ export class WASI {
328
360
  const size = buffer.length;
329
361
  if (size > path_len)
330
362
  return WasiErrno.ENOBUFS;
331
- const { HEAPU8 } = getMemory(this);
363
+ const { HEAPU8 } = getMemory(this, path + size);
332
364
  HEAPU8.set(buffer, path);
333
365
  return WasiErrno.ESUCCESS;
334
366
  });
@@ -342,7 +374,7 @@ export class WASI {
342
374
  const wasi = _wasi.get(this);
343
375
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_SEEK, BigInt(0));
344
376
  const r = fileDescriptor.seek(offset, whence);
345
- const { view } = getMemory(this);
377
+ const { view } = getMemory(this, newOffset + 8);
346
378
  view.setBigUint64(newOffset, r, true);
347
379
  return WasiErrno.ESUCCESS;
348
380
  });
@@ -350,7 +382,7 @@ export class WASI {
350
382
  const wasi = _wasi.get(this);
351
383
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_TELL, BigInt(0));
352
384
  const pos = BigInt(fileDescriptor.pos);
353
- const { view } = getMemory(this);
385
+ const { view } = getMemory(this, Number(offset) + 8);
354
386
  view.setBigUint64(Number(offset), pos, true);
355
387
  return WasiErrno.ESUCCESS;
356
388
  });
@@ -363,7 +395,7 @@ export class WASI {
363
395
  if (in_ptr === 0 || out_ptr === 0 || nsubscriptions === 0 || nevents === 0) {
364
396
  return WasiErrno.EINVAL;
365
397
  }
366
- const { view } = getMemory(this);
398
+ const { view } = getMemory(this, Math.max(in_ptr + nsubscriptions * 48, out_ptr + nsubscriptions * 32, nevents + 4));
367
399
  view.setUint32(nevents, 0, true);
368
400
  let i = 0;
369
401
  let timer_userdata = BigInt(0);
@@ -482,7 +514,7 @@ export class WASI {
482
514
  return WasiErrno.EINVAL;
483
515
  }
484
516
  buf_len = Number(buf_len);
485
- const { HEAPU8, view } = getMemory(this);
517
+ const { HEAPU8, view } = getMemory(this, buf + buf_len);
486
518
  if ((typeof SharedArrayBuffer === 'function' && HEAPU8.buffer instanceof SharedArrayBuffer) ||
487
519
  (Object.prototype.toString.call(HEAPU8.buffer) === '[object SharedArrayBuffer]')) {
488
520
  for (let i = buf; i < buf + buf_len; ++i) {
@@ -504,7 +536,7 @@ export class WASI {
504
536
  return WasiErrno.EINVAL;
505
537
  }
506
538
  buf_len = Number(buf_len);
507
- const { view } = getMemory(this);
539
+ const { view } = getMemory(this, buf + buf_len);
508
540
  for (let i = buf; i < buf + buf_len; ++i) {
509
541
  view.setUint8(i, Math.floor(Math.random() * 256));
510
542
  }
@@ -597,7 +629,7 @@ export class WASI {
597
629
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
598
630
  const fs = getFs(this);
599
631
  const stat = fs.fstatSync(fileDescriptor.fd, { bigint: true });
600
- const { view } = getMemory(this);
632
+ const { view } = getMemory(this, buf + 64);
601
633
  toFileStat(view, buf, stat);
602
634
  return WasiErrno.ESUCCESS;
603
635
  }, async function fd_filestat_get(fd, buf) {
@@ -608,7 +640,7 @@ export class WASI {
608
640
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_FILESTAT_GET, BigInt(0));
609
641
  const h = fileDescriptor.fd;
610
642
  const stat = await h.stat({ bigint: true });
611
- const { view } = getMemory(this);
643
+ const { view } = getMemory(this, buf + 64);
612
644
  toFileStat(view, buf, stat);
613
645
  return WasiErrno.ESUCCESS;
614
646
  }, ['i32', 'i32'], ['i32']);
@@ -659,7 +691,7 @@ export class WASI {
659
691
  if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
660
692
  return WasiErrno.EINVAL;
661
693
  }
662
- const { HEAPU8, view } = getMemory(this);
694
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
663
695
  const wasi = _wasi.get(this);
664
696
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
665
697
  if (!iovslen) {
@@ -695,7 +727,7 @@ export class WASI {
695
727
  if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
696
728
  return WasiErrno.EINVAL;
697
729
  }
698
- const { HEAPU8, view } = getMemory(this);
730
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
699
731
  const wasi = _wasi.get(this);
700
732
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
701
733
  if (!iovslen) {
@@ -724,7 +756,7 @@ export class WASI {
724
756
  if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
725
757
  return WasiErrno.EINVAL;
726
758
  }
727
- const { HEAPU8, view } = getMemory(this);
759
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
728
760
  const wasi = _wasi.get(this);
729
761
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
730
762
  if (!iovslen) {
@@ -747,7 +779,7 @@ export class WASI {
747
779
  if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
748
780
  return WasiErrno.EINVAL;
749
781
  }
750
- const { HEAPU8, view } = getMemory(this);
782
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
751
783
  const wasi = _wasi.get(this);
752
784
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
753
785
  if (!iovslen) {
@@ -770,7 +802,7 @@ export class WASI {
770
802
  if ((iovs === 0 && iovslen) || size === 0) {
771
803
  return WasiErrno.EINVAL;
772
804
  }
773
- const { HEAPU8, view } = getMemory(this);
805
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
774
806
  const wasi = _wasi.get(this);
775
807
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
776
808
  if (!iovslen) {
@@ -817,7 +849,7 @@ export class WASI {
817
849
  if ((iovs === 0 && iovslen) || size === 0) {
818
850
  return WasiErrno.EINVAL;
819
851
  }
820
- const { HEAPU8, view } = getMemory(this);
852
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
821
853
  const wasi = _wasi.get(this);
822
854
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
823
855
  if (!iovslen) {
@@ -861,7 +893,7 @@ export class WASI {
861
893
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READDIR, BigInt(0));
862
894
  const fs = getFs(this);
863
895
  const entries = fs.readdirSync(fileDescriptor.realPath, { withFileTypes: true });
864
- const { HEAPU8, view } = getMemory(this);
896
+ const { HEAPU8, view } = getMemory(this, Math.max(buf + buf_len, bufused + 4));
865
897
  let bufferUsed = 0;
866
898
  for (let i = Number(cookie); i < entries.length; i++) {
867
899
  const nameData = encoder.encode(entries[i].name);
@@ -911,7 +943,7 @@ export class WASI {
911
943
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READDIR, BigInt(0));
912
944
  const fs = getFs(this);
913
945
  const entries = await fs.promises.readdir(fileDescriptor.realPath, { withFileTypes: true });
914
- const { HEAPU8, view } = getMemory(this);
946
+ const { HEAPU8, view } = getMemory(this, Math.max(buf + buf_len, bufused + 4));
915
947
  let bufferUsed = 0;
916
948
  for (let i = Number(cookie); i < entries.length; i++) {
917
949
  const nameData = encoder.encode(entries[i].name);
@@ -979,7 +1011,7 @@ export class WASI {
979
1011
  if ((iovs === 0 && iovslen) || size === 0) {
980
1012
  return WasiErrno.EINVAL;
981
1013
  }
982
- const { HEAPU8, view } = getMemory(this);
1014
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
983
1015
  const wasi = _wasi.get(this);
984
1016
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
985
1017
  if (!iovslen) {
@@ -1009,7 +1041,7 @@ export class WASI {
1009
1041
  if ((iovs === 0 && iovslen) || size === 0) {
1010
1042
  return WasiErrno.EINVAL;
1011
1043
  }
1012
- const { HEAPU8, view } = getMemory(this);
1044
+ const { HEAPU8, view } = getMemory(this, Math.max(iovs + Number(iovslen) * 8, size + 4));
1013
1045
  const wasi = _wasi.get(this);
1014
1046
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
1015
1047
  if (!iovslen) {
@@ -1039,7 +1071,7 @@ export class WASI {
1039
1071
  if (path === 0) {
1040
1072
  return WasiErrno.EINVAL;
1041
1073
  }
1042
- const { HEAPU8 } = getMemory(this);
1074
+ const { HEAPU8 } = getMemory(this, path + path_len);
1043
1075
  const wasi = _wasi.get(this);
1044
1076
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
1045
1077
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -1053,7 +1085,7 @@ export class WASI {
1053
1085
  if (path === 0) {
1054
1086
  return WasiErrno.EINVAL;
1055
1087
  }
1056
- const { HEAPU8 } = getMemory(this);
1088
+ const { HEAPU8 } = getMemory(this, path + path_len);
1057
1089
  const wasi = _wasi.get(this);
1058
1090
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_CREATE_DIRECTORY, BigInt(0));
1059
1091
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -1069,7 +1101,7 @@ export class WASI {
1069
1101
  if (path === 0 || filestat === 0) {
1070
1102
  return WasiErrno.EINVAL;
1071
1103
  }
1072
- const { HEAPU8, view } = getMemory(this);
1104
+ const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, filestat + 64));
1073
1105
  const wasi = _wasi.get(this);
1074
1106
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
1075
1107
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -1091,7 +1123,7 @@ export class WASI {
1091
1123
  if (path === 0 || filestat === 0) {
1092
1124
  return WasiErrno.EINVAL;
1093
1125
  }
1094
- const { HEAPU8, view } = getMemory(this);
1126
+ const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, filestat + 64));
1095
1127
  const wasi = _wasi.get(this);
1096
1128
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_GET, BigInt(0));
1097
1129
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -1112,7 +1144,7 @@ export class WASI {
1112
1144
  path_len = Number(path_len);
1113
1145
  if (path === 0)
1114
1146
  return WasiErrno.EINVAL;
1115
- const { HEAPU8 } = getMemory(this);
1147
+ const { HEAPU8 } = getMemory(this, path + path_len);
1116
1148
  const wasi = _wasi.get(this);
1117
1149
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1118
1150
  if (validateFstFlagsOrReturn(fst_flags)) {
@@ -1133,7 +1165,7 @@ export class WASI {
1133
1165
  path_len = Number(path_len);
1134
1166
  if (path === 0)
1135
1167
  return WasiErrno.EINVAL;
1136
- const { HEAPU8 } = getMemory(this);
1168
+ const { HEAPU8 } = getMemory(this, path + path_len);
1137
1169
  const wasi = _wasi.get(this);
1138
1170
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1139
1171
  if (validateFstFlagsOrReturn(fst_flags)) {
@@ -1168,7 +1200,7 @@ export class WASI {
1168
1200
  oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE, BigInt(0));
1169
1201
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_LINK_TARGET, BigInt(0));
1170
1202
  }
1171
- const { HEAPU8 } = getMemory(this);
1203
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
1172
1204
  const fs = getFs(this);
1173
1205
  const resolvedOldPath = resolvePathSync(fs, oldWrap, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)), old_flags);
1174
1206
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
@@ -1192,7 +1224,7 @@ export class WASI {
1192
1224
  oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_LINK_SOURCE, BigInt(0));
1193
1225
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_LINK_TARGET, BigInt(0));
1194
1226
  }
1195
- const { HEAPU8 } = getMemory(this);
1227
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
1196
1228
  const fs = getFs(this);
1197
1229
  const resolvedOldPath = await resolvePathAsync(fs, oldWrap, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)), old_flags);
1198
1230
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
@@ -1258,12 +1290,12 @@ export class WASI {
1258
1290
  const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
1259
1291
  const wasi = _wasi.get(this);
1260
1292
  const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
1261
- const memory = getMemory(this);
1293
+ const memory = getMemory(this, Math.max(path + path_len, fd + 4));
1262
1294
  const HEAPU8 = memory.HEAPU8;
1263
1295
  const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1264
1296
  const fs = getFs(this);
1265
1297
  const resolved_path = resolvePathSync(fs, fileDescriptor, pathString, dirflags);
1266
- const r = fs.openSync(resolved_path, flagsRes, 0o666);
1298
+ const r = fs.openSync(resolved_path, _isWin32Flags ? _toWinOpenFlags(flagsRes) : flagsRes, 0o666);
1267
1299
  const filetype = wasi.fds.getFileTypeByFd(r);
1268
1300
  if ((filetype !== WasiFileType.DIRECTORY) &&
1269
1301
  ((o_flags & WasiFileControlFlag.O_DIRECTORY) !== 0 ||
@@ -1294,12 +1326,12 @@ export class WASI {
1294
1326
  const { flags: flagsRes, needed_base: neededBase, needed_inheriting: neededInheriting } = pathOpen(o_flags, fs_rights_base, fs_rights_inheriting, fs_flags);
1295
1327
  const wasi = _wasi.get(this);
1296
1328
  const fileDescriptor = wasi.fds.get(dirfd, neededBase, neededInheriting);
1297
- const memory = getMemory(this);
1329
+ const memory = getMemory(this, Math.max(path + path_len, fd + 4));
1298
1330
  const HEAPU8 = memory.HEAPU8;
1299
1331
  const pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
1300
1332
  const fs = getFs(this);
1301
1333
  const resolved_path = await resolvePathAsync(fs, fileDescriptor, pathString, dirflags);
1302
- const r = await fs.promises.open(resolved_path, flagsRes, 0o666);
1334
+ const r = await fs.promises.open(resolved_path, _isWin32Flags ? _toWinOpenFlags(flagsRes) : flagsRes, 0o666);
1303
1335
  const filetype = await wasi.fds.getFileTypeByFd(r);
1304
1336
  if ((o_flags & WasiFileControlFlag.O_DIRECTORY) !== 0 && filetype !== WasiFileType.DIRECTORY) {
1305
1337
  return WasiErrno.ENOTDIR;
@@ -1326,7 +1358,7 @@ export class WASI {
1326
1358
  if (path === 0 || buf === 0 || bufused === 0) {
1327
1359
  return WasiErrno.EINVAL;
1328
1360
  }
1329
- const { HEAPU8, view } = getMemory(this);
1361
+ const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, buf + buf_len, bufused + 4));
1330
1362
  const wasi = _wasi.get(this);
1331
1363
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
1332
1364
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -1350,7 +1382,7 @@ export class WASI {
1350
1382
  if (path === 0 || buf === 0 || bufused === 0) {
1351
1383
  return WasiErrno.EINVAL;
1352
1384
  }
1353
- const { HEAPU8, view } = getMemory(this);
1385
+ const { HEAPU8, view } = getMemory(this, Math.max(path + path_len, buf + buf_len, bufused + 4));
1354
1386
  const wasi = _wasi.get(this);
1355
1387
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_READLINK, BigInt(0));
1356
1388
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -1372,7 +1404,7 @@ export class WASI {
1372
1404
  if (path === 0) {
1373
1405
  return WasiErrno.EINVAL;
1374
1406
  }
1375
- const { HEAPU8 } = getMemory(this);
1407
+ const { HEAPU8 } = getMemory(this, path + path_len);
1376
1408
  const wasi = _wasi.get(this);
1377
1409
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
1378
1410
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -1386,7 +1418,7 @@ export class WASI {
1386
1418
  if (path === 0) {
1387
1419
  return WasiErrno.EINVAL;
1388
1420
  }
1389
- const { HEAPU8 } = getMemory(this);
1421
+ const { HEAPU8 } = getMemory(this, path + path_len);
1390
1422
  const wasi = _wasi.get(this);
1391
1423
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_REMOVE_DIRECTORY, BigInt(0));
1392
1424
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -1413,7 +1445,7 @@ export class WASI {
1413
1445
  oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE, BigInt(0));
1414
1446
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
1415
1447
  }
1416
- const { HEAPU8 } = getMemory(this);
1448
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
1417
1449
  const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)));
1418
1450
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1419
1451
  const fs = getFs(this);
@@ -1437,7 +1469,7 @@ export class WASI {
1437
1469
  oldWrap = wasi.fds.get(old_fd, WasiRights.PATH_RENAME_SOURCE, BigInt(0));
1438
1470
  newWrap = wasi.fds.get(new_fd, WasiRights.PATH_RENAME_TARGET, BigInt(0));
1439
1471
  }
1440
- const { HEAPU8 } = getMemory(this);
1472
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
1441
1473
  const resolvedOldPath = resolve(oldWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len)));
1442
1474
  const resolvedNewPath = resolve(newWrap.realPath, decoder.decode(unsharedSlice(HEAPU8, new_path, new_path + new_path_len)));
1443
1475
  const fs = getFs(this);
@@ -1452,7 +1484,7 @@ export class WASI {
1452
1484
  if (old_path === 0 || new_path === 0) {
1453
1485
  return WasiErrno.EINVAL;
1454
1486
  }
1455
- const { HEAPU8 } = getMemory(this);
1487
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
1456
1488
  const wasi = _wasi.get(this);
1457
1489
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
1458
1490
  const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
@@ -1472,7 +1504,7 @@ export class WASI {
1472
1504
  if (old_path === 0 || new_path === 0) {
1473
1505
  return WasiErrno.EINVAL;
1474
1506
  }
1475
- const { HEAPU8 } = getMemory(this);
1507
+ const { HEAPU8 } = getMemory(this, Math.max(old_path + old_path_len, new_path + new_path_len));
1476
1508
  const wasi = _wasi.get(this);
1477
1509
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_SYMLINK, BigInt(0));
1478
1510
  const oldPath = decoder.decode(unsharedSlice(HEAPU8, old_path, old_path + old_path_len));
@@ -1488,7 +1520,7 @@ export class WASI {
1488
1520
  if (path === 0) {
1489
1521
  return WasiErrno.EINVAL;
1490
1522
  }
1491
- const { HEAPU8 } = getMemory(this);
1523
+ const { HEAPU8 } = getMemory(this, path + path_len);
1492
1524
  const wasi = _wasi.get(this);
1493
1525
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
1494
1526
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
@@ -1502,7 +1534,7 @@ export class WASI {
1502
1534
  if (path === 0) {
1503
1535
  return WasiErrno.EINVAL;
1504
1536
  }
1505
- const { HEAPU8 } = getMemory(this);
1537
+ const { HEAPU8 } = getMemory(this, path + path_len);
1506
1538
  const wasi = _wasi.get(this);
1507
1539
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_UNLINK_FILE, BigInt(0));
1508
1540
  let pathString = decoder.decode(unsharedSlice(HEAPU8, path, path + path_len));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tybys/wasm-util",
3
- "version": "0.10.1",
3
+ "version": "0.10.3",
4
4
  "description": "WASI polyfill for browser and some wasm util",
5
5
  "main": "./lib/cjs/index.js",
6
6
  "module": "./dist/wasm-util.esm-bundler.js",