This is an archive of the discontinued LLVM Phabricator instance.

[libunwind] [SEH] Initialize _msContext with RtlCaptureContext
ClosedPublic

Authored by mstorsjo on Apr 5 2023, 9:59 AM.

Details

Summary

When we initialize the UnwindCursor (unw_cursor_t) based on
an existing Registers object (unw_context_t), we only initialize
a subset of the class.

Fill the struct properly for the current thread with RtlCaptureContext,
followed by overwriting of the subset of registers that we do have
available in the Registers class.

One might think that it's enough to initialize specifically the
registers that we signal availability for with ContextFlags,
however in practice, that's not enough.

This fixes crashes when restoring the context via RtlRestoreContext
(via UnwindCursor::jumpto), via __unw_resume.

Diff Detail

Event Timeline

mstorsjo created this revision.Apr 5 2023, 9:59 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 5 2023, 9:59 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
mstorsjo requested review of this revision.Apr 5 2023, 9:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 5 2023, 9:59 AM
cdavis5x accepted this revision.Apr 6 2023, 1:33 PM

I had hoped to avoid that duplicated work here. Clearly there was something I missed...

I had hoped to avoid that duplicated work here. Clearly there was something I missed...

Yep - I didn't quite pinpoint what goes wrong and where, but it's reproducible both on Wine and on native Windows. A (untested) mini repro of the issue might look something like this:

CONTEXT ctx;
RtlCaptureContext(&ctx);
static int Restored = 0;
if (!Restored) {
  Restored = 1;
  ctx.ContextFlags &= CONTEXT_CONTROL|CONTEXT_INTEGER|CONTEXT_FLOATING_POINT;
  ctx.SegCs = 0;
  RtlRestoreContext(&ctx);
}

One could think it's ok to clobber SegCs since ContextFlags doesn't have CONTEXT_SEGMENTS set - but it looks like it does matter nevertheless.

Can someone give a libunwind group approval for this one? It's got an approval from @cdavis5x who originally authored the SEH codepaths here.

MaskRay accepted this revision.Apr 12 2023, 11:27 PM
MaskRay added a subscriber: MaskRay.

Rubber stamp :)

This revision is now accepted and ready to land.Apr 12 2023, 11:27 PM
This revision was landed with ongoing or failed builds.Apr 13 2023, 2:06 AM
This revision was automatically updated to reflect the committed changes.