Skip to content
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

Forbid casting of struct types #1873

Merged
merged 1 commit into from
Jun 15, 2021
Merged

Conversation

viktormalik
Copy link
Contributor

@viktormalik viktormalik commented Jun 9, 2021

C does not allow casting struct types by value, so we should do the same in BPFtrace.

Resolves #1869.

Checklist
  • Language changes are updated in docs/reference_guide.md
  • User-visible and non-trivial changes updated in CHANGELOG.md
  • The new behaviour is covered by tests

@viktormalik viktormalik force-pushed the forbid-struct-casts branch from 6f14aeb to 11d9380 Compare June 9, 2021 11:04
{
// Casting struct by value is forbidden
test("struct type { int field; }"
"kprobe:f { $s = (struct type)cpu; $u = (uint32)$s; }",
Copy link
Member

Choose a reason for hiding this comment

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

this seems to test the wrong thing

stdin:1:59-74: ERROR: Cannot convert unsigned int64 to struct
struct type { int field; } struct b { int a }; k:f { $s = (struct type )cpu; }
                                                          ~~~~~~~~~~~~~~~

if (cast.expr->type.IsRecordTy())
{
LOG(ERROR, cast.loc, err_)
<< "Cannot cast struct type \"" << cast.expr->type << "\"";
Copy link
Member

Choose a reason for hiding this comment

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

would be nice to include the target type as well, but that means shuffling a bit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

right, that'll also break some existing tests but we should do that nevertheless

C does not allow casting of struct types by value, so we can follow this
behaviour in BPFtrace.

Some existing semantic analyser tests used casting to struct types, so
they were changed to use pointer casts instead.
@viktormalik viktormalik force-pushed the forbid-struct-casts branch from 11d9380 to 867f68f Compare June 9, 2021 14:12
@viktormalik
Copy link
Contributor Author

Updated to block both casts from and to struct types.

@viktormalik viktormalik requested a review from fbs June 9, 2021 14:43
@fbs fbs merged commit 5562198 into bpftrace:master Jun 15, 2021
@viktormalik viktormalik deleted the forbid-struct-casts branch November 24, 2021 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bpftrace type casting problem
3 participants