aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/libitm/testsuite/libitm.c/memset-1.c
blob: 11aea458ca1c5b4a3256a18a0a1d769cc3bcd93d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.  */

/* Verify memcpy operation.  */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <libitm.h>

#define BEG_TRANSACTION \
  _ITM_beginTransaction (pr_instrumentedCode | pr_hasNoAbort \
			 | pr_hasNoIrrevocable)
#define END_TRANSACTION \
  _ITM_commitTransaction ()

#define MEMSET _ITM_memsetW

static unsigned char *buf;
static size_t bufsize, page_size;
static int fail;

#ifndef MAP_ANONYMOUS
#  ifdef MAP_ANON
#    define MAP_ANONYMOUS MAP_ANON
#  endif
#endif

static void
do_test (size_t align, size_t len)
{
  size_t i, j;
  unsigned char c1, c2;

  if (align + len >= bufsize)
    return;

  c1 = random () >> 8;
  c2 = random () >> 8;
  if (c1 == c2)
    c1++;
  memset (buf, c1, bufsize);

  BEG_TRANSACTION;
  MEMSET (buf + align, c2, len);
  END_TRANSACTION;

  i = (align > 64 ? align - 64 : 0);
  for (; i < align; ++i)
    if (buf[i] != c1)
      {
	printf ("Garbage before: ofs %zd\n", i);
	fail = 1;
	break;
      }
  for (; i < align + len; ++i)
    if (buf[i] != c2)
      {
	printf ("Wrong result: ofs %zd\n", i);
	fail = 1;
	break;
      }
  for (j = i + 64 < bufsize ? i + 64 : bufsize; i < j; ++i)
    if (buf[i] != c1)
      {
	printf ("Garbage after: ofs %zd\n", i);
	fail = 1;
	break;
      }
}

int main()
{
  size_t i, j;

  page_size = getpagesize ();
  bufsize = 2 * page_size;

  buf = mmap (NULL, bufsize + 2*page_size, PROT_READ | PROT_WRITE,
	      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (buf == MAP_FAILED)
    return 1;

  if (mprotect (buf, page_size, PROT_NONE))
    return 1;
  buf += page_size;
  if (mprotect (buf + bufsize, page_size, PROT_NONE))
    return 1;

  for (i = 0; i < 18; ++i)
    {
      size_t len = 1 << i;
      do_test (0, len);
      do_test (bufsize - len, len);
    }

  for (i = 0; i < 32; ++i)
    for (j = 0; j < 32; ++j)
      do_test (j, i);

  for (i = 3; i < 32; ++i)
    {
      if ((i & (i - 1)) == 0)
	continue;
      do_test (0, 16 * i);
      do_test (i, 16 * i);
    }

  return fail;
}