Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pzucker/precondition #101

Merged
merged 3 commits into from
Dec 5, 2019
Merged

Pzucker/precondition #101

merged 3 commits into from
Dec 5, 2019

Conversation

philzook58
Copy link
Contributor

@philzook58 philzook58 commented Nov 21, 2019

  • adds the ability the specify assumptions at the beginning of a program about the variables. Closes Add concrete values as preconditions #49 ? Does not address idea about soft constraints
  • It does a string find and replace on variables in smt-lib expressions given to convert them to the internal mangled names.
  • Contains bug fix for popping BIL Let bindings from the Env.var_map
  • bug fix for the returned environments from analyze_proj in wp.ml. Should fix Models are not being printed out #100
  • Moved smt-lib related functions out of precondition.ml
  • Adds the ability to create custom preconditions and postconditions in comparative analysis. The variables are referenced by appending "_orig" and "_mod" to variable names.

Example usage

bap resources/sample_binaries/dummy/hello_world.out --pass=wp --wp-compare=true --wp-file1=resources/sample_binaries/equiv_argc/main_1.bpj --wp-file2=resources/sample_binaries/equiv_argc/main_2.bpj --wp-function=main --wp-output-vars=RAX,EAX --wp-precond="(assert (= RDI_mod #x0000000000000003))  (assert (= RDI_orig #x0000000000000003))"
bap resources/sample_binaries/simple_wp/main --pass=wp --wp-precond="(assert (= RDI #x0000000000000002))"

TODO:

  • Does the naming make sense for precondition? Should it be called assumptions or hypotheses?
  • I'll also need to rebase off of Chloe's pull request
  • I still need to add tests
  • The placement of the new smt-lib code in compare.ml feels a bit ad hoc. Refactor it somewhere?

@codyroux
Copy link
Contributor

Looking good other than the conflicts! I agree that the smtlib stuff should be in its own module maybe something like Z3_utils.ml. I'll take a closer look in the morning.

(** [get_decls_and_symbols] builds from a the var_map in an environment
a mapping of all Z3 func_decl to their symbol. This is a helper function for
[mk_smtlib2] *)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a little nitpick, but there are no blank lines between the ocamldoc comments and the function signatures in the other functions, and I think it would be great to be consistent.

exp_to_z3_body body env'
let z3_expr, env = exp_to_z3_body body env' in
let env = Env.remove_var env v in
(z3_expr, env)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a variable in the let-binding shares a name with a variable that already existed in the env, would removing it also remove the original variable? If so, it might be good to write a comment about that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, this could conceivable occur if some pervert wanted to name a let-bound variable RAX, or more pragmatically if an inner let shadows an outer let. This should cause a run-time error rather than a mysterious change in semantics. A comment might be appropriate though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. That's an interesting point. We didn't implement this correctly basically. We should store the variable we're covering up. In practice it probably isn't a problem because the let bindings seem to use names like "$1", but should probably still be fixed for sanity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really good catch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I fixed this

let compare_subs_eq
~input:(input : Var.Set.t)
~output:(output : Var.Set.t)
~original:(original : Sub.t * Env.t)
~modified:(modified : Sub.t * Env.t)
~smtlib_post:(smtlib_post : string)
~smtlib_pre:(smtlib_pre : string)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, hypothesis might be a better name for this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So smtlib_hyp or smtlib_hype if we're trying to appeal to the youth.

@@ -123,7 +123,7 @@ let test_sub_pair_1 (test_ctx : test_ctxt) : unit =
let output_vars = Var.Set.singleton z in
let compare_prop, _, _ = Comp.compare_subs_eq
~input:input_vars ~output:output_vars
~original:(sub1,env1) ~modified:(sub2,env2) in
~original:(sub1,env1) ~modified:(sub2,env2) ~smtlib_post:"" ~smtlib_pre:"" in

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should smtlib_post and smtlib_pre be optional arguments defaulting to ""? This one is really up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. i debated this one. I just hated putting them as the first arguments, which optional seems to require. But it might be the right call

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vote for true and false as defaults, respectively.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the defaults both be true?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think you're right, actually.

@@ -833,6 +796,7 @@ let check ?refute:(refute = true) (solver : Solver.solver) (ctx : Z3.context)
else
pre'
in
Printf.printf "Z3 Query:\n%s\n" (Z3.Expr.to_string (Z3.Expr.simplify is_correct None));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we can make this an info log or something so that it doesn't print out when we run the unit tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable. Or perhaps a command line flag.
Is there an insane amount of stuff in info? I would be of the opinion we also suppress the Constrain being printed by default and maybe the BIL.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not against having a flag for verbose mode, but it's probably out of the scope for this PR. And there isn't too much stuff in info. From grepping, I can see we have 8 info logs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea info seems reasonable... IDK.

@fortunac
Copy link

Looks super good! Just a few nitpicks. I agree, putting the smtlib functions in its own module and adding unit test(s) for the compare module is a great idea. Good work! 🎉

minor fixes to formatting and 1 silly bug

(**

This module exports types and utilities to process and report results found
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update this comment to match the z3_utils module?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Good catch.

@codyroux codyroux merged commit 6edf3be into master Dec 5, 2019
@codyroux codyroux deleted the pzucker/precondition branch December 5, 2019 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Models are not being printed out Add concrete values as preconditions
3 participants