Skip to content

Commit

Permalink
[JITLink][AArch32] Unittest for error paths of readAddend and applyFi…
Browse files Browse the repository at this point in the history
…xup functionality (#69636)

This test checks for error paths in relocation dependent functions of readAddend and applyFixup. It is useful to check these to avoid unexpected assert errors. Currently opcode errors are triggered in most of the cases in AArch32 but there might be further checks to look for in the future. Different backends can also implement a similar test.
  • Loading branch information
eymay authored and zahiraam committed Nov 20, 2023
1 parent 66e1a0a commit d98fba7
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 2 deletions.
4 changes: 2 additions & 2 deletions llvm/include/llvm/ExecutionEngine/JITLink/aarch32.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ template <> struct FixupInfo<Arm_MovwAbsNC> : public FixupInfo<Arm_MovtAbs> {
};

template <> struct FixupInfo<Thumb_Jump24> {
static constexpr HalfWords Opcode{0xf000, 0x8000};
static constexpr HalfWords OpcodeMask{0xf800, 0x8000};
static constexpr HalfWords Opcode{0xf000, 0x9000};
static constexpr HalfWords OpcodeMask{0xf800, 0x9000};
static constexpr HalfWords ImmMask{0x07ff, 0x2fff};
};

Expand Down
185 changes: 185 additions & 0 deletions llvm/unittests/ExecutionEngine/JITLink/AArch32ErrorTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
//===------- AArch32ErrorTests.cpp - Test AArch32 error handling ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <llvm/ExecutionEngine/JITLink/aarch32.h>

#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"

using namespace llvm;
using namespace llvm::jitlink;
using namespace llvm::jitlink::aarch32;
using namespace llvm::support;
using namespace llvm::support::endian;

constexpr unsigned PointerSize = 4;
auto G = std::make_unique<LinkGraph>("foo", Triple("armv7-linux-gnueabi"),
PointerSize, llvm::endianness::little,
getGenericEdgeKindName);
auto &Sec =
G->createSection("__data", orc::MemProt::Read | orc::MemProt::Write);

auto ArmCfg = getArmConfigForCPUArch(ARMBuildAttrs::v7);

constexpr uint64_t ArmAlignment = 4;
constexpr uint64_t ThumbAlignment = 2;
constexpr uint64_t AlignmentOffset = 0;

constexpr orc::ExecutorAddrDiff SymbolOffset = 0;
constexpr orc::ExecutorAddrDiff SymbolSize = 4;

TEST(AArch32_ELF, readAddendArmErrors) {

constexpr orc::ExecutorAddr B1DummyAddr(0x1000);

// Permanently undefined instruction in ARM
// udf #0
uint8_t ArmWord[] = {0xf0, 0x00, 0xf0, 0xe7};
ArrayRef<char> ArmContent(reinterpret_cast<const char *>(&ArmWord),
sizeof(ArmWord));
auto &BArm = G->createContentBlock(Sec, ArmContent, B1DummyAddr, ArmAlignment,
AlignmentOffset);
Symbol &TargetSymbol =
G->addAnonymousSymbol(BArm, SymbolOffset, SymbolSize, false, false);
Edge InvalidEdge(Edge::GenericEdgeKind::Invalid, 0 /*Offset*/, TargetSymbol,
0 /*Addend*/);

// Edge kind is tested, block itself is not significant here. So it is tested
// once in Arm
EXPECT_THAT_EXPECTED(readAddendData(*G, BArm, InvalidEdge),
FailedWithMessage(testing::HasSubstr(
"can not read implicit addend for aarch32 edge kind "
"INVALID RELOCATION")));

EXPECT_THAT_EXPECTED(readAddendArm(*G, BArm, InvalidEdge),
FailedWithMessage(testing::HasSubstr(
"can not read implicit addend for aarch32 edge kind "
"INVALID RELOCATION")));

EXPECT_THAT_EXPECTED(readAddendThumb(*G, BArm, InvalidEdge, ArmCfg),
FailedWithMessage(testing::HasSubstr(
"can not read implicit addend for aarch32 edge kind "
"INVALID RELOCATION")));

for (Edge::Kind K = FirstArmRelocation; K < LastArmRelocation; K += 1) {
Edge E(K, 0, TargetSymbol, 0);
EXPECT_THAT_EXPECTED(
readAddendArm(*G, BArm, E),
FailedWithMessage(testing::StartsWith("Invalid opcode")));
}
}

TEST(AArch32_ELF, readAddendThumbErrors) {

constexpr orc::ExecutorAddr B2DummyAddr(0x2000);

// Permanently undefined instruction in Thumb
// udf #0
//
// 11110:op:imm4:1:op1:imm12
// op = 1111111 Permanent undefined
// op1 = 010
//
constexpr HalfWords ThumbHalfWords{0xf7f0, 0xa000};
ArrayRef<char> ThumbContent(reinterpret_cast<const char *>(&ThumbHalfWords),
sizeof(ThumbHalfWords));
auto &BThumb = G->createContentBlock(Sec, ThumbContent, B2DummyAddr,
ThumbAlignment, AlignmentOffset);
Symbol &TargetSymbol =
G->addAnonymousSymbol(BThumb, SymbolOffset, SymbolSize, false, false);
Edge InvalidEdge(Edge::GenericEdgeKind::Invalid, 0 /*Offset*/, TargetSymbol,
0 /*Addend*/);

for (Edge::Kind K = FirstThumbRelocation; K < LastThumbRelocation; K += 1) {
Edge E(K, 0, TargetSymbol, 0);
EXPECT_THAT_EXPECTED(
readAddendThumb(*G, BThumb, E, ArmCfg),
FailedWithMessage(testing::StartsWith("Invalid opcode")));
}
}

TEST(AArch32_ELF, applyFixupArmErrors) {

constexpr orc::ExecutorAddr B3DummyAddr(0x5000);

uint8_t ArmWord[] = {0xf0, 0x00, 0xf0, 0xe7};
MutableArrayRef<char> MutableArmContent(reinterpret_cast<char *>(ArmWord),
sizeof(ArmWord));

auto &BArm = G->createMutableContentBlock(Sec, MutableArmContent, B3DummyAddr,
ArmAlignment, AlignmentOffset);

Symbol &TargetSymbol =
G->addAnonymousSymbol(BArm, SymbolOffset, SymbolSize, false, false);
Edge InvalidEdge(Edge::GenericEdgeKind::Invalid, 0 /*Offset*/, TargetSymbol,
0 /*Addend*/);

// Edge kind is tested, block itself is not significant here. So it is tested
// once in Arm
EXPECT_THAT_ERROR(
applyFixupData(*G, BArm, InvalidEdge),
FailedWithMessage(testing::HasSubstr(
"encountered unfixable aarch32 edge kind INVALID RELOCATION")));
EXPECT_THAT_ERROR(
applyFixupArm(*G, BArm, InvalidEdge),
FailedWithMessage(testing::HasSubstr(
"encountered unfixable aarch32 edge kind INVALID RELOCATION")));
EXPECT_THAT_ERROR(
applyFixupThumb(*G, BArm, InvalidEdge, ArmCfg),
FailedWithMessage(testing::HasSubstr(
"encountered unfixable aarch32 edge kind INVALID RELOCATION")));

for (Edge::Kind K = FirstArmRelocation; K < LastArmRelocation; K += 1) {
Edge E(K, 0, TargetSymbol, 0);
EXPECT_THAT_ERROR(applyFixupArm(*G, BArm, E),
FailedWithMessage(testing::AllOf(
testing::StartsWith("Invalid opcode"),
testing::EndsWith(G->getEdgeKindName(K)))));
}
}

TEST(AArch32_ELF, applyFixupThumbErrors) {

struct MutableHalfWords {
constexpr MutableHalfWords(HalfWords Preset)
: Hi(Preset.Hi), Lo(Preset.Lo) {}

uint16_t Hi; // First halfword
uint16_t Lo; // Second halfword
};

constexpr orc::ExecutorAddr B4DummyAddr(0x6000);

// Permanently undefined instruction in Thumb
// udf #0
//
// 11110:op:imm4:1:op1:imm12
// op = 1111111 Permanent undefined
// op1 = 010
//
constexpr HalfWords ThumbHalfWords{0xf7f0, 0xa000};
MutableHalfWords MutableThumbHalfWords{ThumbHalfWords};
MutableArrayRef<char> MutableThumbContent(
reinterpret_cast<char *>(&MutableThumbHalfWords),
sizeof(MutableThumbHalfWords));

auto &BThumb = G->createMutableContentBlock(
Sec, MutableThumbContent, B4DummyAddr, ThumbAlignment, AlignmentOffset);
Symbol &TargetSymbol =
G->addAnonymousSymbol(BThumb, SymbolOffset, SymbolSize, false, false);
Edge InvalidEdge(Edge::GenericEdgeKind::Invalid, 0 /*Offset*/, TargetSymbol,
0 /*Addend*/);

for (Edge::Kind K = FirstThumbRelocation; K < LastThumbRelocation; K += 1) {
Edge E(K, 0, TargetSymbol, 0);
EXPECT_THAT_ERROR(applyFixupThumb(*G, BThumb, E, ArmCfg),
FailedWithMessage(testing::AllOf(
testing::StartsWith("Invalid opcode"),
testing::EndsWith(G->getEdgeKindName(K)))));
}
}
1 change: 1 addition & 0 deletions llvm/unittests/ExecutionEngine/JITLink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(LLVM_LINK_COMPONENTS

add_llvm_unittest(JITLinkTests
AArch32Tests.cpp
AArch32ErrorTests.cpp
EHFrameSupportTests.cpp
JITLinkMocks.cpp
LinkGraphTests.cpp
Expand Down

0 comments on commit d98fba7

Please sign in to comment.