Skip to content

Commit c7f8cfc

Browse files
MDEV-27700 ASAN: Heap_use_after_free in btr_search_drop_page_hash_index()
Reason: ======= Race condition between btr_search_drop_hash_index() and btr_search_lazy_free(). One thread does resizing of buffer pool and clears the ahi on all pages in the buffer pool, frees the index and table while removing the last reference. At the same time, other thread access index->heap in btr_search_drop_hash_index(). Solution: ========= Acquire the respective ahi latch before checking index->freed() btr_search_drop_page_hash_index(): Added new parameter to indicate that drop ahi entries only if the index is marked as freed btr_search_check_marked_free_index(): Acquire all ahi latches and return true if the index was freed
1 parent fd0cd48 commit c7f8cfc

File tree

5 files changed

+63
-22
lines changed

5 files changed

+63
-22
lines changed

storage/innobase/btr/btr0btr.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,9 @@ void btr_page_free(dict_index_t* index, buf_block_t* block, mtr_t* mtr,
721721
bool blob)
722722
{
723723
ut_ad(mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
724-
#ifdef BTR_CUR_HASH_ADAPT
725-
if (block->index && !block->index->freed()) {
724+
#if defined BTR_CUR_HASH_ADAPT && defined UNIV_DEBUG
725+
if (block->index
726+
&& !btr_search_check_marked_free_index(block)) {
726727
ut_ad(!blob);
727728
ut_ad(page_is_leaf(block->frame));
728729
}

storage/innobase/btr/btr0sea.cc

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,8 +1102,11 @@ btr_search_guess_on_hash(
11021102
index page for which we know that
11031103
block->buf_fix_count == 0 or it is an index page which
11041104
has already been removed from the buf_pool->page_hash
1105-
i.e.: it is in state BUF_BLOCK_REMOVE_HASH */
1106-
void btr_search_drop_page_hash_index(buf_block_t* block)
1105+
i.e.: it is in state BUF_BLOCK_REMOVE_HASH
1106+
@param[in] garbage_collect drop ahi only if the index is marked
1107+
as freed */
1108+
void btr_search_drop_page_hash_index(buf_block_t* block,
1109+
bool garbage_collect)
11071110
{
11081111
ulint n_fields;
11091112
ulint n_bytes;
@@ -1149,13 +1152,21 @@ void btr_search_drop_page_hash_index(buf_block_t* block)
11491152
% btr_ahi_parts;
11501153
latch = btr_search_latches[ahi_slot];
11511154

1155+
rw_lock_s_lock(latch);
1156+
11521157
dict_index_t* index = block->index;
11531158

11541159
bool is_freed = index && index->freed();
11551160
if (is_freed) {
1161+
rw_lock_s_unlock(latch);
11561162
rw_lock_x_lock(latch);
1157-
} else {
1158-
rw_lock_s_lock(latch);
1163+
if (index != block->index) {
1164+
rw_lock_x_unlock(latch);
1165+
goto retry;
1166+
}
1167+
} else if (garbage_collect) {
1168+
rw_lock_s_unlock(latch);
1169+
return;
11591170
}
11601171

11611172
assert_block_ahi_valid(block);
@@ -2220,5 +2231,22 @@ btr_search_validate()
22202231
return(true);
22212232
}
22222233

2234+
#ifdef UNIV_DEBUG
2235+
bool btr_search_check_marked_free_index(const buf_block_t *block)
2236+
{
2237+
const index_id_t index_id= btr_page_get_index_id(block->frame);
2238+
2239+
rw_lock_t *ahi_latch= btr_get_search_latch(
2240+
index_id, block->page.id.space());
2241+
2242+
rw_lock_s_lock(ahi_latch);
2243+
2244+
bool is_freed= block->index && block->index->freed();
2245+
2246+
rw_lock_s_unlock(ahi_latch);
2247+
2248+
return is_freed;
2249+
}
2250+
#endif /* UNIV_DEBUG */
22232251
#endif /* defined UNIV_AHI_DEBUG || defined UNIV_DEBUG */
22242252
#endif /* BTR_CUR_HASH_ADAPT */

storage/innobase/buf/buf0buf.cc

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3935,18 +3935,14 @@ static void buf_defer_drop_ahi(buf_block_t *block, mtr_memo_type_t fix_type)
39353935
/* Temporarily release our S-latch. */
39363936
rw_lock_s_unlock(&block->lock);
39373937
rw_lock_x_lock(&block->lock);
3938-
if (dict_index_t *index= block->index)
3939-
if (index->freed())
3940-
btr_search_drop_page_hash_index(block);
3938+
btr_search_drop_page_hash_index(block, true);
39413939
rw_lock_x_unlock(&block->lock);
39423940
rw_lock_s_lock(&block->lock);
39433941
break;
39443942
case MTR_MEMO_PAGE_SX_FIX:
39453943
rw_lock_sx_unlock(&block->lock);
39463944
rw_lock_x_lock(&block->lock);
3947-
if (dict_index_t *index= block->index)
3948-
if (index->freed())
3949-
btr_search_drop_page_hash_index(block);
3945+
btr_search_drop_page_hash_index(block, true);
39503946
rw_lock_x_unlock(&block->lock);
39513947
rw_lock_sx_lock(&block->lock);
39523948
break;
@@ -3993,8 +3989,7 @@ static buf_block_t* buf_page_mtr_lock(buf_block_t *block,
39933989

39943990
#ifdef BTR_CUR_HASH_ADAPT
39953991
{
3996-
dict_index_t *index= block->index;
3997-
if (index && index->freed())
3992+
if (block->index)
39983993
buf_defer_drop_ahi(block, fix_type);
39993994
}
40003995
#endif /* BTR_CUR_HASH_ADAPT */
@@ -4916,7 +4911,7 @@ buf_page_get_known_nowait(
49164911

49174912
# ifdef BTR_CUR_HASH_ADAPT
49184913
ut_ad(!block->page.file_page_was_freed
4919-
|| (block->index && block->index->freed()));
4914+
|| btr_search_check_marked_free_index(block));
49204915
# else /* BTR_CUR_HASH_ADAPT */
49214916
ut_ad(!block->page.file_page_was_freed);
49224917
# endif /* BTR_CUR_HASH_ADAPT */

storage/innobase/include/btr0sea.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ btr_search_move_or_delete_hash_entries(
9999
index page for which we know that
100100
block->buf_fix_count == 0 or it is an index page which
101101
has already been removed from the buf_pool->page_hash
102-
i.e.: it is in state BUF_BLOCK_REMOVE_HASH */
103-
void btr_search_drop_page_hash_index(buf_block_t* block);
102+
i.e.: it is in state BUF_BLOCK_REMOVE_HASH
103+
@param[in] garbage_collect drop ahi only if the index is marked
104+
as freed */
105+
void btr_search_drop_page_hash_index(buf_block_t* block,
106+
bool garbage_collect= false);
104107

105108
/** Drop possible adaptive hash index entries when a page is evicted
106109
from the buffer pool or freed in a file, or the index is being dropped.
@@ -173,16 +176,25 @@ A table is selected from an array of tables using pair of index-id, space-id.
173176
@param[in] index index handler
174177
@return hash table */
175178
static inline hash_table_t* btr_get_search_table(const dict_index_t* index);
179+
180+
#ifdef UNIV_DEBUG
181+
/** @return if the index is marked as freed */
182+
bool btr_search_check_marked_free_index(const buf_block_t *block);
183+
#endif /* UNIV_DEBUG */
184+
176185
#else /* BTR_CUR_HASH_ADAPT */
177186
# define btr_search_sys_create(size)
178187
# define btr_search_sys_free()
179-
# define btr_search_drop_page_hash_index(block)
188+
# define btr_search_drop_page_hash_index(block, garbage_collect)
180189
# define btr_search_s_lock_all(index)
181190
# define btr_search_s_unlock_all(index)
182191
# define btr_search_info_update(index, cursor)
183192
# define btr_search_move_or_delete_hash_entries(new_block, block)
184193
# define btr_search_update_hash_on_insert(cursor, ahi_latch)
185194
# define btr_search_update_hash_on_delete(cursor)
195+
#ifdef UNIV_DEBUG
196+
# define btr_search_check_marked_free_index(block)
197+
#endif /* UNIV_DEBUG */
186198
#endif /* BTR_CUR_HASH_ADAPT */
187199

188200
#ifdef BTR_CUR_ADAPT

storage/innobase/include/btr0sea.inl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ static inline bool btr_search_own_any()
158158
}
159159
#endif /* UNIV_DEBUG */
160160

161+
static inline rw_lock_t* btr_get_search_latch(
162+
index_id_t index_id, ulint space_id)
163+
{
164+
ulint ifold = ut_fold_ulint_pair(ulint(index_id), space_id);
165+
166+
return(btr_search_latches[ifold % btr_ahi_parts]);
167+
}
168+
161169
/** Get the adaptive hash search index latch for a b-tree.
162170
@param[in] index b-tree index
163171
@return latch */
@@ -167,10 +175,7 @@ static inline rw_lock_t* btr_get_search_latch(const dict_index_t* index)
167175
ut_ad(!index->table->space
168176
|| index->table->space->id == index->table->space_id);
169177

170-
ulint ifold = ut_fold_ulint_pair(ulint(index->id),
171-
index->table->space_id);
172-
173-
return(btr_search_latches[ifold % btr_ahi_parts]);
178+
return btr_get_search_latch(index->id, index->table->space_id);
174179
}
175180

176181
/** Get the hash-table based on index attributes.

0 commit comments

Comments
 (0)