|
| 1 | +/* |
| 2 | + * Copyright © 2017-2018 The Charles Stark Draper Laboratory, Inc. and/or Dover Microsystems, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Use and disclosure subject to the following license. |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 8 | + * a copy of this software and associated documentation files (the |
| 9 | + * "Software"), to deal in the Software without restriction, including |
| 10 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 11 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | + * permit persons to whom the Software is furnished to do so, subject to |
| 13 | + * the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be |
| 16 | + * included in all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 22 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 23 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 24 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#ifndef METADATA_H |
| 28 | +#define METADATA_H |
| 29 | + |
| 30 | +#define ISP_METADATA_ELF_SECTION_NAME ".dover_metadata" |
| 31 | + |
| 32 | +/* New metadata format in images: |
| 33 | +
|
| 34 | +The goal with this format is to establish a smaller encoding, and a somewhat more general purpose |
| 35 | +descriptive encoding. Ultimately, some of these fields will be uleb128 encoded, as soon as we |
| 36 | +can figure out some tool bugs. For now, some of the uleb128 destined fields are fixed width. |
| 37 | +We note these fields in the descriptions here for future notice. |
| 38 | +
|
| 39 | +Metadata operations in the image now consist of a stream, with some operations being dependent |
| 40 | +on data from previous operations. This is similar to DWARF debug information encoding. Code |
| 41 | +which processes the metadata should process the stream of operations in order, changing |
| 42 | +internal state as it goes. |
| 43 | +
|
| 44 | +The first operation encountered in any metadata stream sets the current base address off which |
| 45 | +subsequent operations should be based. |
| 46 | +
|
| 47 | +DMD_SET_BASE_ADDRESS_OP (uleb128, but currently byte) |
| 48 | +<address> (uintptr_t) |
| 49 | +
|
| 50 | +Following the SET_BASE_ADDRESS operation, will be a sequence of zero or more other operations. |
| 51 | +The SET_BASE_ADDRESS operation can appear multiple times. It just resets the current base |
| 52 | +address value for interpretation of operations that follow it in the stream. |
| 53 | +
|
| 54 | +DMD_TAG_ADDRESS_OP (uleb128, but currently byte) |
| 55 | +<relative address> (uleb128, but currently 32bits) |
| 56 | +<tag specifier> (uleb128, but currently byte) |
| 57 | +
|
| 58 | +The TAG_ADDRESS operation causes a tag to be applied to a specific address. The address to be |
| 59 | +tagged is formed by adding the <relative address> field to the current base address. The |
| 60 | +<tag specifier> field names the tag to be applied to the resulting address. The specifier |
| 61 | +is a known, stable constant specific to supported policies. The constant value is invariant |
| 62 | +across minor revisions and builds of the operating kernel and policy code, to allow compiled |
| 63 | +binaries to have some longevity. Tagging code will use this constant to look up the appropriate |
| 64 | +runtime tag handle or value to apply to the address. |
| 65 | +
|
| 66 | +DMD_TAG_ADDRESS_RANGE (uleb128, but currently byte) |
| 67 | +<relative start address> (uleb128, but currently 32bits) |
| 68 | +<relative end address> (uleb128, but currently 32bits) |
| 69 | +<tag specifier> (uleb128, but currently byte) |
| 70 | +
|
| 71 | +The TAG_ADDRESS_RANGE operations causes a tag to be applied to a range of addresses. |
| 72 | +The start and end address ranges are formed by taking their respective relative address |
| 73 | +fields and adding them to the current base address. The <tag specifier> field names |
| 74 | +the tag to be applied to the resulting address, as per the TAG_ADDRESS operation. |
| 75 | +
|
| 76 | +DMD_TAG_POLICY_SYMBOL (uleb128, but currently byte) |
| 77 | +<symbol name> (asciiz) |
| 78 | +<tag type> (uleb128, but currently 32bits) |
| 79 | +
|
| 80 | +DMD_TAG_POLICY_SYMBOL operations cause a symbol whose length can be determined from |
| 81 | +a symbol table (e.g. ELF symbol table) to be tagged. The <tag type> field is generated |
| 82 | +by the policy tool, and is not the same as a tag specifier. Tag types are not stable, |
| 83 | +and can and will change from build to build of policies. The symbol name is a null |
| 84 | +terminated name. These records are generated by the policy tool, exclusively. |
| 85 | +
|
| 86 | +DMD_TAG_POLICY_RANGE (uleb128, but currently byte) |
| 87 | +<start address> (uintptr_t) |
| 88 | +<end address> (uintptr_t) |
| 89 | +<tag type> (uleb128, but currently 32bits) |
| 90 | +
|
| 91 | +DMD_TAG_POLICY_RANGE operations cause an address range to be tagged. The <tag type> |
| 92 | +field is generated by the policy tool, and is not the same as a tag specifier. |
| 93 | +Tag types are not stable, and can and will change from build to build of policies. |
| 94 | +These records are generated by the policy tool, exclusively. |
| 95 | +
|
| 96 | +DMD_TAG_POLICY_SYMBOL_RANKED (uleb128, but currently byte) |
| 97 | +<symbol name> (asciiz) |
| 98 | +<tag category> (uleb128, but currently 32bits) |
| 99 | +<rank> (uleb128, but currently 32bits) |
| 100 | +<tag type> (uleb128, but currently 32bits) |
| 101 | +
|
| 102 | +DMD_TAG_POLICY_SYMBOL_RANKED operations cause a symbol whose length can be determined from |
| 103 | +a symbol table (e.g. ELF symbol table) to be tagged. The <tag type> field is generated |
| 104 | +by the policy tool, and is not the same as a tag specifier. Tag types are not stable, |
| 105 | +and can and will change from build to build of policies. The symbol name is a null |
| 106 | +terminated name. These records are generated by the policy tool, exclusively. |
| 107 | +The tag category and rank fields are used to determine overrides of tags. A tag in |
| 108 | +the same category as another with a higher rank will supercede a lower ranked tag |
| 109 | +on any given address. |
| 110 | +
|
| 111 | +DMD_TAG_POLICY_RANGE_RANKED (uleb128, but currently byte) |
| 112 | +<start address> (uintptr_t) |
| 113 | +<end address> (uintptr_t) |
| 114 | +<tag category> (uleb128, but currently 32bits) |
| 115 | +<rank> (uleb128, but currently 32bits) |
| 116 | +<tag type> (uleb128, but currently 32bits) |
| 117 | +
|
| 118 | +DMD_TAG_POLICY_RANGE_RANKED operations cause an address range to be tagged. The <tag type> |
| 119 | +field is generated by the policy tool, and is not the same as a tag specifier. |
| 120 | +Tag types are not stable, and can and will change from build to build of policies. |
| 121 | +These records are generated by the policy tool, exclusively. |
| 122 | +The tag category and rank fields are used to determine overrides of tags. A tag in |
| 123 | +the same category as another with a higher rank will supercede a lower ranked tag |
| 124 | +on any given address. |
| 125 | + */ |
| 126 | + |
| 127 | +/* |
| 128 | + Meta data operations: |
| 129 | + */ |
| 130 | +#define DMD_SET_BASE_ADDRESS_OP 1u |
| 131 | +#define DMD_TAG_ADDRESS_OP 2u |
| 132 | +#define DMD_TAG_ADDRESS_RANGE_OP 3u |
| 133 | +#define DMD_TAG_POLICY_SYMBOL 4u /* deprecated? */ |
| 134 | +#define DMD_TAG_POLICY_RANGE 5u /* deprecated? */ |
| 135 | +#define DMD_TAG_POLICY_SYMBOL_RANKED 6u |
| 136 | +#define DMD_TAG_POLICY_RANGE_RANKED 7u |
| 137 | +#define DMD_END_BLOCK 8u |
| 138 | +#define DMD_END_BLOCK_WEAK_DECL_HACK 9u |
| 139 | +#define DMD_FUNCTION_RANGE 10u /*Followed by 32bit start and 32bit end addresses*/ |
| 140 | + |
| 141 | +/* |
| 142 | + Tag specifiers... also serve as offset in MCSymbol ISPMetadata flags struct. |
| 143 | + */ |
| 144 | +#define DMT_CFI3L_VALID_TGT 1u |
| 145 | +#define DMT_STACK_PROLOGUE_AUTHORITY 2u |
| 146 | +#define DMT_STACK_EPILOGUE_AUTHORITY 3u |
| 147 | +#define DMT_FPTR_STORE_AUTHORITY 4u |
| 148 | +#define DMT_BRANCH_VALID_TGT 5u |
| 149 | +#define DMT_RET_VALID_TGT 6u |
| 150 | +#define DMT_RETURN_INSTR 7u |
| 151 | +#define DMT_CALL_INSTR 8u |
| 152 | +#define DMT_BRANCH_INSTR 9u |
| 153 | +#define DMT_FPTR_CREATE_AUTHORITY 10u |
| 154 | +#define DMT_WRITE_ONCE 11u |
| 155 | + |
| 156 | +//#define WriteOnceFlagVal ((0x1) << (MachineInstr::MaxFlagShift + 1)) |
| 157 | + |
| 158 | +#include <map> |
| 159 | +#include "llvm/CodeGen/MachineInstr.h" |
| 160 | + |
| 161 | +using namespace std; |
| 162 | +using namespace llvm; |
| 163 | + |
| 164 | +const std::map<int, int> MachineInstFlagToMetadata = { |
| 165 | + {MachineInstr::FnProlog, DMT_STACK_PROLOGUE_AUTHORITY}, |
| 166 | + {MachineInstr::FnEpilog, DMT_STACK_EPILOGUE_AUTHORITY}, |
| 167 | + {MachineInstr::FPtrStore, DMT_FPTR_STORE_AUTHORITY}, |
| 168 | + {MachineInstr::FPtrCreate, DMT_FPTR_CREATE_AUTHORITY}, |
| 169 | + {MachineInstr::IsReturn, DMT_RETURN_INSTR}, |
| 170 | + {MachineInstr::IsCall, DMT_CALL_INSTR}, |
| 171 | + {MachineInstr::IsBranch, DMT_BRANCH_INSTR}, |
| 172 | + {MachineInstr::CallTarget, DMT_CFI3L_VALID_TGT}, |
| 173 | + {MachineInstr::ReturnTarget, DMT_RET_VALID_TGT}, |
| 174 | + {MachineInstr::BranchTarget, DMT_BRANCH_VALID_TGT} |
| 175 | +}; |
| 176 | + |
| 177 | +#endif |
0 commit comments