Skip to content

Fix linking in case of function type conflict #6547

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 2 commits into from
Oct 10, 2022
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
5 changes: 4 additions & 1 deletion regression/ansi-c/undeclared_function/fileB.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <stdlib.h>
void *malloc(__CPROVER_size_t s)
{
return (void *)0 + s;
}

int f()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
CORE gcc-only
fileA.c
fileB.c --validate-goto-model
^EXIT=0$
Expand Down
13 changes: 13 additions & 0 deletions regression/cbmc/incomplete-structs/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CORE
typesmain.c
types1.c types2.c types3.c
reason for conflict at \*#this.u: number of members is different \(3/0\)
reason for conflict at \*#this.u: number of members is different \(0/3\)
struct U \(incomplete\)
warning: pointer parameter types differ between declaration and definition "bar"
warning: pointer parameter types differ between declaration and definition "foo"
^VERIFICATION SUCCESSFUL$
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
9 changes: 9 additions & 0 deletions regression/cbmc/incomplete-structs/types1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct S
{
int s;
} s_object;

int foobar()
{
return s_object.s;
}
16 changes: 16 additions & 0 deletions regression/cbmc/incomplete-structs/types2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
struct S
{
struct T *t;
struct U *u;
};

struct U
{
struct S *s;
int j;
};

int bar(struct S *s)
{
return s->u->j;
}
17 changes: 17 additions & 0 deletions regression/cbmc/incomplete-structs/types3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
struct T
{
int i;
};

struct S
{
struct T *t;
struct U *u;
};

int bar(struct S *);

int foo(struct S *s)
{
return bar(s) + s->t->i;
}
29 changes: 29 additions & 0 deletions regression/cbmc/incomplete-structs/typesmain.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <assert.h>

struct T
{
int i;
};

struct U
{
struct S *s;
int j;
};

struct S
{
struct T *t;
struct U *u;
};

int foo(struct S *s);

int main()
{
struct T t = {10};
struct U u = {0, 32};
struct S s = {&t, &u};
int res = foo(&s);
assert(res == 42);
}
5 changes: 2 additions & 3 deletions regression/cbmc/return6/test.desc
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
CORE
main.c
f_def.c
^EXIT=6$
^VERIFICATION SUCCESSFUL$
^EXIT=0$
^SIGNAL=0$
CONVERSION ERROR
--
^warning: ignoring
^VERIFICATION SUCCESSFUL$
2 changes: 1 addition & 1 deletion regression/linking-goto-binaries/chain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ else
$goto_cc "${main}.gb" "${next}.gb" -o "final.gb"
fi

$cbmc "final.gb"
$cbmc --validate-goto-model "final.gb"
19 changes: 19 additions & 0 deletions regression/linking-goto-binaries/type_conflicts/Linking7-main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
struct S
{
void *a;
void *b;
};

typedef void (*fptr)(struct S);

extern void foo(struct S s);

fptr A[] = {foo};

extern void bar();

int main()
{
bar();
return 0;
}
32 changes: 32 additions & 0 deletions regression/linking-goto-binaries/type_conflicts/Linking7-module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <assert.h>

struct S
{
void *a;
void *b;
};

typedef void (*fptr)(struct S);

extern fptr A[1];

struct real_S
{
int *a;
int *b;
};

void foo(struct real_S g)
{
assert(*g.a == 42);
assert(*g.b == 41);
}

void bar()
{
int x = 42;
struct real_S s;
s.a = &x;
s.b = &x;
A[0]((struct S){s.a, s.b});
}
32 changes: 32 additions & 0 deletions regression/linking-goto-binaries/type_conflicts/Linking7-module2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <assert.h>

struct S
{
void *a;
void *b;
};

typedef void (*fptr)(struct S);

extern fptr A[1];

struct real_S
{
int *a;
int *c;
};

void foo(struct real_S g)
{
assert(*g.a == 42);
assert(*g.c == 41);
}

void bar()
{
int x = 42;
struct real_S s;
s.a = &x;
s.c = &x;
A[0]((struct S){s.a, s.c});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <assert.h>

struct S
{
int i;
};

struct S *function();

int main()
{
assert(function() != 0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
struct S
{
int i;
int j; // extra member
} some_var;

struct S *function()
{
return &some_var;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
Linking7-main.c
Linking7-module2.c
^EXIT=10$
^SIGNAL=0$
^VERIFICATION FAILED$
line 21 assertion \*g\.a == 42: SUCCESS
line 22 assertion \*g\.c == 41: FAILURE
^\*\* 1 of 3 failed
--
^warning: ignoring
10 changes: 10 additions & 0 deletions regression/linking-goto-binaries/type_conflicts/return_type.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CORE
Linking7-return_type1.c
Linking7-return_type2.c
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
Note issue #3081
11 changes: 11 additions & 0 deletions regression/linking-goto-binaries/type_conflicts/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
Linking7-main.c
Linking7-module.c
^EXIT=10$
^SIGNAL=0$
^VERIFICATION FAILED$
^\*\* 1 of 3 failed
line 21 assertion \*g\.a == 42: SUCCESS
line 22 assertion \*g\.b == 41: FAILURE
--
^warning: ignoring
30 changes: 25 additions & 5 deletions src/goto-programs/link_goto_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void finalize_linking(
goto_modelt &dest,
const replace_symbolt::expr_mapt &type_updates)
{
unchecked_replace_symbolt object_type_updates;
casting_replace_symbolt object_type_updates;
object_type_updates.get_expr_map().insert(
type_updates.begin(), type_updates.end());

Expand Down Expand Up @@ -196,10 +196,30 @@ void finalize_linking(
{
for(auto &instruction : gf_entry.second.body.instructions)
{
instruction.transform([&object_type_updates](exprt expr) {
object_type_updates(expr);
return expr;
});
if(instruction.is_function_call())
{
const bool changed =
!object_type_updates.replace(instruction.call_function());
if(changed && instruction.call_lhs().is_not_nil())
{
object_type_updates(instruction.call_lhs());
if(
instruction.call_lhs().type() !=
to_code_type(instruction.call_function().type()).return_type())
{
instruction.call_lhs() = typecast_exprt{
instruction.call_lhs(),
to_code_type(instruction.call_function().type()).return_type()};
}
}
}
else
{
instruction.transform([&object_type_updates](exprt expr) {
const bool changed = !object_type_updates.replace(expr);
return changed ? optionalt<exprt>{expr} : nullopt;
});
}
}
}
}
Expand Down
Loading