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

Variable strings size #2044

Merged
merged 5 commits into from
Nov 24, 2021
Merged

Conversation

viktormalik
Copy link
Contributor

@viktormalik viktormalik commented Oct 22, 2021

Instead of using 64 bytes for all strings, use the actual size for string literals. The size includes the terminating null character. When multiple different strings are assigned to a variable/map/map key, its size is determined by the largest assigned string. Strings with length unknown at compile time remain 64 bytes long.

This saves some space on the stack as string literals are usually short and also fixes issues coming from different string sizes, such as #2025.

Fixes #2025, also replaces and closes #2036.

Note: to simplify reviewing, I split implementation for variables, maps, and tuples into separate commits. However, tests do not pass after each commit (since the string literal size is changed in the first commit) so I can squash them together after the reviews, if you prefer.

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

Copy link
Member

@fbs fbs left a comment

Choose a reason for hiding this comment

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

Looks good :)

src/ast/passes/semantic_analyser.cpp Show resolved Hide resolved
src/ast/passes/semantic_analyser.cpp Show resolved Hide resolved
Instead of using 64 bytes for all strings, use their actual size for
literals. The size includes the terminating null character.

This implements support for variable-sized strings for local vars by
using the maximal size of a string assigned to a variable as the
variable size.
Support for variable string size in map values. Maps must be always
updated with same sized values, so we need to fill zeroes in case of
updating it with a shorter string.

This commit also removes SemanticAnalyser::update_assign_map_type, which
didn't do anything useful and replaced it by update_string_size.
Support variable string size in both single- and multi-valued keys.
Similarly to map values, when a string smaller than the key size is
used, we fill the rest with zeroes.

Semantic analysis for map keys has been changed a bit - map key updating
starts in the 2nd pass as we need to wait until
`map.skip_key_validation` is properly set in the first pass. This
affects some of the semantic analyser tests.
This is done in a similar way to support for variable-sized simple
strings, except it is more difficult to check if one tuple has all
strings smaller than the other tuple. We add a new method
SizedType::FitsInto to check this.
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.

BUG: Struct size mismatch
2 participants