-
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
11
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.
+311
−4
Open
Changes from 9 commits
Commits
Show all changes
11 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 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,160 @@ | ||
| // 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 O(n) match-counting scan that the | ||
| // fix added to size the output buffer exactly. | ||
| // | ||
| // new = replace_utf8_utf8_utf8 (counting scan + exact allocation) | ||
| // old = replace_with_max_len_utf8_utf8_utf8(..., capacity, ...) | ||
| // The old wrapper was literally a call to this with a fixed cap of | ||
| // 65535; given a buffer large enough to hold the result it is the | ||
| // pre-change algorithm with no counting scan, so new-vs-old isolates | ||
| // the scan overhead. (The shipped old cap of 65535 additionally errored | ||
| // out for any result above ~64 KB -- the bug this change fixes.) | ||
| // | ||
| // Reported time includes one ExecutionContext::Reset() per call in both arms | ||
| // (it cancels out in the delta), mirroring per-call arena reuse. | ||
|
|
||
| #include <chrono> | ||
| #include <cinttypes> | ||
| #include <cstdint> | ||
| #include <cstdio> | ||
| #include <functional> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "gandiva/execution_context.h" | ||
| #include "gandiva/precompiled/types.h" | ||
|
|
||
| namespace gandiva { | ||
| namespace { | ||
|
|
||
| // Builds a `len`-byte string whose `match` byte occurs once every `stride` | ||
| // bytes (the rest filled with `filler`), giving len/stride non-overlapping | ||
| // matches. | ||
| 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; | ||
| } | ||
|
|
||
| double TimeNsPerCall(const std::function<void()>& fn, int iters) { | ||
| auto t0 = std::chrono::steady_clock::now(); | ||
| for (int i = 0; i < iters; ++i) { | ||
| fn(); | ||
| } | ||
| auto t1 = std::chrono::steady_clock::now(); | ||
| return std::chrono::duration<double, std::nano>(t1 - t0).count() / iters; | ||
| } | ||
|
|
||
| struct Case { | ||
| std::string name; | ||
| int64_t text_len; | ||
| int stride; // a match every `stride` bytes | ||
| std::string from; | ||
| std::string to; | ||
| int iters; | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(StringOpsBenchmark, Replace) { | ||
| ExecutionContext ctx; | ||
| int64_t ctx_ptr = reinterpret_cast<int64_t>(&ctx); | ||
|
|
||
| const int64_t kSmall = 256; | ||
| const int64_t kMedium = 64 * 1024; | ||
| const int64_t kLarge = 4 * 1024 * 1024; | ||
|
|
||
| std::vector<Case> cases = { | ||
| // Expansion cases (to longer than from) -- these take the O(n) scan path. | ||
| {"small/dense expand a->ab", kSmall, 1, "a", "ab", 200000}, | ||
| {"small/sparse expand a->ab", kSmall, 64, "a", "ab", 200000}, | ||
| {"medium/dense expand a->ab", kMedium, 1, "a", "ab", 4000}, | ||
| {"medium/sparse expand a->ab", kMedium, 64, "a", "ab", 4000}, | ||
| {"large/dense expand a->ab", kLarge, 1, "a", "ab", 80}, | ||
| {"large/sparse expand a->ab", kLarge, 64, "a", "ab", 80}, | ||
| // Big-expansion cases (to_len - from_len > from_len) -- these fall back to | ||
| // the exact match-counting scan, so new should still show the scan delta. | ||
| {"large/dense bigexp a->abcd", kLarge, 1, "a", "abcd", 80}, | ||
| {"large/sparse bigexp a->abcd", kLarge, 64, "a", "abcd", 80}, | ||
| // Shrink case (to no longer than from) -- new skips the scan entirely. | ||
| {"large/dense shrink ab->a", kLarge, 2, "ab", "a", 80}, | ||
| }; | ||
|
|
||
| printf("\n%-28s %10s %9s %12s %12s %9s\n", "case", "text_len", "matches", "new ns/call", | ||
| "old ns/call", "delta"); | ||
| printf("%s\n", std::string(92, '-').c_str()); | ||
|
|
||
| for (const auto& c : cases) { | ||
| std::string text = MakeText(c.text_len, c.stride, c.from[0], 'x'); | ||
| const char* tp = text.data(); | ||
| auto tlen = static_cast<int32_t>(text.size()); | ||
| const char* fp = c.from.data(); | ||
| auto flen = static_cast<int32_t>(c.from.size()); | ||
| const char* op = c.to.data(); | ||
| auto olen = static_cast<int32_t>(c.to.size()); | ||
|
|
||
| // Count matches and compute the exact output size to give the "old" arm a | ||
| // buffer large enough to complete (precomputed -- not part of the timing). | ||
| int64_t matches = 0; | ||
| if (flen > 0 && flen <= tlen) { | ||
| for (int32_t i = 0; i <= tlen - flen;) { | ||
| if (memcmp(tp + i, fp, flen) == 0) { | ||
| ++matches; | ||
| i += flen; | ||
| } else { | ||
| ++i; | ||
| } | ||
| } | ||
| } | ||
| auto out_capacity = static_cast<int32_t>(tlen + matches * (olen - flen)); | ||
|
|
||
| auto run_new = [&]() { | ||
| int32_t out_len = 0; | ||
| replace_utf8_utf8_utf8(ctx_ptr, tp, tlen, fp, flen, op, olen, &out_len); | ||
| ctx.Reset(); | ||
| }; | ||
| auto run_old = [&]() { | ||
| int32_t out_len = 0; | ||
| replace_with_max_len_utf8_utf8_utf8(ctx_ptr, tp, tlen, fp, flen, op, olen, | ||
| out_capacity, &out_len); | ||
| ctx.Reset(); | ||
| }; | ||
|
|
||
| // Warm up (and verify both produce the same length / no error). | ||
| run_new(); | ||
| ASSERT_FALSE(ctx.has_error()); | ||
|
Contributor
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. you are resetting context in lambdas so this assert is asserting what exactly? |
||
| run_old(); | ||
| ASSERT_FALSE(ctx.has_error()); | ||
|
|
||
| double new_ns = TimeNsPerCall(run_new, c.iters); | ||
| double old_ns = TimeNsPerCall(run_old, c.iters); | ||
| double delta = old_ns > 0 ? (new_ns - old_ns) / old_ns * 100.0 : 0.0; | ||
|
|
||
| printf("%-28s %10" PRId64 " %9" PRId64 " %12.1f %12.1f %+8.1f%%\n", c.name.c_str(), | ||
| c.text_len, matches, new_ns, old_ns, delta); | ||
| } | ||
| printf("\n"); | ||
| } | ||
|
|
||
| } // namespace gandiva | ||
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
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.
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.
Did you mean to add this? It would seems such an addition would make benchmark a regular test, NOT on demand.