This is an archive of the discontinued LLVM Phabricator instance.

[UBSan] Fix isDerivedFromAtOffset on iOS ARM64
ClosedPublic

Authored by filcab on Jul 24 2015, 11:57 PM.

Details

Summary

iOS on ARM64 doesn't unique RTTI.
Ref: clang's iOS64CXXABI::shouldRTTIBeUnique()

Due to this, pointer-equality will not necessarily work in this
architecture, across dylib boundaries.

dynamic_cast<>() will still work, since Apple ships with one prepared
for this, but we can't rely on it by simply comparing the type names for
pointer-equality.

I've limited the expensive strcmp check to the specific architecture
which needs it.

Example which triggers this bug:

lib.h:

struct X {
  virtual ~X() {}
};
X *libCall();

lib.mm:

X *libCall() {
  return new X;
}

prog.mm:

int main() {
  X *px = libCall();
  delete px;
}

Expected output: Nothing
Actual output:

<unknown>: runtime error: member call on address 0x00017001ef50 which does not point to an object of type 'X'
0x00017001ef50: note: object is of type 'X'
 00 00 00 00  60 00 0f 00 01 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^~~~~~~~~~~~~~~~~~~~~~~
              vptr for ‘X’

Compilation is fairly straightforward (be sure to pass -isysroot,
-mios-min-version, etc). I ended up looking at what Xcode did to compile
and built a script.

No test since it doesn't seem we support anything related to running on
iOS devices. I couldn't even figure out a nice way to do it from the
command line.

Diff Detail

Repository
rL LLVM

Event Timeline

filcab updated this revision to Diff 30632.Jul 24 2015, 11:57 PM
filcab retitled this revision from to [UBSan] Fix isDerivedFromAtOffset on iOS ARM64.
filcab updated this object.
filcab added reviewers: kubamracek, samsonov, eugenis, rsmith.
filcab added a subscriber: llvm-commits.
filcab updated this object.Jul 24 2015, 11:58 PM

I erred on the side of caution by being verbose in the commit message.
The problem is non-obvious and I want anyone who might think about removing that line to have no excuse (assuming they blame that line and see the commit ;-) ).

This might happen in other platforms too… :-)

filcab updated this revision to Diff 30633.Jul 25 2015, 12:11 AM

Reword the dynamic_cast paragraph (wasn't making sense).

Ping! (Adding Anna Zaks)

samsonov accepted this revision.Sep 1 2015, 10:55 AM
samsonov edited edge metadata.

LGTM. Sorry, for some reason I've lost track of this :-/

This revision is now accepted and ready to land.Sep 1 2015, 10:55 AM
zaks.anna edited edge metadata.Sep 1 2015, 2:20 PM

No test since it doesn't seem we support anything related to running on
iOS devices. I couldn't even figure out a nice way to do it from the
command line.

The test case you provided should be passing on all architectures, correct? If so, why not just add it?
(We do have a way to run tests on iOS devices internally.)

filcab added a subscriber: filcab.Sep 1 2015, 2:24 PM

I can try to add a generic "darwin" test (maybe even darwin+linux, but
adding Windows might be harder, since the platform is so different). This
test would pass both before and after my change, though (in every platform
I can test).
Do your internal test harnesses account for tests which need
executable+dylib (two files) to run?

Filipe

I can try to add a generic "darwin" test (maybe even darwin+linux, ...

I think this would be overall goodness.

Do your internal test harnesses account for tests which need
executable+dylib (two files) to run?

Yes. (Though, we are only testing ASan at the moment.)

I can try to add a generic "darwin" test (maybe even darwin+linux, ...

I think this would be overall goodness.

Do your internal test harnesses account for tests which need
executable+dylib (two files) to run?

Yes. (Though, we are only testing ASan at the moment.)

zaks.anna resigned from this revision.Nov 30 2015, 5:37 PM
zaks.anna removed a reviewer: zaks.anna.
filcab updated this revision to Diff 47094.Feb 6 2016, 12:42 PM
filcab edited edge metadata.

Sorry, I totally forgot about this revision. Refreshing it, with a test.

I'm not sure how well the test will run on all platforms (no idea about Windows), especially on the ones that really need it (iOS ARM64 is impossible to test outside of Apple, AFAICT).

This revision was automatically updated to reflect the committed changes.