// Return a pointer into the current buffer with the specified alignment. staticvoid *GetAlignedPtr(void *orig_ptr, int alignment, int or_mask) {
uint64_t ptr = reinterpret_cast<uint64_t>(orig_ptr); if (alignment > 0) { // When setting the alignment, set it to exactly the alignment chosen. // The pointer returned will be guaranteed not to be aligned to anything // more than that.
ptr += alignment - (ptr & (alignment - 1));
ptr |= alignment | or_mask;
}
returnreinterpret_cast<void*>(ptr);
}
staticvoid SetFencepost(uint8_t *buffer) { for (int i = 0; i < FENCEPOST_LENGTH; i += 2) {
buffer[i] = 0xde;
buffer[i+1] = 0xad;
}
}
staticvoid VerifyFencepost(uint8_t *buffer) { for (int i = 0; i < FENCEPOST_LENGTH; i += 2) { if (buffer[i] != 0xde || buffer[i+1] != 0xad) {
uint8_t expected_value; if (buffer[i] == 0xde) {
i++;
expected_value = 0xad;
} else {
expected_value = 0xde;
}
ASSERT_EQ(expected_value, buffer[i]);
}
}
}
// Malloc can return a tagged pointer, which is not accepted in mm system calls like mprotect // in the preliminary version of the syscall tagging support in the current Pixel 2 kernel. // Note: the final version of the kernel patchset may relax this requirement. staticint MprotectHeap(void* addr, size_t len, int prot) { return mprotect(untag_address(addr), len, prot);
}
// Allocate one large buffer with lots of extra space so that we can // guarantee that the all possible alignments will fit.
uint8_t *buf = new uint8_t[3*max_test_size];
uint8_t *buf_align; for (size_t i = 0; i < g_single_aligns_len; i++) {
size_t incr = 1; for (size_t len = 0; len <= max_test_size; len += incr) {
incr = set_incr(len);
// Allocate two large buffers for all of the testing.
uint8_t* src = new uint8_t[3*max_test_size];
uint8_t* dst = new uint8_t[3*max_test_size];
uint8_t* src_align;
uint8_t* dst_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);
// 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);
// 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.
size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
uint8_t* memory;
ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2*pagesize) == 0);
memset(memory, 0x23, 2*pagesize);
// Make the second page unreadable and unwritable.
ASSERT_TRUE(MprotectHeap(&memory[pagesize], pagesize, PROT_NONE) == 0);
for (size_t i = 0; i < pagesize; i++) {
uint8_t* buf = &memory[pagesize-i];
void RunSrcDstBufferOverreadTest(void (*test_func)(uint8_t*, 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.
size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
uint8_t* memory;
ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory), pagesize, 2*pagesize) == 0);
memset(memory, 0x23, 2*pagesize);
// Make the second page unreadable and unwritable.
ASSERT_TRUE(MprotectHeap(&memory[pagesize], pagesize, PROT_NONE) == 0);
uint8_t* dst_buffer = new uint8_t[2*pagesize]; // Change the dst alignment as we change the source. for (size_t i = 0; i < 16; i++) {
uint8_t* dst = &dst_buffer[i]; for (size_t j = 0; j < pagesize; j++) {
uint8_t* src = &memory[pagesize-j];
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(MprotectHeap(&memory1[pagesize], pagesize, PROT_NONE) == 0);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.