GCC Middle and Back End API Reference
ubsan.cc File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "rtl.h"
#include "c-family/c-common.h"
#include "gimple.h"
#include "cfghooks.h"
#include "tree-pass.h"
#include "memmodel.h"
#include "tm_p.h"
#include "ssa.h"
#include "cgraph.h"
#include "tree-pretty-print.h"
#include "stor-layout.h"
#include "cfganal.h"
#include "gimple-iterator.h"
#include "output.h"
#include "cfgloop.h"
#include "ubsan.h"
#include "expr.h"
#include "stringpool.h"
#include "attribs.h"
#include "asan.h"
#include "gimplify-me.h"
#include "dfp.h"
#include "builtins.h"
#include "tree-object-size.h"
#include "tree-cfg.h"
#include "gimple-fold.h"
#include "varasm.h"
#include "realmpfr.h"
#include "target.h"
#include "langhooks.h"
#include "gt-ubsan.h"
Include dependency graph for ubsan.cc:

Data Structures

struct  tree_type_map
 
struct  tree_type_map_cache_hasher
 

Macros

#define OBJSZ_MAX_OFFSET   (1024 * 16)
 

Functions

static tree decl_for_type_lookup (tree type)
 
static void decl_for_type_insert (tree type, tree decl)
 
tree ubsan_encode_value (tree t, enum ubsan_encode_value_phase phase)
 
static tree ubsan_get_type_descriptor_type (void)
 
tree ubsan_get_source_location_type (void)
 
static tree ubsan_source_location (location_t loc)
 
static unsigned short get_ubsan_type_info_for_type (tree type)
 
tree ubsan_type_descriptor (tree type, enum ubsan_print_style pstyle)
 
tree ubsan_create_data (const char *name, int loccnt, const location_t *ploc,...)
 
tree sanitize_unreachable_fn (tree *data, location_t loc)
 
bool ubsan_instrument_unreachable (gimple_stmt_iterator *gsi)
 
bool is_ubsan_builtin_p (tree t)
 
static void ubsan_create_edge (gimple *stmt)
 
bool ubsan_expand_bounds_ifn (gimple_stmt_iterator *gsi)
 
bool ubsan_expand_null_ifn (gimple_stmt_iterator *gsip)
 
bool ubsan_expand_objsize_ifn (gimple_stmt_iterator *gsi)
 
bool ubsan_expand_ptr_ifn (gimple_stmt_iterator *gsip)
 
bool ubsan_expand_vptr_ifn (gimple_stmt_iterator *gsip)
 
static void instrument_mem_ref (tree mem, tree base, gimple_stmt_iterator *iter, bool is_lhs)
 
static void instrument_null (gimple_stmt_iterator gsi, tree t, bool is_lhs)
 
static void instrument_pointer_overflow (gimple_stmt_iterator *gsi, tree ptr, tree off)
 
static void maybe_instrument_pointer_overflow (gimple_stmt_iterator *gsi, tree t)
 
tree ubsan_build_overflow_builtin (tree_code code, location_t loc, tree lhstype, tree op0, tree op1, tree *datap)
 
static void instrument_si_overflow (gimple_stmt_iterator gsi)
 
static void instrument_bool_enum_load (gimple_stmt_iterator *gsi)
 
static bool ubsan_use_new_style_p (location_t loc)
 
tree ubsan_instrument_float_cast (location_t loc, tree type, tree expr)
 
static void instrument_nonnull_arg (gimple_stmt_iterator *gsi)
 
static void instrument_nonnull_return (gimple_stmt_iterator *gsi)
 
static void instrument_object_size (gimple_stmt_iterator *gsi, tree t, bool is_lhs)
 
static void instrument_builtin (gimple_stmt_iterator *gsi)
 
gimple_opt_passmake_pass_ubsan (gcc::context *ctxt)
 

Variables

static hash_table< tree_type_map_cache_hasher > * decl_tree_for_type
 
static tree ubsan_type_descriptor_type
 
static tree ubsan_source_location_type
 
static unsigned int ubsan_ids [2]
 
static tree ubsan_vptr_type_cache_decl
 

Macro Definition Documentation

◆ OBJSZ_MAX_OFFSET

#define OBJSZ_MAX_OFFSET   (1024 * 16)

Function Documentation

◆ decl_for_type_insert()

static void decl_for_type_insert ( tree type,
tree decl )
static
Insert a mapping TYPE->DECL in the VAR_DECL for type hashtable.   

References tree_type_map::decl, decl_tree_for_type, ggc_alloc(), type(), and TYPE_UID.

Referenced by ubsan_type_descriptor().

◆ decl_for_type_lookup()

static tree decl_for_type_lookup ( tree type)
static
Lookup a VAR_DECL for TYPE, and return it if we find one.   

References hash_table< Descriptor, Lazy, Allocator >::create_ggc(), decl_tree_for_type, ggc_alloc(), NULL, NULL_TREE, type(), tree_type_map::type, and TYPE_UID.

Referenced by ubsan_type_descriptor().

◆ get_ubsan_type_info_for_type()

static unsigned short get_ubsan_type_info_for_type ( tree type)
static
This routine returns a magic number for TYPE.   

References exact_log2(), gcc_assert, INTEGRAL_TYPE_P, SCALAR_FLOAT_TYPE_P, tree_to_uhwi(), TYPE_SIZE, and TYPE_UNSIGNED.

Referenced by ubsan_type_descriptor().

◆ instrument_bool_enum_load()

◆ instrument_builtin()

◆ instrument_mem_ref()

◆ instrument_nonnull_arg()

◆ instrument_nonnull_return()

◆ instrument_null()

static void instrument_null ( gimple_stmt_iterator gsi,
tree t,
bool is_lhs )
static
Perform the pointer instrumentation.   

References get_base_address(), ggc_alloc(), instrument_mem_ref(), NULL_TREE, TREE_CODE, and TREE_OPERAND.

◆ instrument_object_size()

◆ instrument_pointer_overflow()

static void instrument_pointer_overflow ( gimple_stmt_iterator * gsi,
tree ptr,
tree off )
static

◆ instrument_si_overflow()

◆ is_ubsan_builtin_p()

bool is_ubsan_builtin_p ( tree t)
Return true if T is a call to a libubsan routine.   

References BUILT_IN_NORMAL, DECL_NAME, fndecl_built_in_p(), ggc_alloc(), IDENTIFIER_POINTER, and TREE_CODE.

◆ make_pass_ubsan()

gimple_opt_pass * make_pass_ubsan ( gcc::context * ctxt)

References ggc_alloc().

◆ maybe_instrument_pointer_overflow()

◆ sanitize_unreachable_fn()

tree sanitize_unreachable_fn ( tree * data,
location_t loc )

◆ ubsan_build_overflow_builtin()

tree ubsan_build_overflow_builtin ( tree_code code,
location_t loc,
tree lhstype,
tree op0,
tree op1,
tree * datap )
Build an ubsan builtin call for the signed-integer-overflow
sanitization.  CODE says what kind of builtin are we building,
LOC is a location, LHSTYPE is the type of LHS, OP0 and OP1
are operands of the binary operation.   

References build_call_expr_loc(), build_fold_addr_expr_loc(), builtin_decl_explicit(), gcc_unreachable, ggc_alloc(), NULL_TREE, SANITIZE_SI_OVERFLOW, ubsan_create_data(), ubsan_encode_value(), UBSAN_ENCODE_VALUE_RTL, and ubsan_type_descriptor().

Referenced by expand_addsub_overflow(), expand_mul_overflow(), and expand_neg_overflow().

◆ ubsan_create_data()

◆ ubsan_create_edge()

◆ ubsan_encode_value()

◆ ubsan_expand_bounds_ifn()

◆ ubsan_expand_null_ifn()

◆ ubsan_expand_objsize_ifn()

◆ ubsan_expand_ptr_ifn()

◆ ubsan_expand_vptr_ifn()

◆ ubsan_get_source_location_type()

◆ ubsan_get_type_descriptor_type()

◆ ubsan_instrument_float_cast()

◆ ubsan_instrument_unreachable()

bool ubsan_instrument_unreachable ( gimple_stmt_iterator * gsi)
Rewrite a gcall to __builtin_unreachable for -fsanitize=unreachable.  Called
by the sanopt pass.   

References g, ggc_alloc(), gimple_build_builtin_unreachable(), gimple_location(), gsi_replace(), and gsi_stmt().

◆ ubsan_source_location()

static tree ubsan_source_location ( location_t loc)
static
Helper routine that returns a CONSTRUCTOR of __ubsan_source_location
type with its fields filled from a location_t LOC.   

References build_array_type_nelts(), build_constructor_va(), build_fold_addr_expr, build_int_cst(), build_string(), char_type_node, expand_location(), ggc_alloc(), NULL, NULL_TREE, ptr_type_node, TREE_CONSTANT, TREE_READONLY, TREE_STATIC, TREE_TYPE, ubsan_get_source_location_type(), and unsigned_type_node.

Referenced by ubsan_create_data().

◆ ubsan_type_descriptor()

tree ubsan_type_descriptor ( tree type,
enum ubsan_print_style pstyle )
Helper routine that returns ADDR_EXPR of a VAR_DECL of a type
descriptor.  It first looks into the hash table; if not found,
create the VAR_DECL, put it into the hash table and return the
ADDR_EXPR of it.  TYPE describes a particular type.  PSTYLE is
an enum controlling how we want to print the type.   

References wi::add(), build_array_type_nelts(), build_constructor_va(), build_decl(), build_fold_addr_expr, build_int_cst(), build_nonstandard_integer_type(), build_qualified_type(), build_string(), char_type_node, DECL_ARTIFICIAL, DECL_EXTERNAL, decl_for_type_insert(), decl_for_type_lookup(), DECL_IGNORED_P, DECL_INITIAL, DECL_NAME, DECL_SIZE, DECL_SIZE_UNIT, double_type_node, varpool_node::finalize_decl(), float_type_node, gcc_assert, varpool_node::get(), get_identifier(), get_ubsan_type_info_for_type(), ggc_alloc(), IDENTIFIER_POINTER, long_double_type_node, MAX_FIXED_MODE_SIZE, NULL, NULL_TREE, POINTER_TYPE_P, pp_formatted_text(), pp_left_bracket, pp_printf(), pp_quote, pp_right_bracket, pp_space, pp_star, pp_string(), pp_unsigned_wide_integer, pp_wide_int(), short_unsigned_type_node, size_binop, strip_array_types(), wi::to_widest(), TREE_CODE, TREE_CONSTANT, tree_fits_uhwi_p(), TREE_PUBLIC, TREE_READONLY, TREE_STATIC, tree_to_uhwi(), TREE_TYPE, type(), TYPE_ATOMIC, TYPE_DOMAIN, TYPE_MAIN_VARIANT, TYPE_MAX_VALUE, TYPE_METHOD_BASETYPE, TYPE_MODE, TYPE_NAME, TYPE_PRECISION, TYPE_QUAL_CONST, TYPE_READONLY, TYPE_RESTRICT, TYPE_SIGN, TYPE_SIZE, TYPE_SIZE_UNIT, TYPE_UNSIGNED, TYPE_VOLATILE, ubsan_get_type_descriptor_type(), ubsan_ids, UBSAN_PRINT_ARRAY, UBSAN_PRINT_FORCE_INT, UBSAN_PRINT_NORMAL, UBSAN_PRINT_POINTER, and UNKNOWN_LOCATION.

Referenced by instrument_bool_enum_load(), ubsan_build_overflow_builtin(), ubsan_expand_bounds_ifn(), ubsan_expand_null_ifn(), ubsan_expand_objsize_ifn(), ubsan_expand_vptr_ifn(), and ubsan_instrument_float_cast().

◆ ubsan_use_new_style_p()

static bool ubsan_use_new_style_p ( location_t loc)
static
Determine if we can propagate given LOCATION to ubsan_data descriptor to use
new style handlers.  Libubsan uses heuristics to destinguish between old and
new styles and relies on these properties for filename:

a) Location's filename must not be NULL.
b) Location's filename must not be equal to "".
c) Location's filename must not be equal to "\1".
d) First two bytes of filename must not contain '\xff' symbol.   

References expand_location(), ggc_alloc(), NULL, startswith(), and UNKNOWN_LOCATION.

Referenced by ubsan_instrument_float_cast().

Variable Documentation

◆ decl_tree_for_type

◆ ubsan_ids

unsigned int ubsan_ids[2]
static
Counters for internal labels.  ubsan_ids[0] for Lubsan_type,
ubsan_ids[1] for Lubsan_data labels.   

Referenced by ubsan_create_data(), and ubsan_type_descriptor().

◆ ubsan_source_location_type

tree ubsan_source_location_type
static
Cached ubsan_get_source_location_type () return value.   

Referenced by ubsan_get_source_location_type().

◆ ubsan_type_descriptor_type

tree ubsan_type_descriptor_type
static
Cached ubsan_get_type_descriptor_type () return value.   

Referenced by ubsan_get_type_descriptor_type().

◆ ubsan_vptr_type_cache_decl

tree ubsan_vptr_type_cache_decl
static
Cached __ubsan_vptr_type_cache decl.   

Referenced by ubsan_expand_vptr_ifn().