-
Notifications
You must be signed in to change notification settings - Fork 4.2k
GH-50186: [C++][Gandiva] REPLACE throws "Buffer overflow for output string" for results larger than 64 KB #50187
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
Open
lriggs
wants to merge
13
commits into
apache:main
Choose a base branch
from
lriggs:replaceLimit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+301
−4
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e2b7c99
Fix REPLACE buffer overflow for large output strings
lriggs 6d6bf39
More tests
lriggs 60653bf
TODO: benchmark
lriggs 3878dc4
New approach to avoid full scan in some cases
lriggs 2c4741e
addresssed pr feedback
lriggs 6470eb0
lint
lriggs 11a880b
Merge remote-tracking branch 'upstream/main' into replaceLimit
lriggs 652ccad
use int64 for counters to prevent overflow
lriggs e9c172d
fix lint
lriggs 97850bb
Fix benchmark test and remove from standard test suite.
lriggs 562dcf9
Fix lambda resets in benchmark
lriggs 89d4b94
Move string ops benchmark to fit existing framework.
lriggs 62860ec
Lint
lriggs 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
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,162 @@ | ||
| // 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. | ||
|
|
||
| // Microbenchmark comparing the current REPLACE implementation against the | ||
| // pre-change one, to measure the cost of the match-counting scan the fix added | ||
| // to size the output buffer. | ||
| // | ||
| // BM_ReplaceNew = replace_utf8_utf8_utf8 (upper bound or counting scan, then a | ||
| // single write pass) | ||
| // BM_ReplaceOld = replace_with_max_len_utf8_utf8_utf8(..., capacity, ...) given | ||
| // an exact buffer: the pre-change algorithm with no counting | ||
| // scan. Compare the two rows per case to read the scan's cost. | ||
| // | ||
| // Unlike the projector-level micro_benchmarks, this calls the precompiled | ||
| // functions directly, so the build compiles string_ops.cc with GANDIVA_UNIT_TEST. | ||
|
|
||
| #include <cstdint> | ||
| #include <cstring> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
|
|
||
| #include "gandiva/execution_context.h" | ||
| #include "gandiva/precompiled/types.h" | ||
|
|
||
| namespace gandiva { | ||
| namespace { | ||
|
|
||
| struct ReplaceCase { | ||
| const char* name; | ||
| int64_t text_len; | ||
| int stride; // a match (the first byte of `from`) every `stride` bytes | ||
| const char* from; | ||
| const char* to; | ||
| }; | ||
|
|
||
| const std::vector<ReplaceCase>& Cases() { | ||
| static const std::vector<ReplaceCase> cases = { | ||
| // Small expansion (to_len - from_len <= from_len): no scan, upper bound. | ||
| {"small/dense expand a->ab", 256, 1, "a", "ab"}, | ||
| {"small/sparse expand a->ab", 256, 64, "a", "ab"}, | ||
| {"medium/dense expand a->ab", 64 * 1024, 1, "a", "ab"}, | ||
| {"medium/sparse expand a->ab", 64 * 1024, 64, "a", "ab"}, | ||
| {"large/dense expand a->ab", 4 * 1024 * 1024, 1, "a", "ab"}, | ||
| {"large/sparse expand a->ab", 4 * 1024 * 1024, 64, "a", "ab"}, | ||
| // Big expansion (to_len - from_len > from_len): falls back to the scan. | ||
| {"large/dense bigexp a->abcd", 4 * 1024 * 1024, 1, "a", "abcd"}, | ||
| {"large/sparse bigexp a->abcd", 4 * 1024 * 1024, 64, "a", "abcd"}, | ||
| // Shrink (to_len <= from_len): no scan. | ||
| {"large/dense shrink ab->a", 4 * 1024 * 1024, 2, "ab", "a"}, | ||
| }; | ||
| return cases; | ||
| } | ||
|
|
||
| // Builds a `len`-byte string with `match` once every `stride` bytes. | ||
| std::string MakeText(int64_t len, int stride, char match, char filler) { | ||
| std::string s(static_cast<size_t>(len), filler); | ||
| for (int64_t i = 0; i < len; i += stride) { | ||
| s[static_cast<size_t>(i)] = match; | ||
| } | ||
| return s; | ||
| } | ||
|
|
||
| // Exact output size, so the "old" arm gets a buffer large enough to complete. | ||
| int32_t ExactCapacity(const std::string& text, const char* from, int flen, int olen) { | ||
| int64_t matches = 0; | ||
| auto tlen = static_cast<int32_t>(text.size()); | ||
| if (flen > 0 && flen <= tlen) { | ||
| for (int32_t i = 0; i <= tlen - flen;) { | ||
| if (memcmp(text.data() + i, from, flen) == 0) { | ||
| ++matches; | ||
| i += flen; | ||
| } else { | ||
| ++i; | ||
| } | ||
| } | ||
| } | ||
| return static_cast<int32_t>(tlen + matches * (olen - flen)); | ||
| } | ||
|
|
||
| void BM_ReplaceNew(benchmark::State& state) { | ||
| const ReplaceCase& c = Cases()[state.range(0)]; | ||
| auto flen = static_cast<int>(strlen(c.from)); | ||
| auto olen = static_cast<int>(strlen(c.to)); | ||
| std::string text = MakeText(c.text_len, c.stride, c.from[0], 'x'); | ||
| auto tlen = static_cast<int32_t>(text.size()); | ||
| ExecutionContext ctx; | ||
| auto ctx_ptr = reinterpret_cast<int64_t>(&ctx); | ||
|
|
||
| // One warm-up call doubling as a correctness guard. | ||
| int32_t out_len = 0; | ||
| replace_utf8_utf8_utf8(ctx_ptr, text.data(), tlen, c.from, flen, c.to, olen, &out_len); | ||
| if (ctx.has_error()) { | ||
| state.SkipWithError(ctx.get_error().c_str()); | ||
| return; | ||
| } | ||
|
|
||
| for (auto _ : state) { | ||
| ctx.Reset(); | ||
| const char* out = replace_utf8_utf8_utf8(ctx_ptr, text.data(), tlen, c.from, flen, | ||
| c.to, olen, &out_len); | ||
| benchmark::DoNotOptimize(out); | ||
| benchmark::DoNotOptimize(out_len); | ||
| } | ||
| state.SetBytesProcessed(state.iterations() * tlen); | ||
| state.SetLabel(c.name); | ||
| } | ||
|
|
||
| void BM_ReplaceOld(benchmark::State& state) { | ||
| const ReplaceCase& c = Cases()[state.range(0)]; | ||
| auto flen = static_cast<int>(strlen(c.from)); | ||
| auto olen = static_cast<int>(strlen(c.to)); | ||
| std::string text = MakeText(c.text_len, c.stride, c.from[0], 'x'); | ||
| auto tlen = static_cast<int32_t>(text.size()); | ||
| int32_t capacity = ExactCapacity(text, c.from, flen, olen); | ||
| ExecutionContext ctx; | ||
| auto ctx_ptr = reinterpret_cast<int64_t>(&ctx); | ||
|
|
||
| int32_t out_len = 0; | ||
| replace_with_max_len_utf8_utf8_utf8(ctx_ptr, text.data(), tlen, c.from, flen, c.to, | ||
| olen, capacity, &out_len); | ||
| if (ctx.has_error()) { | ||
| state.SkipWithError(ctx.get_error().c_str()); | ||
| return; | ||
| } | ||
|
|
||
| for (auto _ : state) { | ||
| ctx.Reset(); | ||
| const char* out = replace_with_max_len_utf8_utf8_utf8( | ||
| ctx_ptr, text.data(), tlen, c.from, flen, c.to, olen, capacity, &out_len); | ||
| benchmark::DoNotOptimize(out); | ||
| benchmark::DoNotOptimize(out_len); | ||
| } | ||
| state.SetBytesProcessed(state.iterations() * tlen); | ||
| state.SetLabel(c.name); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| BENCHMARK(BM_ReplaceNew) | ||
| ->DenseRange(0, static_cast<int64_t>(Cases().size()) - 1) | ||
| ->Unit(benchmark::kMicrosecond); | ||
| BENCHMARK(BM_ReplaceOld) | ||
| ->DenseRange(0, static_cast<int64_t>(Cases().size()) - 1) | ||
| ->Unit(benchmark::kMicrosecond); | ||
|
|
||
| } // namespace gandiva |
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.