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

How to check if a variable can be negative? #25

Closed
frapik99 opened this issue Sep 26, 2016 · 5 comments
Closed

How to check if a variable can be negative? #25

frapik99 opened this issue Sep 26, 2016 · 5 comments

Comments

@frapik99
Copy link

frapik99 commented Sep 26, 2016

Hi,
I'm trying to understand how to check if a number can be negative.

I write this piece of code but I'm note that it is correct:

import claripy
a = claripy.BVV(0xffffffff, 32)
b = claripy.BVS('var_b', 32)
s = claripy.Solver()
s.add(claripy.SLT(b, a))

print s.satisfiable()
print s.eval(b, 1)

The output was:

True
(4294967294L,)

The goal is to check if b can be negative.

is it the proper way of doing it?

Thanks

@zardus
Copy link
Member

zardus commented Sep 26, 2016

That looks like it should work, but it seems more accurate to set a = claripy.BVV(0, 32). -1 is negative, after all.

@axt
Copy link
Contributor

axt commented Sep 26, 2016

I'm not sure if this approach is correct, but this is how I try to infer ranges of singed variables:
https://github.com/axt/angr-utils/blob/master/angrutils/expr.py

If you check the sat_negative function, what I do is to evaluate if the highest bit of the variable being one could be satisfied.

@rhelmot
Copy link
Member

rhelmot commented Sep 26, 2016

Can confirm that this approach is correct too. I have no idea what the performance looks like compared to the inequality method, but my suspicion is that z3 is more heavily optimized for inequalities than bit-extraction logic. I really wish we had someone on z3 to whom we can ask these questions, or at least someone with a background in SMT solving...

@zardus
Copy link
Member

zardus commented Sep 26, 2016

For bit-vectors, it wouldn't surprise me if the bit-extraction logic was better, in terms of Z3 being able to come up with simpler constraints internally for the actual solve, but I'm definitely as clueless about the inner workings of SMT solvers as the average bear.

@ltfish
Copy link
Member

ltfish commented Sep 26, 2016

I really wish we had someone on z3 to whom we can ask these questions, or at least someone with a background in SMT solving

Just open an issue on Z3Solver GitHub repo!

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

No branches or pull requests

5 participants