MySQL 26.10.0
Source Code Documentation
check_stack.cc File Reference
#include "sql/check_stack.h"
#include "my_config.h"
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include <atomic>
#include <new>
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "sql/current_thd.h"
#include "sql/derror.h"
#include "sql/sql_class.h"

Namespaces

namespace  anonymous_namespace{check_stack.cc}
 

Functions

static NO_INLINE int stack_direction_f (const char *a)
 Determine whether the stack grows upward or downward. More...
 
NO_INLINE int initialize_stack_direction ()
 
long anonymous_namespace{check_stack.cc}::used_stack (const char *a_ptr, const char *b_ptr)
 
bool check_stack_overrun (const THD *thd, long margin, unsigned char *buf)
 Check stack for a overrun. More...
 

Variables

int anonymous_namespace{check_stack.cc}::stack_direction = 0
 
static int(* ptr_f )(const char *) = stack_direction_f
 
static std::atomic< long > max_stack_used
 

Function Documentation

◆ check_stack_overrun()

bool check_stack_overrun ( const THD *  thd,
long  margin,
unsigned char *  buf 
)

Check stack for a overrun.

Parameters
thdThread handler.
marginMinimal acceptable unused space in the stack.
bufSee a note below.
Returns
false if success, true if error (reported).
Note
Note: The 'buf' parameter is necessary, and we must have code which uses it.
  • Some of the fix_fields functions have a "dummy" buffer large enough for the corresponding execution. (Thus we only have to check in fix_fields.)
  • Passing the buffer to check_stack_overrun() prevents the compiler from removing it.
  • For -flto builds, the dummy buffer may be optimized away, so we need to write something into it.

◆ initialize_stack_direction()

NO_INLINE int initialize_stack_direction ( )

◆ stack_direction_f()

static NO_INLINE int stack_direction_f ( const char *  a)
static

Determine whether the stack grows upward or downward.

The function is deliberately not inlined, so a belongs to its caller's frame while b_marker belongs to this frame. In ordinary builds, resolve_stack_pointer() returns those local markers unchanged and their relative addresses reveal the stack-growth direction.

In ASan builds, local variables may be allocated on ASan's fake stack. resolve_stack_pointer() therefore replaces both markers with physical frame addresses before comparing them. This keeps stack-direction detection in the same address space as later stack-overrun checks.

Parameters
aStack marker from initialize_stack_direction().
Returns
1 if the stack grows upward, otherwise 0.

Variable Documentation

◆ max_stack_used

std::atomic<long> max_stack_used
static

◆ ptr_f

int(* ptr_f) (const char *) ( const char *  ) = stack_direction_f
static