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