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 <iostream> 17#include <string> 18 19#include <gtest/gtest.h> 20#include "disassembler.h" 21 22using namespace panda::disasm; 23 24#cmakedefine DISASM_BIN_DIR "@DISASM_BIN_DIR@/" 25 26TEST(label_test, test1) 27{ 28 Disassembler d {}; 29 30 std::stringstream ss {}; 31 d.Disassemble(std::string(DISASM_BIN_DIR) + "labels1.bc"); 32 d.Serialize(ss, false); 33 34 size_t beg_g = ss.str().find("u1 g() <static> {\n"); 35 size_t end_g = ss.str().find('}', beg_g); 36 size_t beg_gg = ss.str().find("u1 gg() <static> {\n"); 37 size_t end_gg = ss.str().find('}', beg_gg); 38 39 ASSERT_TRUE(beg_g != std::string::npos && end_g != std::string::npos) << "function g not found"; 40 ASSERT_TRUE(beg_gg != std::string::npos && end_gg != std::string::npos) << "function gg not found"; 41 42 std::string body_g = 43 ss.str().substr(beg_g + strlen("u1 g() <static> {\n"), end_g - (beg_g + strlen("u1 g() <static> {\n"))); 44 std::string body_gg = 45 ss.str().substr(beg_gg + strlen("u1 gg() <static> {\n"), end_gg - (beg_gg + strlen("u1 gg() <static> {\n"))); 46 47 EXPECT_EQ(body_g, "jump_label_0:\n\tjmp jump_label_0\n\treturn\n"); 48 EXPECT_EQ(body_gg, "\tjmp jump_label_0\njump_label_0:\n\treturn\n"); 49} 50 51TEST(label_test, test2) 52{ 53 Disassembler d {}; 54 55 std::stringstream ss {}; 56 d.Disassemble(std::string(DISASM_BIN_DIR) + "labels2.bc"); 57 d.Serialize(ss); 58 59 size_t beg_g = ss.str().find("g() <static> {"); 60 size_t end_g = ss.str().find('}', beg_g); 61 62 ASSERT_TRUE(beg_g != std::string::npos && end_g != std::string::npos) << "function g not found"; 63 64 std::string body_g = ss.str().substr(beg_g + strlen("g() {"), end_g - (beg_g + strlen("g() {"))); 65 66 EXPECT_TRUE(body_g.find("jump_label_0:\n\tmovi v0, 0x0") != std::string::npos) << "jump_label_0 not found"; 67 EXPECT_TRUE(body_g.find("jump_label_2:\n\tmovi v0, 0x1") != std::string::npos) << "jump_label_1 not found"; 68 EXPECT_TRUE(body_g.find("jump_label_4:\n\tmovi v0, 0x2") != std::string::npos) << "jump_label_2 not found"; 69 EXPECT_TRUE(body_g.find("jump_label_6:\n\tmovi v0, 0x3") != std::string::npos) << "jump_label_3 not found"; 70 EXPECT_TRUE(body_g.find("jump_label_7:\n\tmovi v0, 0x4") != std::string::npos) << "jump_label_4 not found"; 71 EXPECT_TRUE(body_g.find("jump_label_5:\n\tmovi v0, 0x5") != std::string::npos) << "jump_label_5 not found"; 72 EXPECT_TRUE(body_g.find("jump_label_3:\n\tmovi v0, 0x6") != std::string::npos) << "jump_label_6 not found"; 73 EXPECT_TRUE(body_g.find("jump_label_1:\n\tmovi v0, 0x7") != std::string::npos) << "jump_label_7 not found"; 74 75 EXPECT_TRUE(body_g.find("\tjmp jump_label_0\n" 76 "\tjmp jump_label_1\n" 77 "\tjmp jump_label_2\n" 78 "\tjmp jump_label_3\n" 79 "\tjmp jump_label_4\n" 80 "\tjmp jump_label_5\n" 81 "\tjmp jump_label_6\n" 82 "\tjmp jump_label_7\n") != std::string::npos) 83 << "label sequence is broken"; 84} 85 86TEST(label_test, test_exceptions) 87{ 88 Disassembler d {}; 89 90 std::stringstream ss {}; 91 d.Disassemble(std::string(DISASM_BIN_DIR) + "exceptions.bc"); 92 d.Serialize(ss); 93 94 std::string res = ss.str(); 95 96 EXPECT_TRUE(res.find("try_begin_label_0:\n\tldai 0x1") != std::string::npos); 97 EXPECT_TRUE(res.find("try_end_label_0:\n\tldai 0x3") != std::string::npos); 98 EXPECT_TRUE(res.find("handler_begin_label_0_0:\n\tcall.virt.short A_exception.getMessage:(A_exception), v0") != 99 std::string::npos); 100 EXPECT_TRUE(res.find("handler_end_label_0_0:\n\tldai 0x6") != std::string::npos); 101 EXPECT_TRUE(res.find("handler_begin_label_0_1:\n\tldai 0x7") != std::string::npos); 102 EXPECT_TRUE( 103 res.find( 104 ".catch A_exception, try_begin_label_0, try_end_label_0, handler_begin_label_0_0, handler_end_label_0_0") != 105 std::string::npos); 106 EXPECT_TRUE(res.find(".catchall try_begin_label_0, try_end_label_0, handler_begin_label_0_1") != std::string::npos); 107} 108 109#undef DISASM_BIN_DIR 110