2000-05-20 00:04:55 +08:00
|
|
|
/* mem.cpp --
|
|
|
|
|
|
|
|
This file is part of the UPX executable compressor.
|
|
|
|
|
2016-09-22 20:07:14 +08:00
|
|
|
Copyright (C) 1996-2016 Markus Franz Xaver Johannes Oberhumer
|
|
|
|
Copyright (C) 1996-2016 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
|
2008-01-02 17:12:42 +08:00
|
|
|
<markus@oberhumer.com> <ml1050@users.sourceforge.net>
|
2000-05-20 00:04:55 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "conf.h"
|
|
|
|
#include "mem.h"
|
|
|
|
|
|
|
|
|
2016-09-20 21:24:07 +08:00
|
|
|
/*************************************************************************
|
2016-09-21 22:42:25 +08:00
|
|
|
// assert sane memory buffer sizes to protect against integer overflows
|
|
|
|
// and malicious header fields
|
2016-09-20 21:24:07 +08:00
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
// DO NOT CHANGE
|
2016-09-21 22:42:25 +08:00
|
|
|
#define MAX_BUF_SIZE (768 * 1024 * 1024)
|
|
|
|
ACC_COMPILE_TIME_ASSERT_HEADER(2ull * MAX_BUF_SIZE * 9 / 8 + 16*1024*1024 < INT_MAX)
|
2016-09-20 21:24:07 +08:00
|
|
|
|
|
|
|
size_t mem_size(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra)
|
|
|
|
{
|
|
|
|
assert(element_size > 0);
|
2016-09-21 22:42:25 +08:00
|
|
|
if (element_size > MAX_BUF_SIZE) throwCantPack("mem_size 1; take care");
|
|
|
|
if (n > MAX_BUF_SIZE) throwCantPack("mem_size 2; take care");
|
|
|
|
if (extra > MAX_BUF_SIZE) throwCantPack("mem_size 3; take care");
|
2016-09-20 21:24:07 +08:00
|
|
|
upx_uint64_t bytes = element_size * n + extra; // cannot overflow
|
2016-09-21 22:42:25 +08:00
|
|
|
if (bytes > MAX_BUF_SIZE) throwCantPack("mem_size 4; take care");
|
2016-09-20 21:24:07 +08:00
|
|
|
return ACC_ICONV(size_t, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n)
|
|
|
|
{
|
|
|
|
(void) mem_size(element_size, n); // check
|
|
|
|
return ACC_ICONV(size_t, n); // return n
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra)
|
|
|
|
{
|
|
|
|
assert(element_size > 0);
|
2016-09-21 22:42:25 +08:00
|
|
|
if (element_size > MAX_BUF_SIZE) return false;
|
|
|
|
if (n > MAX_BUF_SIZE) return false;
|
|
|
|
if (extra > MAX_BUF_SIZE) return false;
|
2016-09-20 21:24:07 +08:00
|
|
|
upx_uint64_t bytes = element_size * n + extra; // cannot overflow
|
2016-09-21 22:42:25 +08:00
|
|
|
if (bytes > MAX_BUF_SIZE) return false;
|
2016-09-20 21:24:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-22 03:33:53 +08:00
|
|
|
bool mem_size_valid_bytes(upx_uint64_t bytes)
|
|
|
|
{
|
|
|
|
if (bytes > MAX_BUF_SIZE) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ptr_diff(const char *p1, const char *p2)
|
|
|
|
{
|
|
|
|
assert(p1 != NULL);
|
|
|
|
assert(p2 != NULL);
|
|
|
|
ptrdiff_t d = p1 - p2;
|
|
|
|
if (p1 >= p2)
|
|
|
|
assert(mem_size_valid_bytes(d));
|
|
|
|
else
|
|
|
|
assert(mem_size_valid_bytes(-d));
|
|
|
|
return ACC_ICONV(int, d);
|
|
|
|
}
|
|
|
|
|
2016-09-21 22:42:25 +08:00
|
|
|
#undef MAX_BUF_SIZE
|
|
|
|
|
2016-09-20 21:24:07 +08:00
|
|
|
|
2000-05-20 00:04:55 +08:00
|
|
|
/*************************************************************************
|
2016-09-25 15:41:26 +08:00
|
|
|
// bool use_mcheck()
|
2000-05-20 00:04:55 +08:00
|
|
|
**************************************************************************/
|
|
|
|
|
2016-09-25 15:41:26 +08:00
|
|
|
#if defined(__SANITIZE_ADDRESS__)
|
|
|
|
__acc_static_forceinline bool use_mcheck() { return false; }
|
|
|
|
#elif (WITH_VALGRIND) && defined(RUNNING_ON_VALGRIND)
|
|
|
|
static int use_mcheck_flag = -1;
|
|
|
|
__acc_static_noinline void use_mcheck_init()
|
2003-01-15 21:38:09 +08:00
|
|
|
{
|
2016-09-25 15:41:26 +08:00
|
|
|
use_mcheck_flag = 1;
|
|
|
|
if (RUNNING_ON_VALGRIND) {
|
|
|
|
use_mcheck_flag = 0;
|
|
|
|
//fprintf(stderr, "upx: detected RUNNING_ON_VALGRIND\n");
|
2003-01-15 21:38:09 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-25 15:41:26 +08:00
|
|
|
__acc_static_forceinline bool use_mcheck()
|
|
|
|
{
|
|
|
|
if __acc_unlikely(use_mcheck_flag < 0)
|
|
|
|
use_mcheck_init();
|
|
|
|
return (bool) use_mcheck_flag;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
__acc_static_forceinline bool use_mcheck() { return true; }
|
|
|
|
#endif
|
2003-01-15 21:38:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
//
|
|
|
|
**************************************************************************/
|
|
|
|
|
2016-09-25 15:41:26 +08:00
|
|
|
MemBuffer::MemBuffer(upx_uint64_t 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
|
|
|
{
|
2016-09-25 15:41:26 +08:00
|
|
|
if (b != NULL)
|
2003-01-15 05:41:03 +08:00
|
|
|
{
|
|
|
|
checkState();
|
2016-09-25 15:41:26 +08:00
|
|
|
if (use_mcheck())
|
2003-01-15 21:38:09 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-12 19:18:55 +08:00
|
|
|
unsigned MemBuffer::getSizeForCompression(unsigned uncompressed_size, unsigned extra)
|
2000-05-20 00:04:55 +08:00
|
|
|
{
|
2016-09-20 21:24:07 +08:00
|
|
|
size_t bytes = mem_size(1, uncompressed_size, extra);
|
|
|
|
bytes += uncompressed_size/8 + 256;
|
|
|
|
return ACC_ICONV(unsigned, bytes);
|
2000-05-20 00:04:55 +08:00
|
|
|
}
|
|
|
|
|
2006-06-12 19:18:55 +08:00
|
|
|
unsigned MemBuffer::getSizeForUncompression(unsigned uncompressed_size, unsigned extra)
|
2000-05-20 00:04:55 +08:00
|
|
|
{
|
2016-09-20 21:24:07 +08:00
|
|
|
size_t bytes = mem_size(1, uncompressed_size, extra);
|
2003-01-15 21:38:09 +08:00
|
|
|
// 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)
|
2016-09-20 21:24:07 +08:00
|
|
|
bytes += 3;
|
2003-01-15 21:38:09 +08:00
|
|
|
#endif
|
2016-09-20 21:24:07 +08:00
|
|
|
return ACC_ICONV(unsigned, bytes);
|
2006-06-12 19:18:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MemBuffer::allocForCompression(unsigned uncompressed_size, unsigned extra)
|
|
|
|
{
|
|
|
|
unsigned size = getSizeForCompression(uncompressed_size, extra);
|
|
|
|
alloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MemBuffer::allocForUncompression(unsigned uncompressed_size, unsigned extra)
|
|
|
|
{
|
|
|
|
unsigned size = getSizeForUncompression(uncompressed_size, extra);
|
2003-01-15 21:38:09 +08:00
|
|
|
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
|
|
|
|
2013-09-20 14:19:18 +08:00
|
|
|
#define PTR(p) ((unsigned) ((upx_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");
|
2016-09-25 15:41:26 +08:00
|
|
|
if (use_mcheck())
|
2003-01-15 21:38:09 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-25 15:41:26 +08:00
|
|
|
void MemBuffer::alloc(upx_uint64_t size)
|
2000-05-20 00:04:55 +08:00
|
|
|
{
|
2003-01-15 21:38:09 +08:00
|
|
|
// 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
|
|
|
//
|
2016-09-22 04:22:41 +08:00
|
|
|
assert(size > 0);
|
2016-09-25 15:41:26 +08:00
|
|
|
size_t bytes = mem_size(1, size, use_mcheck() ? 32 : 0);
|
2016-09-20 21:24:07 +08:00
|
|
|
unsigned char *p = (unsigned char *) malloc(bytes);
|
2003-01-15 05:41:03 +08:00
|
|
|
if (!p)
|
2007-05-08 21:28:35 +08:00
|
|
|
throwOutOfMemoryException();
|
2016-09-25 15:41:26 +08:00
|
|
|
b_size = ACC_ICONV(unsigned, size);
|
|
|
|
if (use_mcheck())
|
2003-01-15 21:38:09 +08:00
|
|
|
{
|
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 ;
|
2006-11-19 04:59:59 +08:00
|
|
|
|
|
|
|
//fill(0, b_size, (rand() & 0xff) | 1); // debug
|
2000-05-20 00:04:55 +08:00
|
|
|
}
|
|
|
|
|
2016-09-22 04:22:41 +08:00
|
|
|
/* vim:set ts=4 sw=4 et: */
|