Skip to content

Commit 5cdadd6

Browse files
authored
Add support for getentropy in d8 (#24185)
Use `os.system` to read from `/dev/urandom` and then base 64 encode the result. Then use `base64Decode` to read the result. Only works when `--enable-os-system` is passed to d8.
1 parent 76860cc commit 5cdadd6

File tree

6 files changed

+44
-2
lines changed

6 files changed

+44
-2
lines changed

src/closure-externs/closure-externs.js

+3
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,6 @@ Navigator.prototype.webkitGetUserMedia = function(
268268
* @type {symbol}
269269
*/
270270
Symbol.dispose;
271+
272+
// Common between node-externs and v8-externs
273+
var os = {};

src/closure-externs/v8-externs.js

+7
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ var scriptArgs = [];
3232
* @suppress {duplicate}
3333
*/
3434
var quit = function(status) {};
35+
36+
/**
37+
* @param {string} cmd
38+
* @param {Array.<string>=} args
39+
* @return {string}
40+
*/
41+
os.system = function (cmd, args) {};

src/lib/libwasi.js

+15
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,9 @@ var WasiLibrary = {
567567

568568
// random.h
569569

570+
#if ENVIRONMENT_MAY_BE_SHELL
571+
$initRandomFill__deps: ['$base64Decode'],
572+
#endif
570573
$initRandomFill: () => {
571574
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
572575
// This block is not needed on v19+ since crypto.getRandomValues is builtin
@@ -576,6 +579,18 @@ var WasiLibrary = {
576579
}
577580
#endif // ENVIRONMENT_MAY_BE_NODE
578581

582+
#if ENVIRONMENT_MAY_BE_SHELL
583+
if (ENVIRONMENT_IS_SHELL) {
584+
return (view) => {
585+
if (!os.system) {
586+
throw new Error('randomFill not supported on d8 unless --enable-os-system is passed');
587+
}
588+
const b64 = os.system('sh', ['-c', `head -c${view.byteLength} /dev/urandom | base64 --wrap=0`]);
589+
view.set(base64Decode(b64));
590+
};
591+
}
592+
#endif
593+
579594
#if SHARED_MEMORY
580595
// like with most Web APIs, we can't use Web Crypto API directly on shared memory,
581596
// so we need to create an intermediate buffer and copy it to the destination

src/modules.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function calculateLibraries() {
7777
libraries.push('libmemoryprofiler.js');
7878
}
7979

80-
if (SUPPORT_BASE64_EMBEDDING) {
80+
if (SUPPORT_BASE64_EMBEDDING || ENVIRONMENT_MAY_BE_SHELL) {
8181
libraries.push('libbase64.js');
8282
}
8383

test/test_other.py

+18
Original file line numberDiff line numberDiff line change
@@ -16026,3 +16026,21 @@ def test_js_base64_api(self):
1602616026
baseline_size = os.path.getsize('baseline.js')
1602716027
js_api_size = os.path.getsize('hello_world.js')
1602816028
self.assertLess(js_api_size, baseline_size)
16029+
16030+
@requires_v8
16031+
def test_getentropy_d8(self):
16032+
create_file('main.c', '''
16033+
#include <assert.h>
16034+
#include <unistd.h>
16035+
16036+
int main() {
16037+
char buf[100];
16038+
assert(getentropy(buf, sizeof(buf)) == 0);
16039+
return 0;
16040+
}
16041+
''')
16042+
16043+
msg = 'randomFill not supported on d8 unless --enable-os-system is passed'
16044+
self.do_runf('main.c', msg, assert_returncode=1)
16045+
self.v8_args += ['--enable-os-system']
16046+
self.do_runf('main.c')

third_party/closure-compiler/node-externs/os.js

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
END_NODE_INCLUDE
2828
*/
2929

30-
var os = {};
3130

3231
/**
3332
* @return {string}

0 commit comments

Comments
 (0)