• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "nweb_log.h"
20 
21 namespace {
22 #if defined(webview_arm64)
23 const std::string CRASHPAD_HANDLER_PATH = "/data/storage/el1/bundle/nweb/libs/arm64";
24 #elif defined(webview_x86_64)
25 const std::string CRASHPAD_HANDLER_PATH = "/data/storage/el1/bundle/nweb/libs/x86_64";
26 #elif defined(webview_arm)
27 const std::string CRASHPAD_HANDLER_PATH = "/data/storage/el1/bundle/nweb/libs/arm";
28 #else
29 const std::string CRASHPAD_HANDLER_PATH = "unsupport";
30 #endif
31 
32 const std::string LIB_CRASHPAD_HANDLER = "libchrome_crashpad_handler.so";
33 }
34 
main(int argc,char * argv[])35 int main(int argc, char* argv[])
36 {
37     const std::string libCrashpadHandler = std::string(WEBVIEW_SANDBOX_LIB_PATH) + "/"
38                                             + std::string(WEBVIEW_CRASHPAD_HANDLER_SO);
39     Dl_namespace dlns;
40     dlns_init(&dlns, "nweb_ns");
41     dlns_create(&dlns, std::string(WEBVIEW_SANDBOX_LIB_PATH).c_str());
42 
43     Dl_namespace ndkns;
44     dlns_get("ndk", &ndkns);
45     dlns_inherit(&dlns, &ndkns, "allow_all_shared_libs");
46 
47     void *handle = dlopen_ns(&dlns, libCrashpadHandler.c_str(), RTLD_NOW | RTLD_GLOBAL);
48 
49     if (handle == nullptr) {
50         const std::string libCrashpadHandler2 = CRASHPAD_HANDLER_PATH + "/" + LIB_CRASHPAD_HANDLER;
51         handle = dlopen(libCrashpadHandler2.c_str(), RTLD_NOW | RTLD_GLOBAL);
52         if (handle == nullptr) {
53             WVLOG_E("crashpad, fail to dlopen %{public}s, errmsg=%{public}s", libCrashpadHandler2.c_str(), dlerror());
54             return -1;
55         }
56     }
57     int ret = 0;
58     using FuncType = int (*)(int argc, char* argv[]);
59     FuncType crashpadHandlerFunc = reinterpret_cast<FuncType>(dlsym(handle, "CrashpadHandlerMain"));
60     if (crashpadHandlerFunc == nullptr) {
61         WVLOG_E("crashpad, fail to dlsym CrashpadHandlerMain, errmsg=%{public}s", dlerror());
62         ret = dlclose(handle);
63         if (ret != 0) {
64             WVLOG_E("crashpad, fail to dlclose, errmsg=%{public}s", dlerror());
65         }
66         return -1;
67     }
68 
69     WVLOG_I("crashpad, success to dlopen and dlsym, enter CrashpadHandlerMain");
70     ret = crashpadHandlerFunc(argc, argv);
71     if (ret != 0) {
72         WVLOG_E("crashpad dump failed, CrashpadHandlerMain return: %{public}d", ret);
73         _exit(1);
74     }
75     _exit(0);
76 }
77