Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _LINKER_CFI_H_ |
| 18 | #define _LINKER_CFI_H_ |
| 19 | |
| 20 | #include "linker.h" |
| 21 | #include "linker_debug.h" |
| 22 | |
| 23 | #include <algorithm> |
| 24 | |
| 25 | #include "private/CFIShadow.h" |
| 26 | |
| 27 | // This class keeps the contents of CFI shadow up-to-date with the current set of loaded libraries. |
| 28 | // See the comment in CFIShadow.h for more context. |
| 29 | // See documentation in http://clang.llvm.org/docs/ControlFlowIntegrityDesign.html#shared-library-support. |
| 30 | // |
| 31 | // Shadow is mapped and initialized lazily as soon as the first CFI-enabled DSO is loaded. |
| 32 | // It is updated after a set of libraries is loaded (but before any constructors are ran), and |
| 33 | // before any library is unloaded. |
| 34 | class CFIShadowWriter : private CFIShadow { |
| 35 | // Returns pointer to the shadow element for an address. |
| 36 | uint16_t* MemToShadow(uintptr_t x) { |
| 37 | return reinterpret_cast<uint16_t*>(*shadow_start + MemToShadowOffset(x)); |
| 38 | } |
| 39 | |
| 40 | // Update shadow for the address range to the given constant value. |
| 41 | void AddConstant(uintptr_t begin, uintptr_t end, uint16_t v); |
| 42 | |
| 43 | // Update shadow for the address range to kUncheckedShadow. |
| 44 | void AddUnchecked(uintptr_t begin, uintptr_t end); |
| 45 | |
| 46 | // Update shadow for the address range to kInvalidShadow. |
| 47 | void AddInvalid(uintptr_t begin, uintptr_t end); |
| 48 | |
| 49 | // Update shadow for the address range to the given __cfi_check value. |
| 50 | void Add(uintptr_t begin, uintptr_t end, uintptr_t cfi_check); |
| 51 | |
| 52 | // Add a DSO to CFI shadow. |
| 53 | bool AddLibrary(soinfo* si); |
| 54 | |
| 55 | // Map CFI shadow. |
| 56 | uintptr_t MapShadow(); |
| 57 | |
| 58 | // Initialize CFI shadow and update its contents for everything in solist if any loaded library is |
| 59 | // CFI-enabled. If soinfos != nullptr, do an incremental check by looking only at the libraries in |
| 60 | // soinfos[]; otherwise look at the entire solist. |
| 61 | // |
| 62 | // Returns false if the shadow is already initialized. It is the caller's responsibility to update |
| 63 | // the shadow for the new libraries in that case. |
| 64 | // Otherwise, returns true and leaves the shadow either up-to-date or uninitialized. |
| 65 | bool MaybeInit(soinfo *new_si, soinfo *solist); |
| 66 | |
| 67 | // Set a human readable name for the entire shadow region. |
| 68 | void FixupVmaName(); |
| 69 | |
| 70 | bool NotifyLibDl(soinfo *solist, uintptr_t p); |
| 71 | |
| 72 | // Pointer to the shadow start address. |
| 73 | uintptr_t *shadow_start; |
| 74 | |
| 75 | bool initial_link_done; |
| 76 | |
| 77 | public: |
| 78 | // Update shadow after loading a set of DSOs. |
| 79 | // This function will initialize the shadow if it sees a CFI-enabled DSO for the first time. |
| 80 | // In that case it will retroactively update shadow for all previously loaded DSOs. "solist" is a |
| 81 | // pointer to the global list. |
| 82 | // This function must be called before any user code has observed the newly loaded DSO. |
| 83 | bool AfterLoad(soinfo* si, soinfo *solist); |
| 84 | |
| 85 | // Update shadow before unloading a DSO. |
| 86 | void BeforeUnload(soinfo* si); |
| 87 | |
| 88 | bool InitialLinkDone(soinfo *solist); |
| 89 | |
| 90 | // Handle failure to locate __cfi_check for a target address. |
| 91 | static void CfiFail(uint64_t CallSiteTypeId, void* Ptr, void* DiagData, void *caller_pc); |
| 92 | }; |
| 93 | |
| 94 | CFIShadowWriter* get_cfi_shadow(); |
| 95 | |
| 96 | #endif // _LINKER_CFI_H_ |