Skip to content

[HWASAN]Implement memcmp interceptor in HWASAN #67204

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 10 commits into from
Sep 29, 2023

Conversation

kstoimenov
Copy link
Contributor

The plan is to fix memcmp interceptor in HWASAN and remove the unsupported statement at that time.

@llvmbot
Copy link
Member

llvmbot commented Sep 22, 2023

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

Changes

The plan is to fix memcmp interceptor in HWASAN and remove the unsupported statement at that time.


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

1 Files Affected:

  • (added) compiler-rt/test/sanitizer_common/TestCases/memcmp.cpp (+15)
diff --git a/compiler-rt/test/sanitizer_common/TestCases/memcmp.cpp b/compiler-rt/test/sanitizer_common/TestCases/memcmp.cpp
new file mode 100644
index 000000000000000..23fe8e2cfe0d620
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/memcmp.cpp
@@ -0,0 +1,15 @@
+// RUN: %clangxx -O0 %s -o %t && %run %t
+// XFAIL: *
+// UNSUPPORTED: lsan, ubsan
+// FIXME: HWASAN should work when we have intercepptors.
+// UNSUPPORTED: hwasan
+
+#include <cstring>
+#include <cstdio>
+
+int main(int argc, char** argv) {
+  int *x = new int(7);
+  delete x;
+  // Trigger use after free error.
+  return memcmp(x, &argc, sizeof(int)) == 0 ? 1 : 0;
+}
\ No newline at end of file

@github-actions
Copy link

github-actions bot commented Sep 22, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.


#include <string.h>
int main(int argc, char **argv) {
char a1[] = {static_cast<char>(argc), 2, 3, 4};
Copy link
Collaborator

Choose a reason for hiding this comment

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

if you do heap allocation we can test in aliasing mode

Copy link
Collaborator

Choose a reason for hiding this comment

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

// XFAIL: *

I believe "// UNSUPPORTED: hwasan" does not work in hwasan, this is sanitizer_common feature.
I guess check-compiler-rt does not pass with this patch

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 it's worth porting bcmp_test.cpp and installing bcmp interceptor

memcmp_strict_test - maybe for consistencly

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I will implement the feature and include the test with in instead of disabling the test.

Copy link
Collaborator

Choose a reason for hiding this comment

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

bcmp ?

@kstoimenov kstoimenov marked this pull request as draft September 25, 2023 23:19
DON NOT SUBMIT - NEED TO TEST ON ARM
@kstoimenov kstoimenov changed the title [HWASAN] Add test to detected use after free in memcmp [HWASAN]Implement memcmp interceptor in HWASAN Sep 25, 2023
@kstoimenov kstoimenov marked this pull request as ready for review September 26, 2023 17:43

#include <string.h>
int main(int argc, char **argv) {
char a1[] = {static_cast<char>(argc), 2, 3, 4};
Copy link
Collaborator

Choose a reason for hiding this comment

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

bcmp ?

@vitalybuka
Copy link
Collaborator

bcmp is OK for another PR
But please try make it work on x86 aliasing mode

kstoimenov and others added 6 commits September 28, 2023 21:12
because the compiler knew it was 4 bytes so it just inlined the check.
This looks like a bug in HWASAN code generation because there was no
memory checks added to this code.
@vitalybuka vitalybuka merged commit 46c1671 into llvm:main Sep 29, 2023
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.

5 participants