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 | |
| 24 | #define FENCEPOST_LENGTH 8 |
| 25 | |
| 26 | static int g_single_aligns[][2] = { |
| 27 | // Both buffers at same alignment. |
| 28 | { 1, 0 }, |
| 29 | { 2, 0 }, |
| 30 | { 4, 0 }, |
| 31 | { 8, 0 }, |
| 32 | { 16, 0 }, |
| 33 | { 32, 0 }, |
| 34 | { 64, 0 }, |
| 35 | { 128, 0 }, |
| 36 | |
| 37 | // General unaligned cases. |
| 38 | { 4, 1 }, |
| 39 | { 4, 2 }, |
| 40 | { 4, 3 }, |
| 41 | |
| 42 | { 8, 1 }, |
| 43 | { 8, 2 }, |
| 44 | { 8, 3 }, |
| 45 | { 8, 4 }, |
| 46 | { 8, 5 }, |
| 47 | { 8, 6 }, |
| 48 | { 8, 7 }, |
| 49 | |
| 50 | { 128, 1 }, |
| 51 | { 128, 4 }, |
| 52 | { 128, 8 }, |
| 53 | { 128, 12 }, |
| 54 | { 128, 16 }, |
| 55 | }; |
| 56 | |
| 57 | static const size_t g_single_aligns_len = sizeof(g_single_aligns)/sizeof(int[2]); |
| 58 | |
| 59 | // Set of multiple buffer alignment combinations to be used for string/memory |
| 60 | // testing routines. |
| 61 | static int g_double_aligns[][4] = { |
| 62 | // Both buffers at same alignment. |
| 63 | { 1, 0, 1, 0 }, |
| 64 | { 2, 0, 2, 0 }, |
| 65 | { 4, 0, 4, 0 }, |
| 66 | { 8, 0, 8, 0 }, |
| 67 | { 16, 0, 16, 0 }, |
| 68 | { 32, 0, 32, 0 }, |
| 69 | { 64, 0, 64, 0 }, |
| 70 | { 128, 0, 128, 0 }, |
| 71 | |
| 72 | // Different word alignments between buffers. |
| 73 | { 8, 0, 4, 0 }, |
| 74 | { 4, 0, 8, 0 }, |
| 75 | { 16, 0, 4, 0 }, |
| 76 | { 4, 0, 16, 0 }, |
| 77 | |
| 78 | // General unaligned cases. |
| 79 | { 4, 0, 4, 1 }, |
| 80 | { 4, 0, 4, 2 }, |
| 81 | { 4, 0, 4, 3 }, |
| 82 | |
| 83 | { 4, 1, 4, 0 }, |
| 84 | { 4, 1, 4, 1 }, |
| 85 | { 4, 1, 4, 2 }, |
| 86 | { 4, 1, 4, 3 }, |
| 87 | |
| 88 | { 4, 2, 4, 0 }, |
| 89 | { 4, 2, 4, 1 }, |
| 90 | { 4, 2, 4, 2 }, |
| 91 | { 4, 2, 4, 3 }, |
| 92 | |
| 93 | { 4, 3, 4, 0 }, |
| 94 | { 4, 3, 4, 1 }, |
| 95 | { 4, 3, 4, 2 }, |
| 96 | { 4, 3, 4, 3 }, |
| 97 | |
| 98 | { 8, 0, 8, 1 }, |
| 99 | { 8, 0, 8, 2 }, |
| 100 | { 8, 0, 8, 3 }, |
| 101 | { 8, 0, 8, 4 }, |
| 102 | { 8, 0, 8, 5 }, |
| 103 | { 8, 0, 8, 6 }, |
| 104 | { 8, 0, 8, 7 }, |
| 105 | |
| 106 | { 8, 1, 8, 0 }, |
| 107 | { 8, 1, 8, 1 }, |
| 108 | { 8, 1, 8, 2 }, |
| 109 | { 8, 1, 8, 3 }, |
| 110 | { 8, 1, 8, 4 }, |
| 111 | { 8, 1, 8, 5 }, |
| 112 | { 8, 1, 8, 6 }, |
| 113 | { 8, 1, 8, 7 }, |
| 114 | |
| 115 | { 8, 2, 8, 0 }, |
| 116 | { 8, 2, 8, 1 }, |
| 117 | { 8, 2, 8, 2 }, |
| 118 | { 8, 2, 8, 3 }, |
| 119 | { 8, 2, 8, 4 }, |
| 120 | { 8, 2, 8, 5 }, |
| 121 | { 8, 2, 8, 6 }, |
| 122 | { 8, 2, 8, 7 }, |
| 123 | |
| 124 | { 8, 3, 8, 0 }, |
| 125 | { 8, 3, 8, 1 }, |
| 126 | { 8, 3, 8, 2 }, |
| 127 | { 8, 3, 8, 3 }, |
| 128 | { 8, 3, 8, 4 }, |
| 129 | { 8, 3, 8, 5 }, |
| 130 | { 8, 3, 8, 6 }, |
| 131 | { 8, 3, 8, 7 }, |
| 132 | |
| 133 | { 8, 4, 8, 0 }, |
| 134 | { 8, 4, 8, 1 }, |
| 135 | { 8, 4, 8, 2 }, |
| 136 | { 8, 4, 8, 3 }, |
| 137 | { 8, 4, 8, 4 }, |
| 138 | { 8, 4, 8, 5 }, |
| 139 | { 8, 4, 8, 6 }, |
| 140 | { 8, 4, 8, 7 }, |
| 141 | |
| 142 | { 8, 5, 8, 0 }, |
| 143 | { 8, 5, 8, 1 }, |
| 144 | { 8, 5, 8, 2 }, |
| 145 | { 8, 5, 8, 3 }, |
| 146 | { 8, 5, 8, 4 }, |
| 147 | { 8, 5, 8, 5 }, |
| 148 | { 8, 5, 8, 6 }, |
| 149 | { 8, 5, 8, 7 }, |
| 150 | |
| 151 | { 8, 6, 8, 0 }, |
| 152 | { 8, 6, 8, 1 }, |
| 153 | { 8, 6, 8, 2 }, |
| 154 | { 8, 6, 8, 3 }, |
| 155 | { 8, 6, 8, 4 }, |
| 156 | { 8, 6, 8, 5 }, |
| 157 | { 8, 6, 8, 6 }, |
| 158 | { 8, 6, 8, 7 }, |
| 159 | |
| 160 | { 8, 7, 8, 0 }, |
| 161 | { 8, 7, 8, 1 }, |
| 162 | { 8, 7, 8, 2 }, |
| 163 | { 8, 7, 8, 3 }, |
| 164 | { 8, 7, 8, 4 }, |
| 165 | { 8, 7, 8, 5 }, |
| 166 | { 8, 7, 8, 6 }, |
| 167 | { 8, 7, 8, 7 }, |
| 168 | |
| 169 | { 128, 1, 128, 4 }, |
| 170 | { 128, 1, 128, 8 }, |
| 171 | { 128, 1, 128, 12 }, |
| 172 | { 128, 1, 128, 16 }, |
| 173 | { 128, 4, 128, 1 }, |
| 174 | { 128, 8, 128, 1 }, |
| 175 | { 128, 12, 128, 1 }, |
| 176 | { 128, 16, 128, 1 }, |
| 177 | }; |
| 178 | |
| 179 | static const size_t g_double_aligns_len = sizeof(g_double_aligns)/sizeof(int[4]); |
| 180 | |
| 181 | static size_t SetIncrement(size_t len) { |
| 182 | if (len >= 4096) { |
| 183 | return 1024; |
| 184 | } else if (len >= 1024) { |
| 185 | return 256; |
| 186 | } |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | // Return a pointer into the current buffer with the specified alignment. |
| 191 | static void *GetAlignedPtr(void *orig_ptr, int alignment, int or_mask) { |
| 192 | uint64_t ptr = reinterpret_cast<uint64_t>(orig_ptr); |
| 193 | if (alignment > 0) { |
| 194 | // When setting the alignment, set it to exactly the alignment chosen. |
| 195 | // The pointer returned will be guaranteed not to be aligned to anything |
| 196 | // more than that. |
| 197 | ptr += alignment - (ptr & (alignment - 1)); |
| 198 | ptr |= alignment | or_mask; |
| 199 | } |
| 200 | |
| 201 | return reinterpret_cast<void*>(ptr); |
| 202 | } |
| 203 | |
| 204 | static void SetFencepost(uint8_t *buffer) { |
| 205 | for (int i = 0; i < FENCEPOST_LENGTH; i += 2) { |
| 206 | buffer[i] = 0xde; |
| 207 | buffer[i+1] = 0xad; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | static void VerifyFencepost(uint8_t *buffer) { |
| 212 | for (int i = 0; i < FENCEPOST_LENGTH; i += 2) { |
| 213 | if (buffer[i] != 0xde || buffer[i+1] != 0xad) { |
| 214 | uint8_t expected_value; |
| 215 | if (buffer[i] == 0xde) { |
| 216 | i++; |
| 217 | expected_value = 0xad; |
| 218 | } else { |
| 219 | expected_value = 0xde; |
| 220 | } |
| 221 | ASSERT_EQ(expected_value, buffer[i]); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void RunSingleBufferAlignTest( |
| 227 | size_t max_test_size, void (*test_func)(uint8_t*, size_t), |
| 228 | size_t (*set_incr)(size_t)) { |
| 229 | if (!set_incr) { |
| 230 | set_incr = SetIncrement; |
| 231 | } |
| 232 | |
| 233 | // Allocate one large buffer with lots of extra space so that we can |
| 234 | // guarantee that the all possible alignments will fit. |
| 235 | uint8_t *buf = new uint8_t[3*max_test_size]; |
| 236 | |
| 237 | uint8_t *buf_align; |
| 238 | for (size_t i = 0; i < g_single_aligns_len; i++) { |
| 239 | size_t incr = 1; |
| 240 | for (size_t len = 0; len <= max_test_size; len += incr) { |
| 241 | incr = set_incr(len); |
| 242 | |
| 243 | buf_align = reinterpret_cast<uint8_t*>(GetAlignedPtr( |
| 244 | buf+FENCEPOST_LENGTH, g_single_aligns[i][0], g_single_aligns[i][1])); |
| 245 | |
| 246 | SetFencepost(&buf_align[-FENCEPOST_LENGTH]); |
| 247 | SetFencepost(&buf_align[len]); |
| 248 | |
| 249 | test_func(buf_align, len); |
| 250 | |
| 251 | if (buf_align != buf) { |
| 252 | VerifyFencepost(&buf_align[-FENCEPOST_LENGTH]); |
| 253 | } |
| 254 | VerifyFencepost(&buf_align[len]); |
| 255 | } |
| 256 | } |
| 257 | delete buf; |
| 258 | } |
| 259 | |
| 260 | void RunSrcDstBufferAlignTest( |
| 261 | size_t max_test_size, void (*test_func)(uint8_t*, uint8_t*, size_t), |
| 262 | size_t (*set_incr)(size_t)) { |
| 263 | if (!set_incr) { |
| 264 | set_incr = SetIncrement; |
| 265 | } |
| 266 | |
| 267 | // Allocate two large buffers for all of the testing. |
| 268 | uint8_t* src = new uint8_t[3*max_test_size]; |
| 269 | uint8_t* dst = new uint8_t[3*max_test_size]; |
| 270 | |
| 271 | uint8_t* src_align; |
| 272 | uint8_t* dst_align; |
| 273 | for (size_t i = 0; i < g_double_aligns_len; i++) { |
| 274 | size_t incr = 1; |
| 275 | for (size_t len = 0; len <= max_test_size; len += incr) { |
| 276 | incr = set_incr(len); |
| 277 | |
| 278 | src_align = |
| 279 | reinterpret_cast<uint8_t*>(GetAlignedPtr( |
| 280 | src+FENCEPOST_LENGTH, g_double_aligns[i][0], g_double_aligns[i][1])); |
| 281 | dst_align = |
| 282 | reinterpret_cast<uint8_t*>(GetAlignedPtr( |
| 283 | dst+FENCEPOST_LENGTH, g_double_aligns[i][2], g_double_aligns[i][3])); |
| 284 | SetFencepost(&dst_align[-FENCEPOST_LENGTH]); |
| 285 | SetFencepost(&dst_align[len]); |
| 286 | |
| 287 | test_func(src_align, dst_align, len); |
| 288 | |
| 289 | if (dst_align != dst) { |
| 290 | VerifyFencepost(&dst_align[-FENCEPOST_LENGTH]); |
| 291 | } |
| 292 | VerifyFencepost(&dst_align[len]); |
| 293 | } |
| 294 | } |
| 295 | delete src; |
| 296 | delete dst; |
| 297 | } |
| 298 | |
| 299 | void RunSingleBufferOverreadTest(void (*test_func)(uint8_t*, size_t)) { |
| 300 | // In order to verify that functions are not reading past the end of the |
| 301 | // src, create data that ends exactly at an unreadable memory boundary. |
| 302 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); |
| 303 | uint8_t* memory; |
| 304 | ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory), pagesize, |
| 305 | 2*pagesize) == 0); |
| 306 | memset(memory, 0x23, 2*pagesize); |
| 307 | |
| 308 | // Make the second page unreadable and unwritable. |
| 309 | ASSERT_TRUE(mprotect(&memory[pagesize], pagesize, PROT_NONE) == 0); |
| 310 | |
| 311 | for (size_t i = 0; i < pagesize; i++) { |
| 312 | uint8_t* buf = &memory[pagesize-i]; |
| 313 | |
| 314 | test_func(buf, i); |
| 315 | } |
| 316 | ASSERT_TRUE(mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0); |
| 317 | free(memory); |
| 318 | } |
| 319 | |
| 320 | void RunSrcDstBufferOverreadTest(void (*test_func)(uint8_t*, uint8_t*, size_t)) { |
| 321 | // In order to verify that functions are not reading past the end of the |
| 322 | // src, create data that ends exactly at an unreadable memory boundary. |
| 323 | size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE)); |
| 324 | uint8_t* memory; |
| 325 | ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory), pagesize, |
| 326 | 2*pagesize) == 0); |
| 327 | memset(memory, 0x23, 2*pagesize); |
| 328 | |
| 329 | // Make the second page unreadable and unwritable. |
| 330 | ASSERT_TRUE(mprotect(&memory[pagesize], pagesize, PROT_NONE) == 0); |
| 331 | |
| 332 | uint8_t* dst = new uint8_t[pagesize]; |
| 333 | for (size_t i = 0; i < pagesize; i++) { |
| 334 | uint8_t* src = &memory[pagesize-i]; |
| 335 | |
| 336 | test_func(src, dst, i); |
| 337 | } |
| 338 | ASSERT_TRUE(mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0); |
| 339 | free(memory); |
| 340 | delete dst; |
| 341 | } |