|
| 1 | +/* |
| 2 | + * Copyright 2024 The Emscripten Authors. All rights reserved. |
| 3 | + * Emscripten is available under two separate licenses, the MIT license and the |
| 4 | + * University of Illinois/NCSA Open Source License. Both these licenses can be |
| 5 | + * found in the LICENSE file. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <assert.h> |
| 9 | +#include <fcntl.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <string.h> |
| 12 | +#include <sys/stat.h> |
| 13 | +#include <sys/uio.h> |
| 14 | +#include <unistd.h> |
| 15 | +#include <emscripten.h> |
| 16 | + |
| 17 | +EM_JS_DEPS(main, "$ERRNO_CODES"); |
| 18 | + |
| 19 | +void setup(void) { |
| 20 | + EM_ASM( |
| 21 | + var device = FS.makedev(80, 0); |
| 22 | + FS.registerDevice(device, { |
| 23 | + write: function(stream, buffer, offset, length, pos) { |
| 24 | + if (length === 0) { |
| 25 | + return 0; |
| 26 | + } |
| 27 | + // Only do a partial write of one byte each time |
| 28 | + out('TO DEVICE: ' + JSON.stringify(String.fromCharCode(buffer[offset]))); |
| 29 | + return 1; |
| 30 | + } |
| 31 | + }); |
| 32 | + FS.mkdev('/device', device); |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +// Run the test with writev directly |
| 37 | +void test_writev_direct(void) { |
| 38 | + int fd = open("/device", O_WRONLY); |
| 39 | + assert(fd); |
| 40 | + struct iovec iovs[] = {{.iov_base = "ABC", .iov_len = 3}, |
| 41 | + {.iov_base = "XYZ", .iov_len = 3}}; |
| 42 | + struct iovec* iov = iovs; |
| 43 | + |
| 44 | + size_t rem = iov[0].iov_len + iov[1].iov_len; |
| 45 | + int iovcnt = 2; |
| 46 | + ssize_t cnt; |
| 47 | + for (;;) { |
| 48 | + cnt = writev(fd, iov, iovcnt); |
| 49 | + assert(cnt >= 0); |
| 50 | + if (cnt == rem) { |
| 51 | + // All data written |
| 52 | + break; |
| 53 | + } |
| 54 | + rem -= cnt; |
| 55 | + if (cnt > iov[0].iov_len) { |
| 56 | + cnt -= iov[0].iov_len; |
| 57 | + iov++; |
| 58 | + iovcnt--; |
| 59 | + } |
| 60 | + iov[0].iov_base = (char*)iov[0].iov_base + cnt; |
| 61 | + iov[0].iov_len -= cnt; |
| 62 | + } |
| 63 | + |
| 64 | + close(fd); |
| 65 | +} |
| 66 | + |
| 67 | +// Run the test using stdio, this test is dependent on specific buffering used |
| 68 | +// and is included here as this code most closely matches the original bug |
| 69 | +// report |
| 70 | +void test_via_stdio(void) { |
| 71 | + // XXX: We open in append mode because truncating JS based files is not |
| 72 | + // supported yet. See #22262 |
| 73 | +#ifdef WASMFS |
| 74 | + FILE* f = fopen("/device", "a"); |
| 75 | +#else |
| 76 | + FILE* f = fopen("/device", "w"); |
| 77 | +#endif |
| 78 | + assert(f); |
| 79 | + // Use line buffering. The bug is exposed with line buffering because with |
| 80 | + // line buffering two entries in __stdio_write's iovs are used. |
| 81 | + setvbuf(f, NULL, _IOLBF, 0); |
| 82 | + fputs("abc", f); |
| 83 | + fputs("\n", f); |
| 84 | + fflush(f); |
| 85 | + fclose(f); |
| 86 | +} |
| 87 | + |
| 88 | +int main() { |
| 89 | + setup(); |
| 90 | + test_writev_direct(); |
| 91 | + test_via_stdio(); |
| 92 | + printf("done\n"); |
| 93 | + return 0; |
| 94 | +} |
0 commit comments