1#!/usr/bin/env python 2# 3#===- exploded-graph-rewriter.py - ExplodedGraph dump tool -----*- python -*--# 4# 5# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 6# See https://llvm.org/LICENSE.txt for license information. 7# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 8# 9#===-----------------------------------------------------------------------===# 10 11 12from __future__ import print_function 13 14import argparse 15import collections 16import difflib 17import json 18import logging 19import re 20 21 22#===-----------------------------------------------------------------------===# 23# These data structures represent a deserialized ExplodedGraph. 24#===-----------------------------------------------------------------------===# 25 26 27# A helper function for finding the difference between two dictionaries. 28def diff_dicts(curr, prev): 29 removed = [k for k in prev if k not in curr or curr[k] != prev[k]] 30 added = [k for k in curr if k not in prev or curr[k] != prev[k]] 31 return (removed, added) 32 33 34# Represents any program state trait that is a dictionary of key-value pairs. 35class GenericMap(object): 36 def __init__(self, items): 37 self.generic_map = collections.OrderedDict(items) 38 39 def diff(self, prev): 40 return diff_dicts(self.generic_map, prev.generic_map) 41 42 def is_different(self, prev): 43 removed, added = self.diff(prev) 44 return len(removed) != 0 or len(added) != 0 45 46 47# A deserialized source location. 48class SourceLocation(object): 49 def __init__(self, json_loc): 50 super(SourceLocation, self).__init__() 51 self.line = json_loc['line'] 52 self.col = json_loc['column'] 53 self.filename = json_loc['filename'] \ 54 if 'filename' in json_loc else '(main file)' 55 56 57# A deserialized program point. 58class ProgramPoint(object): 59 def __init__(self, json_pp): 60 super(ProgramPoint, self).__init__() 61 self.kind = json_pp['kind'] 62 self.tag = json_pp['tag'] 63 if self.kind == 'Edge': 64 self.src_id = json_pp['src_id'] 65 self.dst_id = json_pp['dst_id'] 66 elif self.kind == 'Statement': 67 self.stmt_kind = json_pp['stmt_kind'] 68 self.stmt_point_kind = json_pp['stmt_point_kind'] 69 self.pointer = json_pp['pointer'] 70 self.pretty = json_pp['pretty'] 71 self.loc = SourceLocation(json_pp['location']) \ 72 if json_pp['location'] is not None else None 73 elif self.kind == 'BlockEntrance': 74 self.block_id = json_pp['block_id'] 75 76 77# A single expression acting as a key in a deserialized Environment. 78class EnvironmentBindingKey(object): 79 def __init__(self, json_ek): 80 super(EnvironmentBindingKey, self).__init__() 81 # CXXCtorInitializer is not a Stmt! 82 self.stmt_id = json_ek['stmt_id'] if 'stmt_id' in json_ek \ 83 else json_ek['init_id'] 84 self.pretty = json_ek['pretty'] 85 self.kind = json_ek['kind'] if 'kind' in json_ek else None 86 87 def _key(self): 88 return self.stmt_id 89 90 def __eq__(self, other): 91 return self._key() == other._key() 92 93 def __hash__(self): 94 return hash(self._key()) 95 96 97# Deserialized description of a location context. 98class LocationContext(object): 99 def __init__(self, json_frame): 100 super(LocationContext, self).__init__() 101 self.lctx_id = json_frame['lctx_id'] 102 self.caption = json_frame['location_context'] 103 self.decl = json_frame['calling'] 104 self.line = json_frame['call_line'] 105 106 def _key(self): 107 return self.lctx_id 108 109 def __eq__(self, other): 110 return self._key() == other._key() 111 112 def __hash__(self): 113 return hash(self._key()) 114 115 116# A group of deserialized Environment bindings that correspond to a specific 117# location context. 118class EnvironmentFrame(object): 119 def __init__(self, json_frame): 120 super(EnvironmentFrame, self).__init__() 121 self.location_context = LocationContext(json_frame) 122 self.bindings = collections.OrderedDict( 123 [(EnvironmentBindingKey(b), 124 b['value']) for b in json_frame['items']] 125 if json_frame['items'] is not None else []) 126 127 def diff_bindings(self, prev): 128 return diff_dicts(self.bindings, prev.bindings) 129 130 def is_different(self, prev): 131 removed, added = self.diff_bindings(prev) 132 return len(removed) != 0 or len(added) != 0 133 134 135# A deserialized Environment. This class can also hold other entities that 136# are similar to Environment, such as Objects Under Construction. 137class GenericEnvironment(object): 138 def __init__(self, json_e): 139 super(GenericEnvironment, self).__init__() 140 self.frames = [EnvironmentFrame(f) for f in json_e] 141 142 def diff_frames(self, prev): 143 # TODO: It's difficult to display a good diff when frame numbers shift. 144 if len(self.frames) != len(prev.frames): 145 return None 146 147 updated = [] 148 for i in range(len(self.frames)): 149 f = self.frames[i] 150 prev_f = prev.frames[i] 151 if f.location_context == prev_f.location_context: 152 if f.is_different(prev_f): 153 updated.append(i) 154 else: 155 # We have the whole frame replaced with another frame. 156 # TODO: Produce a nice diff. 157 return None 158 159 # TODO: Add support for added/removed. 160 return updated 161 162 def is_different(self, prev): 163 updated = self.diff_frames(prev) 164 return updated is None or len(updated) > 0 165 166 167# A single binding key in a deserialized RegionStore cluster. 168class StoreBindingKey(object): 169 def __init__(self, json_sk): 170 super(StoreBindingKey, self).__init__() 171 self.kind = json_sk['kind'] 172 self.offset = json_sk['offset'] 173 174 def _key(self): 175 return (self.kind, self.offset) 176 177 def __eq__(self, other): 178 return self._key() == other._key() 179 180 def __hash__(self): 181 return hash(self._key()) 182 183 184# A single cluster of the deserialized RegionStore. 185class StoreCluster(object): 186 def __init__(self, json_sc): 187 super(StoreCluster, self).__init__() 188 self.base_region = json_sc['cluster'] 189 self.bindings = collections.OrderedDict( 190 [(StoreBindingKey(b), b['value']) for b in json_sc['items']]) 191 192 def diff_bindings(self, prev): 193 return diff_dicts(self.bindings, prev.bindings) 194 195 def is_different(self, prev): 196 removed, added = self.diff_bindings(prev) 197 return len(removed) != 0 or len(added) != 0 198 199 200# A deserialized RegionStore. 201class Store(object): 202 def __init__(self, json_s): 203 super(Store, self).__init__() 204 self.ptr = json_s['pointer'] 205 self.clusters = collections.OrderedDict( 206 [(c['pointer'], StoreCluster(c)) for c in json_s['items']]) 207 208 def diff_clusters(self, prev): 209 removed = [k for k in prev.clusters if k not in self.clusters] 210 added = [k for k in self.clusters if k not in prev.clusters] 211 updated = [k for k in prev.clusters if k in self.clusters 212 and prev.clusters[k].is_different(self.clusters[k])] 213 return (removed, added, updated) 214 215 def is_different(self, prev): 216 removed, added, updated = self.diff_clusters(prev) 217 return len(removed) != 0 or len(added) != 0 or len(updated) != 0 218 219 220# Deserialized messages from a single checker in a single program state. 221# Basically a list of raw strings. 222class CheckerLines(object): 223 def __init__(self, json_lines): 224 super(CheckerLines, self).__init__() 225 self.lines = json_lines 226 227 def diff_lines(self, prev): 228 lines = difflib.ndiff(prev.lines, self.lines) 229 return [l.strip() for l in lines 230 if l.startswith('+') or l.startswith('-')] 231 232 def is_different(self, prev): 233 return len(self.diff_lines(prev)) > 0 234 235 236# Deserialized messages of all checkers, separated by checker. 237class CheckerMessages(object): 238 def __init__(self, json_m): 239 super(CheckerMessages, self).__init__() 240 self.items = collections.OrderedDict( 241 [(m['checker'], CheckerLines(m['messages'])) for m in json_m]) 242 243 def diff_messages(self, prev): 244 removed = [k for k in prev.items if k not in self.items] 245 added = [k for k in self.items if k not in prev.items] 246 updated = [k for k in prev.items if k in self.items 247 and prev.items[k].is_different(self.items[k])] 248 return (removed, added, updated) 249 250 def is_different(self, prev): 251 removed, added, updated = self.diff_messages(prev) 252 return len(removed) != 0 or len(added) != 0 or len(updated) != 0 253 254 255# A deserialized program state. 256class ProgramState(object): 257 def __init__(self, state_id, json_ps): 258 super(ProgramState, self).__init__() 259 logging.debug('Adding ProgramState ' + str(state_id)) 260 261 self.state_id = state_id 262 263 self.store = Store(json_ps['store']) \ 264 if json_ps['store'] is not None else None 265 266 self.environment = \ 267 GenericEnvironment(json_ps['environment']['items']) \ 268 if json_ps['environment'] is not None else None 269 270 self.constraints = GenericMap([ 271 (c['symbol'], c['range']) for c in json_ps['constraints'] 272 ]) if json_ps['constraints'] is not None else None 273 274 self.dynamic_types = GenericMap([ 275 (t['region'], '%s%s' % (t['dyn_type'], 276 ' (or a sub-class)' 277 if t['sub_classable'] else '')) 278 for t in json_ps['dynamic_types']]) \ 279 if json_ps['dynamic_types'] is not None else None 280 281 self.constructing_objects = \ 282 GenericEnvironment(json_ps['constructing_objects']) \ 283 if json_ps['constructing_objects'] is not None else None 284 285 self.checker_messages = CheckerMessages(json_ps['checker_messages']) \ 286 if json_ps['checker_messages'] is not None else None 287 288 289# A deserialized exploded graph node. Has a default constructor because it 290# may be referenced as part of an edge before its contents are deserialized, 291# and in this moment we already need a room for predecessors and successors. 292class ExplodedNode(object): 293 def __init__(self): 294 super(ExplodedNode, self).__init__() 295 self.predecessors = [] 296 self.successors = [] 297 298 def construct(self, node_id, json_node): 299 logging.debug('Adding ' + node_id) 300 self.node_id = json_node['node_id'] 301 self.ptr = json_node['pointer'] 302 self.has_report = json_node['has_report'] 303 self.is_sink = json_node['is_sink'] 304 self.points = [ProgramPoint(p) for p in json_node['program_points']] 305 self.state = ProgramState(json_node['state_id'], 306 json_node['program_state']) \ 307 if json_node['program_state'] is not None else None 308 309 assert self.node_name() == node_id 310 311 def node_name(self): 312 return 'Node' + self.ptr 313 314 315# A deserialized ExplodedGraph. Constructed by consuming a .dot file 316# line-by-line. 317class ExplodedGraph(object): 318 # Parse .dot files with regular expressions. 319 node_re = re.compile( 320 '^(Node0x[0-9a-f]*) \\[shape=record,.*label="{(.*)\\\\l}"\\];$') 321 edge_re = re.compile( 322 '^(Node0x[0-9a-f]*) -> (Node0x[0-9a-f]*);$') 323 324 def __init__(self): 325 super(ExplodedGraph, self).__init__() 326 self.nodes = collections.defaultdict(ExplodedNode) 327 self.root_id = None 328 self.incomplete_line = '' 329 330 def add_raw_line(self, raw_line): 331 if raw_line.startswith('//'): 332 return 333 334 # Allow line breaks by waiting for ';'. This is not valid in 335 # a .dot file, but it is useful for writing tests. 336 if len(raw_line) > 0 and raw_line[-1] != ';': 337 self.incomplete_line += raw_line 338 return 339 raw_line = self.incomplete_line + raw_line 340 self.incomplete_line = '' 341 342 # Apply regexps one by one to see if it's a node or an edge 343 # and extract contents if necessary. 344 logging.debug('Line: ' + raw_line) 345 result = self.edge_re.match(raw_line) 346 if result is not None: 347 logging.debug('Classified as edge line.') 348 pred = result.group(1) 349 succ = result.group(2) 350 self.nodes[pred].successors.append(succ) 351 self.nodes[succ].predecessors.append(pred) 352 return 353 result = self.node_re.match(raw_line) 354 if result is not None: 355 logging.debug('Classified as node line.') 356 node_id = result.group(1) 357 if len(self.nodes) == 0: 358 self.root_id = node_id 359 # Note: when writing tests you don't need to escape everything, 360 # even though in a valid dot file everything is escaped. 361 node_label = result.group(2).replace('\\l', '') \ 362 .replace(' ', '') \ 363 .replace('\\"', '"') \ 364 .replace('\\{', '{') \ 365 .replace('\\}', '}') \ 366 .replace('\\\\', '\\') \ 367 .replace('\\|', '|') \ 368 .replace('\\<', '\\\\<') \ 369 .replace('\\>', '\\\\>') \ 370 .rstrip(',') 371 logging.debug(node_label) 372 json_node = json.loads(node_label) 373 self.nodes[node_id].construct(node_id, json_node) 374 return 375 logging.debug('Skipping.') 376 377 378#===-----------------------------------------------------------------------===# 379# Visitors traverse a deserialized ExplodedGraph and do different things 380# with every node and edge. 381#===-----------------------------------------------------------------------===# 382 383 384# A visitor that dumps the ExplodedGraph into a DOT file with fancy HTML-based 385# syntax highlighing. 386class DotDumpVisitor(object): 387 def __init__(self, do_diffs, dark_mode, gray_mode): 388 super(DotDumpVisitor, self).__init__() 389 self._do_diffs = do_diffs 390 self._dark_mode = dark_mode 391 self._gray_mode = gray_mode 392 393 @staticmethod 394 def _dump_raw(s): 395 print(s, end='') 396 397 def _dump(self, s): 398 s = s.replace('&', '&') \ 399 .replace('{', '\\{') \ 400 .replace('}', '\\}') \ 401 .replace('\\<', '<') \ 402 .replace('\\>', '>') \ 403 .replace('\\l', '<br />') \ 404 .replace('|', '\\|') 405 if self._gray_mode: 406 s = re.sub(r'<font color="[a-z0-9]*">', '', s) 407 s = re.sub(r'</font>', '', s) 408 self._dump_raw(s) 409 410 @staticmethod 411 def _diff_plus_minus(is_added): 412 if is_added is None: 413 return '' 414 if is_added: 415 return '<font color="forestgreen">+</font>' 416 return '<font color="red">-</font>' 417 418 @staticmethod 419 def _short_pretty(s): 420 if s is None: 421 return None 422 if len(s) < 20: 423 return s 424 left = s.find('{') 425 right = s.rfind('}') 426 if left == -1 or right == -1 or left >= right: 427 return s 428 candidate = s[0:left + 1] + ' ... ' + s[right:] 429 if len(candidate) >= len(s): 430 return s 431 return candidate 432 433 def visit_begin_graph(self, graph): 434 self._graph = graph 435 self._dump_raw('digraph "ExplodedGraph" {\n') 436 if self._dark_mode: 437 self._dump_raw('bgcolor="gray10";\n') 438 self._dump_raw('label="";\n') 439 440 def visit_program_point(self, p): 441 if p.kind in ['Edge', 'BlockEntrance', 'BlockExit']: 442 color = 'gold3' 443 elif p.kind in ['PreStmtPurgeDeadSymbols', 444 'PostStmtPurgeDeadSymbols']: 445 color = 'red' 446 elif p.kind in ['CallEnter', 'CallExitBegin', 'CallExitEnd']: 447 color = 'dodgerblue' if self._dark_mode else 'blue' 448 elif p.kind in ['Statement']: 449 color = 'cyan4' 450 else: 451 color = 'forestgreen' 452 453 if p.kind == 'Statement': 454 # This avoids pretty-printing huge statements such as CompoundStmt. 455 # Such statements show up only at [Pre|Post]StmtPurgeDeadSymbols 456 skip_pretty = 'PurgeDeadSymbols' in p.stmt_point_kind 457 stmt_color = 'cyan3' 458 if p.loc is not None: 459 self._dump('<tr><td align="left" width="0">' 460 '%s:<b>%s</b>:<b>%s</b>:</td>' 461 '<td align="left" width="0"><font color="%s">' 462 '%s</font></td>' 463 '<td align="left"><font color="%s">%s</font></td>' 464 '<td>%s</td></tr>' 465 % (p.loc.filename, p.loc.line, 466 p.loc.col, color, p.stmt_kind, 467 stmt_color, p.stmt_point_kind, 468 self._short_pretty(p.pretty) 469 if not skip_pretty else '')) 470 else: 471 self._dump('<tr><td align="left" width="0">' 472 '<i>Invalid Source Location</i>:</td>' 473 '<td align="left" width="0">' 474 '<font color="%s">%s</font></td>' 475 '<td align="left"><font color="%s">%s</font></td>' 476 '<td>%s</td></tr>' 477 % (color, p.stmt_kind, 478 stmt_color, p.stmt_point_kind, 479 self._short_pretty(p.pretty) 480 if not skip_pretty else '')) 481 elif p.kind == 'Edge': 482 self._dump('<tr><td width="0"></td>' 483 '<td align="left" width="0">' 484 '<font color="%s">%s</font></td><td align="left">' 485 '[B%d] -\\> [B%d]</td></tr>' 486 % (color, 'BlockEdge', p.src_id, p.dst_id)) 487 elif p.kind == 'BlockEntrance': 488 self._dump('<tr><td width="0"></td>' 489 '<td align="left" width="0">' 490 '<font color="%s">%s</font></td>' 491 '<td align="left">[B%d]</td></tr>' 492 % (color, p.kind, p.block_id)) 493 else: 494 # TODO: Print more stuff for other kinds of points. 495 self._dump('<tr><td width="0"></td>' 496 '<td align="left" width="0" colspan="2">' 497 '<font color="%s">%s</font></td></tr>' 498 % (color, p.kind)) 499 500 if p.tag is not None: 501 self._dump('<tr><td width="0"></td>' 502 '<td colspan="3" align="left">' 503 '<b>Tag: </b> <font color="crimson">' 504 '%s</font></td></tr>' % p.tag) 505 506 def visit_environment(self, e, prev_e=None): 507 self._dump('<table border="0">') 508 509 def dump_location_context(lc, is_added=None): 510 self._dump('<tr><td>%s</td>' 511 '<td align="left"><b>%s</b></td>' 512 '<td align="left" colspan="2">' 513 '<font color="gray60">%s </font>' 514 '%s</td></tr>' 515 % (self._diff_plus_minus(is_added), 516 lc.caption, lc.decl, 517 ('(line %s)' % lc.line) if lc.line is not None 518 else '')) 519 520 def dump_binding(f, b, is_added=None): 521 self._dump('<tr><td>%s</td>' 522 '<td align="left"><i>S%s</i></td>' 523 '%s' 524 '<td align="left">%s</td>' 525 '<td align="left">%s</td></tr>' 526 % (self._diff_plus_minus(is_added), 527 b.stmt_id, 528 '<td align="left"><font color="%s"><i>' 529 '%s</i></font></td>' % ( 530 'lavender' if self._dark_mode else 'darkgreen', 531 ('(%s)' % b.kind) if b.kind is not None else ' ' 532 ), 533 self._short_pretty(b.pretty), f.bindings[b])) 534 535 frames_updated = e.diff_frames(prev_e) if prev_e is not None else None 536 if frames_updated: 537 for i in frames_updated: 538 f = e.frames[i] 539 prev_f = prev_e.frames[i] 540 dump_location_context(f.location_context) 541 bindings_removed, bindings_added = f.diff_bindings(prev_f) 542 for b in bindings_removed: 543 dump_binding(prev_f, b, False) 544 for b in bindings_added: 545 dump_binding(f, b, True) 546 else: 547 for f in e.frames: 548 dump_location_context(f.location_context) 549 for b in f.bindings: 550 dump_binding(f, b) 551 552 self._dump('</table>') 553 554 def visit_environment_in_state(self, selector, title, s, prev_s=None): 555 e = getattr(s, selector) 556 prev_e = getattr(prev_s, selector) if prev_s is not None else None 557 if e is None and prev_e is None: 558 return 559 560 self._dump('<hr /><tr><td align="left"><b>%s: </b>' % title) 561 if e is None: 562 self._dump('<i> Nothing!</i>') 563 else: 564 if prev_e is not None: 565 if e.is_different(prev_e): 566 self._dump('</td></tr><tr><td align="left">') 567 self.visit_environment(e, prev_e) 568 else: 569 self._dump('<i> No changes!</i>') 570 else: 571 self._dump('</td></tr><tr><td align="left">') 572 self.visit_environment(e) 573 574 self._dump('</td></tr>') 575 576 def visit_store(self, s, prev_s=None): 577 self._dump('<table border="0">') 578 579 def dump_binding(s, c, b, is_added=None): 580 self._dump('<tr><td>%s</td>' 581 '<td align="left">%s</td>' 582 '<td align="left">%s</td>' 583 '<td align="left">%s</td>' 584 '<td align="left">%s</td></tr>' 585 % (self._diff_plus_minus(is_added), 586 s.clusters[c].base_region, b.offset, 587 '(<i>Default</i>)' if b.kind == 'Default' 588 else '', 589 s.clusters[c].bindings[b])) 590 591 if prev_s is not None: 592 clusters_removed, clusters_added, clusters_updated = \ 593 s.diff_clusters(prev_s) 594 for c in clusters_removed: 595 for b in prev_s.clusters[c].bindings: 596 dump_binding(prev_s, c, b, False) 597 for c in clusters_updated: 598 bindings_removed, bindings_added = \ 599 s.clusters[c].diff_bindings(prev_s.clusters[c]) 600 for b in bindings_removed: 601 dump_binding(prev_s, c, b, False) 602 for b in bindings_added: 603 dump_binding(s, c, b, True) 604 for c in clusters_added: 605 for b in s.clusters[c].bindings: 606 dump_binding(s, c, b, True) 607 else: 608 for c in s.clusters: 609 for b in s.clusters[c].bindings: 610 dump_binding(s, c, b) 611 612 self._dump('</table>') 613 614 def visit_store_in_state(self, s, prev_s=None): 615 st = s.store 616 prev_st = prev_s.store if prev_s is not None else None 617 if st is None and prev_st is None: 618 return 619 620 self._dump('<hr /><tr><td align="left"><b>Store: </b>') 621 if st is None: 622 self._dump('<i> Nothing!</i>') 623 else: 624 if prev_st is not None: 625 if s.store.is_different(prev_st): 626 self._dump('</td></tr><tr><td align="left">') 627 self.visit_store(st, prev_st) 628 else: 629 self._dump('<i> No changes!</i>') 630 else: 631 self._dump('</td></tr><tr><td align="left">') 632 self.visit_store(st) 633 self._dump('</td></tr>') 634 635 def visit_generic_map(self, m, prev_m=None): 636 self._dump('<table border="0">') 637 638 def dump_pair(m, k, is_added=None): 639 self._dump('<tr><td>%s</td>' 640 '<td align="left">%s</td>' 641 '<td align="left">%s</td></tr>' 642 % (self._diff_plus_minus(is_added), 643 k, m.generic_map[k])) 644 645 if prev_m is not None: 646 removed, added = m.diff(prev_m) 647 for k in removed: 648 dump_pair(prev_m, k, False) 649 for k in added: 650 dump_pair(m, k, True) 651 else: 652 for k in m.generic_map: 653 dump_pair(m, k, None) 654 655 self._dump('</table>') 656 657 def visit_generic_map_in_state(self, selector, title, s, prev_s=None): 658 m = getattr(s, selector) 659 prev_m = getattr(prev_s, selector) if prev_s is not None else None 660 if m is None and prev_m is None: 661 return 662 663 self._dump('<hr />') 664 self._dump('<tr><td align="left">' 665 '<b>%s: </b>' % title) 666 if m is None: 667 self._dump('<i> Nothing!</i>') 668 else: 669 if prev_m is not None: 670 if m.is_different(prev_m): 671 self._dump('</td></tr><tr><td align="left">') 672 self.visit_generic_map(m, prev_m) 673 else: 674 self._dump('<i> No changes!</i>') 675 else: 676 self._dump('</td></tr><tr><td align="left">') 677 self.visit_generic_map(m) 678 679 self._dump('</td></tr>') 680 681 def visit_checker_messages(self, m, prev_m=None): 682 self._dump('<table border="0">') 683 684 def dump_line(l, is_added=None): 685 self._dump('<tr><td>%s</td>' 686 '<td align="left">%s</td></tr>' 687 % (self._diff_plus_minus(is_added), l)) 688 689 def dump_chk(chk, is_added=None): 690 dump_line('<i>%s</i>:' % chk, is_added) 691 692 if prev_m is not None: 693 removed, added, updated = m.diff_messages(prev_m) 694 for chk in removed: 695 dump_chk(chk, False) 696 for l in prev_m.items[chk].lines: 697 dump_line(l, False) 698 for chk in updated: 699 dump_chk(chk) 700 for l in m.items[chk].diff_lines(prev_m.items[chk]): 701 dump_line(l[1:], l.startswith('+')) 702 for chk in added: 703 dump_chk(chk, True) 704 for l in m.items[chk].lines: 705 dump_line(l, True) 706 else: 707 for chk in m.items: 708 dump_chk(chk) 709 for l in m.items[chk].lines: 710 dump_line(l) 711 712 self._dump('</table>') 713 714 def visit_checker_messages_in_state(self, s, prev_s=None): 715 m = s.checker_messages 716 prev_m = prev_s.checker_messages if prev_s is not None else None 717 if m is None and prev_m is None: 718 return 719 720 self._dump('<hr />') 721 self._dump('<tr><td align="left">' 722 '<b>Checker State: </b>') 723 if m is None: 724 self._dump('<i> Nothing!</i>') 725 else: 726 if prev_m is not None: 727 if m.is_different(prev_m): 728 self._dump('</td></tr><tr><td align="left">') 729 self.visit_checker_messages(m, prev_m) 730 else: 731 self._dump('<i> No changes!</i>') 732 else: 733 self._dump('</td></tr><tr><td align="left">') 734 self.visit_checker_messages(m) 735 736 self._dump('</td></tr>') 737 738 def visit_state(self, s, prev_s): 739 self.visit_store_in_state(s, prev_s) 740 self.visit_environment_in_state('environment', 'Environment', 741 s, prev_s) 742 self.visit_generic_map_in_state('constraints', 'Ranges', 743 s, prev_s) 744 self.visit_generic_map_in_state('dynamic_types', 'Dynamic Types', 745 s, prev_s) 746 self.visit_environment_in_state('constructing_objects', 747 'Objects Under Construction', 748 s, prev_s) 749 self.visit_checker_messages_in_state(s, prev_s) 750 751 def visit_node(self, node): 752 self._dump('%s [shape=record,' 753 % (node.node_name())) 754 if self._dark_mode: 755 self._dump('color="white",fontcolor="gray80",') 756 self._dump('label=<<table border="0">') 757 758 self._dump('<tr><td bgcolor="%s"><b>Node %d (%s) - ' 759 'State %s</b></td></tr>' 760 % ("gray20" if self._dark_mode else "gray", 761 node.node_id, node.ptr, node.state.state_id 762 if node.state is not None else 'Unspecified')) 763 if node.has_report: 764 self._dump('<tr><td><font color="red"><b>Bug Report Attached' 765 '</b></font></td></tr>') 766 if node.is_sink: 767 self._dump('<tr><td><font color="cornflowerblue"><b>Sink Node' 768 '</b></font></td></tr>') 769 self._dump('<tr><td align="left" width="0">') 770 if len(node.points) > 1: 771 self._dump('<b>Program points:</b></td></tr>') 772 else: 773 self._dump('<b>Program point:</b></td></tr>') 774 self._dump('<tr><td align="left" width="0">' 775 '<table border="0" align="left" width="0">') 776 for p in node.points: 777 self.visit_program_point(p) 778 self._dump('</table></td></tr>') 779 780 if node.state is not None: 781 prev_s = None 782 # Do diffs only when we have a unique predecessor. 783 # Don't do diffs on the leaf nodes because they're 784 # the important ones. 785 if self._do_diffs and len(node.predecessors) == 1 \ 786 and len(node.successors) > 0: 787 prev_s = self._graph.nodes[node.predecessors[0]].state 788 self.visit_state(node.state, prev_s) 789 self._dump_raw('</table>>];\n') 790 791 def visit_edge(self, pred, succ): 792 self._dump_raw('%s -> %s%s;\n' % ( 793 pred.node_name(), succ.node_name(), 794 ' [color="white"]' if self._dark_mode else '' 795 )) 796 797 def visit_end_of_graph(self): 798 self._dump_raw('}\n') 799 800 801#===-----------------------------------------------------------------------===# 802# Explorers know how to traverse the ExplodedGraph in a certain order. 803# They would invoke a Visitor on every node or edge they encounter. 804#===-----------------------------------------------------------------------===# 805 806 807# BasicExplorer explores the whole graph in no particular order. 808class BasicExplorer(object): 809 def __init__(self): 810 super(BasicExplorer, self).__init__() 811 812 def explore(self, graph, visitor): 813 visitor.visit_begin_graph(graph) 814 for node in sorted(graph.nodes): 815 logging.debug('Visiting ' + node) 816 visitor.visit_node(graph.nodes[node]) 817 for succ in sorted(graph.nodes[node].successors): 818 logging.debug('Visiting edge: %s -> %s ' % (node, succ)) 819 visitor.visit_edge(graph.nodes[node], graph.nodes[succ]) 820 visitor.visit_end_of_graph() 821 822 823# SinglePathExplorer traverses only a single path - the leftmost path 824# from the root. Useful when the trimmed graph is still too large 825# due to a large amount of equivalent reports. 826class SinglePathExplorer(object): 827 def __init__(self): 828 super(SinglePathExplorer, self).__init__() 829 830 def explore(self, graph, visitor): 831 visitor.visit_begin_graph(graph) 832 833 # Keep track of visited nodes in order to avoid loops. 834 visited = set() 835 node_id = graph.root_id 836 while True: 837 visited.add(node_id) 838 node = graph.nodes[node_id] 839 logging.debug('Visiting ' + node_id) 840 visitor.visit_node(node) 841 if len(node.successors) == 0: 842 break 843 844 succ_id = node.successors[0] 845 succ = graph.nodes[succ_id] 846 logging.debug('Visiting edge: %s -> %s ' % (node_id, succ_id)) 847 visitor.visit_edge(node, succ) 848 if succ_id in visited: 849 break 850 851 node_id = succ_id 852 853 visitor.visit_end_of_graph() 854 855 856#===-----------------------------------------------------------------------===# 857# The entry point to the script. 858#===-----------------------------------------------------------------------===# 859 860 861def main(): 862 parser = argparse.ArgumentParser() 863 parser.add_argument('filename', type=str) 864 parser.add_argument('-v', '--verbose', action='store_const', 865 dest='loglevel', const=logging.DEBUG, 866 default=logging.WARNING, 867 help='enable info prints') 868 parser.add_argument('-d', '--diff', action='store_const', dest='diff', 869 const=True, default=False, 870 help='display differences between states') 871 parser.add_argument('-s', '--single-path', action='store_const', 872 dest='single_path', const=True, default=False, 873 help='only display the leftmost path in the graph ' 874 '(useful for trimmed graphs that still ' 875 'branch too much)') 876 parser.add_argument('--dark', action='store_const', dest='dark', 877 const=True, default=False, 878 help='dark mode') 879 parser.add_argument('--gray', action='store_const', dest='gray', 880 const=True, default=False, 881 help='black-and-white mode') 882 args = parser.parse_args() 883 logging.basicConfig(level=args.loglevel) 884 885 graph = ExplodedGraph() 886 with open(args.filename) as fd: 887 for raw_line in fd: 888 raw_line = raw_line.strip() 889 graph.add_raw_line(raw_line) 890 891 explorer = SinglePathExplorer() if args.single_path else BasicExplorer() 892 visitor = DotDumpVisitor(args.diff, args.dark, args.gray) 893 894 explorer.explore(graph, visitor) 895 896 897if __name__ == '__main__': 898 main() 899