Skip to content

Commit 300e4d8

Browse files
committed
Improving the implementation of DisallowJavascriptExecutionScope.
1 parent 7f9d117 commit 300e4d8

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

graal-nodejs/deps/v8/src/graal/graal_function.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ v8::Local<v8::Value> GraalFunction::Call(v8::Local<v8::Value> recv, int argc, v8
9292
reinterpret_cast<GraalIsolate*> (GraalIsolate::GetCurrent())->ReportAPIFailure(
9393
"v8::Function::Call", "Function to be called is a null pointer");
9494
}
95-
if (!Isolate()->GetJSExecutionAllowed()) {
95+
if (!Isolate()->CheckJSExecutionAllowed()) {
9696
return nullptr;
9797
}
9898
jobject java_receiver = reinterpret_cast<GraalValue*> (*recv)->GetJavaObject();

graal-nodejs/deps/v8/src/graal/graal_isolate.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,3 +1518,11 @@ void GraalIsolate::RequestInterrupt(v8::InterruptCallback callback, void* data)
15181518
jvm_->DetachCurrentThread();
15191519
}
15201520
}
1521+
1522+
void GraalIsolate::JSExecutionViolation(JSExecutionAction action) {
1523+
if (action == kJSExecutionThrow) {
1524+
ThrowException(v8::String::NewFromUtf8(reinterpret_cast<v8::Isolate*> (this), "Illegal operation.").ToLocalChecked());
1525+
} else {
1526+
ReportAPIFailure("DisallowJavascriptExecutionScope", "Illegal operation.");
1527+
}
1528+
}

graal-nodejs/deps/v8/src/graal/graal_isolate.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -705,16 +705,29 @@ class GraalIsolate {
705705
return &microtask_queue_;
706706
}
707707

708-
inline bool SetJSExecutionAllowed(bool allowed) {
709-
bool old = js_execution_allowed_;
710-
js_execution_allowed_ = allowed;
708+
enum JSExecutionAction {
709+
kJSExecutionAllowed,
710+
kJSExecutionThrow,
711+
kJSExecutionCrash,
712+
};
713+
714+
inline JSExecutionAction SetJSExecutionAction(JSExecutionAction action) {
715+
JSExecutionAction old = js_execution_action_;
716+
js_execution_action_ = action;
711717
return old;
712718
}
713719

714-
inline bool GetJSExecutionAllowed() {
715-
return js_execution_allowed_;
720+
inline bool CheckJSExecutionAllowed() {
721+
if (js_execution_action_ == kJSExecutionAllowed) {
722+
return true;
723+
} else {
724+
JSExecutionViolation(js_execution_action_);
725+
return false;
726+
}
716727
}
717728

729+
void JSExecutionViolation(JSExecutionAction action);
730+
718731
static void SetFlags(int argc, char** argv) {
719732
char** old_argv = GraalIsolate::argv;
720733
int old_argc = GraalIsolate::argc;
@@ -795,7 +808,7 @@ class GraalIsolate {
795808
intptr_t stack_bottom_;
796809
size_t stack_size_limit_;
797810
bool main_;
798-
bool js_execution_allowed_ = true;
811+
JSExecutionAction js_execution_action_ = kJSExecutionAllowed;
799812
double return_value_;
800813
static bool abort_on_uncaught_exception_;
801814
static bool internal_error_check_;

graal-nodejs/deps/v8/src/graal/graal_object.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@ v8::Local<v8::Object> GraalObject::New(v8::Isolate* isolate) {
7070
}
7171

7272
bool GraalObject::Set(v8::Local<v8::Value> key, v8::Local<v8::Value> value) {
73+
GraalIsolate* graal_isolate = Isolate();
74+
if (!graal_isolate->CheckJSExecutionAllowed()) {
75+
return false;
76+
}
7377
jobject java_key = reinterpret_cast<GraalValue*> (*key)->GetJavaObject();
7478
jobject java_value = reinterpret_cast<GraalValue*> (*value)->GetJavaObject();
75-
JNI_CALL(bool, success, Isolate(), GraalAccessMethod::object_set, Boolean, GetJavaObject(), java_key, java_value);
79+
JNI_CALL(bool, success, graal_isolate, GraalAccessMethod::object_set, Boolean, GetJavaObject(), java_key, java_value);
7680
return success;
7781
}
7882

graal-nodejs/deps/v8/src/graal/v8.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,20 +2885,27 @@ namespace v8 {
28852885

28862886
Isolate::DisallowJavascriptExecutionScope::DisallowJavascriptExecutionScope(Isolate* isolate, OnFailure on_failure) {
28872887
isolate_ = isolate;
2888-
was_execution_allowed_throws_ = reinterpret_cast<GraalIsolate*> (isolate)->SetJSExecutionAllowed(false);
2888+
GraalIsolate::JSExecutionAction newAction = (on_failure == THROW_ON_FAILURE) ? GraalIsolate::kJSExecutionThrow : GraalIsolate::kJSExecutionCrash;
2889+
GraalIsolate::JSExecutionAction oldAction = reinterpret_cast<GraalIsolate*> (isolate)->SetJSExecutionAction(newAction);
2890+
was_execution_allowed_throws_ = (oldAction == GraalIsolate::kJSExecutionThrow);
2891+
was_execution_allowed_assert_ = (oldAction == GraalIsolate::kJSExecutionCrash);
28892892
}
28902893

28912894
Isolate::DisallowJavascriptExecutionScope::~DisallowJavascriptExecutionScope() {
2892-
reinterpret_cast<GraalIsolate*> (isolate_)->SetJSExecutionAllowed(was_execution_allowed_throws_);
2895+
GraalIsolate::JSExecutionAction action = was_execution_allowed_throws_ ? GraalIsolate::kJSExecutionThrow : (was_execution_allowed_assert_ ? GraalIsolate::kJSExecutionCrash : GraalIsolate::kJSExecutionAllowed);
2896+
reinterpret_cast<GraalIsolate*> (isolate_)->SetJSExecutionAction(action);
28932897
}
28942898

28952899
Isolate::AllowJavascriptExecutionScope::AllowJavascriptExecutionScope(Isolate* isolate) {
28962900
isolate_ = isolate;
2897-
was_execution_allowed_throws_ = reinterpret_cast<GraalIsolate*> (isolate)->SetJSExecutionAllowed(true);
2901+
GraalIsolate::JSExecutionAction oldAction = reinterpret_cast<GraalIsolate*> (isolate)->SetJSExecutionAction(GraalIsolate::kJSExecutionAllowed);
2902+
was_execution_allowed_throws_ = (oldAction == GraalIsolate::kJSExecutionThrow);
2903+
was_execution_allowed_assert_ = (oldAction == GraalIsolate::kJSExecutionCrash);
28982904
}
28992905

29002906
Isolate::AllowJavascriptExecutionScope::~AllowJavascriptExecutionScope() {
2901-
reinterpret_cast<GraalIsolate*> (isolate_)->SetJSExecutionAllowed(was_execution_allowed_throws_);
2907+
GraalIsolate::JSExecutionAction action = was_execution_allowed_throws_ ? GraalIsolate::kJSExecutionThrow : (was_execution_allowed_assert_ ? GraalIsolate::kJSExecutionCrash : GraalIsolate::kJSExecutionAllowed);
2908+
reinterpret_cast<GraalIsolate*> (isolate_)->SetJSExecutionAction(action);
29022909
}
29032910

29042911
void HeapProfiler::RemoveBuildEmbedderGraphCallback(BuildEmbedderGraphCallback callback, void* data) {

0 commit comments

Comments
 (0)