/bin/cleanup.py
Python | 160 lines | 158 code | 1 blank | 1 comment | 0 complexity | 752823297788bfd7e848b2376d25a10b MD5 | raw file
- # Cleanup bpl file produced by havoc
- usage = """
- Usage:
- python cleanup.py input.bpl output.bpl
- or
- python cleanup.py -d directory_name
- The latter assumes that both the input and output
- files are directory_name\\test.bpl.
- """
- import sys, re, os, ConfigParser, getopt
- from collections import deque
- inputFile = ''
- outputFile = ''
- # Main - this is where everything starts
- def main():
- if not len(sys.argv) == 3:
- print usage
- sys.exit()
- if sys.argv[1] == '-d':
- inputFile = sys.argv[2] + "\\test.bpl";
- outputFile = inputFile
- else:
- inputFile = sys.argv[1]
- outputFile = sys.argv[2]
- fin = open(inputFile, 'r')
- bpl = fin.read()
- fin.close();
- # Do the initial processing to get rid of unnecessary stuff put in by havoc
- bpl = initialProcessing(bpl);
- fout = open(outputFile, 'w')
- fout.write(bpl)
- fout.close()
- sys.exit()
- # Initial processing doing a few replacementes not falling into any category...
- # Most of this stuff should be done by HAVOC.
- def initialProcessing(bpl):
- p = re.compile('//TAG: alloc is always > 0.*?free ensures INT_LEQ\(old\(alloc\), alloc\);', re.DOTALL)
- bpl = p.sub('modifies alloc;', bpl)
- p = re.compile('free ensures .*;\n')
- bpl = p.sub('', bpl)
- p = re.compile('//TAG: havoc memory locations by default[ ]*\n')
- bpl = p.sub('', bpl)
- p = re.compile('//TAG: requires .*?\n')
- bpl = p.sub('', bpl)
- p = re.compile('requires INT_GEQ\(obj_size, 0\);\n')
- bpl = p.sub('free requires INT_GEQ(obj_size, 0);\n', bpl)
- p = re.compile('assume LOOP_([a-zA-Z0-9_]*)_Res_.+ == Res_.+;\n')
- bpl = p.sub('', bpl)
- p = re.compile('ensures old\(alloc\) <= alloc;\n')
- bpl = p.sub('', bpl)
- p = re.compile('[ ]*assume[ ]+INT_LT\(.+,[ ]*alloc\)[ ]*;[ ]*\n')
- bpl = p.sub('', bpl)
- p = re.compile('/\*assert \*/ assume INT_LEQ\(.+, alloc\);\n')
- bpl = p.sub('', bpl)
- # Removing the "name" type
- p = re.compile('type name;\n')
- bpl = p.sub('', bpl)
- p = re.compile('var Mem: \[name\]\[int\]int;\n')
- bpl = p.sub('', bpl)
- p = re.compile('function Field\(int\) returns \(name\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('const unique .*:name;\n')
- bpl = p.sub('', bpl)
- p = re.compile('var LOOP_.*_Mem:\[name\]\[int\]int;\n')
- bpl = p.sub('', bpl)
- p = re.compile('LOOP_.*_Mem := Mem;\n')
- bpl = p.sub('', bpl)
- # Get rid of all Base stuff - sometimes those blow up
- p = re.compile('function Base\(int\) returns \(int\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('//axiom\(forall x: int :: {Base\(x\)} Base\(x\) <= x\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('axiom\(forall x: int :: {Base\(x\)} INT_LEQ\(Base\(x\), x\)\);\n')
- bpl = p.sub('', bpl)
- 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')
- bpl = p.sub('', bpl)
- 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')
- bpl = p.sub('', bpl)
- p = re.compile('ensures Base\(new\) == new;\n')
- bpl = p.sub('', bpl)
- p = re.compile('axiom\(Base\(.+\) == .+\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('assume \(Base\(.+\) == .+\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('type byte;.*FourBytesToInt\(c0, c1, c2, c3\) ==> b0 == c0 && b1 == c1 && b2 == c2 && b3 == c3\);', re.DOTALL)
- bpl = p.sub('', bpl)
- 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)
- bpl = p.sub('', bpl)
- p = re.compile('function Match\(a:int, t:name\) returns \(bool\);.*Field\(a\) == T.Ptr\(t\)\);', re.DOTALL)
- bpl = p.sub('', bpl)
- 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)
- bpl = p.sub('', bpl)
- p = re.compile('axiom\(forall a:int, b:int :: {DIV\(a,b\)}.*a > b \* \(DIV\(a,b\) \+ 1\)[ ]*\n[ ]*\);', re.DOTALL)
- bpl = p.sub('', bpl)
- p = re.compile('function POW2\(a:int\) returns \(bool\);.*axiom POW2\(33554432\);', re.DOTALL)
- bpl = p.sub('', bpl)
- p = re.compile('procedure nondet_choice\(\) returns \(x:int\);.*ensures x == DetChoiceFunc\(old\(detChoiceCnt\)\);', re.DOTALL)
- bpl = p.sub('', bpl)
- p = re.compile('function .*Inv\(int\) returns \(int\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('function _S_.*Inv\(\[int\]bool\) returns \(\[int\]bool\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('function _S_.*\(\[int\]bool\) returns \(\[int\]bool\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('axiom \(forall x:int :: {.*Inv\(.*\(x\)\)} .*Inv\(.*\(x\)\) == x\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('axiom \(forall x:int :: {.*Inv\(x\)} .*\(.*Inv\(x\)\) == x\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('axiom \(forall x:int, S:\[int\]bool :: {_S_.*\(S\)\[x\]} _S_.*\(S\)\[x\] <==> S\[.*Inv\(x\)\]\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('axiom \(forall x:int, S:\[int\]bool :: {_S_.*Inv\(S\)\[x\]} _S_.*Inv\(S\)\[x\] <==> S\[.*\(x\)\]\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('axiom \(forall x:int, S:\[int\]bool :: {S\[x\], _S_.*\(S\)} S\[x\] ==> _S_.*\(S\)\[.*\(x\)\]\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('axiom \(forall x:int, S:\[int\]bool :: {_S_.*\(S\)\[x\]} _S_.*\(S\)\[x\] <==> S\[.*Inv\(x\)\]\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('axiom \(forall x:int, S:\[int\]bool :: {S\[x\], _S_.*Inv\(S\)} S\[x\] ==> _S_.*Inv\(S\)\[.*Inv\(x\)\]\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('//axiom \(forall x:int :: {.*Inv\(x\)} .*Inv\(x\) == x - .*\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('axiom \(forall x:int :: {.*Inv\(x\)} .*Inv\(x\) == INT_SUB\(x,.+\)\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('axiom \(forall x:int :: {MINUS_BOTH_PTR_OR_BOTH_INT\(x, .*, .*\)} MINUS_BOTH_PTR_OR_BOTH_INT\(x, .*, .*\) == .*Inv\(x\)\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('axiom \(forall x:int :: {MINUS_LEFT_PTR\(x, .*, .*\)} MINUS_LEFT_PTR\(x, .*, .*\) == .*Inv\(x\)\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('//axiom \(forall x:int :: {.+\(x\)} .+\(x\) == PLUS\(x, 1, [0-9]+\)\);\n')
- bpl = p.sub('', bpl)
- p = re.compile('axiom \(forall x:int :: {.+\(x\)} .+\(x\) == PLUS\(x, 1, [0-9]+\)\);\n')
- bpl = p.sub('', bpl)
- return bpl
- main()