[cctest] Check compilation result in v8_compile

This CL introduces a CHECK in v8_compile that compilation succeedes.
Previously, a failed compilation would lead to undefined behavior or
a crash in CompileRun, because it would call Script::Run on a nullptr.
This CL introduced v8_try_compile that returns a MaybeLocal and supports
test-cases that want to ensure that a compilation fails.

Bug: chromium:1014415
Change-Id: I559190da6049f325e8650e4a29c6e387d8ff7af5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1943154
Auto-Submit: Sigurd Schneider <sigurds@chromium.org>
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#65266}
This commit is contained in:
Sigurd Schneider 2019-11-29 11:57:29 +01:00 committed by Commit Bot
parent 6c4cf05863
commit 88f8d801c6
3 changed files with 13 additions and 8 deletions

View File

@ -378,18 +378,23 @@ static inline v8::Local<v8::Symbol> v8_symbol(const char* name) {
static inline v8::Local<v8::Script> v8_compile(v8::Local<v8::String> x) { static inline v8::Local<v8::Script> v8_compile(v8::Local<v8::String> x) {
v8::Local<v8::Script> result; v8::Local<v8::Script> result;
if (v8::Script::Compile(v8::Isolate::GetCurrent()->GetCurrentContext(), x) CHECK(v8::Script::Compile(v8::Isolate::GetCurrent()->GetCurrentContext(), x)
.ToLocal(&result)) { .ToLocal(&result));
return result; return result;
} }
return v8::Local<v8::Script>();
}
static inline v8::Local<v8::Script> v8_compile(const char* x) { static inline v8::Local<v8::Script> v8_compile(const char* x) {
return v8_compile(v8_str(x)); return v8_compile(v8_str(x));
} }
static inline v8::MaybeLocal<v8::Script> v8_try_compile(
v8::Local<v8::String> x) {
return v8::Script::Compile(v8::Isolate::GetCurrent()->GetCurrentContext(), x);
}
static inline v8::MaybeLocal<v8::Script> v8_try_compile(const char* x) {
return v8_try_compile(v8_str(x));
}
static inline int32_t v8_run_int32value(v8::Local<v8::Script> script) { static inline int32_t v8_run_int32value(v8::Local<v8::Script> script) {
v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext(); v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();

View File

@ -8894,7 +8894,7 @@ TEST(CompilationErrorUsingTryCatchHandler) {
LocalContext env; LocalContext env;
v8::HandleScope scope(env->GetIsolate()); v8::HandleScope scope(env->GetIsolate());
v8::TryCatch try_catch(env->GetIsolate()); v8::TryCatch try_catch(env->GetIsolate());
v8_compile("This doesn't &*&@#$&*^ compile."); CHECK(v8_try_compile("This doesn't &*&@#$&*^ compile.").IsEmpty());
CHECK(*try_catch.Exception()); CHECK(*try_catch.Exception());
CHECK(try_catch.HasCaught()); CHECK(try_catch.HasCaught());
} }

View File

@ -1802,7 +1802,7 @@ TEST(StrictOctal) {
" 01; \n" " 01; \n"
" }; \n" " }; \n"
"}; \n"; "}; \n";
v8_compile(v8_str(script)); CHECK(v8_try_compile(v8_str(script)).IsEmpty());
CHECK(try_catch.HasCaught()); CHECK(try_catch.HasCaught());
v8::String::Utf8Value exception(isolate, try_catch.Exception()); v8::String::Utf8Value exception(isolate, try_catch.Exception());
CHECK_EQ(0, CHECK_EQ(0,