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 "ecmascript/mem/concurrent_marker.h"
17 #include "ecmascript/mem/machine_code.h"
18 #include "ecmascript/jit/jit.h"
19
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/js_tagged_value.h"
22 #include "ecmascript/js_array.h"
23 #include "ecmascript/ecma_vm.h"
24 #include "ecmascript/object_factory-inl.h"
25 #include "ecmascript/tests/test_helper.h"
26
27 using namespace panda;
28 using namespace panda::ecmascript;
29
30 namespace panda::test {
31
32 class MachineCodeTest : public BaseTestWithScope<false> {
33 public:
SetUp()34 void SetUp() override
35 {
36 JSRuntimeOptions options;
37 instance = JSNApi::CreateEcmaVM(options);
38 ASSERT_TRUE(instance != nullptr) << "Cannot create EcmaVM";
39 thread = instance->GetJSThread();
40 thread->ManagedCodeBegin();
41 scope = new EcmaHandleScope(thread);
42 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
43 heap->GetConcurrentMarker()->EnableConcurrentMarking(EnableConcurrentMarkType::ENABLE);
44 heap->GetSweeper()->EnableConcurrentSweep(EnableConcurrentSweepType::ENABLE);
45 }
46 };
47
HWTEST_F_L0(MachineCodeTest,SetMachineCodeObjectDataTest001)48 HWTEST_F_L0(MachineCodeTest, SetMachineCodeObjectDataTest001)
49 {
50 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
51 JSHandle<JSFunction> func(thread->GetEcmaVM()->GetGlobalEnv()->GetArrayFunction());
52 JSHandle<Method> method(thread, func->GetMethod());
53 RelocMap relocInfo;
54 MachineCodeDesc desc;
55 desc.codeType = MachineCodeType::BASELINE_CODE;
56 desc.instructionsSize = 100;
57 desc.instructionsAddr = 1000;
58 desc.stackMapSizeAlign = 100;
59 TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
60 factory->SetMachineCodeObjectData(machineCode, 100, desc, method, relocInfo);
61 }
62
HWTEST_F_L0(MachineCodeTest,SetMachineCodeObjectDataTest004)63 HWTEST_F_L0(MachineCodeTest, SetMachineCodeObjectDataTest004)
64 {
65 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
66 JSHandle<JSFunction> func(thread->GetEcmaVM()->GetGlobalEnv()->GetArrayFunction());
67 JSHandle<Method> method(thread, func->GetMethod());
68 RelocMap relocInfo;
69 MachineCodeDesc desc;
70 desc.codeType = MachineCodeType::FAST_JIT_CODE;
71 desc.instructionsSize = 100;
72 desc.instructionsAddr = 1000;
73 desc.stackMapSizeAlign = 100;
74 desc.rodataSizeAfterTextAlign = 0;
75 desc.rodataSizeBeforeTextAlign = 0;
76 desc.isAsyncCompileMode = true;
77 Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
78 Jit::GetInstance()->SetEnableJitFort(true);
79 ASSERT_EQ(Jit::GetInstance()->IsEnableAsyncCopyToFort(), true);
80 TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
81 factory->SetMachineCodeObjectData(machineCode, 100, desc, method, relocInfo);
82 }
83
HWTEST_F_L0(MachineCodeTest,SetMachineCodeObjectDataTest006)84 HWTEST_F_L0(MachineCodeTest, SetMachineCodeObjectDataTest006)
85 {
86 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
87 JSHandle<JSFunction> func(thread->GetEcmaVM()->GetGlobalEnv()->GetArrayFunction());
88 JSHandle<Method> method(thread, func->GetMethod());
89 RelocMap relocInfo;
90 MachineCodeDesc desc;
91 desc.codeType = MachineCodeType::FAST_JIT_CODE;
92 desc.instructionsSize = 100;
93 desc.instructionsAddr = 1000;
94 desc.stackMapSizeAlign = 100;
95 desc.rodataSizeAfterTextAlign = 0;
96 desc.rodataSizeBeforeTextAlign = 0;
97 desc.codeSizeAlign = 0;
98 desc.isAsyncCompileMode = true;
99 desc.funcEntryDesSizeAlign = 0;
100 desc.stackMapOrOffsetTableSize = 100;
101 Jit::GetInstance()->SetEnableJitFort(true);
102 Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
103 TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
104 MachineCode *code = MachineCode::Cast(machineCode);
105 const char src[] = "hello";
106 const char src2[] = "world";
107 desc.stackMapOrOffsetTableAddr = reinterpret_cast<uintptr_t>(src);
108 desc.funcEntryDesAddr = reinterpret_cast<uintptr_t>(src2);
109 desc.funcEntryDesSize = 100;
110 ASSERT_EQ(code->SetData(desc, method, 100, relocInfo), true);
111 }
112
HWTEST_F_L0(MachineCodeTest,SetMachineCodeObjectDataTest007)113 HWTEST_F_L0(MachineCodeTest, SetMachineCodeObjectDataTest007)
114 {
115 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
116 JSHandle<JSFunction> func(thread->GetEcmaVM()->GetGlobalEnv()->GetArrayFunction());
117 JSHandle<Method> method(thread, func->GetMethod());
118 MachineCodeDesc desc;
119 RelocMap relocInfo;
120 desc.codeType = MachineCodeType::FAST_JIT_CODE;
121 desc.instructionsSize = 100;
122 desc.instructionsAddr = 1000;
123 desc.stackMapSizeAlign = -100;
124 desc.rodataSizeAfterTextAlign = 0;
125 desc.rodataSizeBeforeTextAlign = 0;
126 desc.codeSizeAlign = 0;
127 desc.isAsyncCompileMode = true;
128 desc.funcEntryDesSizeAlign = 0;
129 desc.stackMapOrOffsetTableSize = 100;
130 Jit::GetInstance()->SetEnableJitFort(true);
131 Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
132 TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
133 MachineCode *code = MachineCode::Cast(machineCode);
134 const char src[] = "hello";
135 const char src2[] = "world";
136 desc.stackMapOrOffsetTableAddr = reinterpret_cast<uintptr_t>(src);
137 desc.funcEntryDesAddr = reinterpret_cast<uintptr_t>(src2);
138 desc.funcEntryDesSize = 100;
139 ASSERT_EQ(code->SetData(desc, method, -100, relocInfo), true);
140 }
141
HWTEST_F_L0(MachineCodeTest,SetText001)142 HWTEST_F_L0(MachineCodeTest, SetText001)
143 {
144 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
145 JSHandle<JSFunction> func(thread->GetEcmaVM()->GetGlobalEnv()->GetArrayFunction());
146 JSHandle<Method> method(thread, func->GetMethod());
147 MachineCodeDesc desc;
148 desc.codeType = MachineCodeType::FAST_JIT_CODE;
149 desc.instructionsSize = 100;
150 desc.instructionsAddr = 1000;
151 desc.stackMapSizeAlign = 100;
152 desc.rodataSizeAfterTextAlign = 0;
153 desc.rodataSizeBeforeTextAlign = 0;
154 desc.codeSizeAlign = 0;
155 desc.isAsyncCompileMode = true;
156 desc.funcEntryDesSizeAlign = 0;
157 desc.stackMapOrOffsetTableSize = 100;
158 Jit::GetInstance()->SetEnableJitFort(true);
159 Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
160 TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
161 MachineCode *code = MachineCode::Cast(machineCode);
162 desc.rodataSizeAfterTextAlign = 0;
163 desc.rodataSizeBeforeTextAlign = 1;
164 desc.rodataAddrBeforeText = 0;
165 ASSERT_EQ(code->SetText(desc), false);
166 }
167
HWTEST_F_L0(MachineCodeTest,SetText002)168 HWTEST_F_L0(MachineCodeTest, SetText002)
169 {
170 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
171 JSHandle<JSFunction> func(thread->GetEcmaVM()->GetGlobalEnv()->GetArrayFunction());
172 JSHandle<Method> method(thread, func->GetMethod());
173 MachineCodeDesc desc;
174 desc.codeType = MachineCodeType::FAST_JIT_CODE;
175 desc.instructionsSize = 100;
176 desc.instructionsAddr = 1000;
177 desc.stackMapSizeAlign = 100;
178 desc.rodataSizeAfterTextAlign = 0;
179 desc.rodataSizeBeforeTextAlign = 0;
180 desc.codeSizeAlign = 0;
181 desc.isAsyncCompileMode = true;
182 desc.funcEntryDesSizeAlign = 0;
183 desc.stackMapOrOffsetTableSize = 100;
184 Jit::GetInstance()->SetEnableJitFort(true);
185 Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
186 TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
187 MachineCode *code = MachineCode::Cast(machineCode);
188 desc.rodataSizeAfterTextAlign = 1;
189 desc.rodataAddrAfterText = 0;
190 desc.rodataSizeBeforeTextAlign = 0;
191 ASSERT_EQ(code->SetText(desc), false);
192 }
193
HWTEST_F_L0(MachineCodeTest,SetText003)194 HWTEST_F_L0(MachineCodeTest, SetText003)
195 {
196 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
197 JSHandle<JSFunction> func(thread->GetEcmaVM()->GetGlobalEnv()->GetArrayFunction());
198 JSHandle<Method> method(thread, func->GetMethod());
199 MachineCodeDesc desc;
200 desc.codeType = MachineCodeType::FAST_JIT_CODE;
201 desc.instructionsSize = 100;
202 desc.instructionsAddr = 1000;
203 desc.stackMapSizeAlign = 100;
204 desc.rodataSizeAfterTextAlign = 0;
205 desc.rodataSizeBeforeTextAlign = 0;
206 desc.codeSizeAlign = 0;
207 desc.isAsyncCompileMode = true;
208 desc.funcEntryDesSizeAlign = 0;
209 desc.stackMapOrOffsetTableSize = 100;
210 Jit::GetInstance()->SetEnableJitFort(true);
211 Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
212 TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
213 MachineCode *code = MachineCode::Cast(machineCode);
214 desc.rodataSizeAfterTextAlign = 1;
215 desc.rodataAddrAfterText = 0;
216 desc.rodataSizeBeforeTextAlign = 0;
217 desc.isAsyncCompileMode = false;
218 desc.codeAddr = 0;
219 ASSERT_EQ(code->SetText(desc), false);
220
221 Jit::GetInstance()->SetEnableJitFort(false);
222 desc.isAsyncCompileMode = true;
223 ASSERT_EQ(code->SetText(desc), false);
224
225 Jit::GetInstance()->SetEnableAsyncCopyToFort(false);
226 Jit::GetInstance()->SetEnableJitFort(true);
227 ASSERT_EQ(code->SetText(desc), false);
228
229 Jit::GetInstance()->SetEnableJitFort(true);
230 Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
231 }
232
233 }