-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-9285: [C++] Detect unauthorized memory allocations in function kernels #12089
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
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0f4919d
Initial changes for checking whether additional buffers were created …
joosthooz 69c62ba
Moved AddBuffersToSet into exec.cc, added check to Vector Executor
joosthooz a7ec5e4
Added test for detecting allocation misbehavior in Scalar kernels
joosthooz 905a193
Fixes to formatting, removed unused includes
joosthooz 9997e6d
Removed test kernel from registry and API, no longer using DCHECK
joosthooz bd6d281
Moved the misbehaving test kernels inside the test file
joosthooz 9ede680
Code cleanup and formatting
joosthooz 817893e
Changed to Buffer* for performance
joosthooz cf720a0
Fixed bug in AddBuffersToSet
joosthooz 58a964c
Changed Error type to "Invalid"
joosthooz ca2e719
Separate checks for validity and other buffers being modified
joosthooz 6420560
Added check for changed Datum type
joosthooz c3901e1
Code formatting
joosthooz 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
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
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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #include <gmock/gmock-matchers.h> | ||
| #include <gtest/gtest.h> | ||
| #include "arrow/array/concatenate.h" | ||
| #include "arrow/compute/api_scalar.h" | ||
| #include "arrow/testing/gtest_util.h" | ||
| #include "arrow/util/logging.h" | ||
|
|
||
| namespace arrow { | ||
| namespace compute { | ||
|
|
||
| struct ScalarReAllocValidBufExec { | ||
| static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) { | ||
| // allocate a validity buffer even though we've promised not to | ||
| ARROW_ASSIGN_OR_RAISE(out->mutable_array()->buffers[0], ctx->AllocateBitmap(8)); | ||
| return Status::OK(); | ||
| } | ||
| }; | ||
|
|
||
| struct ScalarReAllocDataBufExec { | ||
| static Status Exec(KernelContext* ctx, const ExecBatch& batch, Datum* out) { | ||
| // allocate a validity buffer even though we've promised not to | ||
| ARROW_ASSIGN_OR_RAISE(out->mutable_array()->buffers[1], ctx->Allocate(64)); | ||
| return Status::OK(); | ||
| } | ||
| }; | ||
|
|
||
| const FunctionDoc misbehave_doc{ | ||
| "Test kernel that does nothing but allocate memory " | ||
| "while it shouldn't", | ||
| "This Kernel only exists for testing purposes.\n" | ||
| "It allocates memory while it promised not to \n" | ||
| "(because of MemAllocation::PREALLOCATE).", | ||
| {}}; | ||
|
|
||
| TEST(Misbehave, ReallocValidBufferScalarKernel) { | ||
| ExecContext ctx; | ||
| auto func = std::make_shared<ScalarFunction>("scalar_misbehave", Arity::Unary(), | ||
| &misbehave_doc); | ||
| DCHECK_OK(func->AddKernel({InputType(Type::FIXED_SIZE_BINARY)}, | ||
| OutputType(ValueDescr(fixed_size_binary(2))), | ||
| ScalarReAllocValidBufExec::Exec)); | ||
| Datum datum(ArrayFromJSON(fixed_size_binary(6), R"(["123456"])")); | ||
| const std::vector<Datum>& args = {datum}; | ||
| const FunctionOptions* options = nullptr; | ||
| EXPECT_RAISES_WITH_MESSAGE_THAT( | ||
| Invalid, | ||
| testing::HasSubstr("Invalid: " | ||
| "Pre-allocated validity buffer was modified " | ||
| "in function kernel"), | ||
| func->Execute(args, options, &ctx)); | ||
| } | ||
|
|
||
| TEST(Misbehave, ReallocDataBufferScalarKernel) { | ||
| ExecContext ctx; | ||
| auto func = std::make_shared<ScalarFunction>("scalar_misbehave", Arity::Unary(), | ||
| &misbehave_doc); | ||
| DCHECK_OK(func->AddKernel({InputType(Type::FIXED_SIZE_BINARY)}, | ||
| OutputType(ValueDescr(fixed_size_binary(2))), | ||
| ScalarReAllocDataBufExec::Exec)); | ||
| Datum datum(ArrayFromJSON(fixed_size_binary(6), R"(["123456"])")); | ||
| const std::vector<Datum>& args = {datum}; | ||
| const FunctionOptions* options = nullptr; | ||
| EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, | ||
| testing::HasSubstr("Invalid: " | ||
| "Unauthorized memory allocations " | ||
| "in function kernel"), | ||
| func->Execute(args, options, &ctx)); | ||
| } | ||
|
|
||
| } // namespace compute | ||
| } // namespace arrow |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I am curious if hashing all the values-buffers pointers would suffice instead of storing them in a set. We would still need to traverse nested data structures to capture all the child buffers. This could be solved using a Visitor.
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.
Hashing would be nicer, but it would also trigger the error if a kernel deletes a buffer it doesn't need anymore. And I don't think we care about that, just newly allocated ones. What do you think?