def run_bv_sygus_algo(_BV_Def_file, _size, _count, trace_file, benign_traces, rejected_traces, unary_ops = ['!', 'Y', 'O', 'H'], binary_ops = ['S','&', '|', '=>'], max_trace_length = 0, AP_Lit = None): start_time = timeit.default_timer() nvars = benign_traces[0].vars if AP_Lit is None: AP_Lit = [] for n in range(nvars): key = 'p' + str(n) AP_Lit.append(key) _syngus = BVEncoder(nvars, benign_traces, rejected_traces, max_trace_length, AP_Lit) _bv_def = _BV_Def_file.read() _bv_def = _syngus.bv_sygus_latest_def(_bv_def) result = run_sygus(_bv_def, start_time, trace_file, _count) stop = timeit.default_timer() elasped_time = ('%.2f')%(stop - start_time) logging.info('Total Running Time Time: %s seconds'%(elasped_time)) logging.debug('Checking Synthesize pLTL Formulae...') eval_result(result, benign_traces, rejected_traces, _count) logging.debug('Synthesized Signatures: %s'%(result)) return result
def run_adt_fin_algo(_ADT_Def_file, _size, _count, trace_file, benign_traces, rejected_traces, unary_ops=['!', 'Y', 'O', 'H'], binary_ops=['S', '&', '|', '=>'], AP_Lit=None): start_time = timeit.default_timer() traces = benign_traces + rejected_traces nvars = max_trace_size(benign_traces, rejected_traces) _adtencoder = SMTEncoder(nvars, traces, len(benign_traces), len(traces)) _adt_def = _ADT_Def_file.read() _adt_def = _adtencoder.smt_adt_def(_adt_def) if AP_Lit: key_vars = [] for n in range(nvars): key = 'p' + str(n) key_vars.append(key) vars_dict = dict(zip(key_vars, list(AP_Lit))) tmp_result = run_smt2(_adt_def, start_time, trace_file, _count) cnt = 0 result = [] for f in tmp_result: cnt += 1 if f: if AP_Lit: f = parser.rename_var(f, vars_dict) result.append(f) print('(%d) %s ' % (cnt, f)) line = fp.readline() logging.debug('Checking Synthesize pLTL Formulae...') eval_result(result, benign_traces, rejected_traces, _count) logging.debug('Synthesized Signatures: %s' % (result)) return result
def run_guided_sat_enum_algo(_length, _count, benign_traces, rejected_traces, unary_ops=['!', 'Y', 'O', 'H'], binary_ops=['S', '&', '|', '=>'], AP_Lit=None, solver_type='z3'): start_time = timeit.default_timer() _results = [] sample_trace = benign_traces[0] size_counter = sample_trace.vars p_count = _count while len(_results) <= p_count: fixed_length_result = run_guided_sat_algo(size_counter, _count, benign_traces, rejected_traces, unary_ops, binary_ops, AP_Lit, solver_type) fm_counter = len(fixed_length_result) - 1 while fm_counter >= 0: fml = fixed_length_result[fm_counter] if not fml in _results: _results.append(fml) fm_counter -= 1 if (len(_results) >= p_count): break if (len(fixed_length_result) >= _count): break size_counter += 1 _count -= len(fixed_length_result) stop = timeit.default_timer() elasped_time = ('%.2f') % (stop - start_time) logging.info('Total Running Time: %s seconds' % (elasped_time)) logging.debug('Checking Synthesize pLTL Formulae...') eval_result(_results, benign_traces, rejected_traces, _count) return _results
def run_adt_sygus_algo(_ADT_Def_file, _size, _count, trace_file, benign_traces, rejected_traces, unary_ops=['!', 'Y', 'O', 'H'], binary_ops=['S', '&', '|', '=>'], AP_Lit=None): start_time = timeit.default_timer() traces = benign_traces + rejected_traces nvars = benign_traces[0].vars _syngus = ADTEncoder(nvars, traces, len(benign_traces), len(traces)) _adt_def = _ADT_Def_file.read() _adt_def = _syngus.sygus_adt_def(_adt_def) if AP_Lit: key_vars = [] for n in range(nvars): key = 'p' + str(n) key_vars.append(key) vars_dict = dict(zip(key_vars, list(AP_Lit))) tmp_result = run_sygus(_adt_def, start_time, trace_file, _count, False) parser = pLTLParser() result = [] for f in tmp_result: if AP_Lit: f = parser.dict_var(f, vars_dict) result.append(f) logging.debug('Checking Synthesize pLTL Formulae...') eval_result(result, benign_traces, rejected_traces, _count) logging.debug('Synthesized Signatures: %s' % (result)) return result
def run_guided_sat_algo(_length, _count, benign_traces, rejected_traces, unary_ops=['!', 'Y', 'O', 'H'], binary_ops=['S', '&', '|', '=>'], AP_Lit=None, solver_name="z3"): start_time = timeit.default_timer() sample_trace = benign_traces[0] if _length < sample_trace.vars: logging.warn('In correct size, please correct the size > |AP|') exit() sat_en = GuidedSATEncoder(_length, sample_trace.vars, unary_ops, binary_ops, AP_Lit) root = _length logging.info('Generate Graph Topology...') partial_DAGs = topology_graph(root, sample_trace.vars + 1) logging.info('Encoding Shape Constraints...') shape_fml = sat_en.encode_shape() sat_solver = SATSolver(solver_name) sat_solver.assert_cls(shape_fml) logging.info('Encoding Benign Traces Constraints...') for trace in benign_traces: _enc_fml = sat_en.encode_trace(trace, True) sat_solver.assert_cls(_enc_fml) logging.info('Encoding Rejected Traces Constraints...') for trace in rejected_traces: _enc_fml = sat_en.encode_trace(trace, False) sat_solver.assert_cls(_enc_fml) ap_cls = sat_en.get_AP_cls() sat_solver.assert_cls(ap_cls) _result = [] for p_dag in partial_DAGs: d = sat_en.get_partial_dag_cls(p_dag) _resultf = assert_pdg(sat_solver, d, sat_en, _length) if len(_resultf) > 0: _result.extend(_resultf) stop = timeit.default_timer() elasped_time = ('%.2f') % (stop - start_time) logging.info('Time: %s seconds' % (elasped_time)) logging.debug('Checking Synthesize pLTL Formulae...') eval_result(_result, benign_traces, rejected_traces, _count) # logging.info('Synthesized Signatures: %s'%(_result)) for p_dag in partial_DAGs: d = sat_en.get_partial_dag_cls_reverse(p_dag) _resultf = assert_pdg(sat_solver, d, sat_en, _length) if len(_resultf) > 0: _result.extend(_resultf) stop = timeit.default_timer() elasped_time = ('%.2f') % (stop - start_time) logging.info('Time: %s seconds' % (elasped_time)) logging.debug('Checking Synthesize pLTL Formulae...') eval_result(_result, benign_traces, rejected_traces, _count) # logging.info('Synthesized Signatures: %s'%(_result)) return _result
def run_sat_algo(_length, _count, benign_traces, rejected_traces, unary_ops=['!', 'Y', 'O', 'H', 'G'], binary_ops=['S', '&', '|', '=>'], AP_Lit=None, solver_name="z3"): start_time = timeit.default_timer() sample_trace = benign_traces[0] sat_en = SATEncoder(_length, sample_trace.vars, unary_ops, binary_ops, AP_Lit) #Encoding shape of the Formula logging.info('Encoding Shape Constraints...') shape_fml = sat_en.encode_shape() sat_solver = SATSolver(solver_name) sat_solver.assert_cls(shape_fml) logging.info('Encoding Benign Traces Constraints...') for trace in benign_traces: _enc_fml = sat_en.encode_trace(trace, True) sat_solver.assert_cls(_enc_fml) logging.info('Encoding Rejected Traces Constraints...') for trace in rejected_traces: _enc_fml = sat_en.encode_trace(trace, False) sat_solver.assert_cls(_enc_fml) logging.debug('Constraint solver is running...') model = sat_solver.get_model() _result = [] while model is not None: pLTL_fml = sat_en.m2f(_length - 1, model) if not pLTL_fml in _result: _result.append(pLTL_fml) pmodel = sat_en.dag_shape(model) model = sat_solver.enum_model(pmodel) logging.info('Extracted formula %s' % (pLTL_fml)) stop = timeit.default_timer() elasped_time = ('%.2f') % (stop - start_time) logging.info('Time: %s seconds' % (elasped_time)) if _count <= len(_result): break logging.debug('Checking Synthesize pLTL Formulae...') eval_result(_result, benign_traces, rejected_traces, _count) logging.info('Synthesized Signatures: %s' % (_result)) return _result