Page MenuHomePhabricator

[Windows EH] Fix the order of Nested try-catches in $tryMap$ table
ClosedPublic

Authored by tentzen on May 6 2020, 12:53 AM.

Details

Summary

This bug is exposed by Test7 of ehthrow.cxx in MSVC EH suite where
a rethrow occurs in a try-catch inside a catch (i.e., a nested Catch
handlers). See the test code in
https://github.com/microsoft/compiler-tests/blob/master/eh/ehthrow.cxx#L346

When an object is rethrown in a Catch handler, the copy-ctor of this
object must be executed after the destructions of live objects, but
BEFORE the dtors of live objects in parent handlers.

Today Windows runtime (__CxxFrameHandler3 & 4) expects nested Catch handers
are stored in pre-order (outer first, inner next) in $tryMap$ table, so
that given a State, its Catch's beginning State can be properly
retrieved. The Catch beginning state (which is also the ending State) is
the State where rethrown object's copy-ctor must take place.

LLVM currently stores nested catch handlers in post-ordering because
it's the natural way to compute the highest State in Catch.
The fix is to simply store TryCatch handler in pre-order, but update
Catch's highest State after child Catches are all processed.

Diff Detail

Event Timeline

tentzen created this revision.May 6 2020, 12:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 6 2020, 12:53 AM
tentzen updated this revision to Diff 262309.May 6 2020, 1:07 AM

fix a typo in test case

Hi, Reid,
the existing test case is fixed. I think there is no need to add one more unit test for this patch.
thanks,

majnemer added inline comments.
llvm/lib/CodeGen/WinEHPrepare.cpp
269

CatchHIgh -> CatchHigh

tentzen updated this revision to Diff 262478.May 6 2020, 2:33 PM

fixed a comment.

thank you David, good eyes!
Is there any more concern?

This revision is now accepted and ready to land.May 13 2020, 12:55 PM
tentzen updated this revision to Diff 263919.May 13 2020, 11:04 PM

Per discussion in https://reviews.llvm.org/D79760 , x86 test case there failed.

It turned out that Windows 32-bit runtime expects post-ordering when looking for catch-handler, but sharing the code with 64-bit runtime when looking up EH State during DTor cleaning-up.
As a result, MSVC/X86 today fails on Test7 in ehthrow.cxx.

Incorrect Ctor/Dtor ordering is less significant than Catch handling. for now LLVM must be compatible with MSVC.
This change also apply to the other two targets on Windows; Arm32 & Arm64.

This revision was automatically updated to reflect the committed changes.