blob: 2dcb61650871e8cc6b5a436f5aac534b0b5311cf [file] [log] [blame]
Peter Collingbournea4ccff32015-02-20 20:30:56 +00001===========================================
2Control Flow Integrity Design Documentation
3===========================================
4
5This page documents the design of the :doc:`ControlFlowIntegrity` schemes
6supported by Clang.
7
8Forward-Edge CFI for Virtual Calls
9----------------------------------
10
11This scheme works by allocating, for each static type used to make a virtual
12call, a region of read-only storage in the object file holding a bit vector
13that maps onto to the region of storage used for those virtual tables. Each
14set bit in the bit vector corresponds to the `address point`_ for a virtual
15table compatible with the static type for which the bit vector is being built.
16
17For example, consider the following three C++ classes:
18
19.. code-block:: c++
20
21 struct A {
22 virtual void f();
23 };
24
25 struct B : A {
26 virtual void f();
27 };
28
29 struct C : A {
30 virtual void f();
31 };
32
33The scheme will cause the virtual tables for A, B and C to be laid out
34consecutively:
35
36.. csv-table:: Virtual Table Layout for A, B, C
37 :header: 0, 1, 2, 3, 4, 5, 6, 7, 8
38
39 A::offset-to-top, &A::rtti, &A::f, B::offset-to-top, &B::rtti, &B::f, C::offset-to-top, &C::rtti, &C::f
40
41The bit vector for static types A, B and C will look like this:
42
43.. csv-table:: Bit Vectors for A, B, C
44 :header: Class, 0, 1, 2, 3, 4, 5, 6, 7, 8
45
46 A, 0, 0, 1, 0, 0, 1, 0, 0, 1
47 B, 0, 0, 0, 0, 0, 1, 0, 0, 0
48 C, 0, 0, 0, 0, 0, 0, 0, 0, 1
49
50To emit a virtual call, the compiler will assemble code that checks that
51the object's virtual table pointer is in-bounds and aligned and that the
52relevant bit is set in the bit vector.
53
54The compiler relies on co-operation from the linker in order to assemble
55the bit vector for the whole program. It currently does this using LLVM's
56`bit sets`_ mechanism together with link-time optimization.
57
58.. _address point: https://mentorembedded.github.io/cxx-abi/abi.html#vtable-general
59.. _bit sets: http://llvm.org/docs/BitSets.html