This is an archive of the discontinued LLVM Phabricator instance.

[Object] fixed invalid symbol handling in ELFObjectFile::getSymbolName
ClosedPublic

Authored by JestrTulip on Jul 6 2023, 4:04 PM.

Details

Summary

Found a bug in ElfObjectFile.h that occurred when there was an invalid Symbol Name in an object file. This error affected the behavior of the Expected<> value and leading it to abort, rather than behave as normal. I found this as I was adding tests to llvm-cm, as prompted by @jhenderson.

Without this fix, upon encountering an invalid symbol and trying to get its name, the program states that

Expected<T> must be checked before access or destruction

and aborts.

Diff Detail

Event Timeline

JestrTulip created this revision.Jul 6 2023, 4:04 PM
Herald added a reviewer: MaskRay. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
Herald added a subscriber: emaste. · View Herald Transcript
JestrTulip requested review of this revision.Jul 6 2023, 4:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 6 2023, 4:04 PM
JestrTulip edited the summary of this revision. (Show Details)Jul 6 2023, 4:08 PM
mtrofin added a subscriber: mtrofin.Jul 6 2023, 7:20 PM

you probably want to change the commit title to something more specific - e.g. "fix invalid section handling in ELFObjectFile::getSymbolName" and capture in the description what happens - i.e. without the fix, the test in this patch segfaults (iirc)

llvm/test/tools/llvm-objdump/ELF/section-no-symbol-name.test
19 ↗(On Diff #537918)

newline

MaskRay added inline comments.Jul 6 2023, 8:19 PM
llvm/test/tools/llvm-objdump/ELF/section-no-symbol-name.test
4 ↗(On Diff #537918)

If there is a warning or error, we usually include warning: or error:. See other tests.

17 ↗(On Diff #537918)

Add a comment ## Invalid section index

JestrTulip retitled this revision from caught bug in ElfObjectFile.h to fixed invalid symbol handling in ELFObjectFile::getSymbolName.Jul 6 2023, 11:58 PM
JestrTulip edited the summary of this revision. (Show Details)
JestrTulip edited the summary of this revision. (Show Details)
MaskRay added inline comments.Jul 7 2023, 10:55 AM
llvm/test/tools/llvm-objdump/ELF/section-no-symbol-name.test
2 ↗(On Diff #538010)

I think you want to add a llvm-objdump test.

This directory is not for the proposed llvm-cm.

JestrTulip added inline comments.Jul 7 2023, 1:07 PM
llvm/test/tools/llvm-objdump/ELF/section-no-symbol-name.test
2 ↗(On Diff #538010)

fixed!

To me, the filename section-no-symbol-name.test is not accurate as almost all STT_SECTION symbols don't have names.
Tools like llvm-nm/llvm-readelf use heuristics to give the section name (see D57105).

I am mainly thinking of https://maskray.me/blog/2021-08-08-toolchain-testing "I don't know an existing test can be enhanced".
I usually search for the diagnostics (invalid section index) to find related tests. In this case, it seems that we do want a llvm-objdump -d test as llvm-objdump -t reports an error instead of warning, stopping further processing.
With more investigation, I think we probably should port a llvm-readobj behavior. See D154754.

With D154754, I think you can enhance a test by adding -d beside --syms.

removed redundant test

updated section-symbols.test

MaskRay accepted this revision.Jul 11 2023, 4:26 PM

I made a suggestion to the test. The title probably should use a tag like [Object]

llvm/test/tools/llvm-objdump/ELF/section-symbols.test
14

This check separates CHECK for another RUN line.
I suggest that we move llvm-objdump and # CHECK-DISAS after all CHECK.

## Test that we consume an error in ELFObjectFile<ELFT>::getSymbolName.
# RUN: llvm-objdump -d ...

# CHECK-DISAS: ...
This revision is now accepted and ready to land.Jul 11 2023, 4:26 PM
JestrTulip retitled this revision from fixed invalid symbol handling in ELFObjectFile::getSymbolName to [Object] fixed invalid symbol handling in ELFObjectFile::getSymbolName.Jul 11 2023, 4:34 PM
This revision was landed with ongoing or failed builds.Jul 11 2023, 5:07 PM
This revision was automatically updated to reflect the committed changes.
chapuni added inline comments.
llvm/test/tools/llvm-objdump/ELF/section-symbols.test
23

Disassembling doesn't work if x86 is not configured.

michaelplatings added inline comments.
llvm/test/tools/llvm-objdump/ELF/section-symbols.test
23

Yes, this test is failing in my build with -DLLVM_TARGETS_TO_BUILD="AArch64;ARM"

luporl added a subscriber: luporl.Jul 12 2023, 7:03 AM
mtrofin added inline comments.Jul 12 2023, 8:31 AM
llvm/test/tools/llvm-objdump/ELF/section-symbols.test
23

@JestrTulip @MaskRay judging from line 32, the test should require x86 target? Or -r is expected to run anyway, and it's just -d that can't, and thus should be split off in its own X86/section-symbols-disassemble.test maybe (to give it a name)?