Skip to content

Commit 09e9050

Browse files
authored
Rename errno constants to use the wasi values (#9545)
Redefine errno values to be consistent with wasi. This will let us avoid needing to convert the values back and forth as we use more wasi APIs (which I experiemented with and it adds 1K or so). This is an ABI change, which should not be noticeable from user code unless you use errno defines (like EAGAIN) and keep around binaries compiled with an older version that you link against. In that case, you should rebuild them. Fix NODEFS, which basically just routed the node errno code to ours, which only worked when the underlying system had codes similar to musl - which seems to have been the case on some linuxes. This does make NODEFS larger, as it uses the ERRNO lookup table now, I'll look into a way to avoid including it unnecessarily as a followup (would be a breaking change). Most of the changes in this PR are test changes, places where we printed raw errno codes.
1 parent f02ba54 commit 09e9050

25 files changed

+377
-353
lines changed

ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ See docs/process.md for how version tagging works.
1818

1919
Current Trunk
2020
-------------
21+
- Redefine errno values to be consistent with wasi. This will let us avoid
22+
needing to convert the values back and forth as we use more wasi APIs.
23+
This is an ABI change, which should not be noticeable from user code
24+
unless you use errno defines (like EAGAIN) *and* keep around binaries
25+
compiled with an older version that you link against. In that case, you
26+
should rebuild them. See #9545.
2127

2228
v.1.38.46: 09/25/2019
2329
---------------------

src/library_nodefs.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// found in the LICENSE file.
55

66
mergeInto(LibraryManager.library, {
7-
$NODEFS__deps: ['$FS', '$PATH'],
7+
$NODEFS__deps: ['$FS', '$PATH', '$ERRNO_CODES'],
88
$NODEFS__postset: 'if (ENVIRONMENT_HAS_NODE) { var fs = require("fs"); var NODEJS_PATH = require("path"); NODEFS.staticInit(); }',
99
$NODEFS: {
1010
isWindows: false,
@@ -32,6 +32,11 @@ mergeInto(LibraryManager.library, {
3232
// Buffer.alloc has been added with Buffer.from together, so check it instead
3333
return Buffer["alloc"] ? Buffer.from(arrayBuffer) : new Buffer(arrayBuffer);
3434
},
35+
convertNodeCode: function(e) {
36+
var code = e.code;
37+
assert(code in ERRNO_CODES);
38+
return ERRNO_CODES[code];
39+
},
3540
mount: function (mount) {
3641
assert(ENVIRONMENT_HAS_NODE);
3742
return NODEFS.createNode(null, '/', NODEFS.getMode(mount.opts.root), 0);
@@ -56,7 +61,7 @@ mergeInto(LibraryManager.library, {
5661
}
5762
} catch (e) {
5863
if (!e.code) throw e;
59-
throw new FS.ErrnoError(-e.errno); // syscall errnos are negated, node's are not
64+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
6065
}
6166
return stat.mode;
6267
},
@@ -99,7 +104,7 @@ mergeInto(LibraryManager.library, {
99104
stat = fs.lstatSync(path);
100105
} catch (e) {
101106
if (!e.code) throw e;
102-
throw new FS.ErrnoError(-e.errno);
107+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
103108
}
104109
// node.js v0.10.20 doesn't report blksize and blocks on Windows. Fake them with default blksize of 4096.
105110
// See http://support.microsoft.com/kb/140365
@@ -142,7 +147,7 @@ mergeInto(LibraryManager.library, {
142147
}
143148
} catch (e) {
144149
if (!e.code) throw e;
145-
throw new FS.ErrnoError(-e.errno);
150+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
146151
}
147152
},
148153
lookup: function (parent, name) {
@@ -162,7 +167,7 @@ mergeInto(LibraryManager.library, {
162167
}
163168
} catch (e) {
164169
if (!e.code) throw e;
165-
throw new FS.ErrnoError(-e.errno);
170+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
166171
}
167172
return node;
168173
},
@@ -173,7 +178,7 @@ mergeInto(LibraryManager.library, {
173178
fs.renameSync(oldPath, newPath);
174179
} catch (e) {
175180
if (!e.code) throw e;
176-
throw new FS.ErrnoError(-e.errno);
181+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
177182
}
178183
},
179184
unlink: function(parent, name) {
@@ -182,7 +187,7 @@ mergeInto(LibraryManager.library, {
182187
fs.unlinkSync(path);
183188
} catch (e) {
184189
if (!e.code) throw e;
185-
throw new FS.ErrnoError(-e.errno);
190+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
186191
}
187192
},
188193
rmdir: function(parent, name) {
@@ -191,7 +196,7 @@ mergeInto(LibraryManager.library, {
191196
fs.rmdirSync(path);
192197
} catch (e) {
193198
if (!e.code) throw e;
194-
throw new FS.ErrnoError(-e.errno);
199+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
195200
}
196201
},
197202
readdir: function(node) {
@@ -200,7 +205,7 @@ mergeInto(LibraryManager.library, {
200205
return fs.readdirSync(path);
201206
} catch (e) {
202207
if (!e.code) throw e;
203-
throw new FS.ErrnoError(-e.errno);
208+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
204209
}
205210
},
206211
symlink: function(parent, newName, oldPath) {
@@ -209,7 +214,7 @@ mergeInto(LibraryManager.library, {
209214
fs.symlinkSync(oldPath, newPath);
210215
} catch (e) {
211216
if (!e.code) throw e;
212-
throw new FS.ErrnoError(-e.errno);
217+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
213218
}
214219
},
215220
readlink: function(node) {
@@ -220,7 +225,7 @@ mergeInto(LibraryManager.library, {
220225
return path;
221226
} catch (e) {
222227
if (!e.code) throw e;
223-
throw new FS.ErrnoError(-e.errno);
228+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
224229
}
225230
},
226231
},
@@ -233,7 +238,7 @@ mergeInto(LibraryManager.library, {
233238
}
234239
} catch (e) {
235240
if (!e.code) throw e;
236-
throw new FS.ErrnoError(-e.errno);
241+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
237242
}
238243
},
239244
close: function (stream) {
@@ -243,7 +248,7 @@ mergeInto(LibraryManager.library, {
243248
}
244249
} catch (e) {
245250
if (!e.code) throw e;
246-
throw new FS.ErrnoError(-e.errno);
251+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
247252
}
248253
},
249254
read: function (stream, buffer, offset, length, position) {
@@ -252,14 +257,14 @@ mergeInto(LibraryManager.library, {
252257
try {
253258
return fs.readSync(stream.nfd, NODEFS.bufferFrom(buffer.buffer), offset, length, position);
254259
} catch (e) {
255-
throw new FS.ErrnoError(-e.errno);
260+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
256261
}
257262
},
258263
write: function (stream, buffer, offset, length, position) {
259264
try {
260265
return fs.writeSync(stream.nfd, NODEFS.bufferFrom(buffer.buffer), offset, length, position);
261266
} catch (e) {
262-
throw new FS.ErrnoError(-e.errno);
267+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
263268
}
264269
},
265270
llseek: function (stream, offset, whence) {
@@ -272,7 +277,7 @@ mergeInto(LibraryManager.library, {
272277
var stat = fs.fstatSync(stream.nfd);
273278
position += stat.size;
274279
} catch (e) {
275-
throw new FS.ErrnoError(-e.errno);
280+
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
276281
}
277282
}
278283
}

system/lib/compiler-rt/lib/sanitizer_common/sanitizer_errno_codes.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
#ifndef SANITIZER_ERRNO_CODES_H
2121
#define SANITIZER_ERRNO_CODES_H
2222

23+
// XXX EMSCRIPTEN: use wasi errno codes, which is what our musl port now uses
24+
#include <wasi/wasi.h>
25+
2326
namespace __sanitizer {
2427

25-
#define errno_ENOMEM 12
26-
#define errno_EBUSY 16
27-
#define errno_EINVAL 22
28+
#define errno_ENOMEM __WASI_ENOMEM
29+
#define errno_EBUSY __WASI_EBUSY
30+
#define errno_EINVAL __WASI_EINVAL
2831

2932
// Those might not present or their value differ on different platforms.
3033
extern const int errno_EOWNERDEAD;

0 commit comments

Comments
 (0)