LCOV - code coverage report
Current view: top level - gcc/c-family - c-ubsan.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.1 % 331 318
Test Date: 2024-11-30 13:30:02 Functions: 100.0 % 11 11
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* UndefinedBehaviorSanitizer, undefined behavior detector.
       2                 :             :    Copyright (C) 2013-2024 Free Software Foundation, Inc.
       3                 :             :    Contributed by Marek Polacek <polacek@redhat.com>
       4                 :             : 
       5                 :             : This file is part of GCC.
       6                 :             : 
       7                 :             : GCC is free software; you can redistribute it and/or modify it under
       8                 :             : the terms of the GNU General Public License as published by the Free
       9                 :             : Software Foundation; either version 3, or (at your option) any later
      10                 :             : version.
      11                 :             : 
      12                 :             : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13                 :             : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14                 :             : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15                 :             : for more details.
      16                 :             : 
      17                 :             : You should have received a copy of the GNU General Public License
      18                 :             : along with GCC; see the file COPYING3.  If not see
      19                 :             : <http://www.gnu.org/licenses/>.  */
      20                 :             : 
      21                 :             : #include "config.h"
      22                 :             : #include "system.h"
      23                 :             : #include "coretypes.h"
      24                 :             : #include "tm.h"
      25                 :             : #include "c-family/c-common.h"
      26                 :             : #include "ubsan.h"
      27                 :             : #include "c-family/c-ubsan.h"
      28                 :             : #include "stor-layout.h"
      29                 :             : #include "builtins.h"
      30                 :             : #include "gimplify.h"
      31                 :             : #include "stringpool.h"
      32                 :             : #include "attribs.h"
      33                 :             : #include "asan.h"
      34                 :             : #include "langhooks.h"
      35                 :             : 
      36                 :             : /* Instrument division by zero and INT_MIN / -1.  If not instrumenting,
      37                 :             :    return NULL_TREE.  */
      38                 :             : 
      39                 :             : tree
      40                 :        1385 : ubsan_instrument_division (location_t loc, tree op0, tree op1)
      41                 :             : {
      42                 :        1385 :   tree t, tt, x = NULL_TREE;
      43                 :        1385 :   tree type = TREE_TYPE (op0);
      44                 :        1385 :   enum sanitize_code flag = SANITIZE_DIVIDE;
      45                 :             : 
      46                 :             :   /* At this point both operands should have the same type,
      47                 :             :      because they are already converted to RESULT_TYPE.
      48                 :             :      Use TYPE_MAIN_VARIANT since typedefs can confuse us.  */
      49                 :        1385 :   tree top0 = TYPE_MAIN_VARIANT (type);
      50                 :        1385 :   tree top1 = TYPE_MAIN_VARIANT (TREE_TYPE (op1));
      51                 :        1385 :   gcc_checking_assert (lang_hooks.types_compatible_p (top0, top1));
      52                 :             : 
      53                 :        1385 :   op0 = unshare_expr (op0);
      54                 :        1385 :   op1 = unshare_expr (op1);
      55                 :             : 
      56                 :        1385 :   if (INTEGRAL_TYPE_P (type)
      57                 :        1385 :       && sanitize_flags_p (SANITIZE_DIVIDE))
      58                 :         909 :     t = fold_build2 (EQ_EXPR, boolean_type_node,
      59                 :             :                      op1, build_int_cst (type, 0));
      60                 :         476 :   else if (SCALAR_FLOAT_TYPE_P (type)
      61                 :         476 :            && sanitize_flags_p (SANITIZE_FLOAT_DIVIDE))
      62                 :             :     {
      63                 :          96 :       t = fold_build2 (EQ_EXPR, boolean_type_node,
      64                 :             :                        op1, build_real (type, dconst0));
      65                 :          96 :       flag = SANITIZE_FLOAT_DIVIDE;
      66                 :             :     }
      67                 :             :   else
      68                 :             :     t = NULL_TREE;
      69                 :             : 
      70                 :             :   /* We check INT_MIN / -1 only for signed types.  */
      71                 :        1385 :   if (INTEGRAL_TYPE_P (type)
      72                 :        1174 :       && sanitize_flags_p (SANITIZE_SI_OVERFLOW)
      73                 :        2284 :       && !TYPE_UNSIGNED (type))
      74                 :             :     {
      75                 :         753 :       tt = fold_build2 (EQ_EXPR, boolean_type_node, unshare_expr (op1),
      76                 :             :                         build_int_cst (type, -1));
      77                 :         753 :       x = fold_build2 (EQ_EXPR, boolean_type_node, op0,
      78                 :             :                        TYPE_MIN_VALUE (type));
      79                 :         753 :       x = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, x, tt);
      80                 :         753 :       if (t == NULL_TREE || integer_zerop (t))
      81                 :             :         {
      82                 :             :           t = x;
      83                 :             :           x = NULL_TREE;
      84                 :             :           flag = SANITIZE_SI_OVERFLOW;
      85                 :             :         }
      86                 :         301 :       else if ((((flag_sanitize_trap & SANITIZE_DIVIDE) == 0)
      87                 :         301 :                 == ((flag_sanitize_trap & SANITIZE_SI_OVERFLOW) == 0))
      88                 :         301 :                && (((flag_sanitize_recover & SANITIZE_DIVIDE) == 0)
      89                 :         301 :                    == ((flag_sanitize_recover & SANITIZE_SI_OVERFLOW) == 0)))
      90                 :             :         {
      91                 :         260 :           t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, x);
      92                 :         260 :           x = NULL_TREE;
      93                 :             :         }
      94                 :          41 :       else if (integer_zerop (x))
      95                 :         496 :         x = NULL_TREE;
      96                 :             :     }
      97                 :         632 :   else if (t == NULL_TREE)
      98                 :             :     return NULL_TREE;
      99                 :             : 
     100                 :             :   /* If the condition was folded to 0, no need to instrument
     101                 :             :      this expression.  */
     102                 :        1249 :   if (integer_zerop (t))
     103                 :             :     return NULL_TREE;
     104                 :             : 
     105                 :             :   /* In case we have a SAVE_EXPR in a conditional context, we need to
     106                 :             :      make sure it gets evaluated before the condition.  */
     107                 :         777 :   t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op0), t);
     108                 :         777 :   t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op1), t);
     109                 :         777 :   if ((flag_sanitize_trap & flag) && x == NULL_TREE)
     110                 :          10 :     tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
     111                 :             :   else
     112                 :             :     {
     113                 :         767 :       tree data = ubsan_create_data ("__ubsan_overflow_data", 1, &loc,
     114                 :             :                                      ubsan_type_descriptor (type), NULL_TREE,
     115                 :             :                                      NULL_TREE);
     116                 :         767 :       data = build_fold_addr_expr_loc (loc, data);
     117                 :         767 :       if (flag_sanitize_trap & flag)
     118                 :           0 :         tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP),
     119                 :             :                                   0);
     120                 :             :       else
     121                 :             :         {
     122                 :        1534 :           enum built_in_function bcode
     123                 :         767 :             = (flag_sanitize_recover & flag)
     124                 :         767 :               ? BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW
     125                 :             :               : BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW_ABORT;
     126                 :         767 :           tt = builtin_decl_explicit (bcode);
     127                 :         767 :           op0 = unshare_expr (op0);
     128                 :         767 :           op1 = unshare_expr (op1);
     129                 :         767 :           tt = build_call_expr_loc (loc, tt, 3, data, ubsan_encode_value (op0),
     130                 :             :                                     ubsan_encode_value (op1));
     131                 :             :         }
     132                 :         767 :       if (x)
     133                 :             :         {
     134                 :          41 :           tree xt;
     135                 :          41 :           if (flag_sanitize_trap & SANITIZE_SI_OVERFLOW)
     136                 :           0 :             xt = build_call_expr_loc (loc,
     137                 :             :                                       builtin_decl_explicit (BUILT_IN_TRAP),
     138                 :             :                                       0);
     139                 :             :           else
     140                 :             :             {
     141                 :          82 :               enum built_in_function bcode
     142                 :          41 :                 = (flag_sanitize_recover & SANITIZE_SI_OVERFLOW)
     143                 :          41 :                    ? BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW
     144                 :             :                    : BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW_ABORT;
     145                 :          41 :               xt = builtin_decl_explicit (bcode);
     146                 :          41 :               op0 = unshare_expr (op0);
     147                 :          41 :               op1 = unshare_expr (op1);
     148                 :          41 :               xt = build_call_expr_loc (loc, xt, 3, data,
     149                 :             :                                         ubsan_encode_value (op0),
     150                 :             :                                         ubsan_encode_value (op1));
     151                 :             :             }
     152                 :          41 :           x = fold_build3 (COND_EXPR, void_type_node, x, xt, void_node);
     153                 :             :         }
     154                 :             :     }
     155                 :         777 :   t = fold_build3 (COND_EXPR, void_type_node, t, tt, x ? x : void_node);
     156                 :             : 
     157                 :         777 :   return t;
     158                 :             : }
     159                 :             : 
     160                 :             : /* Instrument left and right shifts.  */
     161                 :             : 
     162                 :             : tree
     163                 :        2156 : ubsan_instrument_shift (location_t loc, enum tree_code code,
     164                 :             :                         tree op0, tree op1)
     165                 :             : {
     166                 :        2156 :   tree t, tt = NULL_TREE;
     167                 :        2156 :   tree type0 = TREE_TYPE (op0);
     168                 :        2156 :   tree type1 = TREE_TYPE (op1);
     169                 :        2156 :   if (!INTEGRAL_TYPE_P (type0))
     170                 :             :     return NULL_TREE;
     171                 :             : 
     172                 :        2142 :   tree op1_utype = unsigned_type_for (type1);
     173                 :        2142 :   HOST_WIDE_INT op0_prec = TYPE_PRECISION (type0);
     174                 :        2142 :   tree uprecm1 = build_int_cst (op1_utype, op0_prec - 1);
     175                 :             : 
     176                 :        2142 :   op0 = unshare_expr (op0);
     177                 :        2142 :   op1 = unshare_expr (op1);
     178                 :             : 
     179                 :        2142 :   if (code == LROTATE_EXPR || code == RROTATE_EXPR)
     180                 :             :     {
     181                 :             :       /* For rotates just check for negative op1.  */
     182                 :          28 :       if (TYPE_UNSIGNED (type1))
     183                 :             :         return NULL_TREE;
     184                 :          28 :       t = fold_build2 (LT_EXPR, boolean_type_node, op1,
     185                 :             :                        build_int_cst (type1, 0));
     186                 :             :     }
     187                 :             :   else
     188                 :             :     {
     189                 :        2114 :       t = fold_convert_loc (loc, op1_utype, op1);
     190                 :        2114 :       t = fold_build2 (GT_EXPR, boolean_type_node, t, uprecm1);
     191                 :             :     }
     192                 :             : 
     193                 :             :   /* If this is not a signed operation, don't perform overflow checks.
     194                 :             :      Also punt on bit-fields.  */
     195                 :        2142 :   if (TYPE_OVERFLOW_WRAPS (type0)
     196                 :        2764 :       || maybe_ne (GET_MODE_BITSIZE (TYPE_MODE (type0)),
     197                 :        1382 :                    TYPE_PRECISION (type0))
     198                 :        1302 :       || !sanitize_flags_p (SANITIZE_SHIFT_BASE)
     199                 :             :       /* In C++20 and later, shifts are well defined except when
     200                 :             :          the second operand is not within bounds.  */
     201                 :        1251 :       || cxx_dialect >= cxx20)
     202                 :             :     ;
     203                 :             : 
     204                 :             :   /* For signed x << y, in C99 and later, the following:
     205                 :             :      (unsigned) x >> (uprecm1 - y)
     206                 :             :      if non-zero, is undefined.  */
     207                 :        1151 :   else if (code == LSHIFT_EXPR && flag_isoc99 && cxx_dialect < cxx11)
     208                 :             :     {
     209                 :         766 :       tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
     210                 :             :                             fold_convert (op1_utype, unshare_expr (op1)));
     211                 :         766 :       tt = fold_convert_loc (loc, unsigned_type_for (type0), op0);
     212                 :         766 :       tt = fold_build2 (RSHIFT_EXPR, TREE_TYPE (tt), tt, x);
     213                 :         766 :       tt = fold_build2 (NE_EXPR, boolean_type_node, tt,
     214                 :             :                         build_int_cst (TREE_TYPE (tt), 0));
     215                 :         766 :     }
     216                 :             : 
     217                 :             :   /* For signed x << y, in C++11 to C++17, the following:
     218                 :             :      x < 0 || ((unsigned) x >> (uprecm1 - y))
     219                 :             :      if > 1, is undefined.  */
     220                 :         134 :   else if (code == LSHIFT_EXPR && cxx_dialect >= cxx11)
     221                 :             :     {
     222                 :          74 :       tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
     223                 :             :                             fold_convert (op1_utype, unshare_expr (op1)));
     224                 :          74 :       tt = fold_convert_loc (loc, unsigned_type_for (type0),
     225                 :             :                              unshare_expr (op0));
     226                 :          74 :       tt = fold_build2 (RSHIFT_EXPR, TREE_TYPE (tt), tt, x);
     227                 :          74 :       tt = fold_build2 (GT_EXPR, boolean_type_node, tt,
     228                 :             :                         build_int_cst (TREE_TYPE (tt), 1));
     229                 :          74 :       x = fold_build2 (LT_EXPR, boolean_type_node, unshare_expr (op0),
     230                 :             :                        build_int_cst (type0, 0));
     231                 :          74 :       tt = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, x, tt);
     232                 :             :     }
     233                 :             : 
     234                 :             :   /* If the condition was folded to 0, no need to instrument
     235                 :             :      this expression.  */
     236                 :        2142 :   if (integer_zerop (t) && (tt == NULL_TREE || integer_zerop (tt)))
     237                 :        1111 :     return NULL_TREE;
     238                 :             : 
     239                 :             :   /* In case we have a SAVE_EXPR in a conditional context, we need to
     240                 :             :      make sure it gets evaluated before the condition.  */
     241                 :        1031 :   t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op0), t);
     242                 :        1031 :   t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op1), t);
     243                 :             : 
     244                 :        1031 :   enum sanitize_code recover_kind = SANITIZE_SHIFT_EXPONENT;
     245                 :        1031 :   tree else_t = void_node;
     246                 :        1031 :   if (tt)
     247                 :             :     {
     248                 :         530 :       if (!sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
     249                 :             :         {
     250                 :          28 :           t = fold_build1 (TRUTH_NOT_EXPR, boolean_type_node, t);
     251                 :          28 :           t = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, t, tt);
     252                 :          28 :           recover_kind = SANITIZE_SHIFT_BASE;
     253                 :             :         }
     254                 :             :       else
     255                 :             :         {
     256                 :         502 :           if (((!(flag_sanitize_trap & SANITIZE_SHIFT_EXPONENT))
     257                 :         502 :                == (!(flag_sanitize_trap & SANITIZE_SHIFT_BASE)))
     258                 :         502 :               && ((!(flag_sanitize_recover & SANITIZE_SHIFT_EXPONENT))
     259                 :         502 :                   == (!(flag_sanitize_recover & SANITIZE_SHIFT_BASE))))
     260                 :         435 :             t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, tt);
     261                 :             :           else
     262                 :             :             else_t = tt;
     263                 :             :         }
     264                 :             :     }
     265                 :             : 
     266                 :        1031 :   if ((flag_sanitize_trap & recover_kind) && else_t == void_node)
     267                 :           0 :     tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
     268                 :             :   else
     269                 :             :     {
     270                 :        1031 :       if (TREE_CODE (type1) == BITINT_TYPE
     271                 :        1063 :           && TYPE_PRECISION (type1) > MAX_FIXED_MODE_SIZE)
     272                 :             :         {
     273                 :             :           /* Workaround for missing _BitInt support in libsanitizer.
     274                 :             :              Instead of crashing in the library, pretend values above
     275                 :             :              maximum value of normal integral type or below minimum value
     276                 :             :              of that type are those extremes.  */
     277                 :          32 :           tree type2 = build_nonstandard_integer_type (MAX_FIXED_MODE_SIZE,
     278                 :          32 :                                                        TYPE_UNSIGNED (type1));
     279                 :          32 :           tree op2 = op1;
     280                 :          32 :           if (!TYPE_UNSIGNED (type1))
     281                 :             :             {
     282                 :          32 :               op2 = fold_build2 (LT_EXPR, boolean_type_node, unshare_expr (op1),
     283                 :             :                                  fold_convert (type1, TYPE_MIN_VALUE (type2)));
     284                 :          32 :               op2 = fold_build3 (COND_EXPR, type2, op2, TYPE_MIN_VALUE (type2),
     285                 :             :                                  fold_convert (type2, unshare_expr (op1)));
     286                 :             :             }
     287                 :             :           else
     288                 :           0 :             op2 = fold_convert (type2, op1);
     289                 :          32 :           tree op3
     290                 :          32 :             = fold_build2 (GT_EXPR, boolean_type_node, unshare_expr (op1),
     291                 :             :                            fold_convert (type1, TYPE_MAX_VALUE (type2)));
     292                 :          32 :           op1 = fold_build3 (COND_EXPR, type2, op3, TYPE_MAX_VALUE (type2),
     293                 :             :                              op2);
     294                 :          32 :           type1 = type2;
     295                 :             :         }
     296                 :        1031 :       tree utd0 = ubsan_type_descriptor (type0, UBSAN_PRINT_FORCE_INT);
     297                 :        1031 :       tree data = ubsan_create_data ("__ubsan_shift_data", 1, &loc, utd0,
     298                 :             :                                      ubsan_type_descriptor (type1), NULL_TREE,
     299                 :             :                                      NULL_TREE);
     300                 :        1031 :       data = build_fold_addr_expr_loc (loc, data);
     301                 :             : 
     302                 :        1031 :       if (flag_sanitize_trap & recover_kind)
     303                 :           0 :         tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
     304                 :             :       else
     305                 :             :         {
     306                 :        2062 :           enum built_in_function bcode
     307                 :        1031 :             = (flag_sanitize_recover & recover_kind)
     308                 :        1031 :               ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
     309                 :             :               : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT;
     310                 :        1031 :           tt = builtin_decl_explicit (bcode);
     311                 :        1031 :           op0 = unshare_expr (op0);
     312                 :        1031 :           op1 = unshare_expr (op1);
     313                 :        1031 :           tt = build_call_expr_loc (loc, tt, 3, data, ubsan_encode_value (op0),
     314                 :             :                                     ubsan_encode_value (op1));
     315                 :             :         }
     316                 :        1031 :       if (else_t != void_node)
     317                 :             :         {
     318                 :          67 :           tree else_tt;
     319                 :          67 :           if (flag_sanitize_trap & SANITIZE_SHIFT_BASE)
     320                 :           0 :             else_tt
     321                 :           0 :               = build_call_expr_loc (loc,
     322                 :             :                                      builtin_decl_explicit (BUILT_IN_TRAP), 0);
     323                 :             :           else
     324                 :             :             {
     325                 :         134 :               enum built_in_function bcode
     326                 :          67 :                 = (flag_sanitize_recover & SANITIZE_SHIFT_BASE)
     327                 :          67 :                   ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
     328                 :             :                   : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT;
     329                 :          67 :               else_tt = builtin_decl_explicit (bcode);
     330                 :          67 :               op0 = unshare_expr (op0);
     331                 :          67 :               op1 = unshare_expr (op1);
     332                 :          67 :               else_tt = build_call_expr_loc (loc, else_tt, 3, data,
     333                 :             :                                              ubsan_encode_value (op0),
     334                 :             :                                              ubsan_encode_value (op1));
     335                 :             :             }
     336                 :          67 :           else_t = fold_build3 (COND_EXPR, void_type_node, else_t,
     337                 :             :                                 else_tt, void_node);
     338                 :             :         }
     339                 :             :     }
     340                 :        1031 :   t = fold_build3 (COND_EXPR, void_type_node, t, tt, else_t);
     341                 :             : 
     342                 :        1031 :   return t;
     343                 :             : }
     344                 :             : 
     345                 :             : /* Instrument variable length array bound.  */
     346                 :             : 
     347                 :             : tree
     348                 :         221 : ubsan_instrument_vla (location_t loc, tree size)
     349                 :             : {
     350                 :         221 :   tree type = TREE_TYPE (size);
     351                 :         221 :   tree t, tt;
     352                 :             : 
     353                 :         221 :   t = fold_build2 (LE_EXPR, boolean_type_node, size, build_int_cst (type, 0));
     354                 :         221 :   if (flag_sanitize_trap & SANITIZE_VLA)
     355                 :           0 :     tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
     356                 :             :   else
     357                 :             :     {
     358                 :         221 :       tree data = ubsan_create_data ("__ubsan_vla_data", 1, &loc,
     359                 :             :                                      ubsan_type_descriptor (type), NULL_TREE,
     360                 :             :                                      NULL_TREE);
     361                 :         221 :       data = build_fold_addr_expr_loc (loc, data);
     362                 :         442 :       enum built_in_function bcode
     363                 :         221 :         = (flag_sanitize_recover & SANITIZE_VLA)
     364                 :         221 :           ? BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE
     365                 :             :           : BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE_ABORT;
     366                 :         221 :       tt = builtin_decl_explicit (bcode);
     367                 :         221 :       tt = build_call_expr_loc (loc, tt, 2, data, ubsan_encode_value (size));
     368                 :             :     }
     369                 :         221 :   t = fold_build3 (COND_EXPR, void_type_node, t, tt, void_node);
     370                 :             : 
     371                 :         221 :   return t;
     372                 :             : }
     373                 :             : 
     374                 :             : /* Instrument missing return in C++ functions returning non-void.  */
     375                 :             : 
     376                 :             : tree
     377                 :        1314 : ubsan_instrument_return (location_t loc)
     378                 :             : {
     379                 :        1314 :   if (flag_sanitize_trap & SANITIZE_RETURN)
     380                 :             :     /* pass_warn_function_return checks for BUILTINS_LOCATION.  */
     381                 :           3 :     return build_call_expr_loc (BUILTINS_LOCATION,
     382                 :           3 :                                 builtin_decl_explicit (BUILT_IN_TRAP), 0);
     383                 :             : 
     384                 :        1311 :   tree data = ubsan_create_data ("__ubsan_missing_return_data", 1, &loc,
     385                 :             :                                  NULL_TREE, NULL_TREE);
     386                 :        1311 :   tree t = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_MISSING_RETURN);
     387                 :        1311 :   return build_call_expr_loc (loc, t, 1, build_fold_addr_expr_loc (loc, data));
     388                 :             : }
     389                 :             : 
     390                 :             : /* Get the tree that represented the number of counted_by, i.e, the maximum
     391                 :             :    number of the elements of the object that the call to .ACCESS_WITH_SIZE
     392                 :             :    points to, this number will be the bound of the corresponding array.  */
     393                 :             : static tree
     394                 :          37 : get_bound_from_access_with_size (tree call)
     395                 :             : {
     396                 :          37 :   if (!is_access_with_size_p (call))
     397                 :             :     return NULL_TREE;
     398                 :             : 
     399                 :          37 :   tree ref_to_size = CALL_EXPR_ARG (call, 1);
     400                 :          37 :   unsigned int class_of_size = TREE_INT_CST_LOW (CALL_EXPR_ARG (call, 2));
     401                 :          37 :   tree type = TREE_TYPE (CALL_EXPR_ARG (call, 3));
     402                 :          37 :   tree size = fold_build2 (MEM_REF, type, unshare_expr (ref_to_size),
     403                 :             :                            build_int_cst (ptr_type_node, 0));
     404                 :             :   /* If size is negative value, treat it as zero.  */
     405                 :          37 :   if (!TYPE_UNSIGNED (type))
     406                 :             :   {
     407                 :          37 :     tree cond = fold_build2 (LT_EXPR, boolean_type_node,
     408                 :             :                              unshare_expr (size), build_zero_cst (type));
     409                 :          37 :     size = fold_build3 (COND_EXPR, type, cond,
     410                 :             :                         build_zero_cst (type), size);
     411                 :             :   }
     412                 :             : 
     413                 :             :   /* Only when class_of_size is 1, i.e, the number of the elements of
     414                 :             :      the object type, return the size.  */
     415                 :          37 :   if (class_of_size != 1)
     416                 :             :     return NULL_TREE;
     417                 :             :   else
     418                 :          37 :     size = fold_convert (sizetype, size);
     419                 :             : 
     420                 :          37 :   return size;
     421                 :             : }
     422                 :             : 
     423                 :             : 
     424                 :             : /* Instrument array bounds for ARRAY_REFs.  We create special builtin,
     425                 :             :    that gets expanded in the sanopt pass, and make an array dimension
     426                 :             :    of it.  ARRAY is the array, *INDEX is an index to the array.
     427                 :             :    Return NULL_TREE if no instrumentation is emitted.
     428                 :             :    IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside an ADDR_EXPR.  */
     429                 :             : 
     430                 :             : tree
     431                 :        4233 : ubsan_instrument_bounds (location_t loc, tree array, tree *index,
     432                 :             :                          bool ignore_off_by_one)
     433                 :             : {
     434                 :        4233 :   tree type = TREE_TYPE (array);
     435                 :        4233 :   tree domain = TYPE_DOMAIN (type);
     436                 :             : 
     437                 :        4233 :   if (domain == NULL_TREE)
     438                 :             :     return NULL_TREE;
     439                 :             : 
     440                 :        4221 :   tree bound = TYPE_MAX_VALUE (domain);
     441                 :        4221 :   if (!bound)
     442                 :             :     {
     443                 :             :       /* Handle C [0] arrays, which have TYPE_MAX_VALUE NULL, like
     444                 :             :          C++ [0] arrays which have TYPE_MIN_VALUE 0 TYPE_MAX_VALUE -1.  */
     445                 :         228 :       if (!c_dialect_cxx ()
     446                 :         228 :           && COMPLETE_TYPE_P (type)
     447                 :         347 :           && integer_zerop (TYPE_SIZE (type)))
     448                 :         119 :         bound = build_int_cst (TREE_TYPE (TYPE_MIN_VALUE (domain)), -1);
     449                 :         109 :       else if (INDIRECT_REF_P (array)
     450                 :         109 :                && is_access_with_size_p ((TREE_OPERAND (array, 0))))
     451                 :             :         {
     452                 :          37 :           bound = get_bound_from_access_with_size ((TREE_OPERAND (array, 0)));
     453                 :          37 :           bound = fold_build2 (MINUS_EXPR, TREE_TYPE (bound),
     454                 :             :                                bound,
     455                 :             :                                build_int_cst (TREE_TYPE (bound), 1));
     456                 :             :         }
     457                 :             :       else
     458                 :          72 :         return NULL_TREE;
     459                 :             :     }
     460                 :             : 
     461                 :        4149 :   bound = fold_build2 (PLUS_EXPR, TREE_TYPE (bound), bound,
     462                 :             :                        build_int_cst (TREE_TYPE (bound),
     463                 :             :                        1 + ignore_off_by_one));
     464                 :             : 
     465                 :             :   /* Detect flexible array members and suchlike, unless
     466                 :             :      -fsanitize=bounds-strict.  */
     467                 :        4149 :   tree base = get_base_address (array);
     468                 :        4149 :   if (!sanitize_flags_p (SANITIZE_BOUNDS_STRICT)
     469                 :        4117 :       && TREE_CODE (array) == COMPONENT_REF
     470                 :        5066 :       && base && (INDIRECT_REF_P (base) || TREE_CODE (base) == MEM_REF))
     471                 :             :     {
     472                 :             :       tree next = NULL_TREE;
     473                 :             :       tree cref = array;
     474                 :             : 
     475                 :             :       /* Walk all structs/unions.  */
     476                 :         827 :       while (TREE_CODE (cref) == COMPONENT_REF)
     477                 :             :         {
     478                 :         606 :           if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
     479                 :         600 :             for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
     480                 :        2441 :                  next && TREE_CODE (next) != FIELD_DECL;
     481                 :        1841 :                  next = DECL_CHAIN (next))
     482                 :             :               ;
     483                 :         606 :           if (next)
     484                 :             :             /* Not a last element.  Instrument it.  */
     485                 :             :             break;
     486                 :         365 :           if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 1))) == ARRAY_TYPE
     487                 :         365 :               && !c_dialect_cxx ())
     488                 :             :             {
     489                 :         268 :               unsigned l
     490                 :         268 :                 = c_strict_flex_array_level_of (TREE_OPERAND (cref, 1));
     491                 :         268 :               tree type2 = TREE_TYPE (TREE_OPERAND (cref, 1));
     492                 :         268 :               if (TYPE_DOMAIN (type2) != NULL_TREE)
     493                 :             :                 {
     494                 :         268 :                   tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (type2));
     495                 :         268 :                   if (max == NULL_TREE)
     496                 :             :                     {
     497                 :             :                       /* C [0] */
     498                 :          47 :                       if (COMPLETE_TYPE_P (type2)
     499                 :          47 :                           && integer_zerop (TYPE_SIZE (type2))
     500                 :          94 :                           && l == 3)
     501                 :           8 :                         next = TREE_OPERAND (cref, 1);
     502                 :             :                     }
     503                 :         221 :                   else if (TREE_CODE (max) == INTEGER_CST)
     504                 :             :                     {
     505                 :         221 :                       if (c_dialect_cxx ()
     506                 :         221 :                           && integer_all_onesp (max))
     507                 :             :                         {
     508                 :             :                           /* C++ [0] */
     509                 :           0 :                           if (l == 3)
     510                 :           0 :                             next = TREE_OPERAND (cref, 1);
     511                 :             :                         }
     512                 :         221 :                       else if (integer_zerop (max))
     513                 :             :                         {
     514                 :             :                           /* C/C++ [1] */
     515                 :          95 :                           if (l >= 2)
     516                 :          16 :                             next = TREE_OPERAND (cref, 1);
     517                 :             :                         }
     518                 :         126 :                       else if (l >= 1)
     519                 :          48 :                         next = TREE_OPERAND (cref, 1);
     520                 :             :                     }
     521                 :             :                 }
     522                 :         268 :               if (next)
     523                 :             :                 break;
     524                 :             :             }
     525                 :             :           /* Ok, this is the last field of the structure/union.  But the
     526                 :             :              aggregate containing the field must be the last field too,
     527                 :             :              recursively.  */
     528                 :         293 :           cref = TREE_OPERAND (cref, 0);
     529                 :             :         }
     530                 :         534 :       if (!next)
     531                 :             :         /* Don't instrument this flexible array member-like array in non-strict
     532                 :             :            -fsanitize=bounds mode.  */
     533                 :             :         return NULL_TREE;
     534                 :             :     }
     535                 :             : 
     536                 :             :   /* Don't emit instrumentation in the most common cases.  */
     537                 :        3928 :   tree idx = NULL_TREE;
     538                 :        3928 :   if (TREE_CODE (*index) == INTEGER_CST)
     539                 :             :     idx = *index;
     540                 :         720 :   else if (TREE_CODE (*index) == BIT_AND_EXPR
     541                 :         720 :            && TREE_CODE (TREE_OPERAND (*index, 1)) == INTEGER_CST)
     542                 :          27 :     idx = TREE_OPERAND (*index, 1);
     543                 :        3235 :   if (idx
     544                 :        3235 :       && TREE_CODE (bound) == INTEGER_CST
     545                 :        2948 :       && tree_int_cst_sgn (idx) >= 0
     546                 :        6161 :       && tree_int_cst_lt (idx, bound))
     547                 :             :     return NULL_TREE;
     548                 :             : 
     549                 :        1483 :   *index = save_expr (*index);
     550                 :             :   /* Create a "(T *) 0" tree node to describe the array type.  */
     551                 :        1483 :   tree zero_with_type = build_int_cst (build_pointer_type (type), 0);
     552                 :        1483 :   return build_call_expr_internal_loc (loc, IFN_UBSAN_BOUNDS,
     553                 :             :                                        void_type_node, 3, zero_with_type,
     554                 :        1483 :                                        *index, bound);
     555                 :             : }
     556                 :             : 
     557                 :             : /* Return true iff T is an array that was instrumented by SANITIZE_BOUNDS.  */
     558                 :             : 
     559                 :             : bool
     560                 :        4240 : ubsan_array_ref_instrumented_p (const_tree t)
     561                 :             : {
     562                 :        4240 :   if (TREE_CODE (t) != ARRAY_REF)
     563                 :             :     return false;
     564                 :             : 
     565                 :        4240 :   tree op1 = TREE_OPERAND (t, 1);
     566                 :        4240 :   return TREE_CODE (op1) == COMPOUND_EXPR
     567                 :           0 :          && TREE_CODE (TREE_OPERAND (op1, 0)) == CALL_EXPR
     568                 :           0 :          && CALL_EXPR_FN (TREE_OPERAND (op1, 0)) == NULL_TREE
     569                 :        4240 :          && CALL_EXPR_IFN (TREE_OPERAND (op1, 0)) == IFN_UBSAN_BOUNDS;
     570                 :             : }
     571                 :             : 
     572                 :             : /* Instrument an ARRAY_REF, if it hasn't already been instrumented.
     573                 :             :    IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR.  */
     574                 :             : 
     575                 :             : void
     576                 :        4240 : ubsan_maybe_instrument_array_ref (tree *expr_p, bool ignore_off_by_one)
     577                 :             : {
     578                 :        4240 :   if (!ubsan_array_ref_instrumented_p (*expr_p)
     579                 :        4240 :       && sanitize_flags_p (SANITIZE_BOUNDS | SANITIZE_BOUNDS_STRICT)
     580                 :        8470 :       && current_function_decl != NULL_TREE)
     581                 :             :     {
     582                 :        4230 :       tree op0 = TREE_OPERAND (*expr_p, 0);
     583                 :        4230 :       tree op1 = TREE_OPERAND (*expr_p, 1);
     584                 :        4230 :       tree e = ubsan_instrument_bounds (EXPR_LOCATION (*expr_p), op0, &op1,
     585                 :             :                                         ignore_off_by_one);
     586                 :        4230 :       if (e != NULL_TREE)
     587                 :        1480 :         TREE_OPERAND (*expr_p, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (op1),
     588                 :             :                                             e, op1);
     589                 :             :     }
     590                 :        4240 : }
     591                 :             : 
     592                 :             : static tree
     593                 :        6121 : ubsan_maybe_instrument_reference_or_call (location_t loc, tree op, tree ptype,
     594                 :             :                                           enum ubsan_null_ckind ckind)
     595                 :             : {
     596                 :        6121 :   if (!sanitize_flags_p (SANITIZE_ALIGNMENT | SANITIZE_NULL)
     597                 :        6121 :       || current_function_decl == NULL_TREE)
     598                 :             :     return NULL_TREE;
     599                 :             : 
     600                 :        6121 :   tree type = TREE_TYPE (ptype);
     601                 :        6121 :   tree orig_op = op;
     602                 :        6121 :   bool instrument = false;
     603                 :        6121 :   unsigned int mina = 0;
     604                 :             : 
     605                 :        6121 :   if (sanitize_flags_p (SANITIZE_ALIGNMENT))
     606                 :             :     {
     607                 :        5890 :       mina = min_align_of_type (type);
     608                 :        5890 :       if (mina <= 1)
     609                 :        1011 :         mina = 0;
     610                 :             :     }
     611                 :        8314 :   while ((TREE_CODE (op) == NOP_EXPR
     612                 :        8314 :           || TREE_CODE (op) == NON_LVALUE_EXPR)
     613                 :        8314 :          && TREE_CODE (TREE_TYPE (op)) == POINTER_TYPE)
     614                 :        2193 :     op = TREE_OPERAND (op, 0);
     615                 :        6121 :   if (TREE_CODE (op) == NOP_EXPR
     616                 :        6121 :       && TREE_CODE (TREE_TYPE (op)) == REFERENCE_TYPE)
     617                 :             :     {
     618                 :           0 :       if (mina && mina > min_align_of_type (TREE_TYPE (TREE_TYPE (op))))
     619                 :             :         instrument = true;
     620                 :             :     }
     621                 :             :   else
     622                 :             :     {
     623                 :        6121 :       if (sanitize_flags_p (SANITIZE_NULL) && TREE_CODE (op) == ADDR_EXPR)
     624                 :             :         {
     625                 :        3271 :           bool strict_overflow_p = false;
     626                 :             :           /* tree_single_nonzero_warnv_p will not return true for non-weak
     627                 :             :              non-automatic decls with -fno-delete-null-pointer-checks,
     628                 :             :              which is disabled during -fsanitize=null.  We don't want to
     629                 :             :              instrument those, just weak vars though.  */
     630                 :        3271 :           int save_flag_delete_null_pointer_checks
     631                 :             :             = flag_delete_null_pointer_checks;
     632                 :        3271 :           flag_delete_null_pointer_checks = 1;
     633                 :        3271 :           if (!tree_single_nonzero_warnv_p (op, &strict_overflow_p)
     634                 :        3271 :               || strict_overflow_p)
     635                 :             :             instrument = true;
     636                 :        3271 :           flag_delete_null_pointer_checks
     637                 :        3271 :             = save_flag_delete_null_pointer_checks;
     638                 :             :         }
     639                 :        2850 :       else if (sanitize_flags_p (SANITIZE_NULL))
     640                 :             :         instrument = true;
     641                 :        6121 :       if (mina && mina > 1)
     642                 :             :         {
     643                 :        6008 :           if (!POINTER_TYPE_P (TREE_TYPE (op))
     644                 :        6008 :               || mina > get_pointer_alignment (op) / BITS_PER_UNIT)
     645                 :             :             instrument = true;
     646                 :             :         }
     647                 :             :     }
     648                 :        2684 :   if (!instrument)
     649                 :        2021 :     return NULL_TREE;
     650                 :        4100 :   op = save_expr (orig_op);
     651                 :        4100 :   gcc_assert (POINTER_TYPE_P (ptype));
     652                 :        4100 :   if (TREE_CODE (ptype) == REFERENCE_TYPE)
     653                 :        1462 :     ptype = build_pointer_type (TREE_TYPE (ptype));
     654                 :        4100 :   tree kind = build_int_cst (ptype, ckind);
     655                 :        4100 :   tree align = build_int_cst (pointer_sized_int_node, mina);
     656                 :        4100 :   tree call
     657                 :        4100 :     = build_call_expr_internal_loc (loc, IFN_UBSAN_NULL, void_type_node,
     658                 :             :                                     3, op, kind, align);
     659                 :        4100 :   TREE_SIDE_EFFECTS (call) = 1;
     660                 :        4100 :   return fold_build2 (COMPOUND_EXPR, TREE_TYPE (op), call, op);
     661                 :             : }
     662                 :             : 
     663                 :             : /* Instrument a NOP_EXPR to REFERENCE_TYPE or INTEGER_CST with REFERENCE_TYPE
     664                 :             :    type if needed.  */
     665                 :             : 
     666                 :             : void
     667                 :        1998 : ubsan_maybe_instrument_reference (tree *stmt_p)
     668                 :             : {
     669                 :        1998 :   tree stmt = *stmt_p;
     670                 :        1998 :   tree op = stmt;
     671                 :        1998 :   if (TREE_CODE (stmt) == NOP_EXPR)
     672                 :        1988 :     op = TREE_OPERAND (stmt, 0);
     673                 :        1998 :   op = ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt), op,
     674                 :        1998 :                                                  TREE_TYPE (stmt),
     675                 :             :                                                  UBSAN_REF_BINDING);
     676                 :        1998 :   if (op)
     677                 :             :     {
     678                 :        1462 :       if (TREE_CODE (stmt) == NOP_EXPR)
     679                 :        1452 :         TREE_OPERAND (stmt, 0) = op;
     680                 :             :       else
     681                 :          10 :         *stmt_p = op;
     682                 :             :     }
     683                 :        1998 : }
     684                 :             : 
     685                 :             : /* Instrument a CALL_EXPR to a method if needed.  */
     686                 :             : 
     687                 :             : void
     688                 :        4123 : ubsan_maybe_instrument_member_call (tree stmt, bool is_ctor)
     689                 :             : {
     690                 :        4123 :   if (call_expr_nargs (stmt) == 0)
     691                 :             :     return;
     692                 :        4123 :   tree op = CALL_EXPR_ARG (stmt, 0);
     693                 :        4123 :   if (op == error_mark_node
     694                 :        4123 :       || !POINTER_TYPE_P (TREE_TYPE (op)))
     695                 :             :     return;
     696                 :        8246 :   op = ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt), op,
     697                 :        4123 :                                                  TREE_TYPE (op),
     698                 :             :                                                  is_ctor ? UBSAN_CTOR_CALL
     699                 :             :                                                  : UBSAN_MEMBER_CALL);
     700                 :        4123 :   if (op)
     701                 :        2638 :     CALL_EXPR_ARG (stmt, 0) = op;
     702                 :             : }
        

Generated by: LCOV version 2.1-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.