-
Notifications
You must be signed in to change notification settings - Fork 19
refactor(profiling): avoid panic on out-of-bounds label set id #2002
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -544,7 +544,8 @@ impl Profile { | |
|
|
||
| let iter = std::mem::take(&mut self.observations).try_into_iter()?; | ||
| for (sample, timestamp, mut values) in iter { | ||
| let labels = &mut extended_label_sets[sample.labels.to_raw_id()]; | ||
| let off = sample.labels.to_offset(); | ||
| let labels = extended_label_sets.get_mut(off).ok_or_else(oob_label_set)?; | ||
| let location_ids: Vec<_> = self | ||
| .get_stacktrace(sample.stacktrace)? | ||
| .locations | ||
|
|
@@ -1068,6 +1069,14 @@ impl Profile { | |
| } | ||
| } | ||
|
|
||
| #[cold] | ||
| fn oob_label_set() -> io::Error { | ||
| io::Error::new( | ||
| io::ErrorKind::InvalidData, | ||
| "out-of-bounds label set id found during serialization", | ||
| ) | ||
| } | ||
|
Comment on lines
+1072
to
+1078
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whats the advantage of naming this function rather than using a closure
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this specific case, to give it a Also, stylistically, it looked bad inline (because the formatter insists on formatting it across 4 lines). |
||
|
|
||
| #[cfg(test)] | ||
| mod api_tests { | ||
| use super::*; | ||
|
|
@@ -2969,4 +2978,57 @@ mod api_tests { | |
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_label_set_id_returns_error_instead_of_panicking() { | ||
| let sample_types = [api::SampleType::CpuSamples]; | ||
| let mapping = api::Mapping { | ||
| filename: "test.php", | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let mut profile = Profile::new(&sample_types, None); | ||
|
|
||
| let locations = vec![api::Location { | ||
| mapping, | ||
| function: api::Function { | ||
| name: "test_function", | ||
| system_name: "test_function", | ||
| filename: "test.php", | ||
| }, | ||
| line: 0, | ||
| ..Default::default() | ||
| }]; | ||
|
|
||
| let sample = api::Sample { | ||
| locations, | ||
| values: &[1], | ||
| labels: vec![api::Label { | ||
| key: "iteration", | ||
| num: 1, | ||
| ..Default::default() | ||
| }], | ||
| }; | ||
|
|
||
| profile | ||
| .try_add_sample(sample, None) | ||
| .expect("profile to not be full"); | ||
|
|
||
| // Simulate an internally inconsistent profile where observations still reference | ||
| // a label set id, but the label sets table no longer contains that id. | ||
| profile.label_sets.clear(); | ||
|
|
||
| let result = profile.serialize_into_compressed_pprof(None, None); | ||
|
|
||
| let err = match result { | ||
| Ok(_) => panic!( | ||
| "Expected serialization to fail due to invalid label set IDs, but it succeeded" | ||
| ), | ||
| Err(err) => err, | ||
| }; | ||
| let io_err = err | ||
| .downcast_ref::<io::Error>() | ||
| .expect("Expected serialization error to be an io::Error"); | ||
| assert_eq!(io_err.kind(), io::ErrorKind::InvalidData); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the difference between offset and raw id?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In these specific cases, nothing, because
LabelSetIdusesusizeas theRawIdtype and there's no shifting. I thinkto_offsetis more obvious about what we're doing, which is why I changed it.For other types, like string ids, the
to_raw_id()might return ani64, butto_offset()is alwaysusize.