2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <gtest/gtest.h>
22 #include "buffer_tests.h"
24 // For the comparison buffer tests, the maximum length to test for the
26 #define MISCMP_MAX_LENGTH 512
28 #define FENCEPOST_LENGTH 8
30 static int g_single_aligns[][2] = {
31 // Both buffers at same alignment.
41 // General unaligned cases.
61 static const size_t g_single_aligns_len = sizeof(g_single_aligns)/sizeof(int[2]);
63 // Set of multiple buffer alignment combinations to be used for string/memory
65 static int g_double_aligns[][4] = {
66 // Both buffers at same alignment.
76 // Different word alignments between buffers.
82 // General unaligned cases.
183 static const size_t g_double_aligns_len = sizeof(g_double_aligns)/sizeof(int[4]);
185 static size_t SetIncrement(size_t len) {
188 } else if (len >= 1024) {
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);
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
201 ptr += alignment - (ptr & (alignment - 1));
202 ptr |= alignment | or_mask;
205 return reinterpret_cast<void*>(ptr);
208 static void SetFencepost(uint8_t *buffer) {
209 for (int i = 0; i < FENCEPOST_LENGTH; i += 2) {
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) {
221 expected_value = 0xad;
223 expected_value = 0xde;
225 ASSERT_EQ(expected_value, buffer[i]);
230 void RunSingleBufferAlignTest(
231 size_t max_test_size, void (*test_func)(uint8_t*, size_t),
232 size_t (*set_incr)(size_t)) {
234 set_incr = SetIncrement;
237 // Allocate one large buffer with lots of extra space so that we can
238 // guarantee that the all possible alignments will fit.
239 uint8_t *buf = new uint8_t[3*max_test_size];
242 for (size_t i = 0; i < g_single_aligns_len; i++) {
244 for (size_t len = 0; len <= max_test_size; len += incr) {
245 incr = set_incr(len);
247 buf_align = reinterpret_cast<uint8_t*>(GetAlignedPtr(
248 buf+FENCEPOST_LENGTH, g_single_aligns[i][0], g_single_aligns[i][1]));
250 SetFencepost(&buf_align[-FENCEPOST_LENGTH]);
251 SetFencepost(&buf_align[len]);
253 test_func(buf_align, len);
255 VerifyFencepost(&buf_align[-FENCEPOST_LENGTH]);
256 VerifyFencepost(&buf_align[len]);
262 void RunSrcDstBufferAlignTest(
263 size_t max_test_size, void (*test_func)(uint8_t*, uint8_t*, size_t),
264 size_t (*set_incr)(size_t)) {
266 set_incr = SetIncrement;
269 // Allocate two large buffers for all of the testing.
270 uint8_t* src = new uint8_t[3*max_test_size];
271 uint8_t* dst = new uint8_t[3*max_test_size];
275 for (size_t i = 0; i < g_double_aligns_len; i++) {
277 for (size_t len = 0; len <= max_test_size; len += incr) {
278 incr = set_incr(len);
281 reinterpret_cast<uint8_t*>(GetAlignedPtr(
282 src+FENCEPOST_LENGTH, g_double_aligns[i][0], g_double_aligns[i][1]));
284 reinterpret_cast<uint8_t*>(GetAlignedPtr(
285 dst+FENCEPOST_LENGTH, g_double_aligns[i][2], g_double_aligns[i][3]));
286 SetFencepost(&dst_align[-FENCEPOST_LENGTH]);
287 SetFencepost(&dst_align[len]);
289 test_func(src_align, dst_align, len);
291 VerifyFencepost(&dst_align[-FENCEPOST_LENGTH]);
292 VerifyFencepost(&dst_align[len]);
299 void RunCmpBufferAlignTest(
300 size_t max_test_size, void (*test_cmp_func)(uint8_t*, uint8_t*, size_t),
301 void (*test_miscmp_func)(uint8_t*, uint8_t*, size_t, size_t),
302 size_t (*set_incr)(size_t)) {
304 set_incr = SetIncrement;
307 // Allocate two large buffers for all of the testing.
308 uint8_t* buf1 = new uint8_t[3*max_test_size];
309 uint8_t* buf2 = new uint8_t[3*max_test_size];
313 for (size_t i = 0; i < g_double_aligns_len; i++) {
315 for (size_t len = 0; len <= max_test_size; len += incr) {
316 incr = set_incr(len);
319 reinterpret_cast<uint8_t*>(GetAlignedPtr(
320 buf1, g_double_aligns[i][0], g_double_aligns[i][1]));
322 reinterpret_cast<uint8_t*>(GetAlignedPtr(
323 buf2, g_double_aligns[i][2], g_double_aligns[i][3]));
325 // Check by putting all zeroes after both buffers.
326 memset(buf1_align+len, 0, 32);
327 memset(buf2_align+len, 0, 32);
328 test_cmp_func(buf1_align, buf2_align, len);
330 // Check by putting different values after both buffers.
331 for (size_t j = 0; j < 32; j++) {
332 buf1_align[len+j] = j;
333 buf2_align[len+j] = j+1;
335 test_cmp_func(buf1_align, buf2_align, len);
338 // Change the lengths of the buffers and verify that there are
340 for (size_t len2 = len+1; len2 < len+32; len2++) {
341 test_miscmp_func(buf1_align, buf2_align, len, len2);
342 test_miscmp_func(buf1_align, buf2_align, len2, len);
351 void RunSingleBufferOverreadTest(void (*test_func)(uint8_t*, size_t)) {
352 // In order to verify that functions are not reading past the end of the
353 // src, create data that ends exactly at an unreadable memory boundary.
354 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
356 ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory), pagesize,
358 memset(memory, 0x23, 2*pagesize);
360 // Make the second page unreadable and unwritable.
361 ASSERT_TRUE(mprotect(&memory[pagesize], pagesize, PROT_NONE) == 0);
363 for (size_t i = 0; i < pagesize; i++) {
364 uint8_t* buf = &memory[pagesize-i];
368 ASSERT_TRUE(mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0);
372 void RunSrcDstBufferOverreadTest(void (*test_func)(uint8_t*, uint8_t*, size_t)) {
373 // In order to verify that functions are not reading past the end of the
374 // src, create data that ends exactly at an unreadable memory boundary.
375 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
377 ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory), pagesize,
379 memset(memory, 0x23, 2*pagesize);
381 // Make the second page unreadable and unwritable.
382 ASSERT_TRUE(mprotect(&memory[pagesize], pagesize, PROT_NONE) == 0);
384 uint8_t* dst_buffer = new uint8_t[2*pagesize];
385 // Change the dst alignment as we change the source.
386 for (size_t i = 0; i < 16; i++) {
387 uint8_t* dst = &dst_buffer[i];
388 for (size_t j = 0; j < pagesize; j++) {
389 uint8_t* src = &memory[pagesize-j];
391 test_func(src, dst, j);
394 ASSERT_TRUE(mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0);
399 void RunCmpBufferOverreadTest(
400 void (*test_cmp_func)(uint8_t*, uint8_t*, size_t),
401 void (*test_miscmp_func)(uint8_t*, uint8_t*, size_t, size_t)) {
402 // In order to verify that functions are not reading past the end of either
403 // of the bufs, create both buffers that end exactly at an unreadable memory
405 size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
407 ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory1), pagesize,
409 memset(memory1, 0x23, 2*pagesize);
411 // Make the second page unreadable and unwritable.
412 ASSERT_TRUE(mprotect(&memory1[pagesize], pagesize, PROT_NONE) == 0);
415 ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory2), pagesize,
417 memset(memory2, 0x23, 2*pagesize);
419 // Make the second page unreadable and unwritable.
420 ASSERT_TRUE(mprotect(&memory2[pagesize], pagesize, PROT_NONE) == 0);
422 for (size_t i = 0; i < pagesize; i++) {
423 uint8_t* buf1 = &memory1[pagesize-i];
424 uint8_t* buf2 = &memory2[pagesize-i];
426 test_cmp_func(buf1, buf2, i);
429 // Don't cycle through pagesize, MISCMP_MAX_LENGTH bytes should be good.
431 if (pagesize > MISCMP_MAX_LENGTH) {
432 miscmp_len = MISCMP_MAX_LENGTH;
434 miscmp_len = pagesize;
436 for (size_t i = 1; i < miscmp_len; i++) {
437 uint8_t* buf1 = &memory1[pagesize-i];
438 for (size_t j = 1; j < miscmp_len; j++) {
442 uint8_t* buf2 = &memory2[pagesize-j];
444 test_miscmp_func(buf1, buf2, i, j);
448 ASSERT_TRUE(mprotect(&memory1[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0);
449 ASSERT_TRUE(mprotect(&memory2[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0);