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