From 9295700ee70a56459ecc5854f6908d23fc881cd8 Mon Sep 17 00:00:00 2001 From: Rubin Xu Date: Wed, 10 Feb 2021 00:09:13 +0000 Subject: Upgrade V8 to 8.8.278.14 Test: gts-tradefed run gts-dev --module GtsGmscoreHostTestCases --test com.google.android.gts.devicepolicy.DeviceOwnerTest#testProxyPacProxyTest Test: m -j proxy_resolver_v8_unittest && adb sync && adb shell \ /data/nativetest/proxy_resolver_v8_unittest/proxy_resolver_v8_unittest Bug: 162604069 Bug: 167389063 Merged-In: I2b2e3d4f8fc99d160b228efb9b96ad6018453e09 Change-Id: I2b2e3d4f8fc99d160b228efb9b96ad6018453e09 --- Android.bp | 1 + src/proxy_resolver_v8.cc | 95 +++++++++++++++++++++++++++++++----------------- 2 files changed, 62 insertions(+), 34 deletions(-) diff --git a/Android.bp b/Android.bp index 9172eae..b552038 100644 --- a/Android.bp +++ b/Android.bp @@ -13,6 +13,7 @@ cc_library_shared { "-Wno-import", "-Wno-format", "-Wno-unused-parameter", + "-Wno-non-virtual-dtor", "-Werror", ], diff --git a/src/proxy_resolver_v8.cc b/src/proxy_resolver_v8.cc index 5884bd1..71226ac 100644 --- a/src/proxy_resolver_v8.cc +++ b/src/proxy_resolver_v8.cc @@ -151,17 +151,19 @@ inline typename string_type::value_type* WriteInto(string_type* str, } // Converts a V8 String to a UTF8 std::string. -std::string V8StringToUTF8(v8::Handle s) { +std::string V8StringToUTF8(v8::Isolate* isolate, v8::Handle s) { + int len = s->Length(); std::string result; - s->WriteUtf8(WriteInto(&result, s->Length() + 1)); + if (len > 0) + s->WriteUtf8(isolate, WriteInto(&result, len + 1)); return result; } // Converts a V8 String to a UTF16 string. -std::u16string V8StringToUTF16(v8::Handle s) { +std::u16string V8StringToUTF16(v8::Isolate* isolate, v8::Handle s) { int len = s->Length(); char16_t* buf = new char16_t[len + 1]; - s->Write(reinterpret_cast(buf), 0, len); + s->Write(isolate, reinterpret_cast(buf), 0, len); std::u16string ret(buf, len); delete[] buf; return ret; @@ -169,13 +171,13 @@ std::u16string V8StringToUTF16(v8::Handle s) { // Converts an ASCII std::string to a V8 string. v8::Local ASCIIStringToV8String(v8::Isolate* isolate, const std::string& s) { - return v8::String::NewFromUtf8(isolate, s.data(), v8::String::kNormalString, s.size()); + return v8::String::NewFromUtf8(isolate, s.data(), v8::NewStringType::kNormal, s.size()).ToLocalChecked(); } v8::Local UTF16StringToV8String(v8::Isolate* isolate, const std::u16string& s) { return v8::String::NewFromTwoByte( isolate, reinterpret_cast(s.data()), - v8::String::kNormalString, s.size()); + v8::NewStringType::kNormal, s.size()).ToLocalChecked(); } // Converts an ASCII string literal to a V8 string. @@ -183,8 +185,8 @@ v8::Local ASCIILiteralToV8String(v8::Isolate* isolate, const char* a // DCHECK(IsStringASCII(ascii)); size_t length = strlen(ascii); if (length <= kMaxStringBytesForCopy) - return v8::String::NewFromUtf8(isolate, ascii, v8::String::kNormalString, length); - return v8::String::NewExternal(isolate, new V8ExternalASCIILiteral(ascii, length)); + return v8::String::NewFromUtf8(isolate, ascii, v8::NewStringType::kNormal, length).ToLocalChecked(); + return v8::String::NewExternalOneByte(isolate, new V8ExternalASCIILiteral(ascii, length)).ToLocalChecked(); } // Stringizes a V8 object by calling its toString() method. Returns true @@ -196,10 +198,11 @@ bool V8ObjectToUTF8String(v8::Handle object, return false; v8::HandleScope scope(isolate); - v8::Local str_object = object->ToString(); - if (str_object.IsEmpty()) + v8::Local str_object; + + if (!object->ToString(isolate->GetCurrentContext()).ToLocal(&str_object)) return false; - *utf8_result = V8StringToUTF8(str_object); + *utf8_result = V8StringToUTF8(isolate, str_object); return true; } @@ -210,11 +213,11 @@ bool GetHostnameArgument(const v8::FunctionCallbackInfo& args, std::s if (args.Length() == 0 || args[0].IsEmpty() || !args[0]->IsString()) return false; - const std::u16string hostname_utf16 = V8StringToUTF16(args[0]->ToString()); + const std::u16string hostname_utf16 = V8StringToUTF16(args.GetIsolate(), v8::Local::Cast(args[0])); // If the hostname is already in ASCII, simply return it as is. if (IsStringASCII(hostname_utf16)) { - *hostname = V8StringToUTF8(args[0]->ToString()); + *hostname = V8StringToUTF8(args.GetIsolate(), v8::Local::Cast(args[0])); return true; } return false; @@ -390,11 +393,10 @@ class ProxyResolverV8::Context { UTF16StringToV8String(isolate_, host) }; v8::TryCatch try_catch(isolate_); - v8::Local ret = v8::Function::Cast(*function)->Call( - context->Global(), 2, argv); - - if (try_catch.HasCaught()) { - error_listener_->ErrorMessage(V8StringToUTF8(try_catch.Message()->Get())); + v8::Local ret; + if (!v8::Function::Cast(*function)->Call( + context, context->Global(), 2, argv).ToLocal(&ret)) { + error_listener_->ErrorMessage(V8StringToUTF8(isolate_, try_catch.Message()->Get())); return ERR_PAC_SCRIPT_FAILED; } @@ -403,7 +405,7 @@ class ProxyResolverV8::Context { return ERR_PAC_SCRIPT_FAILED; } - *results = V8StringToUTF16(ret->ToString()); + *results = V8StringToUTF16(isolate_, v8::Local::Cast(ret)); if (!IsStringASCII(*results)) { // TODO: Rather than failing when a wide string is returned, we @@ -508,16 +510,36 @@ class ProxyResolverV8::Context { bool GetFindProxyForURL(v8::Local* function) { v8::Local context = v8::Local::New(isolate_, v8_context_); - *function = context->Global()->Get( - ASCIILiteralToV8String(isolate_, "FindProxyForURL")); - return (*function)->IsFunction(); + + v8::TryCatch try_catch(isolate_); + + if (!context->Global() + ->Get(context, ASCIILiteralToV8String(isolate_, "FindProxyForURL")) + .ToLocal(function)) { + HandleError(try_catch.Message()); + // Fall through since try_catch.HasCaught() will be true + } + + if (function->IsEmpty() || try_catch.HasCaught()) { + error_listener_->ErrorMessage( + "Accessing FindProxyForURL threw an exception."); + return false; + } + + if (!(*function)->IsFunction()) { + error_listener_->ErrorMessage( + "FindProxyForURL is undefined or not a function."); + return false; + } + + return true; } // Handle an exception thrown by V8. void HandleError(v8::Handle message) { if (message.IsEmpty()) return; - error_listener_->ErrorMessage(V8StringToUTF8(message->Get())); + error_listener_->ErrorMessage(V8StringToUTF8(isolate_, message->Get())); } // Compiles and runs |script| in the current V8 context. @@ -530,14 +552,19 @@ class ProxyResolverV8::Context { // Compile the script. v8::ScriptOrigin origin = v8::ScriptOrigin(ASCIILiteralToV8String(isolate_, script_name)); - v8::MaybeLocal code = v8::Script::Compile(context, script, &origin); + v8::ScriptCompiler::Source script_source(script, origin); + v8::Local code; + if (!v8::ScriptCompiler::Compile( + context, &script_source, v8::ScriptCompiler::kNoCompileOptions, + v8::ScriptCompiler::NoCacheReason::kNoCacheBecausePacScript) + .ToLocal(&code)) { + HandleError(try_catch.Message()); + return ERR_PAC_SCRIPT_FAILED; + } // Execute. - if (!code.IsEmpty()) - code.ToLocalChecked()->Run(context); - - // Check for errors. - if (try_catch.HasCaught()) { + auto result = code->Run(context); + if (result.IsEmpty()) { HandleError(try_catch.Message()); return ERR_PAC_SCRIPT_FAILED; } @@ -672,7 +699,7 @@ class ProxyResolverV8::Context { return; } - std::string ip_address_list = V8StringToUTF8(args[0]->ToString()); + std::string ip_address_list = V8StringToUTF8(args.GetIsolate(), v8::Local::Cast(args[0])); std::string sorted_ip_address_list; bool success = SortIpAddressList(ip_address_list, &sorted_ip_address_list); if (!success) { @@ -691,8 +718,8 @@ class ProxyResolverV8::Context { return; } - std::string ip_address = V8StringToUTF8(args[0]->ToString()); - std::string ip_prefix = V8StringToUTF8(args[1]->ToString()); + std::string ip_address = V8StringToUTF8(args.GetIsolate(), v8::Local::Cast(args[0])); + std::string ip_prefix = V8StringToUTF8(args.GetIsolate(), v8::Local::Cast(args[1])); args.GetReturnValue().Set(IsInNetEx(ip_address, ip_prefix)); } @@ -717,8 +744,8 @@ ProxyResolverV8::ProxyResolverV8( : context_(NULL), js_bindings_(custom_js_bindings), error_listener_(error_listener) { if (!initialized_for_this_process_) { - v8::Platform* platform = v8::platform::CreateDefaultPlatform(); - v8::V8::InitializePlatform(platform); + static std::unique_ptr platform = v8::platform::NewDefaultPlatform(); + v8::V8::InitializePlatform(platform.get()); v8::V8::Initialize(); initialized_for_this_process_ = true; } -- cgit v1.2.3