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
QuickFixQueryFuncColdPatch(std::string baseFileName,std::string & patchFileName,uint8_t ** patchBuffer,size_t & patchBufferSize)57 bool QuickFixQueryFuncColdPatch(std::string baseFileName, std::string &patchFileName,
58 uint8_t ** patchBuffer, size_t &patchBufferSize)
59 {
60 return true;
61 }
62
Main(const int argc,const char ** argv)63 int Main(const int argc, const char **argv)
64 {
65 auto startTime =
66 std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch())
67 .count();
68
69 BlockSignals();
70
71 if (argc < 2) { // 2: at least have two arguments
72 std::cout << GetHelper();
73 return -1;
74 }
75
76 int newArgc = argc;
77 std::string files = argv[argc - 1];
78 if (!base::StringHelper::EndsWith(files, ".abc")) {
79 std::cout << "The last argument must be abc file" << std::endl;
80 std::cout << GetHelper();
81 return 1;
82 }
83
84 newArgc--;
85 JSRuntimeOptions runtimeOptions;
86 bool retOpt = runtimeOptions.ParseCommand(newArgc, argv);
87 if (!retOpt) {
88 std::cout << GetHelper();
89 return 1;
90 }
91
92 if (runtimeOptions.IsStartupTime()) {
93 std::cout << "\n"
94 << "Startup start time: " << startTime << std::endl;
95 }
96 EcmaVM *vm = JSNApi::CreateEcmaVM(runtimeOptions);
97 if (vm == nullptr) {
98 std::cout << "Cannot Create vm" << std::endl;
99 return -1;
100 }
101
102 {
103 std::cout << "QuickFix Execute start" << std::endl;
104 LocalScope scope(vm);
105 std::string entry = runtimeOptions.GetEntryPoint();
106
107 #if defined(PANDA_TARGET_WINDOWS)
108 arg_list_t fileNames = base::StringHelper::SplitString(files, ";");
109 #else
110 arg_list_t fileNames = base::StringHelper::SplitString(files, ":");
111 #endif
112 uint32_t len = fileNames.size();
113 if (len < 4) { // 4: four abc file
114 std::cout << "Must include base.abc, patch.abc, test.abc, retest.abc absolute path" << std::endl;
115 JSNApi::DestroyJSVM(vm);
116 return -1;
117 }
118 std::string baseFileName = fileNames[0];
119 bool isMergeAbc = runtimeOptions.GetMergeAbc();
120 JSNApi::SetBundle(vm, !isMergeAbc);
121
122 arg_list_t entryList = base::StringHelper::SplitString(entry, ":");
123 uint32_t size = entryList.size();
124 uint32_t entryNum = 1; // 1: one entry, excluding coldpatch testcases.
125 if (size < entryNum) { // 1: one entry
126 std::cout << "Must include 1 entry at least" << std::endl;
127 JSNApi::DestroyJSVM(vm);
128 return -1;
129 }
130
131 bool res = false;
132 if (size == entryNum) {
133 res = JSNApi::Execute(vm, baseFileName, entryList[0]);
134 if (!res) {
135 std::cout << "Cannot execute panda file '" << baseFileName <<
136 "' with entry '" << entry << "'" << std::endl;
137 JSNApi::DestroyJSVM(vm);
138 return -1;
139 }
140 }
141 JSNApi::EnableUserUncaughtErrorHandler(vm);
142
143 std::string testLoadFileName = fileNames[1];
144 std::string testUnloadFileName = fileNames[2]; // 2: third file, test unloadpatch abc file.
145 for (uint32_t i = 3; i < len; i++) { // 3: patch file, test unloadpatch abc file.
146 std::string patchFileName = fileNames[i];
147 std::cout << "QuickFix start load patch" << std::endl;
148 auto result = JSNApi::LoadPatch(vm, patchFileName, baseFileName);
149 if (result != PatchErrorCode::SUCCESS) {
150 std::cout << "LoadPatch failed"<< std::endl;
151 break;
152 }
153 std::cout << "QuickFix load patch success" << std::endl;
154
155 if (size > entryNum) {
156 JSNApi::RegisterQuickFixQueryFunc(vm, QuickFixQueryFuncColdPatch);
157 res = JSNApi::Execute(vm, baseFileName, entryList[0]);
158 }
159
160 res = JSNApi::Execute(vm, testLoadFileName, TEST_ENTRY_POINT);
161
162 if (size == entryNum) {
163 std::cout << "QuickFix start check exception" << std::endl;
164 Local<ObjectRef> exception = JSNApi::GetAndClearUncaughtException(vm);
165 res = JSNApi::IsQuickFixCausedException(vm, exception, patchFileName);
166 if (res) {
167 std::cout << "QuickFix have exception" << std::endl;
168 } else {
169 std::cout << "QuickFix have no exception" << std::endl;
170 }
171
172 std::cout << "QuickFix start unload patch" << std::endl;
173 result = JSNApi::UnloadPatch(vm, patchFileName);
174 if (result != PatchErrorCode::SUCCESS) {
175 std::cout << "UnloadPatch failed!" << std::endl;
176 break;
177 }
178 std::cout << "QuickFix unload patch success" << std::endl;
179
180 res = JSNApi::Execute(vm, testUnloadFileName, RETEST_ENTRY_POINT);
181 if (!res) {
182 std::cout << "Cannot execute panda file '" << testUnloadFileName
183 << "' with entry '" << entry << "'" << std::endl;
184 break;
185 }
186 }
187 }
188 std::cout << "QuickFix Execute end" << std::endl;
189 }
190
191 JSNApi::DestroyJSVM(vm);
192 return 0;
193 }
194 } // namespace panda::ecmascript
195
main(int argc,const char ** argv)196 int main(int argc, const char **argv)
197 {
198 return panda::ecmascript::Main(argc, argv);
199 }
200