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