xref: /dragonfly/contrib/gcc-8.0/gcc/ipa-chkp.c (revision 38fd1498)
1*38fd1498Szrj /* Pointer Bounds Checker IPA passes.
2*38fd1498Szrj    Copyright (C) 2014-2018 Free Software Foundation, Inc.
3*38fd1498Szrj    Contributed by Ilya Enkovich (ilya.enkovich@intel.com)
4*38fd1498Szrj 
5*38fd1498Szrj This file is part of GCC.
6*38fd1498Szrj 
7*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
8*38fd1498Szrj the terms of the GNU General Public License as published by the Free
9*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
10*38fd1498Szrj version.
11*38fd1498Szrj 
12*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*38fd1498Szrj for more details.
16*38fd1498Szrj 
17*38fd1498Szrj You should have received a copy of the GNU General Public License
18*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
19*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
20*38fd1498Szrj 
21*38fd1498Szrj #include "config.h"
22*38fd1498Szrj #define INCLUDE_STRING
23*38fd1498Szrj #include "system.h"
24*38fd1498Szrj #include "coretypes.h"
25*38fd1498Szrj #include "backend.h"
26*38fd1498Szrj #include "tree.h"
27*38fd1498Szrj #include "gimple.h"
28*38fd1498Szrj #include "tree-pass.h"
29*38fd1498Szrj #include "stringpool.h"
30*38fd1498Szrj #include "lto-streamer.h"
31*38fd1498Szrj #include "stor-layout.h"
32*38fd1498Szrj #include "calls.h"
33*38fd1498Szrj #include "cgraph.h"
34*38fd1498Szrj #include "tree-chkp.h"
35*38fd1498Szrj #include "tree-inline.h"
36*38fd1498Szrj #include "ipa-chkp.h"
37*38fd1498Szrj #include "stringpool.h"
38*38fd1498Szrj #include "attribs.h"
39*38fd1498Szrj 
40*38fd1498Szrj /*  Pointer Bounds Checker has two IPA passes to support code instrumentation.
41*38fd1498Szrj 
42*38fd1498Szrj     In instrumented code each pointer is provided with bounds.  For input
43*38fd1498Szrj     pointer parameters it means we also have bounds passed.  For calls it
44*38fd1498Szrj     means we have additional bounds arguments for pointer arguments.
45*38fd1498Szrj 
46*38fd1498Szrj     To have all IPA optimizations working correctly we have to express
47*38fd1498Szrj     dataflow between passed and received bounds explicitly via additional
48*38fd1498Szrj     entries in function declaration arguments list and in function type.
49*38fd1498Szrj     Since we may have both instrumented and not instrumented code at the
50*38fd1498Szrj     same time, we cannot replace all original functions with their
51*38fd1498Szrj     instrumented variants.  Therefore we create clones (versions) instead.
52*38fd1498Szrj 
53*38fd1498Szrj     Instrumentation clones creation is a separate IPA pass which is a part
54*38fd1498Szrj     of early local passes.  Clones are created after SSA is built (because
55*38fd1498Szrj     instrumentation pass works on SSA) and before any transformations
56*38fd1498Szrj     which may change pointer flow and therefore lead to incorrect code
57*38fd1498Szrj     instrumentation (possibly causing false bounds check failures).
58*38fd1498Szrj 
59*38fd1498Szrj     Instrumentation clones have pointer bounds arguments added right after
60*38fd1498Szrj     pointer arguments.  Clones have assembler name of the original
61*38fd1498Szrj     function with suffix added.  New assembler name is in transparent
62*38fd1498Szrj     alias chain with the original name.  Thus we expect all calls to the
63*38fd1498Szrj     original and instrumented functions look similar in assembler.
64*38fd1498Szrj 
65*38fd1498Szrj     During instrumentation versioning pass we create instrumented versions
66*38fd1498Szrj     of all function with body and also for all their aliases and thunks.
67*38fd1498Szrj     Clones for functions with no body are created on demand (usually
68*38fd1498Szrj     during call instrumentation).
69*38fd1498Szrj 
70*38fd1498Szrj     Original and instrumented function nodes are connected with IPA
71*38fd1498Szrj     reference IPA_REF_CHKP.  It is mostly done to have reachability
72*38fd1498Szrj     analysis working correctly.  We may have no references to the
73*38fd1498Szrj     instrumented function in the code but it still should be counted
74*38fd1498Szrj     as reachable if the original function is reachable.
75*38fd1498Szrj 
76*38fd1498Szrj     When original function bodies are not needed anymore we release
77*38fd1498Szrj     them and transform functions into a special kind of thunks.  Each
78*38fd1498Szrj     thunk has a call edge to the instrumented version.  These thunks
79*38fd1498Szrj     help to keep externally visible instrumented functions visible
80*38fd1498Szrj     when linker resolution files are used.  Linker has no info about
81*38fd1498Szrj     connection between original and instrumented function and
82*38fd1498Szrj     therefore we may wrongly decide (due to difference in assembler
83*38fd1498Szrj     names) that instrumented function version is local and can be
84*38fd1498Szrj     removed.  */
85*38fd1498Szrj 
86*38fd1498Szrj #define CHKP_BOUNDS_OF_SYMBOL_PREFIX "__chkp_bounds_of_"
87*38fd1498Szrj #define CHKP_WRAPPER_SYMBOL_PREFIX "__mpx_wrapper_"
88*38fd1498Szrj 
89*38fd1498Szrj /* Return 1 calls to FNDECL should be replaced with
90*38fd1498Szrj    a call to wrapper function.  */
91*38fd1498Szrj bool
chkp_wrap_function(tree fndecl)92*38fd1498Szrj chkp_wrap_function (tree fndecl)
93*38fd1498Szrj {
94*38fd1498Szrj   if (!flag_chkp_use_wrappers)
95*38fd1498Szrj     return false;
96*38fd1498Szrj 
97*38fd1498Szrj   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
98*38fd1498Szrj     {
99*38fd1498Szrj       switch (DECL_FUNCTION_CODE (fndecl))
100*38fd1498Szrj 	{
101*38fd1498Szrj 	case BUILT_IN_STRLEN:
102*38fd1498Szrj 	case BUILT_IN_STRCPY:
103*38fd1498Szrj 	case BUILT_IN_STRNCPY:
104*38fd1498Szrj 	case BUILT_IN_STPCPY:
105*38fd1498Szrj 	case BUILT_IN_STPNCPY:
106*38fd1498Szrj 	case BUILT_IN_STRCAT:
107*38fd1498Szrj 	case BUILT_IN_STRNCAT:
108*38fd1498Szrj 	case BUILT_IN_MEMCPY:
109*38fd1498Szrj 	case BUILT_IN_MEMPCPY:
110*38fd1498Szrj 	case BUILT_IN_MEMSET:
111*38fd1498Szrj 	case BUILT_IN_MEMMOVE:
112*38fd1498Szrj 	case BUILT_IN_BZERO:
113*38fd1498Szrj 	case BUILT_IN_MALLOC:
114*38fd1498Szrj 	case BUILT_IN_CALLOC:
115*38fd1498Szrj 	case BUILT_IN_REALLOC:
116*38fd1498Szrj 	  return 1;
117*38fd1498Szrj 
118*38fd1498Szrj 	default:
119*38fd1498Szrj 	  return 0;
120*38fd1498Szrj 	}
121*38fd1498Szrj     }
122*38fd1498Szrj 
123*38fd1498Szrj   return false;
124*38fd1498Szrj }
125*38fd1498Szrj 
126*38fd1498Szrj static const char *
chkp_wrap_function_name(tree fndecl)127*38fd1498Szrj chkp_wrap_function_name (tree fndecl)
128*38fd1498Szrj {
129*38fd1498Szrj   gcc_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
130*38fd1498Szrj 
131*38fd1498Szrj   switch (DECL_FUNCTION_CODE (fndecl))
132*38fd1498Szrj     {
133*38fd1498Szrj     case BUILT_IN_STRLEN:
134*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "strlen";
135*38fd1498Szrj     case BUILT_IN_STRCPY:
136*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "strcpy";
137*38fd1498Szrj     case BUILT_IN_STRNCPY:
138*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "strncpy";
139*38fd1498Szrj     case BUILT_IN_STPCPY:
140*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "stpcpy";
141*38fd1498Szrj     case BUILT_IN_STPNCPY:
142*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "stpncpy";
143*38fd1498Szrj     case BUILT_IN_STRCAT:
144*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "strcat";
145*38fd1498Szrj     case BUILT_IN_STRNCAT:
146*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "strncat";
147*38fd1498Szrj     case BUILT_IN_MEMCPY:
148*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "memcpy";
149*38fd1498Szrj     case BUILT_IN_MEMPCPY:
150*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "mempcpy";
151*38fd1498Szrj     case BUILT_IN_MEMSET:
152*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "memset";
153*38fd1498Szrj     case BUILT_IN_MEMMOVE:
154*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "memmove";
155*38fd1498Szrj     case BUILT_IN_BZERO:
156*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "bzero";
157*38fd1498Szrj     case BUILT_IN_MALLOC:
158*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "malloc";
159*38fd1498Szrj     case BUILT_IN_CALLOC:
160*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "calloc";
161*38fd1498Szrj     case BUILT_IN_REALLOC:
162*38fd1498Szrj       return CHKP_WRAPPER_SYMBOL_PREFIX "realloc";
163*38fd1498Szrj 
164*38fd1498Szrj     default:
165*38fd1498Szrj       gcc_unreachable ();
166*38fd1498Szrj     }
167*38fd1498Szrj 
168*38fd1498Szrj   return "";
169*38fd1498Szrj }
170*38fd1498Szrj 
171*38fd1498Szrj /* Build a clone of FNDECL with a modified name.  */
172*38fd1498Szrj 
173*38fd1498Szrj static tree
chkp_build_instrumented_fndecl(tree fndecl)174*38fd1498Szrj chkp_build_instrumented_fndecl (tree fndecl)
175*38fd1498Szrj {
176*38fd1498Szrj   tree new_decl = copy_node (fndecl);
177*38fd1498Szrj   tree new_name;
178*38fd1498Szrj   std::string s;
179*38fd1498Szrj 
180*38fd1498Szrj   /* called_as_built_in checks DECL_NAME to identify calls to
181*38fd1498Szrj      builtins.  We want instrumented calls to builtins to be
182*38fd1498Szrj      recognized by called_as_built_in.  Therefore use original
183*38fd1498Szrj      DECL_NAME for cloning with no prefixes.  */
184*38fd1498Szrj   s = IDENTIFIER_POINTER (DECL_NAME (fndecl));
185*38fd1498Szrj   s += ".chkp";
186*38fd1498Szrj   DECL_NAME (new_decl) = get_identifier (s.c_str ());
187*38fd1498Szrj 
188*38fd1498Szrj   /* References to the original and to the instrumented version
189*38fd1498Szrj      should look the same in the output assembly.  And we cannot
190*38fd1498Szrj      use the same assembler name for the instrumented version
191*38fd1498Szrj      because it conflicts with decl merging algorithms in LTO.
192*38fd1498Szrj      Achieve the result by using transparent alias name for the
193*38fd1498Szrj      instrumented version.  */
194*38fd1498Szrj   if (chkp_wrap_function(fndecl))
195*38fd1498Szrj     {
196*38fd1498Szrj       new_name = get_identifier (chkp_wrap_function_name (fndecl));
197*38fd1498Szrj       DECL_VISIBILITY (new_decl) = VISIBILITY_DEFAULT;
198*38fd1498Szrj     }
199*38fd1498Szrj   else
200*38fd1498Szrj     {
201*38fd1498Szrj       s = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl));
202*38fd1498Szrj       s += ".chkp";
203*38fd1498Szrj       new_name = get_identifier (s.c_str ());
204*38fd1498Szrj       IDENTIFIER_TRANSPARENT_ALIAS (new_name) = 1;
205*38fd1498Szrj       TREE_CHAIN (new_name) = DECL_ASSEMBLER_NAME (fndecl);
206*38fd1498Szrj     }
207*38fd1498Szrj   SET_DECL_ASSEMBLER_NAME (new_decl, new_name);
208*38fd1498Szrj 
209*38fd1498Szrj   /* For functions with body versioning will make a copy of arguments.
210*38fd1498Szrj      For functions with no body we need to do it here.  */
211*38fd1498Szrj   if (!gimple_has_body_p (fndecl))
212*38fd1498Szrj     {
213*38fd1498Szrj       tree arg;
214*38fd1498Szrj 
215*38fd1498Szrj       DECL_ARGUMENTS (new_decl) = copy_list (DECL_ARGUMENTS (fndecl));
216*38fd1498Szrj       for (arg = DECL_ARGUMENTS (new_decl); arg; arg = DECL_CHAIN (arg))
217*38fd1498Szrj 	DECL_CONTEXT (arg) = new_decl;
218*38fd1498Szrj     }
219*38fd1498Szrj 
220*38fd1498Szrj   /* We are going to modify attributes list and therefore should
221*38fd1498Szrj      make own copy.  */
222*38fd1498Szrj   DECL_ATTRIBUTES (new_decl) = copy_list (DECL_ATTRIBUTES (fndecl));
223*38fd1498Szrj 
224*38fd1498Szrj   /* Change builtin function code.  */
225*38fd1498Szrj   if (DECL_BUILT_IN (new_decl))
226*38fd1498Szrj     {
227*38fd1498Szrj       gcc_assert (DECL_BUILT_IN_CLASS (new_decl) == BUILT_IN_NORMAL);
228*38fd1498Szrj       gcc_assert (DECL_FUNCTION_CODE (new_decl) < BEGIN_CHKP_BUILTINS);
229*38fd1498Szrj       DECL_FUNCTION_CODE (new_decl)
230*38fd1498Szrj 	= (enum built_in_function)(DECL_FUNCTION_CODE (new_decl)
231*38fd1498Szrj 				   + BEGIN_CHKP_BUILTINS + 1);
232*38fd1498Szrj     }
233*38fd1498Szrj 
234*38fd1498Szrj   return new_decl;
235*38fd1498Szrj }
236*38fd1498Szrj 
237*38fd1498Szrj 
238*38fd1498Szrj /* Fix operands of attribute from ATTRS list named ATTR_NAME.
239*38fd1498Szrj    Integer operands are replaced with values according to
240*38fd1498Szrj    INDEXES map having LEN elements.  For operands out of len
241*38fd1498Szrj    we just add DELTA.  */
242*38fd1498Szrj 
243*38fd1498Szrj static void
chkp_map_attr_arg_indexes(tree attrs,const char * attr_name,unsigned * indexes,int len,int delta)244*38fd1498Szrj chkp_map_attr_arg_indexes (tree attrs, const char *attr_name,
245*38fd1498Szrj 			   unsigned *indexes, int len, int delta)
246*38fd1498Szrj {
247*38fd1498Szrj   tree attr = lookup_attribute (attr_name, attrs);
248*38fd1498Szrj   tree op;
249*38fd1498Szrj 
250*38fd1498Szrj   if (!attr)
251*38fd1498Szrj     return;
252*38fd1498Szrj 
253*38fd1498Szrj   TREE_VALUE (attr) = copy_list (TREE_VALUE (attr));
254*38fd1498Szrj   for (op = TREE_VALUE (attr); op; op = TREE_CHAIN (op))
255*38fd1498Szrj     {
256*38fd1498Szrj       int idx;
257*38fd1498Szrj 
258*38fd1498Szrj       if (TREE_CODE (TREE_VALUE (op)) != INTEGER_CST)
259*38fd1498Szrj 	continue;
260*38fd1498Szrj 
261*38fd1498Szrj       idx = TREE_INT_CST_LOW (TREE_VALUE (op));
262*38fd1498Szrj 
263*38fd1498Szrj       /* If idx exceeds indexes length then we just
264*38fd1498Szrj 	 keep it at the same distance from the last
265*38fd1498Szrj 	 known arg.  */
266*38fd1498Szrj       if (idx > len)
267*38fd1498Szrj 	idx += delta;
268*38fd1498Szrj       else
269*38fd1498Szrj 	idx = indexes[idx - 1] + 1;
270*38fd1498Szrj       TREE_VALUE (op) = build_int_cst (TREE_TYPE (TREE_VALUE (op)), idx);
271*38fd1498Szrj     }
272*38fd1498Szrj }
273*38fd1498Szrj 
274*38fd1498Szrj /* Make a copy of function type ORIG_TYPE adding pointer
275*38fd1498Szrj    bounds as additional arguments.  */
276*38fd1498Szrj 
277*38fd1498Szrj tree
chkp_copy_function_type_adding_bounds(tree orig_type)278*38fd1498Szrj chkp_copy_function_type_adding_bounds (tree orig_type)
279*38fd1498Szrj {
280*38fd1498Szrj   tree type;
281*38fd1498Szrj   tree arg_type, attrs;
282*38fd1498Szrj   unsigned len = list_length (TYPE_ARG_TYPES (orig_type));
283*38fd1498Szrj   unsigned *indexes = XALLOCAVEC (unsigned, len);
284*38fd1498Szrj   unsigned idx = 0, new_idx = 0;
285*38fd1498Szrj 
286*38fd1498Szrj   for (arg_type = TYPE_ARG_TYPES (orig_type);
287*38fd1498Szrj        arg_type;
288*38fd1498Szrj        arg_type = TREE_CHAIN (arg_type))
289*38fd1498Szrj     if (TREE_VALUE (arg_type) == void_type_node)
290*38fd1498Szrj       continue;
291*38fd1498Szrj     else if (BOUNDED_TYPE_P (TREE_VALUE (arg_type))
292*38fd1498Szrj 	     || pass_by_reference (NULL, TYPE_MODE (TREE_VALUE (arg_type)),
293*38fd1498Szrj 				   TREE_VALUE (arg_type), true)
294*38fd1498Szrj 	     || chkp_type_has_pointer (TREE_VALUE (arg_type)))
295*38fd1498Szrj       break;
296*38fd1498Szrj 
297*38fd1498Szrj   /* We may use original type if there are no bounds passed.  */
298*38fd1498Szrj   if (!arg_type)
299*38fd1498Szrj     return orig_type;
300*38fd1498Szrj 
301*38fd1498Szrj   type = build_distinct_type_copy (orig_type);
302*38fd1498Szrj   TYPE_ARG_TYPES (type) = copy_list (TYPE_ARG_TYPES (type));
303*38fd1498Szrj 
304*38fd1498Szrj   for (arg_type = TYPE_ARG_TYPES (type);
305*38fd1498Szrj        arg_type;
306*38fd1498Szrj        arg_type = TREE_CHAIN (arg_type))
307*38fd1498Szrj     {
308*38fd1498Szrj       indexes[idx++] = new_idx++;
309*38fd1498Szrj 
310*38fd1498Szrj       /* pass_by_reference returns 1 for void type,
311*38fd1498Szrj 	 so check for it first.  */
312*38fd1498Szrj       if (TREE_VALUE (arg_type) == void_type_node)
313*38fd1498Szrj 	continue;
314*38fd1498Szrj       else if (BOUNDED_TYPE_P (TREE_VALUE (arg_type))
315*38fd1498Szrj 	       || pass_by_reference (NULL, TYPE_MODE (TREE_VALUE (arg_type)),
316*38fd1498Szrj 				     TREE_VALUE (arg_type), true))
317*38fd1498Szrj 	{
318*38fd1498Szrj 	  tree new_type = build_tree_list (NULL_TREE,
319*38fd1498Szrj 					   pointer_bounds_type_node);
320*38fd1498Szrj 	  TREE_CHAIN (new_type) = TREE_CHAIN (arg_type);
321*38fd1498Szrj 	  TREE_CHAIN (arg_type) = new_type;
322*38fd1498Szrj 
323*38fd1498Szrj 	  arg_type = TREE_CHAIN (arg_type);
324*38fd1498Szrj 	  new_idx++;
325*38fd1498Szrj 	}
326*38fd1498Szrj       else if (chkp_type_has_pointer (TREE_VALUE (arg_type)))
327*38fd1498Szrj 	{
328*38fd1498Szrj 	  bitmap slots = BITMAP_ALLOC (NULL);
329*38fd1498Szrj 	  bitmap_iterator bi;
330*38fd1498Szrj 	  unsigned bnd_no;
331*38fd1498Szrj 
332*38fd1498Szrj 	  chkp_find_bound_slots (TREE_VALUE (arg_type), slots);
333*38fd1498Szrj 
334*38fd1498Szrj 	  EXECUTE_IF_SET_IN_BITMAP (slots, 0, bnd_no, bi)
335*38fd1498Szrj 	    {
336*38fd1498Szrj 	      tree new_type = build_tree_list (NULL_TREE,
337*38fd1498Szrj 					       pointer_bounds_type_node);
338*38fd1498Szrj 	      TREE_CHAIN (new_type) = TREE_CHAIN (arg_type);
339*38fd1498Szrj 	      TREE_CHAIN (arg_type) = new_type;
340*38fd1498Szrj 
341*38fd1498Szrj 	      arg_type = TREE_CHAIN (arg_type);
342*38fd1498Szrj 	      new_idx++;
343*38fd1498Szrj 	    }
344*38fd1498Szrj 	  BITMAP_FREE (slots);
345*38fd1498Szrj 	}
346*38fd1498Szrj     }
347*38fd1498Szrj 
348*38fd1498Szrj   /* If function type has attribute with arg indexes then
349*38fd1498Szrj      we have to copy it fixing attribute ops.  Map for
350*38fd1498Szrj      fixing is in indexes array.  */
351*38fd1498Szrj   attrs = TYPE_ATTRIBUTES (type);
352*38fd1498Szrj   if (lookup_attribute ("nonnull", attrs)
353*38fd1498Szrj       || lookup_attribute ("format", attrs)
354*38fd1498Szrj       || lookup_attribute ("format_arg", attrs))
355*38fd1498Szrj     {
356*38fd1498Szrj       int delta = new_idx - len;
357*38fd1498Szrj       attrs = copy_list (TYPE_ATTRIBUTES (type));
358*38fd1498Szrj       chkp_map_attr_arg_indexes (attrs, "nonnull", indexes, len, delta);
359*38fd1498Szrj       chkp_map_attr_arg_indexes (attrs, "format", indexes, len, delta);
360*38fd1498Szrj       chkp_map_attr_arg_indexes (attrs, "format_arg", indexes, len, delta);
361*38fd1498Szrj       TYPE_ATTRIBUTES (type) = attrs;
362*38fd1498Szrj     }
363*38fd1498Szrj 
364*38fd1498Szrj   return type;
365*38fd1498Szrj }
366*38fd1498Szrj 
367*38fd1498Szrj /* For given function FNDECL add bounds arguments to arguments
368*38fd1498Szrj    list.  */
369*38fd1498Szrj 
370*38fd1498Szrj static void
chkp_add_bounds_params_to_function(tree fndecl)371*38fd1498Szrj chkp_add_bounds_params_to_function (tree fndecl)
372*38fd1498Szrj {
373*38fd1498Szrj   tree arg;
374*38fd1498Szrj 
375*38fd1498Szrj   for (arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
376*38fd1498Szrj     if (BOUNDED_P (arg))
377*38fd1498Szrj       {
378*38fd1498Szrj 	std::string new_name = CHKP_BOUNDS_OF_SYMBOL_PREFIX;
379*38fd1498Szrj 	tree new_arg;
380*38fd1498Szrj 
381*38fd1498Szrj 	if (DECL_NAME (arg))
382*38fd1498Szrj 	  new_name += IDENTIFIER_POINTER (DECL_NAME (arg));
383*38fd1498Szrj 	else
384*38fd1498Szrj 	  {
385*38fd1498Szrj 	    char uid[25];
386*38fd1498Szrj 	    snprintf (uid, 25, "D.%u", DECL_UID (arg));
387*38fd1498Szrj 	    new_name += uid;
388*38fd1498Szrj 	  }
389*38fd1498Szrj 
390*38fd1498Szrj 	new_arg = build_decl (DECL_SOURCE_LOCATION (arg), PARM_DECL,
391*38fd1498Szrj 			      get_identifier (new_name.c_str ()),
392*38fd1498Szrj 			      pointer_bounds_type_node);
393*38fd1498Szrj 	DECL_ARG_TYPE (new_arg) = pointer_bounds_type_node;
394*38fd1498Szrj 	DECL_CONTEXT (new_arg) = DECL_CONTEXT (arg);
395*38fd1498Szrj 	DECL_ARTIFICIAL (new_arg) = 1;
396*38fd1498Szrj 	DECL_CHAIN (new_arg) = DECL_CHAIN (arg);
397*38fd1498Szrj 	DECL_CHAIN (arg) = new_arg;
398*38fd1498Szrj 
399*38fd1498Szrj 	arg = DECL_CHAIN (arg);
400*38fd1498Szrj 
401*38fd1498Szrj       }
402*38fd1498Szrj     else if (chkp_type_has_pointer (TREE_TYPE (arg)))
403*38fd1498Szrj       {
404*38fd1498Szrj 	tree orig_arg = arg;
405*38fd1498Szrj 	bitmap slots = BITMAP_ALLOC (NULL);
406*38fd1498Szrj 	bitmap_iterator bi;
407*38fd1498Szrj 	unsigned bnd_no;
408*38fd1498Szrj 
409*38fd1498Szrj 	chkp_find_bound_slots (TREE_TYPE (arg), slots);
410*38fd1498Szrj 
411*38fd1498Szrj 	EXECUTE_IF_SET_IN_BITMAP (slots, 0, bnd_no, bi)
412*38fd1498Szrj 	  {
413*38fd1498Szrj 	    std::string new_name = CHKP_BOUNDS_OF_SYMBOL_PREFIX;
414*38fd1498Szrj 	    tree new_arg;
415*38fd1498Szrj 	    char offs[25];
416*38fd1498Szrj 
417*38fd1498Szrj 	    if (DECL_NAME (orig_arg))
418*38fd1498Szrj 	      new_name += IDENTIFIER_POINTER (DECL_NAME (orig_arg));
419*38fd1498Szrj 	    else
420*38fd1498Szrj 	      {
421*38fd1498Szrj 		snprintf (offs, 25, "D.%u", DECL_UID (arg));
422*38fd1498Szrj 		new_name += offs;
423*38fd1498Szrj 	      }
424*38fd1498Szrj 	    snprintf (offs, 25, "__%u", bnd_no * POINTER_SIZE / BITS_PER_UNIT);
425*38fd1498Szrj 
426*38fd1498Szrj 	    new_arg = build_decl (DECL_SOURCE_LOCATION (orig_arg),
427*38fd1498Szrj 				  PARM_DECL,
428*38fd1498Szrj 				  get_identifier (new_name.c_str ()),
429*38fd1498Szrj 				  pointer_bounds_type_node);
430*38fd1498Szrj 	    DECL_ARG_TYPE (new_arg) = pointer_bounds_type_node;
431*38fd1498Szrj 	    DECL_CONTEXT (new_arg) = DECL_CONTEXT (orig_arg);
432*38fd1498Szrj 	    DECL_ARTIFICIAL (new_arg) = 1;
433*38fd1498Szrj 	    DECL_CHAIN (new_arg) = DECL_CHAIN (arg);
434*38fd1498Szrj 	    DECL_CHAIN (arg) = new_arg;
435*38fd1498Szrj 
436*38fd1498Szrj 	    arg = DECL_CHAIN (arg);
437*38fd1498Szrj 	  }
438*38fd1498Szrj 	BITMAP_FREE (slots);
439*38fd1498Szrj       }
440*38fd1498Szrj 
441*38fd1498Szrj   TREE_TYPE (fndecl) =
442*38fd1498Szrj     chkp_copy_function_type_adding_bounds (TREE_TYPE (fndecl));
443*38fd1498Szrj }
444*38fd1498Szrj 
445*38fd1498Szrj /* Return an instrumentation clone for builtin function
446*38fd1498Szrj    FNDECL.  Create one if needed.  */
447*38fd1498Szrj 
448*38fd1498Szrj tree
chkp_maybe_clone_builtin_fndecl(tree fndecl)449*38fd1498Szrj chkp_maybe_clone_builtin_fndecl (tree fndecl)
450*38fd1498Szrj {
451*38fd1498Szrj   tree clone;
452*38fd1498Szrj   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
453*38fd1498Szrj 
454*38fd1498Szrj   gcc_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
455*38fd1498Szrj 	      && fcode < BEGIN_CHKP_BUILTINS);
456*38fd1498Szrj 
457*38fd1498Szrj   fcode = (enum built_in_function) (fcode + BEGIN_CHKP_BUILTINS + 1);
458*38fd1498Szrj   clone = builtin_decl_explicit (fcode);
459*38fd1498Szrj   if (clone)
460*38fd1498Szrj     return clone;
461*38fd1498Szrj 
462*38fd1498Szrj   clone = chkp_build_instrumented_fndecl (fndecl);
463*38fd1498Szrj   chkp_add_bounds_params_to_function (clone);
464*38fd1498Szrj 
465*38fd1498Szrj   gcc_assert (DECL_FUNCTION_CODE (clone) == fcode);
466*38fd1498Szrj 
467*38fd1498Szrj   set_builtin_decl (fcode, clone, false);
468*38fd1498Szrj 
469*38fd1498Szrj   return clone;
470*38fd1498Szrj }
471*38fd1498Szrj 
472*38fd1498Szrj /* Return 1 if function FNDECL should be instrumented.  */
473*38fd1498Szrj 
474*38fd1498Szrj bool
chkp_instrumentable_p(tree fndecl)475*38fd1498Szrj chkp_instrumentable_p (tree fndecl)
476*38fd1498Szrj {
477*38fd1498Szrj   struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
478*38fd1498Szrj   return (!lookup_attribute ("bnd_legacy", DECL_ATTRIBUTES (fndecl))
479*38fd1498Szrj 	  && (!flag_chkp_instrument_marked_only
480*38fd1498Szrj 	      || lookup_attribute ("bnd_instrument", DECL_ATTRIBUTES (fndecl)))
481*38fd1498Szrj 	  && (!fn || !copy_forbidden (fn)));
482*38fd1498Szrj }
483*38fd1498Szrj 
484*38fd1498Szrj /* Return clone created for instrumentation of NODE or NULL.  */
485*38fd1498Szrj 
486*38fd1498Szrj cgraph_node *
chkp_maybe_create_clone(tree fndecl)487*38fd1498Szrj chkp_maybe_create_clone (tree fndecl)
488*38fd1498Szrj {
489*38fd1498Szrj   cgraph_node *node = cgraph_node::get_create (fndecl);
490*38fd1498Szrj   cgraph_node *clone = node->instrumented_version;
491*38fd1498Szrj 
492*38fd1498Szrj   gcc_assert (!node->instrumentation_clone);
493*38fd1498Szrj 
494*38fd1498Szrj   if (DECL_BUILT_IN (fndecl)
495*38fd1498Szrj       && (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL
496*38fd1498Szrj 	  || DECL_FUNCTION_CODE (fndecl) >= BEGIN_CHKP_BUILTINS))
497*38fd1498Szrj     return NULL;
498*38fd1498Szrj 
499*38fd1498Szrj   clone = node->instrumented_version;
500*38fd1498Szrj 
501*38fd1498Szrj   /* Some instrumented builtin function calls may be optimized and
502*38fd1498Szrj      cgraph nodes may be removed as unreachable.  Later optimizations
503*38fd1498Szrj      may generate new calls to removed functions and in this case
504*38fd1498Szrj      we have to recreate cgraph node.  FUNCTION_DECL for instrumented
505*38fd1498Szrj      builtin still exists and should be reused in such case.  */
506*38fd1498Szrj   if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
507*38fd1498Szrj       && fndecl == builtin_decl_explicit (DECL_FUNCTION_CODE (fndecl))
508*38fd1498Szrj       && !clone)
509*38fd1498Szrj     {
510*38fd1498Szrj       enum built_in_function fncode = DECL_FUNCTION_CODE (fndecl);
511*38fd1498Szrj       tree new_decl;
512*38fd1498Szrj 
513*38fd1498Szrj       fncode = (enum built_in_function) (fncode + BEGIN_CHKP_BUILTINS + 1);
514*38fd1498Szrj       new_decl = builtin_decl_explicit (fncode);
515*38fd1498Szrj 
516*38fd1498Szrj       /* We've actually already created an instrumented clone once.
517*38fd1498Szrj 	 Restore it.  */
518*38fd1498Szrj       if (new_decl)
519*38fd1498Szrj 	{
520*38fd1498Szrj 	  clone = cgraph_node::get (new_decl);
521*38fd1498Szrj 
522*38fd1498Szrj 	  if (!clone)
523*38fd1498Szrj 	    {
524*38fd1498Szrj 	      gcc_assert (!gimple_has_body_p (fndecl));
525*38fd1498Szrj 	      clone = cgraph_node::get_create (new_decl);
526*38fd1498Szrj 	      clone->externally_visible = node->externally_visible;
527*38fd1498Szrj 	      clone->local = node->local;
528*38fd1498Szrj 	      clone->address_taken = node->address_taken;
529*38fd1498Szrj 	      clone->thunk = node->thunk;
530*38fd1498Szrj 	      clone->alias = node->alias;
531*38fd1498Szrj 	      clone->weakref = node->weakref;
532*38fd1498Szrj 	      clone->cpp_implicit_alias = node->cpp_implicit_alias;
533*38fd1498Szrj 	      clone->orig_decl = fndecl;
534*38fd1498Szrj 	      clone->instrumentation_clone = true;
535*38fd1498Szrj 	    }
536*38fd1498Szrj 
537*38fd1498Szrj 	  clone->instrumented_version = node;
538*38fd1498Szrj 	  node->instrumented_version = clone;
539*38fd1498Szrj 	}
540*38fd1498Szrj     }
541*38fd1498Szrj 
542*38fd1498Szrj   if (!clone)
543*38fd1498Szrj     {
544*38fd1498Szrj       tree new_decl = chkp_build_instrumented_fndecl (fndecl);
545*38fd1498Szrj       struct cgraph_edge *e;
546*38fd1498Szrj       struct ipa_ref *ref;
547*38fd1498Szrj       int i;
548*38fd1498Szrj 
549*38fd1498Szrj       clone = node->create_version_clone (new_decl, vNULL, NULL);
550*38fd1498Szrj       clone->externally_visible = node->externally_visible;
551*38fd1498Szrj       clone->local = node->local;
552*38fd1498Szrj       clone->address_taken = node->address_taken;
553*38fd1498Szrj       clone->thunk = node->thunk;
554*38fd1498Szrj       clone->alias = node->alias;
555*38fd1498Szrj       clone->weakref = node->weakref;
556*38fd1498Szrj       clone->cpp_implicit_alias = node->cpp_implicit_alias;
557*38fd1498Szrj       clone->instrumented_version = node;
558*38fd1498Szrj       clone->orig_decl = fndecl;
559*38fd1498Szrj       clone->instrumentation_clone = true;
560*38fd1498Szrj       node->instrumented_version = clone;
561*38fd1498Szrj 
562*38fd1498Szrj       if (gimple_has_body_p (fndecl))
563*38fd1498Szrj 	{
564*38fd1498Szrj 	  gcc_assert (chkp_instrumentable_p (fndecl));
565*38fd1498Szrj 	  tree_function_versioning (fndecl, new_decl, NULL, false,
566*38fd1498Szrj 				    NULL, false, NULL, NULL);
567*38fd1498Szrj 	  clone->lowered = true;
568*38fd1498Szrj 	}
569*38fd1498Szrj 
570*38fd1498Szrj       /* New params are inserted after versioning because it
571*38fd1498Szrj 	 actually copies args list from the original decl.  */
572*38fd1498Szrj       chkp_add_bounds_params_to_function (new_decl);
573*38fd1498Szrj 
574*38fd1498Szrj       /* Remember builtin fndecl.  */
575*38fd1498Szrj       if (DECL_BUILT_IN_CLASS (clone->decl) == BUILT_IN_NORMAL
576*38fd1498Szrj 	  && fndecl == builtin_decl_explicit (DECL_FUNCTION_CODE (fndecl)))
577*38fd1498Szrj 	{
578*38fd1498Szrj 	  gcc_assert (!builtin_decl_explicit (DECL_FUNCTION_CODE (clone->decl)));
579*38fd1498Szrj 	  set_builtin_decl (DECL_FUNCTION_CODE (clone->decl),
580*38fd1498Szrj 			    clone->decl, false);
581*38fd1498Szrj 	}
582*38fd1498Szrj 
583*38fd1498Szrj       /* Clones have the same comdat group as originals.  */
584*38fd1498Szrj       if (node->same_comdat_group
585*38fd1498Szrj 	  || (DECL_ONE_ONLY (node->decl)
586*38fd1498Szrj 	      && !DECL_EXTERNAL (node->decl)))
587*38fd1498Szrj 	clone->add_to_same_comdat_group (node);
588*38fd1498Szrj 
589*38fd1498Szrj       if (gimple_has_body_p (fndecl))
590*38fd1498Szrj 	symtab->call_cgraph_insertion_hooks (clone);
591*38fd1498Szrj 
592*38fd1498Szrj       /* Clone all aliases.  */
593*38fd1498Szrj       for (i = 0; node->iterate_direct_aliases (i, ref); i++)
594*38fd1498Szrj 	chkp_maybe_create_clone (ref->referring->decl);
595*38fd1498Szrj 
596*38fd1498Szrj       /* Clone all thunks.  */
597*38fd1498Szrj       for (e = node->callers; e; e = e->next_caller)
598*38fd1498Szrj 	if (e->caller->thunk.thunk_p
599*38fd1498Szrj 	    && !e->caller->thunk.add_pointer_bounds_args
600*38fd1498Szrj 	    && !e->caller->instrumentation_clone)
601*38fd1498Szrj 	  {
602*38fd1498Szrj 	    struct cgraph_node *thunk
603*38fd1498Szrj 	      = chkp_maybe_create_clone (e->caller->decl);
604*38fd1498Szrj 	    /* Redirect thunk clone edge to the node clone.  */
605*38fd1498Szrj 	    thunk->callees->redirect_callee (clone);
606*38fd1498Szrj 	  }
607*38fd1498Szrj 
608*38fd1498Szrj       /* For aliases and thunks we should make sure target is cloned
609*38fd1498Szrj 	 to have proper references and edges.  */
610*38fd1498Szrj       if (node->thunk.thunk_p)
611*38fd1498Szrj 	chkp_maybe_create_clone (node->callees->callee->decl);
612*38fd1498Szrj       else if (node->alias)
613*38fd1498Szrj 	{
614*38fd1498Szrj 	  struct cgraph_node *target;
615*38fd1498Szrj 
616*38fd1498Szrj 	  ref = node->ref_list.first_reference ();
617*38fd1498Szrj 	  if (ref)
618*38fd1498Szrj 	    {
619*38fd1498Szrj 	      target = chkp_maybe_create_clone (ref->referred->decl);
620*38fd1498Szrj 	      clone->create_reference (target, IPA_REF_ALIAS);
621*38fd1498Szrj 	    }
622*38fd1498Szrj 
623*38fd1498Szrj 	  if (node->alias_target)
624*38fd1498Szrj 	    {
625*38fd1498Szrj 	      if (TREE_CODE (node->alias_target) == FUNCTION_DECL)
626*38fd1498Szrj 		{
627*38fd1498Szrj 		  target = chkp_maybe_create_clone (node->alias_target);
628*38fd1498Szrj 		  clone->alias_target = target->decl;
629*38fd1498Szrj 		}
630*38fd1498Szrj 	      else
631*38fd1498Szrj 		clone->alias_target = node->alias_target;
632*38fd1498Szrj 	    }
633*38fd1498Szrj 	}
634*38fd1498Szrj 
635*38fd1498Szrj       /* Add IPA reference.  It's main role is to keep instrumented
636*38fd1498Szrj 	 version reachable while original node is reachable.  */
637*38fd1498Szrj       ref = node->create_reference (clone, IPA_REF_CHKP, NULL);
638*38fd1498Szrj     }
639*38fd1498Szrj 
640*38fd1498Szrj   return clone;
641*38fd1498Szrj }
642*38fd1498Szrj 
643*38fd1498Szrj /* Create clone for all functions to be instrumented.  */
644*38fd1498Szrj 
645*38fd1498Szrj static unsigned int
chkp_versioning(void)646*38fd1498Szrj chkp_versioning (void)
647*38fd1498Szrj {
648*38fd1498Szrj   struct cgraph_node *node;
649*38fd1498Szrj   const char *reason;
650*38fd1498Szrj 
651*38fd1498Szrj   bitmap_obstack_initialize (NULL);
652*38fd1498Szrj 
653*38fd1498Szrj   FOR_EACH_DEFINED_FUNCTION (node)
654*38fd1498Szrj     {
655*38fd1498Szrj       tree decl = node->decl;
656*38fd1498Szrj       if (!node->instrumentation_clone
657*38fd1498Szrj 	  && !node->instrumented_version
658*38fd1498Szrj 	  && !node->alias
659*38fd1498Szrj 	  && !node->thunk.thunk_p
660*38fd1498Szrj 	  && (!DECL_BUILT_IN (decl)
661*38fd1498Szrj 	      || (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
662*38fd1498Szrj 		  && DECL_FUNCTION_CODE (decl) < BEGIN_CHKP_BUILTINS)))
663*38fd1498Szrj 	{
664*38fd1498Szrj 	  if (chkp_instrumentable_p (decl))
665*38fd1498Szrj 	    chkp_maybe_create_clone (decl);
666*38fd1498Szrj 	  else if ((reason = copy_forbidden (DECL_STRUCT_FUNCTION (decl))))
667*38fd1498Szrj 	    {
668*38fd1498Szrj 	      if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wchkp,
669*38fd1498Szrj 			      "function cannot be instrumented"))
670*38fd1498Szrj 		inform (DECL_SOURCE_LOCATION (decl), reason, decl);
671*38fd1498Szrj 	    }
672*38fd1498Szrj 	}
673*38fd1498Szrj     }
674*38fd1498Szrj 
675*38fd1498Szrj   /* Mark all aliases and thunks of functions with no instrumented
676*38fd1498Szrj      version as legacy function.  */
677*38fd1498Szrj   FOR_EACH_DEFINED_FUNCTION (node)
678*38fd1498Szrj     {
679*38fd1498Szrj       if (!node->instrumentation_clone
680*38fd1498Szrj 	  && !node->instrumented_version
681*38fd1498Szrj 	  && (node->alias || node->thunk.thunk_p)
682*38fd1498Szrj 	  && !lookup_attribute ("bnd_legacy", DECL_ATTRIBUTES (node->decl)))
683*38fd1498Szrj 	DECL_ATTRIBUTES (node->decl)
684*38fd1498Szrj 	  = tree_cons (get_identifier ("bnd_legacy"), NULL,
685*38fd1498Szrj 		       DECL_ATTRIBUTES (node->decl));
686*38fd1498Szrj     }
687*38fd1498Szrj 
688*38fd1498Szrj   bitmap_obstack_release (NULL);
689*38fd1498Szrj 
690*38fd1498Szrj   return 0;
691*38fd1498Szrj }
692*38fd1498Szrj 
693*38fd1498Szrj /* In this pass we remove bodies of functions having
694*38fd1498Szrj    instrumented version.  Functions with removed bodies
695*38fd1498Szrj    become a special kind of thunks to provide a connection
696*38fd1498Szrj    between calls to the original version and instrumented
697*38fd1498Szrj    function.  */
698*38fd1498Szrj 
699*38fd1498Szrj static unsigned int
chkp_produce_thunks(bool early)700*38fd1498Szrj chkp_produce_thunks (bool early)
701*38fd1498Szrj {
702*38fd1498Szrj   struct cgraph_node *node;
703*38fd1498Szrj 
704*38fd1498Szrj   FOR_EACH_DEFINED_FUNCTION (node)
705*38fd1498Szrj     {
706*38fd1498Szrj       if (!node->instrumentation_clone
707*38fd1498Szrj 	  && node->instrumented_version
708*38fd1498Szrj 	  && gimple_has_body_p (node->decl)
709*38fd1498Szrj 	  && gimple_has_body_p (node->instrumented_version->decl)
710*38fd1498Szrj 	  && (!lookup_attribute ("always_inline", DECL_ATTRIBUTES (node->decl))
711*38fd1498Szrj 	      || !early))
712*38fd1498Szrj 	{
713*38fd1498Szrj 	  node->release_body ();
714*38fd1498Szrj 	  node->remove_callees ();
715*38fd1498Szrj 	  node->remove_all_references ();
716*38fd1498Szrj 
717*38fd1498Szrj 	  node->thunk.thunk_p = true;
718*38fd1498Szrj 	  node->thunk.add_pointer_bounds_args = true;
719*38fd1498Szrj 	  node->create_edge (node->instrumented_version, NULL,
720*38fd1498Szrj 			     node->count);
721*38fd1498Szrj 	  node->create_reference (node->instrumented_version,
722*38fd1498Szrj 			       IPA_REF_CHKP, NULL);
723*38fd1498Szrj 	  /* Thunk shouldn't be a cdtor.  */
724*38fd1498Szrj 	  DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
725*38fd1498Szrj 	  DECL_STATIC_DESTRUCTOR (node->decl) = 0;
726*38fd1498Szrj 	}
727*38fd1498Szrj     }
728*38fd1498Szrj 
729*38fd1498Szrj   /* Mark instrumentation clones created for aliases and thunks
730*38fd1498Szrj      as insttrumented so they could be removed as unreachable
731*38fd1498Szrj      now.  */
732*38fd1498Szrj   if (!early)
733*38fd1498Szrj     {
734*38fd1498Szrj       FOR_EACH_DEFINED_FUNCTION (node)
735*38fd1498Szrj       {
736*38fd1498Szrj 	if (node->instrumentation_clone
737*38fd1498Szrj 	    && (node->alias || node->thunk.thunk_p)
738*38fd1498Szrj 	    && !chkp_function_instrumented_p (node->decl))
739*38fd1498Szrj 	  chkp_function_mark_instrumented (node->decl);
740*38fd1498Szrj       }
741*38fd1498Szrj     }
742*38fd1498Szrj 
743*38fd1498Szrj   return TODO_remove_functions;
744*38fd1498Szrj }
745*38fd1498Szrj 
746*38fd1498Szrj const pass_data pass_data_ipa_chkp_versioning =
747*38fd1498Szrj {
748*38fd1498Szrj   SIMPLE_IPA_PASS, /* type */
749*38fd1498Szrj   "chkp_versioning", /* name */
750*38fd1498Szrj   OPTGROUP_NONE, /* optinfo_flags */
751*38fd1498Szrj   TV_NONE, /* tv_id */
752*38fd1498Szrj   0, /* properties_required */
753*38fd1498Szrj   0, /* properties_provided */
754*38fd1498Szrj   0, /* properties_destroyed */
755*38fd1498Szrj   0, /* todo_flags_start */
756*38fd1498Szrj   0 /* todo_flags_finish */
757*38fd1498Szrj };
758*38fd1498Szrj 
759*38fd1498Szrj const pass_data pass_data_ipa_chkp_early_produce_thunks =
760*38fd1498Szrj {
761*38fd1498Szrj   SIMPLE_IPA_PASS, /* type */
762*38fd1498Szrj   "chkp_ecleanup", /* name */
763*38fd1498Szrj   OPTGROUP_NONE, /* optinfo_flags */
764*38fd1498Szrj   TV_NONE, /* tv_id */
765*38fd1498Szrj   0, /* properties_required */
766*38fd1498Szrj   0, /* properties_provided */
767*38fd1498Szrj   0, /* properties_destroyed */
768*38fd1498Szrj   0, /* todo_flags_start */
769*38fd1498Szrj   0 /* todo_flags_finish */
770*38fd1498Szrj };
771*38fd1498Szrj 
772*38fd1498Szrj const pass_data pass_data_ipa_chkp_produce_thunks =
773*38fd1498Szrj {
774*38fd1498Szrj   SIMPLE_IPA_PASS, /* type */
775*38fd1498Szrj   "chkp_cleanup", /* name */
776*38fd1498Szrj   OPTGROUP_NONE, /* optinfo_flags */
777*38fd1498Szrj   TV_NONE, /* tv_id */
778*38fd1498Szrj   0, /* properties_required */
779*38fd1498Szrj   0, /* properties_provided */
780*38fd1498Szrj   0, /* properties_destroyed */
781*38fd1498Szrj   0, /* todo_flags_start */
782*38fd1498Szrj   0 /* todo_flags_finish */
783*38fd1498Szrj };
784*38fd1498Szrj 
785*38fd1498Szrj class pass_ipa_chkp_versioning : public simple_ipa_opt_pass
786*38fd1498Szrj {
787*38fd1498Szrj public:
pass_ipa_chkp_versioning(gcc::context * ctxt)788*38fd1498Szrj   pass_ipa_chkp_versioning (gcc::context *ctxt)
789*38fd1498Szrj     : simple_ipa_opt_pass (pass_data_ipa_chkp_versioning, ctxt)
790*38fd1498Szrj   {}
791*38fd1498Szrj 
792*38fd1498Szrj   /* opt_pass methods: */
clone()793*38fd1498Szrj   virtual opt_pass * clone ()
794*38fd1498Szrj     {
795*38fd1498Szrj       return new pass_ipa_chkp_versioning (m_ctxt);
796*38fd1498Szrj     }
797*38fd1498Szrj 
gate(function *)798*38fd1498Szrj   virtual bool gate (function *)
799*38fd1498Szrj     {
800*38fd1498Szrj       return flag_check_pointer_bounds;
801*38fd1498Szrj     }
802*38fd1498Szrj 
execute(function *)803*38fd1498Szrj   virtual unsigned int execute (function *)
804*38fd1498Szrj     {
805*38fd1498Szrj       return chkp_versioning ();
806*38fd1498Szrj     }
807*38fd1498Szrj 
808*38fd1498Szrj }; // class pass_ipa_chkp_versioning
809*38fd1498Szrj 
810*38fd1498Szrj class pass_ipa_chkp_early_produce_thunks : public simple_ipa_opt_pass
811*38fd1498Szrj {
812*38fd1498Szrj public:
pass_ipa_chkp_early_produce_thunks(gcc::context * ctxt)813*38fd1498Szrj   pass_ipa_chkp_early_produce_thunks (gcc::context *ctxt)
814*38fd1498Szrj     : simple_ipa_opt_pass (pass_data_ipa_chkp_early_produce_thunks, ctxt)
815*38fd1498Szrj   {}
816*38fd1498Szrj 
817*38fd1498Szrj   /* opt_pass methods: */
clone()818*38fd1498Szrj   virtual opt_pass * clone ()
819*38fd1498Szrj     {
820*38fd1498Szrj       return new pass_ipa_chkp_early_produce_thunks (m_ctxt);
821*38fd1498Szrj     }
822*38fd1498Szrj 
gate(function *)823*38fd1498Szrj   virtual bool gate (function *)
824*38fd1498Szrj     {
825*38fd1498Szrj       return flag_check_pointer_bounds;
826*38fd1498Szrj     }
827*38fd1498Szrj 
execute(function *)828*38fd1498Szrj   virtual unsigned int execute (function *)
829*38fd1498Szrj     {
830*38fd1498Szrj       return chkp_produce_thunks (true);
831*38fd1498Szrj     }
832*38fd1498Szrj 
833*38fd1498Szrj }; // class pass_chkp_produce_thunks
834*38fd1498Szrj 
835*38fd1498Szrj class pass_ipa_chkp_produce_thunks : public simple_ipa_opt_pass
836*38fd1498Szrj {
837*38fd1498Szrj public:
pass_ipa_chkp_produce_thunks(gcc::context * ctxt)838*38fd1498Szrj   pass_ipa_chkp_produce_thunks (gcc::context *ctxt)
839*38fd1498Szrj     : simple_ipa_opt_pass (pass_data_ipa_chkp_produce_thunks, ctxt)
840*38fd1498Szrj   {}
841*38fd1498Szrj 
842*38fd1498Szrj   /* opt_pass methods: */
clone()843*38fd1498Szrj   virtual opt_pass * clone ()
844*38fd1498Szrj     {
845*38fd1498Szrj       return new pass_ipa_chkp_produce_thunks (m_ctxt);
846*38fd1498Szrj     }
847*38fd1498Szrj 
gate(function *)848*38fd1498Szrj   virtual bool gate (function *)
849*38fd1498Szrj     {
850*38fd1498Szrj       return flag_check_pointer_bounds;
851*38fd1498Szrj     }
852*38fd1498Szrj 
execute(function *)853*38fd1498Szrj   virtual unsigned int execute (function *)
854*38fd1498Szrj     {
855*38fd1498Szrj       return chkp_produce_thunks (false);
856*38fd1498Szrj     }
857*38fd1498Szrj 
858*38fd1498Szrj }; // class pass_chkp_produce_thunks
859*38fd1498Szrj 
860*38fd1498Szrj simple_ipa_opt_pass *
make_pass_ipa_chkp_versioning(gcc::context * ctxt)861*38fd1498Szrj make_pass_ipa_chkp_versioning (gcc::context *ctxt)
862*38fd1498Szrj {
863*38fd1498Szrj   return new pass_ipa_chkp_versioning (ctxt);
864*38fd1498Szrj }
865*38fd1498Szrj 
866*38fd1498Szrj simple_ipa_opt_pass *
make_pass_ipa_chkp_early_produce_thunks(gcc::context * ctxt)867*38fd1498Szrj make_pass_ipa_chkp_early_produce_thunks (gcc::context *ctxt)
868*38fd1498Szrj {
869*38fd1498Szrj   return new pass_ipa_chkp_early_produce_thunks (ctxt);
870*38fd1498Szrj }
871*38fd1498Szrj 
872*38fd1498Szrj simple_ipa_opt_pass *
make_pass_ipa_chkp_produce_thunks(gcc::context * ctxt)873*38fd1498Szrj make_pass_ipa_chkp_produce_thunks (gcc::context *ctxt)
874*38fd1498Szrj {
875*38fd1498Szrj   return new pass_ipa_chkp_produce_thunks (ctxt);
876*38fd1498Szrj }
877