-
Notifications
You must be signed in to change notification settings - Fork 12
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
base: master
Are you sure you want to change the base?
Handle representation clause address #233
Conversation
gnat2goto/driver/tree_walk.adb
Outdated
-- | ||
-- Produce this C code: | ||
-------------------- | ||
-- &Var = (VarType*)system__to_address(hex_address); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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;
}
4c4708d
to
512e734
Compare
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
gnat2goto/driver/tree_walk.adb
Outdated
begin | ||
New_Object_Symbol_Entry | ||
(Object_Name => | ||
Intern ("Ptr_" & Get_Identifier (Target_Name)), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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#));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
f8b4bdd
to
e2d420f
Compare
89d770a
to
94ef023
Compare
There was a problem hiding this 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 | |||
|
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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.).
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".
94ef023
to
743d8d9
Compare
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
|
@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. |
I know Michael and Kareem were working on similar things; I will have a chat with them. |
Hello @tjj2017, have you seen this PR? Chris said you might be working on something related to |
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
.