1/*
2 * Copyright (C) 2017 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#include <gtest/gtest.h>
18
19#include "linker_patch.h"
20
21namespace art {
22namespace linker {
23
24TEST(LinkerPatch, LinkerPatchOperators) {
25  const DexFile* dex_file1 = reinterpret_cast<const DexFile*>(1);
26  const DexFile* dex_file2 = reinterpret_cast<const DexFile*>(2);
27  LinkerPatch patches[] = {
28      LinkerPatch::RelativeMethodPatch(16u, dex_file1, 3000u, 1000u),
29      LinkerPatch::RelativeMethodPatch(16u, dex_file1, 3001u, 1000u),
30      LinkerPatch::RelativeMethodPatch(16u, dex_file1, 3000u, 1001u),
31      LinkerPatch::RelativeMethodPatch(16u, dex_file1, 3001u, 1001u),  // Index 3.
32      LinkerPatch::RelativeMethodPatch(16u, dex_file2, 3000u, 1000u),
33      LinkerPatch::RelativeMethodPatch(16u, dex_file2, 3001u, 1000u),
34      LinkerPatch::RelativeMethodPatch(16u, dex_file2, 3000u, 1001u),
35      LinkerPatch::RelativeMethodPatch(16u, dex_file2, 3001u, 1001u),
36      LinkerPatch::MethodBssEntryPatch(16u, dex_file1, 3000u, 1000u),
37      LinkerPatch::MethodBssEntryPatch(16u, dex_file1, 3001u, 1000u),
38      LinkerPatch::MethodBssEntryPatch(16u, dex_file1, 3000u, 1001u),
39      LinkerPatch::MethodBssEntryPatch(16u, dex_file1, 3001u, 1001u),
40      LinkerPatch::MethodBssEntryPatch(16u, dex_file2, 3000u, 1000u),
41      LinkerPatch::MethodBssEntryPatch(16u, dex_file2, 3001u, 1000u),
42      LinkerPatch::MethodBssEntryPatch(16u, dex_file2, 3000u, 1001u),
43      LinkerPatch::MethodBssEntryPatch(16u, dex_file2, 3001u, 1001u),
44      LinkerPatch::CodePatch(16u, dex_file1, 1000u),
45      LinkerPatch::CodePatch(16u, dex_file1, 1001u),
46      LinkerPatch::CodePatch(16u, dex_file2, 1000u),
47      LinkerPatch::CodePatch(16u, dex_file2, 1001u),
48      LinkerPatch::RelativeCodePatch(16u, dex_file1, 1000u),
49      LinkerPatch::RelativeCodePatch(16u, dex_file1, 1001u),
50      LinkerPatch::RelativeCodePatch(16u, dex_file2, 1000u),
51      LinkerPatch::RelativeCodePatch(16u, dex_file2, 1001u),
52      LinkerPatch::RelativeTypePatch(16u, dex_file1, 3000u, 1000u),
53      LinkerPatch::RelativeTypePatch(16u, dex_file1, 3001u, 1000u),
54      LinkerPatch::RelativeTypePatch(16u, dex_file1, 3000u, 1001u),
55      LinkerPatch::RelativeTypePatch(16u, dex_file1, 3001u, 1001u),
56      LinkerPatch::RelativeTypePatch(16u, dex_file2, 3000u, 1000u),
57      LinkerPatch::RelativeTypePatch(16u, dex_file2, 3001u, 1000u),
58      LinkerPatch::RelativeTypePatch(16u, dex_file2, 3000u, 1001u),
59      LinkerPatch::RelativeTypePatch(16u, dex_file2, 3001u, 1001u),
60      LinkerPatch::TypeBssEntryPatch(16u, dex_file1, 3000u, 1000u),
61      LinkerPatch::TypeBssEntryPatch(16u, dex_file1, 3001u, 1000u),
62      LinkerPatch::TypeBssEntryPatch(16u, dex_file1, 3000u, 1001u),
63      LinkerPatch::TypeBssEntryPatch(16u, dex_file1, 3001u, 1001u),
64      LinkerPatch::TypeBssEntryPatch(16u, dex_file2, 3000u, 1000u),
65      LinkerPatch::TypeBssEntryPatch(16u, dex_file2, 3001u, 1000u),
66      LinkerPatch::TypeBssEntryPatch(16u, dex_file2, 3000u, 1001u),
67      LinkerPatch::TypeBssEntryPatch(16u, dex_file2, 3001u, 1001u),
68      LinkerPatch::RelativeStringPatch(16u, dex_file1, 3000u, 1000u),
69      LinkerPatch::RelativeStringPatch(16u, dex_file1, 3001u, 1000u),
70      LinkerPatch::RelativeStringPatch(16u, dex_file1, 3000u, 1001u),
71      LinkerPatch::RelativeStringPatch(16u, dex_file1, 3001u, 1001u),
72      LinkerPatch::RelativeStringPatch(16u, dex_file2, 3000u, 1000u),
73      LinkerPatch::RelativeStringPatch(16u, dex_file2, 3001u, 1000u),
74      LinkerPatch::RelativeStringPatch(16u, dex_file2, 3000u, 1001u),
75      LinkerPatch::RelativeStringPatch(16u, dex_file2, 3001u, 1001u),
76      LinkerPatch::StringBssEntryPatch(16u, dex_file1, 3000u, 1000u),
77      LinkerPatch::StringBssEntryPatch(16u, dex_file1, 3001u, 1000u),
78      LinkerPatch::StringBssEntryPatch(16u, dex_file1, 3000u, 1001u),
79      LinkerPatch::StringBssEntryPatch(16u, dex_file1, 3001u, 1001u),
80      LinkerPatch::StringBssEntryPatch(16u, dex_file2, 3000u, 1000u),
81      LinkerPatch::StringBssEntryPatch(16u, dex_file2, 3001u, 1000u),
82      LinkerPatch::StringBssEntryPatch(16u, dex_file2, 3000u, 1001u),
83      LinkerPatch::StringBssEntryPatch(16u, dex_file2, 3001u, 1001u),
84      LinkerPatch::BakerReadBarrierBranchPatch(16u, 0u, 0u),
85      LinkerPatch::BakerReadBarrierBranchPatch(16u, 0u, 1u),
86      LinkerPatch::BakerReadBarrierBranchPatch(16u, 1u, 0u),
87      LinkerPatch::BakerReadBarrierBranchPatch(16u, 1u, 1u),
88
89      LinkerPatch::RelativeMethodPatch(32u, dex_file1, 3000u, 1000u),
90      LinkerPatch::RelativeMethodPatch(32u, dex_file1, 3001u, 1000u),
91      LinkerPatch::RelativeMethodPatch(32u, dex_file1, 3000u, 1001u),
92      LinkerPatch::RelativeMethodPatch(32u, dex_file1, 3001u, 1001u),
93      LinkerPatch::RelativeMethodPatch(32u, dex_file2, 3000u, 1000u),
94      LinkerPatch::RelativeMethodPatch(32u, dex_file2, 3001u, 1000u),
95      LinkerPatch::RelativeMethodPatch(32u, dex_file2, 3000u, 1001u),
96      LinkerPatch::RelativeMethodPatch(32u, dex_file2, 3001u, 1001u),
97      LinkerPatch::MethodBssEntryPatch(32u, dex_file1, 3000u, 1000u),
98      LinkerPatch::MethodBssEntryPatch(32u, dex_file1, 3001u, 1000u),
99      LinkerPatch::MethodBssEntryPatch(32u, dex_file1, 3000u, 1001u),
100      LinkerPatch::MethodBssEntryPatch(32u, dex_file1, 3001u, 1001u),
101      LinkerPatch::MethodBssEntryPatch(32u, dex_file2, 3000u, 1000u),
102      LinkerPatch::MethodBssEntryPatch(32u, dex_file2, 3001u, 1000u),
103      LinkerPatch::MethodBssEntryPatch(32u, dex_file2, 3000u, 1001u),
104      LinkerPatch::MethodBssEntryPatch(32u, dex_file2, 3001u, 1001u),
105      LinkerPatch::CodePatch(32u, dex_file1, 1000u),
106      LinkerPatch::CodePatch(32u, dex_file1, 1001u),
107      LinkerPatch::CodePatch(32u, dex_file2, 1000u),
108      LinkerPatch::CodePatch(32u, dex_file2, 1001u),
109      LinkerPatch::RelativeCodePatch(32u, dex_file1, 1000u),
110      LinkerPatch::RelativeCodePatch(32u, dex_file1, 1001u),
111      LinkerPatch::RelativeCodePatch(32u, dex_file2, 1000u),
112      LinkerPatch::RelativeCodePatch(32u, dex_file2, 1001u),
113      LinkerPatch::RelativeTypePatch(32u, dex_file1, 3000u, 1000u),
114      LinkerPatch::RelativeTypePatch(32u, dex_file1, 3001u, 1000u),
115      LinkerPatch::RelativeTypePatch(32u, dex_file1, 3000u, 1001u),
116      LinkerPatch::RelativeTypePatch(32u, dex_file1, 3001u, 1001u),
117      LinkerPatch::RelativeTypePatch(32u, dex_file2, 3000u, 1000u),
118      LinkerPatch::RelativeTypePatch(32u, dex_file2, 3001u, 1000u),
119      LinkerPatch::RelativeTypePatch(32u, dex_file2, 3000u, 1001u),
120      LinkerPatch::RelativeTypePatch(32u, dex_file2, 3001u, 1001u),
121      LinkerPatch::TypeBssEntryPatch(32u, dex_file1, 3000u, 1000u),
122      LinkerPatch::TypeBssEntryPatch(32u, dex_file1, 3001u, 1000u),
123      LinkerPatch::TypeBssEntryPatch(32u, dex_file1, 3000u, 1001u),
124      LinkerPatch::TypeBssEntryPatch(32u, dex_file1, 3001u, 1001u),
125      LinkerPatch::TypeBssEntryPatch(32u, dex_file2, 3000u, 1000u),
126      LinkerPatch::TypeBssEntryPatch(32u, dex_file2, 3001u, 1000u),
127      LinkerPatch::TypeBssEntryPatch(32u, dex_file2, 3000u, 1001u),
128      LinkerPatch::TypeBssEntryPatch(32u, dex_file2, 3001u, 1001u),
129      LinkerPatch::RelativeStringPatch(32u, dex_file1, 3000u, 1000u),
130      LinkerPatch::RelativeStringPatch(32u, dex_file1, 3001u, 1000u),
131      LinkerPatch::RelativeStringPatch(32u, dex_file1, 3000u, 1001u),
132      LinkerPatch::RelativeStringPatch(32u, dex_file1, 3001u, 1001u),
133      LinkerPatch::RelativeStringPatch(32u, dex_file2, 3000u, 1000u),
134      LinkerPatch::RelativeStringPatch(32u, dex_file2, 3001u, 1000u),
135      LinkerPatch::RelativeStringPatch(32u, dex_file2, 3000u, 1001u),
136      LinkerPatch::RelativeStringPatch(32u, dex_file2, 3001u, 1001u),
137      LinkerPatch::StringBssEntryPatch(32u, dex_file1, 3000u, 1000u),
138      LinkerPatch::StringBssEntryPatch(32u, dex_file1, 3001u, 1000u),
139      LinkerPatch::StringBssEntryPatch(32u, dex_file1, 3000u, 1001u),
140      LinkerPatch::StringBssEntryPatch(32u, dex_file1, 3001u, 1001u),
141      LinkerPatch::StringBssEntryPatch(32u, dex_file2, 3000u, 1000u),
142      LinkerPatch::StringBssEntryPatch(32u, dex_file2, 3001u, 1000u),
143      LinkerPatch::StringBssEntryPatch(32u, dex_file2, 3000u, 1001u),
144      LinkerPatch::StringBssEntryPatch(32u, dex_file2, 3001u, 1001u),
145      LinkerPatch::BakerReadBarrierBranchPatch(32u, 0u, 0u),
146      LinkerPatch::BakerReadBarrierBranchPatch(32u, 0u, 1u),
147      LinkerPatch::BakerReadBarrierBranchPatch(32u, 1u, 0u),
148      LinkerPatch::BakerReadBarrierBranchPatch(32u, 1u, 1u),
149
150      LinkerPatch::RelativeMethodPatch(16u, dex_file1, 3001u, 1001u),  // Same as patch at index 3.
151  };
152  constexpr size_t last_index = arraysize(patches) - 1u;
153
154  for (size_t i = 0; i != arraysize(patches); ++i) {
155    for (size_t j = 0; j != arraysize(patches); ++j) {
156      bool expected = (i != last_index ? i : 3u) == (j != last_index ? j : 3u);
157      EXPECT_EQ(expected, patches[i] == patches[j]) << i << " " << j;
158    }
159  }
160
161  for (size_t i = 0; i != arraysize(patches); ++i) {
162    for (size_t j = 0; j != arraysize(patches); ++j) {
163      bool expected = (i != last_index ? i : 3u) < (j != last_index ? j : 3u);
164      EXPECT_EQ(expected, patches[i] < patches[j]) << i << " " << j;
165    }
166  }
167}
168
169}  // namespace linker
170}  // namespace art
171