changeset 76:48093be5ebb5

Moved initialize-statement to flow.py And made sure it is called automatically Also removed all the now unuseful calls to it.
author Sigurd Meldgaard <stm@daimi.au.dk>
date Wed, 27 May 2009 15:14:07 +0200
parents 2cad6090a114
children 261228c35658
files pysmcl/flow.py pysmcl/ideal_functionality.py pysmcl/test/unit/test_flow.py pysmcl/test/unit/test_rangeanalysis.py pysmcl/test/unit/test_secret_annotator.py pysmcl/test/unit/test_secret_ifs.py
diffstat 6 files changed, 21 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/flow.py	Wed May 27 14:59:22 2009 +0200
+++ b/pysmcl/flow.py	Wed May 27 15:14:07 2009 +0200
@@ -52,7 +52,27 @@
         self.to_loop_exit = set()
         self.to_function_exit = set()
 
+    def init_statements(self, node):
+        """
+        Prepares all statements under node for further analysis.
+        """
+        #make sure to only do this once
+        if "initialized" in node.__dict__:
+            return
+        for child in ast.walk(node):
+            if(isinstance(child, ast.stmt)):
+                # children and parents are used for flow-graph edges
+                child.children = set()
+                child.parents = set()
+                # in_values and out_values are used for storing
+                # lattice-points in for static analyses using the monotone
+                # framework.
+                child.in_values = {"secret" : set(), "range": dict()}
+                child.out_values = {"secret" : set(), "range": dict()}
+        node.initialized = True
+
     def flow(self, function):
+        self.init_statements(function)
         #function.body.append(ast.Pass)
         function.parents = set()
         function.children = [function.body[0]]
--- a/pysmcl/ideal_functionality.py	Wed May 27 14:59:22 2009 +0200
+++ b/pysmcl/ideal_functionality.py	Wed May 27 15:14:07 2009 +0200
@@ -9,23 +9,6 @@
 import pysmcl.compatibility_check
 import pysmcl.proof_burden
 
-
-def init_statements(node):
-    """
-    Prepares all statements under node for further analysis.
-    """
-    for child in ast.walk(node):
-        if(isinstance(child, ast.stmt)):
-            # children and parents are used for flow-graph edges
-            child.children = set()
-            child.parents = set()
-            # in_values and out_values are used for storing
-            # lattice-points in for static analyses using the monotone
-            # framework.
-            child.in_values = defaultdict(set)
-            child.out_values = defaultdict(set)
-
-
 def ideal_functionality(f):
     source_str = inspect.getsource(f)
     source_ast = ast.parse(source_str) # Returns a module
--- a/pysmcl/test/unit/test_flow.py	Wed May 27 14:59:22 2009 +0200
+++ b/pysmcl/test/unit/test_flow.py	Wed May 27 15:14:07 2009 +0200
@@ -4,7 +4,6 @@
 import StringIO
 
 from ast import *
-from pysmcl.ideal_functionality import init_statements
 from pysmcl.range_analysis import RangeAnalysis, Bottom
 
 from pysmcl.graph import Graph
@@ -22,11 +21,10 @@
          a=a+1
     z=a+2-a
 """)
-        init_statements(prog)
         flow = Flow()
         flow.flow(prog.body[0])
         entry = flow.to_dot(prog.body[0])
-        output = StringIO.StringIO()        
+        output = StringIO.StringIO()
         Graph(entry).to_dot(output)
         expected_result = \
 """digraph G {
--- a/pysmcl/test/unit/test_rangeanalysis.py	Wed May 27 14:59:22 2009 +0200
+++ b/pysmcl/test/unit/test_rangeanalysis.py	Wed May 27 15:14:07 2009 +0200
@@ -3,7 +3,6 @@
 import unittest
 
 from ast import *
-from pysmcl.ideal_functionality import init_statements
 from pysmcl.range_analysis import RangeAnalysis, Bottom
 
 class RangeAnalysisTest(unittest.TestCase):
@@ -12,7 +11,6 @@
     def test_range_one_range(self):
         p = 7
         prog = parse("def f():\n\ty=0\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
 #         for node in prog.body[0].body:
@@ -23,7 +21,6 @@
     def test_range_too_large_constant(self):
         p = 7
         prog = parse("def f():\n\ty=8\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         self.assertEquals(prog.body[0].body[0].out_values["range"], {'y': (Bottom(), Bottom())})
@@ -31,7 +28,6 @@
     def test_range_unknown_range(self):
         p = 7
         prog = parse("def f(x):\n\tx\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[0].out_values["range"]
@@ -40,7 +36,6 @@
     def test_range_add(self):
         p = 7
         prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x+y\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[2].out_values["range"]
@@ -49,7 +44,6 @@
     def test_range_add_wrap(self):
         p = 7
         prog = parse("def f(x):\n\tx=6\n\ty=2\n\tz=x+y\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[2].out_values["range"]
@@ -58,7 +52,6 @@
     def test_range_add_bottom(self):
         p = 7
         prog = parse("def f(x):\n\tx=6\n\ty=8\n\tz=x+y\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[2].out_values["range"]
@@ -76,7 +69,6 @@
     y=y+1
     z=x+y
 """)
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[-1].out_values["range"]
@@ -86,7 +78,6 @@
     def test_range_mul(self):
         p = 7
         prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x*y\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[2].out_values["range"]
@@ -95,7 +86,6 @@
     def test_range_mul_wrap(self):
         p = 7
         prog = parse("def f(x):\n\tx=3\n\ty=3\n\tz=x*y\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[2].out_values["range"]
@@ -112,7 +102,6 @@
          y=y+3
     z=x*y
 """)
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[-1].out_values["range"]
@@ -130,7 +119,6 @@
     y=y+1
     z=x*y
 """)
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[-1].out_values["range"]
@@ -147,7 +135,6 @@
          a=a+1
     z=a+2-a
 """)
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[-1].out_values["range"]
@@ -156,7 +143,6 @@
     def test_range_minus_wrap(self):
         p = 7
         prog = parse("def f(x):\n\tx=3\n\ty=4\n\tz=x-y\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[2].out_values["range"]
@@ -165,7 +151,6 @@
     def test_range_minus_bottom(self):
         p = 7
         prog = parse("def f(x):\n\tx=6\n\ty=8\n\tz=x-y\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[2].out_values["range"]
@@ -183,7 +168,6 @@
     y=y-2
     z=y-x
 """)
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[-1].out_values["range"]
@@ -192,7 +176,6 @@
     def test_range_equals(self):
         p = 7
         prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x==y\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[2].out_values["range"]
@@ -201,7 +184,6 @@
     def test_range_leg(self):
         p = 7
         prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x<=y\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[2].out_values["range"]
@@ -210,7 +192,6 @@
     def test_range_geg(self):
         p = 7
         prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x>=y\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[2].out_values["range"]
@@ -219,7 +200,6 @@
     def test_range_multiple_compare(self):
         p = 7
         prog = parse("def f(x):\n\tx=3\n\ty=2\n\tz=x>y<=42\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[2].out_values["range"]
@@ -228,7 +208,6 @@
     def test_range_random(self):
         p = 7
         prog = parse("def f():\n\tx=random()\n")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[0].out_values["range"]
@@ -237,7 +216,6 @@
     def test_range_random_bit(self):
         p = 7
         prog = parse("def f(x):\n\tx=random_bit()")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[0].out_values["range"]
@@ -246,7 +224,6 @@
     def test_range_global_variable(self):
         p = 7
         prog = parse("def f():\n\tx")
-        init_statements(prog)
         range_analysis = RangeAnalysis(p)
         range_analysis.apply(prog.body[0])
         r = prog.body[0].body[0].out_values["range"]
@@ -255,7 +232,6 @@
 #     def test_range_if(self):
 #         p = 7
 #         prog = parse("def f(x):\n\ty=1\n\tif(x>1):\n\t\ty=2\n\telse:\n\t\ty=3")
-#         init_statements(prog)
 #         range_analysis = RangeAnalysis(p)
 #         range_analysis.apply(prog.body[0])
 #         r = prog.body[0].body[1].out_values["range"]
@@ -264,7 +240,6 @@
 #     def test_range_while(self):
 #         p = 7
 #         prog = parse("def f(x):\n\tx=3\n\ty=1\n\twhile(x>0):\n\t\ty+=1\n\t\tx-=1")
-#         init_statements(prog)
 #         range_analysis = RangeAnalysis(p)
 #         range_analysis.apply(prog.body[0])
 #         r = prog.body[0].body[4].out_values["range"]
@@ -273,7 +248,6 @@
 #     def test_range_multiple_assign_and_tuples(self):
 #         p = 7
 #         prog = parse("def f():\n\tx,y = (1, 2)")
-#         init_statements(prog)
 #         range_analysis = RangeAnalysis(p)
 #         range_analysis.apply(prog.body[0])
 #         r = prog.body[0].body[0].out_values["range"]
@@ -282,7 +256,6 @@
 #     def test_range_multiple_assign(self):
 #         p = 7
 #         prog = parse("def f():\n\tx,y = 1, 2")
-#         init_statements(prog)
 #         range_analysis = RangeAnalysis(p)
 #         range_analysis.apply(prog.body[0])
 #         r = prog.body[0].body[0].out_values["range"]
@@ -291,7 +264,6 @@
 #     def test_range_list(self):
 #         p = 7
 #         prog = parse("def f():\n\tz=4\n\tx = [1, z]")
-#         init_statements(prog)
 #         range_analysis = RangeAnalysis(p)
 #         range_analysis.apply(prog.body[0])
 #         r = prog.body[0].body[0].out_values["range"]
@@ -300,7 +272,6 @@
 #     def test_range_dict(self):
 #         p = 7
 #         prog = parse("def f():\n\tz=4\n\tx = {1, z}")
-#         init_statements(prog)
 #         range_analysis = RangeAnalysis(p)
 #         range_analysis.apply(prog.body[0])
 #         r = prog.body[0].body[0].out_values["range"]
@@ -310,7 +281,6 @@
 #         # Todo: unaryop = Invert | Not | UAdd | USub
 #         p = 7
 #         prog = parse("def f():\n\tz=4\n\tx = not (x == 1)")
-#         init_statements(prog)
 #         range_analysis = RangeAnalysis(p)
 #         range_analysis.apply(prog.body[0])
 #         r = prog.body[0].body[0].out_values["range"]
--- a/pysmcl/test/unit/test_secret_annotator.py	Wed May 27 14:59:22 2009 +0200
+++ b/pysmcl/test/unit/test_secret_annotator.py	Wed May 27 15:14:07 2009 +0200
@@ -5,7 +5,6 @@
 from ast import *
 
 from pysmcl.pretty_print import *
-from pysmcl.ideal_functionality import init_statements
 from pysmcl.secret_ifs import TransformIfs
 from pysmcl.secret_annotator import secret_analysis
 
@@ -14,7 +13,6 @@
 
     def test_range_one_range(self):
         prog = parse("def f(x):\n\tx\n")
-        init_statements(prog)
         secret_analysis(prog.body[0])
         self.assertEquals(prog.body[0].body[0].out_values["secret"], set(['x']))
 
--- a/pysmcl/test/unit/test_secret_ifs.py	Wed May 27 14:59:22 2009 +0200
+++ b/pysmcl/test/unit/test_secret_ifs.py	Wed May 27 15:14:07 2009 +0200
@@ -6,7 +6,6 @@
 from ast import *
 
 from pysmcl.pretty_print import *
-from pysmcl.ideal_functionality import init_statements
 from pysmcl.secret_ifs import TransformIfs
 from pysmcl.secret_annotator import secret_analysis
 
@@ -29,7 +28,6 @@
         a=2
 """)
         trans = TransformIfs()
-        init_statements(prog)
         secret_analysis(prog.body[0])
         prog = trans.visit(prog)
         output = StringIO.StringIO()