* commit 'b679f65420702554ae00dffb0e150ca75aeea989': Add strcmp/memcmp testing.
This commit is contained in:
commit
fe3f0ccc84
@ -21,6 +21,10 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "buffer_tests.h"
|
||||
|
||||
// For the comparison buffer tests, the maximum length to test for the
|
||||
// miscompare checks.
|
||||
#define MISCMP_MAX_LENGTH 512
|
||||
|
||||
#define FENCEPOST_LENGTH 8
|
||||
|
||||
static int g_single_aligns[][2] = {
|
||||
@ -248,9 +252,7 @@ void RunSingleBufferAlignTest(
|
||||
|
||||
test_func(buf_align, len);
|
||||
|
||||
if (buf_align != buf) {
|
||||
VerifyFencepost(&buf_align[-FENCEPOST_LENGTH]);
|
||||
}
|
||||
VerifyFencepost(&buf_align[-FENCEPOST_LENGTH]);
|
||||
VerifyFencepost(&buf_align[len]);
|
||||
}
|
||||
}
|
||||
@ -286,9 +288,7 @@ void RunSrcDstBufferAlignTest(
|
||||
|
||||
test_func(src_align, dst_align, len);
|
||||
|
||||
if (dst_align != dst) {
|
||||
VerifyFencepost(&dst_align[-FENCEPOST_LENGTH]);
|
||||
}
|
||||
VerifyFencepost(&dst_align[-FENCEPOST_LENGTH]);
|
||||
VerifyFencepost(&dst_align[len]);
|
||||
}
|
||||
}
|
||||
@ -296,6 +296,58 @@ void RunSrcDstBufferAlignTest(
|
||||
delete dst;
|
||||
}
|
||||
|
||||
void RunCmpBufferAlignTest(
|
||||
size_t max_test_size, void (*test_cmp_func)(uint8_t*, uint8_t*, size_t),
|
||||
void (*test_miscmp_func)(uint8_t*, uint8_t*, size_t, size_t),
|
||||
size_t (*set_incr)(size_t)) {
|
||||
if (!set_incr) {
|
||||
set_incr = SetIncrement;
|
||||
}
|
||||
|
||||
// Allocate two large buffers for all of the testing.
|
||||
uint8_t* buf1 = new uint8_t[3*max_test_size];
|
||||
uint8_t* buf2 = new uint8_t[3*max_test_size];
|
||||
|
||||
uint8_t* buf1_align;
|
||||
uint8_t* buf2_align;
|
||||
for (size_t i = 0; i < g_double_aligns_len; i++) {
|
||||
size_t incr = 1;
|
||||
for (size_t len = 0; len <= max_test_size; len += incr) {
|
||||
incr = set_incr(len);
|
||||
|
||||
buf1_align =
|
||||
reinterpret_cast<uint8_t*>(GetAlignedPtr(
|
||||
buf1, g_double_aligns[i][0], g_double_aligns[i][1]));
|
||||
buf2_align =
|
||||
reinterpret_cast<uint8_t*>(GetAlignedPtr(
|
||||
buf2, g_double_aligns[i][2], g_double_aligns[i][3]));
|
||||
|
||||
// Check by putting all zeroes after both buffers.
|
||||
memset(buf1_align+len, 0, 32);
|
||||
memset(buf2_align+len, 0, 32);
|
||||
test_cmp_func(buf1_align, buf2_align, len);
|
||||
|
||||
// Check by putting different values after both buffers.
|
||||
for (size_t j = 0; j < 32; j++) {
|
||||
buf1_align[len+j] = j;
|
||||
buf2_align[len+j] = j+1;
|
||||
}
|
||||
test_cmp_func(buf1_align, buf2_align, len);
|
||||
|
||||
if (len > 0) {
|
||||
// Change the lengths of the buffers and verify that there are
|
||||
// miscompares.
|
||||
for (size_t len2 = len+1; len2 < len+32; len2++) {
|
||||
test_miscmp_func(buf1_align, buf2_align, len, len2);
|
||||
test_miscmp_func(buf1_align, buf2_align, len2, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
delete buf1;
|
||||
delete buf2;
|
||||
}
|
||||
|
||||
void RunSingleBufferOverreadTest(void (*test_func)(uint8_t*, size_t)) {
|
||||
// In order to verify that functions are not reading past the end of the
|
||||
// src, create data that ends exactly at an unreadable memory boundary.
|
||||
@ -339,3 +391,58 @@ void RunSrcDstBufferOverreadTest(void (*test_func)(uint8_t*, uint8_t*, size_t))
|
||||
free(memory);
|
||||
delete dst;
|
||||
}
|
||||
|
||||
void RunCmpBufferOverreadTest(
|
||||
void (*test_cmp_func)(uint8_t*, uint8_t*, size_t),
|
||||
void (*test_miscmp_func)(uint8_t*, uint8_t*, size_t, size_t)) {
|
||||
// In order to verify that functions are not reading past the end of either
|
||||
// of the bufs, create both buffers that end exactly at an unreadable memory
|
||||
// boundary.
|
||||
size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
|
||||
uint8_t* memory1;
|
||||
ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory1), pagesize,
|
||||
2*pagesize) == 0);
|
||||
memset(memory1, 0x23, 2*pagesize);
|
||||
|
||||
// Make the second page unreadable and unwritable.
|
||||
ASSERT_TRUE(mprotect(&memory1[pagesize], pagesize, PROT_NONE) == 0);
|
||||
|
||||
uint8_t* memory2;
|
||||
ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory2), pagesize,
|
||||
2*pagesize) == 0);
|
||||
memset(memory2, 0x23, 2*pagesize);
|
||||
|
||||
// Make the second page unreadable and unwritable.
|
||||
ASSERT_TRUE(mprotect(&memory2[pagesize], pagesize, PROT_NONE) == 0);
|
||||
|
||||
for (size_t i = 0; i < pagesize; i++) {
|
||||
uint8_t* buf1 = &memory1[pagesize-i];
|
||||
uint8_t* buf2 = &memory2[pagesize-i];
|
||||
|
||||
test_cmp_func(buf1, buf2, i);
|
||||
}
|
||||
|
||||
// Don't cycle through pagesize, MISCMP_MAX_LENGTH bytes should be good.
|
||||
size_t miscmp_len;
|
||||
if (pagesize > MISCMP_MAX_LENGTH) {
|
||||
miscmp_len = MISCMP_MAX_LENGTH;
|
||||
} else {
|
||||
miscmp_len = pagesize;
|
||||
}
|
||||
for (size_t i = 1; i < miscmp_len; i++) {
|
||||
uint8_t* buf1 = &memory1[pagesize-i];
|
||||
for (size_t j = 1; j < miscmp_len; j++) {
|
||||
if (j == i)
|
||||
continue;
|
||||
|
||||
uint8_t* buf2 = &memory2[pagesize-j];
|
||||
|
||||
test_miscmp_func(buf1, buf2, i, j);
|
||||
}
|
||||
}
|
||||
|
||||
ASSERT_TRUE(mprotect(&memory1[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0);
|
||||
ASSERT_TRUE(mprotect(&memory2[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0);
|
||||
free(memory1);
|
||||
free(memory2);
|
||||
}
|
||||
|
@ -28,8 +28,17 @@ void RunSrcDstBufferAlignTest(
|
||||
size_t max_test_size, void (*test_func)(uint8_t*, uint8_t*, size_t),
|
||||
size_t (*set_incr)(size_t) = NULL);
|
||||
|
||||
void RunCmpBufferAlignTest(
|
||||
size_t max_test_size, void (*test_cmp_func)(uint8_t*, uint8_t*, size_t),
|
||||
void (*test_miscmp_func)(uint8_t*, uint8_t*, size_t, size_t),
|
||||
size_t (*set_incr)(size_t) = NULL);
|
||||
|
||||
void RunSingleBufferOverreadTest(void (*test_func)(uint8_t*, size_t));
|
||||
|
||||
void RunSrcDstBufferOverreadTest(void (*test_func)(uint8_t*, uint8_t*, size_t));
|
||||
|
||||
void RunCmpBufferOverreadTest(
|
||||
void (*test_cmp_func)(uint8_t*, uint8_t*, size_t),
|
||||
void (*test_miscmp_func)(uint8_t*, uint8_t*, size_t, size_t));
|
||||
|
||||
#endif // _BIONIC_TESTS_BUFFER_TESTS_H
|
||||
|
@ -956,7 +956,7 @@ TEST(string, strcpy_overread) {
|
||||
}
|
||||
|
||||
// Use our own incrementer to cut down on the total number of calls.
|
||||
static size_t StrcatSetIncrement(size_t len) {
|
||||
static size_t LargeSetIncrement(size_t len) {
|
||||
if (len >= 4096) {
|
||||
return 4096;
|
||||
} else if (len >= 1024) {
|
||||
@ -1001,9 +1001,94 @@ static void DoStrcatTest(uint8_t* src, uint8_t* dst, size_t len) {
|
||||
}
|
||||
|
||||
TEST(string, strcat_align) {
|
||||
RunSrcDstBufferAlignTest(MEDIUM, DoStrcatTest, StrcatSetIncrement);
|
||||
RunSrcDstBufferAlignTest(MEDIUM, DoStrcatTest, LargeSetIncrement);
|
||||
}
|
||||
|
||||
TEST(string, strcat_overread) {
|
||||
RunSrcDstBufferOverreadTest(DoStrcatTest);
|
||||
}
|
||||
|
||||
static void DoStrcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) {
|
||||
if (len >= 1) {
|
||||
memset(buf1, (32 + (len % 96)), len - 1);
|
||||
buf1[len-1] = '\0';
|
||||
memset(buf2, (32 + (len % 96)), len - 1);
|
||||
buf2[len-1] = '\0';
|
||||
ASSERT_EQ(0, strcmp(reinterpret_cast<char*>(buf1),
|
||||
reinterpret_cast<char*>(buf2)));
|
||||
}
|
||||
}
|
||||
|
||||
static void DoStrcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) {
|
||||
// Do string length differences.
|
||||
int c = (32 + (len1 % 96));
|
||||
memset(buf1, c, len1 - 1);
|
||||
buf1[len1-1] = '\0';
|
||||
memset(buf2, c, len2 - 1);
|
||||
buf2[len2-1] = '\0';
|
||||
ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
|
||||
reinterpret_cast<char*>(buf2)));
|
||||
|
||||
// Do single character differences.
|
||||
size_t len;
|
||||
if (len1 > len2) {
|
||||
len = len2;
|
||||
} else {
|
||||
len = len1;
|
||||
}
|
||||
// Need at least a two character buffer to do this test.
|
||||
if (len > 1) {
|
||||
buf1[len-1] = '\0';
|
||||
buf2[len-1] = '\0';
|
||||
int diff_c = (c + 1) % 96;
|
||||
|
||||
buf1[len-2] = diff_c;
|
||||
ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
|
||||
reinterpret_cast<char*>(buf2)));
|
||||
|
||||
buf1[len-2] = c;
|
||||
buf2[len-2] = diff_c;
|
||||
ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
|
||||
reinterpret_cast<char*>(buf2)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(string, strcmp_align) {
|
||||
RunCmpBufferAlignTest(MEDIUM, DoStrcmpTest, DoStrcmpFailTest, LargeSetIncrement);
|
||||
}
|
||||
|
||||
TEST(string, strcmp_overread) {
|
||||
RunCmpBufferOverreadTest(DoStrcmpTest, DoStrcmpFailTest);
|
||||
}
|
||||
|
||||
static void DoMemcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) {
|
||||
memset(buf1, len+1, len);
|
||||
memset(buf2, len+1, len);
|
||||
ASSERT_EQ(0, memcmp(buf1, buf2, len));
|
||||
}
|
||||
|
||||
static void DoMemcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) {
|
||||
size_t len;
|
||||
if (len1 > len2) {
|
||||
len = len2;
|
||||
} else {
|
||||
len = len1;
|
||||
}
|
||||
|
||||
memset(buf1, len2+1, len);
|
||||
buf1[len-1] = len2;
|
||||
memset(buf2, len2+1, len);
|
||||
ASSERT_NE(0, memcmp(buf1, buf2, len));
|
||||
|
||||
buf1[len-1] = len2+1;
|
||||
buf2[len-1] = len2;
|
||||
ASSERT_NE(0, memcmp(buf1, buf2, len));
|
||||
}
|
||||
|
||||
TEST(string, memcmp_align) {
|
||||
RunCmpBufferAlignTest(MEDIUM, DoMemcmpTest, DoMemcmpFailTest, LargeSetIncrement);
|
||||
}
|
||||
|
||||
TEST(string, memcmp_overread) {
|
||||
RunCmpBufferOverreadTest(DoMemcmpTest, DoMemcmpFailTest);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user