[MDEV-22447] SIGSEGV in Item::val_uint on EXPLAIN, UBSAN: member call on null pointer of type 'struct Item' in handle_select Created: 2020-05-04  Updated: 2023-11-28

Status: Stalled
Project: MariaDB Server
Component/s: None
Affects Version/s: 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 10.10, 10.11, 11.0
Fix Version/s: 10.4, 10.5, 10.6, 10.11

Type: Bug Priority: Major
Reporter: Roel Van de Paar Assignee: Oleksandr Byelkin
Resolution: Unresolved Votes: 0
Labels: UBSAN, affects-tests, not-10.1


 Description   

USE test;
SET GLOBAL GENERAL_LOG=ON;
SET GLOBAL log_output="FILE,TABLE";
CREATE TABLE t(a DATE);
EXPLAIN SELECT * FROM t LIMIT ROWS EXAMINED 0;
SELECT 1;

Leads to:

10.5.3 f544a712c8a2ef3f3ecba80cb2782b1839fb36ab

Core was generated by `/test/MD010520-mariadb-10.5.3-linux-x86_64-opt/bin/mysqld --no-defaults --core-'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  __pthread_kill (threadid=<optimized out>, signo=signo@entry=11)
    at ../sysdeps/unix/sysv/linux/pthread_kill.c:57
[Current thread is 1 (Thread 0x7f8d71970700 (LWP 1539686))]
(gdb) bt
#0  __pthread_kill (threadid=<optimized out>, signo=signo@entry=11) at ../sysdeps/unix/sysv/linux/pthread_kill.c:57
#1  0x000055c976d84ac7 in my_write_core (sig=sig@entry=11) at /test/10.5_opt/mysys/stacktrace.c:518
#2  0x000055c9767467ca in handle_fatal_signal (sig=11) at /test/10.5_opt/sql/signal_handler.cc:329
#3  <signal handler called>
#4  Item::val_uint (this=0x0) at /test/10.5_opt/sql/item.h:1282
#5  handle_select (thd=thd@entry=0x7f8d3d812018, lex=lex@entry=0x7f8d3d815da0, result=result@entry=0x7f8d3d847eb8, setup_tables_done_option=setup_tables_done_option@entry=0) at /test/10.5_opt/sql/sql_select.cc:442
#6  0x000055c976555171 in execute_sqlcom_select (thd=thd@entry=0x7f8d3d812018, all_tables=0x0) at /test/10.5_opt/sql/sql_parse.cc:6172
#7  0x000055c976551092 in mysql_execute_command (thd=thd@entry=0x7f8d3d812018) at /test/10.5_opt/sql/sql_parse.cc:3901
#8  0x000055c97655822c in mysql_parse (thd=0x7f8d3d812018, rawbuf=<optimized out>, length=8, parser_state=0x7f8d7196f4d0, is_com_multi=<optimized out>, is_next_command=<optimized out>) at /test/10.5_opt/sql/sql_parse.cc:7957
#9  0x000055c97654d855 in dispatch_command (command=command@entry=COM_QUERY, thd=thd@entry=0x7f8d3d812018, packet=packet@entry=0x7f8d3d83a019 "", packet_length=packet_length@entry=8, is_com_multi=is_com_multi@entry=false, is_next_command=is_next_command@entry=false) at /test/10.5_opt/sql/sql_parse.cc:1839
#10 0x000055c97654bae6 in do_command (thd=0x7f8d3d812018) at /test/10.5_opt/sql/sql_parse.cc:1358
#11 0x000055c97664029e in do_handle_one_connection (connect=<optimized out>, connect@entry=0x7f8d6f4329b8, put_in_cache=put_in_cache@entry=true) at /test/10.5_opt/sql/sql_connect.cc:1422
#12 0x000055c976640444 in handle_one_connection (arg=arg@entry=0x7f8d6f4329b8) at /test/10.5_opt/sql/sql_connect.cc:1319
#13 0x000055c9769ac53a in pfs_spawn_thread (arg=0x7f8d6f44b018) at /test/10.5_opt/storage/perfschema/pfs.cc:2201
#14 0x00007f8d70d976db in start_thread (arg=0x7f8d71970700) at pthread_create.c:463
#15 0x00007f8d7019588f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Bug confirmed present in:
MariaDB: 10.2.32 (dbg), 10.2.32 (opt), 10.3.23 (dbg), 10.3.23 (opt), 10.4.13 (dbg), 10.4.13 (opt), 10.5.2 (dbg), 10.5.2 (opt), 10.5.3 (dbg), 10.5.3 (opt)

Bug confirmed not present in:
MariaDB: 10.1.45 (dbg), 10.1.45 (opt)
MySQL: 5.5.62 (dbg), 5.5.62 (opt), 5.6.47 (dbg), 5.6.47 (opt), 5.7.29 (dbg), 5.7.29 (opt), 8.0.19 (dbg), 8.0.19 (opt)



 Comments   
Comment by Alexander Barkov [ 2020-05-20 ]

sanja, can you please take this over? You're familiar with this area.

It seems this query:

EXPLAIN SELECT * FROM t1 LIMIT ROWS EXAMINED 0;

does not reset thd->lex->limit_rows_examined_cnt back to ULONGLONG_MAX after its execution, so SELECT 1 starts with thd->lex->limit_rows_examined_cnt equal to 0.

Comment by Oleksandr Byelkin [ 2021-09-16 ]

commit 2f812e800c0695535c0c63af30f65d7cdb7dc347 (HEAD -> bb-10.2-MDEV-22447, origin/bb-10.2-MDEV-22447)
Author: Oleksandr Byelkin <sanja@mariadb.com>
Date:   Thu Sep 16 14:12:48 2021 +0200
 
    MDEV-22447: SIGSEGV in Item::val_uint on EXPLAIN
    
    take into account possible NULL

Comment by Roel Van de Paar [ 2022-09-14 ]

This testcase:

SET GLOBAL log_output='FILE,TABLE',GLOBAL general_log=ON;
CREATE TABLE t (c INT);
EXPLAIN SELECT * FROM t LIMIT ROWS EXAMINED 0;
SELECT SLEEP (1);

Results in the described issue, as well as this new ASAN report:

10.11.0 fe1f8f2c6b6f3b8e3383168225f9ae7853028947 (Optimized, UBASAN)

2022-09-14 14:45:33 0 [Note] /test/UBASAN_MD010922-mariadb-10.11.0-linux-x86_64-opt/bin/mysqld: ready for connections.
Version: '10.11.0-MariaDB'  socket: '/test/UBASAN_MD010922-mariadb-10.11.0-linux-x86_64-opt/socket.sock'  port: 12487  MariaDB Server
/test/10.11_opt_san/sql/sql_select.cc:612:64: runtime error: member call on null pointer of type 'struct Item'
    #0 0x55ee384690b2 in handle_select(THD*, LEX*, select_result*, unsigned long) /test/10.11_opt_san/sql/sql_select.cc:612
    #1 0x55ee38075c2f in execute_sqlcom_select /test/10.11_opt_san/sql/sql_parse.cc:6261
    #2 0x55ee380c653b in mysql_execute_command(THD*, bool) /test/10.11_opt_san/sql/sql_parse.cc:3945
    #3 0x55ee38046500 in mysql_parse(THD*, char*, unsigned int, Parser_state*) /test/10.11_opt_san/sql/sql_parse.cc:8035
    #4 0x55ee3809b0ff in dispatch_command(enum_server_command, THD*, char*, unsigned int, bool) /test/10.11_opt_san/sql/sql_parse.cc:1894
    #5 0x55ee380a63fd in do_command(THD*, bool) /test/10.11_opt_san/sql/sql_parse.cc:1407
    #6 0x55ee3898f4cd in do_handle_one_connection(CONNECT*, bool) /test/10.11_opt_san/sql/sql_connect.cc:1418
    #7 0x55ee38991b3c in handle_one_connection /test/10.11_opt_san/sql/sql_connect.cc:1312
    #8 0x1499a5f93608 in start_thread /build/glibc-SzIz7B/glibc-2.31/nptl/pthread_create.c:477
    #9 0x1499a5208132 in __clone (/lib/x86_64-linux-gnu/libc.so.6+0x11f132)
 
220914 14:45:35 [ERROR] mysqld got signal 11 ;

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