def execute(self, toVersionizeList, rawPredicateContentDict, sqlTableInfo, originalRawPredList): smartList = list( ) # analogous to toVersionizeList, format : [ [x], ..., [y, y+1], ..., [z, z+1] ] sqlFileStr = self.generateBooleanVcPlsqlFileString( toVersionizeList, rawPredicateContentDict, originalRawPredList, smartList) file = open('mc/boolean_vc_plsql_file.sql', "w") file.write(sqlFileStr) file.close() input = FileStream('mc/boolean_vc_plsql_file.sql') lexer = PlSqlLexer(input) stream = CommonTokenStream(lexer) parser = PlSqlParser(stream) tree = parser.sql_script() cfg = MyCFG() self.cfg = cfg helper = MyHelper(parser) self.helper = helper self.helper.updateTableDict(sqlTableInfo) utility = MyUtility(self.helper) v = MyVisitor(parser, self.cfg, utility) v.visit(tree) res = MyRawCfgToGraph(v.rawCFG, self.cfg) res.execute() utility.generateDomSet(self.cfg) utility.generateSDomSet(self.cfg) utility.generatIDom(self.cfg) utility.generateDFSet(self.cfg) utility.insertPhiNode(self.cfg) utility.initialiseVersinosedPhiNode(self.cfg) utility.versioniseVariable(self.cfg) utility.phiDestruction(self.cfg) ssaStringObj = MySsaStringGenerator(self.cfg, parser) ssaStringObj.execute() # print('{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{') # for nodeId in self.cfg.nodes: # self.cfg.nodes[nodeId].printPretty() # print('{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{') versionizedPredicateList = self.processSmartList( smartList, rawPredicateContentDict, toVersionizeList) # last element of smartList will be versionized ConsequentList node ids nodeList = smartList[len(smartList) - 1] versionizedConsequentList = list() for i in nodeList: tempCond = self.getEquivalentPredicateForANode(i).strip() versionizedConsequentList.append(tempCond) return versionizedPredicateList, self.variablesForZ3, versionizedConsequentList
def SeVc(self, cfg, ctx): for nodeId in cfg.nodes: ruleName = MyHelper(ctx).getRuleName(ctx) if ruleName == "assignment_statement": if len(list( cfg.nodes[nodeId].versionedLHS.values())) == 1 and len( list( cfg.nodes[nodeId].versionedRHS.values())) == 1: path = path + "(" + list( cfg.nodes[nodeId].versionedLHS.values()) + "==" + list( cfg.nodes[nodeId].versionedRHS.values()) + ")" print(path) print("hi")
def execute(tableInfo, predicates, rawPredicateContent, predicateVarSet, resultString, dataFileName, specFileName): file = open('mc/upper_input.sql', "w") file.write(resultString) file.close() # recording startTime1 startTime1 = datetime.datetime.now() input = FileStream('mc/upper_input.sql') lexer = PlSqlLexer(input) stream = CommonTokenStream(lexer) parser = PlSqlParser(stream) tree = parser.sql_script() cfg = MyCFG() helper = MyHelper(parser) helper.updateTableDict(tableInfo) utility = MyUtility(helper) v = MyVisitor(parser, cfg, utility) v.visit(tree) # print("\n--- Raw CFG >>>\t", v.rawCFG, "\n") res = MyRawCfgToGraph(v.rawCFG, cfg) res.execute() mcCfg = MyCFG() for nodeId in cfg.nodes: tempNode = copyNode(cfg.nodes[nodeId]) mcCfg.nodes[nodeId] = tempNode utility.generateVariableSet(mcCfg) ssaString = MySsaStringGenerator(mcCfg, parser) wpcObj = WpcGenerator(mcCfg, helper, ssaString) mcUtility = McUtility(mcCfg, wpcObj, predicateVarSet) # print("mc graph------------->") # for nodeId in mcCfg.nodes: # mcCfg.nodes[nodeId].printPretty() for i in mcCfg.nodes: if mcCfg.nodes[i].ctx is not None: print(i, mcCfg.nodes[i].ctx.getText()) else: print(i, "ctx = None") # print("\n++++++++++++++++++++++\tPredicates Given in SPEC file:") # for i in predicates: # print(i) # print("++++++++++++++++++++++\n") # mcCfg.dotToPng(cfg.dotGraph, "mc/raw_graph") mcExecutor = McExecutor() # recording endTime1 endTime1 = datetime.datetime.now() pwd = os.getcwd() pwd = pwd + "/" sePathsInfoForMc = SePathsInfoForMc() sePathList, seSatInfoList = sePathsInfoForMc.execute( dataFileName, specFileName, pwd) pathCount = len(sePathList) predicateCount = len(predicates) print("sePathList", sePathList) print("seSatInfoList", seSatInfoList) paths = [] mcExecutor.getAllPaths(mcCfg, 0, [], paths) print("mcPaths", paths) # recording startTime2 startTime2 = datetime.datetime.now() print( "********CDCDCDCDCDCDCDCD******** Entered into McExcuter ********CDCDCDCDCDCDCDCD********" ) spuriousCount, refinementCount = mcExecutor.execute( mcUtility, predicates, rawPredicateContent, sePathList, seSatInfoList, tableInfo) print( "********CDCDCDCDCDCDCDCD******** Exited from McExcuter ********CDCDCDCDCDCDCDCD********\n" ) # recording endTime2 endTime2 = datetime.datetime.now() timeForMcExcludingSe = ((endTime1 - startTime1) + (endTime2 - startTime2)).total_seconds() return timeForMcExcludingSe, pathCount, predicateCount, spuriousCount, refinementCount
class CnfVcGenerator(PlSqlVisitor): def __init__(self, cnfCfg, parser): self.cnfCfg = cnfCfg self.parser = parser self.helper = MyHelper(self.parser) def generateCnfVc(self, path): global vcs vcs = "True" for node in path: context = self.cnfCfg.nodes[node].ctx nodeCondition = self.getNodeCondition(node) self.cnfCfg.nodes[node].antecedent = nodeCondition if context is not None: ruleName = self.helper.getRuleName(context) if ruleName == "condition": #vcs = "AND(" + vcs + ", " + self.getCondition(node, context) + ")" pass if ruleName == "assignment_statement": #vcs = "AND(" + vcs + ", " + nodeCondition + ", " + self.getAssignment_statement(node, context) + ")" self.cnfCfg.nodes[node].consequent.append(self.getAssignment_statement(node, context)) if ruleName == "update_statement": #vcs = "AND(" + vcs + ", " + nodeCondition + ", " + self.getUpdate_statement(node, context) + ")" self.cnfCfg.nodes[node].consequent.append( self.getUpdate_statement(node, context)) if ruleName == "insert_statement": self.cnfCfg.nodes[node].consequent = self.getInsert_statement(node, context) if ruleName == "cursor_declaration": #vcs = "AND(" + vcs + ", " + nodeCondition + ", " + self.getCursor_declaration(node, context) + ")" self.cnfCfg.nodes[node].consequent.append( self.getCursor_declaration(node, context)) if ruleName == "fetch_statement": #vcs = "AND(" + vcs + ", " + nodeCondition + ", " + self.getFetch_statement(node, context) + ")" self.cnfCfg.nodes[node].consequent.append( self.getFetch_statement(node, context)) if ruleName == "select_statement": self.cnfCfg.nodes[node].consequent = self.getSelect_statement(node, context) #todo: alter the function in case of insert statement if ruleName == "assume_statement": #vcs = "AND(" + vcs + ", " + nodeCondition + ", " + self.getAssume_statement(node, context) + ")" self.cnfCfg.nodes[node].consequent.append( self.getConditionalString(node, context.children[1])) if ruleName == "assert_statement": #vcs = "Implies(" + vcs + ", " + self.getAssert_statement(node, context) + ")" self.cnfCfg.nodes[node].consequent.append( self.getConditionalString(node, context.children[1])) self.cnfCfg.nodes[node].isAssertion = True self.cnfCfg.nodes[node].antecedent = [""] else: pass if True: #todo: rearrange it properly if self.cnfCfg.nodes[node].destructedPhi: for element in self.cnfCfg.nodes[node].destructedPhi: values = self.cnfCfg.nodes[node].destructedPhi[element] #vcs = "AND(" + vcs + ", " + values[0] + " == " + values[1] + ")" self.cnfCfg.nodes[node].consequent.append(values[0] + " == " + values[1]) # print(vcs) return vcs def getNodeCondition(self, nodeId): res = [] for ancestor in self.cnfCfg.nodes[nodeId].parentBranching: if self.nullInCondition(self.cnfCfg.nodes[ancestor].ctx): continue if self.cnfCfg.nodes[nodeId].parentBranching[ancestor] == "true": #res = "AND( " + res + ", " + + " )" res.append('( ' + self.getConditionalString(ancestor, self.cnfCfg.nodes[ancestor].ctx) + ' )') else: #res = "AND( " + res + ", ""NOT( " + + " ))" res.append("( ! ( " + self.getConditionalString(ancestor, self.cnfCfg.nodes[ancestor].ctx) + " ) )") if len(res) == 0: return ["( True )"] else: return res def getAssume_statement(self, nodeId, ctx): # global vcs res = self.getWhereexpr(nodeId, ctx.children[1]) return res def getAssert_statement(self, nodeId, ctx): res = self.getWhereexpr(nodeId, ctx.children[1]) return res def getSelect_statement(self, nodeId, ctx): global vcs nodeCondition = self.getNodeCondition(nodeId) res = [] whereIndex = -1 tempCtx = ctx.children[0].children[0] rhsList = [] lhsList = [] fromCondtion = "" whereStr = "" isNullPresent = False for i in range(tempCtx.getChildCount()): if self.helper.getRuleName(tempCtx.children[i]) == 'where_clause': whereIndex = i whereStr = self.getConditionalString(nodeId, tempCtx.children[whereIndex].children[1]) isNullPresent = self.nullInCondition(tempCtx.children[whereIndex].children[1]) break elif self.helper.getRuleName(tempCtx.children[i]) == 'selected_element': varStr = self.getVersionedTerminalRHS(nodeId,tempCtx.children[i]).strip() varStr = self.getVariableForAggregateFunctionInSelect(varStr) self.cnfCfg.nodes[nodeId].versionedRHS[varStr] = varStr rhsList.append(varStr) elif self.helper.getRuleName(tempCtx.children[i]) == 'into_clause': for j in range(tempCtx.children[i].getChildCount()): if self.helper.getRuleName(tempCtx.children[i].children[j]) == 'variable_name': lhsList.append(self.getVersionedTerminalLHS(nodeId, tempCtx.children[i].children[j]).strip()) elif self.helper.getRuleName(tempCtx.children[i]) == 'from_clause': fromCondtion = self.extractConditionsInFromClause(nodeId, tempCtx.children[i].children[1]) finalCondition = "" if whereIndex > -1: if isNullPresent: if fromCondtion == '': finalCondition = "" else: finalCondition = fromCondtion else: if fromCondtion == '': finalCondition = whereStr else: finalCondition = '( ( ' + whereStr + ' ) ^ ( ' + fromCondtion + ' ) )' else: if fromCondtion == '': finalCondition = '' else: finalCondition = fromCondtion if finalCondition == '': if len(rhsList) != len(lhsList): print("\n\n\t********************************************************* wrong select statement****************\n\n") return res else: for i in range(len(rhsList)): res.append('( ( ' + lhsList[i] + ' ) == ( ' + rhsList[i] + ' ) )') # whereStr = self.getConditionalString(nodeId, tempCtx.children[whereIndex].children[1]) # if len(rhsList) != len(lhsList): # print("\n\n\t********************************************************* wrong select statement****************\n\n") # return res # else: # for i in range(len(rhsList)): # res.append('( ( ( ' + lhsList[i] + ' ) == ( ' + rhsList[i] + ' ) ) ^ ( ( ' + whereStr + ' ) ^ ( ' + + ' ) )') else: if len(rhsList) != len(lhsList): print("\n\n\t********************************************************* wrong select statement****************\n\n") else: for i in range(len(rhsList)): res.append('( ( ( ' + lhsList[i] + ' ) == ( ' + rhsList[i] + ' ) ) ^ ( ' + finalCondition + ' ) )') # # print(ctx.children[0].children[0].getChildCount()) # # print(self.helper.getRuleName(ctx.children[0])) # # input("Hi") # # vcs = "AND(" + vcs + ", " + nodeCondition + ", " + self.getVersionedTerminalRHS(nodeId, # # ctx.children[0].children[0].children[1]) + "==" + \ # # self.getInto_clause(nodeId, ctx.children[0].children[0].children[2]) + ")" # # vcs = "AND(" + vcs + ", " + nodeCondition + ", " + self.getWhereClause(nodeId, ctx.children[0].children[0].children[4]) + ")" # temp = "( ( ( " + self.getVersionedTerminalRHS(nodeId,ctx.children[0].children[0].children[1]) + " ) == ( " + self.getInto_clause(nodeId, ctx.children[0].children[0].children[2]) + " ) ) ^ ( " + self.getWhereClause(nodeId, ctx.children[0].children[0].children[4]) + " ) )" # # res.append( + "==" + ) # res.append(temp) return res # input("Wait") def getInto_clause(self, nodeId, ctx): return self.getVersionedTerminalLHS(nodeId, ctx.children[1]) def getFetch_statement(self, nodeId, ctx): # global vcs res = "( ( " + self.getVersionedTerminalRHS(nodeId, ctx.children[1]) + " ) == ( " + self.getVersionedTerminalLHS(nodeId, ctx.children[3]) + " ) )" return res def getCursor_declaration(self, nodeId, ctx): # global vcs lhsVar = "" rhsVar = "" whereCondition = "" isWherePresent = False isNullPresentInWhere = False fromCondtion = "" res = "" for i in range(ctx.getChildCount()): if self.helper.getRuleName(ctx.children[i]) == "cursor_name": #lhsVar = ctx.children[i].getText().strip() lhsVar = self.getVersionedTerminalLHS(nodeId, ctx.children[i]).strip() elif self.helper.getRuleName(ctx.children[i]) == "select_statement": tempCtx = ctx.children[i].children[0].children[0] for j in range(tempCtx.getChildCount()): if self.helper.getRuleName(tempCtx.children[j]) == "where_clause": isWherePresent = True if self.nullInCondition(tempCtx.children[j].children[1]): isNullPresentInWhere = True else: whereCondition = self.getConditionalString(nodeId, tempCtx.children[j].children[1]) elif self.helper.getRuleName(tempCtx.children[j]) == "from_clause": fromCondtion = self.extractConditionsInFromClause(nodeId, tempCtx.children[j].children[1]) varStr = self.getVersionedTerminalRHS(nodeId, tempCtx.children[1]).strip() rhsVar = self.getVariableForAggregateFunctionInSelect(varStr) self.cnfCfg.nodes[nodeId].versionedRHS[rhsVar] = rhsVar finalCondition = '' if isWherePresent: if isNullPresentInWhere: if fromCondtion == '': finalCondition = '' else: finalCondition = fromCondtion else: if fromCondtion == '': finalCondition = whereCondition else: finalCondition = '( ( ' + whereCondition + ' ) ^ ( ' + fromCondtion + ' ) )' else: if fromCondtion == '': finalCondition = '' else: finalCondition = fromCondtion if not (lhsVar == "") and not (rhsVar == ""): if finalCondition == '': res = "( ( " + lhsVar + " ) == ( " + rhsVar + " ) )" else: res = "( ( ( " + lhsVar + " ) == ( " + rhsVar + " ) ) ^ ( " + finalCondition + " ) )" # if ctx.children[3].children[0].children[0].getChildCount() == 4: # res = "( ( ( " + self.getVersionedTerminalLHS(nodeId, ctx.children[1]) + " ) == ( " + varStr + " ) ) ^ ( " + self.getWhereClause(nodeId, ctx.children[3].children[0].children[0].children[3]) + " ) )" # else: # res = "( ( " + self.getVersionedTerminalLHS(nodeId, ctx.children[1]) + " ) == ( " + varStr + " ) )" return res def getCondition(self, nodeId, ctx): res = self.getVersionedTerminalRHS(nodeId, ctx) res = res.replace(" ", " ") res = res.strip() return res def getAssignment_statement(self, node, ctx): # global vcs varString = self.getVersionedTerminalRHS(node, ctx.children[2]) varString = varString.replace("( )", "") varString = varString.replace(" ", " ").strip() replacedBy = "( " + varString + " )" res = '( ' + self.getVersionedTerminalLHS(node, ctx.children[0]) + ' ) == ( ' + replacedBy + ' )' # if self.cnfCfg.nodes[node].destructedPhi: # for element in self.cnfCfg.nodes[node].destructedPhi: # values = self.cnfCfg.nodes[node].destructedPhi[element] # this # res = "AND(" + res + ", " + values[0] + "==" + values[1] + ")" return res # print(vcs) def getUpdate_statement(self, nodeId, ctx): whereFlag = -1 for i in range(ctx.getChildCount()): if self.helper.getRuleName(ctx.children[i]) == 'where_clause': whereFlag = i break if whereFlag > -1 and not self.nullInCondition(ctx.children[whereFlag].children[1]): res = "( ( ( " + self.getSetClause(nodeId, ctx.children[2]) + " ) ^ ( " + self.getConditionalString(nodeId, ctx.children[whereFlag].children[1]) + " ) ) v ( ( ! ( " + self.getConditionalString(nodeId, ctx.children[whereFlag].children[1]) + " ) ) ^ ( " + self.getNotSetClause(nodeId, ctx.children[2]) + " ) ) )" else: res = self.getSetClause(nodeId, ctx.children[2]) # # global vcs # # res = [] # # res = "OR(" + "AND(" + self.getSetClause(nodeId, ctx.children[2]) + "," + self.getWhereClause(nodeId, ctx.children[3]) + ")" + ", " + "AND(" + "NOT(" + self.getWhereClause(nodeId, ctx.children[3]) + ")" + ", " + self.getNotSetClause(nodeId, ctx.children[2]) + "))" # res = "( ( ( "+ self.getSetClause(nodeId, ctx.children[2]) + " ) ^ ( " + self.getWhereClause(nodeId, ctx.children[3]) + " ) ) v ( ( ! ( " + self.getWhereClause(nodeId, ctx.children[3]) + " ) ) ^ ( " + self.getNotSetClause(nodeId, ctx.children[2]) + " ) ) )" # res.append("OR(" + "AND(" + self.getSetClause(nodeId, ctx.children[2]) + "," \ # + self.getWhereClause(nodeId, ctx.children[3]) + ")" + ", " + \ # "AND(" + "NOT(" + self.getWhereClause(nodeId, ctx.children[3]) + ")" + ", " + \ # self.getNotSetClause(nodeId, ctx.children[2]) + "))") # if self.cnfCfg.nodes[nodeId].destructedPhi: # for element in self.cnfCfg.nodes[nodeId].destructedPhi: # values = self.cnfCfg.nodes[nodeId].destructedPhi[element] # res = "AND(" + res + ", " + values[0] + "==" + values[1] + ")" return res def getSetClause(self, NodeId, ctx): child = ctx.getChildCount() res = self.getColsetClause(NodeId, ctx.children[1]) for i in range(2, child): if self.helper.getRuleName(ctx.children[i]) == "column_based_update_set_clause": res = '( ' + res + ' ^ ' + self.getColsetClause(NodeId, ctx.children[i]) + ' )' # # print(child) # # input("wait") # res = self.getColsetClause(NodeId, ctx.children[1]) return res def getColsetClause(self, nodeId, ctx): res = "( ( " + self.getVersionedTerminalLHS(nodeId, ctx.children[0]).strip() + ' ) == ( ' + self.getVersionedTerminalRHS(nodeId, ctx.children[2]).strip() + " ) )" return res def getNotSetClause(self, nodeId, ctx): child = ctx.getChildCount() res = self.getNotColsetClause(nodeId, ctx.children[1]) for i in range(2, child): if self.helper.getRuleName(ctx.children[i]) == "column_based_update_set_clause": res = '( ' + res + ' ^ ' + self.getNotColsetClause(nodeId, ctx.children[i]) + ' )' return res def getNotColsetClause(self, nodeId, ctx): res = "( ( " + self.getVersionedTerminalLHS(nodeId, ctx.children[0]).strip() + ' ) == ( ' + self.getVersionedTerminalRHS(nodeId, ctx.children[0]).strip() + " ) )" return res def getWhereClause(self, nodeId, ctx): res = self.getWhereexpr(nodeId, ctx.children[1]) return res def getWhereexpr(self, nodeId, ctx): # global vcs # opr = ctx.children[1].getText() # oper = self.getTerminal(ctx.children[1]) # print(self.helper.getRuleName(ctx.children[1])) # print(ctx.children[1].getText()) # temp = 'AND' # print(opr == temp) # input("hi") # print(type("AND")) # print(self.helper.getRuleName(ctx.children[1])) # print(self.getTerminal(ctx.children[1])) # input("Wait") ctx.children[1].getText() if ctx.children[1].getText() == "AND": # print(self.helper.getRuleName(ctx.children[0])) res = "( ( " + self.getWhereexpr(nodeId, ctx.children[0]) + " ) ^ ( " + self.getWhereexpr(nodeId, ctx.children[2]) + " ) )" # input("wait") elif ctx.children[1].getText() == "IN": # res = + "==" + + "&&" + res = "( ( ( " + self.getVersionedTerminalRHS(nodeId, ctx.children[0]).strip() + " ) == ( " + self.getVersionedTerminalRHS(nodeId, ctx.children[3].children[0].children[1]).strip() + " ) ) ^ ( " + self.getWhereexpr(nodeId, ctx.children[3].children[0].children[3].children[1]) + " ) )" # print(res) # input("Wait") # input("HI") else: # print(self.helper.getRuleName(ctx.children[1])) # print(self.getTerminal(ctx.children[1])) res = "( " + self.getVersionedTerminalRHS(nodeId, ctx.children[0]) + self.getTerminal(ctx.children[1]) + self.getVersionedTerminalRHS(nodeId, ctx.children[2]) + " )" return res # c = ctx.getChildCount() # print(self.helper.getRuleName(ctx.children[1])) # print(self.getTerminal(ctx.children[1])) # input("Enter the value") # res = "" # res = res + self.getVersionedTerminalRHS(nodeId, ctx) # return res ''' for i in range(c): if ctx.children[i].getChildCount() > 1: res = res + self.getWhereexpr(nodeId, ctx.children[i]) if ctx.children[0].getChildCount() == 3: res = "" res = res + "&&" + self.getWhereexpr(nodeId, ctx.children[0]) else: res = self.getVersionedTerminalRHS(nodeId, ctx) ''' # print(self.helper.getRuleName(ctx.children[0].children[0])) # print(ctx.children[0].children[2].getChildCount()) # input('enter the vlue') # if self.helper.getRuleName(ctx) == "RelExpr": # return res # res = self.getVersionedTerminalRHS(nodeId, ctx.children[0]) +' == '+ \ # self.getVersionedTerminalRHS(nodeId, ctx.children[2]) def getInsert_statement(self, nodeId, ctx): global vcs return self.getInsertIntoandValueClause(nodeId, ctx.children[1]) #return vcs def getInsertIntoandValueClause(self, nodeId, ctx): global vcs nodeCondition = self.getNodeCondition(nodeId) colmnlistinto = [] colmnlistvalues = [] res = [] c1 = ctx.children[0].getChildCount() for item in range(c1): if self.helper.getRuleName(ctx.children[0].children[item]) == "column_name": # pass colmnlistinto.append(self.getVersionedTerminalLHS(nodeId, ctx.children[0].children[item])) else: pass c2 = ctx.children[1].children[1].getChildCount() for i in range(c2): if self.helper.getRuleName(ctx.children[1].children[1].children[i]) == "expression": colmnlistvalues.append(self.getVersionedTerminalRHS(nodeId, ctx.children[1].children[1].children[i])) else: pass for element in range(len(colmnlistinto)): #vcs = "AND(" + vcs + ", " + nodeCondition + ", " + colmnlistinto[element] + " == " + colmnlistvalues[element] + ")" res.append("( ( " + colmnlistinto[element] + " ) == ( " + colmnlistvalues[element] + " ) )") # print("\n\n^^^^^^^^^^^^^^^^^^Insert result^^^^^^^^^^") # print(len(res)) # print("\n\n") return res # c= str(c1) + " " + str(c2) # return colmnlistvalues # return self.helper.getRuleName(ctx) # def getInsertValueClause(self, nodeId, ctx): # pass def getVersionedTerminalRHS(self, nodeId, ctx): c = ctx.getChildCount() if c == 0: if ctx.getText() in self.cnfCfg.nodes[nodeId].versionedRHS.keys(): return self.cnfCfg.nodes[nodeId].versionedRHS[ctx.getText()] + " " else: return str(ctx) + " " else: res = "" for i in range(c): res = res + self.getVersionedTerminalRHS(nodeId, ctx.children[i]) return res def getVersionedTerminalLHS(self, nodeId, ctx): c = ctx.getChildCount() if c == 0: if str(ctx) in self.cnfCfg.nodes[nodeId].versionedLHS.keys(): return self.cnfCfg.nodes[nodeId].versionedLHS[str(ctx)] + " " else: return str(ctx) + " " else: res = "" for i in range(c): res = res + self.getVersionedTerminalLHS(nodeId, ctx.children[i]) return res def getTerminal(self, ctx): if ctx == None: return "" c = ctx.getChildCount() if c == 0: return str(ctx) + " " else: res = "" for i in range(c): res = res + self.getTerminal(ctx.children[i]) return res def extractConditionsInFromClause(self, nodeId, ctx): # ctx ~ from_clause.children[1] if self.helper.getRuleName(ctx) == "table_ref": if ctx.getChildCount() == 2: leftCondition = self.extractConditionsInFromClause(nodeId, ctx.children[0]) rightCondition = self.extractConditionsInFromClause(nodeId, ctx.children[1]) if leftCondition == "" and rightCondition == "": return "" elif leftCondition == "": return rightCondition elif rightCondition == "": return leftCondition else: return "( " + leftCondition + " ^ " + rightCondition + " )" elif ctx.getChildCount() == 1: # one can get Table Name from here... notImportant = "notImportant" return "" elif self.helper.getRuleName(ctx) == "join_clause": condition = "" for i in range(ctx.getChildCount()): if self.helper.getRuleName(ctx.children[i]) == "table_ref": # one can get Table Name from here... notImportant = "notImportant" elif self.helper.getRuleName(ctx.children[i]) == "join_on_part": condition = self.getConditionalString(nodeId, ctx.children[i].children[1].children[0]) return condition def getConditionalString(self, nodeId, ctx): # considering only AND, OR, NOT as 'word' separator if ctx.getChildCount() == 1: return self.getConditionalString(nodeId, ctx.children[0]) elif ctx.getChildCount() == 2: # strictly for "NOT" if ctx.children[0].getText().strip() == "NOT": return "( ! " + self.getConditionalString(nodeId, ctx.children[1]) + " )" elif ctx.getChildCount() == 3: operators = ['=', '>', '<', '>=', '<=', '!=', '<>', '^=', '~='] if ctx.children[1].getText().strip() == "AND": # conditions separated by "AND" return "( " + self.getConditionalString(nodeId, ctx.children[0]) + " ^ " + self.getConditionalString(nodeId, ctx.children[2]) + " )" elif ctx.children[1].getText().strip() == "OR": # conditions separated by "OR" return "( " + self.getConditionalString(nodeId, ctx.children[0]) + " v " + self.getConditionalString(nodeId, ctx.children[2]) + " )" elif ctx.children[1].getText().strip() in operators: return "( " + self.getVersionedTerminalRHS(nodeId, ctx).strip() + " )" else: return self.getConditionalString(nodeId, ctx.children[1]) elif ctx.getChildCount() == 5: # For "XX BETWEEN 10 AND 50" if self.getVersionedTerminalRHS(nodeId, ctx.children[1]).strip() == "BETWEEN": referenceVar = "( " + self.getVersionedTerminalRHS(nodeId, ctx.children[0]).strip() + " )" leftBoundary = "( " + self.getVersionedTerminalRHS(nodeId, ctx.children[2]).strip() + " )" rightBoundary = "( " + self.getVersionedTerminalRHS(nodeId, ctx.children[4]).strip() + " )" return "( ( " + referenceVar + " > " + leftBoundary + " ) ^ ( " + referenceVar + " < " + rightBoundary + " ) )" elif self.getVersionedTerminalRHS(nodeId, ctx.children[1]).strip() == "IN": # For "XX IN ( select_subquery )" leftVar = self.getVersionedTerminalRHS(nodeId, ctx.children[0]).strip() selectQueryCtx = ctx.children[3].children[0] rightVar = self.getVersionedTerminalRHS(nodeId, selectQueryCtx.children[1]).strip() isWhereCondition = False isNullInCondition = False condition = "" for m in range(selectQueryCtx.getChildCount()): if self.helper.getRuleName(selectQueryCtx.children[m]) == "where_clause": isWhereCondition = True if self.nullInCondition(selectQueryCtx.children[m].children[1]): isNullInCondition = True else: condition = self.getConditionalString(nodeId, selectQueryCtx.children[m].children[1]) if isWhereCondition: if isNullInCondition: return "( " + leftVar + " = " + rightVar + " )" else: return "( ( " + leftVar + " = " + rightVar + " ) ^ ( " + condition + " ) )" else: return "( " + leftVar + " = " + rightVar + " )" return "" elif ctx.getChildCount() == 6: # For "XX NOT BETWEEN 10 AND 50" if self.getVersionedTerminalRHS(nodeId, ctx.children[2]).strip() == "BETWEEN": referenceVar = "( " + self.getVersionedTerminalRHS(nodeId, ctx.children[0]).strip() + " )" leftBoundary = "( " + self.getVersionedTerminalRHS(nodeId, ctx.children[3]).strip() + " )" rightBoundary = "( " + self.getVersionedTerminalRHS(nodeId, ctx.children[5]).strip() + " )" return "( ( " + referenceVar + " < " + leftBoundary + " ) v ( " + referenceVar + " > " + rightBoundary + " ) )" elif self.getVersionedTerminalRHS(nodeId, ctx.children[2]).strip() == "IN": # For "XX NOT IN ( select_subquery )" leftVar = self.getVersionedTerminalRHS(nodeId, ctx.children[0]).strip() selectQueryCtx = ctx.children[4].children[0] rightVar = self.getVersionedTerminalRHS(nodeId, selectQueryCtx.children[1]).strip() isWhereCondition = False isNullInCondition = False condition = "" for m in range(selectQueryCtx.getChildCount()): if self.helper.getRuleName(selectQueryCtx.children[m]) == "where_clause": isWhereCondition = True if self.nullInCondition(selectQueryCtx.children[m].children[1]): isNullInCondition = True else: condition = self.getConditionalString(nodeId, selectQueryCtx.children[m].children[1]) if isWhereCondition: if isNullInCondition: return "( ! ( " + leftVar + " = " + rightVar + " ) )" else: return "( ( ! ( " + leftVar + " = " + rightVar + " ) ) ^ ( " + condition + " ) )" else: return "( ! ( " + leftVar + " = " + rightVar + " ) )" return "" elif ctx.getChildCount() == 0: # for stmts like "UPDATE --blah blah-- WHERE SingleWord;" return "( " + self.getVersionedTerminalRHS(nodeId, ctx).strip() + " )" def nullInCondition(self, conditionalCtx): condition = self.getTerminal(conditionalCtx).strip() tokens = condition.split(" ") # tokens will be a list if "NULL" in tokens: return True return False def getVariableForAggregateFunctionInSelect(self, varString): varString = varString.replace("(", "") varString = varString.replace(")", "") varString = varString.replace(",", "") varString = varString.strip() varString = varString.replace(" ", " ") varString = varString.replace(" ", "_") varString = varString.replace("*", "STAR") return varString
def __init__(self, cnfCfg, parser): self.cnfCfg = cnfCfg self.parser = parser self.helper = MyHelper(self.parser)
def main(argv): name = "gen/data/"+argv[1] file = open(name, "r") content = file.read().upper() file.close() file = open('upper_input.sql', "w") file.write(content) file.close() input = FileStream('upper_input.sql') lexer = PlSqlLexer(input) stream = CommonTokenStream(lexer) parser = PlSqlParser(stream) tree = parser.sql_script() #ast = tree.toStringTree(recog=parser) #print(ast) # print(str(MyPlSqlVisitor(parser).getRuleName(tree))) # print("\n\n", signature(tree.toStringTree), "\n") cfg = MyCFG() helper = MyHelper(parser) utility = MyUtility(helper) v = MyVisitor(parser, cfg, utility) v.visit(tree) print(v.rawCFG) for key in v.cfg.nodes: if v.cfg.nodes[key].ctx != None: print(key, " --> ", v.cfg.nodes[key].ctx.getText()) res = MyRawCfgToGraph(v.rawCFG, cfg) res.execute() cfg.printPretty() cfg.dotToPng(cfg.dotGraph, "raw_graph.dot") utility.generateDomSet(cfg) print("Dominator set ended----------->\n\n") utility.generateSDomSet(cfg) print("Strictly Dominator set ended ----------->\n\n") utility.generatIDom(cfg) print("Immediate Dominator ended ----------->\n\n") utility.generateDFSet(cfg) utility.insertPhiNode(cfg) utility.initialiseVersinosedPhiNode(cfg) utility.versioniseVariable(cfg) utility.phiDestruction(cfg) ssaString = MySsaStringGenerator(cfg, parser) ssaString.execute() utility.dfs(cfg.nodes[0].id, cfg) #start = cfg.nodes[0].id for nodeId in cfg.nodes: last_node = cfg.nodes[nodeId].id list_of_path = list(utility.dfs_path(cfg.nodes[0].id, last_node, cfg)) #print(list_of_path) #print(start) #print(last_node) vcs = SymbolicVcGeneration(cfg, parser) #vcs.SymbolicVcCalculation(cfg, start, last_node) list_of_vcs = [] for path in list_of_path: print("\n Path %d --->", path) vc=vcs.SymbolicVc(path) #list_of_vcs.append(vc) print(vc) #file = open("dbvcs.txt","w") #for ele in list_of_vcs: # file.write("%s\n" % ele) #print (ele+"\n") #file.close() z3fr = z3formulaofvcs(cfg, parser) for path in list_of_path: z3fr.z3VariableDeclarationSet(path) #print(list_of_path) #for path in list_of_path: #print(path) #print(cfg.nodes[0].id) # utility.generateFinalDotGraph(cfg) #for nodeId in cfg.nodes: #cfg.nodes[nodeId].printPretty() # #hello = utility.generateFinalDotGraph(cfg) #print(hello) #cfg.dotToPng(hello, "versioned_graph.dot") #hello2 = utility.generateVersionedDotFile(cfg) #print(hello2) #cfg.dotToPng(hello2, "versioned_graph.dot") #hello3 = utility.generateVersionedPhiNodeWalaDotFile(cfg) #print(hello3) #cfg.dotToPng(hello3, "versioned_phi_node_wala_graph.dot") hello4 = utility.generateDestructedPhiNodeWalaDotFile(cfg) #print(hello4) cfg.dotToPng(hello4, "destructed_phi_node_wala_graph.dot")
def executeSinglePlSqlFile(data, spec): f = open(data, 'r') linesOfCode = len(f.readlines()) f.close() processor = PreProcessor(spec, data) tableInfo, assumeConstraint, assertConstraint, resultString = processor.start( ) file = open('cnf/upper_input.sql', "w") file.write(resultString) file.close() # recording startTime startTime = datetime.datetime.now() input = FileStream('cnf/upper_input.sql') lexer = PlSqlLexer(input) stream = CommonTokenStream(lexer) parser = PlSqlParser(stream) tree = parser.sql_script() # ast = tree.toStringTree(recog=parser) # print(str(MyPlSqlVisitor(parser).getRuleName(tree))) # print("\n\n", signature(tree.toStringTree), "\n") cfg = MyCFG() helper = MyHelper(parser) helper.updateTableDict(tableInfo) utility = MyUtility(helper) v = MyVisitor(parser, cfg, utility) v.visit(tree) print("\nRaw CFG : ", v.rawCFG) # for key in v.cfg.nodes: # if v.cfg.nodes[key].ctx != None: # print(key, " --> ", v.cfg.nodes[key].ctx.getText()) res = MyRawCfgToGraph(v.rawCFG, cfg) res.execute() # cfg.printPretty() # cfg.dotToPng(cfg.dotGraph, "cnf/raw_graph") # TODO: make dot file in cnf form utility.generateDomSet(cfg) # print("Dominator set ended----------->\n\n") utility.generateSDomSet(cfg) # print("Strictly Dominator set ended ----------->\n\n") utility.generatIDom(cfg) # print("Immediate Dominator ended ----------->\n\n") utility.generateDFSet(cfg) utility.insertPhiNode(cfg) utility.initialiseVersinosedPhiNode(cfg) utility.versioniseVariable(cfg) utility.phiDestruction(cfg) ssaString = MySsaStringGenerator(cfg, parser) ssaString.execute() # utility.generateFinalDotGraph(cfg) # for nodeId in cfg.nodes: # cfg.nodes[nodeId].printPretty() # cfg.dotToPng(cfg.dotGraph, "cnf/raw_graph") # # hello1 = utility.generateBeforeVersioningDotFile(cfg) # cfg.dotToPng(hello1, "cnf/before_versioning_graph") # # hello4 = utility.generateDestructedPhiNodeWalaDotFile(cfg) # cfg.dotToPng(hello4, "cnf/destructed_phi_node_wala_graph") cnfUtility = CnfUtility(helper) iCnfCfg = cnfUtility.copyCfg(cfg) reverseCnfCfg = cnfUtility.topologicalSort(iCnfCfg) cnfUtility.unvisit(iCnfCfg) cnfUtility.setParentBranching(iCnfCfg) cnfCfg = cnfUtility.reverseDictOrder(reverseCnfCfg) cnfUtility.copyParentBranching(cnfCfg, iCnfCfg) # print("\n\n\n\n\n\t\t\tThe intermediate CNF form is ------------------------------>\n\n\n\n") # for nodeId in iCnfCfg.nodes: # iCnfCfg.nodes[nodeId].printPretty() # print("\n\n\n\n\n\t\t\tThe CNF form is ------------------------------>\n\n\n\n") cnfVcGenerator = CnfVcGenerator(cnfCfg, parser) cnfPath = [] for nodeId in cnfCfg.nodes: cnfPath.append(nodeId) cnfVcGenerator.generateCnfVc(cnfPath) # print("\n\n\n\n\t\t\tThe CNF VCs are : ------------------------------->\n\n\n") # print(cnfVcs) # for nodeId in cnfCfg.nodes: # cnfCfg.nodes[nodeId].printPretty() # cnfVc = cnfUtility.cnfVc(cnfCfg) # # print("\n\n\t\tThe CNF VCs are ----------------->\n\n\n") # # for str in cnfVc: # print(str) varSet, z3Str = cnfUtility.iZ3format(cnfCfg) # print("\n\n*******************\n\n", z3Str, "\n\n--------------\n\n") # print(varSet) # # print("\n\n") z3Str = z3Str.replace(" ", " ") z3Str = z3Str.replace(" == ", " = ") z3Str = z3Str.replace(" = ", " == ") print("\n**** Final CNF VC in Well_Bracketted_Format:\n\n", z3Str, "\n") z3StringConvertor = WpcStringConverter(z3Str) z3StringConvertor.execute() # print("\n**** Final CNF VC in Z3 Format:\n", z3StringConvertor.convertedWpc, "\n") z3FileString = "# This file was generated at runtime on " + str( datetime.datetime.now()) + "\n" z3FileString = z3FileString + "from z3 import *\n\n" z3FileString = z3FileString + "class Z3RuntimeCnfFile():\n" z3FileString = z3FileString + "\t" + "def __init__(self):\n" z3FileString = z3FileString + "\t\t" + "self.finalFormula = \"\"\n" z3FileString = z3FileString + "\t\t" + "self.satisfiability = \"\"\n" z3FileString = z3FileString + "\t\t" + "self.modelForViolation = \"\"\n\n" z3FileString = z3FileString + "\t" + "def execute(self):\n" for i in varSet: z3FileString = z3FileString + "\t\t" + i + " = Real(\'" + i + "\')\n" z3FileString = z3FileString + "\n\t\ts = Solver()\n" if len(z3StringConvertor.implies_p) > 0: for i in range(len(z3StringConvertor.implies_p)): z3FileString = z3FileString + "\t\t" + "s.add(" + z3StringConvertor.implies_p[ i] + ")\n" if not z3StringConvertor.convertedWpc == z3StringConvertor.implies_p_q[ i]: z3FileString = z3FileString + "\t\t" + "s.add(" + z3StringConvertor.implies_p_q[ i] + ")\n" # if z3StringConvertor.convertedWpc not in z3StringConvertor.implies_p_q: # z3FileString = z3FileString + "\t\t" + "s.add(" + z3StringConvertor.convertedWpc + ")\n" # else: # z3FileString = z3FileString + "\t\t" + "s.add(" + z3StringConvertor.convertedWpc + ")\n" z3FileString = z3FileString + "\t\t" + "s.add( Not( " + z3StringConvertor.convertedWpc + " ) )\n" # z3FileString = z3FileString + "\n\t\t" + "print()" # z3FileString = z3FileString + "\n\t\t" + "print(\"%%%%%%%%%% Aggregate Formula %%%%%%%%%%\\n\", s)" z3FileString = z3FileString + "\n\t\t" + "self.finalFormula = str(s)" # z3FileString = z3FileString + "\n\t\t" + "print()" # z3FileString = z3FileString + "\n\t\t" + "print(\"%%%%%%%%%% Satisfiability %%%%%%%%%%\")\n" z3FileString = z3FileString + "\n\t\t" + "self.satisfiability = str(s.check())" z3FileString = z3FileString + "\n\t\t" + "if self.satisfiability == \"sat\":" # z3FileString = z3FileString + "\n\t\t\t" + "print()" # z3FileString = z3FileString + "\n\t\t\t" + "print(\"-------->> Violation Occurred...\")" z3FileString = z3FileString + "\n\t\t\t" + "self.satisfiability = \"violation\"" # z3FileString = z3FileString + "\n\t\t\t" + "print()" # z3FileString = z3FileString + "\n\t\t\t" + "print(\"%%%%%%%%%% An Instance for which Violation Occurred %%%%%%%%%%\\n\", s.model())" z3FileString = z3FileString + "\n\t\t\t" + "self.modelForViolation = str(s.model())" z3FileString = z3FileString + "\n\t\t" + "elif self.satisfiability == \"unsat\":" # z3FileString = z3FileString + "\n\t\t\t" + "print()" # z3FileString = z3FileString + "\n\t\t\t" + "print(\"-------->> NO Violation Detected so far...\")" z3FileString = z3FileString + "\n\t\t\t" + "self.satisfiability = \"sat\"" # z3FileString = z3FileString + "\n\t\t\t" + "print()" # z3FileString = z3FileString + "\n\t\t" + "print()\n" file = open('cnf/Z3RuntimeCnfFile.py', "w") file.write(z3FileString) file.close() import cnf.Z3RuntimeCnfFile from cnf.Z3RuntimeCnfFile import Z3RuntimeCnfFile # Reload after module's creation to avoid old module remain imported from disk...VVI... cnf.Z3RuntimeCnfFile = reload(cnf.Z3RuntimeCnfFile) z3Runtime = Z3RuntimeCnfFile() z3Runtime.execute() finishTime = datetime.datetime.now() timeDifference = (finishTime - startTime).total_seconds() return linesOfCode, timeDifference, z3StringConvertor.convertedWpc, z3Runtime.satisfiability, z3Runtime.modelForViolation
def executeSinglePlSqlFile(data, spec): f = open(data, 'r') linesOfCode = len(f.readlines()) f.close() processor = PreProcessor(spec, data) tableInfo, assumeConstraintList, assertConstraintList, resultString = processor.start( ) file = open('wpc/upper_input.sql', "w") file.write(resultString) file.close() # recording startTime startTime1 = datetime.datetime.now() input = FileStream('wpc/upper_input.sql') lexer = PlSqlLexer(input) stream = CommonTokenStream(lexer) parser = PlSqlParser(stream) tree = parser.sql_script() cfg = MyCFG() helper = MyHelper(parser) helper.updateTableDict(tableInfo) utility = MyUtility(helper) v = MyVisitor(parser, cfg, utility) v.visit(tree) print("\nRaw CFG :", v.rawCFG, "\n") # for key in v.cfg.nodes: # if v.cfg.nodes[key].ctx != None: # print(key, " --> ", v.cfg.nodes[key].ctx.getText()) # print("\n") res = MyRawCfgToGraph(v.rawCFG, cfg) res.execute() # cfg.printPretty() # print("\n") utility.generateVariableSet(cfg) # all properties of each node # for nodeId in cfg.nodes: # cfg.nodes[nodeId].printPretty() ssaString = MySsaStringGenerator(cfg, parser) ssaString.execute( ) # only for generating DOT file for "before_versioning_graph" # recording finishTime finishTime1 = datetime.datetime.now() # cfg.dotToPng(cfg.dotGraph, "wpc/raw_graph") # # hello1 = utility.generateBeforeVersioningDotFile(cfg) # cfg.dotToPng(hello1, "wpc/before_versioning_graph") # recording startTime startTime2 = datetime.datetime.now() algo = WpcGenerator(cfg, helper, ssaString) algo.execute() algo.finalWpcString = algo.finalWpcString.replace(" ", " ") # done: replace " = " with " == " in algo.finalWpcString algo.finalWpcString = algo.finalWpcString.replace(" = ", " == ") print("\n**** Final WPC VC in Well_Bracketted_Format:\n\n", algo.finalWpcString, "\n") # print(algo.variablesForZ3) # algo.finalWpcString = "( ( z ) ^ ( ( ! ( y ) ) ==> ( ( ( 2 ) v ( x ) ) ==> ( y - 2 ) ) ) )" # for testing! Don't UNCOMMENT... # algo.finalWpcString = "( ( ( z ) ==> ( u ) ) ^ ( ( ! ( y ) ) ==> ( ( ( true ) ) ==> ( y - 2 ) ) ) )" # for testing! Don't UNCOMMENT... # algo.finalWpcString = "( ( ( z ) ==> ( u ) ) ^ ( ( ! ( y ) ) ==> ( true ) ) ^ ( ( a ) ==> ( b ) ) )" # for testing! Don't UNCOMMENT... # algo.finalWpcString = "( ( ( ! ( y ) ) ==> ( true ) ) )" # for testing! Don't UNCOMMENT... # algo.finalWpcString = "( ( ( ! ( y ) ) ^ ( true ) v ( g ) ) )" # for testing! Don't UNCOMMENT... z3StringConvertor = WpcStringConverter(algo.finalWpcString) z3StringConvertor.execute() # z3StringConvertor.convertedWpc is the FINAL VC Generated... print("\n**** Final WPC VC in Z3 Format:\n\n", z3StringConvertor.convertedWpc, "\n") z3FileString = "# This file was generated at runtime on " + str( datetime.datetime.now()) + "\n" z3FileString = z3FileString + "from z3 import *\n\n" z3FileString = z3FileString + "class Z3RuntimeWpcFile():\n" z3FileString = z3FileString + "\t" + "def __init__(self):\n" z3FileString = z3FileString + "\t\t" + "self.finalFormula = \"\"\n" z3FileString = z3FileString + "\t\t" + "self.satisfiability = \"\"\n" z3FileString = z3FileString + "\t\t" + "self.modelForViolation = \"\"\n\n" z3FileString = z3FileString + "\t" + "def execute(self):\n" for i in algo.variablesForZ3: z3FileString = z3FileString + "\t\t" + i + " = Real(\'" + i + "\')\n" z3FileString = z3FileString + "\n\t\ts = Solver()\n" if len(z3StringConvertor.implies_p) > 0: for i in range(len(z3StringConvertor.implies_p)): z3FileString = z3FileString + "\t\t" + "s.add(" + z3StringConvertor.implies_p[ i] + ")\n" if not z3StringConvertor.convertedWpc == z3StringConvertor.implies_p_q[ i]: z3FileString = z3FileString + "\t\t" + "s.add(" + z3StringConvertor.implies_p_q[ i] + ")\n" z3FileString = z3FileString + "\t\t" + "s.add( Not( " + z3StringConvertor.convertedWpc + " ) )\n" # z3FileString = z3FileString + "\n\t\t" + "print()" z3FileString = z3FileString + "\n\t\t" + "#print(\"\\n%%%%%%%%%% Aggregate Formula %%%%%%%%%%\\n\", s)" z3FileString = z3FileString + "\n\t\t" + "self.finalFormula = str(s)" # z3FileString = z3FileString + "\n\t\t" + "print()" z3FileString = z3FileString + "\n\t\t" + "#print(\"\\n%%%%%%%%%% Satisfiability %%%%%%%%%%\")\n" z3FileString = z3FileString + "\n\t\t" + "self.satisfiability = str(s.check())" z3FileString = z3FileString + "\n\t\t" + "if self.satisfiability == \"sat\":" # z3FileString = z3FileString + "\n\t\t\t" + "print()" z3FileString = z3FileString + "\n\t\t\t" + "#print(\"\\n-------->> Violation Occurred...\")" z3FileString = z3FileString + "\n\t\t\t" + "self.satisfiability = \"violation\"" # z3FileString = z3FileString + "\n\t\t\t" + "print()" z3FileString = z3FileString + "\n\t\t\t" + "#print(\"\\n%%%%%%%%%% An Instance for which Violation Occurred %%%%%%%%%%\\n\", s.model())" z3FileString = z3FileString + "\n\t\t\t" + "self.modelForViolation = str(s.model())" z3FileString = z3FileString + "\n\t\t" + "elif self.satisfiability == \"unsat\":" # z3FileString = z3FileString + "\n\t\t\t" + "print()" z3FileString = z3FileString + "\n\t\t\t" + "#print(\"\\n-------->> NO Violation Detected so far...\\n\")" z3FileString = z3FileString + "\n\t\t\t" + "self.satisfiability = \"sat\"" # z3FileString = z3FileString + "\n\t\t\t" + "print()" # z3FileString = z3FileString + "\n\t\t" + "print()\n" file = open('wpc/Z3RuntimeWpcFile.py', "w") file.write(z3FileString) file.close() # time.sleep(2) # import file created on Runtime... import wpc.Z3RuntimeWpcFile from wpc.Z3RuntimeWpcFile import Z3RuntimeWpcFile # Reload after module's creation to avoid old module remain imported from disk...VVI... wpc.Z3RuntimeWpcFile = reload(wpc.Z3RuntimeWpcFile) z3Runtime = Z3RuntimeWpcFile() z3Runtime.execute() # print(z3Runtime.finalFormula) # print(z3Runtime.satisfiability) # print(z3Runtime.modelForViolation) # recording finishTime finishTime2 = datetime.datetime.now() timeDifference = ((finishTime1 - startTime1) + (finishTime2 - startTime2)).total_seconds() return linesOfCode, timeDifference, z3StringConvertor.convertedWpc, z3Runtime.satisfiability, z3Runtime.modelForViolation
class MySsaStringGenerator(PlSqlVisitor): def __init__(self, cfg, parser): self.cfg = cfg self.parser = parser self.helper = MyHelper(self.parser) def execute(self): for nodeId in self.cfg.nodes: self.cfg.nodes[nodeId].oldString = self.getTerminal(self.cfg.nodes[nodeId].ctx) self.cfg.nodes[nodeId].stringSsa = self.getSsaString(nodeId) def getSsaString(self, nodeId): ctx = self.cfg.nodes[nodeId].ctx res = "" if ctx==None: pass else: ruleName = self.helper.getRuleName(ctx) if ruleName == "parameter": res = self.getParameter(nodeId, ctx) if ruleName == "variable_declaration": res = self.getVariable_declaration(nodeId, ctx) if ruleName == "cursor_declaration": res = self.getCursor_declaration(nodeId, ctx) if ruleName == "open_statement": res = self.getOpen_statement(nodeId, ctx) if ruleName == "fetch_statement": res = self.getFetch_statement(nodeId, ctx) if ruleName == "close_statement": res = self.getClose_statement(nodeId, ctx) if ruleName == "condition": res = self.getCondition(nodeId, ctx) if ruleName == "insert_statement": res = self.getInsert_statement(nodeId, ctx) if ruleName == "delete_statement": res = self.getDelete_statement(nodeId, ctx) if ruleName == "update_statement": res = self.getUpdate_statement(nodeId, ctx) if ruleName == "select_statement": res = self.getSelect_statement(nodeId, ctx) if ruleName == "assignment_statement": res = self.getAssignment_statement(nodeId, ctx) if ruleName == "function_call": res = self.getFunction_call(nodeId, ctx) if ruleName == "assert_statement": res = self.getCondition(nodeId, ctx) if ruleName == "assume_statement": res = self.getCondition(nodeId, ctx) return res def getParameter(self, nodeId, ctx): # varName = ctx.children[0].getText() # res = self.cfg.nodes[nodeId].versionedRHS[varName] + " " + ctx.children[1].getText() + " " + ctx.children[2].getText() res = self.getVersionedTerminalRHS(nodeId, ctx) return res def getVariable_declaration(self, nodeId, ctx): # variable_declaration : variable_name CONSTANT? type_spec (NOT NULL)? default_value_part? ';' # varName = ctx.children[0].getText() # res = self.cfg.nodes[nodeId].versionedRHS[varName] # for i in range(1, ctx.getChildCount()-1): # removed last ";" # res = res + " " + ctx.children[i].getText() res = self.getVersionedTerminalRHS(nodeId, ctx) return res def getCursor_declaration(self, nodeId, ctx): res = "" + str(ctx.children[0]) + " " res = res + self.getVersionedTerminalLHS(nodeId, ctx.children[1]) + str(ctx.children[2]) + " " res = res + self.getVersionedTerminalRHS(nodeId, ctx.children[3]) return res def getOpen_statement(self, nodeId, ctx): return self.getVersionedTerminalRHS(nodeId, ctx) def getFetch_statement(self, nodeId, ctx): res = "" + str(ctx.children[0]) + " " res = res + self.getVersionedTerminalRHS(nodeId, ctx.children[1]) + str(ctx.children[2]) + " " res = res + self.getVersionedTerminalLHS(nodeId, ctx.children[3]) return res def getClose_statement(self, nodeId, ctx): return self.getVersionedTerminalRHS(nodeId, ctx) def getCondition(self, nodeId, ctx): return self.getVersionedTerminalRHS(nodeId, ctx) def getInsert_statement(self, nodeId, ctx): res = "" + str(ctx.children[0]) + " " res = res + self.getVersionedTerminalLHS(nodeId, ctx.children[1].children[0]) res = res + self.getVersionedTerminalRHS(nodeId, ctx.children[1].children[1]) return res def getDelete_statement(self, nodeId, ctx): return self.getVersionedTerminalRHS(nodeId, ctx) def getUpdate_statement(self, nodeId, ctx): res = "" + str(ctx.children[0]) + " " + self.getTerminal(ctx.children[1]) res = res + self.getVersionedTerminalLHS(nodeId, ctx.children[2]) res = res + self.getVersionedTerminalRHS(nodeId, ctx.children[3]) # considering WHERE is mandatory for UPDATE statement (bug) return res def getSelect_statement(self, nodeId, ctx): res = "" for i in range(ctx.children[0].children[0].getChildCount()): ruleName = self.helper.getRuleName(ctx.children[0].children[0].children[i]) if ruleName == "into_clause": res = res + self.getVersionedTerminalLHS(nodeId, ctx.children[0].children[0].children[i]) else: res = res + self.getVersionedTerminalRHS(nodeId, ctx.children[0].children[0].children[i]) return res def getAssignment_statement(self, nodeId, ctx): res = "" res = res + self.getVersionedTerminalLHS(nodeId, ctx.children[0]) + str(ctx.children[1]) + " " res = res + self.getVersionedTerminalRHS(nodeId, ctx.children[2]) return res def getFunction_call(self, nodeId, ctx): return "FUNCTION-CALL" def getVersionedTerminalRHS(self, nodeId, ctx): c = ctx.getChildCount() if c==0: if str(ctx) in self.cfg.nodes[nodeId].versionedRHS.keys(): return self.cfg.nodes[nodeId].versionedRHS[str(ctx)] + " " else: return str(ctx) + " " else: res = "" for i in range(c): res = res + self.getVersionedTerminalRHS(nodeId, ctx.children[i]) return res def getVersionedTerminalLHS(self, nodeId, ctx): c = ctx.getChildCount() if c==0: if str(ctx) in self.cfg.nodes[nodeId].versionedLHS.keys(): return self.cfg.nodes[nodeId].versionedLHS[str(ctx)] + " " else: return str(ctx) + " " else: res = "" for i in range(c): res = res + self.getVersionedTerminalLHS(nodeId, ctx.children[i]) return res def getTerminal(self, ctx): if ctx==None: return "" c = ctx.getChildCount() if c==0: return str(ctx) + " " else: res = "" for i in range(c): res = res + self.getTerminal(ctx.children[i]) return res
class SymbolicVcGeneration(PlSqlVisitor): def __init__(self, cfg, parser): self.cfg = cfg self.parser = parser self.helper = MyHelper(self.parser) #input("enter a num") def SymbolicVc(self, path): global vcs vcs = "True" for i in range(len(path)): node = path[i] context = self.cfg.nodes[node].ctx if context is None: if self.cfg.nodes[node].destructedPhi: #print(context) for element in self.cfg.nodes[node].destructedPhi: values = self.cfg.nodes[node].destructedPhi[element] vcs = "And(" + vcs + ", " + values[ 0] + " == " + values[1] + ")" #versioned_Variable_Set.add(values[0]) #versioned_Variable_Set.add(values[1]) #print(self.cfg.nodes[node].destructedPhi[element]) #vcs = vcs + else: ruleName = self.helper.getRuleName(context) #print(ruleName) #print(str(context.toStringTree(recog=self.parser))) if ruleName == "condition": ctx = context.children[0] if self.cfg.nodes[node].branching['true'] == path[i + 1]: if ctx.children[2].getText() == "NULL": vcs = "And(" + vcs + " , " + "True" + ")" elif ctx.children[2].getText() == "NOTNULL": vcs = "And(" + vcs + " , " + "True" + ")" else: vcs = "And(" + vcs + ", " + self.getCondition( node, ctx) + ")" else: if ctx.children[2].getText() == "NULL": vcs = "And(" + vcs + " , " + "True" + ")" elif ctx.children[2].getText() == "NOTNULL": vcs = "And(" + vcs + " , " + "True" + ")" else: vcs = "And(" + vcs + " , " + "Not(" + self.getCondition( node, ctx) + ")" + ")" if ruleName == "assignment_statement": vcs = "And(" + vcs + ", " + self.getAssignment_statement( node, context) + ")" if ruleName == "update_statement": vcs = "And(" + vcs + ", " + self.getUpdate_statement( node, context) + ")" if ruleName == "insert_statement": vcs = self.getInsert_statement(node, context) if ruleName == "cursor_declaration": vcs = "And(" + vcs + ", " + self.getCursor_declaration( node, context) + ")" if ruleName == "fetch_statement": vcs = "And(" + vcs + ", " + self.getFetch_statement( node, context) + ")" if ruleName == "select_statement": vcs = self.getSelect_statement(node, context) if ruleName == "delete_statement": vcs = "And(" + vcs + " , " + self.getDelete_statement( node, context) + ")" if ruleName == "raise_statement": print("Exception Raised") break if ruleName == "exception_handler": temp_ctx = context.children[3] child_count = temp_ctx.getChildCount() for child in range(child_count): if temp_ctx.children[child].getText() == ";": continue else: child_ctx = temp_ctx.children[child] if self.helper.getRuleName(child_ctx.children[0] ) == "assert_statement": #node = path[len(path)-1] # input("wait") vcs = "Implies(" + vcs + ", " + self.getAssert_statement( node, child_ctx.children[0]) + ")" else: continue if ruleName == "assume_statement": vcs = "And(" + vcs + ", " + self.getAssume_statement( node, context) + ")" if ruleName == "assert_statement": vcs = "Implies(" + vcs + ", " + self.getAssert_statement( node, context) + ")" else: pass #print(vcs) return vcs def getAssume_statement(self, nodeId, ctx): #global vcs res = self.getWhereexpr(nodeId, ctx.children[1]) return res def getAssert_statement(self, nodeId, ctx): res = self.assertExpression(nodeId, ctx.children[1]) return res def assertExpression(self, nodeId, ctx): if ctx.children[1].getText() == "AND": #print(self.helper.getRuleName(ctx.children[0])) res = "And(" + self.assertExpression(nodeId, ctx.children[0])+ ", " + \ self.assertExpression(nodeId, ctx.children[2])+ ")" else: #print(self.helper.getRuleName(ctx.children[1])) #print(self.getTerminal(ctx.children[1])) #print(ctx.children[1].getText()) if ctx.children[1].getText() == "=": #input("wait") res = self.getVersionedTerminalRHS(nodeId, ctx.children[0]) + "==" + \ self.getVersionedTerminalRHS(nodeId, ctx.children[2]) else: res = self.getVersionedTerminalRHS(nodeId, ctx.children[0]) + self.getTerminal(ctx.children[1]) + \ self.getVersionedTerminalRHS(nodeId, ctx.children[2]) return res def getSelect_statement(self, nodeId, ctx): global vcs #print(ctx.children[0].children[0].getChildCount()) #print(self.helper.getRuleName(ctx.children[0])) #input("Hi") vcs = "And(" + vcs + ", " + self.getInto_clause(nodeId, ctx.children[0].children[0].children[2]) + "==" + \ self.getVersionedTerminalRHS(nodeId, ctx.children[0].children[0].children[1]) + ")" #versioned_Variable_Set.add(self.getVersionedTerminalRHS(nodeId, ctx.children[0].children[0].children[1])) vcs = "And(" + vcs + ", " + self.getWhereClause( nodeId, ctx.children[0].children[0].children[4]) + ")" return vcs #input("Wait") def getDelete_statement(self, node, ctx): c = ctx.getChildCount() res = "Not(" + self.getWhereClause(node, ctx.children[c - 1]) + ")" for key in self.cfg.nodes[node].versionedLHS: res = "And(" + res + "," + self.cfg.nodes[node].versionedLHS[ key] + "==" + self.cfg.nodes[node].versionedRHS[key] + ")" return res def getInto_clause(self, nodeId, ctx): return self.getVersionedTerminalLHS(nodeId, ctx.children[1]) def getFetch_statement(self, nodeId, ctx): #global vcs res = self.getVersionedTerminalLHS(nodeId, ctx.children[3])+ " == " \ + self.getVersionedTerminalRHS(nodeId, ctx.children[1]) return res def getCursor_declaration(self, nodeId, ctx): global vcs if ctx.children[3].children[0].children[0].getChildCount() == 4: res = "And(" + self.getVersionedTerminalLHS(nodeId, ctx.children[1]) + " == " \ + self.getVersionedTerminalRHS(nodeId, ctx.children[3].children[0].children[0].children[1])+ ", " + \ self.getWhereClause(nodeId, ctx.children[3].children[0].children[0].children[3]) + ")" else: res = self.getVersionedTerminalLHS(nodeId, ctx.children[1]) + " == " \ + self.getVersionedTerminalRHS(nodeId, ctx.children[3].children[0].children[0].children[1]) return res def getCondition(self, nodeId, ctx): #print(ctx.children[0].getChildCount()) #ctx = ctx.children[0] #print(ctx.children[1].getText()) if ctx.children[1].getText() == "AND": res = "And(" + self.getCondition(nodeId, ctx.children[0])+ ", " + \ self.getCondition(nodeId, ctx.children[2])+ ")" elif ctx.children[1].getText() == "OR": #input("wait") res = "Or(" + self.getCondition(nodeId, ctx.children[0])+ ", " + \ self.getCondition(nodeId, ctx.children[2])+ ")" elif ctx.children[1].getText() == "BETWEEN": res = "And("+ self.getVersionedTerminalRHS(nodeId, ctx.children[0])+ ">" +self.getVersionedTerminalRHS(nodeId, ctx.children[2])+\ ","+ self.getVersionedTerminalRHS(nodeId, ctx.children[0])+ "<" +self.getVersionedTerminalRHS(nodeId, ctx.children[4])+")" else: #print(self.helper.getRuleName(ctx.children[1])) #print(self.getTerminal(ctx.children[1])) #print(ctx.children[1].getText()) if ctx.children[1].getText() == "=": #input("wait") res = self.getVersionedTerminalRHS(nodeId, ctx.children[0]) + "==" + \ self.getVersionedTerminalRHS(nodeId, ctx.children[2]) else: res = self.getVersionedTerminalRHS(nodeId, ctx.children[0]) + self.getTerminal(ctx.children[1]) + \ self.getVersionedTerminalRHS(nodeId, ctx.children[2]) return res #for i in range(ctx.getChildCount()): #print(self.helper.getRuleName(ctx.children[i])) #print(ctx.children[i].getText()) #input("wait") #print(self.helper.getRuleName(ctx)) #input("wait") #if ctx.children[0].getChildCount() == 1: #res = self.getVersionedTerminalRHS(nodeId, ctx) #else: #if ctx.children[0].children[1].getText()== "OR": #res = "Or("+self.getVersionedTerminalRHS(nodeId, ctx.children[0].children[0])+" , " +\ #self.getVersionedTerminalRHS(nodeId, ctx.children[0].children[2])+")" #elif ctx.children[0].children[1].getText()== "AND": #res = "And("+self.getVersionedTerminalRHS(nodeId, ctx.children[0].children[0])+" , " +\ #self.getVersionedTerminalRHS(nodeId, ctx.children[0].children[2])+")" #elif ctx.children[0].children[1].getText() == "=": #res = self.getVersionedTerminalRHS(nodeId, ctx.children[0].children[0]) + "==" + \ #self.getVersionedTerminalRHS(nodeId, ctx.children[0].children[2]) #else: #res = self.getVersionedTerminalRHS(nodeId, ctx) #return res def getAssignment_statement(self, node, ctx): #global vcs res = self.getVersionedTerminalLHS(node, ctx.children[0]) +' == '+ \ self.getVersionedTerminalRHS(node, ctx.children[2]) if self.cfg.nodes[node].destructedPhi: for element in self.cfg.nodes[node].destructedPhi: values = self.cfg.nodes[node].destructedPhi[element] res = "And(" + res + ", " + values[0] + "==" + values[1] + ")" #versioned_Variable_Set.add(values[0]) #versioned_Variable_Set.add(values[1]) return res #print(vcs) def getUpdate_statement(self, nodeId, ctx): #global vcs res = "Or(" + "And(" + self.getSetClause(nodeId, ctx.children[2]) + "," \ + self.getWhereClause(nodeId, ctx.children[3])+")" + ", " + \ "And(" + "Not(" + self.getWhereClause(nodeId, ctx.children[3]) +")"+ ", "+\ self.getNotSetClause(nodeId, ctx.children[2]) +"))" if self.cfg.nodes[nodeId].destructedPhi: for element in self.cfg.nodes[nodeId].destructedPhi: values = self.cfg.nodes[nodeId].destructedPhi[element] res = "And(" + res + ", " + values[0] + "==" + values[1] + ")" #versioned_Variable_Set.add(values[0]) #versioned_Variable_Set.add(values[1]) return res def getSetClause(self, NodeId, ctx): child = ctx.getChildCount() for i in range(child): if self.helper.getRuleName( ctx.children[i]) == "column_based_update_set_clause": print("") #print(child) #input("wait") res = self.getColsetClause(NodeId, ctx.children[1]) return res def getColsetClause(self, nodeId, ctx): res = self.getVersionedTerminalLHS(nodeId, ctx.children[0]) +' == '+ \ self.getVersionedTerminalRHS(nodeId, ctx.children[2]) return res def getNotSetClause(self, nodeId, ctx): res = self.getNotColsetClause(nodeId, ctx.children[1]) return res def getNotColsetClause(self, nodeId, ctx): res = self.getVersionedTerminalLHS(nodeId, ctx.children[0]) +' == '+ \ self.getVersionedTerminalRHS(nodeId, ctx.children[0]) return res def getWhereClause(self, nodeId, ctx): res = self.getWhereexpr(nodeId, ctx.children[1]) return res def getWhereexpr(self, nodeId, ctx): #global vcs #opr = ctx.children[1].getText() #oper = self.getTerminal(ctx.children[1]) #print(self.helper.getRuleName(ctx.children[1])) #print(ctx.children[1].getText()) #temp = 'AND' #print(opr == temp) #input("hi") #print(type("AND")) #print(self.helper.getRuleName(ctx.children[1])) #print(self.getTerminal(ctx.children[1])) #input("Wait") ctx.children[1].getText() if ctx.children[1].getText() == "AND": #print(self.helper.getRuleName(ctx.children[0])) res = "And(" + self.getWhereexpr(nodeId, ctx.children[0])+ ", " + \ self.getWhereexpr(nodeId, ctx.children[2])+ ")" #input("wait") elif ctx.children[1].getText() == "IN": res = "And(" + self.getVersionedTerminalRHS(nodeId, ctx.children[0]) + "==" +\ self.getVersionedTerminalRHS(nodeId, ctx.children[3].children[0].children[1]) +\ "," + self.getWhereexpr(nodeId, ctx.children[3].children[0].children[3].children[1])+")" #print(res) #input("Wait") #input("HI") else: #print(self.helper.getRuleName(ctx.children[1])) #print(self.getTerminal(ctx.children[1])) #print(ctx.children[1].getText()) if ctx.children[1].getText() == "=": #input("wait") res = self.getVersionedTerminalRHS(nodeId, ctx.children[0]) + "==" + \ self.getVersionedTerminalRHS(nodeId, ctx.children[2]) else: res = self.getVersionedTerminalRHS(nodeId, ctx.children[0]) + self.getTerminal(ctx.children[1]) + \ self.getVersionedTerminalRHS(nodeId, ctx.children[2]) return res #c = ctx.getChildCount() #print(self.helper.getRuleName(ctx.children[1])) #print(self.getTerminal(ctx.children[1])) #input("Enter the value") #res = "" #res = res + self.getVersionedTerminalRHS(nodeId, ctx) #return res ''' for i in range(c): if ctx.children[i].getChildCount() > 1: res = res + self.getWhereexpr(nodeId, ctx.children[i]) if ctx.children[0].getChildCount() == 3: res = "" res = res + "&&" + self.getWhereexpr(nodeId, ctx.children[0]) else: res = self.getVersionedTerminalRHS(nodeId, ctx) ''' #print(self.helper.getRuleName(ctx.children[0].children[0])) #print(ctx.children[0].children[2].getChildCount()) #input('enter the vlue') #if self.helper.getRuleName(ctx) == "RelExpr": #return res #res = self.getVersionedTerminalRHS(nodeId, ctx.children[0]) +' == '+ \ #self.getVersionedTerminalRHS(nodeId, ctx.children[2]) def getInsert_statement(self, nodeId, ctx): global vcs vcs = str(self.getInsertIntoandValueClause(nodeId, ctx.children[1])) #res = self.getInsertIntoandValueClause(nodeId, ctx.children[1]) if self.cfg.nodes[nodeId].destructedPhi: for element in self.cfg.nodes[nodeId].destructedPhi: values = self.cfg.nodes[nodeId].destructedPhi[element] vcs = "And(" + vcs + ", " + values[0] + "==" + values[1] + ")" return vcs def getInsertIntoandValueClause(self, nodeId, ctx): global vcs colmnlistinto = [] colmnlistvalues = [] c1 = ctx.children[0].getChildCount() for item in range(c1): if self.helper.getRuleName( ctx.children[0].children[item]) == "column_name": #pass colmnlistinto.append( self.getVersionedTerminalLHS( nodeId, ctx.children[0].children[item])) else: pass c2 = ctx.children[1].children[1].getChildCount() for i in range(c2): if self.helper.getRuleName( ctx.children[1].children[1].children[i]) == "expression": colmnlistvalues.append( self.getVersionedTerminalRHS( nodeId, ctx.children[1].children[1].children[i])) else: pass for element in range(len(colmnlistinto)): vcs = "And(" + vcs + ", " + colmnlistinto[ element] + " == " + colmnlistvalues[element] + ")" return vcs #c= str(c1) + " " + str(c2) #return colmnlistvalues #return self.helper.getRuleName(ctx) #def getInsertValueClause(self, nodeId, ctx): # pass def getVersionedTerminalRHS(self, nodeId, ctx): c = ctx.getChildCount() if c == 0: if str(ctx) in self.cfg.nodes[nodeId].versionedRHS.keys(): return self.cfg.nodes[nodeId].versionedRHS[str(ctx)] + " " else: return str(ctx) + " " else: res = "" for i in range(c): res = res + self.getVersionedTerminalRHS( nodeId, ctx.children[i]) return res def getVersionedTerminalLHS(self, nodeId, ctx): c = ctx.getChildCount() if c == 0: if str(ctx) in self.cfg.nodes[nodeId].versionedLHS.keys(): return self.cfg.nodes[nodeId].versionedLHS[str(ctx)] + " " else: return str(ctx) + " " else: res = "" for i in range(c): res = res + self.getVersionedTerminalLHS( nodeId, ctx.children[i]) return res def getTerminal(self, ctx): if ctx == None: return "" c = ctx.getChildCount() if c == 0: return str(ctx) + " " else: res = "" for i in range(c): res = res + self.getTerminal(ctx.children[i]) return res
def start(self): tableInfo, predicates = self.getSpec() magicString = "CREATE OR REPLACE PROCEDURE TEST(X IN VARCHAR)\nIS\nBEGIN\n" for predicate in predicates: magicString = magicString + "\tASSUME " + predicate + ";\n" magicString = magicString + "\nEND;" file = open('mc/magic_file.sql', "w") file.write(magicString) file.close() input = FileStream('mc/magic_file.sql') lexer = PlSqlLexer(input) stream = CommonTokenStream(lexer) parser = PlSqlParser(stream) tree = parser.sql_script() cfg = MyCFG() helper = MyHelper(parser) helper.updateTableDict(tableInfo) utility = MyUtility(helper) v = MyVisitor(parser, cfg, utility) v.visit(tree) res = MyRawCfgToGraph(v.rawCFG, cfg) res.execute() utility.generateVariableSet(cfg) ssaStringObj = MySsaStringGenerator(cfg, parser) wpcGeneratorObj = WpcGenerator(cfg, helper, ssaStringObj) newPredicates = [] predicateVarSet = set() for nodeId in cfg.nodes: if helper.getRuleName(cfg.nodes[nodeId].ctx) == "assume_statement": assumeCondition = wpcGeneratorObj.getConditionalString( cfg.nodes[nodeId].ctx.children[1]) assumeCondition = assumeCondition.replace(" ", " ").strip() newPredicates.append(assumeCondition) predicateVarSet = predicateVarSet.union( cfg.nodes[nodeId].variableSet) f = open(self.fileData, "r") content = f.read().upper() f.close() assumeConstraintString = "TO_CORRECT_PROBLEMS_IN_SE_API > 0" assertConstraintString = "TO_CORRECT_PROBLEMS_IN_SE_API > 0" temp = content.strip().split("BEGIN") result = "" result = result + temp[ 0] + "BEGIN\n\t" + "ASSUME " + assumeConstraintString + " ;\n" + temp[ 1] for i in range(2, len(temp)): result = result + "BEGIN" + temp[i] temp = result.strip().split("END") result = temp[0] for i in range(1, len(temp) - 1): result = result + "END" + temp[i] result = result + "ASSERT " + assertConstraintString + " ;\n\t" + "END" + temp[ len(temp) - 1] return tableInfo, newPredicates, predicates, predicateVarSet, result # newPredicates : in wpc format ; predicate : in plsql format
class SymbolicVcGeneration(PlSqlVisitor): def __init__(self, cfg, parser): self.cfg = cfg self.parser = parser self.helper = MyHelper(self.parser) #input("enter a num") def SymbolicVc(self, path): global vcs vcs = "True" for i in range(len(path)): node = path[i] context = self.cfg.nodes[node].ctx if context is None: if self.cfg.nodes[node].destructedPhi: #print(context) for element in self.cfg.nodes[node].destructedPhi: values = self.cfg.nodes[node].destructedPhi[element] vcs = "And(" + vcs + ", " +values[0] + " == " + values[1] + ")" #print(self.cfg.nodes[node].destructedPhi[element]) #vcs = vcs + else: ruleName = self.helper.getRuleName(context) #print(ruleName) #print(str(context.toStringTree(recog=self.parser))) if ruleName == "condition": tempctx = context.children[0] if self.cfg.nodes[node].branching['true'] == path[i + 1]: if childctx.children[2].getText() == "NULL": vcs = "And(" + vcs + ", " +"True" + ")" elif childctx.children[2].getText() == "NOTNULL": vcs = "And(" + vcs + ", " + "True" + ")" else: vcs = "And(" + vcs + ", " + self.getCondition(node, childctx) + ")" else: if childctx.children[2].getText() == "NULL": vcs = "And(" + vcs + ", "+ "True" + ")" elif childctx.children[2].getText() == "NOTNULL": vcs = "And(" + vcs + ", " + "True" + ")" else: vcs = "And(" + vcs + ", " + "Not("+self.getCondition(node, childctx) + "))" if ruleName == "assignment_statement": vcs = "And(" + vcs + ", " + self.getAssignment_statement(node, context) + ")" if ruleName == "update_statement": vcs = "And(" + vcs + ", " + self.getUpdate_statement(node, context) + ")" if ruleName == "insert_statement": vcs = self.getInsert_statement(node, context) if ruleName == "cursor_declaration": vcs = "And(" + vcs + ", " + self.getCursor_declaration(node, context) +")" if ruleName == "fetch_statement": vcs = "And(" + vcs + ", " + self.getFetch_statement(node, context)+ ")" if ruleName == "select_statement": vcs = self.getSelect_statement(node, context) if ruleName == "delete_statement": vcs = "And("+vcs+" , "+self.getDelete_statement(node, context)+")" if ruleName == "raise_statement": print("Exception Raised") break if ruleName == "exception_handler": temp_ctx = context.children[3] child_count =temp_ctx.getChildCount() for child in range(child_count): if temp_ctx.children[child].getText() == ";": continue else: child_ctx = temp_ctx.children[child] if self.helper.getRuleName(child_ctx.children[0]) == "assert_statement": #input("wait") vcs = "Implies(" + vcs + ", " + self.getAssert_statement(node, child_ctx.children[0]) + ")" else: continue if ruleName == "assume_statement": vcs = "And(" + vcs + ", "+ self.getAssume_statement(node, context) +")" if ruleName == "assert_statement": vcs = "Implies(" + vcs + ", " + self.getAssert_statement(node, context)+")" else: pass #print(vcs) return vcs def getAssume_statement(self, nodeId, ctx): #global vcs res = self.getWhereexpr(nodeId, ctx.children[1]) return res def getAssert_statement(self, nodeId, ctx): res = self.assertExpression(nodeId, ctx.children[1]) return res def assertExpression(self, nodeId, ctx): if ctx.children[1].getText() == "AND": #print(self.helper.getRuleName(ctx.children[0])) res = "Or(" + self.assertExpression(nodeId, ctx.children[0])+ ", " + \ self.assertExpression(nodeId, ctx.children[2])+ ")" else: if ctx.children[1].getText() == "=": #input("wait") res = self.getVersionedTerminalRHS(nodeId, ctx.children[0]) + "==" + \ self.getVersionedTerminalRHS(nodeId, ctx.children[2]) else: res = self.getVersionedTerminalRHS(nodeId, ctx.children[0]) + self.getTerminal(ctx.children[1]) + \ self.getVersionedTerminalRHS(nodeId, ctx.children[2]) return res
def main(argv): datafile = "se/data/" + argv[1] specfile = "se/spec/" + argv[2] # file = open(datafile, "r") # content = file.read().upper() # file.close() processor = PreProcessor(specfile, datafile) tableInfo, assumeConstraintList, assertConstraintList, resultString = processor.start( ) file = open('se/upper_input.sql', "w") file.write(resultString) file.close() input = FileStream('se/upper_input.sql') lexer = PlSqlLexer(input) stream = CommonTokenStream(lexer) parser = PlSqlParser(stream) tree = parser.sql_script() # ast = tree.toStringTree(recog=parser) # print(str(MyPlSqlVisitor(parser).getRuleName(tree))) # print("\n\n", signature(tree.toStringTree), "\n") cfg = MyCFG() helper = MyHelper(parser) helper.updateTableDict(tableInfo) utility = MyUtility(helper) v = MyVisitor(parser, cfg, utility) v.visit(tree) print("\n\t", v.rawCFG, "\n") # for key in v.cfg.nodes: # if v.cfg.nodes[key].ctx != None: # print(key, " --> ", v.cfg.nodes[key].ctx.getText()) res = MyRawCfgToGraph(v.rawCFG, cfg) res.execute() # cfg.printPretty() cfg.dotToPng(cfg.dotGraph, "se/raw_graph") utility.generateDomSet(cfg) # print("Dominator set ended----------->\n\n") utility.generateSDomSet(cfg) # print("Strictly Dominator set ended ----------->\n\n") utility.generatIDom(cfg) # print("Immediate Dominator ended ----------->\n\n") utility.generateDFSet(cfg) utility.insertPhiNode(cfg) utility.initialiseVersinosedPhiNode(cfg) utility.versioniseVariable(cfg) utility.phiDestruction(cfg) ssaString = MySsaStringGenerator(cfg, parser) ssaString.execute() for nodeId in cfg.nodes: cfg.nodes[nodeId].printPretty() hello1 = utility.generateBeforeVersioningDotFile(cfg) # print(hello1) cfg.dotToPng(hello1, "se/before_versioning_graph") hello2 = utility.generateVersionedDotFile(cfg) #print(hello2) cfg.dotToPng(hello2, "se/versioned_graph") hello3 = utility.generateVersionedPhiNodeWalaDotFile(cfg) #print(hello3) cfg.dotToPng(hello3, "se/versioned_phi_node_wala_graph") hello4 = utility.generateDestructedPhiNodeWalaDotFile(cfg) #print(hello4) cfg.dotToPng(hello4, "se/destructed_phi_node_wala_graph")