Skip to content
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
12 changes: 4 additions & 8 deletions src/rust/jsg-bench/bench.c++
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ class Context final: public Object, public ContextGlobal {
JSG_DECLARE_ISOLATE_TYPE(BenchIsolate, Context, Target, Node, Pair, Octet);
JSG_DECLARE_DEBUG_ISOLATE_TYPE(VerifyFastIsolate, Context, Target, Node, Pair, Octet);

V8System& system() {
static V8System system({"--allow-natives-syntax"_kj, "--expose-gc"_kj});
return system;
}
// Constructed during static initialization so that V8 starts up, and is torn down, on the main
// thread, like every JSG test and benchmark. V8 grants access to the memory protection key that
// guards its JIT memory only to the thread that starts it and the threads that thread creates.
V8System v8System({"--allow-natives-syntax"_kj, "--expose-gc"_kj});

enum class Binding { CPP, RUST, CPP_FAST, RUST_FAST, CPP_STRUCT_FAST, RUST_STRUCT_FAST };
enum class CpuTime { THREAD, PROCESS };
Expand Down Expand Up @@ -298,8 +298,6 @@ void runCase(benchmark::State& state, Binding binding, const Case& test) {
binding == Binding::RUST_STRUCT_FAST;
JsgConfig config = {
.fastApiEnabled = binding == Binding::CPP_FAST || binding == Binding::RUST_FAST};
// Initializes V8 before newIsolateGroup() needs it; argument evaluation order is unspecified.
auto& v8System = system();
Isolate isolate(v8System, newIsolateGroup(), config, kj::heap<IsolateObserver>());
isolate.runInLockScope([&](auto& js) {
if (binding == Binding::CPP_STRUCT_FAST) js.setUsingFastJsgStruct();
Expand Down Expand Up @@ -408,8 +406,6 @@ void runCase(benchmark::State& state, Binding binding, const Case& test) {
// Runs `func` with an isolate locked, a Rust realm attached and a context entered, as creating
// a Rust resource requires.
void withRealm(auto func) {
// Initializes V8 before newIsolateGroup() needs it; argument evaluation order is unspecified.
auto& v8System = system();
JsgConfig config = {};
BenchIsolate isolate(v8System, newIsolateGroup(), config, kj::heap<IsolateObserver>());
isolate.runInLockScope([&](auto& js) {
Expand Down
32 changes: 23 additions & 9 deletions src/rust/jsg-test/ffi.c++
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,20 @@ JSG_DECLARE_ISOLATE_TYPE(TestIsolate,

namespace rust::jsg_test {

// Lazy initialization of V8System to avoid static initialization order issues
static ::workerd::jsg::V8System& getV8System() {
// Lets tests optimize functions on demand to reach fast callbacks.
static ::workerd::jsg::V8System v8System({"--allow-natives-syntax"});
return v8System;
}
namespace {
// Constructed during static initialization so that V8 starts up on the main thread, before the
// Rust test harness spawns any test thread, and is torn down on that same thread at exit. On
// Linux x86-64 V8 protects JIT memory, such as the WebAssembly code pointer table, with a memory
// protection key that it allocates as the platform is created. Only the allocating thread gets
// access to the key, and threads inherit their access from the thread that creates them, so V8
// must start before any thread that will use it. `--allow-natives-syntax` lets tests optimize
// functions on demand to reach fast callbacks.
::workerd::jsg::V8System v8System({"--allow-natives-syntax"});
} // namespace

TestHarness::TestHarness(
::workerd::jsg::V8StackScope&, ::rust::Slice<const uint8_t> flags, FastApi fastApi)
: isolate(kj::heap<TestIsolate>(getV8System(), kj::heap<::workerd::jsg::IsolateObserver>())),
: isolate(kj::heap<TestIsolate>(v8System, kj::heap<::workerd::jsg::IsolateObserver>())),
locker(isolate->getIsolate()),
isolateScope(isolate->getIsolate()),
realm(::workerd::rust::jsg::realm_create(isolate->getIsolate(), flags)) {
Expand Down Expand Up @@ -262,7 +266,7 @@ void TestHarness::run_in_context(
// As Worker::Worker does in the zygote: the serializer callbacks are registered with the
// context, the top level runs, and the context handle and the Rust realm's templates are
// handed to createSnapshotBlob().
auto zygote = kj::heap<TestIsolate>(getV8System(), ::workerd::jsg::newIsolateGroup(), nullptr,
auto zygote = kj::heap<TestIsolate>(v8System, ::workerd::jsg::newIsolateGroup(), nullptr,
kj::heap<::workerd::jsg::IsolateObserver>(),
::workerd::jsg::defaultExternalStringAllocator(), v8::Isolate::CreateParams{}, true,
::workerd::jsg::SnapshotConfig(::workerd::jsg::MutableSnapshot{
Expand Down Expand Up @@ -300,7 +304,7 @@ void TestHarness::run_in_context(
artifact = zygote->extractSnapshotArtifact();
}

auto isolate = kj::heap<TestIsolate>(getV8System(), ::workerd::jsg::newIsolateGroup(), nullptr,
auto isolate = kj::heap<TestIsolate>(v8System, ::workerd::jsg::newIsolateGroup(), nullptr,
kj::heap<::workerd::jsg::IsolateObserver>(),
::workerd::jsg::defaultExternalStringAllocator(), v8::Isolate::CreateParams{}, true,
::workerd::jsg::SnapshotConfig(
Expand Down Expand Up @@ -524,6 +528,16 @@ void set_javascript_execution_disallowed(Isolate* isolate, bool disallowed) {
::workerd::jsg::Lock::from(isolate).setDisallowJavascriptExecution(disallowed);
}

void set_allow_eval(Isolate* isolate, bool allow) {
::workerd::jsg::Lock::from(isolate).setAllowEval(allow);
}

::workerd::rust::jsg::Local wasm_module_object_from_compiled_module(
Isolate* isolate, kj::Own<::workerd::rust::jsg::CompiledWasmModule> module) {
return ::workerd::rust::jsg::to_ffi(
::workerd::jsg::check(v8::WasmModuleObject::FromCompiledModule(isolate, *module)));
}

void set_error_slots(
Isolate* isolate, ::workerd::rust::jsg::Local object, ::rust::Str name, ::rust::Str message) {
auto& js = ::workerd::jsg::Lock::from(isolate);
Expand Down
8 changes: 8 additions & 0 deletions src/rust/jsg-test/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ ::workerd::rust::jsg::Local private_symbol(Isolate* isolate, ::rust::Str name);
// own guard, or allows it again.
void set_javascript_execution_disallowed(Isolate* isolate, bool disallowed);

// Allows or disallows eval() and WebAssembly compilation, as jsg::Lock::setAllowEval() does.
void set_allow_eval(Isolate* isolate, bool allow);

// Makes a WebAssembly.Module from `module` without recompiling it, as C++ code that Rust hands a
// compiled module to does. Throws a KJ exception if V8 creates none.
::workerd::rust::jsg::Local wasm_module_object_from_compiled_module(
Isolate* isolate, kj::Own<::workerd::rust::jsg::CompiledWasmModule> module);

// Gives `object` `name` and `message` under jsg::ERROR_NAME_KEY and jsg::ERROR_MESSAGE_KEY, as an
// error implemented in the per-isolate bootstrap holds them. Throws a KJ exception if it is not
// an object.
Expand Down
48 changes: 48 additions & 0 deletions src/rust/jsg-test/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod ffi {
type Local = jsg::v8::ffi::Local;
type MaybeLocal = jsg::v8::ffi::MaybeLocal;
type ModuleRegistry = jsg::v8::ffi::ModuleRegistry;
type CompiledWasmModule = jsg::v8::ffi::CompiledWasmModule;
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -217,6 +218,20 @@ mod ffi {
disallowed: bool,
) -> Result<()>;

/// Allows or disallows dynamic code evaluation, WebAssembly compilation included.
#[expect(clippy::allow_attributes)]
#[allow(dead_code)]
pub unsafe fn set_allow_eval(isolate: *mut Isolate, allow: bool);

/// Makes a `WebAssembly.Module` from `module` without recompiling it, as C++ code
/// that the module is handed to does. Fails if V8 creates none.
#[expect(clippy::allow_attributes)]
#[allow(dead_code)]
pub unsafe fn wasm_module_object_from_compiled_module(
isolate: *mut Isolate,
module: KjOwn<CompiledWasmModule>,
) -> Result<Local>;

/// Gives `object` `name` and `message` as an error implemented in the per-isolate
/// bootstrap holds them. Fails if it is not an object.
#[expect(clippy::allow_attributes)]
Expand Down Expand Up @@ -842,6 +857,39 @@ impl Harness {
.expect("JavaScript execution should have been disallowed");
}

/// Allows or disallows dynamic code evaluation (`eval()`, `new Function()` and WebAssembly
/// compilation), as C++ JSG's `jsg::Lock::setAllowEval()` does. The harness disallows it.
pub fn set_allow_eval(lock: &mut jsg::Lock<'_>, allow: bool) {
// SAFETY: `lock` proves the isolate is locked.
unsafe { ffi::set_allow_eval(lock.isolate().as_ffi(), allow) }
}

/// Makes a `WebAssembly.Module` from `module` in C++, without recompiling it, as C++ code
/// that Rust hands a [`v8::CompiledWasmModule`] to does.
///
/// # Panics
///
/// Panics if V8 creates no module object.
pub fn wasm_module_object_from_compiled_module<'s>(
lock: &mut jsg::Lock<'s>,
module: v8::CompiledWasmModule,
) -> v8::Local<'s, v8::WasmModuleObject> {
// SAFETY: `lock` proves the isolate is locked, a context is entered and a HandleScope is
// open, which the result belongs to.
let local = unsafe {
v8::Local::<v8::Value>::from_ffi(
ffi::wasm_module_object_from_compiled_module(
lock.isolate().as_ffi(),
module.into_ffi(),
)
.expect("V8 should create the module object"),
)
};
local
.try_into()
.expect("the result should be a WebAssembly.Module")
}

/// Gives `object` `name` and `message` as an error implemented in the per-isolate bootstrap,
/// whose `name` and `message` are accessors, holds them: under the private symbols that C++
/// JSG's `ERROR_NAME_KEY` and `ERROR_MESSAGE_KEY` name, which
Expand Down
1 change: 1 addition & 0 deletions src/rust/jsg-test/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ mod traced_global_lifetime;
mod tunneled_exception;
mod typed_promise;
mod unwrap;
mod wasm_module_object;
mod weak_condemned;
mod wildcard_property;
Loading
Loading