Skip to content

box: fix key corruption when pagination by tuple is used#11677

Merged
locker merged 2 commits intomasterfrom
drewdzzz/pagination_realloc_err
Aug 15, 2025
Merged

box: fix key corruption when pagination by tuple is used#11677
locker merged 2 commits intomasterfrom
drewdzzz/pagination_realloc_err

Conversation

@drewdzzz
Copy link
Contributor

@drewdzzz drewdzzz commented Jul 17, 2025

Function iterator_pos_set works completely incorrectly when tuple
is passed to the option after.

Firstly, before extracting position, we call ibuf:consume() for
the memory allocated before position (this method is actaully ASAN
poisoning) - that's not correct since consumed memory contains query key
that will be used later. Let's remove this call.

Secondly, new allocation on ibuf can lead to its reallocation, so
pointers to old allocations can be invalidated. Let's set the pointers
to the key after all allocations are done, just like in index:quantile().

What's about performance, perf/lua/box_select.lua shows no degradation.

Closes #11221
Closes #11648

@drewdzzz drewdzzz force-pushed the drewdzzz/pagination_realloc_err branch from 816a928 to 39860aa Compare July 17, 2025 10:39
@drewdzzz drewdzzz self-assigned this Jul 17, 2025
@drewdzzz drewdzzz force-pushed the drewdzzz/pagination_realloc_err branch 4 times, most recently from 3f063eb to 56e2601 Compare July 18, 2025 10:51
@coveralls
Copy link

coveralls commented Jul 18, 2025

Coverage Status

coverage: 87.614% (+0.03%) from 87.582%
when pulling c829142 on drewdzzz/pagination_realloc_err
into 2120de9
on master
.

@drewdzzz drewdzzz force-pushed the drewdzzz/pagination_realloc_err branch from 56e2601 to 399e66b Compare July 23, 2025 12:35
@drewdzzz drewdzzz changed the title box: fix pagination ibuf memory issues box: fix key corruption when pagination by tuple is used Jul 23, 2025
@drewdzzz drewdzzz force-pushed the drewdzzz/pagination_realloc_err branch 3 times, most recently from e4a2989 to 140a6e8 Compare July 24, 2025 10:55
@drewdzzz drewdzzz marked this pull request as ready for review July 24, 2025 10:56
@drewdzzz drewdzzz requested a review from a team as a code owner July 24, 2025 10:56
@nshy nshy self-requested a review July 31, 2025 17:21
@drewdzzz drewdzzz force-pushed the drewdzzz/pagination_realloc_err branch 2 times, most recently from 2850445 to d40fc25 Compare August 11, 2025 14:33
@drewdzzz drewdzzz requested a review from nshy August 11, 2025 14:39
@drewdzzz drewdzzz assigned nshy and unassigned drewdzzz Aug 11, 2025
@nshy nshy assigned drewdzzz and unassigned nshy Aug 11, 2025
@nshy
Copy link
Contributor

nshy commented Aug 11, 2025

I'd also try to use new tuple_encode() result in base_index_mt.quantile. We can move from set of begin, size pairs to begin, end pairs.

@drewdzzz drewdzzz force-pushed the drewdzzz/pagination_realloc_err branch 2 times, most recently from 5a09174 to 636a4b9 Compare August 12, 2025 12:29
@drewdzzz
Copy link
Contributor Author

drewdzzz commented Aug 12, 2025

I'd also try to use new tuple_encode() result in base_index_mt.quantile. We can move from set of begin, size pairs to begin, end pairs.

Good idea! Done, right in the first commit. PTAL.

A Lua helper `tuple_encode` encodes tuple on ibuf and returns its `rpos`
and `wpos` as pointers to the tuple beginning and end. If the ibuf is not
empty, the returned pointer to the tuple beginning will actually point
to the previously encoded tuple. Now it doesn't break anything, but
anyway such behavior is inconvinient, so let's fix this function and
return pointer to the actual beginning of the encoded tuple.

Along the way, rewrite `index:quantile()` in a simpler manner using the
new function behavior.

Part of #11221

NO_TEST=refactoring
NO_CHANGELOG=refactoring
NO_DOC=refactoring
Function `iterator_pos_set` works completely incorrectly when tuple
is passed to the option `after`.

Firstly, before extracting position, we call `ibuf:consume()` for
the memory allocated before position (this method is actually ASAN
poisoning) - that's not correct since consumed memory contains query key
that will be used later. Let's remove this call.

Secondly, new allocation on ibuf can lead to its reallocation, so
pointers to old allocations can be invalidated. Let's set the pointers
to the key after all allocations are done, just like in `index:quantile()`.

What's about performance, `perf/lua/box_select.lua` shows no degradation.

Closes #11221
Closes #11648

NO_DOC=bugfix
@drewdzzz drewdzzz force-pushed the drewdzzz/pagination_realloc_err branch from 636a4b9 to c829142 Compare August 12, 2025 12:30
@drewdzzz drewdzzz requested a review from nshy August 12, 2025 12:44
@drewdzzz drewdzzz assigned nshy and unassigned drewdzzz Aug 12, 2025
@nshy nshy assigned drewdzzz and unassigned nshy Aug 12, 2025
@drewdzzz drewdzzz requested a review from locker August 12, 2025 14:38
@drewdzzz drewdzzz assigned locker and unassigned drewdzzz Aug 12, 2025
@locker locker added the full-ci Enables all tests for a pull request label Aug 13, 2025
@locker locker merged commit 7426f59 into master Aug 15, 2025
58 checks passed
@locker locker deleted the drewdzzz/pagination_realloc_err branch August 15, 2025 14:34
@locker
Copy link
Member

locker commented Aug 15, 2025

Cherry-picked to 3.3 and 3.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Enables all tests for a pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect pagination integration with ASAN Unexpected error when using select or pairs with after

5 participants