Skip to content

Commit 0d9db6b

Browse files
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 e2f91f4 commit 0d9db6b

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
@@ -517,7 +517,19 @@ int sp_lex_keeper::validate_lex_and_exec_core(THD *thd, uint *nextp,
517517
m_first_execution= false;
518518

519519
if (!rc)
520+
{
521+
/*
522+
sp_lex_instr is re-parsed after the metadata change, which sets up a new
523+
mem_root for reparsing. Once the sp_lex_instr is reparsed and re-executed
524+
(via reset_lex_and_exec_core) it should be marked as read-only to enforce
525+
sp memory root protection.
526+
*/
527+
#ifdef PROTECT_STATEMENT_MEMROOT
528+
if (rerun_the_same_instr && instr->mem_root)
529+
instr->mem_root->flags |= ROOT_FLAG_READ_ONLY;
530+
#endif
520531
break;
532+
}
521533

522534
/*
523535
Raise the error upper level in case:

0 commit comments

Comments
 (0)