| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2013 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 <stdlib.h> | 
|  | 18 | #include <string.h> | 
|  | 19 | #include <sys/mman.h> | 
|  | 20 |  | 
|  | 21 | #include <gtest/gtest.h> | 
|  | 22 | #include "buffer_tests.h" | 
|  | 23 |  | 
| Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 24 | // For the comparison buffer tests, the maximum length to test for the | 
|  | 25 | // miscompare checks. | 
|  | 26 | #define MISCMP_MAX_LENGTH 512 | 
|  | 27 |  | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 28 | #define FENCEPOST_LENGTH 8 | 
|  | 29 |  | 
|  | 30 | static int g_single_aligns[][2] = { | 
|  | 31 | // Both buffers at same alignment. | 
|  | 32 | { 1, 0 }, | 
|  | 33 | { 2, 0 }, | 
|  | 34 | { 4, 0 }, | 
|  | 35 | { 8, 0 }, | 
|  | 36 | { 16, 0 }, | 
|  | 37 | { 32, 0 }, | 
|  | 38 | { 64, 0 }, | 
|  | 39 | { 128, 0 }, | 
|  | 40 |  | 
|  | 41 | // General unaligned cases. | 
|  | 42 | { 4, 1 }, | 
|  | 43 | { 4, 2 }, | 
|  | 44 | { 4, 3 }, | 
|  | 45 |  | 
|  | 46 | { 8, 1 }, | 
|  | 47 | { 8, 2 }, | 
|  | 48 | { 8, 3 }, | 
|  | 49 | { 8, 4 }, | 
|  | 50 | { 8, 5 }, | 
|  | 51 | { 8, 6 }, | 
|  | 52 | { 8, 7 }, | 
|  | 53 |  | 
|  | 54 | { 128, 1 }, | 
|  | 55 | { 128, 4 }, | 
|  | 56 | { 128, 8 }, | 
|  | 57 | { 128, 12 }, | 
|  | 58 | { 128, 16 }, | 
|  | 59 | }; | 
|  | 60 |  | 
|  | 61 | static const size_t g_single_aligns_len = sizeof(g_single_aligns)/sizeof(int[2]); | 
|  | 62 |  | 
|  | 63 | // Set of multiple buffer alignment combinations to be used for string/memory | 
|  | 64 | // testing routines. | 
|  | 65 | static int g_double_aligns[][4] = { | 
|  | 66 | // Both buffers at same alignment. | 
|  | 67 | { 1, 0, 1, 0 }, | 
|  | 68 | { 2, 0, 2, 0 }, | 
|  | 69 | { 4, 0, 4, 0 }, | 
|  | 70 | { 8, 0, 8, 0 }, | 
|  | 71 | { 16, 0, 16, 0 }, | 
|  | 72 | { 32, 0, 32, 0 }, | 
|  | 73 | { 64, 0, 64, 0 }, | 
|  | 74 | { 128, 0, 128, 0 }, | 
|  | 75 |  | 
|  | 76 | // Different word alignments between buffers. | 
|  | 77 | { 8, 0, 4, 0 }, | 
|  | 78 | { 4, 0, 8, 0 }, | 
|  | 79 | { 16, 0, 4, 0 }, | 
|  | 80 | { 4, 0, 16, 0 }, | 
|  | 81 |  | 
|  | 82 | // General unaligned cases. | 
|  | 83 | { 4, 0, 4, 1 }, | 
|  | 84 | { 4, 0, 4, 2 }, | 
|  | 85 | { 4, 0, 4, 3 }, | 
|  | 86 |  | 
|  | 87 | { 4, 1, 4, 0 }, | 
|  | 88 | { 4, 1, 4, 1 }, | 
|  | 89 | { 4, 1, 4, 2 }, | 
|  | 90 | { 4, 1, 4, 3 }, | 
|  | 91 |  | 
|  | 92 | { 4, 2, 4, 0 }, | 
|  | 93 | { 4, 2, 4, 1 }, | 
|  | 94 | { 4, 2, 4, 2 }, | 
|  | 95 | { 4, 2, 4, 3 }, | 
|  | 96 |  | 
|  | 97 | { 4, 3, 4, 0 }, | 
|  | 98 | { 4, 3, 4, 1 }, | 
|  | 99 | { 4, 3, 4, 2 }, | 
|  | 100 | { 4, 3, 4, 3 }, | 
|  | 101 |  | 
|  | 102 | { 8, 0, 8, 1 }, | 
|  | 103 | { 8, 0, 8, 2 }, | 
|  | 104 | { 8, 0, 8, 3 }, | 
|  | 105 | { 8, 0, 8, 4 }, | 
|  | 106 | { 8, 0, 8, 5 }, | 
|  | 107 | { 8, 0, 8, 6 }, | 
|  | 108 | { 8, 0, 8, 7 }, | 
|  | 109 |  | 
|  | 110 | { 8, 1, 8, 0 }, | 
|  | 111 | { 8, 1, 8, 1 }, | 
|  | 112 | { 8, 1, 8, 2 }, | 
|  | 113 | { 8, 1, 8, 3 }, | 
|  | 114 | { 8, 1, 8, 4 }, | 
|  | 115 | { 8, 1, 8, 5 }, | 
|  | 116 | { 8, 1, 8, 6 }, | 
|  | 117 | { 8, 1, 8, 7 }, | 
|  | 118 |  | 
|  | 119 | { 8, 2, 8, 0 }, | 
|  | 120 | { 8, 2, 8, 1 }, | 
|  | 121 | { 8, 2, 8, 2 }, | 
|  | 122 | { 8, 2, 8, 3 }, | 
|  | 123 | { 8, 2, 8, 4 }, | 
|  | 124 | { 8, 2, 8, 5 }, | 
|  | 125 | { 8, 2, 8, 6 }, | 
|  | 126 | { 8, 2, 8, 7 }, | 
|  | 127 |  | 
|  | 128 | { 8, 3, 8, 0 }, | 
|  | 129 | { 8, 3, 8, 1 }, | 
|  | 130 | { 8, 3, 8, 2 }, | 
|  | 131 | { 8, 3, 8, 3 }, | 
|  | 132 | { 8, 3, 8, 4 }, | 
|  | 133 | { 8, 3, 8, 5 }, | 
|  | 134 | { 8, 3, 8, 6 }, | 
|  | 135 | { 8, 3, 8, 7 }, | 
|  | 136 |  | 
|  | 137 | { 8, 4, 8, 0 }, | 
|  | 138 | { 8, 4, 8, 1 }, | 
|  | 139 | { 8, 4, 8, 2 }, | 
|  | 140 | { 8, 4, 8, 3 }, | 
|  | 141 | { 8, 4, 8, 4 }, | 
|  | 142 | { 8, 4, 8, 5 }, | 
|  | 143 | { 8, 4, 8, 6 }, | 
|  | 144 | { 8, 4, 8, 7 }, | 
|  | 145 |  | 
|  | 146 | { 8, 5, 8, 0 }, | 
|  | 147 | { 8, 5, 8, 1 }, | 
|  | 148 | { 8, 5, 8, 2 }, | 
|  | 149 | { 8, 5, 8, 3 }, | 
|  | 150 | { 8, 5, 8, 4 }, | 
|  | 151 | { 8, 5, 8, 5 }, | 
|  | 152 | { 8, 5, 8, 6 }, | 
|  | 153 | { 8, 5, 8, 7 }, | 
|  | 154 |  | 
|  | 155 | { 8, 6, 8, 0 }, | 
|  | 156 | { 8, 6, 8, 1 }, | 
|  | 157 | { 8, 6, 8, 2 }, | 
|  | 158 | { 8, 6, 8, 3 }, | 
|  | 159 | { 8, 6, 8, 4 }, | 
|  | 160 | { 8, 6, 8, 5 }, | 
|  | 161 | { 8, 6, 8, 6 }, | 
|  | 162 | { 8, 6, 8, 7 }, | 
|  | 163 |  | 
|  | 164 | { 8, 7, 8, 0 }, | 
|  | 165 | { 8, 7, 8, 1 }, | 
|  | 166 | { 8, 7, 8, 2 }, | 
|  | 167 | { 8, 7, 8, 3 }, | 
|  | 168 | { 8, 7, 8, 4 }, | 
|  | 169 | { 8, 7, 8, 5 }, | 
|  | 170 | { 8, 7, 8, 6 }, | 
|  | 171 | { 8, 7, 8, 7 }, | 
|  | 172 |  | 
|  | 173 | { 128, 1, 128, 4 }, | 
|  | 174 | { 128, 1, 128, 8 }, | 
|  | 175 | { 128, 1, 128, 12 }, | 
|  | 176 | { 128, 1, 128, 16 }, | 
|  | 177 | { 128, 4, 128, 1 }, | 
|  | 178 | { 128, 8, 128, 1 }, | 
|  | 179 | { 128, 12, 128, 1 }, | 
|  | 180 | { 128, 16, 128, 1 }, | 
|  | 181 | }; | 
|  | 182 |  | 
|  | 183 | static const size_t g_double_aligns_len = sizeof(g_double_aligns)/sizeof(int[4]); | 
|  | 184 |  | 
|  | 185 | static size_t SetIncrement(size_t len) { | 
|  | 186 | if (len >= 4096) { | 
|  | 187 | return 1024; | 
|  | 188 | } else if (len >= 1024) { | 
|  | 189 | return 256; | 
|  | 190 | } | 
|  | 191 | return 1; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | // Return a pointer into the current buffer with the specified alignment. | 
|  | 195 | static void *GetAlignedPtr(void *orig_ptr, int alignment, int or_mask) { | 
|  | 196 | uint64_t ptr = reinterpret_cast<uint64_t>(orig_ptr); | 
|  | 197 | if (alignment > 0) { | 
|  | 198 | // When setting the alignment, set it to exactly the alignment chosen. | 
|  | 199 | // The pointer returned will be guaranteed not to be aligned to anything | 
|  | 200 | // more than that. | 
|  | 201 | ptr += alignment - (ptr & (alignment - 1)); | 
|  | 202 | ptr |= alignment | or_mask; | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | return reinterpret_cast<void*>(ptr); | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | static void SetFencepost(uint8_t *buffer) { | 
|  | 209 | for (int i = 0; i < FENCEPOST_LENGTH; i += 2) { | 
|  | 210 | buffer[i] = 0xde; | 
|  | 211 | buffer[i+1] = 0xad; | 
|  | 212 | } | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | static void VerifyFencepost(uint8_t *buffer) { | 
|  | 216 | for (int i = 0; i < FENCEPOST_LENGTH; i += 2) { | 
|  | 217 | if (buffer[i] != 0xde || buffer[i+1] != 0xad) { | 
|  | 218 | uint8_t expected_value; | 
|  | 219 | if (buffer[i] == 0xde) { | 
|  | 220 | i++; | 
|  | 221 | expected_value = 0xad; | 
|  | 222 | } else { | 
|  | 223 | expected_value = 0xde; | 
|  | 224 | } | 
|  | 225 | ASSERT_EQ(expected_value, buffer[i]); | 
|  | 226 | } | 
|  | 227 | } | 
|  | 228 | } | 
|  | 229 |  | 
| Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 230 | // Malloc can return a tagged pointer, which is not accepted in mm system calls like mprotect. | 
|  | 231 | // Clear top 8 bits of the address on 64-bit platforms. | 
|  | 232 | static int MprotectHeap(void* addr, size_t len, int prot) { | 
|  | 233 | #if defined(__LP64__) | 
|  | 234 | constexpr uintptr_t mask = (static_cast<uintptr_t>(1) << 56) - 1; | 
|  | 235 | void* untagged_addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) & mask); | 
|  | 236 | #else | 
|  | 237 | void* untagged_addr = addr; | 
|  | 238 | #endif | 
|  | 239 | return mprotect(untagged_addr, len, prot); | 
|  | 240 | } | 
|  | 241 |  | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 242 | void RunSingleBufferAlignTest( | 
|  | 243 | size_t max_test_size, void (*test_func)(uint8_t*, size_t), | 
|  | 244 | size_t (*set_incr)(size_t)) { | 
|  | 245 | if (!set_incr) { | 
|  | 246 | set_incr = SetIncrement; | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | // Allocate one large buffer with lots of extra space so that we can | 
|  | 250 | // guarantee that the all possible alignments will fit. | 
|  | 251 | uint8_t *buf = new uint8_t[3*max_test_size]; | 
|  | 252 |  | 
|  | 253 | uint8_t *buf_align; | 
|  | 254 | for (size_t i = 0; i < g_single_aligns_len; i++) { | 
|  | 255 | size_t incr = 1; | 
|  | 256 | for (size_t len = 0; len <= max_test_size; len += incr) { | 
|  | 257 | incr = set_incr(len); | 
|  | 258 |  | 
|  | 259 | buf_align = reinterpret_cast<uint8_t*>(GetAlignedPtr( | 
|  | 260 | buf+FENCEPOST_LENGTH, g_single_aligns[i][0], g_single_aligns[i][1])); | 
|  | 261 |  | 
|  | 262 | SetFencepost(&buf_align[-FENCEPOST_LENGTH]); | 
|  | 263 | SetFencepost(&buf_align[len]); | 
|  | 264 |  | 
|  | 265 | test_func(buf_align, len); | 
|  | 266 |  | 
| Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 267 | VerifyFencepost(&buf_align[-FENCEPOST_LENGTH]); | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 268 | VerifyFencepost(&buf_align[len]); | 
|  | 269 | } | 
|  | 270 | } | 
| Pirama Arumuga Nainar | 4d44675 | 2015-07-09 10:15:15 -0700 | [diff] [blame] | 271 | delete[] buf; | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 272 | } | 
|  | 273 |  | 
|  | 274 | void RunSrcDstBufferAlignTest( | 
|  | 275 | size_t max_test_size, void (*test_func)(uint8_t*, uint8_t*, size_t), | 
|  | 276 | size_t (*set_incr)(size_t)) { | 
|  | 277 | if (!set_incr) { | 
|  | 278 | set_incr = SetIncrement; | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | // Allocate two large buffers for all of the testing. | 
|  | 282 | uint8_t* src = new uint8_t[3*max_test_size]; | 
|  | 283 | uint8_t* dst = new uint8_t[3*max_test_size]; | 
|  | 284 |  | 
|  | 285 | uint8_t* src_align; | 
|  | 286 | uint8_t* dst_align; | 
|  | 287 | for (size_t i = 0; i < g_double_aligns_len; i++) { | 
|  | 288 | size_t incr = 1; | 
|  | 289 | for (size_t len = 0; len <= max_test_size; len += incr) { | 
|  | 290 | incr = set_incr(len); | 
|  | 291 |  | 
|  | 292 | src_align = | 
|  | 293 | reinterpret_cast<uint8_t*>(GetAlignedPtr( | 
|  | 294 | src+FENCEPOST_LENGTH, g_double_aligns[i][0], g_double_aligns[i][1])); | 
|  | 295 | dst_align = | 
|  | 296 | reinterpret_cast<uint8_t*>(GetAlignedPtr( | 
|  | 297 | dst+FENCEPOST_LENGTH, g_double_aligns[i][2], g_double_aligns[i][3])); | 
|  | 298 | SetFencepost(&dst_align[-FENCEPOST_LENGTH]); | 
|  | 299 | SetFencepost(&dst_align[len]); | 
|  | 300 |  | 
|  | 301 | test_func(src_align, dst_align, len); | 
|  | 302 |  | 
| Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 303 | VerifyFencepost(&dst_align[-FENCEPOST_LENGTH]); | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 304 | VerifyFencepost(&dst_align[len]); | 
|  | 305 | } | 
|  | 306 | } | 
| Pirama Arumuga Nainar | 4d44675 | 2015-07-09 10:15:15 -0700 | [diff] [blame] | 307 | delete[] src; | 
|  | 308 | delete[] dst; | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 309 | } | 
|  | 310 |  | 
| Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 311 | void RunCmpBufferAlignTest( | 
|  | 312 | size_t max_test_size, void (*test_cmp_func)(uint8_t*, uint8_t*, size_t), | 
|  | 313 | void (*test_miscmp_func)(uint8_t*, uint8_t*, size_t, size_t), | 
|  | 314 | size_t (*set_incr)(size_t)) { | 
|  | 315 | if (!set_incr) { | 
|  | 316 | set_incr = SetIncrement; | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | // Allocate two large buffers for all of the testing. | 
|  | 320 | uint8_t* buf1 = new uint8_t[3*max_test_size]; | 
|  | 321 | uint8_t* buf2 = new uint8_t[3*max_test_size]; | 
|  | 322 |  | 
|  | 323 | uint8_t* buf1_align; | 
|  | 324 | uint8_t* buf2_align; | 
|  | 325 | for (size_t i = 0; i < g_double_aligns_len; i++) { | 
|  | 326 | size_t incr = 1; | 
|  | 327 | for (size_t len = 0; len <= max_test_size; len += incr) { | 
|  | 328 | incr = set_incr(len); | 
|  | 329 |  | 
|  | 330 | buf1_align = | 
|  | 331 | reinterpret_cast<uint8_t*>(GetAlignedPtr( | 
|  | 332 | buf1, g_double_aligns[i][0], g_double_aligns[i][1])); | 
|  | 333 | buf2_align = | 
|  | 334 | reinterpret_cast<uint8_t*>(GetAlignedPtr( | 
|  | 335 | buf2, g_double_aligns[i][2], g_double_aligns[i][3])); | 
|  | 336 |  | 
|  | 337 | // Check by putting all zeroes after both buffers. | 
|  | 338 | memset(buf1_align+len, 0, 32); | 
|  | 339 | memset(buf2_align+len, 0, 32); | 
|  | 340 | test_cmp_func(buf1_align, buf2_align, len); | 
|  | 341 |  | 
|  | 342 | // Check by putting different values after both buffers. | 
|  | 343 | for (size_t j = 0; j < 32; j++) { | 
|  | 344 | buf1_align[len+j] = j; | 
|  | 345 | buf2_align[len+j] = j+1; | 
|  | 346 | } | 
|  | 347 | test_cmp_func(buf1_align, buf2_align, len); | 
|  | 348 |  | 
|  | 349 | if (len > 0) { | 
|  | 350 | // Change the lengths of the buffers and verify that there are | 
|  | 351 | // miscompares. | 
|  | 352 | for (size_t len2 = len+1; len2 < len+32; len2++) { | 
|  | 353 | test_miscmp_func(buf1_align, buf2_align, len, len2); | 
|  | 354 | test_miscmp_func(buf1_align, buf2_align, len2, len); | 
|  | 355 | } | 
|  | 356 | } | 
|  | 357 | } | 
|  | 358 | } | 
| Pirama Arumuga Nainar | 4d44675 | 2015-07-09 10:15:15 -0700 | [diff] [blame] | 359 | delete[] buf1; | 
|  | 360 | delete[] buf2; | 
| Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 361 | } | 
|  | 362 |  | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 363 | void RunSingleBufferOverreadTest(void (*test_func)(uint8_t*, size_t)) { | 
|  | 364 | // In order to verify that functions are not reading past the end of the | 
|  | 365 | // src, create data that ends exactly at an unreadable memory boundary. | 
|  | 366 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); | 
|  | 367 | uint8_t* memory; | 
|  | 368 | ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory), pagesize, | 
|  | 369 | 2*pagesize) == 0); | 
|  | 370 | memset(memory, 0x23, 2*pagesize); | 
|  | 371 |  | 
|  | 372 | // Make the second page unreadable and unwritable. | 
| Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 373 | ASSERT_TRUE(MprotectHeap(&memory[pagesize], pagesize, PROT_NONE) == 0); | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 374 |  | 
|  | 375 | for (size_t i = 0; i < pagesize; i++) { | 
|  | 376 | uint8_t* buf = &memory[pagesize-i]; | 
|  | 377 |  | 
|  | 378 | test_func(buf, i); | 
|  | 379 | } | 
| Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 380 | ASSERT_TRUE(MprotectHeap(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0); | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 381 | free(memory); | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | void RunSrcDstBufferOverreadTest(void (*test_func)(uint8_t*, uint8_t*, size_t)) { | 
|  | 385 | // In order to verify that functions are not reading past the end of the | 
|  | 386 | // src, create data that ends exactly at an unreadable memory boundary. | 
|  | 387 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); | 
|  | 388 | uint8_t* memory; | 
|  | 389 | ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory), pagesize, | 
|  | 390 | 2*pagesize) == 0); | 
|  | 391 | memset(memory, 0x23, 2*pagesize); | 
|  | 392 |  | 
|  | 393 | // Make the second page unreadable and unwritable. | 
| Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 394 | ASSERT_TRUE(MprotectHeap(&memory[pagesize], pagesize, PROT_NONE) == 0); | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 395 |  | 
| Christopher Ferris | fdfcfce | 2015-09-23 22:09:09 -0700 | [diff] [blame] | 396 | uint8_t* dst_buffer = new uint8_t[2*pagesize]; | 
|  | 397 | // Change the dst alignment as we change the source. | 
|  | 398 | for (size_t i = 0; i < 16; i++) { | 
|  | 399 | uint8_t* dst = &dst_buffer[i]; | 
|  | 400 | for (size_t j = 0; j < pagesize; j++) { | 
|  | 401 | uint8_t* src = &memory[pagesize-j]; | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 402 |  | 
| Christopher Ferris | fdfcfce | 2015-09-23 22:09:09 -0700 | [diff] [blame] | 403 | test_func(src, dst, j); | 
|  | 404 | } | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 405 | } | 
| Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 406 | ASSERT_TRUE(MprotectHeap(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0); | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 407 | free(memory); | 
| Christopher Ferris | fdfcfce | 2015-09-23 22:09:09 -0700 | [diff] [blame] | 408 | delete[] dst_buffer; | 
| Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 409 | } | 
| Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 410 |  | 
|  | 411 | void RunCmpBufferOverreadTest( | 
|  | 412 | void (*test_cmp_func)(uint8_t*, uint8_t*, size_t), | 
|  | 413 | void (*test_miscmp_func)(uint8_t*, uint8_t*, size_t, size_t)) { | 
|  | 414 | // In order to verify that functions are not reading past the end of either | 
|  | 415 | // of the bufs, create both buffers that end exactly at an unreadable memory | 
|  | 416 | // boundary. | 
|  | 417 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); | 
|  | 418 | uint8_t* memory1; | 
|  | 419 | ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory1), pagesize, | 
|  | 420 | 2*pagesize) == 0); | 
|  | 421 | memset(memory1, 0x23, 2*pagesize); | 
|  | 422 |  | 
|  | 423 | // Make the second page unreadable and unwritable. | 
| Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 424 | ASSERT_TRUE(MprotectHeap(&memory1[pagesize], pagesize, PROT_NONE) == 0); | 
| Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 425 |  | 
|  | 426 | uint8_t* memory2; | 
|  | 427 | ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory2), pagesize, | 
|  | 428 | 2*pagesize) == 0); | 
|  | 429 | memset(memory2, 0x23, 2*pagesize); | 
|  | 430 |  | 
|  | 431 | // Make the second page unreadable and unwritable. | 
| Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 432 | ASSERT_TRUE(MprotectHeap(&memory2[pagesize], pagesize, PROT_NONE) == 0); | 
| Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 433 |  | 
|  | 434 | for (size_t i = 0; i < pagesize; i++) { | 
|  | 435 | uint8_t* buf1 = &memory1[pagesize-i]; | 
|  | 436 | uint8_t* buf2 = &memory2[pagesize-i]; | 
|  | 437 |  | 
|  | 438 | test_cmp_func(buf1, buf2, i); | 
|  | 439 | } | 
|  | 440 |  | 
|  | 441 | // Don't cycle through pagesize, MISCMP_MAX_LENGTH bytes should be good. | 
|  | 442 | size_t miscmp_len; | 
|  | 443 | if (pagesize > MISCMP_MAX_LENGTH) { | 
|  | 444 | miscmp_len = MISCMP_MAX_LENGTH; | 
|  | 445 | } else { | 
|  | 446 | miscmp_len = pagesize; | 
|  | 447 | } | 
|  | 448 | for (size_t i = 1; i < miscmp_len; i++) { | 
|  | 449 | uint8_t* buf1 = &memory1[pagesize-i]; | 
|  | 450 | for (size_t j = 1; j < miscmp_len; j++) { | 
|  | 451 | if (j == i) | 
|  | 452 | continue; | 
|  | 453 |  | 
|  | 454 | uint8_t* buf2 = &memory2[pagesize-j]; | 
|  | 455 |  | 
|  | 456 | test_miscmp_func(buf1, buf2, i, j); | 
|  | 457 | } | 
|  | 458 | } | 
|  | 459 |  | 
| Evgenii Stepanov | acd6f4f | 2018-11-06 16:48:27 -0800 | [diff] [blame] | 460 | ASSERT_TRUE(MprotectHeap(&memory1[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0); | 
|  | 461 | ASSERT_TRUE(MprotectHeap(&memory2[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0); | 
| Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 462 | free(memory1); | 
|  | 463 | free(memory2); | 
|  | 464 | } |