Skip to content

Commit 4a73c46

Browse files
committed
Add support for getentropy in d8
1 parent 50532ec commit 4a73c46

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

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
@@ -78,7 +78,7 @@ function calculateLibraries() {
7878
libraries.push('libmemoryprofiler.js');
7979
}
8080

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

test/test_other.py

+17
Original file line numberDiff line numberDiff line change
@@ -16016,3 +16016,20 @@ def test_locate_file_abspath_esm(self, args):
1601616016
output_suffix='.mjs',
1601716017
emcc_args=['--pre-js', 'pre.js',
1601816018
'--extern-post-js', test_file('modularize_post_js.js')] + args)
16019+
@requires_v8
16020+
def test_getentropy_d8(self):
16021+
create_file('main.c', '''
16022+
#include "assert.h"
16023+
#include <unistd.h>
16024+
16025+
int main() {
16026+
char buf[100];
16027+
assert(getentropy(buf, sizeof(buf)) == 0);
16028+
return 0;
16029+
}
16030+
''')
16031+
16032+
msg = 'randomFill not supported on d8 unless --enable-os-system is passed'
16033+
self.do_runf('main.c', msg, assert_returncode=1)
16034+
self.v8_args += ['--enable-os-system']
16035+
self.do_runf('main.c')

0 commit comments

Comments
 (0)