• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(thread));
53     MachineCodeDesc desc;
54     desc.codeType = MachineCodeType::BASELINE_CODE;
55     desc.instructionsSize = 100;
56     desc.instructionsAddr = 1000;
57     desc.stackMapSizeAlign = 100;
58     TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
59     factory->SetMachineCodeObjectData(machineCode, 100, desc, method);
60 }
61 
HWTEST_F_L0(MachineCodeTest,SetMachineCodeObjectDataTest004)62 HWTEST_F_L0(MachineCodeTest, SetMachineCodeObjectDataTest004)
63 {
64     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
65     JSHandle<JSFunction> func(thread->GetEcmaVM()->GetGlobalEnv()->GetArrayFunction());
66     JSHandle<Method> method(thread, func->GetMethod(thread));
67     MachineCodeDesc desc;
68     desc.codeType = MachineCodeType::FAST_JIT_CODE;
69     desc.instructionsSize = 100;
70     desc.instructionsAddr = 1000;
71     desc.stackMapSizeAlign = 100;
72     desc.rodataSizeAfterTextAlign = 0;
73     desc.rodataSizeBeforeTextAlign = 0;
74     desc.isAsyncCompileMode = true;
75     Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
76     Jit::GetInstance()->SetEnableJitFort(true);
77     ASSERT_EQ(Jit::GetInstance()->IsEnableAsyncCopyToFort(), true);
78     TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
79     factory->SetMachineCodeObjectData(machineCode, 100, desc, method);
80 }
81 
HWTEST_F_L0(MachineCodeTest,SetMachineCodeObjectDataTest006)82 HWTEST_F_L0(MachineCodeTest, SetMachineCodeObjectDataTest006)
83 {
84     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
85     JSHandle<JSFunction> func(thread->GetEcmaVM()->GetGlobalEnv()->GetArrayFunction());
86     JSHandle<Method> method(thread, func->GetMethod(thread));
87     MachineCodeDesc desc;
88     desc.codeType = MachineCodeType::FAST_JIT_CODE;
89     desc.instructionsSize = 100;
90     desc.instructionsAddr = 1000;
91     desc.stackMapSizeAlign = 100;
92     desc.rodataSizeAfterTextAlign = 0;
93     desc.rodataSizeBeforeTextAlign = 0;
94     desc.codeSizeAlign = 0;
95     desc.isAsyncCompileMode = true;
96     desc.funcEntryDesSizeAlign = 0;
97     desc.stackMapOrOffsetTableSize = 100;
98     Jit::GetInstance()->SetEnableJitFort(true);
99     Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
100     TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
101     MachineCode *code = MachineCode::Cast(machineCode);
102     const char src[] = "hello";
103     const char src2[] = "world";
104     desc.stackMapOrOffsetTableAddr = reinterpret_cast<uintptr_t>(src);
105     desc.funcEntryDesAddr = reinterpret_cast<uintptr_t>(src2);
106     desc.funcEntryDesSize = 100;
107     ASSERT_EQ(code->SetData(thread, desc, method, 100), true);
108 }
109 
HWTEST_F_L0(MachineCodeTest,SetMachineCodeObjectDataTest007)110 HWTEST_F_L0(MachineCodeTest, SetMachineCodeObjectDataTest007)
111 {
112     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
113     JSHandle<JSFunction> func(thread->GetEcmaVM()->GetGlobalEnv()->GetArrayFunction());
114     JSHandle<Method> method(thread, func->GetMethod(thread));
115     MachineCodeDesc desc;
116     desc.codeType = MachineCodeType::FAST_JIT_CODE;
117     desc.instructionsSize = 100;
118     desc.instructionsAddr = 1000;
119     desc.stackMapSizeAlign = -100;
120     desc.rodataSizeAfterTextAlign = 0;
121     desc.rodataSizeBeforeTextAlign = 0;
122     desc.codeSizeAlign = 0;
123     desc.isAsyncCompileMode = true;
124     desc.funcEntryDesSizeAlign = 0;
125     desc.stackMapOrOffsetTableSize = 100;
126     Jit::GetInstance()->SetEnableJitFort(true);
127     Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
128     TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
129     MachineCode *code = MachineCode::Cast(machineCode);
130     const char src[] = "hello";
131     const char src2[] = "world";
132     desc.stackMapOrOffsetTableAddr = reinterpret_cast<uintptr_t>(src);
133     desc.funcEntryDesAddr = reinterpret_cast<uintptr_t>(src2);
134     desc.funcEntryDesSize = 100;
135     ASSERT_EQ(code->SetData(thread, desc, method, -100), true);
136 }
137 
HWTEST_F_L0(MachineCodeTest,SetText001)138 HWTEST_F_L0(MachineCodeTest, SetText001)
139 {
140     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
141     JSHandle<JSFunction> func(thread->GetEcmaVM()->GetGlobalEnv()->GetArrayFunction());
142     JSHandle<Method> method(thread, func->GetMethod(thread));
143     MachineCodeDesc desc;
144     desc.codeType = MachineCodeType::FAST_JIT_CODE;
145     desc.instructionsSize = 100;
146     desc.instructionsAddr = 1000;
147     desc.stackMapSizeAlign = 100;
148     desc.rodataSizeAfterTextAlign = 0;
149     desc.rodataSizeBeforeTextAlign = 0;
150     desc.codeSizeAlign = 0;
151     desc.isAsyncCompileMode = true;
152     desc.funcEntryDesSizeAlign = 0;
153     desc.stackMapOrOffsetTableSize = 100;
154     Jit::GetInstance()->SetEnableJitFort(true);
155     Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
156     TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
157     MachineCode *code = MachineCode::Cast(machineCode);
158     desc.rodataSizeAfterTextAlign = 0;
159     desc.rodataSizeBeforeTextAlign = 1;
160     desc.rodataAddrBeforeText = 0;
161     ASSERT_EQ(code->SetText(desc), false);
162 }
163 
HWTEST_F_L0(MachineCodeTest,SetText002)164 HWTEST_F_L0(MachineCodeTest, SetText002)
165 {
166     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
167     JSHandle<JSFunction> func(thread->GetEcmaVM()->GetGlobalEnv()->GetArrayFunction());
168     JSHandle<Method> method(thread, func->GetMethod(thread));
169     MachineCodeDesc desc;
170     desc.codeType = MachineCodeType::FAST_JIT_CODE;
171     desc.instructionsSize = 100;
172     desc.instructionsAddr = 1000;
173     desc.stackMapSizeAlign = 100;
174     desc.rodataSizeAfterTextAlign = 0;
175     desc.rodataSizeBeforeTextAlign = 0;
176     desc.codeSizeAlign = 0;
177     desc.isAsyncCompileMode = true;
178     desc.funcEntryDesSizeAlign = 0;
179     desc.stackMapOrOffsetTableSize = 100;
180     Jit::GetInstance()->SetEnableJitFort(true);
181     Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
182     TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
183     MachineCode *code = MachineCode::Cast(machineCode);
184     desc.rodataSizeAfterTextAlign = 1;
185     desc.rodataAddrAfterText = 0;
186     desc.rodataSizeBeforeTextAlign = 0;
187     ASSERT_EQ(code->SetText(desc), false);
188 }
189 
HWTEST_F_L0(MachineCodeTest,SetText003)190 HWTEST_F_L0(MachineCodeTest, SetText003)
191 {
192     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
193     JSHandle<JSFunction> func(thread->GetEcmaVM()->GetGlobalEnv()->GetArrayFunction());
194     JSHandle<Method> method(thread, func->GetMethod(thread));
195     MachineCodeDesc desc;
196     desc.codeType = MachineCodeType::FAST_JIT_CODE;
197     desc.instructionsSize = 100;
198     desc.instructionsAddr = 1000;
199     desc.stackMapSizeAlign = 100;
200     desc.rodataSizeAfterTextAlign = 0;
201     desc.rodataSizeBeforeTextAlign = 0;
202     desc.codeSizeAlign = 0;
203     desc.isAsyncCompileMode = true;
204     desc.funcEntryDesSizeAlign = 0;
205     desc.stackMapOrOffsetTableSize = 100;
206     Jit::GetInstance()->SetEnableJitFort(true);
207     Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
208     TaggedObject *machineCode = factory->NewMachineCodeObject(100, desc);
209     MachineCode *code = MachineCode::Cast(machineCode);
210     desc.rodataSizeAfterTextAlign = 1;
211     desc.rodataAddrAfterText = 0;
212     desc.rodataSizeBeforeTextAlign = 0;
213     desc.isAsyncCompileMode = false;
214     desc.codeAddr = 0;
215     ASSERT_EQ(code->SetText(desc), false);
216 
217     Jit::GetInstance()->SetEnableJitFort(false);
218     desc.isAsyncCompileMode = true;
219     ASSERT_EQ(code->SetText(desc), false);
220 
221     Jit::GetInstance()->SetEnableAsyncCopyToFort(false);
222     Jit::GetInstance()->SetEnableJitFort(true);
223     ASSERT_EQ(code->SetText(desc), false);
224 
225     Jit::GetInstance()->SetEnableJitFort(true);
226     Jit::GetInstance()->SetEnableAsyncCopyToFort(true);
227 }
228 
229 }
230