1 /*
2 * Copyright (c) 2022 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 <chrono>
17 #include <iostream>
18 #include <iterator>
19 #include <ostream>
20 #include <signal.h> // NOLINTNEXTLINE(modernize-deprecated-headers)
21 #include <vector>
22
23 #include "ecmascript/base/string_helper.h"
24 #include "ecmascript/ecma_string.h"
25 #include "ecmascript/ecma_vm.h"
26 #include "ecmascript/js_runtime_options.h"
27 #include "ecmascript/jspandafile/js_pandafile.h"
28 #include "ecmascript/log.h"
29 #include "ecmascript/mem/mem_controller.h"
30 #include "ecmascript/napi/include/jsnapi.h"
31
32 namespace panda::ecmascript {
33 using PatchErrorCode = panda::JSNApi::PatchErrorCode;
34
35 const std::string TEST_ENTRY_POINT = "test";
36 const std::string RETEST_ENTRY_POINT = "retest";
37
BlockSignals()38 void BlockSignals()
39 {
40 #if defined(PANDA_TARGET_UNIX)
41 sigset_t set;
42 if (sigemptyset(&set) == -1) {
43 LOG_ECMA(ERROR) << "sigemptyset failed";
44 return;
45 }
46 #endif // PANDA_TARGET_UNIX
47 }
48
GetHelper()49 std::string GetHelper()
50 {
51 std::string str;
52 str.append(COMMON_HELP_HEAD_MSG);
53 str.append(HELP_OPTION_MSG);
54 return str;
55 }
56
Main(const int argc,const char ** argv)57 int Main(const int argc, const char **argv)
58 {
59 auto startTime =
60 std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch())
61 .count();
62
63 BlockSignals();
64
65 if (argc < 2) { // 2: at least have two arguments
66 std::cout << GetHelper();
67 return -1;
68 }
69
70 int newArgc = argc;
71 std::string files = argv[argc - 1];
72 if (!base::StringHelper::EndsWith(files, ".abc")) {
73 std::cout << "The last argument must be abc file" << std::endl;
74 std::cout << GetHelper();
75 return 1;
76 }
77
78 newArgc--;
79 JSRuntimeOptions runtimeOptions;
80 bool retOpt = runtimeOptions.ParseCommand(newArgc, argv);
81 if (!retOpt) {
82 std::cout << GetHelper();
83 return 1;
84 }
85
86 if (runtimeOptions.IsStartupTime()) {
87 std::cout << "\n"
88 << "Startup start time: " << startTime << std::endl;
89 }
90 EcmaVM *vm = JSNApi::CreateEcmaVM(runtimeOptions);
91 if (vm == nullptr) {
92 std::cout << "Cannot Create vm" << std::endl;
93 return -1;
94 }
95
96 {
97 std::cout << "QuickFix Execute start" << std::endl;
98 LocalScope scope(vm);
99 std::string entry = runtimeOptions.GetEntryPoint();
100
101 #if defined(PANDA_TARGET_WINDOWS)
102 arg_list_t fileNames = base::StringHelper::SplitString(files, ";");
103 #else
104 arg_list_t fileNames = base::StringHelper::SplitString(files, ":");
105 #endif
106 uint32_t len = fileNames.size();
107 if (len < 4) { // 4: four abc file
108 std::cout << "Must include base.abc, patch.abc, test.abc, retest.abc absolute path" << std::endl;
109 JSNApi::DestroyJSVM(vm);
110 return -1;
111 }
112 std::string baseFileName = fileNames[0];
113 bool isMergeAbc = runtimeOptions.GetMergeAbc();
114 JSNApi::SetBundle(vm, !isMergeAbc);
115
116 arg_list_t entryList = base::StringHelper::SplitString(entry, ":");
117 uint32_t size = entryList.size();
118 if (size != 2) { // 2: two entries
119 std::cout << "Must include 2 entries and with ':' to spilt" << std::endl;
120 JSNApi::DestroyJSVM(vm);
121 return -1;
122 }
123
124 auto res = JSNApi::Execute(vm, baseFileName, entryList[0]);
125 if (!res) {
126 std::cout << "Cannot execute panda file '" << baseFileName << "' with entry '" << entry << "'" << std::endl;
127 JSNApi::DestroyJSVM(vm);
128 return -1;
129 }
130 JSNApi::EnableUserUncaughtErrorHandler(vm);
131
132 std::string testLoadFileName = fileNames[1];
133 std::string testUnloadFileName = fileNames[2]; // 2: third file, test unloadpatch abc file.
134 for (uint32_t i = 3; i < len; i++) { // 3: patch file, test unloadpatch abc file.
135 std::string patchFileName = fileNames[i];
136 std::cout << "QuickFix start load patch" << std::endl;
137 auto result = JSNApi::LoadPatch(vm, patchFileName, baseFileName);
138 if (result != PatchErrorCode::SUCCESS) {
139 std::cout << "LoadPatch failed"<< std::endl;
140 break;
141 }
142 std::cout << "QuickFix load patch success" << std::endl;
143
144 // cold patch
145 res = JSNApi::Execute(vm, baseFileName, entryList[1]); // 1: second entrypoint, for cold patch.
146
147 res = JSNApi::Execute(vm, testLoadFileName, TEST_ENTRY_POINT);
148 if (!res) {
149 std::cout << "Cannot execute panda file '" << testLoadFileName
150 << "' with entry '" << entry << "'" << std::endl;
151 break;
152 }
153
154 std::cout << "QuickFix start check exception" << std::endl;
155 Local<ObjectRef> exception = JSNApi::GetAndClearUncaughtException(vm);
156 res = JSNApi::IsQuickFixCausedException(vm, exception, patchFileName);
157 if (res) {
158 std::cout << "QuickFix have exception." << std::endl;
159 } else {
160 std::cout << "QuickFix have no exception" << std::endl;
161 }
162
163 std::cout << "QuickFix start unload patch" << std::endl;
164 result = JSNApi::UnloadPatch(vm, patchFileName);
165 if (result != PatchErrorCode::SUCCESS) {
166 std::cout << "UnloadPatch failed!" << std::endl;
167 break;
168 }
169 std::cout << "QuickFix unload patch success" << std::endl;
170
171 res = JSNApi::Execute(vm, testUnloadFileName, RETEST_ENTRY_POINT);
172 if (!res) {
173 std::cout << "Cannot execute panda file '" << testUnloadFileName
174 << "' with entry '" << entry << "'" << std::endl;
175 break;
176 }
177 }
178 std::cout << "QuickFix Execute end" << std::endl;
179 }
180
181 JSNApi::DestroyJSVM(vm);
182 return 0;
183 }
184 } // namespace panda::ecmascript
185
main(int argc,const char ** argv)186 int main(int argc, const char **argv)
187 {
188 return panda::ecmascript::Main(argc, argv);
189 }
190