Skip to content
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

[lsan] Fix free(NULL) interception during initialization #106912

Merged
merged 1 commit into from
Sep 11, 2024

Conversation

tmiasko
Copy link
Contributor

@tmiasko tmiasko commented Sep 1, 2024

Previously an attempt to free a null pointer during initialization would fail on ENSURE_LSAN_INITED assertion (since a null pointer is not owned by DlsymAlloc).

@llvmbot
Copy link
Member

llvmbot commented Sep 1, 2024

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

Author: None (tmiasko)

Changes

Previously an attempt to free a null pointer during initialization would fail on ENSURE_LSAN_INITED assertion (since a null pointer is not owned by DlsymAlloc).


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

1 Files Affected:

  • (modified) compiler-rt/lib/lsan/lsan_interceptors.cpp (+2)
diff --git a/compiler-rt/lib/lsan/lsan_interceptors.cpp b/compiler-rt/lib/lsan/lsan_interceptors.cpp
index b569c337e97641..db27be7d06995f 100644
--- a/compiler-rt/lib/lsan/lsan_interceptors.cpp
+++ b/compiler-rt/lib/lsan/lsan_interceptors.cpp
@@ -77,6 +77,8 @@ INTERCEPTOR(void*, malloc, uptr size) {
 }
 
 INTERCEPTOR(void, free, void *p) {
+  if (!p)
+    return;
   if (DlsymAlloc::PointerIsMine(p))
     return DlsymAlloc::Free(p);
   ENSURE_LSAN_INITED;

@@ -77,6 +77,8 @@ INTERCEPTOR(void*, malloc, uptr size) {
}

INTERCEPTOR(void, free, void *p) {
if (!p)
Copy link
Collaborator

Choose a reason for hiding this comment

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

this will fix only lsan

to fix all, I propose to change
DlsymAlloc::PointerIsMine to return true for NULL
and teach DlsymAlloc::Free to handle it correctly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed there are a few more sanitizers that missed this special case.

I would suggest fixing those by considering NULL, since DlsymAlloc owning NULL would be counterproductive for realloc.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see, you are correct.

Can you please include all other sanitizers here, also cfree, and please use UNLIKELY(!p)

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 updated the patch to include UNLIKELY in the condition.

For lsan cfree interceptor is aliased to free and doesn't need further changes.

I looked a little bit more at other sanitizers and I don't think they actually need any further changes. For those that don't immediately return for null, it is fine if execution just falls through to the main deallocation function, since null case is handled early on there.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please rebase and include into the patch removal of XFAIL added in #108289

@vitalybuka vitalybuka self-requested a review September 6, 2024 21:29
vitalybuka added a commit that referenced this pull request Sep 11, 2024
Almost all sanitizers already support the test.
* Tsan does not use DlsymAlloc yet.
* Lsan will support with #106912.

memprof,rtsan,nsan are not tested as part of
sanitizer_common, but we should keep them here to
show up when it happen.

---------

Co-authored-by: Xiaofeng Tian <110771974+txff99@users.noreply.github.com>
Copy link
Collaborator

@vitalybuka vitalybuka left a comment

Choose a reason for hiding this comment

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

With update test

Previously an attempt to free a null pointer during initialization would
fail on ENSURE_LSAN_INITED assertion (since a null pointer is not owned
by DlsymAlloc).
@tmiasko
Copy link
Contributor Author

tmiasko commented Sep 11, 2024

Thanks. Can you merge this for me?

Copy link
Collaborator

@vitalybuka vitalybuka left a comment

Choose a reason for hiding this comment

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

Thanks!

@vitalybuka
Copy link
Collaborator

Thanks. Can you merge this for me?

Will do!

@vitalybuka vitalybuka merged commit ae0ed3d into llvm:main Sep 11, 2024
5 of 6 checks passed
@tmiasko tmiasko deleted the lsan-free-null branch September 12, 2024 03:43
VitaNuo pushed a commit to VitaNuo/llvm-project that referenced this pull request Sep 12, 2024
Almost all sanitizers already support the test.
* Tsan does not use DlsymAlloc yet.
* Lsan will support with llvm#106912.

memprof,rtsan,nsan are not tested as part of
sanitizer_common, but we should keep them here to
show up when it happen.

---------

Co-authored-by: Xiaofeng Tian <110771974+txff99@users.noreply.github.com>
VitaNuo pushed a commit to VitaNuo/llvm-project that referenced this pull request Sep 12, 2024
Previously an attempt to free a null pointer during initialization would
fail on ENSURE_LSAN_INITED assertion (since a null pointer is not owned
by DlsymAlloc).
@aeubanks
Copy link
Contributor

this seems to have broken the test on mac: https://green.lab.llvm.org/job/llvm.org/job/clang-stage1-RA/2058/

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.

4 participants