TypeMetadata.rst 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. =============
  2. Type Metadata
  3. =============
  4. Type metadata is a mechanism that allows IR modules to co-operatively build
  5. pointer sets corresponding to addresses within a given set of globals. LLVM's
  6. `control flow integrity`_ implementation uses this metadata to efficiently
  7. check (at each call site) that a given address corresponds to either a
  8. valid vtable or function pointer for a given class or function type, and its
  9. whole-program devirtualization pass uses the metadata to identify potential
  10. callees for a given virtual call.
  11. To use the mechanism, a client creates metadata nodes with two elements:
  12. 1. a byte offset into the global (generally zero for functions)
  13. 2. a metadata object representing an identifier for the type
  14. These metadata nodes are associated with globals by using global object
  15. metadata attachments with the ``!type`` metadata kind.
  16. Each type identifier must exclusively identify either global variables
  17. or functions.
  18. .. admonition:: Limitation
  19. The current implementation only supports attaching metadata to functions on
  20. the x86-32 and x86-64 architectures.
  21. An intrinsic, :ref:`llvm.type.test <type.test>`, is used to test whether a
  22. given pointer is associated with a type identifier.
  23. .. _control flow integrity: http://clang.llvm.org/docs/ControlFlowIntegrity.html
  24. Representing Type Information using Type Metadata
  25. =================================================
  26. This section describes how Clang represents C++ type information associated with
  27. virtual tables using type metadata.
  28. Consider the following inheritance hierarchy:
  29. .. code-block:: c++
  30. struct A {
  31. virtual void f();
  32. };
  33. struct B : A {
  34. virtual void f();
  35. virtual void g();
  36. };
  37. struct C {
  38. virtual void h();
  39. };
  40. struct D : A, C {
  41. virtual void f();
  42. virtual void h();
  43. };
  44. The virtual table objects for A, B, C and D look like this (under the Itanium ABI):
  45. .. csv-table:: Virtual Table Layout for A, B, C, D
  46. :header: Class, 0, 1, 2, 3, 4, 5, 6
  47. A, A::offset-to-top, &A::rtti, &A::f
  48. B, B::offset-to-top, &B::rtti, &B::f, &B::g
  49. C, C::offset-to-top, &C::rtti, &C::h
  50. D, D::offset-to-top, &D::rtti, &D::f, &D::h, D::offset-to-top, &D::rtti, thunk for &D::h
  51. When an object of type A is constructed, the address of ``&A::f`` in A's
  52. virtual table object is stored in the object's vtable pointer. In ABI parlance
  53. this address is known as an `address point`_. Similarly, when an object of type
  54. B is constructed, the address of ``&B::f`` is stored in the vtable pointer. In
  55. this way, the vtable in B's virtual table object is compatible with A's vtable.
  56. D is a little more complicated, due to the use of multiple inheritance. Its
  57. virtual table object contains two vtables, one compatible with A's vtable and
  58. the other compatible with C's vtable. Objects of type D contain two virtual
  59. pointers, one belonging to the A subobject and containing the address of
  60. the vtable compatible with A's vtable, and the other belonging to the C
  61. subobject and containing the address of the vtable compatible with C's vtable.
  62. The full set of compatibility information for the above class hierarchy is
  63. shown below. The following table shows the name of a class, the offset of an
  64. address point within that class's vtable and the name of one of the classes
  65. with which that address point is compatible.
  66. .. csv-table:: Type Offsets for A, B, C, D
  67. :header: VTable for, Offset, Compatible Class
  68. A, 16, A
  69. B, 16, A
  70. , , B
  71. C, 16, C
  72. D, 16, A
  73. , , D
  74. , 48, C
  75. The next step is to encode this compatibility information into the IR. The way
  76. this is done is to create type metadata named after each of the compatible
  77. classes, with which we associate each of the compatible address points in
  78. each vtable. For example, these type metadata entries encode the compatibility
  79. information for the above hierarchy:
  80. ::
  81. @_ZTV1A = constant [...], !type !0
  82. @_ZTV1B = constant [...], !type !0, !type !1
  83. @_ZTV1C = constant [...], !type !2
  84. @_ZTV1D = constant [...], !type !0, !type !3, !type !4
  85. !0 = !{i64 16, !"_ZTS1A"}
  86. !1 = !{i64 16, !"_ZTS1B"}
  87. !2 = !{i64 16, !"_ZTS1C"}
  88. !3 = !{i64 16, !"_ZTS1D"}
  89. !4 = !{i64 48, !"_ZTS1C"}
  90. With this type metadata, we can now use the ``llvm.type.test`` intrinsic to
  91. test whether a given pointer is compatible with a type identifier. Working
  92. backwards, if ``llvm.type.test`` returns true for a particular pointer,
  93. we can also statically determine the identities of the virtual functions
  94. that a particular virtual call may call. For example, if a program assumes
  95. a pointer to be a member of ``!"_ZST1A"``, we know that the address can
  96. be only be one of ``_ZTV1A+16``, ``_ZTV1B+16`` or ``_ZTV1D+16`` (i.e. the
  97. address points of the vtables of A, B and D respectively). If we then load
  98. an address from that pointer, we know that the address can only be one of
  99. ``&A::f``, ``&B::f`` or ``&D::f``.
  100. .. _address point: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-general
  101. Testing Addresses For Type Membership
  102. =====================================
  103. If a program tests an address using ``llvm.type.test``, this will cause
  104. a link-time optimization pass, ``LowerTypeTests``, to replace calls to this
  105. intrinsic with efficient code to perform type member tests. At a high level,
  106. the pass will lay out referenced globals in a consecutive memory region in
  107. the object file, construct bit vectors that map onto that memory region,
  108. and generate code at each of the ``llvm.type.test`` call sites to test
  109. pointers against those bit vectors. Because of the layout manipulation, the
  110. globals' definitions must be available at LTO time. For more information,
  111. see the `control flow integrity design document`_.
  112. A type identifier that identifies functions is transformed into a jump table,
  113. which is a block of code consisting of one branch instruction for each
  114. of the functions associated with the type identifier that branches to the
  115. target function. The pass will redirect any taken function addresses to the
  116. corresponding jump table entry. In the object file's symbol table, the jump
  117. table entries take the identities of the original functions, so that addresses
  118. taken outside the module will pass any verification done inside the module.
  119. Jump tables may call external functions, so their definitions need not
  120. be available at LTO time. Note that if an externally defined function is
  121. associated with a type identifier, there is no guarantee that its identity
  122. within the module will be the same as its identity outside of the module,
  123. as the former will be the jump table entry if a jump table is necessary.
  124. The `GlobalLayoutBuilder`_ class is responsible for laying out the globals
  125. efficiently to minimize the sizes of the underlying bitsets.
  126. .. _control flow integrity design document: http://clang.llvm.org/docs/ControlFlowIntegrityDesign.html
  127. :Example:
  128. ::
  129. target datalayout = "e-p:32:32"
  130. @a = internal global i32 0, !type !0
  131. @b = internal global i32 0, !type !0, !type !1
  132. @c = internal global i32 0, !type !1
  133. @d = internal global [2 x i32] [i32 0, i32 0], !type !2
  134. define void @e() !type !3 {
  135. ret void
  136. }
  137. define void @f() {
  138. ret void
  139. }
  140. declare void @g() !type !3
  141. !0 = !{i32 0, !"typeid1"}
  142. !1 = !{i32 0, !"typeid2"}
  143. !2 = !{i32 4, !"typeid2"}
  144. !3 = !{i32 0, !"typeid3"}
  145. declare i1 @llvm.type.test(i8* %ptr, metadata %typeid) nounwind readnone
  146. define i1 @foo(i32* %p) {
  147. %pi8 = bitcast i32* %p to i8*
  148. %x = call i1 @llvm.type.test(i8* %pi8, metadata !"typeid1")
  149. ret i1 %x
  150. }
  151. define i1 @bar(i32* %p) {
  152. %pi8 = bitcast i32* %p to i8*
  153. %x = call i1 @llvm.type.test(i8* %pi8, metadata !"typeid2")
  154. ret i1 %x
  155. }
  156. define i1 @baz(void ()* %p) {
  157. %pi8 = bitcast void ()* %p to i8*
  158. %x = call i1 @llvm.type.test(i8* %pi8, metadata !"typeid3")
  159. ret i1 %x
  160. }
  161. define void @main() {
  162. %a1 = call i1 @foo(i32* @a) ; returns 1
  163. %b1 = call i1 @foo(i32* @b) ; returns 1
  164. %c1 = call i1 @foo(i32* @c) ; returns 0
  165. %a2 = call i1 @bar(i32* @a) ; returns 0
  166. %b2 = call i1 @bar(i32* @b) ; returns 1
  167. %c2 = call i1 @bar(i32* @c) ; returns 1
  168. %d02 = call i1 @bar(i32* getelementptr ([2 x i32]* @d, i32 0, i32 0)) ; returns 0
  169. %d12 = call i1 @bar(i32* getelementptr ([2 x i32]* @d, i32 0, i32 1)) ; returns 1
  170. %e = call i1 @baz(void ()* @e) ; returns 1
  171. %f = call i1 @baz(void ()* @f) ; returns 0
  172. %g = call i1 @baz(void ()* @g) ; returns 1
  173. ret void
  174. }
  175. .. _GlobalLayoutBuilder: https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h