|
| 1 | +//===---------- llvm/unittests/MC/AssemblerTest.cpp -----------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm-c/Assembler.h" |
| 10 | +#include "llvm-c/Core.h" |
| 11 | +#include "llvm/Support/MemoryBuffer.h" |
| 12 | +// NOLINTNEXTLINE(misc-header-include-cycle) |
| 13 | +#include "gtest/gtest.h" |
| 14 | + |
| 15 | +using namespace llvm; |
| 16 | + |
| 17 | +class AssemblerCTest : public testing::Test { |
| 18 | + void SetUp() override { |
| 19 | + LLVMInitializeEraVMTargetInfo(); |
| 20 | + LLVMInitializeEraVMTarget(); |
| 21 | + LLVMInitializeEraVMTargetMC(); |
| 22 | + LLVMInitializeEraVMAsmParser(); |
| 23 | + LLVMInitializeEraVMAsmPrinter(); |
| 24 | + |
| 25 | + LLVMTargetRef Target = nullptr; |
| 26 | + const char *Triple = "eravm"; |
| 27 | + char *ErrMsg = nullptr; |
| 28 | + if (LLVMGetTargetFromTriple(Triple, &Target, &ErrMsg)) { |
| 29 | + FAIL() << "Failed to create target from the triple (" << Triple |
| 30 | + << "): " << ErrMsg; |
| 31 | + return; |
| 32 | + } |
| 33 | + ASSERT_TRUE(Target); |
| 34 | + |
| 35 | + // Construct a TargetMachine. |
| 36 | + TM = |
| 37 | + LLVMCreateTargetMachine(Target, Triple, "", "", LLVMCodeGenLevelDefault, |
| 38 | + LLVMRelocDefault, LLVMCodeModelDefault); |
| 39 | + } |
| 40 | + |
| 41 | + void TearDown() override { LLVMDisposeTargetMachine(TM); } |
| 42 | + |
| 43 | +public: |
| 44 | + LLVMTargetMachineRef TM = nullptr; |
| 45 | +}; |
| 46 | + |
| 47 | +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) |
| 48 | +TEST_F(AssemblerCTest, Basic) { |
| 49 | + StringRef Asm = " \ |
| 50 | + .text \n\ |
| 51 | + incsp 1 \n\ |
| 52 | + add code[@glob_initializer_0], r0, stack[@glob] \n\ |
| 53 | + \n\ |
| 54 | + get_glob: \n\ |
| 55 | + add 3, r0, r1 \n\ |
| 56 | + add stack[@glob], r1, r1 \n\ |
| 57 | + ret \n\ |
| 58 | + \n\ |
| 59 | + .data \n\ |
| 60 | + glob: \n\ |
| 61 | + .cell 113 \n\ |
| 62 | + \n\ |
| 63 | + .rodata \n\ |
| 64 | + glob_initializer_0: \n\ |
| 65 | + .cell 113 \n\ |
| 66 | + \n\ |
| 67 | + .text \n\ |
| 68 | + DEFAULT_UNWIND: \n\ |
| 69 | + pncl @DEFAULT_UNWIND \n\ |
| 70 | + DEFAULT_FAR_RETURN: \n\ |
| 71 | + retl r1, @DEFAULT_FAR_RETURN \n\ |
| 72 | + DEFAULT_FAR_REVERT: \n\ |
| 73 | + revl r1, @DEFAULT_FAR_REVERT"; |
| 74 | + |
| 75 | + LLVMMemoryBufferRef AsmMemBuffer = |
| 76 | + LLVMCreateMemoryBufferWithMemoryRange(Asm.data(), Asm.size(), "test", 0); |
| 77 | + LLVMMemoryBufferRef ObjMemBuffer = nullptr; |
| 78 | + char *ErrMsg = nullptr; |
| 79 | + if (LLVMAssembleEraVM(TM, AsmMemBuffer, &ObjMemBuffer, &ErrMsg)) { |
| 80 | + FAIL() << "Failed to assembly:" << ErrMsg; |
| 81 | + LLVMDisposeMessage(ErrMsg); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + LLVMDisposeMemoryBuffer(AsmMemBuffer); |
| 86 | + LLVMDisposeMemoryBuffer(ObjMemBuffer); |
| 87 | +} |
| 88 | + |
| 89 | +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) |
| 90 | +TEST_F(AssemblerCTest, NonNullTerminatedInput) { |
| 91 | + StringRef Asm = ".text\nxxxxx777"; |
| 92 | + LLVMMemoryBufferRef AsmMemBuffer = |
| 93 | + LLVMCreateMemoryBufferWithMemoryRange(Asm.data(), 5, "test", 0); |
| 94 | + LLVMMemoryBufferRef ObjMemBuffer = nullptr; |
| 95 | + char *ErrMsg = nullptr; |
| 96 | + // Return code 'true' denotes an error. |
| 97 | + if (LLVMAssembleEraVM(TM, AsmMemBuffer, &ObjMemBuffer, &ErrMsg)) { |
| 98 | + FAIL() << "Failed to assembly:" << ErrMsg; |
| 99 | + LLVMDisposeMessage(ErrMsg); |
| 100 | + return; |
| 101 | + } |
| 102 | + LLVMDisposeMemoryBuffer(AsmMemBuffer); |
| 103 | + LLVMDisposeMemoryBuffer(ObjMemBuffer); |
| 104 | +} |
| 105 | + |
| 106 | +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) |
| 107 | +TEST_F(AssemblerCTest, AsmParserError) { |
| 108 | + StringRef Asm = " \ |
| 109 | + .text \n\ |
| 110 | + get_glob: \n\ |
| 111 | + add 3, r0, r44 \n\ |
| 112 | + add stack[@glob], r1, r1 \n\ |
| 113 | + ret"; |
| 114 | + |
| 115 | + LLVMMemoryBufferRef AsmMemBuffer = |
| 116 | + LLVMCreateMemoryBufferWithMemoryRange(Asm.data(), Asm.size(), "test", 0); |
| 117 | + LLVMMemoryBufferRef ObjMemBuffer = nullptr; |
| 118 | + char *ErrMsg = nullptr; |
| 119 | + // Return code 'true' denotes an error. |
| 120 | + EXPECT_TRUE(LLVMAssembleEraVM(TM, AsmMemBuffer, &ObjMemBuffer, &ErrMsg)); |
| 121 | + EXPECT_TRUE(StringRef(ErrMsg).contains("cannot parse operand")); |
| 122 | + |
| 123 | + LLVMDisposeMessage(ErrMsg); |
| 124 | + LLVMDisposeMemoryBuffer(AsmMemBuffer); |
| 125 | +} |
| 126 | + |
| 127 | +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) |
| 128 | +TEST_F(AssemblerCTest, ToManyInstructionsError) { |
| 129 | + std::string Asm = ".text"; |
| 130 | + // Max number of instructions is 2^16. |
| 131 | + for (unsigned Num = 0; Num < (1 << 16) + 1; ++Num) |
| 132 | + Asm += "\nadd r0, r0, r0"; |
| 133 | + |
| 134 | + LLVMMemoryBufferRef AsmMemBuffer = |
| 135 | + LLVMCreateMemoryBufferWithMemoryRange(Asm.data(), Asm.size(), "test", 0); |
| 136 | + LLVMMemoryBufferRef ObjMemBuffer = nullptr; |
| 137 | + char *ErrMsg = nullptr; |
| 138 | + if (LLVMAssembleEraVM(TM, AsmMemBuffer, &ObjMemBuffer, &ErrMsg)) { |
| 139 | + FAIL() << "Failed to assembly:" << ErrMsg; |
| 140 | + LLVMDisposeMessage(ErrMsg); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + // Return code 'true' denotes an error. |
| 145 | + EXPECT_TRUE(LLVMExceedsSizeLimitEraVM(ObjMemBuffer, /*metadataSize=*/0)); |
| 146 | + |
| 147 | + LLVMDisposeMemoryBuffer(AsmMemBuffer); |
| 148 | + LLVMDisposeMemoryBuffer(ObjMemBuffer); |
| 149 | +} |
| 150 | + |
| 151 | +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) |
| 152 | +TEST_F(AssemblerCTest, BytecodeIsTooBigError) { |
| 153 | + std::string Asm = ".rodata\nglob_initializer:"; |
| 154 | + // The bytecode size limit is (2^16 - 1) * 32 bytes. |
| 155 | + // Each .cell dierective gives 32 bytes. |
| 156 | + for (unsigned Num = 0; Num < (1 << 16) - 1; ++Num) |
| 157 | + Asm += "\n.cell 7"; |
| 158 | + |
| 159 | + LLVMMemoryBufferRef AsmMemBuffer = |
| 160 | + LLVMCreateMemoryBufferWithMemoryRange(Asm.data(), Asm.size(), "test", 0); |
| 161 | + LLVMMemoryBufferRef ObjMemBuffer = nullptr; |
| 162 | + char *ErrMsg = nullptr; |
| 163 | + if (LLVMAssembleEraVM(TM, AsmMemBuffer, &ObjMemBuffer, &ErrMsg)) { |
| 164 | + FAIL() << "Failed to assembly:" << ErrMsg; |
| 165 | + LLVMDisposeMessage(ErrMsg); |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + // Return code 'true' denotes an error. |
| 170 | + EXPECT_FALSE(LLVMExceedsSizeLimitEraVM(ObjMemBuffer, /*metadataSize=*/0)); |
| 171 | + // With the metadata word we exceed the maximal size. |
| 172 | + EXPECT_TRUE(LLVMExceedsSizeLimitEraVM(ObjMemBuffer, /*metadataSize=*/32)); |
| 173 | + |
| 174 | + LLVMDisposeMemoryBuffer(AsmMemBuffer); |
| 175 | + LLVMDisposeMemoryBuffer(ObjMemBuffer); |
| 176 | +} |
0 commit comments