Skip to content

Add support for getentropy in d8 #24185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/closure-externs/closure-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,6 @@ Navigator.prototype.webkitGetUserMedia = function(
* @type {symbol}
*/
Symbol.dispose;

// Common between node-externs and v8-externs
var os = {};
7 changes: 7 additions & 0 deletions src/closure-externs/v8-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ var scriptArgs = [];
* @suppress {duplicate}
*/
var quit = function(status) {};

/**
* @param {string} cmd
* @param {Array.<string>=} args
* @return {string}
*/
os.system = function (cmd, args) {};
15 changes: 15 additions & 0 deletions src/lib/libwasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,9 @@ var WasiLibrary = {

// random.h

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

#if ENVIRONMENT_MAY_BE_SHELL
if (ENVIRONMENT_IS_SHELL) {
return (view) => {
if (!os.system) {
throw new Error('randomFill not supported on d8 unless --enable-os-system is passed');
}
const b64 = os.system('sh', ['-c', `head -c${view.byteLength} /dev/urandom | base64 --wrap=0`]);
view.set(base64Decode(b64));
};
}
#endif

#if SHARED_MEMORY
// like with most Web APIs, we can't use Web Crypto API directly on shared memory,
// so we need to create an intermediate buffer and copy it to the destination
Expand Down
2 changes: 1 addition & 1 deletion src/modules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function calculateLibraries() {
libraries.push('libmemoryprofiler.js');
}

if (SUPPORT_BASE64_EMBEDDING) {
if (SUPPORT_BASE64_EMBEDDING || ENVIRONMENT_MAY_BE_SHELL) {
libraries.push('libbase64.js');
}

Expand Down
18 changes: 18 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -16028,3 +16028,21 @@ def test_js_base64_api(self):
baseline_size = os.path.getsize('baseline.js')
js_api_size = os.path.getsize('hello_world.js')
self.assertLess(js_api_size, baseline_size)

@requires_v8
def test_getentropy_d8(self):
create_file('main.c', '''
#include <assert.h>
#include <unistd.h>

int main() {
char buf[100];
assert(getentropy(buf, sizeof(buf)) == 0);
return 0;
}
''')

msg = 'randomFill not supported on d8 unless --enable-os-system is passed'
self.do_runf('main.c', msg, assert_returncode=1)
self.v8_args += ['--enable-os-system']
self.do_runf('main.c')
1 change: 0 additions & 1 deletion third_party/closure-compiler/node-externs/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
END_NODE_INCLUDE
*/

var os = {};

/**
* @return {string}
Expand Down