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

New intrinsic for initialization of variables #436

Merged
merged 7 commits into from
Jun 17, 2022
Merged

Conversation

rafaelsamenezes
Copy link
Contributor

This add the intrinsic __ESBMC_init_var(void*), which initializes the value of a given symbol with nondeterministic values, this is useful for initializing constant-sized arrays and structures.

Copy link
Contributor

@lucasccordeiro lucasccordeiro left a comment

Choose a reason for hiding this comment

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

LGTM.

@mikhailramalho
Copy link
Member

We don't actually need an intrinsic for that...

Two easier solutions:

  1. When processing the goto program, you can add a transformation module that add nondet side effects to all decl_exprt.
  2. During symex, you can add the nondet initialization when handling DECL instructions.

On both cases, you'll only need to add a flag to esbmc that should force such behavior (I think cbmc has a flag for that as well) . I suggest going for option 1, which is a more modular approach.

@rafaelsamenezes
Copy link
Contributor Author

We don't actually need an intrinsic for that...

From what I understood, this means that every non-initialized variable is going to be nondeterministic, right?

How that would work for global arrays? For instance:

char arr[100];
int loop_init() {
        nondet_init_array();
        int a = foo();
        while (a > 1)
                a = a / 2;
        __ESBMC_assert(a == 2, "Failed as expected");

        return 0;
}

@mikhailramalho
Copy link
Member

mikhailramalho commented Apr 29, 2021 via email

@rafaelsamenezes
Copy link
Contributor Author

Nope, because global and static variables are initialized by the frontend.

Is there a way to mark a global array as nondet? This was the main intent of this intrinsic.

@mikhailramalho
Copy link
Member

mikhailramalho commented Apr 29, 2021 via email

@mikhailramalho
Copy link
Member

mikhailramalho commented Apr 29, 2021 via email

@rafaelsamenezes
Copy link
Contributor Author

There still a need for this Intrinsic. Some users want to have a default way not only to mark a variable as nondet but to also keep making it nondet during the flow. Also this should be a part of #652

int a;
__ESBMC_init_var(a);
// do stuff with a ...
__ESBMC_init_var(a);
// do stuff with other a ...

@mikhailramalho could you look into this again?

1 similar comment
@rafaelsamenezes
Copy link
Contributor Author

There still a need for this Intrinsic. Some users want to have a default way not only to mark a variable as nondet but to also keep making it nondet during the flow. Also this should be a part of #652

int a;
__ESBMC_init_var(a);
// do stuff with a ...
__ESBMC_init_var(a);
// do stuff with other a ...

@mikhailramalho could you look into this again?

src/goto-symex/symex_main.cpp Outdated Show resolved Hide resolved
src/goto-symex/symex_main.cpp Outdated Show resolved Hide resolved
src/goto-symex/symex_main.cpp Outdated Show resolved Hide resolved
src/clang-c-frontend/clang_c_language.cpp Outdated Show resolved Hide resolved
@mikhailramalho
Copy link
Member

There still a need for this Intrinsic. Some users want to have a default way not only to mark a variable as nondet but to also keep making it nondet during the flow. Also this should be a part of #652

int a;
__ESBMC_init_var(a);
// do stuff with a ...
__ESBMC_init_var(a);
// do stuff with other a ...

@mikhailramalho could you look into this again?

Oh, now I see! Indeed, marking a variable nondet is a bit cumbersome because of the return types of our nondet builtins. Let me just try something using macros here and I'll get back to you. My idea is something like:

#define __ESBMC_mark_nondet(var)
    __auto_type(var) nondet##var();
    var = nondet##var();

If that works, we can simply add this macro to the preprocessor!

@mikhailramalho
Copy link
Member

@rafaelsamenezes, how about something like:

#include<assert.h>

#define __ESBMC_mark_nondet(var) \
    typeof(var) nondet_##var(); \
    var = nondet_##var() \

int main() {
  int v;
  v = 0;

  assert(v == 0);
  __ESBMC_mark_nondet(v);
  assert(v == 0);
}

drawbacks: we'll create one empty symbol for each variable that we mark nondet (in this example, we are creating a new nondet_v() symbol).

This needs to be expanded to prevent type conflicts, e.g., if we define a variable v in another function in the same TU with a different type the frontend will abort the verification, but we can solve that by using a location macro (LINE maybe?).

Let me know what you think, if this ends up being too complicated, I agree we should use the new builtin.

@rafaelsamenezes
Copy link
Contributor Author

Let me know what you think, if this ends up being too complicated, I agree we should use the new builtin.

The main drawback for me is that it wont't work for arrays.

int main() {
  int v[10];
  v[5] = 0;

  assert(v[5] == 0);
  __ESBMC_mark_nondet(v);
  assert(v[5] == 0);
}

@rafaelsamenezes rafaelsamenezes added this to the ESBMC 6.10 milestone Mar 22, 2022
@mikhailramalho
Copy link
Member

Let me know what you think, if this ends up being too complicated, I agree we should use the new builtin.

The main drawback for me is that it wont't work for arrays.

int main() {
  int v[10];
  v[5] = 0;

  assert(v[5] == 0);
  __ESBMC_mark_nondet(v);
  assert(v[5] == 0);
}

True :/

The only thing annoying me is the void*, can we templatefy it? Using C's extension __auto_type?

If we can't use it, I agree we should proceed with the void pointer.

@rafaelsamenezes
Copy link
Contributor Author

Using C's extension __auto_type?

We can only do this for function definitions: error: a parameter list without types is only allowed in a function definition

Copy link
Member

@mikhailramalho mikhailramalho left a comment

Choose a reason for hiding this comment

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

I get the reasoning now, and I'm fine with this PR after checking the 2 comments I left.

src/goto-symex/symex_main.cpp Outdated Show resolved Hide resolved
src/goto-symex/symex_main.cpp Show resolved Hide resolved
@lucasccordeiro
Copy link
Contributor

@fbrausse: could you please review this PR?

Copy link
Member

@fbrausse fbrausse left a comment

Choose a reason for hiding this comment

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

Besides exception handling and some bikeshedding regarding the name (see comments), LGTM.

src/goto-symex/symex_main.cpp Outdated Show resolved Hide resolved
src/goto-symex/symex_main.cpp Outdated Show resolved Hide resolved
src/goto-symex/symex_main.cpp Show resolved Hide resolved
src/goto-symex/symex_main.cpp Show resolved Hide resolved
Copy link
Member

@mikhailramalho mikhailramalho left a comment

Choose a reason for hiding this comment

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

LGTM, just one minor comment.

@@ -0,0 +1,5 @@
int main() {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe these belong to a separate PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops. Nice catch

Copy link
Contributor

@lucasccordeiro lucasccordeiro left a comment

Choose a reason for hiding this comment

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

LGTM.

@lucasccordeiro
Copy link
Contributor

@rafaelsamenezes: could you please update our documentation page https://ssvlab.github.io/esbmc/documentation.html? You have created many intrinsics that are not documented for the ESBMC users.

@lucasccordeiro
Copy link
Contributor

@rafaelsamenezes: could you please update our documentation page https://ssvlab.github.io/esbmc/documentation.html? You have created many intrinsic that are not documented anywhere.

@rafaelsamenezes
Copy link
Contributor Author

@lucasccordeiro I just added it ssvlab/ssvlab.github.io#9

You have created many intrinsic that are not documented anywhere.

We definitely should define a proper way to document intrinsics.

@lucasccordeiro
Copy link
Contributor

I just added it ssvlab/ssvlab.github.io#9

Thanks!

@rafaelsamenezes rafaelsamenezes merged commit 2761907 into master Jun 17, 2022
@rafaelsamenezes rafaelsamenezes deleted the init_var branch June 17, 2022 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants