1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <dlfcn.h>
17 #include <string>
18 #include <string_view>
19 #include <optional>
20
21 #include "nweb_log.h"
22
23 namespace {
24 #if defined(webview_arm64)
25 const std::string CRASHPAD_HANDLER_PATH = "/data/storage/el1/bundle/nweb/libs/arm64";
26 #elif defined(webview_x86_64)
27 const std::string CRASHPAD_HANDLER_PATH = "/data/storage/el1/bundle/nweb/libs/x86_64";
28 #elif defined(webview_arm)
29 const std::string CRASHPAD_HANDLER_PATH = "/data/storage/el1/bundle/nweb/libs/arm";
30 #else
31 const std::string CRASHPAD_HANDLER_PATH = "unsupport";
32 #endif
33
34 const std::string LIB_CRASHPAD_HANDLER = "libchrome_crashpad_handler.so";
35 }
36
GetEngineType(int argc,char * argv[])37 std::optional<std::string> GetEngineType(int argc, char* argv[]) {
38 constexpr std::string_view prefix = "--engine-type";
39 for (int i = 0; i < argc; ++i) {
40 std::string_view arg = argv[i];
41 if (arg.size() > prefix.size() && arg.substr(0, prefix.size()) == prefix) {
42 return std::string(arg.substr(prefix.size()));
43 }
44 }
45 return std::nullopt;
46 }
47
main(int argc,char * argv[])48 int main(int argc, char* argv[])
49 {
50 std::string libCrashpadHandler;
51 if (auto engineType = GetEngineType(argc, argv); engineType.has_value() && engineType.value() == "LEGACY") {
52 libCrashpadHandler = std::string(LEGACY_WEBVIEW_SANDBOX_LIB_PATH) + "/"
53 + std::string(WEBVIEW_CRASHPAD_HANDLER_SO);
54 } else {
55 libCrashpadHandler = std::string(WEBVIEW_SANDBOX_LIB_PATH) + "/"
56 + std::string(WEBVIEW_CRASHPAD_HANDLER_SO);
57 }
58
59 Dl_namespace dlns;
60 dlns_init(&dlns, "nweb_ns");
61 dlns_create(&dlns, std::string(WEBVIEW_SANDBOX_LIB_PATH).c_str());
62
63 Dl_namespace ndkns;
64 dlns_get("ndk", &ndkns);
65 dlns_inherit(&dlns, &ndkns, "allow_all_shared_libs");
66
67 void *handle = dlopen_ns(&dlns, libCrashpadHandler.c_str(), RTLD_NOW | RTLD_GLOBAL);
68
69 if (handle == nullptr) {
70 const std::string libCrashpadHandler2 = CRASHPAD_HANDLER_PATH + "/" + LIB_CRASHPAD_HANDLER;
71 handle = dlopen(libCrashpadHandler2.c_str(), RTLD_NOW | RTLD_GLOBAL);
72 if (handle == nullptr) {
73 WVLOG_E("crashpad, fail to dlopen %{public}s, errmsg=%{public}s", libCrashpadHandler2.c_str(), dlerror());
74 return -1;
75 }
76 }
77 int ret = 0;
78 using FuncType = int (*)(int argc, char* argv[]);
79 FuncType crashpadHandlerFunc = reinterpret_cast<FuncType>(dlsym(handle, "CrashpadHandlerMain"));
80 if (crashpadHandlerFunc == nullptr) {
81 WVLOG_E("crashpad, fail to dlsym CrashpadHandlerMain, errmsg=%{public}s", dlerror());
82 ret = dlclose(handle);
83 if (ret != 0) {
84 WVLOG_E("crashpad, fail to dlclose, errmsg=%{public}s", dlerror());
85 }
86 return -1;
87 }
88
89 WVLOG_I("crashpad, success to dlopen and dlsym, enter CrashpadHandlerMain");
90 ret = crashpadHandlerFunc(argc, argv);
91 if (ret != 0) {
92 WVLOG_E("crashpad dump failed, CrashpadHandlerMain return: %{public}d", ret);
93 _exit(1);
94 }
95 _exit(0);
96 }
97