Skip to content

Commit a775788

Browse files
raghunandanbhatitzanway
authored andcommitted
MDEV-37243: SP memory root protection disappears after a metadata change
Problem: When a stored routine involes a cursor and metadata of table on which the cursor is defined changes, the SP instruction has to be reparsed. For ex: CREATE OR REPLACE TABLE t1 (a INT); CREATE OR REPLACE FUNCTION f1() RETURNS INT BEGIN DECLARE vc INT DEFAULT 0; DECLARE cur CURSOR FOR SELECT a FROM t1; OPEN cur; FETCH cur INTO vc; CLOSE cur; RETURN vc; END; SELECT f1(); - first execution, sp-mem_root marked read-only on exec SELECT f1(); - read-only sp-mem_root ALTER TABLE t1 MODIFY a TEXT; - metadta change SELECT f1(); - reparse, rerun instr and mark new mem_root read-only sp_lex_instr is re-parsed after the metadata change, which sets up a new mem_root for reparsing. Once the instruction is re-parsed and re-executed(via reset_lex_and_exec_core), the new memory root assigned to the instruction being reparsed remains writable. This violates the invariant of SP memory root protection. Fix: Mark the new memory root created for reparsing with read-only flag, after the first execution of the SP instruction.
1 parent d3767f9 commit a775788

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

sql/sp_instr.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,19 @@ int sp_lex_keeper::validate_lex_and_exec_core(THD *thd, uint *nextp,
534534
m_first_execution= false;
535535

536536
if (!rc)
537+
{
538+
/*
539+
sp_lex_instr is re-parsed after the metadata change, which sets up a new
540+
mem_root for reparsing. Once the sp_lex_instr is reparsed and re-executed
541+
(via reset_lex_and_exec_core) it should be marked as read-only to enforce
542+
sp memory root protection.
543+
*/
544+
#ifdef PROTECT_STATEMENT_MEMROOT
545+
if (rerun_the_same_instr && instr->mem_root)
546+
instr->mem_root->flags |= ROOT_FLAG_READ_ONLY;
547+
#endif
537548
break;
549+
}
538550

539551
/*
540552
Raise the error upper level in case:

0 commit comments

Comments
 (0)