Print this page
11506 smatch resync
   1 #!/usr/bin/python
   2 
   3 # Copyright (C) 2013 Oracle.
   4 #
   5 # Licensed under the Open Software License version 1.1
   6 
   7 import sqlite3
   8 import sys
   9 import re
  10 
  11 try:
  12     con = sqlite3.connect('smatch_db.sqlite')
  13 except sqlite3.Error, e:
  14     print "Error %s:" % e.args[0]
  15     sys.exit(1)
  16 
  17 def usage():
  18     print "%s" %(sys.argv[0])
  19     print "<function> - how a function is called"

  20     print "return_states <function> - what a function returns"
  21     print "call_tree <function> - show the call tree"
  22     print "where <struct_type> <member> - where a struct member is set"
  23     print "type_size <struct_type> <member> - how a struct member is allocated"
  24     print "data_info <struct_type> <member> - information about a given data type"
  25     print "function_ptr <function> - which function pointers point to this"
  26     print "trace_param <function> <param> - trace where a parameter came from"
  27     print "locals <file> - print the local values in a file."
  28     sys.exit(1)
  29 
  30 function_ptrs = []
  31 searched_ptrs = []
  32 def get_function_pointers_helper(func):
  33     cur = con.cursor()
  34     cur.execute("select distinct ptr from function_ptr where function = '%s';" %(func))
  35     for row in cur:
  36         ptr = row[0]
  37         if ptr in function_ptrs:
  38             continue
  39         function_ptrs.append(ptr)
  40         if not ptr in searched_ptrs:
  41             searched_ptrs.append(ptr)
  42             get_function_pointers_helper(ptr)
  43 
  44 def get_function_pointers(func):
  45     global function_ptrs
  46     global searched_ptrs
  47     function_ptrs = [func]
  48     searched_ptrs = [func]
  49     get_function_pointers_helper(func)
  50     return function_ptrs
  51 
  52 db_types = {   0: "INTERNAL",
  53              101: "PARAM_CLEARED",
  54              103: "PARAM_LIMIT",
  55              104: "PARAM_FILTER",
  56             1001: "PARAM_VALUE",
  57             1002: "BUF_SIZE",
  58             1003: "USER_DATA",
  59             1004: "CAPPED_DATA",
  60             1005: "RETURN_VALUE",
  61             1006: "DEREFERENCE",
  62             1007: "RANGE_CAP",
  63             1008: "LOCK_HELD",
  64             1009: "LOCK_RELEASED",
  65             1010: "ABSOLUTE_LIMITS",
  66             1012: "PARAM_ADD",
  67             1013: "PARAM_FREED",
  68             1014: "DATA_SOURCE",
  69             1015: "FUZZY_MAX",
  70             1016: "STR_LEN",
  71             1017: "ARRAY_LEN",
  72             1018: "CAPABLE",
  73             1019: "NS_CAPABLE",
  74             1022: "TYPE_LINK",
  75             1023: "UNTRACKED_PARAM",
  76             1024: "CULL_PATH",
  77             1025: "PARAM_SET",
  78             1026: "PARAM_USED",
  79             1027: "BYTE_UNITS",
  80             1028: "COMPARE_LIMIT",
  81             1029: "PARAM_COMPARE",
  82             8017: "USER_DATA2",















  83             8018: "NO_OVERFLOW",
  84             8019: "NO_OVERFLOW_SIMPLE",
  85             8020: "LOCKED",
  86             8021: "UNLOCKED",
  87             8023: "ATOMIC_INC",
  88             8024: "ATOMIC_DEC",
  89 };
  90 
  91 def add_range(rl, min_val, max_val):
  92     check_next = 0
  93     done = 0
  94     ret = []
  95     idx = 0
  96 
  97     if len(rl) == 0:
  98         return [[min_val, max_val]]
  99 
 100     for idx in range(len(rl)):
 101         cur_min = rl[idx][0]
 102         cur_max = rl[idx][1]


 180     if (rl1 or rl2) and not ret:
 181         print "bug: merging %s + %s gives empty" %(rl1, rl2)
 182 
 183     return ret
 184 
 185 def txt_to_val(txt):
 186     if txt == "s64min":
 187         return -(2**63)
 188     elif txt == "s32min":
 189         return -(2**31)
 190     elif txt == "s16min":
 191         return -(2**15)
 192     elif txt == "s64max":
 193         return 2**63 - 1
 194     elif txt == "s32max":
 195         return 2**31 - 1
 196     elif txt == "s16max":
 197         return 2**15 - 1
 198     elif txt == "u64max":
 199         return 2**64 - 1


 200     elif txt == "u32max":
 201         return 2**32 - 1
 202     elif txt == "u16max":
 203         return 2**16 - 1
 204     else:
 205         try:
 206             return int(txt)
 207         except ValueError:
 208             return 0
 209 
 210 def val_to_txt(val):
 211     if val == -(2**63):
 212         return "s64min"
 213     elif val == -(2**31):
 214         return "s32min"
 215     elif val == -(2**15):
 216         return "s16min"
 217     elif val == 2**63 - 1:
 218         return "s64max"
 219     elif val == 2**31 - 1:


 572     trace_param_helper(func, param)
 573 
 574 def print_locals(filename):
 575     cur = con.cursor()
 576     cur.execute("select file,data,value from data_info where file = '%s' and type = 8029 and value != 0;" %(filename))
 577     for txt in cur:
 578         print "%s | %s | %s" %(txt[0], txt[1], txt[2])
 579 
 580 def constraint(struct_type, member):
 581     cur = con.cursor()
 582     cur.execute("select * from constraints_required where data like '(struct %s)->%s' or bound like '(struct %s)->%s';" %(struct_type, member, struct_type, member))
 583     for txt in cur:
 584         print "%-30s | %-30s | %s | %s" %(txt[0], txt[1], txt[2], txt[3])
 585 
 586 if len(sys.argv) < 2:
 587     usage()
 588 
 589 if len(sys.argv) == 2:
 590     func = sys.argv[1]
 591     print_caller_info("", func)






 592 elif sys.argv[1] == "call_info":
 593     if len(sys.argv) != 4:
 594         usage()
 595     filename = sys.argv[2]
 596     func = sys.argv[3]
 597     caller_info_values(filename, func)
 598     print_caller_info(filename, func)
 599 elif sys.argv[1] == "user_data":
 600     func = sys.argv[2]
 601     print_caller_info(filename, func, "USER_DATA")
 602 elif sys.argv[1] == "param_value":
 603     func = sys.argv[2]
 604     print_caller_info(filename, func, "PARAM_VALUE")
 605 elif sys.argv[1] == "function_ptr" or sys.argv[1] == "fn_ptr":
 606     func = sys.argv[2]
 607     print_fn_ptrs(func)
 608 elif sys.argv[1] == "return_states":
 609     func = sys.argv[2]
 610     print_return_states(func)
 611     print "================================================"
 612     print_return_implies(func)
 613 elif sys.argv[1] == "return_implies":
 614     func = sys.argv[2]
 615     print_return_implies(func)
 616 elif sys.argv[1] == "type_size" or sys.argv[1] == "buf_size":
 617     struct_type = sys.argv[2]
 618     member = sys.argv[3]
 619     print_type_size(struct_type, member)
 620 elif sys.argv[1] == "data_info":
 621     struct_type = sys.argv[2]
 622     member = sys.argv[3]
 623     print_data_info(struct_type, member)
 624 elif sys.argv[1] == "call_tree":


   1 #!/usr/bin/python
   2 
   3 # Copyright (C) 2013 Oracle.
   4 #
   5 # Licensed under the Open Software License version 1.1
   6 
   7 import sqlite3
   8 import sys
   9 import re
  10 
  11 try:
  12     con = sqlite3.connect('smatch_db.sqlite')
  13 except sqlite3.Error, e:
  14     print "Error %s:" % e.args[0]
  15     sys.exit(1)
  16 
  17 def usage():
  18     print "%s" %(sys.argv[0])
  19     print "<function> - how a function is called"
  20     print "info <type> - how a function is called, filtered by type"
  21     print "return_states <function> - what a function returns"
  22     print "call_tree <function> - show the call tree"
  23     print "where <struct_type> <member> - where a struct member is set"
  24     print "type_size <struct_type> <member> - how a struct member is allocated"
  25     print "data_info <struct_type> <member> - information about a given data type"
  26     print "function_ptr <function> - which function pointers point to this"
  27     print "trace_param <function> <param> - trace where a parameter came from"
  28     print "locals <file> - print the local values in a file."
  29     sys.exit(1)
  30 
  31 function_ptrs = []
  32 searched_ptrs = []
  33 def get_function_pointers_helper(func):
  34     cur = con.cursor()
  35     cur.execute("select distinct ptr from function_ptr where function = '%s';" %(func))
  36     for row in cur:
  37         ptr = row[0]
  38         if ptr in function_ptrs:
  39             continue
  40         function_ptrs.append(ptr)
  41         if not ptr in searched_ptrs:
  42             searched_ptrs.append(ptr)
  43             get_function_pointers_helper(ptr)
  44 
  45 def get_function_pointers(func):
  46     global function_ptrs
  47     global searched_ptrs
  48     function_ptrs = [func]
  49     searched_ptrs = [func]
  50     get_function_pointers_helper(func)
  51     return function_ptrs
  52 
  53 db_types = {   0: "INTERNAL",
  54              101: "PARAM_CLEARED",
  55              103: "PARAM_LIMIT",
  56              104: "PARAM_FILTER",
  57             1001: "PARAM_VALUE",
  58             1002: "BUF_SIZE",

  59             1004: "CAPPED_DATA",
  60             1005: "RETURN_VALUE",
  61             1006: "DEREFERENCE",
  62             1007: "RANGE_CAP",
  63             1008: "LOCK_HELD",
  64             1009: "LOCK_RELEASED",
  65             1010: "ABSOLUTE_LIMITS",
  66             1012: "PARAM_ADD",
  67             1013: "PARAM_FREED",
  68             1014: "DATA_SOURCE",
  69             1015: "FUZZY_MAX",
  70             1016: "STR_LEN",
  71             1017: "ARRAY_LEN",
  72             1018: "CAPABLE",
  73             1019: "NS_CAPABLE",
  74             1022: "TYPE_LINK",
  75             1023: "UNTRACKED_PARAM",
  76             1024: "CULL_PATH",
  77             1025: "PARAM_SET",
  78             1026: "PARAM_USED",
  79             1027: "BYTE_UNITS",
  80             1028: "COMPARE_LIMIT",
  81             1029: "PARAM_COMPARE",
  82             1030: "EXPECTS_TYPE",
  83             1031: "CONSTRAINT",
  84             1032: "PASSES_TYPE",
  85             1033: "CONSTRAINT_REQUIRED",
  86             1034: "BIT_INFO",
  87             1035: "NOSPEC",
  88             1036: "NOSPEC_WB",
  89             1037: "STMT_CNT",
  90             1038: "TERMINATED",
  91             1039: "SLEEP",
  92             1040: "NO_SLEEP_CNT",
  93             1041: "SMALLISH",
  94             1042: "FRESH_MTAG",
  95 
  96             8017: "USER_DATA",
  97             9017: "USER_DATA_SET",
  98             8018: "NO_OVERFLOW",
  99             8019: "NO_OVERFLOW_SIMPLE",
 100             8020: "LOCKED",
 101             8021: "UNLOCKED",
 102             8023: "ATOMIC_INC",
 103             8024: "ATOMIC_DEC",
 104 };
 105 
 106 def add_range(rl, min_val, max_val):
 107     check_next = 0
 108     done = 0
 109     ret = []
 110     idx = 0
 111 
 112     if len(rl) == 0:
 113         return [[min_val, max_val]]
 114 
 115     for idx in range(len(rl)):
 116         cur_min = rl[idx][0]
 117         cur_max = rl[idx][1]


 195     if (rl1 or rl2) and not ret:
 196         print "bug: merging %s + %s gives empty" %(rl1, rl2)
 197 
 198     return ret
 199 
 200 def txt_to_val(txt):
 201     if txt == "s64min":
 202         return -(2**63)
 203     elif txt == "s32min":
 204         return -(2**31)
 205     elif txt == "s16min":
 206         return -(2**15)
 207     elif txt == "s64max":
 208         return 2**63 - 1
 209     elif txt == "s32max":
 210         return 2**31 - 1
 211     elif txt == "s16max":
 212         return 2**15 - 1
 213     elif txt == "u64max":
 214         return 2**64 - 1
 215     elif txt == "ptr_max":
 216         return 2**64 - 1
 217     elif txt == "u32max":
 218         return 2**32 - 1
 219     elif txt == "u16max":
 220         return 2**16 - 1
 221     else:
 222         try:
 223             return int(txt)
 224         except ValueError:
 225             return 0
 226 
 227 def val_to_txt(val):
 228     if val == -(2**63):
 229         return "s64min"
 230     elif val == -(2**31):
 231         return "s32min"
 232     elif val == -(2**15):
 233         return "s16min"
 234     elif val == 2**63 - 1:
 235         return "s64max"
 236     elif val == 2**31 - 1:


 589     trace_param_helper(func, param)
 590 
 591 def print_locals(filename):
 592     cur = con.cursor()
 593     cur.execute("select file,data,value from data_info where file = '%s' and type = 8029 and value != 0;" %(filename))
 594     for txt in cur:
 595         print "%s | %s | %s" %(txt[0], txt[1], txt[2])
 596 
 597 def constraint(struct_type, member):
 598     cur = con.cursor()
 599     cur.execute("select * from constraints_required where data like '(struct %s)->%s' or bound like '(struct %s)->%s';" %(struct_type, member, struct_type, member))
 600     for txt in cur:
 601         print "%-30s | %-30s | %s | %s" %(txt[0], txt[1], txt[2], txt[3])
 602 
 603 if len(sys.argv) < 2:
 604     usage()
 605 
 606 if len(sys.argv) == 2:
 607     func = sys.argv[1]
 608     print_caller_info("", func)
 609 elif sys.argv[1] == "info":
 610     my_type = ""
 611     if len(sys.argv) == 4:
 612         my_type = sys.argv[3]
 613     func = sys.argv[2]
 614     print_caller_info("", func, my_type)
 615 elif sys.argv[1] == "call_info":
 616     if len(sys.argv) != 4:
 617         usage()
 618     filename = sys.argv[2]
 619     func = sys.argv[3]
 620     caller_info_values(filename, func)
 621     print_caller_info(filename, func)






 622 elif sys.argv[1] == "function_ptr" or sys.argv[1] == "fn_ptr":
 623     func = sys.argv[2]
 624     print_fn_ptrs(func)
 625 elif sys.argv[1] == "return_states":
 626     func = sys.argv[2]
 627     print_return_states(func)
 628     print "================================================"
 629     print_return_implies(func)
 630 elif sys.argv[1] == "return_implies":
 631     func = sys.argv[2]
 632     print_return_implies(func)
 633 elif sys.argv[1] == "type_size" or sys.argv[1] == "buf_size":
 634     struct_type = sys.argv[2]
 635     member = sys.argv[3]
 636     print_type_size(struct_type, member)
 637 elif sys.argv[1] == "data_info":
 638     struct_type = sys.argv[2]
 639     member = sys.argv[3]
 640     print_data_info(struct_type, member)
 641 elif sys.argv[1] == "call_tree":