mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-23 11:13:28 +01:00
e3f12b0e68
The bitset metadata currently used in LLVM has a few problems: 1. It has the wrong name. The name "bitset" refers to an implementation detail of one use of the metadata (i.e. its original use case, CFI). This makes it harder to understand, as the name makes no sense in the context of virtual call optimization. 2. It is represented using a global named metadata node, rather than being directly associated with a global. This makes it harder to manipulate the metadata when rebuilding global variables, summarise it as part of ThinLTO and drop unused metadata when associated globals are dropped. For this reason, CFI does not currently work correctly when both CFI and vcall opt are enabled, as vcall opt needs to rebuild vtable globals, and fails to associate metadata with the rebuilt globals. As I understand it, the same problem could also affect ASan, which rebuilds globals with a red zone. This patch solves both of those problems in the following way: 1. Rename the metadata to "type metadata". This new name reflects how the metadata is currently being used (i.e. to represent type information for CFI and vtable opt). The new name is reflected in the name for the associated intrinsic (llvm.type.test) and pass (LowerTypeTests). 2. Attach metadata directly to the globals that it pertains to, rather than using the "llvm.bitsets" global metadata node as we are doing now. This is done using the newly introduced capability to attach metadata to global variables (r271348 and r271358). See also: http://lists.llvm.org/pipermail/llvm-dev/2016-June/100462.html Differential Revision: http://reviews.llvm.org/D21053 llvm-svn: 273729
166 lines
5.8 KiB
C++
166 lines
5.8 KiB
C++
//===- WholeProgramDevirt.cpp - Unit tests for whole-program devirt -------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
using namespace llvm;
|
|
using namespace wholeprogramdevirt;
|
|
|
|
TEST(WholeProgramDevirt, findLowestOffset) {
|
|
VTableBits VT1;
|
|
VT1.ObjectSize = 8;
|
|
VT1.Before.BytesUsed = {1 << 0};
|
|
VT1.After.BytesUsed = {1 << 1};
|
|
|
|
VTableBits VT2;
|
|
VT2.ObjectSize = 8;
|
|
VT2.Before.BytesUsed = {1 << 1};
|
|
VT2.After.BytesUsed = {1 << 0};
|
|
|
|
TypeMemberInfo TM1{&VT1, 0};
|
|
TypeMemberInfo TM2{&VT2, 0};
|
|
VirtualCallTarget Targets[] = {
|
|
{&TM1, /*IsBigEndian=*/false},
|
|
{&TM2, /*IsBigEndian=*/false},
|
|
};
|
|
|
|
EXPECT_EQ(2ull, findLowestOffset(Targets, /*IsAfter=*/false, 1));
|
|
EXPECT_EQ(66ull, findLowestOffset(Targets, /*IsAfter=*/true, 1));
|
|
|
|
EXPECT_EQ(8ull, findLowestOffset(Targets, /*IsAfter=*/false, 8));
|
|
EXPECT_EQ(72ull, findLowestOffset(Targets, /*IsAfter=*/true, 8));
|
|
|
|
TM1.Offset = 4;
|
|
EXPECT_EQ(33ull, findLowestOffset(Targets, /*IsAfter=*/false, 1));
|
|
EXPECT_EQ(65ull, findLowestOffset(Targets, /*IsAfter=*/true, 1));
|
|
|
|
EXPECT_EQ(40ull, findLowestOffset(Targets, /*IsAfter=*/false, 8));
|
|
EXPECT_EQ(72ull, findLowestOffset(Targets, /*IsAfter=*/true, 8));
|
|
|
|
TM1.Offset = 8;
|
|
TM2.Offset = 8;
|
|
EXPECT_EQ(66ull, findLowestOffset(Targets, /*IsAfter=*/false, 1));
|
|
EXPECT_EQ(2ull, findLowestOffset(Targets, /*IsAfter=*/true, 1));
|
|
|
|
EXPECT_EQ(72ull, findLowestOffset(Targets, /*IsAfter=*/false, 8));
|
|
EXPECT_EQ(8ull, findLowestOffset(Targets, /*IsAfter=*/true, 8));
|
|
|
|
VT1.After.BytesUsed = {0xff, 0, 0, 0, 0xff};
|
|
VT2.After.BytesUsed = {0xff, 1, 0, 0, 0};
|
|
EXPECT_EQ(16ull, findLowestOffset(Targets, /*IsAfter=*/true, 16));
|
|
EXPECT_EQ(40ull, findLowestOffset(Targets, /*IsAfter=*/true, 32));
|
|
}
|
|
|
|
TEST(WholeProgramDevirt, setReturnValues) {
|
|
VTableBits VT1;
|
|
VT1.ObjectSize = 8;
|
|
|
|
VTableBits VT2;
|
|
VT2.ObjectSize = 8;
|
|
|
|
TypeMemberInfo TM1{&VT1, 0};
|
|
TypeMemberInfo TM2{&VT2, 0};
|
|
VirtualCallTarget Targets[] = {
|
|
{&TM1, /*IsBigEndian=*/false},
|
|
{&TM2, /*IsBigEndian=*/false},
|
|
};
|
|
|
|
TM1.Offset = 4;
|
|
TM2.Offset = 4;
|
|
|
|
int64_t OffsetByte;
|
|
uint64_t OffsetBit;
|
|
|
|
Targets[0].RetVal = 1;
|
|
Targets[1].RetVal = 0;
|
|
setBeforeReturnValues(Targets, 32, 1, OffsetByte, OffsetBit);
|
|
EXPECT_EQ(-5ll, OffsetByte);
|
|
EXPECT_EQ(0ull, OffsetBit);
|
|
EXPECT_EQ(std::vector<uint8_t>{1}, VT1.Before.Bytes);
|
|
EXPECT_EQ(std::vector<uint8_t>{1}, VT1.Before.BytesUsed);
|
|
EXPECT_EQ(std::vector<uint8_t>{0}, VT2.Before.Bytes);
|
|
EXPECT_EQ(std::vector<uint8_t>{1}, VT2.Before.BytesUsed);
|
|
|
|
Targets[0].RetVal = 0;
|
|
Targets[1].RetVal = 1;
|
|
setBeforeReturnValues(Targets, 39, 1, OffsetByte, OffsetBit);
|
|
EXPECT_EQ(-5ll, OffsetByte);
|
|
EXPECT_EQ(7ull, OffsetBit);
|
|
EXPECT_EQ(std::vector<uint8_t>{1}, VT1.Before.Bytes);
|
|
EXPECT_EQ(std::vector<uint8_t>{0x81}, VT1.Before.BytesUsed);
|
|
EXPECT_EQ(std::vector<uint8_t>{0x80}, VT2.Before.Bytes);
|
|
EXPECT_EQ(std::vector<uint8_t>{0x81}, VT2.Before.BytesUsed);
|
|
|
|
Targets[0].RetVal = 12;
|
|
Targets[1].RetVal = 34;
|
|
setBeforeReturnValues(Targets, 40, 8, OffsetByte, OffsetBit);
|
|
EXPECT_EQ(-6ll, OffsetByte);
|
|
EXPECT_EQ(0ull, OffsetBit);
|
|
EXPECT_EQ((std::vector<uint8_t>{1, 12}), VT1.Before.Bytes);
|
|
EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff}), VT1.Before.BytesUsed);
|
|
EXPECT_EQ((std::vector<uint8_t>{0x80, 34}), VT2.Before.Bytes);
|
|
EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff}), VT2.Before.BytesUsed);
|
|
|
|
Targets[0].RetVal = 56;
|
|
Targets[1].RetVal = 78;
|
|
setBeforeReturnValues(Targets, 48, 16, OffsetByte, OffsetBit);
|
|
EXPECT_EQ(-8ll, OffsetByte);
|
|
EXPECT_EQ(0ull, OffsetBit);
|
|
EXPECT_EQ((std::vector<uint8_t>{1, 12, 0, 56}), VT1.Before.Bytes);
|
|
EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff, 0xff, 0xff}),
|
|
VT1.Before.BytesUsed);
|
|
EXPECT_EQ((std::vector<uint8_t>{0x80, 34, 0, 78}), VT2.Before.Bytes);
|
|
EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff, 0xff, 0xff}),
|
|
VT2.Before.BytesUsed);
|
|
|
|
Targets[0].RetVal = 1;
|
|
Targets[1].RetVal = 0;
|
|
setAfterReturnValues(Targets, 32, 1, OffsetByte, OffsetBit);
|
|
EXPECT_EQ(4ll, OffsetByte);
|
|
EXPECT_EQ(0ull, OffsetBit);
|
|
EXPECT_EQ(std::vector<uint8_t>{1}, VT1.After.Bytes);
|
|
EXPECT_EQ(std::vector<uint8_t>{1}, VT1.After.BytesUsed);
|
|
EXPECT_EQ(std::vector<uint8_t>{0}, VT2.After.Bytes);
|
|
EXPECT_EQ(std::vector<uint8_t>{1}, VT2.After.BytesUsed);
|
|
|
|
Targets[0].RetVal = 0;
|
|
Targets[1].RetVal = 1;
|
|
setAfterReturnValues(Targets, 39, 1, OffsetByte, OffsetBit);
|
|
EXPECT_EQ(4ll, OffsetByte);
|
|
EXPECT_EQ(7ull, OffsetBit);
|
|
EXPECT_EQ(std::vector<uint8_t>{1}, VT1.After.Bytes);
|
|
EXPECT_EQ(std::vector<uint8_t>{0x81}, VT1.After.BytesUsed);
|
|
EXPECT_EQ(std::vector<uint8_t>{0x80}, VT2.After.Bytes);
|
|
EXPECT_EQ(std::vector<uint8_t>{0x81}, VT2.After.BytesUsed);
|
|
|
|
Targets[0].RetVal = 12;
|
|
Targets[1].RetVal = 34;
|
|
setAfterReturnValues(Targets, 40, 8, OffsetByte, OffsetBit);
|
|
EXPECT_EQ(5ll, OffsetByte);
|
|
EXPECT_EQ(0ull, OffsetBit);
|
|
EXPECT_EQ((std::vector<uint8_t>{1, 12}), VT1.After.Bytes);
|
|
EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff}), VT1.After.BytesUsed);
|
|
EXPECT_EQ((std::vector<uint8_t>{0x80, 34}), VT2.After.Bytes);
|
|
EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff}), VT2.After.BytesUsed);
|
|
|
|
Targets[0].RetVal = 56;
|
|
Targets[1].RetVal = 78;
|
|
setAfterReturnValues(Targets, 48, 16, OffsetByte, OffsetBit);
|
|
EXPECT_EQ(6ll, OffsetByte);
|
|
EXPECT_EQ(0ull, OffsetBit);
|
|
EXPECT_EQ((std::vector<uint8_t>{1, 12, 56, 0}), VT1.After.Bytes);
|
|
EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff, 0xff, 0xff}),
|
|
VT1.After.BytesUsed);
|
|
EXPECT_EQ((std::vector<uint8_t>{0x80, 34, 78, 0}), VT2.After.Bytes);
|
|
EXPECT_EQ((std::vector<uint8_t>{0x81, 0xff, 0xff, 0xff}),
|
|
VT2.After.BytesUsed);
|
|
}
|