This is an archive of the discontinued LLVM Phabricator instance.

Use a RAII guard to control access to DisassemblerLLVMC.
ClosedPublic

Authored by teemperor on Aug 27 2018, 1:33 PM.

Details

Summary

This patch replaces the manual lock/unlock calls for gaining exclusive access to the disassembler with
a RAII-powered access scope. This should prevent that we somehow skip over these trailing Unlock calls
(e.g. with early returns).

We also have a second GetDisasmToUse method now that takes an already constructed access scope to
prevent deadlocks when we call this from other methods.

Diff Detail

Repository
rL LLVM

Event Timeline

teemperor created this revision.Aug 27 2018, 1:33 PM
vsk added a subscriber: vsk.Aug 27 2018, 1:50 PM
vsk added inline comments.
source/Plugins/Disassembler/llvm/DisassemblerLLVMC.h
81 ↗(On Diff #162736)

This is nice. Do you think it might be even safer to have the guard own the disassembler shared_ptr instance? Users could then access the disassembler via the guard's operator->, and there's less chance of the shared_ptr escaping / being abused. We could even have GetDisassembler() return a guard instance.

85 ↗(On Diff #162736)

It doesn't make sense to me that DisassemblerLLVMC's "Lock" method conflates locking and initialization. If you decide to have a guard own the disassembler instance, it'd make sense to split the initialization step out.

teemperor updated this revision to Diff 162775.Aug 27 2018, 4:33 PM
teemperor retitled this revision from Use a RAII guard to lock/unlock DisassemblerLLVMC [NFC] to Use a RAII guard to control access to DisassemblerLLVMC..
teemperor edited the summary of this revision. (Show Details)
  • Got rid of Lock/Unlock.
  • Replaced guard with a mandatory scope guard that grants exclusive access to the debugger.
  • Added a second GetDisasmToUse that reuses an existing guard object to fix the resulting deadlocks.
davide accepted this revision.Aug 27 2018, 4:38 PM
davide added a subscriber: davide.

LGTM if Vedant is happy with this.

This revision is now accepted and ready to land.Aug 27 2018, 4:38 PM
vsk accepted this revision.Aug 27 2018, 4:55 PM

Looks great, thanks!

source/Plugins/Disassembler/llvm/DisassemblerLLVMC.cpp
176 ↗(On Diff #162775)

Were all callers into Decode locking the disasm instance prior to this patch? If not, this is a sweet fix.

This revision was automatically updated to reflect the committed changes.
teemperor added inline comments.Aug 28 2018, 8:39 AM
source/Plugins/Disassembler/llvm/DisassemblerLLVMC.cpp
176 ↗(On Diff #162775)

Not from what I can see. Otherwise I would have seen deadlocks when adding this lock here.