PostgreSQL Source Code git master
Loading...
Searching...
No Matches
stack_depth.c File Reference
#include "postgres.h"
#include <limits.h>
#include <sys/resource.h>
#include "miscadmin.h"
#include "utils/guc_hooks.h"
Include dependency graph for stack_depth.c:

Go to the source code of this file.

Functions

pg_stack_base_t set_stack_base (void)
 
void restore_stack_base (pg_stack_base_t base)
 
void check_stack_depth (void)
 
bool stack_is_too_deep (void)
 
bool check_max_stack_depth (int *newval, void **extra, GucSource source)
 
void assign_max_stack_depth (int newval, void *extra)
 
ssize_t get_stack_depth_rlimit (void)
 

Variables

int max_stack_depth = 100
 
static ssize_t max_stack_depth_bytes = 100 * (ssize_t) 1024
 
static charstack_base_ptr = NULL
 

Function Documentation

◆ assign_max_stack_depth()

void assign_max_stack_depth ( int  newval,
void extra 
)

Definition at line 175 of file stack_depth.c.

176{
178
180}
#define newval
static int fb(int x)
static ssize_t max_stack_depth_bytes
Definition stack_depth.c:29

References fb(), max_stack_depth_bytes, and newval.

◆ check_max_stack_depth()

bool check_max_stack_depth ( int newval,
void **  extra,
GucSource  source 
)

Definition at line 158 of file stack_depth.c.

159{
162
164 {
165 GUC_check_errdetail("\"max_stack_depth\" must not exceed %zdkB.",
166 (stack_rlimit - STACK_DEPTH_SLOP) / 1024);
167 GUC_check_errhint("Increase the platform's stack depth limit via \"ulimit -s\" or local equivalent.");
168 return false;
169 }
170 return true;
171}
#define GUC_check_errdetail
Definition guc.h:507
#define GUC_check_errhint
Definition guc.h:511
#define STACK_DEPTH_SLOP
Definition miscadmin.h:300
ssize_t get_stack_depth_rlimit(void)

References fb(), get_stack_depth_rlimit(), GUC_check_errdetail, GUC_check_errhint, newval, and STACK_DEPTH_SLOP.

◆ check_stack_depth()

void check_stack_depth ( void  )

Definition at line 96 of file stack_depth.c.

97{
99 {
102 errmsg("stack depth limit exceeded"),
103 errhint("Increase the configuration parameter \"max_stack_depth\" (currently %dkB), "
104 "after ensuring the platform's stack depth limit is adequate.",
106 }
107}
int errcode(int sqlerrcode)
Definition elog.c:875
int errhint(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
static char * errmsg
int max_stack_depth
Definition stack_depth.c:26
bool stack_is_too_deep(void)

References ereport, errcode(), errhint(), errmsg, ERROR, fb(), max_stack_depth, and stack_is_too_deep().

Referenced by _jumbleNode(), AlterTypeRecurse(), apply_scanjoin_target_to_paths(), ATAddCheckNNConstraint(), ATExecAddColumn(), ATExecAlterCheckConstrEnforceability(), ATExecAlterConstrDeferrability(), ATExecAlterFKConstrEnforceability(), ATExecDropColumn(), ATExecSetNotNull(), CheckAttributeType(), checkCond(), clean_NOT_intree(), clean_stopword_intree(), cntsize(), contains_required_value(), convertJsonbValue(), copyObjectImpl(), Cover(), create_plan_recurse(), datum_to_json_internal(), datum_to_jsonb_internal(), dofindsubquery(), dropconstraint_internal(), emit_jsp_gin_entries(), equal(), eval_const_expressions_mutator(), EventTriggerInvoke(), ExecEndNode(), ExecEvalSubPlan(), ExecInitExprRec(), ExecInitNode(), ExecMakeFunctionResultSet(), ExecProcNodeFirst(), ExecShutdownNode_walker(), execute(), executeAnyItem(), executeBoolItem(), executeItemOptUnwrapTarget(), expand_partitioned_rtentry(), expr_is_nonnullable(), expression_tree_mutator_impl(), expression_tree_walker_impl(), extract_jsp_bool_expr(), fillQT(), find_composite_type_dependencies(), find_matching_subplans_recurse(), findDependentObjects(), findoprnd(), findoprnd(), findoprnd_recurse(), flatten_grouping_sets(), flattenJsonPathParseItem(), FreePageManagerDumpBtree(), freetree(), generate_partition_qual(), generate_partitionwise_join_paths(), generate_queries_for_path_pattern_recurse(), generate_setop_from_pathqueries(), get_graph_label_expr(), get_query_def(), get_rels_with_domain(), get_rule_expr(), get_setop_query(), get_steps_using_prefix_recurse(), gistSplit(), hash_range(), hash_range_extended(), hash_record(), hash_record_extended(), hk_depth_search(), infix(), infix(), infix(), inline_function_in_from(), int_query_opr_selec(), is_simple_union_all_recurse(), json_check_mutability(), JsonbDeepContains(), lseg_inside_poly(), ltree_execute(), makepol(), makepol(), makepol(), maketree(), MatchText(), MultiExecProcNode(), outNode(), p_ishost(), p_isURLPath(), parse_array(), parse_object(), parseNodeString(), plainnode(), planstate_tree_walker_impl(), plperl_hash_from_tuple(), plperl_sv_to_datum(), PLy_input_setup_func(), PLy_output_setup_func(), populate_array_dim_jsonb(), populate_record_field(), printJsonPathItem(), pull_up_sublinks_jointree_recurse(), pull_up_subqueries_recurse(), QT2QTN(), QTNBinary(), QTNClearFlags(), QTNCopy(), QTNFree(), QTNodeCompare(), QTNSort(), QTNTernary(), QueueFKConstraintValidation(), range_cmp(), range_in(), range_out(), range_recv(), range_send(), raw_expression_tree_walker_impl(), record_cmp(), record_eq(), record_in(), record_out(), record_recv(), record_send(), recurse_set_operations(), regex_selectivity_sub(), relation_is_updatable(), resolve_special_varno(), set_append_rel_size(), setPath(), split_array(), SplitToVariants(), standard_ProcessUtility(), transformExprRecurse(), transformFromClauseItem(), transformLabelExpr(), transformSetOperationTree(), traverse_lacons(), try_partitionwise_join(), TS_execute_locations_recurse(), TS_execute_recurse(), TS_phrase_execute(), tsquery_opr_selec(), and tsquery_requires_match().

◆ get_stack_depth_rlimit()

ssize_t get_stack_depth_rlimit ( void  )

Definition at line 192 of file stack_depth.c.

193{
194#if defined(HAVE_GETRLIMIT)
195 static ssize_t val = 0;
196
197 /* This won't change after process launch, so check just once */
198 if (val == 0)
199 {
200 struct rlimit rlim;
201
202 if (getrlimit(RLIMIT_STACK, &rlim) < 0)
203 val = -1;
204 else if (rlim.rlim_cur == RLIM_INFINITY)
205 val = SSIZE_MAX;
206 /* rlim_cur is probably of an unsigned type, so check for overflow */
207 else if (rlim.rlim_cur >= SSIZE_MAX)
208 val = SSIZE_MAX;
209 else
210 val = rlim.rlim_cur;
211 }
212 return val;
213#else
214 /* On Windows we set the backend stack size in src/backend/Makefile */
215 return WIN32_STACK_RLIMIT;
216#endif
217}
long val
Definition informix.c:689

References fb(), and val.

Referenced by check_max_stack_depth(), and InitializeGUCOptionsFromEnvironment().

◆ restore_stack_base()

void restore_stack_base ( pg_stack_base_t  base)

Definition at line 78 of file stack_depth.c.

79{
80 stack_base_ptr = base;
81}
static char * stack_base_ptr
Definition stack_depth.c:35

References stack_base_ptr.

◆ set_stack_base()

pg_stack_base_t set_stack_base ( void  )

Definition at line 44 of file stack_depth.c.

45{
46#ifndef HAVE__BUILTIN_FRAME_ADDRESS
47 char stack_base;
48#endif
50
52
53 /*
54 * Set up reference point for stack depth checking. On recent gcc we use
55 * __builtin_frame_address() to avoid a warning about storing a local
56 * variable's address in a long-lived variable. This is also important
57 * with address sanitizer, see comment in stack_is_too_deep().
58 */
59#ifdef HAVE__BUILTIN_FRAME_ADDRESS
61#else
63#endif
64
65 return old;
66}
char * pg_stack_base_t
Definition miscadmin.h:302

References fb(), and stack_base_ptr.

Referenced by main().

◆ stack_is_too_deep()

bool stack_is_too_deep ( void  )

Definition at line 110 of file stack_depth.c.

111{
112#ifndef HAVE__BUILTIN_FRAME_ADDRESS
113 char stack_top_loc;
114#endif
116 char *stack_address;
117
118 /*
119 * With address sanitizer's stack-use-after-return check, stack variables
120 * are moved to heap allocations, to allow to detect references to the
121 * memory at a later time. That would break our stack-depth check. Luckily
122 * __builtin_frame_address() works correctly, even under asan.
123 */
124#ifndef HAVE__BUILTIN_FRAME_ADDRESS
126#else
128#endif
129
130 /*
131 * Compute distance from reference point to my stack frame.
132 */
134
135 /*
136 * Take abs value, since stacks grow up on some machines, down on others
137 */
138 if (stack_depth < 0)
140
141 /*
142 * Trouble?
143 *
144 * The test on stack_base_ptr prevents us from erroring out if called
145 * before that's been set. Logically it should be done first, but putting
146 * it last avoids wasting cycles during normal cases.
147 */
150 return true;
151
152 return false;
153}

References fb(), max_stack_depth_bytes, and stack_base_ptr.

Referenced by check_stack_depth(), IsSquashableConstant(), MemoryContextStatsInternal(), rstacktoodeep(), and ShowTransactionStateRec().

Variable Documentation

◆ max_stack_depth

int max_stack_depth = 100

Definition at line 26 of file stack_depth.c.

Referenced by check_stack_depth().

◆ max_stack_depth_bytes

ssize_t max_stack_depth_bytes = 100 * (ssize_t) 1024
static

Definition at line 29 of file stack_depth.c.

Referenced by assign_max_stack_depth(), and stack_is_too_deep().

◆ stack_base_ptr

char* stack_base_ptr = NULL
static

Definition at line 35 of file stack_depth.c.

Referenced by restore_stack_base(), set_stack_base(), and stack_is_too_deep().