Skip to content

Commit

Permalink
bootstrap-ubsan.mk (POSTSTAGE1_LDFLAGS): Add -ldl.
Browse files Browse the repository at this point in the history
config/
	* bootstrap-ubsan.mk (POSTSTAGE1_LDFLAGS): Add -ldl.
gcc/c-family/
	* c-ubsan.c (ubsan_instrument_division): Adjust ubsan_create_data
	call.
	(ubsan_instrument_shift): Likewise.
	(ubsan_instrument_vla): Likewise.
gcc/
	* opts.c (common_handle_option): Add -fsanitize=null option.
	Turn off -fdelete-null-pointer-checks option when doing the
	NULL pointer checking.
	* sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH): Add.
	* tree-pass.h (make_pass_ubsan): Declare.
	(make_pass_sanopt): Declare.
	* timevar.def (TV_TREE_UBSAN): New timevar.
	* passes.def: Add pass_sanopt and pass_ubsan.
	* ubsan.h (ubsan_null_ckind): New enum.
	(ubsan_mismatch_data): New struct.
	(ubsan_expand_null_ifn): Declare.
	(ubsan_create_data): Adjust declaration.
	(ubsan_type_descriptor): Likewise.
	* asan.c: Include "ubsan.h".
	(pass_data_sanopt): New pass.
	(execute_sanopt): New function.
	(gate_sanopt): Likewise.
	(make_pass_sanopt): Likewise.
	(class pass_sanopt): New class.
	* ubsan.c: Include tree-pass.h, gimple-ssa.h, gimple-walk.h,
	gimple-iterator.h and cfgloop.h. 
	(PROB_VERY_UNLIKELY): Define.
	(tree_type_map_hash): New function.
	(ubsan_type_descriptor): Add new parameter.
	Improve type name generation.
	(ubsan_create_data): Add new parameter.  Add pointer data into
	ubsan structure.
	(ubsan_expand_null_ifn): New function.
	(instrument_member_call): Likewise.
	(instrument_mem_ref): Likewise.
	(instrument_null): Likewise.
	(ubsan_pass): Likewise.
	(gate_ubsan): Likewise.
	(make_pass_ubsan): Likewise.
	(ubsan_instrument_unreachable): Adjust ubsan_create_data call.
	(class pass_ubsan): New class.
	(pass_data_ubsan): New pass.
	* flag-types.h (enum sanitize_code): Add SANITIZE_NULL.
	* internal-fn.c (expand_UBSAN_NULL): New function.
	* cgraphunit.c (varpool_finalize_decl): Call varpool_assemble_decl
	even when !flag_toplevel_reorder.
	* internal-fn.def (UBSAN_NULL): New.
gcc/testsuite/
	* c-c++-common/ubsan/null-1.c: New test.
	* c-c++-common/ubsan/null-2.c: New test.
	* c-c++-common/ubsan/null-3.c: New test.
	* c-c++-common/ubsan/null-4.c: New test.
	* c-c++-common/ubsan/null-5.c: New test.
	* c-c++-common/ubsan/null-6.c: New test.
	* c-c++-common/ubsan/null-7.c: New test.
	* c-c++-common/ubsan/null-8.c: New test.
	* c-c++-common/ubsan/null-9.c: New test.
	* c-c++-common/ubsan/null-10.c: New test.
	* c-c++-common/ubsan/null-11.c: New test.
	* gcc.dg/ubsan/c99-shift-2.c: Adjust dg-output.
	* c-c++-common/ubsan/shift-1.c: Likewise.
	* c-c++-common/ubsan/div-by-zero-3.c: Likewise.

From-SVN: r205021
  • Loading branch information
mpolacek authored and Marek Polacek committed Nov 19, 2013
1 parent a186c90 commit b9a55b1
Show file tree
Hide file tree
Showing 32 changed files with 707 additions and 50 deletions.
4 changes: 4 additions & 0 deletions config/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2013-11-19 Marek Polacek <[email protected]>

* bootstrap-ubsan.mk (POSTSTAGE1_LDFLAGS): Add -ldl.

2013-11-15 Andreas Schwab <[email protected]>

* picflag.m4 (m68k-*-*): Use default PIC flag.
Expand Down
2 changes: 1 addition & 1 deletion config/bootstrap-ubsan.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

STAGE2_CFLAGS += -fsanitize=undefined
STAGE3_CFLAGS += -fsanitize=undefined
POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread \
POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread -ldl \
-B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/ \
-B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/.libs
45 changes: 45 additions & 0 deletions gcc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
2013-11-19 Marek Polacek <[email protected]>

* opts.c (common_handle_option): Add -fsanitize=null option.
Turn off -fdelete-null-pointer-checks option when doing the
NULL pointer checking.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH): Add.
* tree-pass.h (make_pass_ubsan): Declare.
(make_pass_sanopt): Declare.
* timevar.def (TV_TREE_UBSAN): New timevar.
* passes.def: Add pass_sanopt and pass_ubsan.
* ubsan.h (ubsan_null_ckind): New enum.
(ubsan_mismatch_data): New struct.
(ubsan_expand_null_ifn): Declare.
(ubsan_create_data): Adjust declaration.
(ubsan_type_descriptor): Likewise.
* asan.c: Include "ubsan.h".
(pass_data_sanopt): New pass.
(execute_sanopt): New function.
(gate_sanopt): Likewise.
(make_pass_sanopt): Likewise.
(class pass_sanopt): New class.
* ubsan.c: Include tree-pass.h, gimple-ssa.h, gimple-walk.h,
gimple-iterator.h and cfgloop.h.
(PROB_VERY_UNLIKELY): Define.
(tree_type_map_hash): New function.
(ubsan_type_descriptor): Add new parameter.
Improve type name generation.
(ubsan_create_data): Add new parameter. Add pointer data into
ubsan structure.
(ubsan_expand_null_ifn): New function.
(instrument_member_call): Likewise.
(instrument_mem_ref): Likewise.
(instrument_null): Likewise.
(ubsan_pass): Likewise.
(gate_ubsan): Likewise.
(make_pass_ubsan): Likewise.
(ubsan_instrument_unreachable): Adjust ubsan_create_data call.
(class pass_ubsan): New class.
(pass_data_ubsan): New pass.
* flag-types.h (enum sanitize_code): Add SANITIZE_NULL.
* internal-fn.c (expand_UBSAN_NULL): New function.
* cgraphunit.c (varpool_finalize_decl): Call varpool_assemble_decl
even when !flag_toplevel_reorder.
* internal-fn.def (UBSAN_NULL): New.

2013-11-19 Jan Hubicka <[email protected]>

* cgraph.c (cgraph_create_indirect_edge): Use get_polymorphic_call_info.
Expand Down
84 changes: 84 additions & 0 deletions gcc/asan.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ along with GCC; see the file COPYING3. If not see
#include "alloc-pool.h"
#include "cfgloop.h"
#include "gimple-builder.h"
#include "ubsan.h"

/* AddressSanitizer finds out-of-bounds and use-after-free bugs
with <2x slowdown on average.
Expand Down Expand Up @@ -2370,4 +2371,87 @@ make_pass_asan_O0 (gcc::context *ctxt)
return new pass_asan_O0 (ctxt);
}

/* Perform optimization of sanitize functions. */

static unsigned int
execute_sanopt (void)
{
basic_block bb;

FOR_EACH_BB (bb)
{
gimple_stmt_iterator gsi;
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple stmt = gsi_stmt (gsi);

if (!is_gimple_call (stmt))
continue;

if (gimple_call_internal_p (stmt))
switch (gimple_call_internal_fn (stmt))
{
case IFN_UBSAN_NULL:
ubsan_expand_null_ifn (gsi);
break;
default:
break;
}

if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "Optimized\n ");
print_gimple_stmt (dump_file, stmt, 0, dump_flags);
fprintf (dump_file, "\n");
}
}
}
return 0;
}

static bool
gate_sanopt (void)
{
return flag_sanitize;
}

namespace {

const pass_data pass_data_sanopt =
{
GIMPLE_PASS, /* type */
"sanopt", /* name */
OPTGROUP_NONE, /* optinfo_flags */
true, /* has_gate */
true, /* has_execute */
TV_NONE, /* tv_id */
( PROP_ssa | PROP_cfg | PROP_gimple_leh ), /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
( TODO_verify_flow | TODO_verify_stmts
| TODO_update_ssa ), /* todo_flags_finish */
};

class pass_sanopt : public gimple_opt_pass
{
public:
pass_sanopt (gcc::context *ctxt)
: gimple_opt_pass (pass_data_sanopt, ctxt)
{}

/* opt_pass methods: */
bool gate () { return gate_sanopt (); }
unsigned int execute () { return execute_sanopt (); }

}; // class pass_sanopt

} // anon namespace

gimple_opt_pass *
make_pass_sanopt (gcc::context *ctxt)
{
return new pass_sanopt (ctxt);
}

#include "gt-asan.h"
7 changes: 7 additions & 0 deletions gcc/c-family/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2013-11-19 Marek Polacek <[email protected]>

* c-ubsan.c (ubsan_instrument_division): Adjust ubsan_create_data
call.
(ubsan_instrument_shift): Likewise.
(ubsan_instrument_vla): Likewise.

2013-11-18 Richard Sandiford <[email protected]>

* c-common.c (convert_vector_to_pointer_for_subscript): Remove
Expand Down
13 changes: 9 additions & 4 deletions gcc/c-family/c-ubsan.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ ubsan_instrument_division (location_t loc, tree op0, tree op1)
make sure it gets evaluated before the condition. */
t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), op0, t);
tree data = ubsan_create_data ("__ubsan_overflow_data",
loc, ubsan_type_descriptor (type),
loc, NULL,
ubsan_type_descriptor (type, false),
NULL_TREE);
data = build_fold_addr_expr_loc (loc, data);
tt = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW);
Expand Down Expand Up @@ -141,8 +142,10 @@ ubsan_instrument_shift (location_t loc, enum tree_code code,
make sure it gets evaluated before the condition. */
t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), op0, t);
tree data = ubsan_create_data ("__ubsan_shift_data",
loc, ubsan_type_descriptor (type0),
ubsan_type_descriptor (type1), NULL_TREE);
loc, NULL,
ubsan_type_descriptor (type0, false),
ubsan_type_descriptor (type1, false),
NULL_TREE);

data = build_fold_addr_expr_loc (loc, data);

Expand All @@ -166,7 +169,9 @@ ubsan_instrument_vla (location_t loc, tree size)

t = fold_build2 (LE_EXPR, boolean_type_node, size, build_int_cst (type, 0));
tree data = ubsan_create_data ("__ubsan_vla_data",
loc, ubsan_type_descriptor (type), NULL_TREE);
loc, NULL,
ubsan_type_descriptor (type, false),
NULL_TREE);
data = build_fold_addr_expr_loc (loc, data);
tt = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE);
tt = build_call_expr_loc (loc, tt, 2, data, ubsan_encode_value (size));
Expand Down
3 changes: 2 additions & 1 deletion gcc/cgraphunit.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,8 @@ varpool_finalize_decl (tree decl)
varpool_analyze_node (node);
/* Some frontends produce various interface variables after compilation
finished. */
if (cgraph_state == CGRAPH_STATE_FINISHED)
if (cgraph_state == CGRAPH_STATE_FINISHED
|| (!flag_toplevel_reorder && cgraph_state == CGRAPH_STATE_EXPANSION))
varpool_assemble_decl (node);
}

Expand Down
3 changes: 2 additions & 1 deletion gcc/flag-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ enum sanitize_code {
SANITIZE_DIVIDE = 1 << 3,
SANITIZE_UNREACHABLE = 1 << 4,
SANITIZE_VLA = 1 << 5,
SANITIZE_NULL = 1 << 6,
SANITIZE_UNDEFINED = SANITIZE_SHIFT | SANITIZE_DIVIDE | SANITIZE_UNREACHABLE
| SANITIZE_VLA
| SANITIZE_VLA | SANITIZE_NULL
};

/* flag_vtable_verify initialization levels. */
Expand Down
8 changes: 8 additions & 0 deletions gcc/internal-fn.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ expand_GOMP_SIMD_LAST_LANE (gimple stmt ATTRIBUTE_UNUSED)
gcc_unreachable ();
}

/* This should get expanded in the sanopt pass. */

static void
expand_UBSAN_NULL (gimple stmt ATTRIBUTE_UNUSED)
{
gcc_unreachable ();
}

/* Routines to expand each internal function, indexed by function number.
Each routine has the prototype:
Expand Down
1 change: 1 addition & 0 deletions gcc/internal-fn.def
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ DEF_INTERNAL_FN (GOMP_SIMD_LANE, ECF_NOVOPS | ECF_LEAF | ECF_NOTHROW)
DEF_INTERNAL_FN (GOMP_SIMD_VF, ECF_CONST | ECF_LEAF | ECF_NOTHROW)
DEF_INTERNAL_FN (GOMP_SIMD_LAST_LANE, ECF_CONST | ECF_LEAF | ECF_NOTHROW)
DEF_INTERNAL_FN (ANNOTATE, ECF_CONST | ECF_LEAF | ECF_NOTHROW)
DEF_INTERNAL_FN (UBSAN_NULL, ECF_LEAF | ECF_NOTHROW)
5 changes: 5 additions & 0 deletions gcc/opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,7 @@ common_handle_option (struct gcc_options *opts,
{ "unreachable", SANITIZE_UNREACHABLE,
sizeof "unreachable" - 1 },
{ "vla-bound", SANITIZE_VLA, sizeof "vla-bound" - 1 },
{ "null", SANITIZE_NULL, sizeof "null" - 1 },
{ NULL, 0, 0 }
};
const char *comma;
Expand Down Expand Up @@ -1487,6 +1488,10 @@ common_handle_option (struct gcc_options *opts,
p = comma + 1;
}

/* When instrumenting the pointers, we don't want to remove
the null pointer checks. */
if (flag_sanitize & SANITIZE_NULL)
opts->x_flag_delete_null_pointer_checks = 0;
break;
}

Expand Down
2 changes: 2 additions & 0 deletions gcc/passes.def
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ along with GCC; see the file COPYING3. If not see
NEXT_PASS (pass_init_datastructures);

NEXT_PASS (pass_build_ssa);
NEXT_PASS (pass_ubsan);
NEXT_PASS (pass_early_warn_uninitialized);
NEXT_PASS (pass_rebuild_cgraph_edges);
NEXT_PASS (pass_inline_parameters);
Expand Down Expand Up @@ -304,6 +305,7 @@ along with GCC; see the file COPYING3. If not see
NEXT_PASS (pass_lower_complex_O0);
NEXT_PASS (pass_asan_O0);
NEXT_PASS (pass_tsan_O0);
NEXT_PASS (pass_sanopt);
NEXT_PASS (pass_cleanup_eh);
NEXT_PASS (pass_lower_resx);
NEXT_PASS (pass_nrv);
Expand Down
4 changes: 4 additions & 0 deletions gcc/sanitizer.def
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,7 @@ DEF_SANITIZER_BUILTIN(BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE,
"__ubsan_handle_vla_bound_not_positive",
BT_FN_VOID_PTR_PTR,
ATTR_COLD_NOTHROW_LEAF_LIST)
DEF_SANITIZER_BUILTIN(BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH,
"__ubsan_handle_type_mismatch",
BT_FN_VOID_PTR_PTR,
ATTR_COLD_NOTHROW_LEAF_LIST)
17 changes: 17 additions & 0 deletions gcc/testsuite/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
2013-11-19 Marek Polacek <[email protected]>

* c-c++-common/ubsan/null-1.c: New test.
* c-c++-common/ubsan/null-2.c: New test.
* c-c++-common/ubsan/null-3.c: New test.
* c-c++-common/ubsan/null-4.c: New test.
* c-c++-common/ubsan/null-5.c: New test.
* c-c++-common/ubsan/null-6.c: New test.
* c-c++-common/ubsan/null-7.c: New test.
* c-c++-common/ubsan/null-8.c: New test.
* c-c++-common/ubsan/null-9.c: New test.
* c-c++-common/ubsan/null-10.c: New test.
* c-c++-common/ubsan/null-11.c: New test.
* gcc.dg/ubsan/c99-shift-2.c: Adjust dg-output.
* c-c++-common/ubsan/shift-1.c: Likewise.
* c-c++-common/ubsan/div-by-zero-3.c: Likewise.

2013-11-19 Uros Bizjak <[email protected]>

* gcc.dg/c11-complex-1.c: Use dg-add-options ieee.
Expand Down
6 changes: 3 additions & 3 deletions gcc/testsuite/c-c++-common/ubsan/div-by-zero-3.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ main (void)
return 0;
}

/* { dg-output "division of -2147483648 by -1 cannot be represented in type int(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*division of -2147483648 by -1 cannot be represented in type int(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*division of -2147483648 by -1 cannot be represented in type int(\n|\r\n|\r)" } */
/* { dg-output "division of -2147483648 by -1 cannot be represented in type 'int'(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*division of -2147483648 by -1 cannot be represented in type 'int'(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*division of -2147483648 by -1 cannot be represented in type 'int'(\n|\r\n|\r)" } */
13 changes: 13 additions & 0 deletions gcc/testsuite/c-c++-common/ubsan/null-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* { dg-do run } */
/* { dg-options "-fsanitize=null -w" } */
/* { dg-shouldfail "ubsan" } */
/* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */

int
main (void)
{
int *p = 0;
return *p;
}

/* { dg-output "load of null pointer of type 'int'(\n|\r\n|\r)" } */
14 changes: 14 additions & 0 deletions gcc/testsuite/c-c++-common/ubsan/null-10.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* { dg-do run } */
/* { dg-options "-fsanitize=null -w" } */
/* { dg-shouldfail "ubsan" } */
/* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */

int
main (void)
{
short *p = 0, *u;
*(u + *p) = 23;
return 0;
}

/* { dg-output "load of null pointer of type 'short int'(\n|\r\n|\r)" } */
17 changes: 17 additions & 0 deletions gcc/testsuite/c-c++-common/ubsan/null-11.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* { dg-do run } */
/* { dg-options "-fsanitize=null -w" } */
/* { dg-shouldfail "ubsan" } */
/* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */

struct S {
int i;
};

int
main (void)
{
struct S **s = 0;
return (*s)->i;
}

/* { dg-output "load of null pointer of type 'struct S \\*'(\n|\r\n|\r)" } */
13 changes: 13 additions & 0 deletions gcc/testsuite/c-c++-common/ubsan/null-2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* { dg-do run } */
/* { dg-options "-fsanitize=null -w" } */
/* { dg-shouldfail "ubsan" } */
/* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */

int
main (void)
{
int ***ppp = 0;
return ***ppp;
}

/* { dg-output "load of null pointer of type 'int \\*\\*'(\n|\r\n|\r)" } */
Loading

0 comments on commit b9a55b1

Please sign in to comment.