[compiler-rt] [win/asan] Add a test skeleton for function GetInstructionSize. (PR #116948)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 20 14:30:49 PST 2024
https://github.com/bernhardu updated https://github.com/llvm/llvm-project/pull/116948
>From b4919e88110924a305cd98408d47cb880b6bb6a7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bernhard=20=C3=9Cbelacker?= <bernhardu at mailbox.org>
Date: Thu, 7 Nov 2024 15:00:52 +0100
Subject: [PATCH] [win/asan] Add a test skeleton for function
GetInstructionSize.
---
.../lib/interception/interception_win.cpp | 8 +++++
.../lib/interception/interception_win.h | 3 ++
.../tests/interception_win_test.cpp | 35 +++++++++++++++++++
3 files changed, 46 insertions(+)
diff --git a/compiler-rt/lib/interception/interception_win.cpp b/compiler-rt/lib/interception/interception_win.cpp
index cfd92619a1e15d..d56b091822a231 100644
--- a/compiler-rt/lib/interception/interception_win.cpp
+++ b/compiler-rt/lib/interception/interception_win.cpp
@@ -544,6 +544,10 @@ static const u8 kPrologueWithShortJump2[] = {
// Returns 0 on error.
static size_t GetInstructionSize(uptr address, size_t* rel_offset = nullptr) {
+ if (rel_offset) {
+ *rel_offset = 0;
+ }
+
#if SANITIZER_ARM64
// An ARM64 instruction is 4 bytes long.
return 4;
@@ -907,6 +911,10 @@ static size_t GetInstructionSize(uptr address, size_t* rel_offset = nullptr) {
return 0;
}
+size_t TestOnlyGetInstructionSize(uptr address, size_t *rel_offset) {
+ return GetInstructionSize(address, rel_offset);
+}
+
// Returns 0 on error.
static size_t RoundUpToInstrBoundary(size_t size, uptr address) {
size_t cursor = 0;
diff --git a/compiler-rt/lib/interception/interception_win.h b/compiler-rt/lib/interception/interception_win.h
index f6eca82191cba5..91c7e38bfe7dbf 100644
--- a/compiler-rt/lib/interception/interception_win.h
+++ b/compiler-rt/lib/interception/interception_win.h
@@ -63,6 +63,9 @@ bool OverrideFunctionWithTrampoline(
// Exposed for unittests
void TestOnlyReleaseTrampolineRegions();
+// Exposed for unittests
+SIZE_T TestOnlyGetInstructionSize(uptr address, SIZE_T *rel_offset);
+
} // namespace __interception
#if defined(INTERCEPTION_DYNAMIC_CRT)
diff --git a/compiler-rt/lib/interception/tests/interception_win_test.cpp b/compiler-rt/lib/interception/tests/interception_win_test.cpp
index c004d187768de6..0650a45f619a9f 100644
--- a/compiler-rt/lib/interception/tests/interception_win_test.cpp
+++ b/compiler-rt/lib/interception/tests/interception_win_test.cpp
@@ -793,6 +793,41 @@ TEST(Interception, EmptyExportTable) {
EXPECT_EQ(0U, FunPtr);
}
+const struct InstructionSizeData {
+ size_t size; // hold instruction size or 0 for failure,
+ // e.g. on control instructions
+ u8 instr[16];
+ size_t rel_offset;
+ const char *comment;
+} data[] = {
+ /* sorted list */
+ {1, {0x50}, 0, "50 : push eax / rax"},
+};
+
+std::string dumpInstruction(unsigned arrayIndex,
+ const InstructionSizeData &data) {
+ std::stringstream ret;
+ ret << " with arrayIndex=" << arrayIndex << " {";
+ for (size_t i = 0; i < data.size; i++) {
+ if (i > 0)
+ ret << ", ";
+ ret << "0x" << std::setfill('0') << std::setw(2) << std::right << std::hex
+ << (int)data.instr[i];
+ }
+ ret << "} " << data.comment;
+ return ret.str();
+}
+
+TEST(Interception, GetInstructionSize) {
+ for (unsigned i = 0; i < sizeof(data) / sizeof(*data); i++) {
+ size_t rel_offset = ~0L;
+ size_t size = __interception::TestOnlyGetInstructionSize(
+ (uptr)data[i].instr, &rel_offset);
+ EXPECT_EQ(data[i].size, size) << dumpInstruction(i, data[i]);
+ EXPECT_EQ(data[i].rel_offset, rel_offset) << dumpInstruction(i, data[i]);
+ }
+}
+
} // namespace __interception
# endif // !SANITIZER_WINDOWS_ARM64
More information about the llvm-commits
mailing list