[MDEV-23385] Server crashes in Item_in_subselect::inject_in_to_exists_cond upon 2nd execution of SP Created: 2020-08-03  Updated: 2023-04-14  Resolved: 2023-04-14

Status: Closed
Project: MariaDB Server
Component/s: Optimizer, Stored routines
Affects Version/s: 10.1, 10.2, 10.3
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Elena Stepanova Assignee: Dmitry Shulga
Resolution: Cannot Reproduce Votes: 1
Labels: None


 Description   

CREATE TABLE t1 (id INT PRIMARY KEY, a INT);
INSERT INTO t1 VALUES (1,10),(2,11);
 
CREATE TABLE t2 (b INT NOT NULL, c INT);
INSERT INTO t2 VALUES (3,4),(5,6);
 
CREATE PROCEDURE sp() SELECT * FROM t1 WHERE t1.a NOT IN (SELECT MAX(t1.id) FROM t1 JOIN (t2 AS t2a LEFT JOIN t2 AS t2b ON (t2a.c = t2b.b)) WHERE t2b.b != t2b.b);
 
CALL sp;
CALL sp;
 
# Cleanup
DROP PROCEDURE sp;
DROP TABLE t1, t2;

10.2 6e09e7c1

#3  <signal handler called>
#4  0x000055f0e49a6c36 in Item_in_subselect::inject_in_to_exists_cond (this=0x7f800403a830, join_arg=0x7f8004047be8) at /data/src/10.2/sql/item_subselect.cc:2650
#5  0x000055f0e4813d4a in JOIN::choose_tableless_subquery_plan (this=0x7f8004047be8) at /data/src/10.2/sql/opt_subselect.cc:6070
#6  0x000055f0e46a82dc in JOIN::optimize_inner (this=0x7f8004047be8) at /data/src/10.2/sql/sql_select.cc:2263
#7  0x000055f0e46a4324 in JOIN::optimize (this=0x7f8004047be8) at /data/src/10.2/sql/sql_select.cc:1114
#8  0x000055f0e46547dd in st_select_lex::optimize_unflattened_subqueries (this=0x7f80040371e8, const_only=false) at /data/src/10.2/sql/sql_lex.cc:3871
#9  0x000055f0e4812018 in JOIN::optimize_unflattened_subqueries (this=0x7f8004047680) at /data/src/10.2/sql/opt_subselect.cc:5331
#10 0x000055f0e46a7ad2 in JOIN::optimize_inner (this=0x7f8004047680) at /data/src/10.2/sql/sql_select.cc:2076
#11 0x000055f0e46a4324 in JOIN::optimize (this=0x7f8004047680) at /data/src/10.2/sql/sql_select.cc:1114
#12 0x000055f0e46ad852 in mysql_select (thd=0x7f8004000af0, tables=0x7f8004035488, wild_num=0, fields=..., conds=0x7f800403b820, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147749632, result=0x7f8004047660, unit=0x7f8004036aa8, select_lex=0x7f80040371e8) at /data/src/10.2/sql/sql_select.cc:3819
#13 0x000055f0e46a1aa0 in handle_select (thd=0x7f8004000af0, lex=0x7f80040369e8, result=0x7f8004047660, setup_tables_done_option=0) at /data/src/10.2/sql/sql_select.cc:361
#14 0x000055f0e466d4ec in execute_sqlcom_select (thd=0x7f8004000af0, all_tables=0x7f8004035488) at /data/src/10.2/sql/sql_parse.cc:6218
#15 0x000055f0e4663d6d in mysql_execute_command (thd=0x7f8004000af0) at /data/src/10.2/sql/sql_parse.cc:3524
#16 0x000055f0e45b88d2 in sp_instr_stmt::exec_core (this=0x7f800403b8e8, thd=0x7f8004000af0, nextp=0x7f8015b585ec) at /data/src/10.2/sql/sp_head.cc:3332
#17 0x000055f0e45b7f4b in sp_lex_keeper::reset_lex_and_exec_core (this=0x7f800403b928, thd=0x7f8004000af0, nextp=0x7f8015b585ec, open_tables=false, instr=0x7f800403b8e8) at /data/src/10.2/sql/sp_head.cc:3095
#18 0x000055f0e45b8583 in sp_instr_stmt::execute (this=0x7f800403b8e8, thd=0x7f8004000af0, nextp=0x7f8015b585ec) at /data/src/10.2/sql/sp_head.cc:3248
#19 0x000055f0e45b35dd in sp_head::execute (this=0x7f80040344c8, thd=0x7f8004000af0, merge_da_on_success=true) at /data/src/10.2/sql/sp_head.cc:1326
#20 0x000055f0e45b57ca in sp_head::execute_procedure (this=0x7f80040344c8, thd=0x7f8004000af0, args=0x7f8004005430) at /data/src/10.2/sql/sp_head.cc:2202
#21 0x000055f0e4662310 in do_execute_sp (thd=0x7f8004000af0, sp=0x7f80040344c8) at /data/src/10.2/sql/sql_parse.cc:2946
#22 0x000055f0e466ab93 in mysql_execute_command (thd=0x7f8004000af0) at /data/src/10.2/sql/sql_parse.cc:5564
#23 0x000055f0e4671223 in mysql_parse (thd=0x7f8004000af0, rawbuf=0x7f8004012458 "CALL sp", length=7, parser_state=0x7f8015b59610, is_com_multi=false, is_next_command=false) at /data/src/10.2/sql/sql_parse.cc:7733
#24 0x000055f0e465f54f in dispatch_command (command=COM_QUERY, thd=0x7f8004000af0, packet=0x7f800408d321 "CALL sp", packet_length=7, is_com_multi=false, is_next_command=false) at /data/src/10.2/sql/sql_parse.cc:1824
#25 0x000055f0e465dfca in do_command (thd=0x7f8004000af0) at /data/src/10.2/sql/sql_parse.cc:1377
#26 0x000055f0e47b4099 in do_handle_one_connection (connect=0x55f0e73ddcf0) at /data/src/10.2/sql/sql_connect.cc:1336
#27 0x000055f0e47b3e04 in handle_one_connection (arg=0x55f0e73ddcf0) at /data/src/10.2/sql/sql_connect.cc:1241
#28 0x000055f0e4fcaaa8 in pfs_spawn_thread (arg=0x55f0e73e8ae0) at /data/src/10.2/storage/perfschema/pfs.cc:1869
#29 0x00007f801d7ae4a4 in start_thread (arg=0x7f8015b5a700) at pthread_create.c:456
#30 0x00007f801b8e2d0f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:97

10.2 6e09e7c1 ASAN non-debug

==21731==ERROR: AddressSanitizer: heap-use-after-free on address 0x6250000f0f68 at pc 0x55cfad610aa7 bp 0x7f3f653b92e0 sp 0x7f3f653b92d8
READ of size 8 at 0x6250000f0f68 thread T5
    #0 0x55cfad610aa6 in base_list_iterator::next_fast() /data/src/10.2/sql/sql_list.h:482
    #1 0x55cfad610aa6 in List_iterator_fast<Item_sum>::operator++(int) /data/src/10.2/sql/sql_list.h:604
    #2 0x55cfad610aa6 in Item_in_subselect::inject_in_to_exists_cond(JOIN*) /data/src/10.2/sql/item_subselect.cc:2648
    #3 0x55cfad2bea12 in JOIN::choose_tableless_subquery_plan() /data/src/10.2/sql/opt_subselect.cc:6070
    #4 0x55cfad0a31c9 in JOIN::optimize_inner() /data/src/10.2/sql/sql_select.cc:2263
    #5 0x55cfad0acb22 in JOIN::optimize() /data/src/10.2/sql/sql_select.cc:1114
    #6 0x55cfacfa1da7 in st_select_lex::optimize_unflattened_subqueries(bool) /data/src/10.2/sql/sql_lex.cc:3871
    #7 0x55cfad0a7d1b in JOIN::optimize_inner() /data/src/10.2/sql/sql_select.cc:2076
    #8 0x55cfad0acb22 in JOIN::optimize() /data/src/10.2/sql/sql_select.cc:1114
    #9 0x55cfad0aef46 in mysql_select(THD*, TABLE_LIST*, unsigned int, List<Item>&, Item*, unsigned int, st_order*, st_order*, Item*, st_order*, unsigned long long, select_result*, st_select_lex_unit*, st_select_lex*) /data/src/10.2/sql/sql_select.cc:3819
    #10 0x55cfad0af742 in handle_select(THD*, LEX*, select_result*, unsigned long) /data/src/10.2/sql/sql_select.cc:361
    #11 0x55cfacda5c80 in execute_sqlcom_select /data/src/10.2/sql/sql_parse.cc:6218
    #12 0x55cfacfca957 in mysql_execute_command(THD*) /data/src/10.2/sql/sql_parse.cc:3524
    #13 0x55cface77703 in sp_instr_stmt::exec_core(THD*, unsigned int*) /data/src/10.2/sql/sp_head.cc:3332
    #14 0x55cface899ec in sp_lex_keeper::reset_lex_and_exec_core(THD*, unsigned int*, bool, sp_instr*) /data/src/10.2/sql/sp_head.cc:3095
    #15 0x55cface8aa75 in sp_instr_stmt::execute(THD*, unsigned int*) /data/src/10.2/sql/sp_head.cc:3248
    #16 0x55cface80408 in sp_head::execute(THD*, bool) /data/src/10.2/sql/sp_head.cc:1326
    #17 0x55cface83d08 in sp_head::execute_procedure(THD*, List<Item>*) /data/src/10.2/sql/sp_head.cc:2202
    #18 0x55cfacfa5dbd in do_execute_sp /data/src/10.2/sql/sql_parse.cc:2946
    #19 0x55cfacfcbe7c in mysql_execute_command(THD*) /data/src/10.2/sql/sql_parse.cc:5564
    #20 0x55cfacfd2a1b in mysql_parse(THD*, char*, unsigned int, Parser_state*, bool, bool) /data/src/10.2/sql/sql_parse.cc:7733
    #21 0x55cfacfd833b in dispatch_command(enum_server_command, THD*, char*, unsigned int, bool, bool) /data/src/10.2/sql/sql_parse.cc:1824
    #22 0x55cfacfdad92 in do_command(THD*) /data/src/10.2/sql/sql_parse.cc:1377
    #23 0x55cfad22000f in do_handle_one_connection(CONNECT*) /data/src/10.2/sql/sql_connect.cc:1336
    #24 0x55cfad22043a in handle_one_connection /data/src/10.2/sql/sql_connect.cc:1241
    #25 0x55cfae10d183 in pfs_spawn_thread /data/src/10.2/storage/perfschema/pfs.cc:1869
    #26 0x7f3f70fb44a3 in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x74a3)
    #27 0x7f3f6f0e8d0e in __clone (/lib/x86_64-linux-gnu/libc.so.6+0xe8d0e)
 
0x6250000f0f68 is located 3688 bytes inside of 8160-byte region [0x6250000f0100,0x6250000f20e0)
freed by thread T5 here:
    #0 0x7f3f7128ba10 in free (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc1a10)
    #1 0x55cfae19b7a3 in free_root /data/src/10.2/mysys/my_alloc.c:394
    #2 0x1f  (<unknown module>)
 
previously allocated by thread T5 here:
    #0 0x7f3f7128bd28 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc1d28)
    #1 0x55cfae1ad7cc in my_malloc /data/src/10.2/mysys/my_malloc.c:101
 
Thread T5 created by T0 here:
    #0 0x7f3f711faf59 in __interceptor_pthread_create (/usr/lib/x86_64-linux-gnu/libasan.so.3+0x30f59)
    #1 0x55cfae116e22 in spawn_thread_v1 /data/src/10.2/storage/perfschema/pfs.cc:1919
 
SUMMARY: AddressSanitizer: heap-use-after-free /data/src/10.2/sql/sql_list.h:482 in base_list_iterator::next_fast()
Shadow bytes around the buggy address:
  0x0c4a80016190: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c4a800161a0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c4a800161b0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c4a800161c0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c4a800161d0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
=>0x0c4a800161e0: fd fd fd fd fd fd fd fd fd fd fd fd fd[fd]fd fd
  0x0c4a800161f0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c4a80016200: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c4a80016210: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c4a80016220: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c4a80016230: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Heap right redzone:      fb
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack partial redzone:   f4
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==21731==ABORTING

Reproducible on 10.1-10.3 with at least MyISAM, InnoDB, Aria.
My non-debug builds don't crash, but non-debug ASAN build still returns heap-use-after-free as above, so they are potentially affected.
Couldn't reproduce on 10.4-10.5 with the provided test case.



 Comments   
Comment by Alice Sherepa [ 2021-03-10 ]

non-debug 10.2-10.3 crashes if prepared statement were used instead of sp

210310 11:12:51 [ERROR] mysqld got signal 11 ;
Server version: 10.3.27-MariaDB
 
/lib/x86_64-linux-gnu/libpthread.so.0(+0x12730)[0x7fcde69e7730]
sql/sql_list.h:442(Item_in_subselect::inject_in_to_exists_cond(JOIN*))[0x5625291dfe19]
sql/opt_subselect.cc:6075(JOIN::choose_tableless_subquery_plan())[0x562529065cc3]
sql/sql_select.cc:2695(JOIN::optimize_stage2())[0x562528f9fbff]
sql/sql_select.cc:1981(JOIN::optimize_inner())[0x562528fa3050]
sql/sql_select.cc:1503(JOIN::optimize())[0x562528fa4a12]
sql/sql_lex.cc:4102(st_select_lex::optimize_unflattened_subqueries(bool))[0x562528f2fbd0]
sql/sql_select.cc:2509(JOIN::optimize_stage2())[0x562528fa105f]
sql/sql_select.cc:1981(JOIN::optimize_inner())[0x562528fa3050]
sql/sql_select.cc:1503(JOIN::optimize())[0x562528fa4a12]
sql/sql_select.cc:4305(mysql_select(THD*, TABLE_LIST*, unsigned int, List<Item>&, Item*, unsigned int, st_order*, st_order*, Item*, st_order*, unsigned long long, select_result*, st_select_lex_unit*, st_select_lex*))[0x562528fa61d4]
sql/sql_select.cc:382(handle_select(THD*, LEX*, select_result*, unsigned long))[0x562528fa6d7c]
sql/sql_parse.cc:6319(execute_sqlcom_select(THD*, TABLE_LIST*))[0x562528e57739]
sql/sql_parse.cc:3847(mysql_execute_command(THD*))[0x562528f4cc5a]
sql/sql_prepare.cc:4997(Prepared_statement::execute(String*, bool))[0x562528f65215]
sql/sql_prepare.cc:4425(Prepared_statement::execute_loop(String*, bool, unsigned char*, unsigned char*))[0x562528f6533f]
sql/sql_class.h:1385(mysql_sql_stmt_execute(THD*))[0x562528f65773]
sql/sql_parse.cc:3863(mysql_execute_command(THD*))[0x562528f4d668]
sql/sql_parse.cc:7839(mysql_parse(THD*, char*, unsigned int, Parser_state*, bool, bool))[0x562528f53562]
sql/sql_parse.cc:1916(dispatch_command(enum_server_command, THD*, char*, unsigned int, bool, bool))[0x562528f54daf]
sql/sql_parse.cc:1401(do_command(THD*))[0x562528f568db]
sql/sql_connect.cc:1403(do_handle_one_connection(CONNECT*))[0x56252902c8c6]
sql/sql_connect.cc:1310(handle_one_connection)[0x56252902c9dd]
nptl/pthread_create.c:487(start_thread)[0x7fcde69dcfa3]
x86_64/clone.S:97(clone)[0x7fcde65b64cf]

Comment by Dmitry Shulga [ 2023-04-14 ]

10.3 is EOL. The bug report is not reproduced in 10.4. So, close it.

Generated at Thu Feb 08 09:22:00 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.