| 1 |
/* This program is free software; you can redistribute it and/or modify |
| 2 |
it under the terms of the GNU General Public License as published by |
| 3 |
the Free Software Foundation; either version 2 of the License, or |
| 4 |
(at your option) any later version. |
| 5 |
|
| 6 |
This program is distributed in the hope that it will be useful, but |
| 7 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 9 |
General Public License for more details. |
| 10 |
|
| 11 |
You should have received a copy of the GNU General Public License |
| 12 |
along with this program; if not, write to the Free Software |
| 13 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 14 |
02110-1301, USA. */ |
| 15 |
|
| 16 |
/* Verify memcpy operation. */ |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <sys/mman.h> |
| 22 |
#include <libitm.h> |
| 23 |
|
| 24 |
#define BEG_TRANSACTION \ |
| 25 |
_ITM_beginTransaction (pr_instrumentedCode | pr_hasNoAbort \ |
| 26 |
| pr_hasNoIrrevocable) |
| 27 |
#define END_TRANSACTION \ |
| 28 |
_ITM_commitTransaction () |
| 29 |
|
| 30 |
#define MEMSET _ITM_memsetW |
| 31 |
|
| 32 |
static unsigned char *buf; |
| 33 |
static size_t bufsize, page_size; |
| 34 |
static int fail; |
| 35 |
int getpagesize (void); |
| 36 |
|
| 37 |
#ifndef MAP_ANONYMOUS |
| 38 |
# ifdef MAP_ANON |
| 39 |
# define MAP_ANONYMOUS MAP_ANON |
| 40 |
# endif |
| 41 |
#endif |
| 42 |
|
| 43 |
static void |
| 44 |
do_test (size_t align, size_t len) |
| 45 |
{ |
| 46 |
size_t i, j; |
| 47 |
unsigned char c1, c2; |
| 48 |
|
| 49 |
if (align + len >= bufsize) |
| 50 |
return; |
| 51 |
|
| 52 |
c1 = random () >> 8; |
| 53 |
c2 = random () >> 8; |
| 54 |
if (c1 == c2) |
| 55 |
c1++; |
| 56 |
memset (buf, c1, bufsize); |
| 57 |
|
| 58 |
BEG_TRANSACTION; |
| 59 |
MEMSET (buf + align, c2, len); |
| 60 |
END_TRANSACTION; |
| 61 |
|
| 62 |
i = (align > 64 ? align - 64 : 0); |
| 63 |
for (; i < align; ++i) |
| 64 |
if (buf[i] != c1) |
| 65 |
{ |
| 66 |
printf ("Garbage before: ofs %zd\n", i); |
| 67 |
fail = 1; |
| 68 |
break; |
| 69 |
} |
| 70 |
for (; i < align + len; ++i) |
| 71 |
if (buf[i] != c2) |
| 72 |
{ |
| 73 |
printf ("Wrong result: ofs %zd\n", i); |
| 74 |
fail = 1; |
| 75 |
break; |
| 76 |
} |
| 77 |
for (j = i + 64 < bufsize ? i + 64 : bufsize; i < j; ++i) |
| 78 |
if (buf[i] != c1) |
| 79 |
{ |
| 80 |
printf ("Garbage after: ofs %zd\n", i); |
| 81 |
fail = 1; |
| 82 |
break; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
int main() |
| 87 |
{ |
| 88 |
size_t i, j; |
| 89 |
|
| 90 |
page_size = getpagesize (); |
| 91 |
bufsize = 2 * page_size; |
| 92 |
|
| 93 |
buf = mmap (NULL, bufsize + 2*page_size, PROT_READ | PROT_WRITE, |
| 94 |
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 95 |
if (buf == MAP_FAILED) |
| 96 |
return 1; |
| 97 |
|
| 98 |
if (mprotect (buf, page_size, PROT_NONE)) |
| 99 |
return 1; |
| 100 |
buf += page_size; |
| 101 |
if (mprotect (buf + bufsize, page_size, PROT_NONE)) |
| 102 |
return 1; |
| 103 |
|
| 104 |
for (i = 0; i < 18; ++i) |
| 105 |
{ |
| 106 |
size_t len = 1 << i; |
| 107 |
do_test (0, len); |
| 108 |
do_test (bufsize - len, len); |
| 109 |
} |
| 110 |
|
| 111 |
for (i = 0; i < 32; ++i) |
| 112 |
for (j = 0; j < 32; ++j) |
| 113 |
do_test (j, i); |
| 114 |
|
| 115 |
for (i = 3; i < 32; ++i) |
| 116 |
{ |
| 117 |
if ((i & (i - 1)) == 0) |
| 118 |
continue; |
| 119 |
do_test (0, 16 * i); |
| 120 |
do_test (i, 16 * i); |
| 121 |
} |
| 122 |
|
| 123 |
return fail; |
| 124 |
} |