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

Incorrect rewrite of a simple program #1008

Open
anuragsiy7504 opened this issue Nov 4, 2019 · 7 comments
Open

Incorrect rewrite of a simple program #1008

anuragsiy7504 opened this issue Nov 4, 2019 · 7 comments

Comments

@anuragsiy7504
Copy link

Hello,

I am trying to optimise the following program:

#include <stdio.h>
#include <cstdlib>
#include <stddef.h>
#include <stdint.h>


int count(int var) {
  int res;
  for(res = 0; res < var; res++);
  return res;
}

int main() {
  int val = 100000;
  int res = count(val);

  printf("%d\n", res);
  return 0;
}

The Function extracted by stoke is:

  .text
  .globl _Z5counti
  .type _Z5counti, @function

#! file-offset 0x566
#! rip-offset  0x400566
#! capacity    26 bytes

# Text              #  Line  RIP       Bytes  Opcode
._Z5counti:         #        0x400566  0      OPC=<label>
  movl %edi, %eax   #  1     0x400566  2      OPC=movl_r32_r32
  testl %edi, %edi  #  2     0x400568  2      OPC=testl_r32_r32
  jle .L_40057a     #  3     0x40056a  2      OPC=jle_label
  movl $0x0, %edx   #  4     0x40056c  5      OPC=movl_r32_imm32
.L_400571:          #        0x400571  0      OPC=<label>
  addl $0x1, %edx   #  5     0x400571  3      OPC=addl_r32_imm8
  cmpl %eax, %edx   #  6     0x400574  2      OPC=cmpl_r32_r32
  jne .L_400571     #  7     0x400576  2      OPC=jne_label
  nop               #  8     0x400578  1      OPC=nop
  retq              #  9     0x400579  1      OPC=retq
.L_40057a:          #        0x40057a  0      OPC=<label>
  movl $0x0, %eax   #  10    0x40057a  5      OPC=movl_r32_imm32
  retq              #  11    0x40057f  1      OPC=retq

.size _Z5counti, .-_Z5counti

The rewrite discovered by stoke is:

  .text
  .globl _Z5counti
  .type _Z5counti, @function

#! file-offset 0x566
#! rip-offset  0x400566
#! capacity    26 bytes

# Text                    #  Line  RIP       Bytes  Opcode
._Z5counti:               #        0x400566  0      OPC=<label>
  lzcntl %edi, %edx       #  1     0x400566  4      OPC=lzcntl_r32_r32
  bzhil %edx, %edi, %eax  #  2     0x40056a  5      OPC=bzhil_r32_r32_r32
  retq                    #  3     0x40056f  1      OPC=retq

.size _Z5counti, .-_Z5counti

The stoke debug verify command :
stoke debug verify --def_in "{ %edi }" --live_out "{ %eax }" --target bins/_Z5counti.s --rewrite result.s --abi_check --strategy bounded --bound 64

Returns : Equivalent: yes

But on incorporating the rewrite into the original program produces incorrect results.
Is my understanding correct here?

@stefanheule
Copy link
Member

What is the input that produces incorrect result (what's the input, what is the expected output, and what output do you see instead)?

Without having looked too much at your code, it is definitely possible for bounded verification to say the rewrite is equivalent when it is not equivalent for all possible inputs. In particular, as the name suggest, the strategy explores a bounded set of executions, so any execution beyond this bound remain completely unchecked.

@bchurchill
Copy link
Member

bchurchill commented Nov 4, 2019 via email

@anuragsiy7504
Copy link
Author

The input to the loop is 10000.
Should I increase the --bound flag to the verifier?

I am trying to find out if stoke can optimize cases which look complex to begin with but are actually very simple.

In this case, the input program has a loop, but it can be replaced with a simple instruction.

@anuragsiy7504
Copy link
Author

Any help here?
I even tried the following verification command:
stoke debug verify --def_in "{ %edi }" --live_out "{ %eax }" --target bins/_Z5counti.s --rewrite result.s --abi_check --strategy bounded --bound 1024

Still got the output "yes"

@bchurchill
Copy link
Member

bchurchill commented Nov 15, 2019 via email

@anuragsiy7504
Copy link
Author

Any number which is greater than 16bits, leads to a mismatch.
I used the stoke_tcgen to generate test cases.

@bchurchill
Copy link
Member

bchurchill commented Nov 16, 2019 via email

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

3 participants