Check and remove high stack usage

I checked all stack overflow potential problems found with
gcc -Wstack-usage=16384
and
clang -Wframe-larger-than=16384 -no-inline

Fixes:
Added '#pragma clang diagnostic ignored "-Wframe-larger-than="'
  to a lot of function to where stack usage large but resonable.
- Added stack check warnings to BUILD scrips when using clang and debug.

Function changed to use malloc instead allocating things on stack:
- read_bootstrap_query() now allocates line_buffer (20000 bytes) with
  malloc() instead of using stack. This has a small performance impact
  but this is not releant for bootstrap.
- mroonga grn_select() used 65856 bytes on stack. Changed it to use
  malloc().
- Wsrep_schema::replay_transaction() and
  Wsrep_schema::recover_sr_transactions().
- Connect zipOpen3()

Not fixed:
- mroonga/vendor/groonga/lib/expr.c grn_proc_call() uses
  43712 byte on stack.  However this is not easy to fix as the stack
  used is caused by a lot of code generated by defines.
- Most changes in mroonga/groonga where only adding of pragmas to disable
  stack warnings.
- rocksdb/options/options_helper.cc uses 20288 of stack space.
  (no reason to fix except to get rid of the compiler warning)
- Causes using alloca() where the allocation size is resonable.
- An issue in libmariadb (reported to connectors).
This commit is contained in:
Monty 2024-04-19 13:10:58 +03:00
commit 0ccdf54b64
32 changed files with 293 additions and 106 deletions

View file

@ -200,6 +200,7 @@ endif()
include_directories(
BEFORE
${CMAKE_SOURCE_DIR}/include
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/lib

View file

@ -38,6 +38,7 @@
#include "grn_util.h"
#include "grn_cache.h"
#include "grn_window_functions.h"
#include <my_attribute.h>
#include <string.h>
#include <math.h>
@ -1060,6 +1061,8 @@ grn_table_create_validate(grn_ctx *ctx, const char *name, unsigned int name_size
return ctx->rc;
}
PRAGMA_DISABLE_CHECK_STACK_FRAME
static grn_obj *
grn_table_create_with_max_n_subrecs(grn_ctx *ctx, const char *name,
unsigned int name_size, const char *path,
@ -1238,6 +1241,7 @@ grn_table_create_with_max_n_subrecs(grn_ctx *ctx, const char *name,
}
return res;
}
PRAGMA_REENABLE_CHECK_STACK_FRAME
grn_obj *
grn_table_create(grn_ctx *ctx, const char *name, unsigned int name_size,
@ -4776,6 +4780,9 @@ _grn_table_key(grn_ctx *ctx, grn_obj *table, grn_id id, uint32_t *key_size)
/* column */
PRAGMA_DISABLE_CHECK_STACK_FRAME
grn_obj *
grn_column_create(grn_ctx *ctx, grn_obj *table,
const char *name, unsigned int name_size,
@ -4978,6 +4985,7 @@ exit :
if (!res && id) { grn_obj_delete_by_id(ctx, db, id, GRN_TRUE); }
GRN_API_RETURN(res);
}
PRAGMA_REENABLE_CHECK_STACK_FRAME
grn_obj *
grn_column_open(grn_ctx *ctx, grn_obj *table,
@ -8540,6 +8548,8 @@ grn_obj_spec_save(grn_ctx *ctx, grn_db_obj *obj)
grn_obj_close(ctx, &v);
}
PRAGMA_DISABLE_CHECK_STACK_FRAME
inline static void
grn_obj_set_info_source_invalid_lexicon_error(grn_ctx *ctx,
const char *message,
@ -8590,6 +8600,8 @@ grn_obj_set_info_source_invalid_lexicon_error(grn_ctx *ctx,
source_name_size, source_name);
}
PRAGMA_REENABLE_CHECK_STACK_FRAME
inline static grn_rc
grn_obj_set_info_source_validate(grn_ctx *ctx, grn_obj *obj, grn_obj *value)
{
@ -8597,7 +8609,7 @@ grn_obj_set_info_source_validate(grn_ctx *ctx, grn_obj *obj, grn_obj *value)
grn_obj *lexicon = NULL;
grn_id lexicon_domain_id;
grn_obj *lexicon_domain = NULL;
grn_bool lexicon_domain_is_table;
grn_bool lexicon_domain_is_table __attribute__((unused));
grn_bool lexicon_have_tokenizer;
grn_id *source_ids;
int i, n_source_ids;
@ -9330,7 +9342,7 @@ remove_reference_tables(grn_ctx *ctx, grn_obj *table, grn_obj *db)
grn_bool is_close_opened_object_mode = GRN_FALSE;
grn_id table_id;
char table_name[GRN_TABLE_MAX_KEY_SIZE];
int table_name_size;
int table_name_size __attribute__((unused));
grn_table_cursor *cursor;
if (grn_thread_get_limit() == 1) {
@ -10317,12 +10329,10 @@ grn_db_spec_unpack(grn_ctx *ctx,
const char *error_message_tag)
{
grn_obj *db;
grn_db *db_raw;
grn_rc rc;
uint32_t spec_size;
db = ctx->impl->db;
db_raw = (grn_db *)db;
rc = grn_vector_decode(ctx,
decoded_spec,

View file

@ -20,6 +20,9 @@
#include "grn_ctx_impl.h"
#include "grn_db.h"
#include "grn_util.h"
#include <my_attribute.h>
PRAGMA_DISABLE_CHECK_STACK_FRAME
static void
grn_loader_save_error(grn_ctx *ctx, grn_loader *loader)
@ -1228,3 +1231,5 @@ grn_load(grn_ctx *ctx, grn_content_type input_type,
}
GRN_API_RETURN(ctx->rc);
}
PRAGMA_REENABLE_CHECK_STACK_FRAME

View file

@ -20,6 +20,7 @@
#include "grn_db.h"
#include "grn_str.h"
#include "grn_normalizer.h"
#include <my_attribute.h>
#include <string.h>
@ -31,6 +32,8 @@
# include <onigmo.h>
#endif
PRAGMA_DISABLE_CHECK_STACK_FRAME
static const char *operator_names[] = {
"push",
"pop",
@ -1360,3 +1363,5 @@ grn_operator_exec_regexp(grn_ctx *ctx, grn_obj *target, grn_obj *pattern)
}
GRN_API_RETURN(matched);
}
PRAGMA_REENABLE_CHECK_STACK_FRAME

View file

@ -18,6 +18,7 @@
#include "../grn_proc.h"
#include "../grn_db.h"
#include <my_attribute.h>
#include <groonga/plugin.h>
@ -73,6 +74,8 @@ command_object_list_dump_flags(grn_ctx *ctx, grn_obj_spec *spec)
GRN_OBJ_FIN(ctx, &flags);
}
PRAGMA_DISABLE_CHECK_STACK_FRAME
static grn_obj *
command_object_list(grn_ctx *ctx,
int nargs,
@ -401,6 +404,7 @@ command_object_list(grn_ctx *ctx,
return NULL;
}
PRAGMA_REENABLE_CHECK_STACK_FRAME
void
grn_proc_init_object_list(grn_ctx *ctx)

View file

@ -17,9 +17,8 @@
*/
#include "../grn_proc.h"
#include "../grn_db.h"
#include <my_attribute.h>
#include <groonga/plugin.h>
typedef struct {
@ -572,6 +571,8 @@ command_schema_table_output_token_filters(grn_ctx *ctx, grn_obj *table)
GRN_OBJ_FIN(ctx, &token_filters);
}
PRAGMA_DISABLE_CHECK_STACK_FRAME
static void
command_schema_table_command_collect_arguments(grn_ctx *ctx,
grn_obj *table,
@ -692,6 +693,7 @@ command_schema_table_command_collect_arguments(grn_ctx *ctx,
#undef ADD_OBJECT_NAME
#undef ADD
}
PRAGMA_REENABLE_CHECK_STACK_FRAME
static void
command_schema_table_output_command(grn_ctx *ctx, grn_obj *table)
@ -875,6 +877,8 @@ command_schema_output_indexes(grn_ctx *ctx, grn_obj *object)
}
}
PRAGMA_DISABLE_CHECK_STACK_FRAME
static void
command_schema_column_command_collect_arguments(grn_ctx *ctx,
grn_obj *table,
@ -973,6 +977,7 @@ command_schema_column_command_collect_arguments(grn_ctx *ctx,
#undef ADD_OBJECT_NAME
#undef ADD
}
PRAGMA_REENABLE_CHECK_STACK_FRAME
static void
command_schema_column_output_command(grn_ctx *ctx,

View file

@ -24,6 +24,7 @@
#include "../grn_util.h"
#include "../grn_cache.h"
#include "../grn_ii.h"
#include <my_attribute.h>
#include "../grn_ts.h"
@ -2912,7 +2913,7 @@ grn_select(grn_ctx *ctx, grn_select_data *data)
uint32_t nhits;
grn_obj *outbuf = ctx->impl->output.buf;
grn_content_type output_type = ctx->impl->output.type;
char cache_key[GRN_CACHE_MAX_KEY_SIZE];
char *cache_key_buffer= 0;
uint32_t cache_key_size;
long long int threshold, original_threshold = 0;
grn_cache *cache_obj = grn_cache_current_get(ctx);
@ -2985,8 +2986,9 @@ grn_select(grn_ctx *ctx, grn_select_data *data)
} GRN_HASH_EACH_END(ctx, cursor);
}
#undef DRILLDOWN_CACHE_SIZE
if (cache_key_size <= GRN_CACHE_MAX_KEY_SIZE) {
char *cp = cache_key;
if (cache_key_size <= GRN_CACHE_MAX_KEY_SIZE &&
(cache_key_buffer= (char*) malloc(cache_key_size+1))) {
char *cp = cache_key_buffer;
#define PUT_CACHE_KEY(string) \
if ((string).value) \
@ -3066,11 +3068,12 @@ grn_select(grn_ctx *ctx, grn_select_data *data)
{
grn_rc rc;
rc = grn_cache_fetch(ctx, cache_obj, cache_key, cache_key_size, outbuf);
rc = grn_cache_fetch(ctx, cache_obj, cache_key_buffer, cache_key_size, outbuf);
if (rc == GRN_SUCCESS) {
GRN_QUERY_LOG(ctx, GRN_QUERY_LOG_CACHE,
":", "cache(%" GRN_FMT_LLD ")",
(long long int)GRN_TEXT_LEN(outbuf));
free(cache_key_buffer);
return ctx->rc;
}
}
@ -3119,7 +3122,7 @@ grn_select(grn_ctx *ctx, grn_select_data *data)
data->cache.length != 2 ||
data->cache.value[0] != 'n' ||
data->cache.value[1] != 'o')) {
grn_cache_update(ctx, cache_obj, cache_key, cache_key_size, outbuf);
grn_cache_update(ctx, cache_obj, cache_key_buffer, cache_key_size, outbuf);
}
goto exit;
}
@ -3186,7 +3189,7 @@ grn_select(grn_ctx *ctx, grn_select_data *data)
data->cache.length != 2 ||
data->cache.value[0] != 'n' ||
data->cache.value[1] != 'o')) {
grn_cache_update(ctx, cache_obj, cache_key, cache_key_size, outbuf);
grn_cache_update(ctx, cache_obj, cache_key_buffer, cache_key_size, outbuf);
}
if (data->taintable > 0) {
grn_db_touch(ctx, DB_OBJ(data->tables.target)->db);
@ -3200,6 +3203,7 @@ exit :
/* GRN_LOG(ctx, GRN_LOG_NONE, "%d", ctx->seqno); */
free(cache_key_buffer);
return ctx->rc;
}
@ -3424,6 +3428,9 @@ grn_select_data_fill_drilldown_columns(grn_ctx *ctx,
strlen(prefix));
}
PRAGMA_DISABLE_CHECK_STACK_FRAME
static grn_bool
grn_select_data_fill_drilldowns(grn_ctx *ctx,
grn_user_data *user_data,
@ -3562,6 +3569,7 @@ grn_select_data_fill_drilldowns(grn_ctx *ctx,
return succeeded;
}
}
PRAGMA_REENABLE_CHECK_STACK_FRAME
static grn_obj *
command_select(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)