Skip to content

Handle representation clause address #233

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

xbauch
Copy link
Contributor

@xbauch xbauch commented May 17, 2019

Ada allows specifying the memory address for variables (which do not have to be pointers). To replicate it in C (without relying on some special compilers) we introduce a new variable of type pointer-to-original-type, change its address, and then redirect every reference to the original variable to the new pointer-to variable. The redirection takes place after the tree-walk parsing, on the irep-level, in a similar fashion as the follow_irep.

--
-- Produce this C code:
--------------------
-- &Var = (VarType*)system__to_address(hex_address);

Choose a reason for hiding this comment

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

Is the system__to_address here necessary? Seems to me that just casting the hex address directly to a pointer should work just as well.

I also don't really know what this means, addresses aren't assignable in C proper. Are addresses assignable in GOTO code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean like this?:

#include <assert.h>

int main() {
  int *var = (int*)0x40001000;

  *var = 4;
  assert(*(int*)0x40001000==4);
  assert(var==0x40001000);
  assert(*var==4);

  return 0;
}

@chrisr-diffblue chrisr-diffblue changed the title Handle representation clause address [depends-on: #218] Handle representation clause address Jun 4, 2019
@xbauch xbauch self-assigned this Jun 5, 2019
@xbauch xbauch force-pushed the feature/representation-clause-address branch 2 times, most recently from 4c4708d to 512e734 Compare June 6, 2019 13:45
@xbauch xbauch changed the title Handle representation clause address Handle representation clause address [depends-on: #231, #251] Jun 6, 2019
@xbauch xbauch marked this pull request as ready for review June 6, 2019 13:47
@xbauch xbauch removed the draft label Jun 6, 2019
@xbauch xbauch removed their assignment Jun 6, 2019
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this may be reasonable

@@ -503,14 +498,25 @@ package body GOTO_Utils is
return False;
end Has_GNAT2goto_Annotation;

function Integer_Constant_To_BV_Expr

Choose a reason for hiding this comment

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

I don't quite understand, are there non-bitvector types to which this is applicable?

Also, seems like this could be its own 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.

The CPROVER_Size_T is not a bit-vector type.

begin
New_Object_Symbol_Entry
(Object_Name =>
Intern ("Ptr_" & Get_Identifier (Target_Name)),

Choose a reason for hiding this comment

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

This name could conceivably already exist in the symbol table, how about Get_Identifier (Target_Name) & "$Ptr? Names with $ in them are valid in cbmc, but I don't think they're valid in Ada.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The dollar sounds like a good idea, I wasn't sure what decoration would be safe.

Copy link
Contributor Author

@xbauch xbauch Jun 7, 2019

Choose a reason for hiding this comment

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

Here: 43c5e3f.

for Device_Input_Value'Address use System'To_Address (16#8000_05C4#);
begin
Device_Input_Value := 5;
pragma Assert (Device_Input_Value + 1 = 6);

Choose a reason for hiding this comment

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

Can you also do pragma Assert (Device_Input_Value'Address = System'To_Address (16#8000_05C4#));

Copy link
Contributor Author

@xbauch xbauch Jun 7, 2019

Choose a reason for hiding this comment

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

This one is a bit more tricky: bb7f3ad -- f2356d1.

Choose a reason for hiding this comment

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

Had a conversation with @martin-cs OOB: that will require diffblue/cbmc#1086 to be completed and merged first.

@xbauch xbauch force-pushed the feature/representation-clause-address branch from f8b4bdd to e2d420f Compare June 7, 2019 11:40
@xbauch xbauch changed the title Handle representation clause address [depends-on: #231, #251] Handle representation clause address Jun 7, 2019
@xbauch xbauch changed the title Handle representation clause address Handle representation clause address [depends-on: #237] Jun 7, 2019
@xbauch xbauch force-pushed the feature/representation-clause-address branch from 89d770a to 94ef023 Compare June 7, 2019 12:11
Copy link
Contributor

@NlightNFotis NlightNFotis left a comment

Choose a reason for hiding this comment

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

LGTM overall, minor comments.

-- Ireps were disappearing when new list was created (even if it was filled
-- with the same ireps).
-------------------------------------------------------------------------------
separate (Ireps)
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh nice, didn't even know what this was or what it could do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well neither did I initially, but it was used before in ireps.

@@ -153,6 +153,7 @@ package body GOTO_Utils is

Copy link
Contributor

Choose a reason for hiding this comment

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

Slightly confused with the commit message here. Should it read "Add default body for some subprograms and add that to those that have identity-like type"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about the confusion, no: the commit body only specifies which are the some subprogram for which we generate the default body (those that have identity-like type, etc.).

petr-bauch added 11 commits June 7, 2019 17:03
We now have three wrappers:
1: takes values (results has type cprover_size_t)
2: takes value, type (type is assumed to be bit-vector)
3: takes value, type, bit-width.
We handle it by
1: declaring a new variable with type one pointer deeper
2: assign the address to it
3: store the name (we'll need to replace it in the symbol table -- next commit)
And that those for which the address was specified (via clause). We chase them
using the same idea as follow_irep and replace their symbol expression with
dereference expression to the newly introduced pointer-to symbol.
into the symbol table. Since it was missing and CBMC need it for pointer
analysis.
with dollar sign and via a helper function.
by returning address-of expression.
and that to those that have identity-like type: A -> A. The default body is
exactly that identity. And their name starts with "system".
@xbauch xbauch force-pushed the feature/representation-clause-address branch from 94ef023 to 743d8d9 Compare June 7, 2019 16:03
@xbauch xbauch changed the title Handle representation clause address [depends-on: #237] Handle representation clause address Jun 7, 2019
@chrisr-diffblue
Copy link
Contributor

I slightly worry that this mechanism might change the semantics of the Ada program, just because what was a bunch of straight forward assignments or reads to/from variables now includes dereference operations. I don't honestly know what other options we have though. Is it worth looking at the linker script stuff in CBMC and see if there is any code you can either piggy back on, or something you can copy to use here? e.g. If your compiler doesn't support something like __atttribute__((at(0xabcde))) but does support linker scripts (e.g. gcc) then typically what you'd do is declare your variable as something like:
int foo __attribute__((section ("FOOSEC"))) and then have a linker script that places section "FOOSEC" at a specific address, e.g.

SECTIONS
{
.fooSecRange 0xABCDEF :
{
KEEP (*(.FOOSEC))
}
}
  • paraphrasing slightly as I'm going mostly by memory - don't trust the actual syntax there... It looks like CBMC has some support for this sort of thing in src/goto-cc/linker_script_merge.cpp - so there might be some clues there?

@chrisr-diffblue
Copy link
Contributor

@martin-cs - would be good to have some input from you here too as you may have other thoughts about how this could be implemented.

@martin-cs
Copy link
Collaborator

I know Michael and Kareem were working on similar things; I will have a chat with them.

@xbauch
Copy link
Contributor Author

xbauch commented Jun 25, 2019

Hello @tjj2017, have you seen this PR? Chris said you might be working on something related to address clause. Just wanted to avoid duplicating effort.

@martin-cs martin-cs mentioned this pull request Jun 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants