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 <cstdint>
17 #include <gtest/gtest.h>
18 #include <type_traits>
19
20 #include "disassembler.h"
21
22 namespace panda::disasm {
23
24 panda_file::File::EntityId method_id {0x00};
25 panda::disasm::Disassembler disasm {};
26
TEST(BytecodeInstruction,Signed)27 TEST(BytecodeInstruction, Signed)
28 {
29 {
30 // jeqz 23
31 const uint8_t bytecode[] = {0x4f, 0x17};
32 BytecodeInstruction inst(bytecode);
33 const panda::pandasm::Ins &ins = disasm.BytecodeInstructionToPandasmInstruction(inst, method_id);
34 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x4f);
35 EXPECT_EQ(std::get<int64_t>(ins.imms[0]), static_cast<int64_t>(0x17));
36 }
37
38 {
39 // jmp -22
40 const uint8_t bytecode[] = {0x4d, 0xea};
41 BytecodeInstruction inst(bytecode);
42 const panda::pandasm::Ins &ins = disasm.BytecodeInstructionToPandasmInstruction(inst, method_id);
43 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x4d);
44 EXPECT_EQ(std::get<int64_t>(ins.imms[0]), static_cast<int8_t>(-22));
45 }
46
47 {
48 // ldai 30
49 const uint8_t bytecode[] = {0x62, 0x1e, 0x00, 0x00, 0x00};
50 BytecodeInstruction inst(bytecode);
51 const panda::pandasm::Ins &ins = disasm.BytecodeInstructionToPandasmInstruction(inst, method_id);
52 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x62);
53 EXPECT_EQ(std::get<int64_t>(ins.imms[0]), static_cast<int32_t>(0x1e));
54 }
55
56 {
57 // fldai 3.14
58 const uint8_t bytecode[] = {0x63, 0x1f, 0x85, 0xeb, 0x51, 0xb8, 0x1e, 0x09, 0x40};
59 BytecodeInstruction inst(bytecode);
60 const panda::pandasm::Ins &ins = disasm.BytecodeInstructionToPandasmInstruction(inst, method_id);
61 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x63);
62 EXPECT_EQ(std::get<double>(ins.imms[0]), 3.14);
63 }
64 }
65
TEST(BytecodeInstruction,Unsigned)66 TEST(BytecodeInstruction, Unsigned)
67 {
68 {
69 // callthis2 142, v0, v2, v3
70 const uint8_t bytecode[] = {0x2f, 0x8e, 0x00, 0x02, 0x03};
71 BytecodeInstruction inst(bytecode);
72 const panda::pandasm::Ins &ins = disasm.BytecodeInstructionToPandasmInstruction(inst, method_id);
73 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x2f);
74 EXPECT_EQ(std::get<int64_t>(ins.imms[0]), static_cast<uint8_t>(0x8e));
75 }
76
77 {
78 // neg 13
79 const uint8_t bytecode[] = {0x1f, 0x0d};
80 BytecodeInstruction inst(bytecode);
81 const panda::pandasm::Ins &ins = disasm.BytecodeInstructionToPandasmInstruction(inst, method_id);
82 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x1f);
83 EXPECT_EQ(std::get<int64_t>(ins.imms[0]), static_cast<uint8_t>(0x0d));
84 }
85
86 {
87 // stlexvar 0, 2
88 const uint8_t bytecode[] = {0x3d, 0x20};
89 BytecodeInstruction inst(bytecode);
90 const panda::pandasm::Ins &ins = disasm.BytecodeInstructionToPandasmInstruction(inst, method_id);
91 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x3d);
92 EXPECT_EQ(std::get<int64_t>(ins.imms[0]), 0);
93 EXPECT_EQ(std::get<int64_t>(ins.imms[1]), 2);
94 }
95
96 {
97 // newobjrange 17, 1, v11
98 const uint8_t bytecode[] = {0x08, 0x11, 0x01, 0x0b};
99 BytecodeInstruction inst(bytecode);
100 const panda::pandasm::Ins &ins = disasm.BytecodeInstructionToPandasmInstruction(inst, method_id);
101 EXPECT_EQ(static_cast<uint8_t>(inst.GetOpcode()), 0x08);
102 EXPECT_EQ(std::get<int64_t>(ins.imms[0]), static_cast<int8_t>(0x11));
103 EXPECT_EQ(std::get<int64_t>(ins.imms[1]), static_cast<int8_t>(0x01));
104 }
105 }
106
107 } // namespace panda::disasm
108