2000-05-20 00:04:55 +08:00
|
|
|
/* mem.cpp --
|
|
|
|
|
|
|
|
This file is part of the UPX executable compressor.
|
|
|
|
|
2004-01-21 02:37:48 +08:00
|
|
|
Copyright (C) 1996-2004 Markus Franz Xaver Johannes Oberhumer
|
|
|
|
Copyright (C) 1996-2004 Laszlo Molnar
|
2000-11-13 20:22:40 +08:00
|
|
|
All Rights Reserved.
|
2000-05-20 00:04:55 +08:00
|
|
|
|
|
|
|
UPX and the UCL library are free software; you can redistribute them
|
|
|
|
and/or modify them 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; see the file COPYING.
|
|
|
|
If not, write to the Free Software Foundation, Inc.,
|
|
|
|
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
2002-07-17 00:00:58 +08:00
|
|
|
Markus F.X.J. Oberhumer Laszlo Molnar
|
|
|
|
<mfx@users.sourceforge.net> <ml1050@users.sourceforge.net>
|
2000-05-20 00:04:55 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "conf.h"
|
|
|
|
#include "mem.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
//
|
|
|
|
**************************************************************************/
|
|
|
|
|
2003-01-15 21:38:09 +08:00
|
|
|
static int use_mcheck = -1;
|
|
|
|
|
|
|
|
static int mcheck_init()
|
|
|
|
{
|
|
|
|
if (use_mcheck < 0)
|
|
|
|
{
|
|
|
|
use_mcheck = 1;
|
|
|
|
#if defined(WITH_VALGRIND) && defined(RUNNING_ON_VALGRIND)
|
|
|
|
if (RUNNING_ON_VALGRIND)
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "upx: detected RUNNING_ON_VALGRIND\n");
|
|
|
|
use_mcheck = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return use_mcheck;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
//
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
MemBuffer::MemBuffer() :
|
2003-01-28 02:54:06 +08:00
|
|
|
b(NULL), b_size(0)
|
2003-01-15 21:38:09 +08:00
|
|
|
{
|
2006-03-17 23:17:37 +08:00
|
|
|
if (use_mcheck < 0)
|
|
|
|
mcheck_init();
|
2003-01-15 21:38:09 +08:00
|
|
|
}
|
|
|
|
|
2000-09-10 14:23:47 +08:00
|
|
|
MemBuffer::MemBuffer(unsigned size) :
|
2003-01-28 02:54:06 +08:00
|
|
|
b(NULL), b_size(0)
|
2000-05-20 00:04:55 +08:00
|
|
|
{
|
2003-01-15 21:38:09 +08:00
|
|
|
alloc(size);
|
2000-05-20 00:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MemBuffer::~MemBuffer()
|
|
|
|
{
|
2001-02-25 22:02:57 +08:00
|
|
|
this->dealloc();
|
2000-05-20 00:04:55 +08:00
|
|
|
}
|
|
|
|
|
2001-02-25 22:02:57 +08:00
|
|
|
void MemBuffer::dealloc()
|
2000-05-20 00:04:55 +08:00
|
|
|
{
|
2003-01-28 02:54:06 +08:00
|
|
|
if (b)
|
2003-01-15 05:41:03 +08:00
|
|
|
{
|
|
|
|
checkState();
|
2003-01-15 21:38:09 +08:00
|
|
|
if (use_mcheck)
|
|
|
|
{
|
2003-01-28 02:54:06 +08:00
|
|
|
// remove magic constants
|
|
|
|
set_be32(b - 8, 0);
|
|
|
|
set_be32(b - 4, 0);
|
|
|
|
set_be32(b + b_size, 0);
|
|
|
|
set_be32(b + b_size + 4, 0);
|
2003-01-15 21:38:09 +08:00
|
|
|
//
|
2006-03-17 23:17:37 +08:00
|
|
|
::free(b - 16);
|
2003-01-15 21:38:09 +08:00
|
|
|
}
|
|
|
|
else
|
2003-01-28 02:54:06 +08:00
|
|
|
::free(b);
|
|
|
|
b = NULL;
|
|
|
|
b_size = 0;
|
2003-01-15 05:41:03 +08:00
|
|
|
}
|
2003-01-15 21:38:09 +08:00
|
|
|
else
|
2003-01-28 02:54:06 +08:00
|
|
|
assert(b_size == 0);
|
2000-05-20 00:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-15 05:41:03 +08:00
|
|
|
void MemBuffer::allocForCompression(unsigned uncompressed_size, unsigned extra)
|
2000-05-20 00:04:55 +08:00
|
|
|
{
|
2003-01-15 05:41:03 +08:00
|
|
|
assert((int)uncompressed_size > 0);
|
|
|
|
assert((int)extra >= 0);
|
2003-01-15 21:38:09 +08:00
|
|
|
unsigned size = uncompressed_size + uncompressed_size/8 + 256 + extra;
|
|
|
|
alloc(size);
|
2000-05-20 00:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-15 05:41:03 +08:00
|
|
|
void MemBuffer::allocForUncompression(unsigned uncompressed_size, unsigned extra)
|
2000-05-20 00:04:55 +08:00
|
|
|
{
|
2003-01-15 05:41:03 +08:00
|
|
|
assert((int)uncompressed_size > 0);
|
|
|
|
assert((int)extra >= 0);
|
2003-01-15 21:38:09 +08:00
|
|
|
unsigned size = uncompressed_size + extra;
|
|
|
|
// size += 512; // 512 safety bytes
|
|
|
|
// INFO: 3 bytes are the allowed overrun for the i386 asm_fast decompressors
|
2005-03-01 19:03:06 +08:00
|
|
|
#if (ACC_ARCH_I386)
|
2003-01-15 21:38:09 +08:00
|
|
|
size += 3;
|
|
|
|
#endif
|
|
|
|
alloc(size);
|
2000-05-20 00:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-28 02:54:06 +08:00
|
|
|
void MemBuffer::fill(unsigned off, unsigned len, int value)
|
|
|
|
{
|
|
|
|
checkState();
|
|
|
|
assert((int)off >= 0);
|
|
|
|
assert((int)len >= 0);
|
|
|
|
assert(off <= b_size);
|
|
|
|
assert(len <= b_size);
|
|
|
|
assert(off + len <= b_size);
|
|
|
|
if (len > 0)
|
|
|
|
memset(b + off, value, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-15 05:41:03 +08:00
|
|
|
/*************************************************************************
|
|
|
|
//
|
|
|
|
**************************************************************************/
|
2000-05-20 00:04:55 +08:00
|
|
|
|
2003-08-01 20:10:07 +08:00
|
|
|
#define PTR(p) ((unsigned) ((acc_uintptr_t)(p) & 0xffffffff))
|
2003-01-17 01:27:18 +08:00
|
|
|
#define MAGIC1(p) (PTR(p) ^ 0xfefdbeeb)
|
|
|
|
#define MAGIC2(p) (PTR(p) ^ 0xfefdbeeb ^ 0x80024001)
|
2000-05-20 00:04:55 +08:00
|
|
|
|
2003-01-15 05:41:03 +08:00
|
|
|
unsigned MemBuffer::global_alloc_counter = 0;
|
|
|
|
|
|
|
|
|
|
|
|
void MemBuffer::checkState() const
|
2000-05-20 00:04:55 +08:00
|
|
|
{
|
2003-01-28 02:54:06 +08:00
|
|
|
if (!b)
|
2003-01-15 05:41:03 +08:00
|
|
|
throwInternalError("block not allocated");
|
2003-01-15 21:38:09 +08:00
|
|
|
if (use_mcheck)
|
|
|
|
{
|
2003-01-28 02:54:06 +08:00
|
|
|
if (get_be32(b - 4) != MAGIC1(b))
|
2003-01-15 21:38:09 +08:00
|
|
|
throwInternalError("memory clobbered before allocated block 1");
|
2003-01-28 02:54:06 +08:00
|
|
|
if (get_be32(b - 8) != b_size)
|
2003-01-15 21:38:09 +08:00
|
|
|
throwInternalError("memory clobbered before allocated block 2");
|
2003-01-28 02:54:06 +08:00
|
|
|
if (get_be32(b + b_size) != MAGIC2(b))
|
2003-01-15 21:38:09 +08:00
|
|
|
throwInternalError("memory clobbered past end of allocated block");
|
|
|
|
}
|
2003-01-28 02:54:06 +08:00
|
|
|
assert((int)b_size > 0);
|
2000-05-20 00:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-15 05:41:03 +08:00
|
|
|
void MemBuffer::alloc(unsigned size)
|
2000-05-20 00:04:55 +08:00
|
|
|
{
|
2003-01-15 21:38:09 +08:00
|
|
|
if (use_mcheck < 0)
|
|
|
|
mcheck_init();
|
|
|
|
|
|
|
|
// NOTE: we don't automatically free a used buffer
|
2003-01-28 02:54:06 +08:00
|
|
|
assert(b == NULL);
|
|
|
|
assert(b_size == 0);
|
2003-01-15 05:41:03 +08:00
|
|
|
//
|
|
|
|
assert((int)size > 0);
|
2006-03-17 23:17:37 +08:00
|
|
|
unsigned total = use_mcheck ? size + 32 : size;
|
2003-01-15 05:41:03 +08:00
|
|
|
assert((int)total > 0);
|
|
|
|
unsigned char *p = (unsigned char *) malloc(total);
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
//throw bad_alloc();
|
2003-01-28 02:54:06 +08:00
|
|
|
throw OutOfMemoryException("out of memory");
|
2003-01-15 05:41:03 +08:00
|
|
|
//exit(1);
|
|
|
|
}
|
2003-01-28 02:54:06 +08:00
|
|
|
b_size = size;
|
2003-01-15 21:38:09 +08:00
|
|
|
if (use_mcheck)
|
|
|
|
{
|
2006-03-17 23:17:37 +08:00
|
|
|
b = p + 16;
|
2003-01-15 21:38:09 +08:00
|
|
|
// store magic constants to detect buffer overruns
|
2003-01-28 02:54:06 +08:00
|
|
|
set_be32(b - 8, b_size);
|
|
|
|
set_be32(b - 4, MAGIC1(b));
|
|
|
|
set_be32(b + b_size, MAGIC2(b));
|
|
|
|
set_be32(b + b_size + 4, global_alloc_counter++);
|
2003-01-15 21:38:09 +08:00
|
|
|
}
|
|
|
|
else
|
2003-01-28 02:54:06 +08:00
|
|
|
b = p ;
|
2000-05-20 00:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
vi:ts=4:et
|
|
|
|
*/
|
|
|
|
|