summaryrefslogtreecommitdiff
path: root/gcc/rtl-chkp.c
blob: 2d56ba29f4efd3362da7cac6d5367234a4aed485 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* RTL manipulation functions exported by Pointer Bounds Checker.
   Copyright (C) 2014 Free Software Foundation, Inc.
   Contributed by Ilya Enkovich (ilya.enkovich@intel.com)

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "expr.h"
#include "target.h"
#include "tree-ssa-alias.h"
#include "internal-fn.h"
#include "is-a.h"
#include "predict.h"
#include "basic-block.h"
#include "tree.h"
#include "gimple-expr.h"
#include "gimple.h"
#include "bitmap.h"
#include "rtl-chkp.h"
#include "tree-chkp.h"
#include "hash-map.h"

static hash_map<tree, rtx> *chkp_rtx_bounds_map;

/* Get bounds rtx associated with NODE via
   chkp_set_rtl_bounds call.  */
rtx
chkp_get_rtl_bounds (tree node)
{
  rtx *slot;

  if (!chkp_rtx_bounds_map)
    return NULL_RTX;

  slot = chkp_rtx_bounds_map->get (node);
  return slot ? *slot : NULL_RTX;
}

/* Associate bounds rtx VAL with NODE.  */
void
chkp_set_rtl_bounds (tree node, rtx val)
{
  if (!chkp_rtx_bounds_map)
    chkp_rtx_bounds_map = new hash_map<tree, rtx>;

  chkp_rtx_bounds_map->put (node, val);
}

/* Reset all bounds stored via chkp_set_rtl_bounds.  */
void
chkp_reset_rtl_bounds ()
{
  if (!chkp_rtx_bounds_map)
    return;

  delete chkp_rtx_bounds_map;
  chkp_rtx_bounds_map = NULL;
}

/* Split SLOT identifying slot for function value or
   argument into two parts SLOT_VAL and SLOT_BND.
   First is the slot for regular value and the other one is
   for bounds.  */
void
chkp_split_slot (rtx slot, rtx *slot_val, rtx *slot_bnd)
{
  int i;
  int val_num = 0;
  int bnd_num = 0;
  rtx *val_tmps;
  rtx *bnd_tmps;

  *slot_bnd = 0;

  if (!slot
      || GET_CODE (slot) != PARALLEL)
    {
      *slot_val = slot;
      return;
    }

  val_tmps = XALLOCAVEC (rtx, XVECLEN (slot, 0));
  bnd_tmps = XALLOCAVEC (rtx, XVECLEN (slot, 0));

  for (i = 0; i < XVECLEN (slot, 0); i++)
    {
      rtx elem = XVECEXP (slot, 0, i);
      rtx reg = GET_CODE (elem) == EXPR_LIST ? XEXP (elem, 0) : elem;

      if (!reg)
	continue;

      if (POINTER_BOUNDS_MODE_P (GET_MODE (reg)) || CONST_INT_P (reg))
	bnd_tmps[bnd_num++] = elem;
      else
	val_tmps[val_num++] = elem;
    }

  gcc_assert (val_num);

  if (!bnd_num)
    {
      *slot_val = slot;
      return;
    }

  if ((GET_CODE (val_tmps[0]) == EXPR_LIST) || (val_num > 1))
    *slot_val = gen_rtx_PARALLEL (GET_MODE (slot),
				  gen_rtvec_v (val_num, val_tmps));
  else
    *slot_val = val_tmps[0];

  if ((GET_CODE (bnd_tmps[0]) == EXPR_LIST) || (bnd_num > 1))
    *slot_bnd = gen_rtx_PARALLEL (VOIDmode,
				  gen_rtvec_v (bnd_num, bnd_tmps));
  else
    *slot_bnd = bnd_tmps[0];
}

/* Join previously splitted to VAL and BND rtx for function
   value or argument and return it.  */
rtx
chkp_join_splitted_slot (rtx val, rtx bnd)
{
  rtx res;
  int i, n = 0;

  if (!bnd)
    return val;

  if (GET_CODE (val) == PARALLEL)
    n += XVECLEN (val, 0);
  else
    n++;

  if (GET_CODE (bnd) == PARALLEL)
    n += XVECLEN (bnd, 0);
  else
    n++;

  res = gen_rtx_PARALLEL (GET_MODE (val), rtvec_alloc (n));

  n = 0;

  if (GET_CODE (val) == PARALLEL)
    for (i = 0; i < XVECLEN (val, 0); i++)
      XVECEXP (res, 0, n++) = XVECEXP (val, 0, i);
  else
    XVECEXP (res, 0, n++) = val;

  if (GET_CODE (bnd) == PARALLEL)
    for (i = 0; i < XVECLEN (bnd, 0); i++)
      XVECEXP (res, 0, n++) = XVECEXP (bnd, 0, i);
  else
    XVECEXP (res, 0, n++) = bnd;

  return res;
}

/* If PAR is PARALLEL holding registers then transform
   it into PARALLEL holding EXPR_LISTs of those regs
   and zero constant (similar to how function value
   on multiple registers looks like).  */
void
chkp_put_regs_to_expr_list (rtx par)
{
  int n;

  if (GET_CODE (par) != PARALLEL
      || GET_CODE (XVECEXP (par, 0, 0)) == EXPR_LIST)
    return;

  for (n = 0; n < XVECLEN (par, 0); n++)
    XVECEXP (par, 0, n) = gen_rtx_EXPR_LIST (VOIDmode,
					     XVECEXP (par, 0, n),
					     const0_rtx);
}

/*  Search rtx PAR describing function return value for an
    item related to value at offset OFFS and return it.
    Return NULL if item was not found.  */
rtx
chkp_get_value_with_offs (rtx par, rtx offs)
{
  int n;

  gcc_assert (GET_CODE (par) == PARALLEL);

  for (n = 0; n < XVECLEN (par, 0); n++)
    {
      rtx par_offs = XEXP (XVECEXP (par, 0, n), 1);
      if (INTVAL (offs) == INTVAL (par_offs))
	return XEXP (XVECEXP (par, 0, n), 0);
    }

  return NULL;
}

/* Emit instructions to store BOUNDS for pointer VALUE
   stored in MEM.
   Function is used by expand to pass bounds for args
   passed on stack.  */
void
chkp_emit_bounds_store (rtx bounds, rtx value, rtx mem)
{
  gcc_assert (MEM_P (mem));

  if (REG_P (bounds) || CONST_INT_P (bounds))
    {
      rtx ptr;

      if (REG_P (value))
	ptr = value;
      else
	{
	  rtx slot = adjust_address (value, Pmode, 0);
	  ptr = gen_reg_rtx (Pmode);
	  emit_move_insn (ptr, slot);
	}

      if (CONST_INT_P (bounds))
	bounds = targetm.calls.load_bounds_for_arg (value, ptr, bounds);

      targetm.calls.store_bounds_for_arg (ptr, mem,
					  bounds, NULL);
    }
  else
    {
      int i;

      gcc_assert (GET_CODE (bounds) == PARALLEL);
      gcc_assert (GET_CODE (value) == PARALLEL || MEM_P (value) || REG_P (value));

      for (i = 0; i < XVECLEN (bounds, 0); i++)
	{
	  rtx reg = XEXP (XVECEXP (bounds, 0, i), 0);
	  rtx offs = XEXP (XVECEXP (bounds, 0, i), 1);
	  rtx slot = adjust_address (mem, Pmode, INTVAL (offs));
	  rtx ptr;

	  if (GET_CODE (value) == PARALLEL)
	    ptr = chkp_get_value_with_offs (value, offs);
	  else if (MEM_P (value))
	    {
	      rtx tmp = adjust_address (value, Pmode, INTVAL (offs));
	      ptr = gen_reg_rtx (Pmode);
	      emit_move_insn (ptr, tmp);
	    }
	  else
	    ptr = gen_rtx_SUBREG (Pmode, value, INTVAL (offs));

	  targetm.calls.store_bounds_for_arg (ptr, slot, reg, NULL);
	}
    }
}

/* Emit code to copy bounds for structure VALUE of type TYPE
   copied to SLOT.  */
void
chkp_copy_bounds_for_stack_parm (rtx slot, rtx value, tree type)
{
  bitmap have_bound;
  bitmap_iterator bi;
  unsigned i;
  rtx tmp = NULL, bnd;

  gcc_assert (TYPE_SIZE (type));
  gcc_assert (MEM_P (value));
  gcc_assert (MEM_P (slot));
  gcc_assert (RECORD_OR_UNION_TYPE_P (type));

  bitmap_obstack_initialize (NULL);
  have_bound = BITMAP_ALLOC (NULL);
  chkp_find_bound_slots (type, have_bound);

  EXECUTE_IF_SET_IN_BITMAP (have_bound, 0, i, bi)
    {
      rtx ptr = adjust_address (value, Pmode, i * POINTER_SIZE / 8);
      rtx to = adjust_address (slot, Pmode, i * POINTER_SIZE / 8);

      if (!tmp)
	tmp = gen_reg_rtx (Pmode);

      emit_move_insn (tmp, ptr);
      bnd = targetm.calls.load_bounds_for_arg (ptr, tmp, NULL);
      targetm.calls.store_bounds_for_arg (tmp, to, bnd, NULL);
    }

  BITMAP_FREE (have_bound);
  bitmap_obstack_release (NULL);
}