PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/cleanup.py

#
Python | 160 lines | 158 code | 1 blank | 1 comment | 0 complexity | 752823297788bfd7e848b2376d25a10b MD5 | raw file
  1. # Cleanup bpl file produced by havoc
  2. usage = """
  3. Usage:
  4. python cleanup.py input.bpl output.bpl
  5. or
  6. python cleanup.py -d directory_name
  7. The latter assumes that both the input and output
  8. files are directory_name\\test.bpl.
  9. """
  10. import sys, re, os, ConfigParser, getopt
  11. from collections import deque
  12. inputFile = ''
  13. outputFile = ''
  14. # Main - this is where everything starts
  15. def main():
  16. if not len(sys.argv) == 3:
  17. print usage
  18. sys.exit()
  19. if sys.argv[1] == '-d':
  20. inputFile = sys.argv[2] + "\\test.bpl";
  21. outputFile = inputFile
  22. else:
  23. inputFile = sys.argv[1]
  24. outputFile = sys.argv[2]
  25. fin = open(inputFile, 'r')
  26. bpl = fin.read()
  27. fin.close();
  28. # Do the initial processing to get rid of unnecessary stuff put in by havoc
  29. bpl = initialProcessing(bpl);
  30. fout = open(outputFile, 'w')
  31. fout.write(bpl)
  32. fout.close()
  33. sys.exit()
  34. # Initial processing doing a few replacementes not falling into any category...
  35. # Most of this stuff should be done by HAVOC.
  36. def initialProcessing(bpl):
  37. p = re.compile('//TAG: alloc is always > 0.*?free ensures INT_LEQ\(old\(alloc\), alloc\);', re.DOTALL)
  38. bpl = p.sub('modifies alloc;', bpl)
  39. p = re.compile('free ensures .*;\n')
  40. bpl = p.sub('', bpl)
  41. p = re.compile('//TAG: havoc memory locations by default[ ]*\n')
  42. bpl = p.sub('', bpl)
  43. p = re.compile('//TAG: requires .*?\n')
  44. bpl = p.sub('', bpl)
  45. p = re.compile('requires INT_GEQ\(obj_size, 0\);\n')
  46. bpl = p.sub('free requires INT_GEQ(obj_size, 0);\n', bpl)
  47. p = re.compile('assume LOOP_([a-zA-Z0-9_]*)_Res_.+ == Res_.+;\n')
  48. bpl = p.sub('', bpl)
  49. p = re.compile('ensures old\(alloc\) <= alloc;\n')
  50. bpl = p.sub('', bpl)
  51. p = re.compile('[ ]*assume[ ]+INT_LT\(.+,[ ]*alloc\)[ ]*;[ ]*\n')
  52. bpl = p.sub('', bpl)
  53. p = re.compile('/\*assert \*/ assume INT_LEQ\(.+, alloc\);\n')
  54. bpl = p.sub('', bpl)
  55. # Removing the "name" type
  56. p = re.compile('type name;\n')
  57. bpl = p.sub('', bpl)
  58. p = re.compile('var Mem: \[name\]\[int\]int;\n')
  59. bpl = p.sub('', bpl)
  60. p = re.compile('function Field\(int\) returns \(name\);\n')
  61. bpl = p.sub('', bpl)
  62. p = re.compile('const unique .*:name;\n')
  63. bpl = p.sub('', bpl)
  64. p = re.compile('var LOOP_.*_Mem:\[name\]\[int\]int;\n')
  65. bpl = p.sub('', bpl)
  66. p = re.compile('LOOP_.*_Mem := Mem;\n')
  67. bpl = p.sub('', bpl)
  68. # Get rid of all Base stuff - sometimes those blow up
  69. p = re.compile('function Base\(int\) returns \(int\);\n')
  70. bpl = p.sub('', bpl)
  71. p = re.compile('//axiom\(forall x: int :: {Base\(x\)} Base\(x\) <= x\);\n')
  72. bpl = p.sub('', bpl)
  73. p = re.compile('axiom\(forall x: int :: {Base\(x\)} INT_LEQ\(Base\(x\), x\)\);\n')
  74. bpl = p.sub('', bpl)
  75. p = re.compile('axiom\(forall b:int, a:int, t:name :: {MatchBase\(b, a, T.Ptr\(t\)\)} MatchBase\(b, a, T.Ptr\(t\)\) <==> Base\(a\) == b\);\n')
  76. bpl = p.sub('', bpl)
  77. p = re.compile('axiom\(forall v:int, t:name :: {HasType\(v, T.Ptr\(t\)\)} HasType\(v, T.Ptr\(t\)\) <==> \(v == 0 \|\| \(INT_GT\(v, 0\) && Match\(v, t\) && MatchBase\(Base\(v\), v, t\)\)\)\);\n')
  78. bpl = p.sub('', bpl)
  79. p = re.compile('ensures Base\(new\) == new;\n')
  80. bpl = p.sub('', bpl)
  81. p = re.compile('axiom\(Base\(.+\) == .+\);\n')
  82. bpl = p.sub('', bpl)
  83. p = re.compile('assume \(Base\(.+\) == .+\);\n')
  84. bpl = p.sub('', bpl)
  85. p = re.compile('type byte;.*FourBytesToInt\(c0, c1, c2, c3\) ==> b0 == c0 && b1 == c1 && b2 == c2 && b3 == c3\);', re.DOTALL)
  86. bpl = p.sub('', bpl)
  87. p = re.compile('function Equal\(\[int\]bool, \[int\]bool\) returns \(bool\);.*Unified\(M\[Field\(x\) := M\[Field\(x\)\]\[x := y\]\]\) == Unified\(M\)\[x := y\]\);', re.DOTALL)
  88. bpl = p.sub('', bpl)
  89. p = re.compile('function Match\(a:int, t:name\) returns \(bool\);.*Field\(a\) == T.Ptr\(t\)\);', re.DOTALL)
  90. bpl = p.sub('', bpl)
  91. p = re.compile('axiom\(forall a:int, b:int :: {BIT_BAND\(a,b\)}.*{BIT_BAND\(a,b\)} a == 0 \|\| b == 0 ==> BIT_BAND\(a,b\) == 0\);', re.DOTALL)
  92. bpl = p.sub('', bpl)
  93. p = re.compile('axiom\(forall a:int, b:int :: {DIV\(a,b\)}.*a > b \* \(DIV\(a,b\) \+ 1\)[ ]*\n[ ]*\);', re.DOTALL)
  94. bpl = p.sub('', bpl)
  95. p = re.compile('function POW2\(a:int\) returns \(bool\);.*axiom POW2\(33554432\);', re.DOTALL)
  96. bpl = p.sub('', bpl)
  97. p = re.compile('procedure nondet_choice\(\) returns \(x:int\);.*ensures x == DetChoiceFunc\(old\(detChoiceCnt\)\);', re.DOTALL)
  98. bpl = p.sub('', bpl)
  99. p = re.compile('function .*Inv\(int\) returns \(int\);\n')
  100. bpl = p.sub('', bpl)
  101. p = re.compile('function _S_.*Inv\(\[int\]bool\) returns \(\[int\]bool\);\n')
  102. bpl = p.sub('', bpl)
  103. p = re.compile('function _S_.*\(\[int\]bool\) returns \(\[int\]bool\);\n')
  104. bpl = p.sub('', bpl)
  105. p = re.compile('axiom \(forall x:int :: {.*Inv\(.*\(x\)\)} .*Inv\(.*\(x\)\) == x\);\n')
  106. bpl = p.sub('', bpl)
  107. p = re.compile('axiom \(forall x:int :: {.*Inv\(x\)} .*\(.*Inv\(x\)\) == x\);\n')
  108. bpl = p.sub('', bpl)
  109. p = re.compile('axiom \(forall x:int, S:\[int\]bool :: {_S_.*\(S\)\[x\]} _S_.*\(S\)\[x\] <==> S\[.*Inv\(x\)\]\);\n')
  110. bpl = p.sub('', bpl)
  111. p = re.compile('axiom \(forall x:int, S:\[int\]bool :: {_S_.*Inv\(S\)\[x\]} _S_.*Inv\(S\)\[x\] <==> S\[.*\(x\)\]\);\n')
  112. bpl = p.sub('', bpl)
  113. p = re.compile('axiom \(forall x:int, S:\[int\]bool :: {S\[x\], _S_.*\(S\)} S\[x\] ==> _S_.*\(S\)\[.*\(x\)\]\);\n')
  114. bpl = p.sub('', bpl)
  115. p = re.compile('axiom \(forall x:int, S:\[int\]bool :: {_S_.*\(S\)\[x\]} _S_.*\(S\)\[x\] <==> S\[.*Inv\(x\)\]\);\n')
  116. bpl = p.sub('', bpl)
  117. p = re.compile('axiom \(forall x:int, S:\[int\]bool :: {S\[x\], _S_.*Inv\(S\)} S\[x\] ==> _S_.*Inv\(S\)\[.*Inv\(x\)\]\);\n')
  118. bpl = p.sub('', bpl)
  119. p = re.compile('//axiom \(forall x:int :: {.*Inv\(x\)} .*Inv\(x\) == x - .*\);\n')
  120. bpl = p.sub('', bpl)
  121. p = re.compile('axiom \(forall x:int :: {.*Inv\(x\)} .*Inv\(x\) == INT_SUB\(x,.+\)\);\n')
  122. bpl = p.sub('', bpl)
  123. p = re.compile('axiom \(forall x:int :: {MINUS_BOTH_PTR_OR_BOTH_INT\(x, .*, .*\)} MINUS_BOTH_PTR_OR_BOTH_INT\(x, .*, .*\) == .*Inv\(x\)\);\n')
  124. bpl = p.sub('', bpl)
  125. p = re.compile('axiom \(forall x:int :: {MINUS_LEFT_PTR\(x, .*, .*\)} MINUS_LEFT_PTR\(x, .*, .*\) == .*Inv\(x\)\);\n')
  126. bpl = p.sub('', bpl)
  127. p = re.compile('//axiom \(forall x:int :: {.+\(x\)} .+\(x\) == PLUS\(x, 1, [0-9]+\)\);\n')
  128. bpl = p.sub('', bpl)
  129. p = re.compile('axiom \(forall x:int :: {.+\(x\)} .+\(x\) == PLUS\(x, 1, [0-9]+\)\);\n')
  130. bpl = p.sub('', bpl)
  131. return bpl
  132. main()