Welcome to mirror list, hosted at ThFree Co, Russian Federation.

user_malloc.c « winsup.api « testsuite « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8685f86abc995a57139432dab4729cfbf88e24af (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Test of external malloc, calloc, realloc and free capability */

#if 1
#include "test.h"
#include "usctest.h"
#else
enum {TPASS, TFAIL, TBROK, TINFO};
#define tst_resm(xxx, yyy...) printf(yyy), printf(" RES %d\n", xxx)
#define tst_brkm(xxx, yyy, zzz...) printf(zzz), printf(" RES %d\n", xxx)
#define tst_exit()
int Tst_count;
#endif

#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>

const char *TCID = "malloc";    /* Test program identifier. */
int TST_TOTAL = 2;              /* Total number of test cases. */
extern int Tst_count;           /* Test Case counter for tst_* routines */

/* Main test.
   Verbose mode if argc > 0
   Note that malloc and friends are also called by Cygwin before main,
   and that malloc can call getenv. */

int malloc_error = 0, realloc_error = 0, free_error = 0; 
int calloc_count = 0, malloc_count = 0, realloc_count = 0, free_count = 0;

void
cleanup (void)
{
  tst_exit(); 
}

int
syncwithchild (pid_t pid, int expected_exit_status)
{
  int status;

  if (waitpid (pid, &status, 0) != pid)
    {
      tst_brkm (TBROK, cleanup, "Wait for child: %s", strerror (errno));
      return 1;
    }
  if (!WIFEXITED (status))
    {
      tst_brkm (TBROK, cleanup, "Child had abnormal exit");
      return 1;
    }
  if (WEXITSTATUS (status) != expected_exit_status)
    {
      tst_brkm (TFAIL, cleanup, "Child had exit status %d != %d",
		WEXITSTATUS (status), expected_exit_status);
      return 1;
    }
  return 0;
}

#if 0
void * mallocX (size_t size);
void * callocX (size_t nmemb, size_t size);
void * reallocX (void * ptr, size_t size);
void freeX(void *);

#define malloc mallocX
#define calloc callocX
#define realloc reallocX
#define free freeX
#endif

int main (int argc, char * argv[])
{ 
  void * ptr;
  int error = 0;
  pid_t pid;

  Tst_count = 0;

  tst_resm(TINFO, "Testing if external malloc works. ppid %d", getppid());

  ptr = malloc (16);
  ptr = calloc (1, 16);
  ptr = realloc (ptr, 24);
  free (ptr);

  error = (malloc_count == 0 || calloc_count == 0 || realloc_count == 0 || free_count == 0);

  if (error || argc > 1)
    {
      printf ("malloc_count %d, calloc_count %d, realloc_count %d, free_count %d\n", 
	      malloc_count, calloc_count, realloc_count, free_count);
      printf ("malloc_error %d, realloc_error %d, free_error %d\n",
	      malloc_error, realloc_error, free_error);
    }
  tst_resm (!error ? TPASS : TFAIL, "Running in pid %d", getpid());

  /* If run from Windows, run also from Cygwin */
  if (getppid() == 1)
    {
      tst_resm(TINFO, "Ready to test if malloc works from Cygwin");

      if ((pid = fork()) == 0)
	{
	  tst_resm(TINFO, "Ready to exec with pid %d\n", getpid());
	  error = execl(argv[0], argv[0], argc > 1? argv[1]:NULL, NULL);
	  exit(error);
	}
      else if (pid < 0)
	tst_brkm (TBROK, cleanup, "Fork failed: %s", strerror (errno));
      else
	{
	  error = syncwithchild (pid, 0);
	  tst_resm (!error ? TPASS : TFAIL, "Running in pid %d", pid);
	}
    }

  tst_exit ();
}

/****************************************
Actual malloc & friends implementation 
****************************************/

typedef unsigned long long ull;
 
#define SIZE (1024*1024ULL) /* long long */ 
ull buffer[SIZE]; 
ull * current = buffer;

static int is_valid (void * ptr)
{
  unsigned int iptr = (unsigned int) ptr;
  ull * ullptr = (ull *) ptr;

  iptr = (iptr / sizeof(ull)) * sizeof(ull);
  if (iptr != (int) ptr)
    return 0;
  if (--ullptr < buffer || ullptr[0] > SIZE || ullptr  + ullptr[0]  > current)
    return 0;
  return 1;
}

void * malloc (size_t size)
{
  ull llsize = (size + 2 * sizeof (ull) - 1) / sizeof (ull);
  static char * envptr;
  void * ret;
  
  /* Make sure getenv works */
  if (!envptr)
    envptr = getenv ("PATH");

  malloc_count++;
  if (current + llsize >= buffer + SIZE) 
    {
      malloc_error++;
      errno = ENOMEM;
      return NULL;
    }
  *current = llsize;
  ret = (void *) (current + 1);
  current += llsize;

  return ret;
}

void * calloc (size_t nmemb, size_t size) 
{
  calloc_count++;
  void * ptr = malloc (nmemb * size);
  malloc_count--;
  if (ptr)
    memset(ptr, 0, nmemb * size);
  return ptr;
}

void * realloc (void * ptr, size_t size)
{
  const ull ullsize = (size + 2 * sizeof (ull) - 1) / sizeof (ull);
  ull * const ullptr = (ull *) ptr;
  void * newptr;
  
  realloc_count++;
  
  if (ptr)
    {
      if (!is_valid (ptr))
	{
	  realloc_error++;
	  errno = ENOMEM;
	  return NULL;
	}  
      if (ullptr[-1] >= ullsize)
	return ptr;
    }

  newptr = malloc (size);
  malloc_count--;
  
  if (ptr && newptr)
    memcpy (newptr, ptr, size);
  
  return newptr;
}

void free (void * x)
{
  free_count++;
  if (x && ! is_valid (x))
      free_error++;
}