1 /**
2 * Copyright (c) 2021-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 <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 panda::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 Runtime::Create(options);
34 thread_ = panda::MTManagedThread::GetCurrent();
35 thread_->ManagedCodeBegin();
36 }
37
~FrameTest()38 ~FrameTest()
39 {
40 thread_->ManagedCodeEnd();
41 Runtime::Destroy();
42 }
43
44 protected:
45 panda::MTManagedThread *thread_;
46 };
47
48 template <bool is_dynamic = false>
CreateFrame(size_t nregs,Method * method,Frame * prev)49 Frame *CreateFrame(size_t nregs, Method *method, Frame *prev)
50 {
51 uint32_t ext_sz = EmptyExtFrameDataSize;
52 void *mem = aligned_alloc(8, panda::Frame::GetAllocSize(Frame::GetActualSize<is_dynamic>(nregs), ext_sz));
53 return new (Frame::FromExt(mem, ext_sz)) panda::Frame(mem, method, prev, nregs);
54 }
55
FreeFrame(Frame * f)56 void FreeFrame(Frame *f)
57 {
58 std::free(f->GetExt());
59 }
60
TEST_F(FrameTest,Test)61 TEST_F(FrameTest, Test)
62 {
63 Frame *f = panda::test::CreateFrame(2, nullptr, nullptr);
64 auto frame_handler = StaticFrameHandler(f);
65 frame_handler.GetVReg(0).SetReference(nullptr);
66 EXPECT_TRUE(frame_handler.GetVReg(0).HasObject());
67 frame_handler.GetVReg(0).SetPrimitive(0);
68 EXPECT_FALSE(frame_handler.GetVReg(0).HasObject());
69
70 int64_t v64 = 0x1122334455667788;
71 frame_handler.GetVReg(0).SetPrimitive(v64);
72 EXPECT_EQ(frame_handler.GetVReg(0).GetLong(), v64);
73 EXPECT_EQ(frame_handler.GetVReg(0).GetAs<int64_t>(), v64);
74
75 frame_handler.GetVReg(1).MovePrimitive(frame_handler.GetVReg(0));
76 EXPECT_FALSE(frame_handler.GetVReg(0).HasObject());
77 EXPECT_EQ(frame_handler.GetVReg(0).Get(), static_cast<int32_t>(v64));
78
79 frame_handler.GetVReg(1).MovePrimitive(frame_handler.GetVReg(0));
80 EXPECT_FALSE(frame_handler.GetVReg(0).HasObject());
81 EXPECT_EQ(frame_handler.GetVReg(0).GetLong(), v64);
82
83 ObjectHeader *ptr = panda::mem::AllocateNullifiedPayloadString(15);
84 frame_handler.GetVReg(0).SetReference(ptr);
85 frame_handler.GetVReg(1).MoveReference(frame_handler.GetVReg(0));
86 EXPECT_TRUE(frame_handler.GetVReg(0).HasObject());
87 EXPECT_EQ(frame_handler.GetVReg(0).GetReference(), ptr);
88
89 int32_t v32 = 0x11223344;
90 frame_handler.GetVReg(0).SetPrimitive(v32);
91 EXPECT_EQ(frame_handler.GetVReg(0).Get(), v32);
92 EXPECT_EQ(frame_handler.GetVReg(0).GetAs<int32_t>(), v32);
93
94 int16_t v16 = 0x1122;
95 frame_handler.GetVReg(0).SetPrimitive(v16);
96 EXPECT_EQ(frame_handler.GetVReg(0).Get(), v16);
97 EXPECT_EQ(frame_handler.GetVReg(0).GetAs<int32_t>(), v16);
98
99 int8_t v8 = 0x11;
100 frame_handler.GetVReg(0).SetPrimitive(v8);
101 EXPECT_EQ(frame_handler.GetVReg(0).Get(), v8);
102 EXPECT_EQ(frame_handler.GetVReg(0).GetAs<int32_t>(), v8);
103
104 float f32 = 123.5;
105 frame_handler.GetVReg(0).SetPrimitive(f32);
106 EXPECT_EQ(frame_handler.GetVReg(0).GetFloat(), f32);
107 EXPECT_EQ(frame_handler.GetVReg(0).GetAs<float>(), f32);
108
109 double f64 = 456.7;
110 frame_handler.GetVReg(0).SetPrimitive(f64);
111 EXPECT_EQ(frame_handler.GetVReg(0).GetDouble(), f64);
112 EXPECT_EQ(frame_handler.GetVReg(0).GetAs<double>(), f64);
113
114 panda::test::FreeFrame(f);
115 }
116
117 } // namespace panda::test
118