[MDEV-22463] Element_type &Bounds_checked_array<Item *>::operator[](size_t) [Element_type = Item *]: Assertion `n < m_size' failed. Created: 2020-05-05  Updated: 2020-07-13  Resolved: 2020-07-13

Status: Closed
Project: MariaDB Server
Component/s: Data Definition - Create Table, Optimizer - Window functions
Affects Version/s: 10.5.2, 10.2, 10.3, 10.4, 10.5
Fix Version/s: 10.2.33, 10.3.24, 10.4.14, 10.5.5

Type: Bug Priority: Major
Reporter: Yongheng Chen Assignee: Varun Gupta (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Environment:

Ubuntu 18.04


Issue Links:
Relates
relates to MDEV-15902 Assertion `n < m_size' failed, sql_ar... Closed

 Description   

OOB assertion failed in debug version of MariaDB.

POC:

CREATE TABLE v0 ( v1 INT ) ;
INSERT INTO v0 ( v1 ) VALUES ( 2 ) ;
UPDATE v0 SET v1 = 68 WHERE v1 = -128 ;
INSERT INTO v0 ( v1 ) VALUES ( -32768 ) , ( -128 ) ;
CREATE VIEW v3 ( v4 , v5 ) AS SELECT * FROM v0 , v0 WINDOW v2 AS ( PARTITION BY 24 , -1 , 18 , 0 ORDER BY v1 DESC ) ;

Stack dump:

mysqld: /home/mysql/mariadb/sql/sql_array.h:65: Element_type &Bounds_checked_array<Item *>::operator[](size_t) [Element_type = Item *]: Assertion `n < m_size' failed.
200505  5:03:49 [ERROR] mysqld got signal 6 ;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.
 
To report this bug, see https://mariadb.com/kb/en/reporting-bugs
 
We will try our best to scrape up some info that will hopefully help
diagnose the problem, but since we have already crashed,
something is definitely wrong and this may fail.
 
Server version: 10.5.3-MariaDB-debug
key_buffer_size=134217728
read_buffer_size=131072
max_used_connections=2
max_threads=153
thread_count=3
It is possible that mysqld could use up to
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 467925 K  bytes of memory
Hope that's ok; if not, decrease some variables in the equation.
 
Thread pointer: 0x7f67ac000d78
Attempting backtrace. You can use the following information to find out
where mysqld died. If you see no messages after this, something went
terribly wrong...
stack_bottom = 0x7f67ec95cdc0 thread_stack 0x49000
fil/fil0fil.cc:3410(fil_ibd_discover(unsigned long, Datafile&))[0x32d4681]
sql/multi_range_read.cc:764(Mrr_ordered_index_reader::refill_buffer(bool))[0x13c0898]
??:0(__restore_rt)[0x7f6806fab890]
linux/raise.c:51(__GI_raise)[0x7f6804c5de97]
stdlib/abort.c:81(__GI_abort)[0x7f6804c5f801]
assert/assert.c:89(__assert_fail_base)[0x7f6804c4f39a]
??:0(__assert_fail)[0x7f6804c4f412]
sql/slave.cc:4601(check_io_slave_killed(Master_info*, char const*))[0xaf8057]
sql/sql_array.h:272(Dynamic_array<GRANT_NAME*>::~Dynamic_array())[0xbeac1b]
sql/sql_table.cc:7799(mysql_inplace_alter_table(THD*, TABLE_LIST*, TABLE*, TABLE*, Alter_inplace_info*, enum_alter_inplace_result, MDL_request*, Alter_table_ctx*))[0x112093c]
sql/slave.cc:3914(sql_delay_event(Log_event*, THD*, rpl_group_info*))[0xaf0938]
sql/sql_lex.cc:2821(Lex_input_stream::scan_ident_delimited(THD*, Lex_ident_cli_st*, unsigned char))[0xd6ee36]
sql/sql_lex.cc:910(Lex_input_stream::body_utf8_append(char const*, char const*))[0xd5cc9d]
sql/sql_lex.cc:7823(LEX::create_item_ident_nospvar(THD*, Lex_ident_sys_st const*, Lex_ident_sys_st const*))[0xda15d4]
7.5.0/iostream:74(__cxx_global_var_init.13)[0xa1e9eb]
sql/sys_vars.ic:627(Sys_var_charptr_fscs::Sys_var_charptr(char const*, char const, int, long, unsigned long, CMD_LINE, char const, PolyLock*, sys_var::binlog_status_enum, bool (*)(PolyLock**, THD*, set_var*), bool (*)(sys_var::binlog_status_enum, THD, enum_var_type), char const))[0xa07b70]
sql/set_var.h:258(_GLOBAL__sub_I_sys_vars.cc)[0x9fd70e]
sql/sys_vars.cc:5730(__cxx_global_var_init.1236)[0xa099cb]
sql/item.h:4563(Item_empty_string::Item_empty_string(THD*, char const*, unsigned int, charset_info_st const*))[0xedb6d1]
sql/item.h:746(show_binlog_info_get_fields(THD*, List<Item>*))[0xedaec1]
gcalc_slicescan.cc:0(__afl_fork_wait_loop)[0x1e8dfc6]
nptl/pthread_create.c:463(start_thread)[0x7f6806fa06db]
x86_64/clone.S:97(clone)[0x7f6804d4088f]
 
Trying to get some variables.
Some pointers may be invalid and cause the dump to abort.
Query (0x7f67ac0150b3): CREATE VIEW v3 ( v4 , v5 ) AS SELECT * FROM v0 , v0 WINDOW v2 AS ( PARTITION BY 24 , -1 , 18 , 0 ORDER BY v1 DESC )
Connection ID (thread ID): 12575
Status: NOT_KILLED
 
Optimizer switch: index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on,not_null_range_scan=off
 
The manual page at http://dev.mysql.com/doc/mysql/en/crashing.html contains
information that should help you find out what is causing the crash.
Writing a core file...
Working directory at /usr/local/mysql/data
Resource Limits:
Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            8388608              unlimited            bytes
Max core file size        unlimited            unlimited            bytes
Max resident set          unlimited            unlimited            bytes
Max processes             unlimited            unlimited            processes
Max open files            1048576              1048576              files
Max locked memory         16777216             16777216             bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       1030951              1030951              signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us
Core pattern: co...
---



 Comments   
Comment by Alice Sherepa [ 2020-05-05 ]

Thanks a lot!

Note! Named window frames are not documented in KB (MDEV-17191)
Test case (based on a reported one, but fails on 10.2-10.5):

create table t1 (a int);
insert into t1 (a) values (2), (3), (4);
 
select * from t1 window w2 as (partition by -1,1,0);

10.2 28325b08633372cc3

#3  <signal handler called>
#4  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#5  0x00007f88a5e4d535 in __GI_abort () at abort.c:79
#6  0x00007f88a5e4d40f in __assert_fail_base (fmt=0x7f88a5fafee0 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=0x55bf6ec9293c "n < m_size", file=0x55bf6ec9291f "/10.2/sql/sql_array.h", line=64, function=<optimized out>) at assert.c:92
#7  0x00007f88a5e5b102 in __GI___assert_fail (assertion=0x55bf6ec9293c "n < m_size", file=0x55bf6ec9291f "/10.2/sql/sql_array.h", line=64, function=0x55bf6ec929a0 <Bounds_checked_array<Item*>::operator[](unsigned long)::__PRETTY_FUNCTION__> "Element_type& Bounds_checked_array<Element_type>::operator[](size_t) [with Element_type = Item*; size_t = long unsigned int]") at assert.c:101
#8  0x000055bf6e14ddab in Bounds_checked_array<Item*>::operator[] (this=0x7f88a008af00, n=3) at /10.2/sql/sql_array.h:64
#9  0x000055bf6e266dbb in find_order_in_list (thd=0x7f8884000d50, ref_pointer_array=..., tables=0x7f88840128d0, order=0x7f8884013190, fields=..., all_fields=..., is_group_field=true, add_to_all_fields=true, from_window_spec=true) at /10.2/sql/sql_select.cc:22678
#10 0x000055bf6e267091 in setup_group (thd=0x7f8884000d50, ref_pointer_array=..., tables=0x7f88840128d0, fields=..., all_fields=..., order=0x7f8884012fb0, hidden_group_fields=0x7f88a008b0b7, from_window_spec=true) at /10.2/sql/sql_select.cc:22774
#11 0x000055bf6e3b60e2 in setup_windows (thd=0x7f8884000d50, ref_pointer_array=..., tables=0x7f88840128d0, fields=..., all_fields=..., win_specs=..., win_funcs=...) at /10.2/sql/sql_window.cc:208
#12 0x000055bf6e22c23f in setup_without_group (thd=0x7f8884000d50, ref_pointer_array=..., tables=0x7f88840128d0, leaves=..., fields=..., all_fields=..., conds=0x7f8884013758, order=0x0, group=0x0, win_specs=..., win_funcs=..., hidden_group_fields=0x7f8884013637, reserved=0x7f888400534c) at /10.2/sql/sql_select.cc:661
#13 0x000055bf6e22cafa in JOIN::prepare (this=0x7f8884013350, tables_init=0x7f88840128d0, wild_num=1, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7f8884005088, unit_arg=0x7f8884004948) at /10.2/sql/sql_select.cc:816
#14 0x000055bf6e23735c in mysql_select (thd=0x7f8884000d50, tables=0x7f88840128d0, wild_num=1, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7f8884013330, unit=0x7f8884004948, select_lex=0x7f8884005088) at /10.2/sql/sql_select.cc:3806
#15 0x000055bf6e22b654 in handle_select (thd=0x7f8884000d50, lex=0x7f8884004888, result=0x7f8884013330, setup_tables_done_option=0) at /10.2/sql/sql_select.cc:361
#16 0x000055bf6e1f7468 in execute_sqlcom_select (thd=0x7f8884000d50, all_tables=0x7f88840128d0) at /10.2/sql/sql_parse.cc:6225
#17 0x000055bf6e1edd60 in mysql_execute_command (thd=0x7f8884000d50) at /10.2/sql/sql_parse.cc:3532
#18 0x000055bf6e1fb195 in mysql_parse (thd=0x7f8884000d50, rawbuf=0x7f88840126a8 "select * from t1 window w2 as (partition by -1,1,0)", length=51, parser_state=0x7f88a008c5f0, is_com_multi=false, is_next_command=false) at /10.2/sql/sql_parse.cc:7740
#19 0x000055bf6e1e95d6 in dispatch_command (command=COM_QUERY, thd=0x7f8884000d50, packet=0x7f8884008b01 "select * from t1 window w2 as (partition by -1,1,0) ", packet_length=52, is_com_multi=false, is_next_command=false) at /10.2/sql/sql_parse.cc:1831
#20 0x000055bf6e1e805b in do_command (thd=0x7f8884000d50) at /10.2/sql/sql_parse.cc:1385
#21 0x000055bf6e33b8ae in do_handle_one_connection (connect=0x55bf715387b0) at /10.2/sql/sql_connect.cc:1336
#22 0x000055bf6e33b619 in handle_one_connection (arg=0x55bf715387b0) at /10.2/sql/sql_connect.cc:1241
#23 0x000055bf6eb3b29c in pfs_spawn_thread (arg=0x55bf715f1c40) at /10.2/storage/perfschema/pfs.cc:1869
#24 0x00007f88a65a0fa3 in start_thread (arg=<optimized out>) at pthread_create.c:486
#25 0x00007f88a5f244cf in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Comment by Varun Gupta (Inactive) [ 2020-07-01 ]

This problem arises when we have the the name window frames. The assert that is hit show that we have not allocated enough size in the ref_pointer_array. Currently the allocation procedure does not take into account the elements inside the named windows

Comment by Varun Gupta (Inactive) [ 2020-07-01 ]

In the function st_select_lex::setup_ref_array we find the size for 5 slices of the ref pointer array

  const uint n_elems= (n_sum_items +
                       n_child_sum_items +
                       item_list.elements +
                       select_n_reserved +
                       select_n_having_items +
                       select_n_where_fields +
                       order_group_num +
                       hidden_bit_fields) * 5;

Here in this size we don't take into account the elements inside the partition list of the named window.

Currently for queries for window functions:

SELECT ROW_NUMBER() OVER (PARTITION BY -1,0) FROM t1;

the elements inside the partition list are counted in select_n_having_items. This looks odd to me.
Here is the code doing it:

  if (thd->lex->current_select)
  {
    enum_parsing_place place= 
      thd->lex->current_select->parsing_place;
    if (place == SELECT_LIST || place == IN_HAVING)
      thd->lex->current_select->select_n_having_items++;
  }

Needs to discuss it with sanja

Comment by Varun Gupta (Inactive) [ 2020-07-07 ]

Patch
http://lists.askmonty.org/pipermail/commits/2020-July/014279.html

Comment by Oleksandr Byelkin [ 2020-07-08 ]

OK to push

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