1+ use std:: sync:: Arc ;
2+
13use jingle:: modeling:: { ModeledInstruction , ModelingContext } ;
24use jingle:: sleigh:: { Instruction , OpCode } ;
35use jingle:: JingleContext ;
46use tracing:: trace;
5- use z3:: ast:: Ast ;
7+ use z3:: ast:: { Ast , Bool } ;
68use z3:: Solver ;
79
810use crate :: gadget:: signature:: GadgetSignature ;
911use crate :: gadget:: Gadget ;
12+ use crate :: synthesis:: builder:: StateConstraintGenerator ;
1013
1114pub struct TraceCandidateIterator < ' ctx , ' a , T >
1215where
@@ -15,20 +18,27 @@ where
1518 jingle : JingleContext < ' ctx > ,
1619 _solver : Solver < ' ctx > ,
1720 gadgets : T ,
18- trace : Vec < ModeledInstruction < ' ctx > > ,
21+ step_length : usize ,
22+ postconditions : Vec < Arc < StateConstraintGenerator > > ,
1923}
2024
2125impl < ' ctx , ' a , T > TraceCandidateIterator < ' ctx , ' a , T >
2226where
2327 T : Iterator < Item = & ' a Gadget > ,
2428{
25- pub ( crate ) fn new ( jingle : & JingleContext < ' ctx > , gadgets : T , trace : Vec < ModeledInstruction < ' ctx > > ) -> Self {
29+ pub ( crate ) fn new (
30+ jingle : & JingleContext < ' ctx > ,
31+ gadgets : T ,
32+ step_length : usize ,
33+ postconditions : Vec < Arc < StateConstraintGenerator > > ,
34+ ) -> Self {
2635 let _solver = Solver :: new ( jingle. z3 ) ;
2736 Self {
2837 jingle : jingle. clone ( ) ,
2938 _solver,
3039 gadgets,
31- trace,
40+ step_length,
41+ postconditions,
3242 }
3343 }
3444}
@@ -39,41 +49,27 @@ where
3949 type Item = Vec < Option < & ' a Gadget > > ;
4050
4151 fn next ( & mut self ) -> Option < Self :: Item > {
42- let mut next_entry = vec ! [ None ; self . trace. len( ) ] ;
4352 loop {
4453 let gadget = self . gadgets . next ( ) ?;
45- let gadget_signature = GadgetSignature :: from ( gadget) ;
4654 trace ! ( "Evaluating gadget at {:x}" , gadget. address( ) ) ;
47- let is_candidate: Vec < bool > = self
48- . trace
49- . iter ( )
50- . map ( |i| {
51- trace ! ( "Checking {} signature vs gadget {}" , i. instr. disassembly, gadget) ;
52-
53- gadget_signature. covers ( & GadgetSignature :: from_instr ( & i. instr , i) )
54- && has_compatible_control_flow ( & i. instr , gadget)
55- } )
56- . collect ( ) ;
57- if is_candidate. iter ( ) . any ( |b| * b) {
58- let model = gadget. model ( & self . jingle ) ;
59- if let Ok ( model) = & model {
60- is_candidate. iter ( ) . enumerate ( ) . for_each ( |( i, c) | {
61- if * c {
62- let expr = model
63- . upholds_postcondition ( & self . trace [ i] )
64- . unwrap ( )
65- . simplify ( ) ;
66- if !expr. is_const ( ) || expr. as_bool ( ) . unwrap ( ) {
67- next_entry[ i] = Some ( gadget)
55+ let model = gadget. model ( & self . jingle ) ;
56+ if let Ok ( model) = & model {
57+ for predicate in & self . postconditions {
58+ let f = predicate ( & self . jingle , model. get_final_state ( ) , 0 ) ;
59+ let i = predicate ( & self . jingle , model. get_original_state ( ) , 0 ) ;
60+ if let Ok ( f) = f{
61+ if let Ok ( i) = i {
62+ let f = f. simplify ( ) ;
63+ //
64+ if f. is_const ( ) && f. as_bool ( ) . unwrap ( ) || !f. is_const ( ) {
65+ let i = i. simplify ( ) ;
66+ if !f. _eq ( & i) . simplify ( ) . is_const ( ) {
67+ return Some ( vec ! [ Some ( gadget) ; self . step_length] ) ;
68+ }
6869 }
6970 }
70- } )
71- } else {
72- trace ! ( "Could not model gadget: \n {}" , gadget)
71+ }
7372 }
74- return Some ( next_entry) ;
75- } else {
76- continue ;
7773 }
7874 }
7975 }
0 commit comments