1 /*
2  * Copyright (C) 2014 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 <stdint.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/mman.h>
21 #include <sys/types.h>
22 
23 #include <memory>
24 
25 #include <cutils/memory.h>
26 #include <gtest/gtest.h>
27 
28 #define FENCEPOST_LENGTH 8
29 
30 #define MAX_TEST_SIZE (64*1024)
31 // Choose values that have no repeating byte values.
32 #define MEMSET16_PATTERN 0xb139
33 #define MEMSET32_PATTERN 0x48193a27
34 
35 enum test_e {
36   MEMSET16 = 0,
37   MEMSET32,
38 };
39 
40 static int g_memset16_aligns[][2] = {
41   { 2, 0 },
42   { 4, 0 },
43   { 8, 0 },
44   { 16, 0 },
45   { 32, 0 },
46   { 64, 0 },
47   { 128, 0 },
48 
49   { 4, 2 },
50 
51   { 8, 2 },
52   { 8, 4 },
53   { 8, 6 },
54 
55   { 128, 2 },
56   { 128, 4 },
57   { 128, 6 },
58   { 128, 8 },
59   { 128, 10 },
60   { 128, 12 },
61   { 128, 14 },
62   { 128, 16 },
63 };
64 
65 static int g_memset32_aligns[][2] = {
66   { 4, 0 },
67   { 8, 0 },
68   { 16, 0 },
69   { 32, 0 },
70   { 64, 0 },
71   { 128, 0 },
72 
73   { 8, 4 },
74 
75   { 128, 4 },
76   { 128, 8 },
77   { 128, 12 },
78   { 128, 16 },
79 };
80 
GetIncrement(size_t len,size_t min_incr)81 static size_t GetIncrement(size_t len, size_t min_incr) {
82   if (len >= 4096) {
83     return 1024;
84   } else if (len >= 1024) {
85     return 256;
86   }
87   return min_incr;
88 }
89 
90 // Return a pointer into the current buffer with the specified alignment.
GetAlignedPtr(void * orig_ptr,int alignment,int or_mask)91 static void *GetAlignedPtr(void *orig_ptr, int alignment, int or_mask) {
92   uint64_t ptr = reinterpret_cast<uint64_t>(orig_ptr);
93   if (alignment > 0) {
94       // When setting the alignment, set it to exactly the alignment chosen.
95       // The pointer returned will be guaranteed not to be aligned to anything
96       // more than that.
97       ptr += alignment - (ptr & (alignment - 1));
98       ptr |= alignment | or_mask;
99   }
100 
101   return reinterpret_cast<void*>(ptr);
102 }
103 
SetFencepost(uint8_t * buffer)104 static void SetFencepost(uint8_t *buffer) {
105   for (int i = 0; i < FENCEPOST_LENGTH; i += 2) {
106     buffer[i] = 0xde;
107     buffer[i+1] = 0xad;
108   }
109 }
110 
VerifyFencepost(uint8_t * buffer)111 static void VerifyFencepost(uint8_t *buffer) {
112   for (int i = 0; i < FENCEPOST_LENGTH; i += 2) {
113     if (buffer[i] != 0xde || buffer[i+1] != 0xad) {
114       uint8_t expected_value;
115       if (buffer[i] == 0xde) {
116         i++;
117         expected_value = 0xad;
118       } else {
119         expected_value = 0xde;
120       }
121       ASSERT_EQ(expected_value, buffer[i]);
122     }
123   }
124 }
125 
RunMemsetTests(test_e test_type,uint32_t value,int align[][2],size_t num_aligns)126 void RunMemsetTests(test_e test_type, uint32_t value, int align[][2], size_t num_aligns) {
127   size_t min_incr = 4;
128   if (test_type == MEMSET16) {
129     min_incr = 2;
130     value |= value << 16;
131   }
132   std::unique_ptr<uint32_t[]> expected_buf(new uint32_t[MAX_TEST_SIZE/sizeof(uint32_t)]);
133   for (size_t i = 0; i < MAX_TEST_SIZE/sizeof(uint32_t); i++) {
134     expected_buf[i] = value;
135   }
136 
137   // Allocate one large buffer with lots of extra space so that we can
138   // guarantee that all possible alignments will fit.
139   std::unique_ptr<uint8_t[]> buf(new uint8_t[3*MAX_TEST_SIZE]);
140   uint8_t *buf_align;
141   for (size_t i = 0; i < num_aligns; i++) {
142     size_t incr = min_incr;
143     for (size_t len = incr; len <= MAX_TEST_SIZE; len += incr) {
144       incr = GetIncrement(len, min_incr);
145 
146       buf_align = reinterpret_cast<uint8_t*>(GetAlignedPtr(
147           buf.get()+FENCEPOST_LENGTH, align[i][0], align[i][1]));
148 
149       SetFencepost(&buf_align[-FENCEPOST_LENGTH]);
150       SetFencepost(&buf_align[len]);
151 
152       memset(buf_align, 0xff, len);
153       if (test_type == MEMSET16) {
154         android_memset16(reinterpret_cast<uint16_t*>(buf_align), value, len);
155       } else {
156         android_memset32(reinterpret_cast<uint32_t*>(buf_align), value, len);
157       }
158       ASSERT_EQ(0, memcmp(expected_buf.get(), buf_align, len))
159           << "Failed size " << len << " align " << align[i][0] << " " << align[i][1] << "\n";
160 
161       VerifyFencepost(&buf_align[-FENCEPOST_LENGTH]);
162       VerifyFencepost(&buf_align[len]);
163     }
164   }
165 }
166 
TEST(libcutils,android_memset16_non_zero)167 TEST(libcutils, android_memset16_non_zero) {
168   RunMemsetTests(MEMSET16, MEMSET16_PATTERN, g_memset16_aligns, sizeof(g_memset16_aligns)/sizeof(int[2]));
169 }
170 
TEST(libcutils,android_memset16_zero)171 TEST(libcutils, android_memset16_zero) {
172   RunMemsetTests(MEMSET16, 0, g_memset16_aligns, sizeof(g_memset16_aligns)/sizeof(int[2]));
173 }
174 
TEST(libcutils,android_memset32_non_zero)175 TEST(libcutils, android_memset32_non_zero) {
176   RunMemsetTests(MEMSET32, MEMSET32_PATTERN, g_memset32_aligns, sizeof(g_memset32_aligns)/sizeof(int[2]));
177 }
178 
TEST(libcutils,android_memset32_zero)179 TEST(libcutils, android_memset32_zero) {
180   RunMemsetTests(MEMSET32, 0, g_memset32_aligns, sizeof(g_memset32_aligns)/sizeof(int[2]));
181 }
182