aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIndu Bhagat <indu.bhagat@oracle.com>2025-07-09 14:33:17 +0300
committerClaudiu Zissulescu <claudiu.zissulescu-ianculescu@oracle.com>2025-08-18 18:09:07 +0300
commit0e09be99c01cc1517875be58a78e76cba7b58757 (patch)
tree83e5c9a33cb59422f8d8303144a7dcbbed6df9d2
parentf88e1fa41e76f774b29537e2f54c1fa1074063bd (diff)
downloadgcc-0e09be99c01cc1517875be58a78e76cba7b58757.zip
gcc-0e09be99c01cc1517875be58a78e76cba7b58757.tar.gz
gcc-0e09be99c01cc1517875be58a78e76cba7b58757.tar.bz2
opts: use sanitize_code_type for sanitizer flags
Currently, the data type of sanitizer flags is unsigned int, with SANITIZE_SHADOW_CALL_STACK (1UL << 31) being highest individual enumerator for enum sanitize_code. Use 'sanitize_code_type' data type to allow for more distinct instrumentation modes be added when needed. gcc/ChangeLog: * flag-types.h (sanitize_code_type): Define. * asan.h (sanitize_flags_p): Use 'sanitize_code_type' instead of 'unsigned int'. * common.opt: Likewise. * dwarf2asm.cc (dw2_output_indirect_constant_1): Likewise. * opts.cc (find_sanitizer_argument): Likewise. (report_conflicting_sanitizer_options): Likewise. (parse_sanitizer_options): Likewise. (parse_no_sanitize_attribute): Likewise. * opts.h (parse_sanitizer_options): Likewise. (parse_no_sanitize_attribute): Likewise. * tree-cfg.cc (print_no_sanitize_attr_value): Likewise. * tree.cc (tree_fits_sanitize_code_type_p): Define. (tree_to_sanitize_code_type): Likewise. * tree.h (tree_fits_sanitize_code_type_p): Declare. (tree_to_sanitize_code_type): Likewise. gcc/c-family/ChangeLog: * c-attribs.cc (add_no_sanitize_value): Use 'sanitize_code_type' instead of 'unsigned int'. (handle_no_sanitize_attribute): Likewise. (handle_no_sanitize_address_attribute): Likewise. (handle_no_sanitize_thread_attribute): Likewise. (handle_no_address_safety_analysis_attribute): Likewise. * c-common.h (add_no_sanitize_value): Likewise. gcc/c/ChangeLog: * c-parser.cc (c_parser_declaration_or_fndef): Use 'sanitize_code_type' instead of 'unsigned int'. gcc/cp/ChangeLog: * typeck.cc (get_member_function_from_ptrfunc): Use 'sanitize_code_type' instead of 'unsigned int'. gcc/d/ChangeLog: * d-attribs.cc (d_handle_no_sanitize_attribute): Use 'sanitize_code_type' instead of 'unsigned int'. Signed-off-by: Claudiu Zissulescu <claudiu.zissulescu-ianculescu@oracle.com>
-rw-r--r--gcc/asan.h5
-rw-r--r--gcc/c-family/c-attribs.cc17
-rw-r--r--gcc/c-family/c-common.h2
-rw-r--r--gcc/c/c-parser.cc4
-rw-r--r--gcc/common.opt6
-rw-r--r--gcc/cp/typeck.cc2
-rw-r--r--gcc/d/d-attribs.cc9
-rw-r--r--gcc/dwarf2asm.cc2
-rw-r--r--gcc/flag-types.h3
-rw-r--r--gcc/opts.cc26
-rw-r--r--gcc/opts.h9
-rw-r--r--gcc/tree-cfg.cc2
-rw-r--r--gcc/tree.cc23
-rw-r--r--gcc/tree.h4
14 files changed, 75 insertions, 39 deletions
diff --git a/gcc/asan.h b/gcc/asan.h
index 273d674..0a3f5e9 100644
--- a/gcc/asan.h
+++ b/gcc/asan.h
@@ -242,9 +242,10 @@ asan_protect_stack_decl (tree decl)
remove all flags mentioned in "no_sanitize" of DECL_ATTRIBUTES. */
inline bool
-sanitize_flags_p (unsigned int flag, const_tree fn = current_function_decl)
+sanitize_flags_p (sanitize_code_type flag,
+ const_tree fn = current_function_decl)
{
- unsigned int result_flags = flag_sanitize & flag;
+ sanitize_code_type result_flags = flag_sanitize & flag;
if (result_flags == 0)
return false;
diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index decc4d0..1e3a94e 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -1420,23 +1420,24 @@ handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
/* Add FLAGS for a function NODE to no_sanitize_flags in DECL_ATTRIBUTES. */
void
-add_no_sanitize_value (tree node, unsigned int flags)
+add_no_sanitize_value (tree node, sanitize_code_type flags)
{
tree attr = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (node));
if (attr)
{
- unsigned int old_value = tree_to_uhwi (TREE_VALUE (attr));
+ sanitize_code_type old_value =
+ tree_to_sanitize_code_type (TREE_VALUE (attr));
flags |= old_value;
if (flags == old_value)
return;
- TREE_VALUE (attr) = build_int_cst (unsigned_type_node, flags);
+ TREE_VALUE (attr) = build_int_cst (uint64_type_node, flags);
}
else
DECL_ATTRIBUTES (node)
= tree_cons (get_identifier ("no_sanitize"),
- build_int_cst (unsigned_type_node, flags),
+ build_int_cst (uint64_type_node, flags),
DECL_ATTRIBUTES (node));
}
@@ -1447,7 +1448,7 @@ static tree
handle_no_sanitize_attribute (tree *node, tree name, tree args, int,
bool *no_add_attrs)
{
- unsigned int flags = 0;
+ sanitize_code_type flags = 0;
*no_add_attrs = true;
if (TREE_CODE (*node) != FUNCTION_DECL)
{
@@ -1484,7 +1485,7 @@ handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
if (TREE_CODE (*node) != FUNCTION_DECL)
warning (OPT_Wattributes, "%qE attribute ignored", name);
else
- add_no_sanitize_value (*node, SANITIZE_ADDRESS);
+ add_no_sanitize_value (*node, (sanitize_code_type) SANITIZE_ADDRESS);
return NULL_TREE;
}
@@ -1500,7 +1501,7 @@ handle_no_sanitize_thread_attribute (tree *node, tree name, tree, int,
if (TREE_CODE (*node) != FUNCTION_DECL)
warning (OPT_Wattributes, "%qE attribute ignored", name);
else
- add_no_sanitize_value (*node, SANITIZE_THREAD);
+ add_no_sanitize_value (*node, (sanitize_code_type) SANITIZE_THREAD);
return NULL_TREE;
}
@@ -1517,7 +1518,7 @@ handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
if (TREE_CODE (*node) != FUNCTION_DECL)
warning (OPT_Wattributes, "%qE attribute ignored", name);
else
- add_no_sanitize_value (*node, SANITIZE_ADDRESS);
+ add_no_sanitize_value (*node, (sanitize_code_type) SANITIZE_ADDRESS);
return NULL_TREE;
}
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index 009c8ef..b6021d2 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -1713,7 +1713,7 @@ extern enum flt_eval_method
excess_precision_mode_join (enum flt_eval_method, enum flt_eval_method);
extern int c_flt_eval_method (bool ts18661_p);
-extern void add_no_sanitize_value (tree node, unsigned int flags);
+extern void add_no_sanitize_value (tree node, sanitize_code_type flags);
extern void maybe_add_include_fixit (rich_location *, const char *, bool);
extern void maybe_suggest_missing_token_insertion (rich_location *richloc,
diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index 510d727..e8b6494 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -2822,7 +2822,7 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
specs->constexpr_p, &richloc);
/* A parameter is initialized, which is invalid. Don't
attempt to instrument the initializer. */
- int flag_sanitize_save = flag_sanitize;
+ sanitize_code_type flag_sanitize_save = flag_sanitize;
if (nested && !empty_ok)
flag_sanitize = 0;
init = c_parser_expr_no_commas (parser, NULL);
@@ -2911,7 +2911,7 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
specs->constexpr_p, &richloc);
/* A parameter is initialized, which is invalid. Don't
attempt to instrument the initializer. */
- int flag_sanitize_save = flag_sanitize;
+ sanitize_code_type flag_sanitize_save = flag_sanitize;
if (TREE_CODE (d) == PARM_DECL)
flag_sanitize = 0;
init = c_parser_initializer (parser, d);
diff --git a/gcc/common.opt b/gcc/common.opt
index c58ac13..be53e6d 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -205,15 +205,15 @@ bool flag_opts_finished
; What the sanitizer should instrument
Variable
-unsigned int flag_sanitize
+sanitize_code_type flag_sanitize
; What sanitizers should recover from errors
Variable
-unsigned int flag_sanitize_recover = (SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT | SANITIZE_KERNEL_ADDRESS | SANITIZE_KERNEL_HWADDRESS) & ~(SANITIZE_UNREACHABLE | SANITIZE_RETURN)
+sanitize_code_type flag_sanitize_recover = (SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT | SANITIZE_KERNEL_ADDRESS | SANITIZE_KERNEL_HWADDRESS) & ~(SANITIZE_UNREACHABLE | SANITIZE_RETURN)
; What sanitizers should use __builtin_trap () instead of runtime diagnostics
Variable
-unsigned int flag_sanitize_trap
+sanitize_code_type flag_sanitize_trap
; Flag whether a prefix has been added to dump_base_name
Variable
diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index f592894..41a3f4c 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -4287,7 +4287,7 @@ get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
idx = build1 (NOP_EXPR, vtable_index_type, e3);
switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
{
- int flag_sanitize_save;
+ sanitize_code_type flag_sanitize_save;
case ptrmemfunc_vbit_in_pfn:
e1 = cp_build_binary_op (input_location,
BIT_AND_EXPR, idx, integer_one_node,
diff --git a/gcc/d/d-attribs.cc b/gcc/d/d-attribs.cc
index 77315dc..53aea5e 100644
--- a/gcc/d/d-attribs.cc
+++ b/gcc/d/d-attribs.cc
@@ -1406,7 +1406,7 @@ d_handle_no_sanitize_attribute (tree *node, tree name, tree args, int,
return NULL_TREE;
}
- unsigned int flags = 0;
+ sanitize_code_type flags = 0;
for (; args; args = TREE_CHAIN (args))
{
tree id = TREE_VALUE (args);
@@ -1424,16 +1424,17 @@ d_handle_no_sanitize_attribute (tree *node, tree name, tree args, int,
merge existing flags if no_sanitize was previously handled. */
if (tree attr = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (*node)))
{
- unsigned int old_value = tree_to_uhwi (TREE_VALUE (attr));
+ sanitize_code_type old_value =
+ tree_to_sanitize_code_type (TREE_VALUE (attr));
flags |= old_value;
if (flags != old_value)
- TREE_VALUE (attr) = build_int_cst (d_uint_type, flags);
+ TREE_VALUE (attr) = build_int_cst (d_ulong_type, flags);
}
else
{
DECL_ATTRIBUTES (*node) = tree_cons (get_identifier ("no_sanitize"),
- build_int_cst (d_uint_type, flags),
+ build_int_cst (d_ulong_type, flags),
DECL_ATTRIBUTES (*node));
}
diff --git a/gcc/dwarf2asm.cc b/gcc/dwarf2asm.cc
index ec5c684..bf7fef3 100644
--- a/gcc/dwarf2asm.cc
+++ b/gcc/dwarf2asm.cc
@@ -1041,7 +1041,7 @@ dw2_output_indirect_constant_1 (const char *sym, tree id)
sym_ref = gen_rtx_SYMBOL_REF (Pmode, sym);
/* Disable ASan for decl because redzones cause ABI breakage between GCC and
libstdc++ for `.LDFCM*' variables. See PR 78651 for details. */
- unsigned int save_flag_sanitize = flag_sanitize;
+ sanitize_code_type save_flag_sanitize = flag_sanitize;
flag_sanitize &= ~(SANITIZE_ADDRESS | SANITIZE_USER_ADDRESS
| SANITIZE_KERNEL_ADDRESS);
/* And also temporarily disable -fsection-anchors. These indirect constants
diff --git a/gcc/flag-types.h b/gcc/flag-types.h
index 9a3cc4a..bf681c3 100644
--- a/gcc/flag-types.h
+++ b/gcc/flag-types.h
@@ -350,6 +350,9 @@ enum sanitize_code {
| SANITIZE_BOUNDS_STRICT
};
+/* Sanitizer flag type. */
+typedef uint64_t sanitize_code_type;
+
/* Different settings for zeroing subset of registers. */
namespace zero_regs_flags {
const unsigned int UNSET = 0;
diff --git a/gcc/opts.cc b/gcc/opts.cc
index a02d017..3ab993a 100644
--- a/gcc/opts.cc
+++ b/gcc/opts.cc
@@ -993,7 +993,8 @@ vec<const char *> help_option_arguments;
/* Return the string name describing a sanitizer argument which has been
provided on the command line and has set this particular flag. */
const char *
-find_sanitizer_argument (struct gcc_options *opts, unsigned int flags)
+find_sanitizer_argument (struct gcc_options *opts,
+ sanitize_code_type flags)
{
for (int i = 0; sanitizer_opts[i].name != NULL; ++i)
{
@@ -1027,10 +1028,11 @@ find_sanitizer_argument (struct gcc_options *opts, unsigned int flags)
set these flags. */
static void
report_conflicting_sanitizer_options (struct gcc_options *opts, location_t loc,
- unsigned int left, unsigned int right)
+ sanitize_code_type left,
+ sanitize_code_type right)
{
- unsigned int left_seen = (opts->x_flag_sanitize & left);
- unsigned int right_seen = (opts->x_flag_sanitize & right);
+ sanitize_code_type left_seen = (opts->x_flag_sanitize & left);
+ sanitize_code_type right_seen = (opts->x_flag_sanitize & right);
if (left_seen && right_seen)
{
const char* left_arg = find_sanitizer_argument (opts, left_seen);
@@ -2168,9 +2170,9 @@ const struct sanitizer_opts_s sanitizer_opts[] =
SANITIZER_OPT (pointer-overflow, SANITIZE_POINTER_OVERFLOW, true, true),
SANITIZER_OPT (builtin, SANITIZE_BUILTIN, true, true),
SANITIZER_OPT (shadow-call-stack, SANITIZE_SHADOW_CALL_STACK, false, false),
- SANITIZER_OPT (all, ~0U, true, true),
+ SANITIZER_OPT (all, ~sanitize_code_type (0), true, true),
#undef SANITIZER_OPT
- { NULL, 0U, 0UL, false, false }
+ { NULL, sanitize_code_type (0), 0UL, false, false }
};
/* -fzero-call-used-regs= suboptions. */
@@ -2241,7 +2243,7 @@ get_closest_sanitizer_option (const string_fragment &arg,
{
/* -fsanitize=all is not valid, so don't offer it. */
if (code == OPT_fsanitize_
- && opts[i].flag == ~0U
+ && opts[i].flag == ~sanitize_code_type (0)
&& value)
continue;
@@ -2268,9 +2270,9 @@ get_closest_sanitizer_option (const string_fragment &arg,
adjust previous FLAGS and return new ones. If COMPLAIN is false,
don't issue diagnostics. */
-unsigned int
+sanitize_code_type
parse_sanitizer_options (const char *p, location_t loc, int scode,
- unsigned int flags, int value, bool complain)
+ sanitize_code_type flags, int value, bool complain)
{
enum opt_code code = (enum opt_code) scode;
@@ -2296,7 +2298,7 @@ parse_sanitizer_options (const char *p, location_t loc, int scode,
&& memcmp (p, sanitizer_opts[i].name, len) == 0)
{
/* Handle both -fsanitize and -fno-sanitize cases. */
- if (value && sanitizer_opts[i].flag == ~0U)
+ if (value && sanitizer_opts[i].flag == ~sanitize_code_type (0))
{
if (code == OPT_fsanitize_)
{
@@ -2377,10 +2379,10 @@ parse_sanitizer_options (const char *p, location_t loc, int scode,
/* Parse string values of no_sanitize attribute passed in VALUE.
Values are separated with comma. */
-unsigned int
+sanitize_code_type
parse_no_sanitize_attribute (char *value)
{
- unsigned int flags = 0;
+ sanitize_code_type flags = 0;
unsigned int i;
char *q = strtok (value, ",");
diff --git a/gcc/opts.h b/gcc/opts.h
index ea92c49..65f249c 100644
--- a/gcc/opts.h
+++ b/gcc/opts.h
@@ -22,6 +22,7 @@ along with GCC; see the file COPYING3. If not see
#include "rich-location.h"
#include "obstack.h"
+#include "flag-types.h"
/* Specifies how a switch's VAR_VALUE relates to its FLAG_VAR. */
enum cl_var_type {
@@ -432,10 +433,10 @@ extern char *write_langs (unsigned int mask);
extern void print_ignored_options (void);
extern void handle_common_deferred_options (void);
extern void handle_deferred_dump_options (void);
-unsigned int parse_sanitizer_options (const char *, location_t, int,
- unsigned int, int, bool);
+sanitize_code_type parse_sanitizer_options (const char *, location_t, int,
+ sanitize_code_type, int, bool);
-unsigned int parse_no_sanitize_attribute (char *value);
+sanitize_code_type parse_no_sanitize_attribute (char *value);
extern bool common_handle_option (struct gcc_options *opts,
struct gcc_options *opts_set,
const struct cl_decoded_option *decoded,
@@ -477,7 +478,7 @@ extern bool opt_enum_arg_to_value (size_t opt_index, const char *arg,
extern const struct sanitizer_opts_s
{
const char *const name;
- unsigned int flag;
+ sanitize_code_type flag;
size_t len;
bool can_recover;
bool can_trap;
diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc
index 079bfc9..4c88563 100644
--- a/gcc/tree-cfg.cc
+++ b/gcc/tree-cfg.cc
@@ -8180,7 +8180,7 @@ dump_default_def (FILE *file, tree def, int spc, dump_flags_t flags)
static void
print_no_sanitize_attr_value (FILE *file, tree value)
{
- unsigned int flags = tree_to_uhwi (value);
+ sanitize_code_type flags = tree_to_sanitize_code_type (value);
bool first = true;
for (int i = 0; sanitizer_opts[i].name != NULL; ++i)
{
diff --git a/gcc/tree.cc b/gcc/tree.cc
index dcc1abd..905c2d6 100644
--- a/gcc/tree.cc
+++ b/gcc/tree.cc
@@ -6639,6 +6639,18 @@ tree_fits_poly_uint64_p (const_tree t)
&& wi::fits_uhwi_p (wi::to_widest (t)));
}
+/* Return true if T is an INTEGER_CST whose numerical value (extended according
+ to TYPE_UNSIGNED) fits in a sanitize_code_type (uint64_t). */
+
+bool
+tree_fits_sanitize_code_type_p (const_tree t)
+{
+ if (t == NULL_TREE)
+ return false;
+ return (TREE_CODE (t) == INTEGER_CST
+ && wi::fits_uhwi_p (wi::to_widest (t)));
+}
+
/* T is an INTEGER_CST whose numerical value (extended according to
TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
HOST_WIDE_INT. */
@@ -6661,6 +6673,17 @@ tree_to_uhwi (const_tree t)
return TREE_INT_CST_LOW (t);
}
+/* T is an INTEGER_CST whose numerical value (extended according to
+ TYPE_UNSIGNED) fits in a sanitize_code_type. Return that
+ sanitize_code_type. */
+
+sanitize_code_type
+tree_to_sanitize_code_type (const_tree t)
+{
+ gcc_assert (tree_fits_sanitize_code_type_p (t));
+ return TREE_INT_CST_LOW (t);
+}
+
/* Return the most significant (sign) bit of T. */
int
diff --git a/gcc/tree.h b/gcc/tree.h
index f1aacb31b..ce8c778 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -5040,6 +5040,8 @@ extern bool tree_fits_shwi_p (const_tree) ATTRIBUTE_PURE;
extern bool tree_fits_poly_int64_p (const_tree) ATTRIBUTE_PURE;
extern bool tree_fits_uhwi_p (const_tree) ATTRIBUTE_PURE;
extern bool tree_fits_poly_uint64_p (const_tree) ATTRIBUTE_PURE;
+extern bool tree_fits_sanitize_code_type_p (const_tree) ATTRIBUTE_PURE;
+
extern HOST_WIDE_INT tree_to_shwi (const_tree)
ATTRIBUTE_NONNULL (1) ATTRIBUTE_PURE;
@@ -5049,6 +5051,8 @@ extern unsigned HOST_WIDE_INT tree_to_uhwi (const_tree)
ATTRIBUTE_NONNULL (1) ATTRIBUTE_PURE;
extern poly_uint64 tree_to_poly_uint64 (const_tree)
ATTRIBUTE_NONNULL (1) ATTRIBUTE_PURE;
+extern sanitize_code_type tree_to_sanitize_code_type (const_tree)
+ ATTRIBUTE_NONNULL (1) ATTRIBUTE_PURE;
#if !defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 4003)
extern inline __attribute__ ((__gnu_inline__)) HOST_WIDE_INT
tree_to_shwi (const_tree t)