Skip to content

[win/asan] Add a test skeleton for function GetInstructionSize. #116948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2024

Conversation

bernhardu
Copy link
Contributor

Was first part of PR #113085.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Nov 20, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: None (bernhardu)

Changes

Was first part of PR #113085.


Full diff: https://github.com/llvm/llvm-project/pull/116948.diff

3 Files Affected:

  • (modified) compiler-rt/lib/interception/interception_win.cpp (+10)
  • (modified) compiler-rt/lib/interception/interception_win.h (+3)
  • (modified) compiler-rt/lib/interception/tests/interception_win_test.cpp (+32)
diff --git a/compiler-rt/lib/interception/interception_win.cpp b/compiler-rt/lib/interception/interception_win.cpp
index cfd92619a1e15d..ac5c5f7da85be4 100644
--- a/compiler-rt/lib/interception/interception_win.cpp
+++ b/compiler-rt/lib/interception/interception_win.cpp
@@ -544,6 +544,11 @@ 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 +912,11 @@ static size_t GetInstructionSize(uptr address, size_t* rel_offset = nullptr) {
   return 0;
 }
 
+// Unfortunately size_t is not known when compiling asan_allocator.cpp
+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..1c669977302cef 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..29141438f88ffd 100644
--- a/compiler-rt/lib/interception/tests/interception_win_test.cpp
+++ b/compiler-rt/lib/interception/tests/interception_win_test.cpp
@@ -793,6 +793,38 @@ TEST(Interception, EmptyExportTable) {
   EXPECT_EQ(0U, FunPtr);
 }
 
+const struct InstructionSizeData_t {
+  size_t size;       // hold instruction size or 0 for failure, e.g. on control instructions
+  u8 instr[16];
+  size_t rel_offset;
+} data[] = {
+  /* sorted list */
+  {  1, { 0x50 }, 0 },  // 50 : push eax / rax
+};
+
+std::string dumpInstruction(unsigned int arrayIndex, const InstructionSizeData_t& data) {
+    std::stringstream ret;
+    ret << "  with arrayIndex=" << arrayIndex << " ( ";
+    for (size_t byteIndex = 0; byteIndex < data.size; byteIndex++) {
+        ret << std::setfill('0') << std::setw(2) << std::right << std::hex << (int)data.instr[byteIndex] << " ";
+    }
+    ret << ")";
+    return ret.str();
+}
+
+TEST(Interception, GetInstructionSize) {
+
+  size_t size;
+  size_t rel_offset;
+
+  for (unsigned int arrayIndex = 0; arrayIndex < sizeof(data)/sizeof(*data); arrayIndex++) {
+    rel_offset = ~0L;
+    size = __interception::TestOnlyGetInstructionSize((uptr)data[arrayIndex].instr, &rel_offset);
+    EXPECT_EQ(data[arrayIndex].size, size) << dumpInstruction(arrayIndex, data[arrayIndex]);
+    EXPECT_EQ(data[arrayIndex].rel_offset, rel_offset) << dumpInstruction(arrayIndex, data[arrayIndex]);
+  }
+}
+
 }  // namespace __interception
 
 #    endif  // !SANITIZER_WINDOWS_ARM64

@@ -907,6 +912,11 @@ static size_t GetInstructionSize(uptr address, size_t* rel_offset = nullptr) {
return 0;
}

// Unfortunately size_t is not known when compiling asan_allocator.cpp
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the problem with size_t is only in the header file, interception_win.h? If so, can we use SIZE_T only there, and size_t everywhere else? (Or are they not compatible?)

We could also use unsigned for the test function instead of size_t..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I have replaced just the SIZE_T in the test, unfortunately not yet in interception_win.cpp.
Now the only SIZE_T should be in the header.

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 47ef5c4b7f85bc1c8a859d721db9fd1dde7b8d8e 47328b1848e34a7477e892af77fe5221eb92ac26 --extensions cpp,h -- compiler-rt/lib/interception/interception_win.cpp compiler-rt/lib/interception/interception_win.h compiler-rt/lib/interception/tests/interception_win_test.cpp
View the diff from clang-format here.
diff --git a/compiler-rt/lib/interception/interception_win.cpp b/compiler-rt/lib/interception/interception_win.cpp
index ac5c5f7da8..d01548088a 100644
--- a/compiler-rt/lib/interception/interception_win.cpp
+++ b/compiler-rt/lib/interception/interception_win.cpp
@@ -544,7 +544,6 @@ 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;
   }
@@ -913,7 +912,7 @@ static size_t GetInstructionSize(uptr address, size_t* rel_offset = nullptr) {
 }
 
 // Unfortunately size_t is not known when compiling asan_allocator.cpp
-SIZE_T TestOnlyGetInstructionSize(uptr address, SIZE_T* rel_offset) {
+SIZE_T TestOnlyGetInstructionSize(uptr address, SIZE_T *rel_offset) {
   return GetInstructionSize(address, rel_offset);
 }
 
diff --git a/compiler-rt/lib/interception/interception_win.h b/compiler-rt/lib/interception/interception_win.h
index 1c66997730..91c7e38bfe 100644
--- a/compiler-rt/lib/interception/interception_win.h
+++ b/compiler-rt/lib/interception/interception_win.h
@@ -64,7 +64,7 @@ bool OverrideFunctionWithTrampoline(
 void TestOnlyReleaseTrampolineRegions();
 
 // Exposed for unittests
-SIZE_T TestOnlyGetInstructionSize(uptr address, SIZE_T* rel_offset);
+SIZE_T TestOnlyGetInstructionSize(uptr address, SIZE_T *rel_offset);
 
 }  // namespace __interception
 
diff --git a/compiler-rt/lib/interception/tests/interception_win_test.cpp b/compiler-rt/lib/interception/tests/interception_win_test.cpp
index 29141438f8..361168e68b 100644
--- a/compiler-rt/lib/interception/tests/interception_win_test.cpp
+++ b/compiler-rt/lib/interception/tests/interception_win_test.cpp
@@ -794,34 +794,40 @@ TEST(Interception, EmptyExportTable) {
 }
 
 const struct InstructionSizeData_t {
-  size_t size;       // hold instruction size or 0 for failure, e.g. on control instructions
+  size_t size;  // hold instruction size or 0 for failure, e.g. on control
+                // instructions
   u8 instr[16];
   size_t rel_offset;
 } data[] = {
-  /* sorted list */
-  {  1, { 0x50 }, 0 },  // 50 : push eax / rax
+    /* sorted list */
+    {1, {0x50}, 0},  // 50 : push eax / rax
 };
 
-std::string dumpInstruction(unsigned int arrayIndex, const InstructionSizeData_t& data) {
-    std::stringstream ret;
-    ret << "  with arrayIndex=" << arrayIndex << " ( ";
-    for (size_t byteIndex = 0; byteIndex < data.size; byteIndex++) {
-        ret << std::setfill('0') << std::setw(2) << std::right << std::hex << (int)data.instr[byteIndex] << " ";
-    }
-    ret << ")";
-    return ret.str();
+std::string dumpInstruction(unsigned int arrayIndex,
+                            const InstructionSizeData_t &data) {
+  std::stringstream ret;
+  ret << "  with arrayIndex=" << arrayIndex << " ( ";
+  for (size_t byteIndex = 0; byteIndex < data.size; byteIndex++) {
+    ret << std::setfill('0') << std::setw(2) << std::right << std::hex
+        << (int)data.instr[byteIndex] << " ";
+  }
+  ret << ")";
+  return ret.str();
 }
 
 TEST(Interception, GetInstructionSize) {
-
   size_t size;
   size_t rel_offset;
 
-  for (unsigned int arrayIndex = 0; arrayIndex < sizeof(data)/sizeof(*data); arrayIndex++) {
+  for (unsigned int arrayIndex = 0; arrayIndex < sizeof(data) / sizeof(*data);
+       arrayIndex++) {
     rel_offset = ~0L;
-    size = __interception::TestOnlyGetInstructionSize((uptr)data[arrayIndex].instr, &rel_offset);
-    EXPECT_EQ(data[arrayIndex].size, size) << dumpInstruction(arrayIndex, data[arrayIndex]);
-    EXPECT_EQ(data[arrayIndex].rel_offset, rel_offset) << dumpInstruction(arrayIndex, data[arrayIndex]);
+    size = __interception::TestOnlyGetInstructionSize(
+        (uptr)data[arrayIndex].instr, &rel_offset);
+    EXPECT_EQ(data[arrayIndex].size, size)
+        << dumpInstruction(arrayIndex, data[arrayIndex]);
+    EXPECT_EQ(data[arrayIndex].rel_offset, rel_offset)
+        << dumpInstruction(arrayIndex, data[arrayIndex]);
   }
 }
 

@bernhardu bernhardu force-pushed the mr/interception_win/add-test-skeleton branch from 47328b1 to 7324976 Compare November 20, 2024 21:13
@bernhardu
Copy link
Contributor Author

Changes in v2:

  • Really left just SIZE_T in the header.
  • s/unsigned int/unsigned/
  • s/InstructionSizeData_t/InstructionSizeData/
  • s/byteIndex/i/
  • s/arrayIndex/i/
  • checked commit with git-clang-format --diff 47ef5c4b7f85

@bernhardu bernhardu force-pushed the mr/interception_win/add-test-skeleton branch from 7324976 to b4919e8 Compare November 20, 2024 22:30
@bernhardu
Copy link
Contributor Author

Changes in v3:

  • Format the output more like the test line would appear. E.g. before ( 70 71 ), now {0x70, 0x71}.
  • Make the comment part of the test data as a string.

Unfortunately, when later adding the longer instruction sequences,
they will still get split to different lines by git-clang-format.

Copy link
Collaborator

@zmodem zmodem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Unfortunately, when later adding the longer instruction sequences,
they will still get split to different lines by git-clang-format.

You can add directives like this:

to disable clang-format on a range of lines.

@zmodem
Copy link
Collaborator

zmodem commented Nov 25, 2024

I think you can press the "Squash and merge" button now to land this. If not, let me know and I can do it for you.

@bernhardu
Copy link
Contributor Author

I think you can press the "Squash and merge" button now to land this. If not, let me know and I can do it for you.

Thanks, but I don't see such a button, I guess I have not deserved the needed write permission yet?

@zmodem zmodem merged commit 36b1811 into llvm:main Nov 26, 2024
6 checks passed
Copy link

@bernhardu Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@bernhardu bernhardu deleted the mr/interception_win/add-test-skeleton branch November 28, 2024 22:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants