mirror of
https://github.com/Zenithsiz/ftmemsim-valgrind.git
synced 2026-02-04 02:18:37 +00:00
overview
-----------------------------------------------------------------------------
Previously Valgrind had its own versions of malloc() et al that replaced
glibc's. This is necessary for various reasons for Memcheck, but isn't needed,
and was actually detrimental, to some other skins. I never managed to treat
this satisfactorily w.r.t the core/skin split.
Now I have. If a skin needs to know about malloc() et al, it must provide its
own replacements. But because this is not uncommon, the core provides a module
vg_replace_malloc.c which a skin can link with, which provides skeleton
definitions, to reduce the amount of work a skin must do. The skeletons handle
the transfer of control from the simd CPU to the real CPU, and also the
--alignment, --sloppy-malloc and --trace-malloc options. These skeleton
definitions subsequently call functions SK_(malloc), SK_(free), etc, which the
skin must define; in these functions the skin can do the things it needs to do
about tracking heap blocks.
For skins that track extra info about malloc'd blocks -- previously done with
ShadowChunks -- there is a new file vg_hashtable.c that implements a
generic-ish hash table (using dodgy C-style inheritance using struct overlays)
which allows skins to continue doing this fairly easily.
Skins can also replace other functions too, eg. Memcheck has its own versions
of strcpy(), memcpy(), etc.
Overall, it's slightly more work now for skins that need to replace malloc(),
but other skins don't have to use Valgrind's malloc(), so they're getting a
"purer" program run, which is good, and most of the remaining rough edges from
the core/skin split have been removed.
-----------------------------------------------------------------------------
details
-----------------------------------------------------------------------------
Moved malloc() et al intercepts from vg_clientfuncs.c into vg_replace_malloc.c.
Skins can link to it if they want to replace malloc() and friends; it does
some stuff then passes control to SK_(malloc)() et al which the skin must
define. They can call VG_(cli_malloc)() and VG_(cli_free)() to do the actual
allocation/deallocation. Redzone size for the client (the CLIENT arena) is
specified by the static variable VG_(vg_malloc_redzone_szB).
vg_replace_malloc.c thus represents a kind of "mantle" level service.
To get automake to build vg_replace_malloc.o, had to resort to a similar trick
as used for the demangler -- ask for a "no install" library (which is never
used) to be built from it.
Note that all malloc, calloc, realloc, builtin_new, builtin_vec_new, memalign
are now aware of --alignment, when running on simd CPU or real CPU.
This means the new_mem_heap, die_mem_heap, copy_mem_heap and ban_mem_heap
events no longer exist, since the core doesn't control malloc() any more, and
skins can watch for these events themselves.
This required moving all the ShadowChunk stuff out of the core, which meant
the sizeof_shadow_block ``need'' could be removed, yay -- it was a horrible
hack. Now ShadowChunks are done with a generic HashTable type, in
vg_hashtable.c, which skins can "inherit from" (in a dodgy C-only fashion by
using structs with similar layouts). Also, the free_list stuff was all moved
as a part of this. Also, VgAllocKind was moved out of core into
Memcheck/Addrcheck and renamed MAC_AllocKind.
Moved these options out of core into vg_replace_malloc.c:
--trace-malloc
--sloppy-malloc
--alignment
The alternative_free ``need'' could go, too, since Memcheck is now in complete
control of free(), yay -- another horribility.
The bad_free and free_mismatch events could go too, since they're now not
detected by core, yay -- yet another horribility.
Moved malloc() et al wrappers for Memcheck out of vg_clientmalloc.c into
mac_malloc_wrappers.c. Helgrind has its own wrappers now too.
Introduced VG_USERREQ__CLIENT_CALL[123] client requests. When a skin function
is operating on the simd CPU, this will call a given function and run it on the
real CPU. The macros VG_NON_SIMD_CALL[123] in valgrind.h present a cleaner
interface to actually use. Also introduce analogues of these that pass 'tst'
from the scheduler as the first arg to the called function -- needed for
MC_(client_malloc)() et al.
Fiddled with USERREQ_{MALLOC,FREE} etc. in vg_scheduler.c; they call
SK_({malloc,free})() which by default call VG_(cli_malloc)() -- can't call
glibc's malloc() here. All the other default SK_(calloc)() etc. instantly
panic; there's a lock variable to ensure that the default SK_({malloc,free})()
are only called from the scheduler, which prevents a skin from forgetting to
override SK_({malloc,free})(). Got rid of the unused USERREQ_CALLOC,
USERREQ_BUILTIN_NEW, etc.
Moved special versions of strcpy/strlen, etc, memcpy() and memchr() into
mac_replace_strmem.c -- they are only necessary for memcheck, because the
hyper-optimised normal glibc versions confuse it, and for memcpy() etc. overlap
checking.
Also added dst/src overlap checks to strcpy(), memcpy(), strcat(). They are
reported not as proper errors, but just with single line warnings, as for silly
args to malloc() et al; this is mainly because they're on the simulated CPU
and proper error handling would be a pain; hopefully they're rare enough to
not be a problem. The strcpy check is done after the copy, because it would
require counting the length of the string beforehand. Also added strncpy() and
strncat(), which have overlap checks too. Note that addrcheck doesn't do
overlap checking.
Put USERREQ__LOGMESSAGE in vg_skin.h to do the overlap check error messages.
After removing malloc() et al and strcpy() et al out of vg_clientfuncs.c, moved
the remaining three things (sigsuspend, VG_(__libc_freeres_wrapper),
__errno_location) into vg_intercept.c, since it contains things that run on the
simulated CPU too. Removed vg_clientfuncs.c altogether.
Moved regression test "malloc3" out of corecheck into memcheck, since corecheck
no longer looks for silly (eg. negative) args to malloc().
Removed the m_eip, m_esp, m_ebp fields from the `Error' type. They were being
set up, and then read immediately only once, only if GDB attachment was done.
So now they're just being held in local variables. This saves 12 bytes per
Error.
Made replacement calloc() check for --sloppy-malloc; previously it didn't.
Added "silly" negative size arg check to realloc(), it didn't have one.
Changed VG_(read_selfprocmaps)() so it can parse the file directly, or from a
previously read buffer. Buffer can be filled with the new
VG_(read_selfprocmaps_contents)(). Using this at start-up to snapshot
/proc/self/maps before the skins do anything, and then parsing it once they
have done their setup stuff. Skins can now safely call VG_(malloc)() in
SK_({pre,post}_clo_init)() without the mmap'd superblock erroneously being
identified as client memory.
Changed the --help usage message slightly, now divided into four sections: core
normal, skin normal, core debugging, skin debugging. Changed the interface for
the command_line_options need slightly -- now two functions, VG_(print_usage)()
and VG_(print_debug_usage)(), and they do the printing themselves, instead of
just returning a string -- that's more flexible.
Removed DEBUG_CLIENTMALLOC code, it wasn't being used and was a pain.
Added a regression test testing leak suppressions (nanoleak_supp), and another
testing strcpy/memcpy/etc overlap warnings (overlap).
Also changed Addrcheck to link with the files shared with Memcheck, rather than
#including the .c files directly.
Commoned up a little more shared Addrcheck/Memcheck code, for the usage
message, and initialisation/finalisation.
Added a Bool param to VG_(unique_error)() dictating whether it should allow
GDB to be attached; for leak checks, because we don't want to attach GDB on
leak errors (causes seg faults). A bit hacky, but it will do.
Had to change lots of the expected outputs from regression files now that
malloc() et al are in vg_replace_malloc.c rather than vg_clientfuncs.c.
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@1524
275 lines
13 KiB
C
275 lines
13 KiB
C
|
|
/*
|
|
----------------------------------------------------------------
|
|
|
|
Notice that the following BSD-style license applies to this one
|
|
file (valgrind.h) only. The entire rest of Valgrind is licensed
|
|
under the terms of the GNU General Public License, version 2. See
|
|
the COPYING file in the source distribution for details.
|
|
|
|
----------------------------------------------------------------
|
|
|
|
This file is part of Valgrind, an extensible x86 protected-mode
|
|
emulator for monitoring program execution on x86-Unixes.
|
|
|
|
Copyright (C) 2000-2002 Julian Seward. All rights reserved.
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
modification, are permitted provided that the following conditions
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. The origin of this software must not be misrepresented; you must
|
|
not claim that you wrote the original software. If you use this
|
|
software in a product, an acknowledgment in the product
|
|
documentation would be appreciated but is not required.
|
|
|
|
3. Altered source versions must be plainly marked as such, and must
|
|
not be misrepresented as being the original software.
|
|
|
|
4. The name of the author may not be used to endorse or promote
|
|
products derived from this software without specific prior written
|
|
permission.
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
----------------------------------------------------------------
|
|
|
|
Notice that the above BSD-style license applies to this one file
|
|
(valgrind.h) only. The entire rest of Valgrind is licensed under
|
|
the terms of the GNU General Public License, version 2. See the
|
|
COPYING file in the source distribution for details.
|
|
|
|
----------------------------------------------------------------
|
|
*/
|
|
|
|
|
|
#ifndef __VALGRIND_H
|
|
#define __VALGRIND_H
|
|
|
|
|
|
#ifndef __VALGRIND_SOMESKIN_H
|
|
#warning For valgrind versions 1.9.0 and after,
|
|
#warning you should not include valgrind.h directly.
|
|
#warning Instead include the .h relevant to the skin
|
|
#warning you want to use. For most people this means
|
|
#warning you need to include memcheck.h instead of
|
|
#warning valgrind.h.
|
|
#error Compilation of your source will now abort.
|
|
#endif
|
|
|
|
|
|
/* This file is for inclusion into client (your!) code.
|
|
|
|
You can use these macros to manipulate and query Valgrind's
|
|
execution inside your own programs.
|
|
|
|
The resulting executables will still run without Valgrind, just a
|
|
little bit more slowly than they otherwise would, but otherwise
|
|
unchanged. When not running on valgrind, each client request
|
|
consumes about 9 x86 instructions, so the resulting performance
|
|
loss is negligible unless you plan to execute client requests
|
|
millions of times per second. Nevertheless, if that is still a
|
|
problem, you can compile with the NVALGRIND symbol defined (gcc
|
|
-DNVALGRIND) so that client requests are not even compiled in. */
|
|
|
|
|
|
|
|
#ifndef NVALGRIND
|
|
/* This defines the magic code sequence which the JITter spots and
|
|
handles magically. Don't look too closely at this; it will rot
|
|
your brain. Valgrind dumps the result value in %EDX, so we first
|
|
copy the default value there, so that it is returned when not
|
|
running on Valgrind. Since %EAX points to a block of mem
|
|
containing the args, you can pass as many args as you want like
|
|
this. Currently this is set up to deal with 4 args since that's
|
|
the max that we appear to need (pthread_create).
|
|
*/
|
|
#define VALGRIND_MAGIC_SEQUENCE( \
|
|
_zzq_rlval, /* result lvalue */ \
|
|
_zzq_default, /* result returned when running on real CPU */ \
|
|
_zzq_request, /* request code */ \
|
|
_zzq_arg1, /* request first param */ \
|
|
_zzq_arg2, /* request second param */ \
|
|
_zzq_arg3, /* request third param */ \
|
|
_zzq_arg4 /* request fourth param */ ) \
|
|
\
|
|
{ volatile unsigned int _zzq_args[5]; \
|
|
_zzq_args[0] = (volatile unsigned int)(_zzq_request); \
|
|
_zzq_args[1] = (volatile unsigned int)(_zzq_arg1); \
|
|
_zzq_args[2] = (volatile unsigned int)(_zzq_arg2); \
|
|
_zzq_args[3] = (volatile unsigned int)(_zzq_arg3); \
|
|
_zzq_args[4] = (volatile unsigned int)(_zzq_arg4); \
|
|
asm volatile("movl %1, %%eax\n\t" \
|
|
"movl %2, %%edx\n\t" \
|
|
"roll $29, %%eax ; roll $3, %%eax\n\t" \
|
|
"rorl $27, %%eax ; rorl $5, %%eax\n\t" \
|
|
"roll $13, %%eax ; roll $19, %%eax\n\t" \
|
|
"movl %%edx, %0\t" \
|
|
: "=r" (_zzq_rlval) \
|
|
: "r" (&_zzq_args[0]), "r" (_zzq_default) \
|
|
: "eax", "edx", "cc", "memory" \
|
|
); \
|
|
}
|
|
#else /* NVALGRIND */
|
|
/* Define NVALGRIND to completely remove the Valgrind magic sequence
|
|
from the compiled code (analogous to NDEBUG's effects on
|
|
assert()) */
|
|
#define VALGRIND_MAGIC_SEQUENCE( \
|
|
_zzq_rlval, /* result lvalue */ \
|
|
_zzq_default, /* result returned when running on real CPU */ \
|
|
_zzq_request, /* request code */ \
|
|
_zzq_arg1, /* request first param */ \
|
|
_zzq_arg2, /* request second param */ \
|
|
_zzq_arg3, /* request third param */ \
|
|
_zzq_arg4 /* request fourth param */ ) \
|
|
{ \
|
|
(_zzq_rlval) = (_zzq_default); \
|
|
}
|
|
#endif /* NVALGRIND */
|
|
|
|
/* Some request codes. There are many more of these, but most are not
|
|
exposed to end-user view. These are the public ones, all of the
|
|
form 0x1000 + small_number.
|
|
*/
|
|
|
|
#define VG_USERREQ_SKIN_BASE(a,b) ((unsigned int)(((a)&0xff) << 24 | ((b)&0xff) << 16))
|
|
#define VG_IS_SKIN_USERREQ(a, b, v) (VG_USERREQ_SKIN_BASE(a,b) == ((v) & 0xffff0000))
|
|
|
|
typedef
|
|
enum { VG_USERREQ__RUNNING_ON_VALGRIND = 0x1001,
|
|
VG_USERREQ__DISCARD_TRANSLATIONS,
|
|
|
|
/* These allow any function of 0--3 args to be called from the
|
|
simulated CPU but run on the real CPU */
|
|
VG_USERREQ__CLIENT_CALL0 = 0x1100,
|
|
VG_USERREQ__CLIENT_CALL1,
|
|
VG_USERREQ__CLIENT_CALL2,
|
|
VG_USERREQ__CLIENT_CALL3,
|
|
|
|
/* As above, but a pointer to the current ThreadState is inserted
|
|
as the first arg. */
|
|
VG_USERREQ__CLIENT_tstCALL0 = 0x1200,
|
|
VG_USERREQ__CLIENT_tstCALL1,
|
|
VG_USERREQ__CLIENT_tstCALL2,
|
|
VG_USERREQ__CLIENT_tstCALL3,
|
|
|
|
VG_USERREQ__FINAL_DUMMY_CLIENT_REQUEST
|
|
} Vg_ClientRequest;
|
|
|
|
|
|
/* Returns 1 if running on Valgrind, 0 if running on the real CPU.
|
|
Currently implemented but untested. */
|
|
#define RUNNING_ON_VALGRIND \
|
|
({unsigned int _qzz_res; \
|
|
VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* returned if not */, \
|
|
VG_USERREQ__RUNNING_ON_VALGRIND, \
|
|
0, 0, 0, 0); \
|
|
_qzz_res; \
|
|
})
|
|
|
|
|
|
/* Discard translation of code in the range [_qzz_addr .. _qzz_addr +
|
|
_qzz_len - 1]. Useful if you are debugging a JITter or some such,
|
|
since it provides a way to make sure valgrind will retranslate the
|
|
invalidated area. Returns no value. */
|
|
#define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len) \
|
|
{unsigned int _qzz_res; \
|
|
VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
|
|
VG_USERREQ__DISCARD_TRANSLATIONS, \
|
|
_qzz_addr, _qzz_len, 0, 0); \
|
|
}
|
|
|
|
|
|
/* These requests allow control to move from the simulated CPU to the
|
|
real CPU, calling an arbitary function */
|
|
#define VG_NON_SIMD_CALL0(_qyy_fn) \
|
|
({unsigned int _qyy_res; \
|
|
VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
|
|
VG_USERREQ__CLIENT_CALL0, \
|
|
_qyy_fn, \
|
|
0, 0, 0); \
|
|
_qyy_res; \
|
|
})
|
|
|
|
#define VG_NON_SIMD_CALL1(_qyy_fn, _qyy_arg1) \
|
|
({unsigned int _qyy_res; \
|
|
VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
|
|
VG_USERREQ__CLIENT_CALL1, \
|
|
_qyy_fn, \
|
|
_qyy_arg1, 0, 0); \
|
|
_qyy_res; \
|
|
})
|
|
|
|
#define VG_NON_SIMD_CALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \
|
|
({unsigned int _qyy_res; \
|
|
VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
|
|
VG_USERREQ__CLIENT_CALL2, \
|
|
_qyy_fn, \
|
|
_qyy_arg1, _qyy_arg2, 0); \
|
|
_qyy_res; \
|
|
})
|
|
|
|
#define VG_NON_SIMD_CALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \
|
|
({unsigned int _qyy_res; \
|
|
VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
|
|
VG_USERREQ__CLIENT_CALL3, \
|
|
_qyy_fn, \
|
|
_qyy_arg1, _qyy_arg2, _qyy_arg3); \
|
|
_qyy_res; \
|
|
})
|
|
|
|
|
|
/* These requests are similar to those above; they insert the current
|
|
ThreadState as the first argument to the called function. */
|
|
#define VG_NON_SIMD_tstCALL0(_qyy_fn) \
|
|
({unsigned int _qyy_res; \
|
|
VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
|
|
VG_USERREQ__CLIENT_tstCALL0, \
|
|
_qyy_fn, \
|
|
0, 0, 0); \
|
|
_qyy_res; \
|
|
})
|
|
|
|
#define VG_NON_SIMD_tstCALL1(_qyy_fn, _qyy_arg1) \
|
|
({unsigned int _qyy_res; \
|
|
VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
|
|
VG_USERREQ__CLIENT_tstCALL1, \
|
|
_qyy_fn, \
|
|
_qyy_arg1, 0, 0); \
|
|
_qyy_res; \
|
|
})
|
|
|
|
#define VG_NON_SIMD_tstCALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \
|
|
({unsigned int _qyy_res; \
|
|
VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
|
|
VG_USERREQ__CLIENT_tstCALL2, \
|
|
_qyy_fn, \
|
|
_qyy_arg1, _qyy_arg2, 0); \
|
|
_qyy_res; \
|
|
})
|
|
|
|
#define VG_NON_SIMD_tstCALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \
|
|
({unsigned int _qyy_res; \
|
|
VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
|
|
VG_USERREQ__CLIENT_tstCALL3, \
|
|
_qyy_fn, \
|
|
_qyy_arg1, _qyy_arg2, _qyy_arg3); \
|
|
_qyy_res; \
|
|
})
|
|
|
|
|
|
#endif /* __VALGRIND_H */
|