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

fix rev16 instruction #1128

Merged
merged 9 commits into from
Jun 25, 2020
Merged

Conversation

Phosphorus15
Copy link
Contributor

Hopefully this PR will fix #1127, not sure if this would affects related tests.

lib/arm/arm_lifter.ml Outdated Show resolved Hide resolved
Copy link
Member

@ivg ivg left a comment

Choose a reason for hiding this comment

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

please use extract and express the semantics exactly as in the Reference Manual.

And thanks for fixing it.

@Phosphorus15
Copy link
Contributor Author

please use extract and express the semantics exactly as in the Reference Manual.

And thanks for fixing it.

Thanks for the fast and detailed response, would you think it a good idea that I change the REV instruction to use extract too? since its current implementation is using bitmask & logand too:

| `REV, [|`Reg dest; src; cond; _|] ->
    let s = exp_of_op src in
    let i24 = int32 24 in
    let i8 = int32 8 in
    let umask = int32 0xff0000 in
    let lmask = int32 0xff00 in
    let rev =
      let open Bil in
      s              lsl i24 lor
      s              lsr i24 lor
      (s land umask) lsr i8  lor
      (s land lmask) lsl i8
    in

@ivg
Copy link
Member

ivg commented Jun 15, 2020

Thanks for the fast and detailed response, would you think it a good idea that I change the REV instruction to use extract too? since its current implementation is using bitmask & logand too:

Certainly, it is a good idea!

@Phosphorus15
Copy link
Contributor Author

Phosphorus15 commented Jun 17, 2020

Please check if the fix is valid and can be merged @ivg .

@ivg
Copy link
Member

ivg commented Jun 17, 2020

@Phosphorus15, we use the ready for review label to make it easier to identify PRs which are, well, ready for review :)

((extract 15 8 s) lsr i8) lor
((extract 23 16 s) lsl i8) lor
((extract 31 24 s) lsr i8)
) in
Copy link
Member

Choose a reason for hiding this comment

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

Semanitcs notes:

The code is not well-typed and rev is always 0. The expression extract 7 0 s has type i8 (i.e., an 8-bit word). Thus you're shifting 8-bit words left or right by eight bits (getting zeros always). When you lor N 8-bit words, you will still get an 8-bit word. Finally, you assing the dest variable (which is a 32-bit register) an 8-bit word. And booms.

Instead of using shifts and ors, you can use extract and concat which lets you express the semantics exactly as in the reference manual.

Style notes:

  1. Do not leave in ) in, ), or any other purely grammatical/service words on a single line. Each line of code, shall be a line of code. The exception is begin/end and other structural expressions, such as while, for, etc

  2. Do not put superfluous parentheses. They clutter the code and confuse as it is not clear what were the author's intentions.

In this case, parenthesis around extract are acceptable, but keep in mind that function applications bind tighter than infix operators. Since in this case the infix operator is fully made of ASCII characters, it is really easy to confuse it with function application especially when there is no syntax highlighting, e.g., extract 7 0 s lsl i8 is hard to parse without highlighting, thoug it has the same (extract 7 0 s) lsl i8 meaning.

let rev =
let open Bil in
s lsl i24 lor
s lsr i24 lor
(s land umask) lsr i8 lor
(s land lmask) lsl i8
(extract 23 16 s) lsr i8 lor
Copy link
Member

Choose a reason for hiding this comment

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

see the comment about REV16, it is also applicable here. And please, also just use a concatenation of 4 bytes, obtained with extract.

Copy link
Member

@ivg ivg left a comment

Choose a reason for hiding this comment

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

See the inline comments.

Also, it is advised to run tests locally. E.g., make test for unit tests (provided that you passed --with-tests to the ./configure script), and make check for the functional tests. This will help you to catch bugs earlier and enable shorter iterations. And remove stress from Travis, which is currently on the edge of collapsing (in other words it doesn't really work).

@ivg ivg added changes-requested the PR was reviewed and needs reporter actions and removed ready for review labels Jun 17, 2020
@Phosphorus15 Phosphorus15 requested a review from ivg June 18, 2020 10:57
(s land umask) lsr i8 lor
(s land lmask) lsl i8
in
let rev = Bil.(concat
Copy link
Member

Choose a reason for hiding this comment

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

First of all, the code is not indented correctly. Second, and most importantly, you can use the [(^)] operator to make your code much more readable, e.g.,

    let rev = Bil.(extract 7 0 s ^
                   extract 15 8 s ^
                   extract 23 16 s ^
                   extract 31 24 s) in

exec [assn (Env.of_reg dest) rev] cond
| `REV16, [|`Reg dest; src; cond; _|] ->
let s = exp_of_op src in
let i16 = int32 16 in
let rev = Bil.(s lsl i16 lor s lsr i16) in
let rev = Bil.(concat
Copy link
Member

Choose a reason for hiding this comment

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

the same is true for this snippet, use ^ for concatenation.

Copy link
Member

@ivg ivg left a comment

Choose a reason for hiding this comment

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

now it looks awesome!

@ivg
Copy link
Member

ivg commented Jun 25, 2020

thanks for fixing it and making the code nicer, good job!

@ivg ivg merged commit 68582d1 into BinaryAnalysisPlatform:master Jun 25, 2020
ivg added a commit to ivg/bap that referenced this pull request Jun 25, 2020
* fix rev16 instruction

* use extract in place of bitmask

* use 'concat' and 'extract' syntax

* use better sematics

Co-authored-by: Ivan Gotovchits <ivg@ieee.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changes-requested the PR was reviewed and needs reporter actions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect instruction interpretation within ARM lifter
2 participants