Skip to content

[SV-COMP'18 14/19] Fixing member offset computation in presence of bitfields. #2003

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

Merged
merged 1 commit into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions regression/cbmc/Bitfields3/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <stdlib.h>
#include <assert.h>

#pragma pack(1)
struct A
{
unsigned char a;
unsigned char b : 2;
unsigned char c : 2;
unsigned char d;
};

struct B
{
unsigned char a;
unsigned char b : 2;
unsigned char c;
unsigned char d : 2;
};

struct C
{
unsigned char a;
unsigned char b : 4;
unsigned char c : 4;
unsigned char d;
};
#pragma pack()

int main(void)
{
assert(sizeof(struct A) == 3);
struct A *p = malloc(3);
assert((unsigned char *)p + 2 == &(p->d));
p->c = 3;
if(p->c != 3)
{
free(p);
}
free(p);

assert(sizeof(struct B) == 4);
struct B *pb = malloc(4);
assert((unsigned char *)pb + 2 == &(pb->c));
free(pb);

assert(sizeof(struct C) == 3);
struct C *pc = malloc(3);
assert((unsigned char *)pc + 2 == &(pc->d));
free(pc);

return 0;
}
8 changes: 8 additions & 0 deletions regression/cbmc/Bitfields3/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c
--pointer-check --bounds-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
15 changes: 6 additions & 9 deletions src/ansi-c/padding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,19 @@ void add_padding(struct_typet &type, const namespacet &ns)
max_alignment=a;

std::size_t w=to_c_bit_field_type(it_type).get_width();
std::size_t bytes;
for(bytes=0; w>bit_field_bits; ++bytes, bit_field_bits+=8) {}
bit_field_bits-=w;
bit_field_bits += w;
const std::size_t bytes = bit_field_bits / 8;
bit_field_bits %= 8;
offset+=bytes;
continue;
}
}
else
a=alignment(it_type, ns);

DATA_INVARIANT(
bit_field_bits == 0, "padding ensures offset at byte boundaries");

// check minimum alignment
if(a<config.ansi_c.alignment && !packed)
a=config.ansi_c.alignment;
Expand Down Expand Up @@ -246,12 +249,6 @@ void add_padding(struct_typet &type, const namespacet &ns)
offset+=size;
}

if(bit_field_bits!=0)
{
// these are now assumed to be multiples of 8
offset+=bit_field_bits/8;
}

// any explicit alignment for the struct?
if(type.find(ID_C_alignment).is_not_nil())
{
Expand Down
23 changes: 15 additions & 8 deletions src/util/pointer_offset_size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ member_offset_iterator &member_offset_iterator::operator++()
{
// take the extra bytes needed
std::size_t w=to_c_bit_field_type(comp.type()).get_width();
for(; w>bit_field_bits; ++current.second, bit_field_bits+=8) {}
bit_field_bits-=w;
bit_field_bits += w;
current.second += bit_field_bits / 8;
bit_field_bits %= 8;
}
else
{
DATA_INVARIANT(
bit_field_bits == 0, "padding ensures offset at byte boundaries");
const typet &subtype=comp.type();
mp_integer sub_size=pointer_offset_size(subtype, ns);
if(sub_size==-1)
Expand Down Expand Up @@ -287,13 +290,15 @@ exprt member_offset_expr(
if(it->type().id()==ID_c_bit_field)
{
std::size_t w=to_c_bit_field_type(it->type()).get_width();
std::size_t bytes;
for(bytes=0; w>bit_field_bits; ++bytes, bit_field_bits+=8) {}
bit_field_bits-=w;
bit_field_bits += w;
const std::size_t bytes = bit_field_bits / 8;
bit_field_bits %= 8;
result=plus_exprt(result, from_integer(bytes, result.type()));
}
else
{
DATA_INVARIANT(
bit_field_bits == 0, "padding ensures offset at byte boundaries");
const typet &subtype=it->type();
exprt sub_size=size_of_expr(subtype, ns);
if(sub_size.is_nil())
Expand Down Expand Up @@ -381,13 +386,15 @@ exprt size_of_expr(
if(it->type().id()==ID_c_bit_field)
{
std::size_t w=to_c_bit_field_type(it->type()).get_width();
std::size_t bytes;
for(bytes=0; w>bit_field_bits; ++bytes, bit_field_bits+=8) {}
bit_field_bits-=w;
bit_field_bits += w;
const std::size_t bytes = bit_field_bits / 8;
bit_field_bits %= 8;
result=plus_exprt(result, from_integer(bytes, result.type()));
}
else
{
DATA_INVARIANT(
bit_field_bits == 0, "padding ensures offset at byte boundaries");
const typet &subtype=it->type();
exprt sub_size=size_of_expr(subtype, ns);
if(sub_size.is_nil())
Expand Down