]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - tests/buffer_tests.cpp
Merge "Remove bogus transitive includes."
[android-sdk/platform-bionic.git] / tests / buffer_tests.cpp
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  */
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/mman.h>
21 #include <gtest/gtest.h>
22 #include "buffer_tests.h"
24 // For the comparison buffer tests, the maximum length to test for the
25 // miscompare checks.
26 #define MISCMP_MAX_LENGTH 512
28 #define FENCEPOST_LENGTH 8
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 },
41   // General unaligned cases.
42   { 4, 1 },
43   { 4, 2 },
44   { 4, 3 },
46   { 8, 1 },
47   { 8, 2 },
48   { 8, 3 },
49   { 8, 4 },
50   { 8, 5 },
51   { 8, 6 },
52   { 8, 7 },
54   { 128, 1 },
55   { 128, 4 },
56   { 128, 8 },
57   { 128, 12 },
58   { 128, 16 },
59 };
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
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 },
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 },
82   // General unaligned cases.
83   { 4, 0, 4, 1 },
84   { 4, 0, 4, 2 },
85   { 4, 0, 4, 3 },
87   { 4, 1, 4, 0 },
88   { 4, 1, 4, 1 },
89   { 4, 1, 4, 2 },
90   { 4, 1, 4, 3 },
92   { 4, 2, 4, 0 },
93   { 4, 2, 4, 1 },
94   { 4, 2, 4, 2 },
95   { 4, 2, 4, 3 },
97   { 4, 3, 4, 0 },
98   { 4, 3, 4, 1 },
99   { 4, 3, 4, 2 },
100   { 4, 3, 4, 3 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 };
183 static const size_t g_double_aligns_len = sizeof(g_double_aligns)/sizeof(int[4]);
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;
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   }
205   return reinterpret_cast<void*>(ptr);
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   }
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   }
230 void RunSingleBufferAlignTest(
231     size_t max_test_size, void (*test_func)(uint8_t*, size_t),
232     size_t (*set_incr)(size_t)) {
233   if (!set_incr) {
234     set_incr = SetIncrement;
235   }
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];
241   uint8_t *buf_align;
242   for (size_t i = 0; i < g_single_aligns_len; i++) {
243     size_t incr = 1;
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]);
257     }
258   }
259   delete buf;
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)) {
265   if (!set_incr) {
266     set_incr = SetIncrement;
267   }
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];
273   uint8_t* src_align;
274   uint8_t* dst_align;
275   for (size_t i = 0; i < g_double_aligns_len; i++) {
276     size_t incr = 1;
277     for (size_t len = 0; len <= max_test_size; len += incr) {
278       incr = set_incr(len);
280       src_align =
281           reinterpret_cast<uint8_t*>(GetAlignedPtr(
282               src+FENCEPOST_LENGTH, g_double_aligns[i][0], g_double_aligns[i][1]));
283       dst_align =
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]);
293     }
294   }
295   delete src;
296   delete dst;
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)) {
303   if (!set_incr) {
304     set_incr = SetIncrement;
305   }
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];
311   uint8_t* buf1_align;
312   uint8_t* buf2_align;
313   for (size_t i = 0; i < g_double_aligns_len; i++) {
314     size_t incr = 1;
315     for (size_t len = 0; len <= max_test_size; len += incr) {
316       incr = set_incr(len);
318       buf1_align =
319           reinterpret_cast<uint8_t*>(GetAlignedPtr(
320               buf1, g_double_aligns[i][0], g_double_aligns[i][1]));
321       buf2_align =
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;
334       }
335       test_cmp_func(buf1_align, buf2_align, len);
337       if (len > 0) {
338         // Change the lengths of the buffers and verify that there are
339         // miscompares.
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);
343         }
344       }
345     }
346   }
347   delete buf1;
348   delete buf2;
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));
355   uint8_t* memory;
356   ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory), pagesize,
357                              2*pagesize) == 0);
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];
366     test_func(buf, i);
367   }
368   ASSERT_TRUE(mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0);
369   free(memory);
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));
376   uint8_t* memory;
377   ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory), pagesize,
378                              2*pagesize) == 0);
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 = new uint8_t[pagesize];
385   for (size_t i = 0; i < pagesize; i++) {
386     uint8_t* src = &memory[pagesize-i];
388     test_func(src, dst, i);
389   }
390   ASSERT_TRUE(mprotect(&memory[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0);
391   free(memory);
392   delete dst;
395 void RunCmpBufferOverreadTest(
396     void (*test_cmp_func)(uint8_t*, uint8_t*, size_t),
397     void (*test_miscmp_func)(uint8_t*, uint8_t*, size_t, size_t)) {
398   // In order to verify that functions are not reading past the end of either
399   // of the bufs, create both buffers that end exactly at an unreadable memory
400   // boundary.
401   size_t pagesize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));
402   uint8_t* memory1;
403   ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory1), pagesize,
404                              2*pagesize) == 0);
405   memset(memory1, 0x23, 2*pagesize);
407   // Make the second page unreadable and unwritable.
408   ASSERT_TRUE(mprotect(&memory1[pagesize], pagesize, PROT_NONE) == 0);
410   uint8_t* memory2;
411   ASSERT_TRUE(posix_memalign(reinterpret_cast<void**>(&memory2), pagesize,
412                              2*pagesize) == 0);
413   memset(memory2, 0x23, 2*pagesize);
415   // Make the second page unreadable and unwritable.
416   ASSERT_TRUE(mprotect(&memory2[pagesize], pagesize, PROT_NONE) == 0);
418   for (size_t i = 0; i < pagesize; i++) {
419     uint8_t* buf1 = &memory1[pagesize-i];
420     uint8_t* buf2 = &memory2[pagesize-i];
422     test_cmp_func(buf1, buf2, i);
423   }
425   // Don't cycle through pagesize, MISCMP_MAX_LENGTH bytes should be good.
426   size_t miscmp_len;
427   if (pagesize > MISCMP_MAX_LENGTH) {
428     miscmp_len = MISCMP_MAX_LENGTH;
429   } else {
430     miscmp_len = pagesize;
431   }
432   for (size_t i = 1; i < miscmp_len; i++) {
433     uint8_t* buf1 = &memory1[pagesize-i];
434     for (size_t j = 1; j < miscmp_len; j++) {
435       if (j == i)
436         continue;
438       uint8_t* buf2 = &memory2[pagesize-j];
440       test_miscmp_func(buf1, buf2, i, j);
441     }
442   }
444   ASSERT_TRUE(mprotect(&memory1[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0);
445   ASSERT_TRUE(mprotect(&memory2[pagesize], pagesize, PROT_READ | PROT_WRITE) == 0);
446   free(memory1);
447   free(memory2);