summaryrefslogtreecommitdiff
path: root/vg_clientfuncs.c
blob: e27fa30f96708b0b680afef2233e0fa184531d8a (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464

/*--------------------------------------------------------------------*/
/*--- Code which runs on the simulated CPU.                        ---*/
/*---                                             vg_clientfuncs.c ---*/
/*--------------------------------------------------------------------*/

/*
   This file is part of Valgrind, an x86 protected-mode emulator 
   designed for debugging and profiling binaries on x86-Unixes.

   Copyright (C) 2000-2002 Julian Seward 
      jseward@acm.org

   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., 59 Temple Place, Suite 330, Boston, MA
   02111-1307, USA.

   The GNU General Public License is contained in the file LICENSE.
*/

#include "vg_include.h"
#include "vg_constants.h"

#include "valgrind.h"   /* for VALGRIND_MAGIC_SEQUENCE */


/* ---------------------------------------------------------------------
   All the code in this file runs on the SIMULATED CPU.  It is
   intended for various reasons as drop-in replacements for libc
   functions.  These functions have global visibility (obviously) and
   have no prototypes in vg_include.h, since they are not intended to
   be called from within Valgrind.
   ------------------------------------------------------------------ */

/* ---------------------------------------------------------------------
   Intercepts for the GNU malloc interface.
   ------------------------------------------------------------------ */

#define SIMPLE_REQUEST1(_qyy_request, _qyy_arg1)                 \
   ({unsigned int _qyy_res;                                      \
    VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */,    \
                            _qyy_request,                        \
                            _qyy_arg1, 0, 0, 0);                 \
    _qyy_res;                                                    \
   })

#define SIMPLE_REQUEST2(_qyy_request, _qyy_arg1, _qyy_arg2)      \
   ({unsigned int _qyy_res;                                      \
    VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */,    \
                            _qyy_request,                        \
                            _qyy_arg1, _qyy_arg2, 0, 0);         \
    _qyy_res;                                                    \
   })


/* Below are new versions of malloc, __builtin_new, free, 
   __builtin_delete, calloc and realloc.

   malloc, __builtin_new, free, __builtin_delete, calloc and realloc
   can be entered either on the real CPU or the simulated one.  If on
   the real one, this is because the dynamic linker is running the
   static initialisers for C++, before starting up Valgrind itself.
   In this case it is safe to route calls through to
   VG_(malloc)/vg_free, since that is self-initialising.

   Once Valgrind is initialised, vg_running_on_simd_CPU becomes True.
   The call needs to be transferred from the simulated CPU back to the
   real one and routed to the vg_client_* functions.  To do that, the
   client-request mechanism (in valgrind.h) is used to convey requests
   to the scheduler.
*/

/* ALL calls to malloc wind up here. */
void* malloc ( Int n )
{
   void* v;

   if (VG_(clo_trace_malloc))
      VG_(printf)("malloc[simd=%d](%d)", 
                  (UInt)VG_(running_on_simd_CPU), n );
   if (n < 0) {
      v = NULL;
      VG_(message)(Vg_UserMsg, 
         "Warning: silly arg (%d) to malloc()", n );
   } else {
      if (VG_(clo_sloppy_malloc)) { while ((n % 4) > 0) n++; }

      if (VG_(running_on_simd_CPU)) {
         v = (void*)SIMPLE_REQUEST1(VG_USERREQ__MALLOC, n);
      } else {
         v = VG_(malloc)(VG_AR_CLIENT, n);
      }
   }
   if (VG_(clo_trace_malloc)) 
      VG_(printf)(" = %p\n", v );
   return (void*)v;
}


void* __builtin_new ( Int n )
{
   void* v;

   if (VG_(clo_trace_malloc))
      VG_(printf)("__builtin_new[simd=%d](%d)", 
                  (UInt)VG_(running_on_simd_CPU), n );
   if (n < 0) {
      v = NULL;
      VG_(message)(Vg_UserMsg, 
         "Warning: silly arg (%d) to __builtin_new()", n );
   } else {
      if (VG_(clo_sloppy_malloc)) { while ((n % 4) > 0) n++; }

      if (VG_(running_on_simd_CPU)) {
         v = (void*)SIMPLE_REQUEST1(VG_USERREQ__BUILTIN_NEW, n);
      } else {
         v = VG_(malloc)(VG_AR_CLIENT, n);
      }
   }
   if (VG_(clo_trace_malloc)) 
      VG_(printf)(" = %p\n", v );
   return v;
}


void* __builtin_vec_new ( Int n )
{
   void* v;

   if (VG_(clo_trace_malloc))
      VG_(printf)("__builtin_vec_new[simd=%d](%d)", 
                  (UInt)VG_(running_on_simd_CPU), n );
   if (n < 0) {
      v = NULL;
      VG_(message)(Vg_UserMsg, 
         "Warning: silly arg (%d) to __builtin_vec_new()", n );
   } else {
      if (VG_(clo_sloppy_malloc)) { while ((n % 4) > 0) n++; }

      if (VG_(running_on_simd_CPU)) {
         v = (void*)SIMPLE_REQUEST1(VG_USERREQ__BUILTIN_VEC_NEW, n);
      } else {
         v = VG_(malloc)(VG_AR_CLIENT, n);
      }
   }
   if (VG_(clo_trace_malloc)) 
      VG_(printf)(" = %p\n", v );
   return v;
}


void free ( void* p )
{
   if (VG_(clo_trace_malloc))
      VG_(printf)("free[simd=%d](%p)\n", 
                  (UInt)VG_(running_on_simd_CPU), p );
   if (p == NULL) 
      return;
   if (VG_(running_on_simd_CPU)) {
      (void)SIMPLE_REQUEST1(VG_USERREQ__FREE, p);
   } else {
      VG_(free)(VG_AR_CLIENT, p);      
   }
}


void __builtin_delete ( void* p )
{
   if (VG_(clo_trace_malloc))
      VG_(printf)("__builtin_delete[simd=%d](%p)\n", 
                  (UInt)VG_(running_on_simd_CPU), p );
   if (p == NULL) 
      return;
   if (VG_(running_on_simd_CPU)) {
      (void)SIMPLE_REQUEST1(VG_USERREQ__BUILTIN_DELETE, p);
   } else {
      VG_(free)(VG_AR_CLIENT, p);
   }
}


void __builtin_vec_delete ( void* p )
{
   if (VG_(clo_trace_malloc))
       VG_(printf)("__builtin_vec_delete[simd=%d](%p)\n", 
                   (UInt)VG_(running_on_simd_CPU), p );
   if (p == NULL) 
      return;
   if (VG_(running_on_simd_CPU)) {
      (void)SIMPLE_REQUEST1(VG_USERREQ__BUILTIN_VEC_DELETE, p);
   } else {
      VG_(free)(VG_AR_CLIENT, p);
   }
}


void* calloc ( Int nmemb, Int size )
{
   void* v;

   if (VG_(clo_trace_malloc))
      VG_(printf)("calloc[simd=%d](%d,%d)", 
                  (UInt)VG_(running_on_simd_CPU), nmemb, size );
   if (nmemb < 0 || size < 0) {
      v = NULL;
      VG_(message)(Vg_UserMsg, "Warning: silly args (%d,%d) to calloc()", 
                               nmemb, size );
   } else {
      if (VG_(running_on_simd_CPU)) {
         v = (void*)SIMPLE_REQUEST2(VG_USERREQ__CALLOC, nmemb, size);
      } else {
         v = VG_(calloc)(VG_AR_CLIENT, nmemb, size);
      }
   }
   if (VG_(clo_trace_malloc)) 
      VG_(printf)(" = %p\n", v );
   return v;
}


void* realloc ( void* ptrV, Int new_size )
{
   void* v;

   if (VG_(clo_trace_malloc))
      VG_(printf)("realloc[simd=%d](%p,%d)", 
                  (UInt)VG_(running_on_simd_CPU), ptrV, new_size );

   if (VG_(clo_sloppy_malloc)) 
      { while ((new_size % 4) > 0) new_size++; }

   if (ptrV == NULL)
      return malloc(new_size);
   if (new_size <= 0) {
      free(ptrV);
      if (VG_(clo_trace_malloc)) 
         VG_(printf)(" = 0\n" );
      return NULL;
   }   
   if (VG_(running_on_simd_CPU)) {
      v = (void*)SIMPLE_REQUEST2(VG_USERREQ__REALLOC, ptrV, new_size);
   } else {
      v = VG_(realloc)(VG_AR_CLIENT, ptrV, new_size);
   }
   if (VG_(clo_trace_malloc)) 
      VG_(printf)(" = %p\n", v );
   return v;
}


void* memalign ( Int alignment, Int n )
{
   void* v;

   if (VG_(clo_trace_malloc))
      VG_(printf)("memalign[simd=%d](al %d, size %d)", 
                  (UInt)VG_(running_on_simd_CPU), alignment, n );
   if (n < 0) {
      v = NULL;
   } else {
      if (VG_(clo_sloppy_malloc)) { while ((n % 4) > 0) n++; }

      if (VG_(running_on_simd_CPU)) {
         v = (void*)SIMPLE_REQUEST2(VG_USERREQ__MEMALIGN, alignment, n);
      } else {
         v = VG_(malloc_aligned)(VG_AR_CLIENT, alignment, n);
      }
   }
   if (VG_(clo_trace_malloc)) 
      VG_(printf)(" = %p\n", v );
   return (void*)v;
}


void* valloc ( Int size )
{
   return memalign(VKI_BYTES_PER_PAGE, size);
}


/* Various compatibility wrapper functions, for glibc and libstdc++. */
void cfree ( void* p )
{
   free ( p );
}


int mallopt ( int cmd, int value )
{
   /* In glibc-2.2.4, 1 denotes a successful return value for mallopt */
   return 1;
}


int __posix_memalign ( void **memptr, UInt alignment, UInt size )
{
    void *mem;

    /* Test whether the SIZE argument is valid.  It must be a power of
       two multiple of sizeof (void *).  */
    if (size % sizeof (void *) != 0 || (size & (size - 1)) != 0)
       return VKI_EINVAL /*22*/ /*EINVAL*/;

    mem = memalign (alignment, size);

    if (mem != NULL) {
       *memptr = mem;
       return 0;
    }

    return VKI_ENOMEM /*12*/ /*ENOMEM*/;
}


/* Bomb out if we get any of these. */
/* HACK: We shouldn't call VG_(panic) or VG_(message) on the simulated
   CPU.  Really we should pass the request in the usual way, and
   Valgrind itself can do the panic.  Too tedious, however.  
*/
void pvalloc ( void )
{ VG_(panic)("call to pvalloc\n"); }
void malloc_stats ( void )
{ VG_(panic)("call to malloc_stats\n"); }
void malloc_usable_size ( void )
{ VG_(panic)("call to malloc_usable_size\n"); }
void malloc_trim ( void )
{ VG_(panic)("call to malloc_trim\n"); }
void malloc_get_state ( void )
{ VG_(panic)("call to malloc_get_state\n"); }
void malloc_set_state ( void )
{ VG_(panic)("call to malloc_set_state\n"); }

void* mallinfo ( void )
{ 
   VG_(message)(Vg_UserMsg, 
                "Warning: incorrectly-handled call to mallinfo()"); 
   return NULL;
}


/* ---------------------------------------------------------------------
   Replace some C lib things with equivs which don't get
   spurious value warnings.  THEY RUN ON SIMD CPU!
   ------------------------------------------------------------------ */

char* strrchr ( const char* s, int c )
{
   UChar  ch   = (UChar)((UInt)c);
   UChar* p    = (UChar*)s;
   UChar* last = NULL;
   while (True) {
      if (*p == ch) last = p;
      if (*p == 0) return last;
      p++;
   }
}

char* strchr ( const char* s, int c )
{
   UChar  ch = (UChar)((UInt)c);
   UChar* p  = (UChar*)s;
   while (True) {
      if (*p == ch) return p;
      if (*p == 0) return NULL;
      p++;
   }
}

char* strcat ( char* dest, const char* src )
{
   Char* dest_orig = dest;
   while (*dest) dest++;
   while (*src) *dest++ = *src++;
   *dest = 0;
   return dest_orig;
}

unsigned int strlen ( const char* str )
{
   UInt i = 0;
   while (str[i] != 0) i++;
   return i;
}

char* strcpy ( char* dest, const char* src )
{
   Char* dest_orig = dest;
   while (*src) *dest++ = *src++;
   *dest = 0;
   return dest_orig;
}

int strncmp ( const char* s1, const char* s2, unsigned int nmax )
{
   unsigned int n = 0;
   while (True) {
      if (n >= nmax) return 0;
      if (*s1 == 0 && *s2 == 0) return 0;
      if (*s1 == 0) return -1;
      if (*s2 == 0) return 1;

      if (*(UChar*)s1 < *(UChar*)s2) return -1;
      if (*(UChar*)s1 > *(UChar*)s2) return 1;

      s1++; s2++; n++;
   }
}

int strcmp ( const char* s1, const char* s2 )
{
   while (True) {
      if (*s1 == 0 && *s2 == 0) return 0;
      if (*s1 == 0) return -1;
      if (*s2 == 0) return 1;

      if (*(char*)s1 < *(char*)s2) return -1;
      if (*(char*)s1 > *(char*)s2) return 1;

      s1++; s2++;
   }
}

void* memchr(const void *s, int c, unsigned int n)
{
   unsigned int i;
   UChar c0 = (UChar)c;
   UChar* p = (UChar*)s;
   for (i = 0; i < n; i++)
      if (p[i] == c0) return (void*)(&p[i]);
   return NULL;
}

void* memcpy( void *dst, const void *src, unsigned int len )
{
    register char *d;
    register char *s;
    if ( dst > src ) {
        d = (char *)dst + len - 1;
        s = (char *)src + len - 1;
        while ( len-- )
            *d-- = *s--;
    } else if ( dst < src ) {
        d = (char *)dst;
        s = (char *)src;
        while ( len-- )
            *d++ = *s++;
    }
    return dst;
}

/*--------------------------------------------------------------------*/
/*--- end                                         vg_clientfuncs.c ---*/
/*--------------------------------------------------------------------*/