This is an archive of the discontinued LLVM Phabricator instance.

[MS] Apply adjustments after storing 'this'
ClosedPublic

Authored by rnk on Nov 15 2017, 4:27 PM.

Details

Summary

The MS ABI convention is that the 'this' pointer on entry is the address
of the vfptr that was used to make the virtual method call. In other
words, the pointer on entry always points to the base subobject that
introduced the virtual method. Consider this hierarchy:

struct A { virtual void f() = 0; };
struct B { virtual void g() = 0; };
struct C : A, B {
  void f() override;
  void g() override;
};

On entry to C::g, [ER]CX will contain the address of C's B subobject,
and C::g will have to subtract sizeof(A) to recover a pointer to C.

Before this change, we applied this adjustment in the prologue and
stored the new value into the "this" local variable alloca used for
debug info. However, MSVC does not do this, presumably because it is
often profitable to fold the adjustment into later field accesses. This
creates a problem, because the debugger expects the variable to be
unadjusted. Unfortunately, CodeView doesn't have anything like DWARF
expressions for computing variables that aren't in the program anymore,
so we have to declare 'this' to be the unadjusted value if we want the
debugger to see the right value.

This has the side benefit that, in optimized builds, the 'this' pointer
will usually be available on function entry because it doesn't require
any adjustment.

Diff Detail

Repository
rL LLVM

Event Timeline

rnk created this revision.Nov 15 2017, 4:27 PM
hans accepted this revision.Nov 15 2017, 5:17 PM

lgtm

This revision is now accepted and ready to land.Nov 15 2017, 5:17 PM
rnk added a comment.Nov 15 2017, 5:47 PM

This seems to cause a crash on startup in some gtest binaries when I self-host, so I guess I should debug that tomorrow before committing. The rest of clang's tests pass. I guess we don't use virtual inheritance. =S

rnk added a comment.Nov 16 2017, 10:55 AM
In D40109#926975, @rnk wrote:

This seems to cause a crash on startup in some gtest binaries when I self-host, so I guess I should debug that tomorrow before committing. The rest of clang's tests pass. I guess we don't use virtual inheritance. =S

The fix was small: don't adjust 'this' in vtable thunks. The thunk will ultimately delegate to the implementation which does the adjustment.

This revision was automatically updated to reflect the committed changes.