Skip to content
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

[bugfix](memtable) arena is freed early and will cause use after free #46997

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions be/src/olap/memtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,35 +144,41 @@ void MemTable::_init_agg_functions(const vectorized::Block* block) {

MemTable::~MemTable() {
SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_query_thread_context.query_mem_tracker);
{
SCOPED_CONSUME_MEM_TRACKER(_mem_tracker);
g_memtable_cnt << -1;
if (_keys_type != KeysType::DUP_KEYS) {
for (auto it = _row_in_blocks.begin(); it != _row_in_blocks.end(); it++) {
if (!(*it)->has_init_agg()) {
continue;
}
// We should release agg_places here, because they are not released when a
// load is canceled.
for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
auto function = _agg_functions[i];
DCHECK(function != nullptr);
function->destroy((*it)->agg_places(i));
}
}
}
std::for_each(_row_in_blocks.begin(), _row_in_blocks.end(),
std::default_delete<RowInBlock>());
// Arena has to be destroyed after agg state, because some agg state's memory may be
// allocated in arena.
_arena.reset();
_vec_row_comparator.reset();
_row_in_blocks.clear();
_agg_functions.clear();
_input_mutable_block.clear();
_output_mutable_block.clear();
}
if (_is_flush_success) {
// If the memtable is flush success, then its memtracker's consumption should be 0
if (_mem_tracker->consumption() != 0 && config::crash_in_memory_tracker_inaccurate) {
LOG(FATAL) << "memtable flush success but cosumption is not 0, it is "
<< _mem_tracker->consumption();
}
}
g_memtable_cnt << -1;
if (_keys_type != KeysType::DUP_KEYS) {
for (auto it = _row_in_blocks.begin(); it != _row_in_blocks.end(); it++) {
if (!(*it)->has_init_agg()) {
continue;
}
// We should release agg_places here, because they are not released when a
// load is canceled.
for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) {
auto function = _agg_functions[i];
DCHECK(function != nullptr);
function->destroy((*it)->agg_places(i));
}
}
}
std::for_each(_row_in_blocks.begin(), _row_in_blocks.end(), std::default_delete<RowInBlock>());
_arena.reset();
_vec_row_comparator.reset();
_row_in_blocks.clear();
_agg_functions.clear();
_input_mutable_block.clear();
_output_mutable_block.clear();
}

int RowInBlockComparator::operator()(const RowInBlock* left, const RowInBlock* right) const {
Expand Down Expand Up @@ -629,8 +635,6 @@ Status MemTable::_to_block(std::unique_ptr<vectorized::Block>* res) {
RETURN_IF_ERROR(_sort_by_cluster_keys());
}
_input_mutable_block.clear();
// After to block, all data in arena is saved in the block
_arena.reset();
*res = vectorized::Block::create_unique(_output_mutable_block.to_block());
return Status::OK();
}
Expand Down
Loading