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

Fix misaligned stack access for map keys #2012

Merged
merged 2 commits into from
Sep 27, 2021

Conversation

viktormalik
Copy link
Contributor

@viktormalik viktormalik commented Sep 20, 2021

Using multi-value keys with one component not aligned to 8 bytes (e.g. by using buf) may cause misaligned stack access errors. This is because scalar components of keys (e.g. those of int type) are stored onto stack with 8-byte alignment.

The simplest fix is to change the alignment to 1. Hopefully, this shouldn't cause any trouble since we read the keys byte-by-byte anyways.

Fixes #2006.

Checklist
  • Language changes are updated in man/adoc/bpftrace.adoc and if needed 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 mapkey-align-fix branch 2 times, most recently from 1dbf3bb to 7ce52aa Compare September 20, 2021 07:29
@mmisono
Copy link
Collaborator

mmisono commented Sep 20, 2021

Looks good but maybe is it good to align to 1 only if the buffer size is not multiple of eight to prevent slow down?

@viktormalik
Copy link
Contributor Author

Looks good but maybe is it good to align to 1 only if the buffer size is not multiple of eight to prevent slow down?

Hm, we could do this, but the code would get a bit more complicated (we'd have to track the current alignment while creating the key). Not sure if it's worth it.

@viktormalik
Copy link
Contributor Author

In the end the code is not much more complex and since the key will be mostly aligned, it makes sense to align to 1 only if necessary.

Construction of multi-value map keys on the stack is done in multiple
places, so it is more practical to have a separate function for it.
Using multi-value keys with one component not aligned to 8 bytes (e.g.
by using buf) may cause misaligned stack access errors.

If a component is not aligned, we set the alignmnent of all the
following components to 1.

Appropriate runtime tests are added.
@viktormalik viktormalik merged commit b2ba39f into bpftrace:master Sep 27, 2021
@viktormalik viktormalik deleted the mapkey-align-fix branch November 24, 2021 12:29
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.

misaligned stack access when using buf() as a key for hist() or lhist() functions
3 participants