Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-42276

Assertion `(mem_root->flags & 4) == 0' failed in alloc_root() when a stored procedure with a parameter DEFAULT (SELECT a FROM t1) is called with and then without an argument

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • 13.2
    • None
    • Stored routines
    • None

    Description

      Problem

      A stored procedure with a parameter whose default is a subquery that reads a table hits a debug assertion on the second call, when the procedure is called first with an argument and then without one. The assertion text is the same as in MDEV-37491, whose testcase (a default without a table reference, DEFAULT (SELECT 1)) no longer crashes on current main. With a table reference in the default subquery the assertion is still reachable, through a different code path.

      How to repeat

      CREATE TABLE t1 (a INT);
      DELIMITER //
      CREATE PROCEDURE p1(par1 INT DEFAULT (SELECT a FROM t1)) BEGIN SELECT par1 FROM DUAL; END//
      DELIMITER ;
      CALL p1(10);
      CALL p1();

      Actual result

      The server aborts on the last statement:

      mysys/my_alloc.c:276: void *alloc_root(MEM_ROOT *, size_t): Assertion `(mem_root->flags & 4) == 0' failed.
      mariadbd got signal 6 ;
      Query (0x...): CALL p1()

      Fresh server, 3 of 3 runs crash. The testcase of MDEV-37491 (DEFAULT (SELECT 1), no table) does not crash on the same build.

      Analysis

      This is inferred from the stack, no debugger session on the source. A MEM_ROOT that was marked read-only after the first execution of the procedure (flag value 4) is used to allocate when the default subquery is evaluated again. In this variant the allocation happens in st_select_lex::save_leaf_tables() while the default expression of the procedure parameter is evaluated, which is a different site from the one in MDEV-37491 (there the stack went through JOIN::prepare and setup_ref_array). It may belong to the same family as MDEV-41346 (systematic handling of allocations on a read-only MEM_ROOT after the first execution); whether it needs its own fix is for the maintainers to decide.

      Stack, debug build at 52391ebe (top frames from the debugger session):

      alloc_root(MEM_ROOT*, size_t)                      mysys/my_alloc.c:276
      Sql_alloc::operator new(size_t, MEM_ROOT*)         sql/sql_alloc.h:37
      base_list::push_back(...)                          sql/sql_list.h:190
      List<TABLE_LIST>::push_back(...)                   sql/sql_list.h:518
      st_select_lex::save_leaf_tables(THD*)              sql/sql_lex.cc:6064
      JOIN::optimize_inner()
      (subselect execution)
      Item_singlerow_subselect::val_int()
      Item::save_int_in_field(Field*, bool)
      Item_save_in_field

      Attachments

        Activity

          People

            Unassigned Unassigned
            hacker_zheng Wang Zheng
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.