-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[yaml2obj] Don't use uninitialized Type #123274
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
[yaml2obj] Don't use uninitialized Type #123274
Conversation
Created using spr 1.3.4 [skip ci]
Created using spr 1.3.4
@llvm/pr-subscribers-llvm-support @llvm/pr-subscribers-objectyaml Author: Vitaly Buka (vitalybuka) ChangesWith -DMACHINE=EM_NONE, machine specific Full diff: https://github.com/llvm/llvm-project/pull/123274.diff 1 Files Affected:
diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index 83e6cf76dd746f..961815392a1348 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1588,7 +1588,7 @@ static bool isInteger(StringRef Val) {
void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
IO &IO, std::unique_ptr<ELFYAML::Chunk> &Section) {
- ELFYAML::ELF_SHT Type = ELF::ET_NONE;
+ ELFYAML::ELF_SHT Type;
StringRef TypeStr;
if (IO.outputting()) {
if (auto *S = dyn_cast<ELFYAML::Section>(Section.get()))
@@ -1599,9 +1599,12 @@ void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
// When the Type string does not have a "SHT_" prefix, we know it is not a
// description of a regular ELF output section.
TypeStr = getStringValue(IO, "Type");
- if (TypeStr.starts_with("SHT_") || isInteger(TypeStr))
+ if (TypeStr.starts_with("SHT_") || isInteger(TypeStr)) {
IO.mapRequired("Type", Type);
- }
+ if (static_cast<Input&>(IO).error())
+ Type = ELF::SHT_NULL;
+ }
+ }
if (TypeStr == "Fill") {
assert(!IO.outputting()); // We don't dump fills currently.
|
Created using spr 1.3.4 [skip ci]
✅ With the latest revision this PR passed the C/C++ code formatter. |
@jh7370 I don't this is significantly different #123137 And we can't bailout on error(), as without |
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.
Zero initialize the member variable instead?
Which one? EC? There is no Output::EC, only Input::EC. |
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.
I think we may have been looking at different cases, though I'm not 100% certain. When I tried this out yesterday, I omitted the Type
field from the YAML entirely. As a result, the getStringValue
call on line 1601 resulted in an error (because Type
was missing) and TypeStr
was left as an empty string. Subsequently, Type
was then read without being initialised.
Looking at the code and your description, in the case you've been looking at, I'm guessing you might have a Type
field in the YAML that starts with SHT_
but which doesn't have a valid mapping (based on the test you described, because the machine-specific section type is used with a non-applicable machine). Is that correct?
Your fix only fixes the latter case, which is insufficient. The correct fix takes place outside the if statement at line 1602, in one form or another. If, as you described, it's not possible to simply do return
from this function after seeing the invalid/missing Type
value in the YAML, then we should adopt MaskRay's suggestion of zero-initialising, but of the Type
variable, like you did originally, but with an SHT_NULL
value like you are doing already (but at the declaration point, not here). This might be preferable to checking the error state anyway.
What do you think?
Close, there are SHT_s which are ON/OF deppending on machine type set.
To clarify "zero-initialising the field", are we talking about local variable 'Type', not a field
EC contains some error, not sure what we can do here?
Yes, that essentially what was #123137 , I just copied wrong 0 constant ET_NONE there. |
Yes - see my edit to the original comment (looks like you were looking at the original one, which I fixed as I realised it was unclear).
I'm not following what this comment is saying? We can ignore the error, like we were before, if we have an initialized See also my comment in #123280 - perhaps the fix is to do something with |
Created using spr 1.3.4
Yes, it would not be enough here. |
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.
Couple of thoughts about the test, in addition to the inline comments:
- As we identified when both looking at this, there are two separate cases we may want to consider, namely the case the current test covers (a "Type" field which doesn't start with
SHT_
but also not a recognised other type) and the case you originally identified (aSHT_
type that isn't a known value, e.g.SHT_FOO
). We should probably test both of these. - I don't think you added the case to the right test file. The specific file you added it to was for testing the SectionHeaderTable chunk. It would probably make more sense to add the cases to the
section-type.yaml
file.
Done both. |
Created using spr 1.3.4
Created using spr 1.3.4
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.
LGTM, once remaining nits fixed.
Alternative to #123137
With -DMACHINE=EM_NONE, machine specific
sections, like SHT_ARM_EXIDX, will fall to parse
and set
Type
.This triggers msan on