This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho]Rework error-checking in peeking at first-member in archive to avoid segfault.
ClosedPublic

Authored by oontvoo on Aug 7 2023, 10:44 AM.

Details

Summary

Details:
calling getMemoryBufferRef() on an empty archive can sometimes trigger segfault so the code should check before calling this.
this seems like a bug in the Archive API but that can be fixed separately.

P.S: follow up to D156468

Diff Detail

Event Timeline

oontvoo created this revision.Aug 7 2023, 10:44 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptAug 7 2023, 10:44 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
oontvoo requested review of this revision.Aug 7 2023, 10:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 7 2023, 10:44 AM

It seems maskray might be OOO , could someone else give this a quick look please? :) @int3 @keith ? TIA!

calling getMemoryBufferRef() on an empty archive can sometimes trigger segfault so the code should check before calling this. this seems like a bug in the Archive API but that can be fixed separately.

Is there a test case? When is "sometimes"?

calling getMemoryBufferRef() on an empty archive can sometimes trigger segfault so the code should check before calling this. this seems like a bug in the Archive API but that can be fixed separately.

Is there a test case? When is "sometimes"?

reducing the test-case further, I think it's safe to change "sometimes" => "100% of the time".
Test case is simply linking with at least one empty archive in the input

(empty archive produced by llvm-ar --format=darwin rcs libempty.a
This will produce an empty archive but the ::isEmpty() method on the API doesn't return true, so getBufferRef() would crash.)

oontvoo updated this revision to Diff 548273.Aug 8 2023, 10:20 AM

added trivial test case

smeenai accepted this revision.Aug 8 2023, 11:10 AM
smeenai added a subscriber: smeenai.

LGTM with comments.

lld/MachO/InputFiles.cpp
2117–2133

Typo: BUffer -> Buffer

I think this would be cleaner as an early return, instead of nesting everything else inside it, since zero symbols should mean the loop below is a no-op as well.

This revision is now accepted and ready to land.Aug 8 2023, 11:10 AM
gulfem added a subscriber: gulfem.Aug 8 2023, 11:14 AM
MaskRay accepted this revision.Aug 8 2023, 5:02 PM
oontvoo updated this revision to Diff 548592.Aug 9 2023, 6:09 AM
oontvoo marked an inline comment as done.

addressed review comment

lld/MachO/InputFiles.cpp
2117–2133

Thanks!
Fixed typo and moved the numberOfSymbols check to beginning.
(Still need the nested if,unfortunately, because we dont want to access the child if there was error )

This revision was landed with ongoing or failed builds.Aug 9 2023, 6:54 AM
This revision was automatically updated to reflect the committed changes.