1 /**
2 * Copyright (c) 2021-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 <gtest/gtest.h>
17
18 #include <cstdint>
19
20 #include "runtime/compiler.h"
21 #include "runtime/interpreter/frame.h"
22 #include "runtime/tests/test_utils.h"
23
24 namespace ark::test {
25
26 class FrameTest : public testing::Test {
27 public:
FrameTest()28 FrameTest()
29 {
30 RuntimeOptions options;
31 options.SetShouldLoadBootPandaFiles(false);
32 options.SetShouldInitializeIntrinsics(false);
33 // this test doesn't check GC logic - just to make test easier without any handles
34 options.SetGcType("epsilon");
35 Runtime::Create(options);
36 thread_ = ark::MTManagedThread::GetCurrent();
37 thread_->ManagedCodeBegin();
38 }
39
~FrameTest()40 ~FrameTest() override
41 {
42 thread_->ManagedCodeEnd();
43 Runtime::Destroy();
44 }
45
46 NO_COPY_SEMANTIC(FrameTest);
47 NO_MOVE_SEMANTIC(FrameTest);
48
49 private:
50 ark::MTManagedThread *thread_;
51 };
52
53 template <bool IS_DYNAMIC = false>
CreateFrame(size_t nregs,Method * method,Frame * prev)54 Frame *CreateFrame(size_t nregs, Method *method, Frame *prev)
55 {
56 uint32_t extSz = EMPTY_EXT_FRAME_DATA_SIZE;
57 void *mem = aligned_alloc(8, ark::Frame::GetAllocSize(Frame::GetActualSize<IS_DYNAMIC>(nregs), extSz));
58 return new (Frame::FromExt(mem, extSz)) ark::Frame(mem, method, prev, nregs);
59 }
60
FreeFrame(Frame * f)61 void FreeFrame(Frame *f)
62 {
63 // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
64 std::free(f->GetExt());
65 }
66
TEST_F(FrameTest,Test)67 TEST_F(FrameTest, Test)
68 {
69 Frame *f = ark::test::CreateFrame(2, nullptr, nullptr);
70 auto frameHandler = StaticFrameHandler(f);
71 frameHandler.GetVReg(0).SetReference(nullptr);
72 EXPECT_TRUE(frameHandler.GetVReg(0).HasObject());
73 frameHandler.GetVReg(0).SetPrimitive(0);
74 EXPECT_FALSE(frameHandler.GetVReg(0).HasObject());
75
76 // NOLINTNEXTLINE(readability-magic-numbers)
77 int64_t v64 = 0x1122334455667788;
78 frameHandler.GetVReg(0).SetPrimitive(v64);
79 EXPECT_EQ(frameHandler.GetVReg(0).GetLong(), v64);
80 EXPECT_EQ(frameHandler.GetVReg(0).GetAs<int64_t>(), v64);
81
82 frameHandler.GetVReg(1).MovePrimitive(frameHandler.GetVReg(0));
83 EXPECT_FALSE(frameHandler.GetVReg(0).HasObject());
84 EXPECT_EQ(frameHandler.GetVReg(0).Get(), static_cast<int32_t>(v64));
85
86 frameHandler.GetVReg(1).MovePrimitive(frameHandler.GetVReg(0));
87 EXPECT_FALSE(frameHandler.GetVReg(0).HasObject());
88 EXPECT_EQ(frameHandler.GetVReg(0).GetLong(), v64);
89
90 // NOLINTNEXTLINE(readability-magic-numbers)
91 ObjectHeader *ptr = ark::mem::AllocateNullifiedPayloadString(15);
92 frameHandler.GetVReg(0).SetReference(ptr);
93 frameHandler.GetVReg(1).MoveReference(frameHandler.GetVReg(0));
94 EXPECT_TRUE(frameHandler.GetVReg(0).HasObject());
95 EXPECT_EQ(frameHandler.GetVReg(0).GetReference(), ptr);
96
97 // NOLINTNEXTLINE(readability-magic-numbers)
98 int32_t v32 = 0x11223344;
99 frameHandler.GetVReg(0).SetPrimitive(v32);
100 EXPECT_EQ(frameHandler.GetVReg(0).Get(), v32);
101 EXPECT_EQ(frameHandler.GetVReg(0).GetAs<int32_t>(), v32);
102
103 // NOLINTNEXTLINE(readability-magic-numbers)
104 int16_t v16 = 0x1122;
105 frameHandler.GetVReg(0).SetPrimitive(v16);
106 EXPECT_EQ(frameHandler.GetVReg(0).Get(), v16);
107 EXPECT_EQ(frameHandler.GetVReg(0).GetAs<int32_t>(), v16);
108
109 // NOLINTNEXTLINE(readability-magic-numbers)
110 int8_t v8 = 0x11;
111 frameHandler.GetVReg(0).SetPrimitive(v8);
112 EXPECT_EQ(frameHandler.GetVReg(0).Get(), v8);
113 EXPECT_EQ(frameHandler.GetVReg(0).GetAs<int32_t>(), v8);
114
115 // NOLINTNEXTLINE(readability-magic-numbers)
116 float f32 = 123.5;
117 frameHandler.GetVReg(0).SetPrimitive(f32);
118 EXPECT_EQ(frameHandler.GetVReg(0).GetFloat(), f32);
119 EXPECT_EQ(frameHandler.GetVReg(0).GetAs<float>(), f32);
120
121 // NOLINTNEXTLINE(readability-magic-numbers)
122 double f64 = 456.7;
123 frameHandler.GetVReg(0).SetPrimitive(f64);
124 EXPECT_EQ(frameHandler.GetVReg(0).GetDouble(), f64);
125 EXPECT_EQ(frameHandler.GetVReg(0).GetAs<double>(), f64);
126
127 ark::test::FreeFrame(f);
128 }
129
130 } // namespace ark::test
131