blob: 430359d8c50c963b433f0e506fdf8cf78c30f452 [file] [log] [blame]
Stephen Hines86277eb2015-03-23 12:06:32 -07001#ifndef UTILS_H
2#define UTILS_H
3
4inline void break_optimization(void *arg) {
5 __asm__ __volatile__("" : : "r" (arg) : "memory");
6}
7
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08008// Tests will instantiate this class to pad out bit sets to test out the
9// various ways we can represent the bit set (32-bit inline, 64-bit inline,
10// memory). Instantiating this class will trigger the instantiation of I
11// templates with I virtual tables for classes deriving from T, I-2 of which
12// will be of size sizeof(void*) * 5, 1 of which will be of size sizeof(void*)
13// * 3, and 1 of which will be of size sizeof(void*) * 9. (Under the MS ABI
14// each virtual table will be sizeof(void*) bytes smaller). Each category
15// of virtual tables is aligned to a different power of 2, precluding the
16// all-ones optimization. As a result, the bit vector for the base class will
17// need to contain at least I*2 entries to accommodate all the derived virtual
18// tables.
Stephen Hines86277eb2015-03-23 12:06:32 -070019template <typename T, unsigned I>
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080020struct Deriver : T {
21 Deriver() {
22 break_optimization(new Deriver<T, I-1>);
23 }
Stephen Hines86277eb2015-03-23 12:06:32 -070024 virtual void f() {}
25 virtual void g() {}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080026 virtual void h() {}
Stephen Hines86277eb2015-03-23 12:06:32 -070027};
28
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080029template <typename T>
30struct Deriver<T, 0> : T {
31 virtual void f() {}
32 void g() {}
33};
34
35template <typename T>
36struct Deriver<T, 1> : T {
37 Deriver() {
38 break_optimization(new Deriver<T, 0>);
39 }
40 virtual void f() {}
41 virtual void g() {}
42 virtual void h() {}
43 virtual void i() {}
44 virtual void j() {}
45 virtual void k() {}
46 virtual void l() {}
47};
48
49// Instantiate enough classes to force CFI checks for type T to use bit
50// vectors of size 32 (if B32 defined), 64 (if B64 defined) or >64 (if BM
51// defined).
52template <typename T>
53void create_derivers() {
54#ifdef B32
55 break_optimization(new Deriver<T, 10>);
56#endif
57
58#ifdef B64
59 break_optimization(new Deriver<T, 25>);
60#endif
61
62#ifdef BM
63 break_optimization(new Deriver<T, 40>);
64#endif
65}
66
Stephen Hines86277eb2015-03-23 12:06:32 -070067#endif