blob: 0e05f37144cd82de38604f7b3d518f12e0d343e9 [file] [log] [blame]
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001/*
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.
Evgenii Stepanov636a2ec2017-01-20 13:47:04 -080032// It is updated after any library is loaded (but before any constructors are ran), and
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070033// before any library is unloaded.
34class 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
Evgenii Stepanov636a2ec2017-01-20 13:47:04 -080059 // CFI-enabled. If new_si != nullptr, do an incremental check by looking only at new_si; otherwise
60 // look at the entire solist.
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070061 bool MaybeInit(soinfo *new_si, soinfo *solist);
62
63 // Set a human readable name for the entire shadow region.
64 void FixupVmaName();
65
Evgenii Stepanov636a2ec2017-01-20 13:47:04 -080066 // Pass the pointer to the mapped shadow region to libdl. Must only be called once.
67 // Flips shadow_start to a non-nullptr value.
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070068 bool NotifyLibDl(soinfo *solist, uintptr_t p);
69
70 // Pointer to the shadow start address.
71 uintptr_t *shadow_start;
72
73 bool initial_link_done;
74
75 public:
Evgenii Stepanov636a2ec2017-01-20 13:47:04 -080076 // Update shadow after loading a DSO.
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070077 // This function will initialize the shadow if it sees a CFI-enabled DSO for the first time.
78 // In that case it will retroactively update shadow for all previously loaded DSOs. "solist" is a
79 // pointer to the global list.
80 // This function must be called before any user code has observed the newly loaded DSO.
81 bool AfterLoad(soinfo* si, soinfo *solist);
82
83 // Update shadow before unloading a DSO.
84 void BeforeUnload(soinfo* si);
85
Evgenii Stepanov636a2ec2017-01-20 13:47:04 -080086 // This is called as soon as the initial set of libraries is linked.
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070087 bool InitialLinkDone(soinfo *solist);
88
89 // Handle failure to locate __cfi_check for a target address.
90 static void CfiFail(uint64_t CallSiteTypeId, void* Ptr, void* DiagData, void *caller_pc);
91};
92
93CFIShadowWriter* get_cfi_shadow();
94
95#endif // _LINKER_CFI_H_