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     void *handle = dlopen(libCrashpadHandler.c_str(), RTLD_NOW | RTLD_GLOBAL);
40     if (handle == nullptr) {
41         const std::string libCrashpadHandler2 = CRASHPAD_HANDLER_PATH + "/" + LIB_CRASHPAD_HANDLER;
42         handle = dlopen(libCrashpadHandler2.c_str(), RTLD_NOW | RTLD_GLOBAL);
43         if (handle == nullptr) {
44             WVLOG_E("crashpad, fail to dlopen %{public}s, errmsg=%{public}s", libCrashpadHandler2.c_str(), dlerror());
45             return -1;
46         }
47     }
48 
49     using FuncType = int (*)(int argc, char* argv[]);
50     FuncType crashpadHandlerFunc = reinterpret_cast<FuncType>(dlsym(handle, "CrashpadHandlerMain"));
51     if (crashpadHandlerFunc == nullptr) {
52         WVLOG_E("crashpad, fail to dlsym CrashpadHandlerMain, errmsg=%{public}s", dlerror());
53         int ret = dlclose(handle);
54         if (ret != 0) {
55             WVLOG_E("crashped, fail to dlclose, errmsg=%{public}s", dlerror());
56         }
57         return -1;
58     }
59 
60     WVLOG_I("crashpad, success to dlopen and dlsym, enter CrashpadHandlerMain");
61     return crashpadHandlerFunc(argc, argv);
62 }
63