-
Notifications
You must be signed in to change notification settings - Fork 4k
GH-41460: [C++] Use ASAN to poison temp vector stack memory #41695
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e8d7ab4
WIP
zanmato1984 bf9abee
WIP
zanmato1984 e6e5dd8
Finalize
zanmato1984 9da611e
Remove dead code
zanmato1984 b75b804
Add macro check for asan code
zanmato1984 4100c32
Fix
zanmato1984 3f5a3f0
Use macros for copy/assign and move/assign
zanmato1984 142419c
Add guard back
zanmato1984 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,16 +20,29 @@ | |
| #include "arrow/compute/util.h" | ||
| #include "arrow/memory_pool.h" | ||
|
|
||
| #ifdef ADDRESS_SANITIZER | ||
| #include <sanitizer/asan_interface.h> | ||
| #endif | ||
|
|
||
| namespace arrow { | ||
| namespace util { | ||
|
|
||
| TempVectorStack::~TempVectorStack() { | ||
| #ifdef ADDRESS_SANITIZER | ||
| if (buffer_) { | ||
| ASAN_UNPOISON_MEMORY_REGION(buffer_->mutable_data(), buffer_size_); | ||
| } | ||
|
Comment on lines
+32
to
+34
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must unpoison the buffer before releasing. |
||
| #endif | ||
| } | ||
|
|
||
| Status TempVectorStack::Init(MemoryPool* pool, int64_t size) { | ||
| num_vectors_ = 0; | ||
| top_ = 0; | ||
| buffer_size_ = EstimatedAllocationSize(size); | ||
| ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateResizableBuffer(size, pool)); | ||
| // Ensure later operations don't accidentally read uninitialized memory. | ||
| std::memset(buffer->mutable_data(), 0xFF, size); | ||
| #ifdef ADDRESS_SANITIZER | ||
| ASAN_POISON_MEMORY_REGION(buffer->mutable_data(), size); | ||
| #endif | ||
| buffer_ = std::move(buffer); | ||
| return Status::OK(); | ||
| } | ||
|
|
@@ -53,12 +66,17 @@ void TempVectorStack::alloc(uint32_t num_bytes, uint8_t** data, int* id) { | |
| ARROW_CHECK_LE(new_top, buffer_size_) | ||
| << "TempVectorStack::alloc overflow: allocating " << estimated_alloc_size | ||
| << " on top of " << top_ << " in stack of size " << buffer_size_; | ||
| *data = buffer_->mutable_data() + top_ + sizeof(uint64_t); | ||
| #ifdef ADDRESS_SANITIZER | ||
| ASAN_UNPOISON_MEMORY_REGION(buffer_->mutable_data() + top_, estimated_alloc_size); | ||
| #endif | ||
| *data = buffer_->mutable_data() + top_ + /*one guard*/ sizeof(uint64_t); | ||
| #ifndef NDEBUG | ||
| // We set 8 bytes before the beginning of the allocated range and | ||
| // 8 bytes after the end to check for stack overflow (which would | ||
| // result in those known bytes being corrupted). | ||
| reinterpret_cast<uint64_t*>(buffer_->mutable_data() + top_)[0] = kGuard1; | ||
| reinterpret_cast<uint64_t*>(buffer_->mutable_data() + new_top)[-1] = kGuard2; | ||
| #endif | ||
| *id = num_vectors_++; | ||
| top_ = new_top; | ||
| } | ||
|
|
@@ -72,6 +90,9 @@ void TempVectorStack::release(int id, uint32_t num_bytes) { | |
| top_ -= size; | ||
| ARROW_DCHECK(reinterpret_cast<const uint64_t*>(buffer_->mutable_data() + top_)[0] == | ||
| kGuard1); | ||
| #ifdef ADDRESS_SANITIZER | ||
| ASAN_POISON_MEMORY_REGION(buffer_->mutable_data() + top_, size); | ||
| #endif | ||
| --num_vectors_; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some platforms don't have ASAN, so wrap the ASAN related code with a flag that is only defined with ASAN.