This is an archive of the discontinued LLVM Phabricator instance.

Avoid dangling reference when indexing sections
ClosedPublic

Authored by serge-sans-paille on Feb 25 2020, 4:10 AM.

Details

Summary

Bug spotted by https://cookieplmonster.github.io/2020/02/01/emulator-bug-llvm-bug/

The blog post proposes a simple solution (using a std::deque instead of a smallvector to store section entries). They also propose an alternative, namely to avoid taking any section reference at all. This patch enforces the latter using a proxy class. I'm totally fine with the dequeue approach too, and will gladly update the patch if needed.

Diff Detail

Event Timeline

Drive by code review, no comment on the bug or proposed solution.

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
83 ↗(On Diff #246411)

How come SectionEntryProxy is sometimes passed by reference, but other times passed by value to different functions in this CL?

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h
105 ↗(On Diff #246411)

Every time I see Sections[] in RuntimeDyldELF.cpp, I think "This should be using getSection()."

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h
134 ↗(On Diff #246411)

If this is the same between MachO and ELF (and I suspect all other derived classes), can it be a method on a shared base class?

llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldELFMips.cpp
33 ↗(On Diff #246411)

Why not a &? Won't this result in a copy-assignment?

Thanks very much for this Serge!

Is the std::deque fix a one-liner? This shouldn't be very performance sensitive, so I'd be inclined to go with that.

Thanks very much for this Serge!

Is the std::deque fix a one-liner? This shouldn't be very performance sensitive, so I'd be inclined to go with that.

It is! Compared to this patch, which was a pleasure to write nonetheless, I'd go for the <dequeue> approach ;-)

Use the <deque> approach

Fix include order

@lhames is that okay like this?

lhames accepted this revision.Mar 4 2020, 10:17 AM

Looks good to me. Thanks Serge!

This revision is now accepted and ready to land.Mar 4 2020, 10:17 AM
grimar accepted this revision.Mar 4 2020, 11:54 PM

Nasty bug. LGTM too.

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
257

LLVM code usually uses "std::" prefix before STL containers names and algorithms.
(See std::map below and other variables).

@grimar ok, change added to the commit.

This revision was automatically updated to reflect the committed changes.