@tybys/wasm-util 0.8.3 → 0.9.0

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.
@@ -118,6 +118,7 @@ async function resolvePathAsync(fs, fileDescriptor, path, flags) {
118
118
  const encoder = /*#__PURE__*/ new TextEncoder();
119
119
  // eslint-disable-next-line spaced-comment
120
120
  const decoder = /*#__PURE__*/ new TextDecoder();
121
+ const INT64_MAX = (BigInt(1) << BigInt(63)) - BigInt(1);
121
122
  function readStdin() {
122
123
  const value = window.prompt();
123
124
  if (value === null)
@@ -125,6 +126,14 @@ function readStdin() {
125
126
  const buffer = new TextEncoder().encode(value + '\n');
126
127
  return buffer;
127
128
  }
129
+ function validateFstFlagsOrReturn(flags) {
130
+ return (Boolean((flags) & ~(WasiFstFlag.SET_ATIM | WasiFstFlag.SET_ATIM_NOW |
131
+ WasiFstFlag.SET_MTIM | WasiFstFlag.SET_MTIM_NOW)) ||
132
+ ((flags) & (WasiFstFlag.SET_ATIM | WasiFstFlag.SET_ATIM_NOW)) ===
133
+ (WasiFstFlag.SET_ATIM | WasiFstFlag.SET_ATIM_NOW) ||
134
+ ((flags) & (WasiFstFlag.SET_MTIM | WasiFstFlag.SET_MTIM_NOW)) ===
135
+ (WasiFstFlag.SET_MTIM | WasiFstFlag.SET_MTIM_NOW));
136
+ }
128
137
  export class WASI {
129
138
  constructor(args, env, fds, asyncFs, fs, asyncify) {
130
139
  this.args_get = syscallWrap(this, 'args_get', function (argv, argv_buf) {
@@ -285,7 +294,7 @@ export class WASI {
285
294
  const { view } = getMemory(this);
286
295
  // preopen type is dir(0)
287
296
  view.setUint32(prestat, 0, true);
288
- view.setUint32(prestat + 4, encoder.encode(fileDescriptor.path).length + 1, true);
297
+ view.setUint32(prestat + 4, encoder.encode(fileDescriptor.path).length, true);
289
298
  return WasiErrno.ESUCCESS;
290
299
  });
291
300
  this.fd_prestat_dir_name = syscallWrap(this, 'fd_prestat_dir_name', function (fd, path, path_len) {
@@ -298,7 +307,7 @@ export class WASI {
298
307
  const fileDescriptor = wasi.fds.get(fd, BigInt(0), BigInt(0));
299
308
  if (fileDescriptor.preopen !== 1)
300
309
  return WasiErrno.EBADF;
301
- const buffer = encoder.encode(fileDescriptor.path + '\0');
310
+ const buffer = encoder.encode(fileDescriptor.path);
302
311
  const size = buffer.length;
303
312
  if (size > path_len)
304
313
  return WasiErrno.ENOBUFS;
@@ -493,6 +502,9 @@ export class WASI {
493
502
  this.sock_shutdown = syscallWrap(this, 'sock_shutdown', function () {
494
503
  return WasiErrno.ENOTSUP;
495
504
  });
505
+ this.sock_accept = syscallWrap(this, 'sock_accept', function () {
506
+ return WasiErrno.ENOTSUP;
507
+ });
496
508
  _wasi.set(this, {
497
509
  fds,
498
510
  args,
@@ -608,11 +620,17 @@ export class WASI {
608
620
  return { fileDescriptor, atim, mtim };
609
621
  }
610
622
  defineImport('fd_filestat_set_times', function fd_filestat_set_times(fd, atim, mtim, flags) {
623
+ if (validateFstFlagsOrReturn(flags)) {
624
+ return WasiErrno.EINVAL;
625
+ }
611
626
  const { fileDescriptor, atim: atimRes, mtim: mtimRes } = fdFilestatGetTimes.call(this, fd, atim, mtim, flags);
612
627
  const fs = getFs(this);
613
628
  fs.futimesSync(fileDescriptor.fd, Number(atimRes), Number(mtimRes));
614
629
  return WasiErrno.ESUCCESS;
615
630
  }, async function fd_filestat_set_times(fd, atim, mtim, flags) {
631
+ if (validateFstFlagsOrReturn(flags)) {
632
+ return WasiErrno.EINVAL;
633
+ }
616
634
  const { fileDescriptor, atim: atimRes, mtim: mtimRes } = fdFilestatGetTimes.call(this, fd, atim, mtim, flags);
617
635
  const h = fileDescriptor.fd;
618
636
  await h.utimes(Number(atimRes), Number(mtimRes));
@@ -621,12 +639,16 @@ export class WASI {
621
639
  defineImport('fd_pread', function fd_pread(fd, iovs, iovslen, offset, size) {
622
640
  iovs = Number(iovs);
623
641
  size = Number(size);
624
- if (iovs === 0 || size === 0) {
642
+ if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
625
643
  return WasiErrno.EINVAL;
626
644
  }
627
645
  const { HEAPU8, view } = getMemory(this);
628
646
  const wasi = _wasi.get(this);
629
647
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
648
+ if (!iovslen) {
649
+ view.setUint32(size, 0, true);
650
+ return WasiErrno.ESUCCESS;
651
+ }
630
652
  let totalSize = 0;
631
653
  const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
632
654
  const offset = iovs + (i * 8);
@@ -653,12 +675,16 @@ export class WASI {
653
675
  }, async function (fd, iovs, iovslen, offset, size) {
654
676
  iovs = Number(iovs);
655
677
  size = Number(size);
656
- if (iovs === 0 || size === 0) {
678
+ if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
657
679
  return WasiErrno.EINVAL;
658
680
  }
659
681
  const { HEAPU8, view } = getMemory(this);
660
682
  const wasi = _wasi.get(this);
661
683
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ | WasiRights.FD_SEEK, BigInt(0));
684
+ if (!iovslen) {
685
+ view.setUint32(size, 0, true);
686
+ return WasiErrno.ESUCCESS;
687
+ }
662
688
  let totalSize = 0;
663
689
  const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
664
690
  const offset = iovs + (i * 8);
@@ -678,12 +704,16 @@ export class WASI {
678
704
  defineImport('fd_pwrite', function fd_pwrite(fd, iovs, iovslen, offset, size) {
679
705
  iovs = Number(iovs);
680
706
  size = Number(size);
681
- if (iovs === 0 || size === 0) {
707
+ if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
682
708
  return WasiErrno.EINVAL;
683
709
  }
684
710
  const { HEAPU8, view } = getMemory(this);
685
711
  const wasi = _wasi.get(this);
686
712
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
713
+ if (!iovslen) {
714
+ view.setUint32(size, 0, true);
715
+ return WasiErrno.ESUCCESS;
716
+ }
687
717
  const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
688
718
  const offset = iovs + (i * 8);
689
719
  const buf = view.getInt32(offset, true);
@@ -697,12 +727,16 @@ export class WASI {
697
727
  }, async function fd_pwrite(fd, iovs, iovslen, offset, size) {
698
728
  iovs = Number(iovs);
699
729
  size = Number(size);
700
- if (iovs === 0 || size === 0) {
730
+ if ((iovs === 0 && iovslen) || size === 0 || offset > INT64_MAX) {
701
731
  return WasiErrno.EINVAL;
702
732
  }
703
733
  const { HEAPU8, view } = getMemory(this);
704
734
  const wasi = _wasi.get(this);
705
735
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE | WasiRights.FD_SEEK, BigInt(0));
736
+ if (!iovslen) {
737
+ view.setUint32(size, 0, true);
738
+ return WasiErrno.ESUCCESS;
739
+ }
706
740
  const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
707
741
  const offset = iovs + (i * 8);
708
742
  const buf = view.getInt32(offset, true);
@@ -716,12 +750,16 @@ export class WASI {
716
750
  defineImport('fd_read', function fd_read(fd, iovs, iovslen, size) {
717
751
  iovs = Number(iovs);
718
752
  size = Number(size);
719
- if (iovs === 0 || size === 0) {
753
+ if ((iovs === 0 && iovslen) || size === 0) {
720
754
  return WasiErrno.EINVAL;
721
755
  }
722
756
  const { HEAPU8, view } = getMemory(this);
723
757
  const wasi = _wasi.get(this);
724
758
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
759
+ if (!iovslen) {
760
+ view.setUint32(size, 0, true);
761
+ return WasiErrno.ESUCCESS;
762
+ }
725
763
  let totalSize = 0;
726
764
  const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
727
765
  const offset = iovs + (i * 8);
@@ -759,12 +797,16 @@ export class WASI {
759
797
  }, async function fd_read(fd, iovs, iovslen, size) {
760
798
  iovs = Number(iovs);
761
799
  size = Number(size);
762
- if (iovs === 0 || size === 0) {
800
+ if ((iovs === 0 && iovslen) || size === 0) {
763
801
  return WasiErrno.EINVAL;
764
802
  }
765
803
  const { HEAPU8, view } = getMemory(this);
766
804
  const wasi = _wasi.get(this);
767
805
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_READ, BigInt(0));
806
+ if (!iovslen) {
807
+ view.setUint32(size, 0, true);
808
+ return WasiErrno.ESUCCESS;
809
+ }
768
810
  let totalSize = 0;
769
811
  const ioVecs = Array.from({ length: Number(iovslen) }, (_, i) => {
770
812
  const offset = iovs + (i * 8);
@@ -917,12 +959,16 @@ export class WASI {
917
959
  defineImport('fd_write', function fd_write(fd, iovs, iovslen, size) {
918
960
  iovs = Number(iovs);
919
961
  size = Number(size);
920
- if (iovs === 0 || size === 0) {
962
+ if ((iovs === 0 && iovslen) || size === 0) {
921
963
  return WasiErrno.EINVAL;
922
964
  }
923
965
  const { HEAPU8, view } = getMemory(this);
924
966
  const wasi = _wasi.get(this);
925
967
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
968
+ if (!iovslen) {
969
+ view.setUint32(size, 0, true);
970
+ return WasiErrno.ESUCCESS;
971
+ }
926
972
  const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
927
973
  const offset = iovs + (i * 8);
928
974
  const buf = view.getInt32(offset, true);
@@ -943,12 +989,16 @@ export class WASI {
943
989
  }, async function fd_write(fd, iovs, iovslen, size) {
944
990
  iovs = Number(iovs);
945
991
  size = Number(size);
946
- if (iovs === 0 || size === 0) {
992
+ if ((iovs === 0 && iovslen) || size === 0) {
947
993
  return WasiErrno.EINVAL;
948
994
  }
949
995
  const { HEAPU8, view } = getMemory(this);
950
996
  const wasi = _wasi.get(this);
951
997
  const fileDescriptor = wasi.fds.get(fd, WasiRights.FD_WRITE, BigInt(0));
998
+ if (!iovslen) {
999
+ view.setUint32(size, 0, true);
1000
+ return WasiErrno.ESUCCESS;
1001
+ }
952
1002
  const buffer = concatBuffer(Array.from({ length: Number(iovslen) }, (_, i) => {
953
1003
  const offset = iovs + (i * 8);
954
1004
  const buf = view.getInt32(offset, true);
@@ -1045,15 +1095,12 @@ export class WASI {
1045
1095
  path_len = Number(path_len);
1046
1096
  if (path === 0)
1047
1097
  return WasiErrno.EINVAL;
1048
- if ((fst_flags) & ~(WasiFstFlag.SET_ATIM |
1049
- WasiFstFlag.SET_ATIM_NOW |
1050
- WasiFstFlag.SET_MTIM |
1051
- WasiFstFlag.SET_MTIM_NOW)) {
1052
- return WasiErrno.EINVAL;
1053
- }
1054
1098
  const { HEAPU8 } = getMemory(this);
1055
1099
  const wasi = _wasi.get(this);
1056
1100
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1101
+ if (validateFstFlagsOrReturn(fst_flags)) {
1102
+ return WasiErrno.EINVAL;
1103
+ }
1057
1104
  const fs = getFs(this);
1058
1105
  const resolvedPath = resolvePathSync(fs, fileDescriptor, decoder.decode(unsharedSlice(HEAPU8, path, path + path_len)), flags);
1059
1106
  if ((fst_flags & WasiFstFlag.SET_ATIM_NOW) === WasiFstFlag.SET_ATIM_NOW) {
@@ -1069,15 +1116,12 @@ export class WASI {
1069
1116
  path_len = Number(path_len);
1070
1117
  if (path === 0)
1071
1118
  return WasiErrno.EINVAL;
1072
- if ((fst_flags) & ~(WasiFstFlag.SET_ATIM |
1073
- WasiFstFlag.SET_ATIM_NOW |
1074
- WasiFstFlag.SET_MTIM |
1075
- WasiFstFlag.SET_MTIM_NOW)) {
1076
- return WasiErrno.EINVAL;
1077
- }
1078
1119
  const { HEAPU8 } = getMemory(this);
1079
1120
  const wasi = _wasi.get(this);
1080
1121
  const fileDescriptor = wasi.fds.get(fd, WasiRights.PATH_FILESTAT_SET_TIMES, BigInt(0));
1122
+ if (validateFstFlagsOrReturn(fst_flags)) {
1123
+ return WasiErrno.EINVAL;
1124
+ }
1081
1125
  const fs = getFs(this);
1082
1126
  const resolvedPath = await resolvePathAsync(fs, fileDescriptor, decoder.decode(unsharedSlice(HEAPU8, path, path + path_len)), flags);
1083
1127
  if ((fst_flags & WasiFstFlag.SET_ATIM_NOW) === WasiFstFlag.SET_ATIM_NOW) {
@@ -1276,7 +1320,7 @@ export class WASI {
1276
1320
  return WasiErrno.ENOBUFS;
1277
1321
  HEAPU8.set(linkData.subarray(0, len), buf);
1278
1322
  HEAPU8[buf + len] = 0;
1279
- view.setUint32(bufused, len + 1, true);
1323
+ view.setUint32(bufused, len, true);
1280
1324
  return WasiErrno.ESUCCESS;
1281
1325
  }, async function path_readlink(fd, path, path_len, buf, buf_len, bufused) {
1282
1326
  path = Number(path);
@@ -1300,7 +1344,7 @@ export class WASI {
1300
1344
  return WasiErrno.ENOBUFS;
1301
1345
  HEAPU8.set(linkData.subarray(0, len), buf);
1302
1346
  HEAPU8[buf + len] = 0;
1303
- view.setUint32(bufused, len + 1, true);
1347
+ view.setUint32(bufused, len, true);
1304
1348
  return WasiErrno.ESUCCESS;
1305
1349
  }, ['i32', 'i32', 'i32', 'i32', 'i32', 'i32'], ['i32']);
1306
1350
  defineImport('path_remove_directory', function path_remove_directory(fd, path, path_len) {
@@ -28,7 +28,8 @@ export const RIGHTS_ALL = WasiRights.FD_DATASYNC |
28
28
  WasiRights.PATH_UNLINK_FILE |
29
29
  WasiRights.PATH_REMOVE_DIRECTORY |
30
30
  WasiRights.POLL_FD_READWRITE |
31
- WasiRights.SOCK_SHUTDOWN;
31
+ WasiRights.SOCK_SHUTDOWN |
32
+ WasiRights.SOCK_ACCEPT;
32
33
  export const BLOCK_DEVICE_BASE = RIGHTS_ALL;
33
34
  export const BLOCK_DEVICE_INHERITING = RIGHTS_ALL;
34
35
  export const CHARACTER_DEVICE_BASE = RIGHTS_ALL;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tybys/wasm-util",
3
- "version": "0.8.3",
3
+ "version": "0.9.0",
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",