11use jingle:: JingleContext ;
2- use jingle:: modeling:: { ModeledBlock , ModeledInstruction , ModelingContext } ;
2+ use jingle:: modeling:: ModeledInstruction ;
33use jingle:: sleigh:: Instruction ;
44use std:: cmp:: Ordering ;
55use std:: sync:: Arc ;
66use tracing:: { Level , event, instrument} ;
7- use z3:: { Config , Context , Solver } ;
7+ use z3:: { Config , Context } ;
88
99use crate :: error:: CrackersError ;
1010use crate :: error:: CrackersError :: EmptySpecification ;
1111use crate :: gadget:: candidates:: { CandidateBuilder , Candidates } ;
1212use crate :: gadget:: library:: GadgetLibrary ;
13- use crate :: synthesis:: assignment_model:: AssignmentModel ;
13+ use crate :: synthesis:: assignment_model:: builder :: { ArchInfo , AssignmentModelBuilder } ;
1414use crate :: synthesis:: builder:: {
1515 StateConstraintGenerator , SynthesisParams , SynthesisSelectionStrategy ,
1616 TransitionConstraintGenerator ,
1717} ;
1818use crate :: synthesis:: pcode_theory:: builder:: PcodeTheoryBuilder ;
19- use crate :: synthesis:: pcode_theory:: pcode_assignment:: PcodeAssignment ;
2019use crate :: synthesis:: pcode_theory:: theory_worker:: TheoryWorker ;
2120use crate :: synthesis:: selection_strategy:: AssignmentResult :: { Failure , Success } ;
2221use crate :: synthesis:: selection_strategy:: OuterProblem :: { OptimizeProb , SatProb } ;
2322use crate :: synthesis:: selection_strategy:: optimization_problem:: OptimizationProblem ;
2423use crate :: synthesis:: selection_strategy:: sat_problem:: SatProblem ;
2524use crate :: synthesis:: selection_strategy:: { OuterProblem , SelectionFailure , SelectionStrategy } ;
25+ use crate :: synthesis:: slot_assignments:: SlotAssignments ;
2626
2727pub mod assignment_model;
2828pub mod builder;
@@ -45,8 +45,8 @@ impl PartialOrd for Decision {
4545}
4646
4747#[ derive( Debug ) ]
48- pub enum DecisionResult < ' ctx , T : ModelingContext < ' ctx > > {
49- AssignmentFound ( AssignmentModel < ' ctx , T > ) ,
48+ pub enum DecisionResult {
49+ AssignmentFound ( AssignmentModelBuilder ) ,
5050 Unsat ( SelectionFailure ) ,
5151}
5252
@@ -107,11 +107,54 @@ impl<'ctx> AssignmentSynthesis<'ctx> {
107107 } )
108108 }
109109
110+ fn make_model_builder ( & self , slot_assignments : SlotAssignments ) -> AssignmentModelBuilder {
111+ AssignmentModelBuilder {
112+ templates : self . instructions . clone ( ) ,
113+ gadgets : slot_assignments. interpret_from_library ( & self . candidates ) ,
114+ preconditions : self . preconditions . clone ( ) ,
115+ postconditions : self . postconditions . clone ( ) ,
116+ pointer_invariants : self . pointer_invariants . clone ( ) ,
117+ arch_info : ArchInfo :: from ( self . library . as_ref ( ) ) ,
118+ }
119+ }
120+
121+ fn make_pcode_theory_builder ( & self ) -> PcodeTheoryBuilder {
122+ PcodeTheoryBuilder :: new ( self . candidates . clone ( ) , & self . library )
123+ . with_pointer_invariants ( & self . pointer_invariants )
124+ . with_preconditions ( & self . preconditions )
125+ . with_postconditions ( & self . postconditions )
126+ . with_max_candidates ( self . candidates_per_slot )
127+ . with_templates ( self . instructions . clone ( ) . into_iter ( ) )
128+ }
129+
130+ pub fn decide_single_threaded ( & mut self ) -> Result < DecisionResult , CrackersError > {
131+ let theory_builder = self . make_pcode_theory_builder ( ) ;
132+ let theory = theory_builder. build ( self . z3 ) ?;
133+ loop {
134+ let assignment = self . outer_problem . get_assignments ( ) ?;
135+ match assignment {
136+ Success ( a) => {
137+ let theory_result = theory. check_assignment ( & a) ?;
138+ match theory_result {
139+ None => {
140+ // success
141+ return Ok ( DecisionResult :: AssignmentFound ( self . make_model_builder ( a) ) ) ;
142+ }
143+ Some ( conflict) => {
144+ self . outer_problem . add_theory_clauses ( & conflict) ;
145+ }
146+ }
147+ }
148+ Failure ( d) => return Ok ( DecisionResult :: Unsat ( d) ) ,
149+ }
150+ }
151+ }
110152 #[ instrument( skip_all) ]
111- pub fn decide ( & mut self ) -> Result < DecisionResult < ' ctx , ModeledBlock < ' ctx > > , CrackersError > {
153+ pub fn decide ( & mut self ) -> Result < DecisionResult , CrackersError > {
112154 let mut req_channels = vec ! [ ] ;
113155 let mut kill_senders = vec ! [ ] ;
114- let theory_builder = PcodeTheoryBuilder :: new ( self . candidates . clone ( ) , & self . library )
156+ let library = self . library . clone ( ) ;
157+ let theory_builder = PcodeTheoryBuilder :: new ( self . candidates . clone ( ) , & library)
115158 . with_pointer_invariants ( & self . pointer_invariants )
116159 . with_preconditions ( & self . preconditions )
117160 . with_postconditions ( & self . postconditions )
@@ -188,19 +231,9 @@ impl<'ctx> AssignmentSynthesis<'ctx> {
188231 x. send ( ( ) ) . unwrap ( ) ;
189232 }
190233 kill_senders. clear ( ) ;
191- let t = theory_builder. clone ( ) ;
192- let jingle = JingleContext :: new ( self . z3 , self . library . as_ref ( ) ) ;
193- let a: PcodeAssignment < ' ctx > =
194- t. build_assignment ( & jingle, response. assignment ) ?;
195- event ! (
196- Level :: DEBUG ,
197- "Workers Terminated; building and checking model"
198- ) ;
199- let solver = Solver :: new ( self . z3 ) ;
200- let jingle = JingleContext :: new ( self . z3 , self . library . as_ref ( ) ) ;
201- let model = a. check ( & jingle, & solver) ?;
202- event ! ( Level :: DEBUG , "Model built" ) ;
203- return Ok ( DecisionResult :: AssignmentFound ( model) ) ;
234+ return Ok ( DecisionResult :: AssignmentFound (
235+ self . make_model_builder ( response. assignment ) ,
236+ ) ) ;
204237 }
205238 Some ( c) => {
206239 event ! (
0 commit comments