bool walk_stmt_load_store_addr_ops (gimple *stmt, void *data, walk_stmt_load_store_addr_fn visit_load, walk_stmt_load_store_addr_fn visit_store, walk_stmt_load_store_addr_fn visit_addr) { bool ret = false; unsigned i; if (gimple_assign_single_p (stmt)) { tree lhs, rhs, arg; if (visit_store) { arg = gimple_assign_lhs (stmt); lhs = get_base_loadstore (arg); if (lhs) ret |= visit_store (stmt, lhs, arg, data); } arg = gimple_assign_rhs1 (stmt); rhs = arg; while (handled_component_p (rhs)) rhs = TREE_OPERAND (rhs, 0); if (visit_addr) { if (TREE_CODE (rhs) == ADDR_EXPR) ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), arg, data); else if (TREE_CODE (rhs) == TARGET_MEM_REF && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR) ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), arg, data); else if (TREE_CODE (rhs) == OBJ_TYPE_REF && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR) ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs), 0), arg, data); else if (TREE_CODE (rhs) == CONSTRUCTOR) { unsigned int ix; tree val; FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), ix, val) if (TREE_CODE (val) == ADDR_EXPR) ret |= visit_addr (stmt, TREE_OPERAND (val, 0), arg, data); else if (TREE_CODE (val) == OBJ_TYPE_REF && TREE_CODE (OBJ_TYPE_REF_OBJECT (val)) == ADDR_EXPR) ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (val), 0), arg, data); } lhs = gimple_assign_lhs (stmt); if (TREE_CODE (lhs) == TARGET_MEM_REF && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR) ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), lhs, data); } if (visit_load) { rhs = get_base_loadstore (rhs); if (rhs) ret |= visit_load (stmt, rhs, arg, data); } }
void get_address_description (tree op, struct mem_address *addr) { addr->symbol = TMR_SYMBOL (op); addr->base = TMR_BASE (op); addr->index = TMR_INDEX (op); addr->step = TMR_STEP (op); addr->offset = TMR_OFFSET (op); }
tree tree_mem_ref_addr (tree type, tree mem_ref) { tree addr; tree act_elem; tree step = TMR_STEP (mem_ref), offset = TMR_OFFSET (mem_ref); tree sym = TMR_SYMBOL (mem_ref), base = TMR_BASE (mem_ref); tree addr_base = NULL_TREE, addr_off = NULL_TREE; if (sym) addr_base = fold_convert (type, build_addr (sym, current_function_decl)); else if (base && POINTER_TYPE_P (TREE_TYPE (base))) { addr_base = fold_convert (type, base); base = NULL_TREE; } act_elem = TMR_INDEX (mem_ref); if (act_elem) { if (step) act_elem = fold_build2 (MULT_EXPR, sizetype, act_elem, step); addr_off = act_elem; } act_elem = base; if (act_elem) { if (addr_off) addr_off = fold_build2 (PLUS_EXPR, sizetype, addr_off, act_elem); else addr_off = act_elem; } if (offset && !integer_zerop (offset)) { if (addr_off) addr_off = fold_build2 (PLUS_EXPR, sizetype, addr_off, offset); else addr_off = offset; } if (addr_off) { if (addr_base) addr = fold_build2 (POINTER_PLUS_EXPR, type, addr_base, addr_off); else addr = fold_convert (type, addr_off); } else if (addr_base) addr = addr_base; else addr = build_int_cst (type, 0); return addr; }
static void get_tmr_operands (gimple stmt, tree expr, int flags) { if (TREE_THIS_VOLATILE (expr)) gimple_set_has_volatile_ops (stmt, true); /* First record the real operands. */ get_expr_operands (stmt, &TMR_BASE (expr), opf_use | (flags & opf_no_vops)); get_expr_operands (stmt, &TMR_INDEX (expr), opf_use | (flags & opf_no_vops)); get_expr_operands (stmt, &TMR_INDEX2 (expr), opf_use | (flags & opf_no_vops)); add_virtual_operand (stmt, flags); }
void get_address_description (tree op, struct mem_address *addr) { if (TREE_CODE (TMR_BASE (op)) == ADDR_EXPR) { addr->symbol = TMR_BASE (op); addr->base = TMR_INDEX2 (op); } else { addr->symbol = NULL_TREE; if (TMR_INDEX2 (op)) { gcc_assert (integer_zerop (TMR_BASE (op))); addr->base = TMR_INDEX2 (op); } else addr->base = TMR_BASE (op); } addr->index = TMR_INDEX (op); addr->step = TMR_STEP (op); addr->offset = TMR_OFFSET (op); }
tree tree_mem_ref_addr (tree type, tree mem_ref) { tree addr; tree act_elem; tree step = TMR_STEP (mem_ref), offset = TMR_OFFSET (mem_ref); tree addr_base = NULL_TREE, addr_off = NULL_TREE; addr_base = fold_convert (type, TMR_BASE (mem_ref)); act_elem = TMR_INDEX (mem_ref); if (act_elem) { if (step) act_elem = fold_build2 (MULT_EXPR, TREE_TYPE (act_elem), act_elem, step); addr_off = act_elem; } act_elem = TMR_INDEX2 (mem_ref); if (act_elem) { if (addr_off) addr_off = fold_build2 (PLUS_EXPR, TREE_TYPE (addr_off), addr_off, act_elem); else addr_off = act_elem; } if (offset && !integer_zerop (offset)) { if (addr_off) addr_off = fold_build2 (PLUS_EXPR, TREE_TYPE (addr_off), addr_off, fold_convert (TREE_TYPE (addr_off), offset)); else addr_off = offset; } if (addr_off) addr = fold_build_pointer_plus (addr_base, addr_off); else addr = addr_base; return addr; }
static void mf_xform_derefs_1 (gimple_stmt_iterator *iter, tree *tp, location_t location, tree dirflag) { tree type, base, limit, addr, size, t; /* Don't instrument read operations. */ if (dirflag == integer_zero_node && flag_mudflap_ignore_reads) return; /* Don't instrument marked nodes. */ if (mf_marked_p (*tp)) return; t = *tp; type = TREE_TYPE (t); if (type == error_mark_node) return; size = TYPE_SIZE_UNIT (type); switch (TREE_CODE (t)) { case ARRAY_REF: case COMPONENT_REF: { /* This is trickier than it may first appear. The reason is that we are looking at expressions from the "inside out" at this point. We may have a complex nested aggregate/array expression (e.g. "a.b[i].c"), maybe with an indirection as the leftmost operator ("p->a.b.d"), where instrumentation is necessary. Or we may have an innocent "a.b.c" expression that must not be instrumented. We need to recurse all the way down the nesting structure to figure it out: looking just at the outer node is not enough. */ tree var; int component_ref_only = (TREE_CODE (t) == COMPONENT_REF); /* If we have a bitfield component reference, we must note the innermost addressable object in ELT, from which we will construct the byte-addressable bounds of the bitfield. */ tree elt = NULL_TREE; int bitfield_ref_p = (TREE_CODE (t) == COMPONENT_REF && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1))); /* Iterate to the top of the ARRAY_REF/COMPONENT_REF containment hierarchy to find the outermost VAR_DECL. */ var = TREE_OPERAND (t, 0); while (1) { if (bitfield_ref_p && elt == NULL_TREE && (TREE_CODE (var) == ARRAY_REF || TREE_CODE (var) == COMPONENT_REF)) elt = var; if (TREE_CODE (var) == ARRAY_REF) { component_ref_only = 0; var = TREE_OPERAND (var, 0); } else if (TREE_CODE (var) == COMPONENT_REF) var = TREE_OPERAND (var, 0); else if (INDIRECT_REF_P (var) || TREE_CODE (var) == MEM_REF) { base = TREE_OPERAND (var, 0); break; } else if (TREE_CODE (var) == VIEW_CONVERT_EXPR) { var = TREE_OPERAND (var, 0); if (CONSTANT_CLASS_P (var) && TREE_CODE (var) != STRING_CST) return; } else { gcc_assert (TREE_CODE (var) == VAR_DECL || TREE_CODE (var) == PARM_DECL || TREE_CODE (var) == RESULT_DECL || TREE_CODE (var) == STRING_CST); /* Don't instrument this access if the underlying variable is not "eligible". This test matches those arrays that have only known-valid indexes, and thus are not labeled TREE_ADDRESSABLE. */ if (! mf_decl_eligible_p (var) || component_ref_only) return; else { base = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (var)), var); break; } } } /* Handle the case of ordinary non-indirection structure accesses. These have only nested COMPONENT_REF nodes (no INDIRECT_REF), but pass through the above filter loop. Note that it's possible for such a struct variable to match the eligible_p test because someone else might take its address sometime. */ /* We need special processing for bitfield components, because their addresses cannot be taken. */ if (bitfield_ref_p) { tree field = TREE_OPERAND (t, 1); if (TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST) size = DECL_SIZE_UNIT (field); if (elt) elt = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (elt)), elt); addr = fold_convert_loc (location, ptr_type_node, elt ? elt : base); addr = fold_build_pointer_plus_loc (location, addr, byte_position (field)); } else addr = build1 (ADDR_EXPR, build_pointer_type (type), t); limit = fold_build2_loc (location, MINUS_EXPR, mf_uintptr_type, fold_build2_loc (location, PLUS_EXPR, mf_uintptr_type, fold_convert (mf_uintptr_type, addr), size), integer_one_node); } break; case INDIRECT_REF: addr = TREE_OPERAND (t, 0); base = addr; limit = fold_build_pointer_plus_hwi_loc (location, fold_build_pointer_plus_loc (location, base, size), -1); break; case MEM_REF: if (addr_expr_of_non_mem_decl_p (TREE_OPERAND (t, 0))) return; addr = fold_build_pointer_plus_loc (location, TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)); base = addr; limit = fold_build_pointer_plus_hwi_loc (location, fold_build_pointer_plus_loc (location, base, size), -1); break; case TARGET_MEM_REF: if (addr_expr_of_non_mem_decl_p (TMR_BASE (t))) return; addr = tree_mem_ref_addr (ptr_type_node, t); base = addr; limit = fold_build_pointer_plus_hwi_loc (location, fold_build_pointer_plus_loc (location, base, size), -1); break; case ARRAY_RANGE_REF: warning (OPT_Wmudflap, "mudflap checking not yet implemented for ARRAY_RANGE_REF"); return; case BIT_FIELD_REF: /* ??? merge with COMPONENT_REF code above? */ { tree ofs, rem, bpu; /* If we're not dereferencing something, then the access must be ok. */ if (TREE_CODE (TREE_OPERAND (t, 0)) != INDIRECT_REF) return; bpu = bitsize_int (BITS_PER_UNIT); ofs = fold_convert (bitsizetype, TREE_OPERAND (t, 2)); rem = size_binop_loc (location, TRUNC_MOD_EXPR, ofs, bpu); ofs = size_binop_loc (location, TRUNC_DIV_EXPR, ofs, bpu); size = fold_convert (bitsizetype, TREE_OPERAND (t, 1)); size = size_binop_loc (location, PLUS_EXPR, size, rem); size = size_binop_loc (location, CEIL_DIV_EXPR, size, bpu); size = fold_convert (sizetype, size); addr = TREE_OPERAND (TREE_OPERAND (t, 0), 0); addr = fold_convert (ptr_type_node, addr); addr = fold_build_pointer_plus_loc (location, addr, ofs); base = addr; limit = fold_build_pointer_plus_hwi_loc (location, fold_build_pointer_plus_loc (location, base, size), -1); } break; default: return; } mf_build_check_statement_for (base, limit, iter, location, dirflag); }
bool for_each_index (tree *addr_p, bool (*cbck) (tree, tree *, void *), void *data) { tree *nxt, *idx; for (; ; addr_p = nxt) { switch (TREE_CODE (*addr_p)) { case SSA_NAME: return cbck (*addr_p, addr_p, data); case MISALIGNED_INDIRECT_REF: case ALIGN_INDIRECT_REF: case INDIRECT_REF: nxt = &TREE_OPERAND (*addr_p, 0); return cbck (*addr_p, nxt, data); case BIT_FIELD_REF: case VIEW_CONVERT_EXPR: case REALPART_EXPR: case IMAGPART_EXPR: nxt = &TREE_OPERAND (*addr_p, 0); break; case COMPONENT_REF: /* If the component has varying offset, it behaves like index as well. */ idx = &TREE_OPERAND (*addr_p, 2); if (*idx && !cbck (*addr_p, idx, data)) return false; nxt = &TREE_OPERAND (*addr_p, 0); break; case ARRAY_REF: case ARRAY_RANGE_REF: nxt = &TREE_OPERAND (*addr_p, 0); if (!cbck (*addr_p, &TREE_OPERAND (*addr_p, 1), data)) return false; break; case VAR_DECL: case PARM_DECL: case STRING_CST: case RESULT_DECL: case VECTOR_CST: case COMPLEX_CST: case INTEGER_CST: case REAL_CST: case FIXED_CST: case CONSTRUCTOR: return true; case TARGET_MEM_REF: idx = &TMR_BASE (*addr_p); if (*idx && !cbck (*addr_p, idx, data)) return false; idx = &TMR_INDEX (*addr_p); if (*idx && !cbck (*addr_p, idx, data)) return false; return true; default: gcc_unreachable (); } } }