Skip to content

Commit dbeef00

Browse files
committed
MDEV-37052 JSON_SCHEMA_VALID stack overflow handling errors
Since MDEV-33209 (09ea2dc) the the stack overflow errors are just injected instead of frailer mechanisms to consume stack. These mechanims where not carried forward to the JSON_TABLE or JSON_SCHEMA_VALID where the pattern was the same. add_extra_deps also no-longer recursively iterates in out of stack conditions. Tests performed in json_debug_nonembedded(_noasan).
1 parent 29c51ee commit dbeef00

File tree

4 files changed

+21
-44
lines changed

4 files changed

+21
-44
lines changed

sql/item_jsonfunc.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static bool check_overlaps(json_engine_t *, json_engine_t *, bool);
2626
static int json_find_overlap_with_object(json_engine_t *, json_engine_t *, bool);
2727

2828
#ifndef DBUG_OFF
29-
static int dbug_json_check_min_stack_requirement()
29+
int dbug_json_check_min_stack_requirement()
3030
{
3131
my_error(ER_STACK_OVERRUN_NEED_MORE, MYF(ME_FATAL),
3232
my_thread_stack_size, my_thread_stack_size, STACK_MIN_SIZE);

sql/json_schema.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
#include "json_schema.h"
2222
#include "json_schema_helper.h"
2323
#include "pcre2.h"
24+
25+
#ifndef DBUG_OFF
26+
int dbug_json_check_min_stack_requirement();
27+
#endif
28+
2429
static HASH all_keywords_hash;
2530

2631
static Json_schema_keyword *create_json_schema_keyword(THD *thd)
@@ -2779,15 +2784,9 @@ bool create_object_and_handle_keyword(THD *thd, json_engine_t *je,
27792784
List<Json_schema_keyword> temporary_list;
27802785

27812786
DBUG_EXECUTE_IF("json_check_min_stack_requirement",
2782-
{
2783-
long arbitrary_var;
2784-
long stack_used_up=
2785-
(available_stack_size(thd->thread_stack,
2786-
&arbitrary_var));
2787-
ALLOCATE_MEM_ON_STACK(my_thread_stack_size-stack_used_up-STACK_MIN_SIZE);
2788-
});
2787+
dbug_json_check_min_stack_requirement(); return true;);
27892788
if (check_stack_overrun(thd, STACK_MIN_SIZE , NULL))
2790-
return 1;
2789+
return true;
27912790

27922791
while (json_scan_next(je)== 0 && je->stack_p >= level)
27932792
{

sql/json_table.cc

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#include "create_tmp_table.h"
2828
#include "sql_parse.h"
2929

30+
#ifndef DBUG_OFF
31+
int dbug_json_check_min_stack_requirement();
32+
#endif
33+
3034
#define HA_ERR_JSON_TABLE (HA_ERR_LAST+1)
3135

3236
class table_function_handlerton
@@ -104,13 +108,9 @@ int get_disallowed_table_deps_for_list(MEM_ROOT *mem_root,
104108
List_iterator<TABLE_LIST> li(*join_list);
105109

106110
DBUG_EXECUTE_IF("json_check_min_stack_requirement",
107-
{
108-
long arbitrary_var;
109-
long stack_used_up= (available_stack_size(current_thd->thread_stack, &arbitrary_var));
110-
ALLOCATE_MEM_ON_STACK(my_thread_stack_size-stack_used_up-STACK_MIN_SIZE);
111-
});
111+
return -dbug_json_check_min_stack_requirement(););
112112
if (check_stack_overrun(current_thd, STACK_MIN_SIZE , NULL))
113-
return 1;
113+
return -1;
114114

115115
while ((table= li++))
116116
{
@@ -1345,29 +1345,27 @@ void Table_function_json_table::fix_after_pullout(TABLE_LIST *sql_table,
13451345
Recursively make all tables in the join_list also depend on deps.
13461346
*/
13471347

1348-
static void add_extra_deps(List<TABLE_LIST> *join_list, table_map deps)
1348+
static bool add_extra_deps(List<TABLE_LIST> *join_list, table_map deps)
13491349
{
13501350
TABLE_LIST *table;
13511351
List_iterator<TABLE_LIST> li(*join_list);
13521352

13531353
DBUG_EXECUTE_IF("json_check_min_stack_requirement",
1354-
{
1355-
long arbitrary_var;
1356-
long stack_used_up= (available_stack_size(current_thd->thread_stack, &arbitrary_var));
1357-
ALLOCATE_MEM_ON_STACK(my_thread_stack_size-stack_used_up-STACK_MIN_SIZE);
1358-
});
1354+
dbug_json_check_min_stack_requirement(); return true;);
13591355
if (check_stack_overrun(current_thd, STACK_MIN_SIZE , NULL))
1360-
return;
1356+
return true;
13611357
while ((table= li++))
13621358
{
13631359
table->dep_tables |= deps;
13641360
NESTED_JOIN *nested_join;
13651361
if ((nested_join= table->nested_join))
13661362
{
13671363
// set the deps inside, too
1368-
add_extra_deps(&nested_join->join_list, deps);
1364+
if (add_extra_deps(&nested_join->join_list, deps))
1365+
return true;
13691366
}
13701367
}
1368+
return false;
13711369
}
13721370

13731371

@@ -1447,11 +1445,7 @@ table_map add_table_function_dependencies(List<TABLE_LIST> *join_list,
14471445
List_iterator<TABLE_LIST> li(*join_list);
14481446

14491447
DBUG_EXECUTE_IF("json_check_min_stack_requirement",
1450-
{
1451-
long arbitrary_var;
1452-
long stack_used_up= (available_stack_size(current_thd->thread_stack, &arbitrary_var));
1453-
ALLOCATE_MEM_ON_STACK(my_thread_stack_size-stack_used_up-STACK_MIN_SIZE);
1454-
});
1448+
if (dbug_json_check_min_stack_requirement()) return 0;);
14551449
if ((res=check_stack_overrun(current_thd, STACK_MIN_SIZE , NULL)))
14561450
return res;
14571451

sql/sql_parse.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,20 +189,4 @@ check_table_access(THD *thd, privilege_t requirements,TABLE_LIST *tables,
189189
{ return false; }
190190
#endif /*NO_EMBEDDED_ACCESS_CHECKS*/
191191

192-
193-
/*
194-
Allocating memory and *also* using it (reading and
195-
writing from it) because some build instructions cause
196-
compiler to optimize out stack_used_up. Since alloca()
197-
here depends on stack_used_up, it doesnt get executed
198-
correctly and causes json_debug_nonembedded to fail
199-
( --error ER_STACK_OVERRUN_NEED_MORE does not occur).
200-
*/
201-
#define ALLOCATE_MEM_ON_STACK(A) do \
202-
{ \
203-
uchar *array= (uchar*)alloca(A); \
204-
bzero(array, A); \
205-
my_checksum(0, array, A); \
206-
} while(0)
207-
208192
#endif /* SQL_PARSE_INCLUDED */

0 commit comments

Comments
 (0)