^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright 2013-2017 by PaX Team <pageexec@freemail.hu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Licensed under the GPL v2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Note: the choice of the license means that the compilation process is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * NOT 'eligible' as defined by gcc's library exception to the GPL v3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * but for the kernel it doesn't matter since it doesn't link against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * any of the gcc libraries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * gcc plugin to forcibly initialize certain local variables that could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * otherwise leak kernel stack to userland if they aren't properly initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * by later code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Homepage: https://pax.grsecurity.net/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Options:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * -fplugin-arg-structleak_plugin-disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * -fplugin-arg-structleak_plugin-verbose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * -fplugin-arg-structleak_plugin-byref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * -fplugin-arg-structleak_plugin-byref-all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Usage:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * $ # for 4.5/4.6/C based 4.7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * $ gcc -I`gcc -print-file-name=plugin`/include -I`gcc -print-file-name=plugin`/include/c-family -fPIC -shared -O2 -o structleak_plugin.so structleak_plugin.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * $ # for C++ based 4.7/4.8+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * $ g++ -I`g++ -print-file-name=plugin`/include -I`g++ -print-file-name=plugin`/include/c-family -fPIC -shared -O2 -o structleak_plugin.so structleak_plugin.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * $ gcc -fplugin=./structleak_plugin.so test.c -O2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * TODO: eliminate redundant initializers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "gcc-common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* unused C type flag in all versions 4.5-6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define TYPE_USERSPACE(TYPE) TYPE_LANG_FLAG_5(TYPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) __visible int plugin_is_GPL_compatible;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static struct plugin_info structleak_plugin_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) .version = "20190125vanilla",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) .help = "disable\tdo not activate plugin\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) "byref\tinit structs passed by reference\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) "byref-all\tinit anything passed by reference\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) "verbose\tprint all initialized variables\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define BYREF_STRUCT 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define BYREF_ALL 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static bool verbose;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static int byref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static tree handle_user_attribute(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) *no_add_attrs = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* check for types? for now accept everything linux has to offer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (TREE_CODE(*node) != FIELD_DECL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return NULL_TREE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) *no_add_attrs = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return NULL_TREE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static struct attribute_spec user_attr = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static void register_attributes(void *event_data, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) user_attr.name = "user";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) user_attr.handler = handle_user_attribute;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #if BUILDING_GCC_VERSION >= 4007
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) user_attr.affects_type_identity = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) register_attribute(&user_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static tree get_field_type(tree field)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return strip_array_types(TREE_TYPE(field));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static bool is_userspace_type(tree type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) tree field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) for (field = TYPE_FIELDS(type); field; field = TREE_CHAIN(field)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) tree fieldtype = get_field_type(field);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) enum tree_code code = TREE_CODE(fieldtype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (code == RECORD_TYPE || code == UNION_TYPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (is_userspace_type(fieldtype))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (lookup_attribute("user", DECL_ATTRIBUTES(field)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static void finish_type(void *event_data, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) tree type = (tree)event_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (type == NULL_TREE || type == error_mark_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #if BUILDING_GCC_VERSION >= 5000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (TREE_CODE(type) == ENUMERAL_TYPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (TYPE_USERSPACE(type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (is_userspace_type(type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) TYPE_USERSPACE(type) = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static void initialize(tree var)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) basic_block bb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) gimple_stmt_iterator gsi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) tree initializer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) gimple init_stmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) tree type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* this is the original entry bb before the forced split */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* first check if variable is already initialized, warn otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) gimple stmt = gsi_stmt(gsi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) tree rhs1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /* we're looking for an assignment of a single rhs... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!gimple_assign_single_p(stmt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) rhs1 = gimple_assign_rhs1(stmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #if BUILDING_GCC_VERSION >= 4007
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* ... of a non-clobbering expression... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (TREE_CLOBBER_P(rhs1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* ... to our variable... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (gimple_get_lhs(stmt) != var)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* if it's an initializer then we're good */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (TREE_CODE(rhs1) == CONSTRUCTOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* these aren't the 0days you're looking for */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) inform(DECL_SOURCE_LOCATION(var),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) "%s variable will be forcibly initialized",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) (byref && TREE_ADDRESSABLE(var)) ? "byref"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) : "userspace");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* build the initializer expression */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) type = TREE_TYPE(var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (AGGREGATE_TYPE_P(type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) initializer = build_constructor(type, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) initializer = fold_convert(type, integer_zero_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* build the initializer stmt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) init_stmt = gimple_build_assign(var, initializer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) gsi = gsi_after_labels(single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) gsi_insert_before(&gsi, init_stmt, GSI_NEW_STMT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) update_stmt(init_stmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static unsigned int structleak_execute(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) basic_block bb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) unsigned int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) tree var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* split the first bb where we can put the forced initializers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (!single_pred_p(bb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) split_edge(single_succ_edge(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /* enumerate all local variables and forcibly initialize our targets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) FOR_EACH_LOCAL_DECL(cfun, i, var) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) tree type = TREE_TYPE(var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) gcc_assert(DECL_P(var));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!auto_var_in_fn_p(var, current_function_decl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) /* only care about structure types unless byref-all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (byref != BYREF_ALL && TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /* if the type is of interest, examine the variable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (TYPE_USERSPACE(type) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) (byref && TREE_ADDRESSABLE(var)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) initialize(var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) #define PASS_NAME structleak
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) #define NO_GATE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) #define PROPERTIES_REQUIRED PROP_cfg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) #define TODO_FLAGS_FINISH TODO_verify_il | TODO_verify_ssa | TODO_verify_stmts | TODO_dump_func | TODO_remove_unused_locals | TODO_update_ssa | TODO_ggc_collect | TODO_verify_flow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) #include "gcc-generate-gimple-pass.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) const char * const plugin_name = plugin_info->base_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) const int argc = plugin_info->argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) const struct plugin_argument * const argv = plugin_info->argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) bool enable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) PASS_INFO(structleak, "early_optimizations", 1, PASS_POS_INSERT_BEFORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!plugin_default_version_check(version, &gcc_version)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) error(G_("incompatible gcc/plugin versions"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (strncmp(lang_hooks.name, "GNU C", 5) && !strncmp(lang_hooks.name, "GNU C+", 6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) inform(UNKNOWN_LOCATION, G_("%s supports C only, not %s"), plugin_name, lang_hooks.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) enable = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) for (i = 0; i < argc; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (!strcmp(argv[i].key, "disable")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) enable = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (!strcmp(argv[i].key, "verbose")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) verbose = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (!strcmp(argv[i].key, "byref")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) byref = BYREF_STRUCT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (!strcmp(argv[i].key, "byref-all")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) byref = BYREF_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) register_callback(plugin_name, PLUGIN_INFO, NULL, &structleak_plugin_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &structleak_pass_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) register_callback(plugin_name, PLUGIN_FINISH_TYPE, finish_type, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) register_callback(plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }