caoccao/Javet

Android Build Break with V8 v12.1.285.17

Closed this issue · 0 comments

Problem

V8 Android build breaks on v12.1.285.17 with the following error messages.

../../src/wasm/wasm-engine.cc:163:19: error: no viable overloaded '='
  163 |       source_url_ = String::cast(script->name())->ToCString();
      |       ~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../third_party/android_toolchain/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/memory:3798:17: note: candidate function not viable: no known conversion from 'std::unique_ptr<char[]>' to 'const shared_ptr<const char[]>' for 1st argument
 3798 |     shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
      |                 ^         ~~~~~~~~~~~~~~~~~~~~~
../../third_party/android_toolchain/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/memory:3806:9: note: candidate template ignored: could not match 'shared_ptr' against 'unique_ptr'
 3806 |         operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
      |         ^
../../third_party/android_toolchain/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/memory:3809:17: note: candidate function not viable: no known conversion from 'std::unique_ptr<char[]>' to 'shared_ptr<const char[]>' for 1st argument
 3809 |     shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
      |                 ^         ~~~~~~~~~~~~~~~~
../../third_party/android_toolchain/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/memory:3817:9: note: candidate template ignored: could not match 'shared_ptr' against 'unique_ptr'
 3817 |         operator=(shared_ptr<_Yp>&& __r);
      |         ^
../../third_party/android_toolchain/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/memory:3851:9: note: candidate template ignored: requirement '!is_array<char[]>::value' was not satisfied [with _Yp = char[], _Dp = std::default_delete<char[]>]
 3851 |         operator=(unique_ptr<_Yp, _Dp>&& __r);
      |         ^
../../src/wasm/wasm-engine.cc:1344:21: error: cannot initialize a variable of type 'const char *' with an rvalue of type 'element_type *' (aka 'const char (*)[]')
 1344 |         const char* source_url = code_to_log.source_url.get();
      |                     ^            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.

Root Cause

The CPP compiler used by V8 Android build is not compatible with the conversion from 'std::unique_ptr<char[]>' to 'const shared_ptr<const char[]>' introduced by 5033410: [wasm] Assign unique_ptr to shared_ptr directly | https://chromium-review.googlesource.com/c/v8/v8/+/5033410.

Solution

Execute the following commands to revert that change.

sed -i 's/char\[\]/char/g' src/wasm/wasm-engine.cc
sed -i 's/source_url_ = String::cast(script->name())->ToCString();/std::unique_ptr<char[]> source_url = String::cast(script->name())->ToCString(); source_url_ = {source_url.release(), source_url.get_deleter()};/' src/wasm/wasm-engine.cc

The diff is as follows.

diff --git a/src/wasm/wasm-engine.cc b/src/wasm/wasm-engine.cc
index f01abea2576..72f968f2ff4 100644
--- a/src/wasm/wasm-engine.cc
+++ b/src/wasm/wasm-engine.cc
@@ -160,7 +160,7 @@ class WeakScriptHandle {
       : script_id_(script->id()), isolate_(isolate) {
     DCHECK(IsString(script->name()) || IsUndefined(script->name()));
     if (IsString(script->name())) {
-      source_url_ = String::cast(script->name())->ToCString();
+      std::unique_ptr<char[]> source_url = String::cast(script->name())->ToCString(); source_url_ = {source_url.release(), source_url.get_deleter()};
     }
     auto global_handle =
         script->GetIsolate()->global_handles()->Create(*script);
@@ -192,7 +192,7 @@ class WeakScriptHandle {

   int script_id() const { return script_id_; }

-  const std::shared_ptr<const char[]>& source_url() const {
+  const std::shared_ptr<const char>& source_url() const {
     return source_url_;
   }

@@ -210,7 +210,7 @@ class WeakScriptHandle {
   // The shared pointer is kept alive by unlogged code, even if this entry is
   // collected in the meantime.
   // TODO(chromium:1132260): Revisit this for huge URLs.
-  std::shared_ptr<const char[]> source_url_;
+  std::shared_ptr<const char> source_url_;

   // The Isolate that the handled script belongs to.
   Isolate* isolate_;
@@ -428,7 +428,7 @@ struct WasmEngine::IsolateInfo {
   // the respective source URL.
   struct CodeToLogPerScript {
     std::vector<WasmCode*> code;
-    std::shared_ptr<const char[]> source_url;
+    std::shared_ptr<const char> source_url;
   };
   std::unordered_map<int, CodeToLogPerScript> code_to_log;