Skip to content

Allow assume bounds casts for function pointers: tests #407

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
Jul 8, 2020
Merged
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
25 changes: 25 additions & 0 deletions tests/typechecking/function_casts.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,31 @@ void const_field_call(struct S1_Const s1, union U1_Const u1) {
u1.f2(0, 0);
}

// Check _Assume_bounds_casts
void assume_bounds_casts(struct S1 s1, struct S1_Const s1_const, ptr<int(int)> f1, int(*f2)(int, int), void(*g2)(void)) {
// _Assume_bounds_casts can be used to cast a checked function pointer
// to a checked function pointer.
ptr<int(int, int)> safe = _Assume_bounds_cast<ptr<int(int, int)>>(s1.f1);

// _Assume_bounds_casts can be used to cast an unchecked function pointer
// to a checked function pointer (if the unchecked pointer's pointee type
// is compatible with the checked pointer's pointee type).
ptr<int(int, int)> unsafe_to_safe = _Assume_bounds_cast<ptr<int(int, int)>>(s1.f2);
const ptr<int(int, int)> unsafe_to_safe_const = _Assume_bounds_cast<const ptr<int(int, int)>>(s1_const.f2);
s1.f1 = _Assume_bounds_cast<ptr<int(int, int)>>(f2);
s1.f1 = _Assume_bounds_cast<ptr<int(int, int)>>(s1.f2);

// _Assume_bounds_casts can be used to cast null to a checked function pointer.
ptr<int(void)> null_to_safe = _Assume_bounds_cast<ptr<int(void)>>(0);
s1.f1 = _Assume_bounds_cast<ptr<int(int, int)>>(0);

// _Assume_bounds_casts cannot be used to cast to a checked function pointer
// from an incompatible checked or unchecked type.
ptr<void(void)> incompat_safe = _Assume_bounds_cast<ptr<void(void)>>(f1); // expected-error {{cast to checked function pointer type '_Ptr<void (void)>' from incompatible checked pointer type '_Ptr<int (int)>'}}
ptr<int(int)> incompat_unsafe = _Assume_bounds_cast<ptr<int(int)>>(f2); // expected-error {{cast to checked function pointer type '_Ptr<int (int)>' from incompatible type 'int (*)(int, int)'}}
s1.f1 = _Assume_bounds_cast<ptr<int(int, int)>>(g2); // expected-error {{cast to checked function pointer type '_Ptr<int (int, int)>' from incompatible type 'void (*)(void)'}}
}

// Constness propagated via member access.
struct myops {
_Ptr<void (void)> myfptr;
Expand Down