1 /*
   2  * Copyright (C) 2017 Oracle.  All rights reserved.
   3  *
   4  * This program is free software; you can redistribute it and/or
   5  * modify it under the terms of the GNU General Public License
   6  * as published by the Free Software Foundation; either version 2
   7  * of the License, or (at your option) any later version.
   8  *
   9  * This program is distributed in the hope that it will be useful,
  10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  * GNU General Public License for more details.
  13  *
  14  * You should have received a copy of the GNU General Public License
  15  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
  16  */
  17 
  18 /*
  19  * This basically stores when a pointer is stored as a struct member.
  20  *
  21  */
  22 
  23 #include "smatch.h"
  24 #include "smatch_slist.h"
  25 #include "smatch_extra.h"
  26 
  27 static int my_id;
  28 
  29 static void match_assign(struct expression *expr)
  30 {
  31         struct expression *left, *right;
  32         mtag_t right_tag, left_tag;
  33         int right_offset, left_offset;
  34         sval_t sval;
  35 
  36         if (expr->op != '=')
  37                 return;
  38 
  39         left = strip_expr(expr->left);
  40         right = strip_expr(expr->right);
  41 
  42         if (!type_is_ptr(get_type(right)))
  43                 return;
  44         if (!get_implied_value(right, &sval))
  45                 return;
  46         if (sval_cmp(sval, valid_ptr_min_sval) < 0 ||
  47             sval_cmp(sval, valid_ptr_max_sval) > 0)
  48                 return;
  49         right_tag = sval.uvalue & ~MTAG_OFFSET_MASK;
  50         right_offset = sval.uvalue & MTAG_OFFSET_MASK;
  51 
  52         if (!expr_to_mtag_offset(left, &left_tag, &left_offset) ||
  53             left_offset >= MTAG_OFFSET_MASK)
  54                 return;
  55 
  56         sql_insert_mtag_map(left_tag, left_offset, right_tag, right_offset);
  57 }
  58 
  59 void register_mtag_map(int id)
  60 {
  61         my_id = id;
  62 
  63         add_hook(&match_assign, ASSIGNMENT_HOOK);
  64         add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
  65 }