From daafc29d2bc1bb47318edefb072d31a57f1b474b Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Sun, 25 Sep 2016 09:41:26 +0200 Subject: [PATCH] Add support for UPX_VERSION_GITREV. Some cleanups. --- src/Makefile | 9 ++++++- src/compress_ucl.cpp | 4 +++ src/help.cpp | 15 ++++++----- src/main.cpp | 10 ++++---- src/mem.cpp | 60 ++++++++++++++++++++------------------------ src/mem.h | 6 ++--- src/p_lx_elf.h | 8 ++++-- src/p_mach.cpp | 4 +-- 8 files changed, 62 insertions(+), 54 deletions(-) diff --git a/src/Makefile b/src/Makefile index bed6b030..ee9011aa 100644 --- a/src/Makefile +++ b/src/Makefile @@ -46,6 +46,13 @@ objext ?= .o upx_SOURCES := $(sort $(wildcard $(srcdir)/*.cpp)) upx_OBJECTS := $(notdir $(upx_SOURCES:.cpp=$(objext))) +ifneq ($(wildcard $(top_srcdir)/.git/.),) +UPX_VERSION_GITREV := $(strip $(shell cd '$(top_srcdir)' && git rev-parse --short=6 HEAD || echo "")) +ifneq ($(UPX_VERSION_GITREV),) +DEFS += '-DUPX_VERSION_GITREV="$(UPX_VERSION_GITREV)"' +endif +endif + # we need UCL and zlib - you can set envvar UPX_UCLDIR ifneq ($(wildcard $(UPX_UCLDIR)/include/ucl/ucl.h),) INCLUDES += -I$(UPX_UCLDIR)/include @@ -126,7 +133,7 @@ endif CLANG_FORMAT_FILES += packhead.cpp CLANG_FORMAT_FILES += s_djgpp2.cpp s_object.cpp s_vcsa.cpp s_win32.cpp screen.h CLANG_FORMAT_FILES += snprintf.cpp -CLANG_FORMAT_FILES += stdcxx.h stdcxx.cpp +CLANG_FORMAT_FILES += stdcxx.cpp stdcxx.h CLANG_FORMAT_FILES += ui.cpp ui.h util.h clang-format: $(top_srcdir)/src/stub/scripts/upx-clang-format -i $(addprefix $(top_srcdir)/src/,$(CLANG_FORMAT_FILES)) diff --git a/src/compress_ucl.cpp b/src/compress_ucl.cpp index 2e8acdc9..f980cf62 100644 --- a/src/compress_ucl.cpp +++ b/src/compress_ucl.cpp @@ -285,6 +285,10 @@ int upx_ucl_init(void) { if (ucl_init() != UCL_E_OK) return -1; +#if defined(UPX_OFFICIAL_BUILD) + if (UCL_VERSION != ucl_version()) + return -2; +#endif return 0; } diff --git a/src/help.cpp b/src/help.cpp index 5fd80c13..e86babdb 100644 --- a/src/help.cpp +++ b/src/help.cpp @@ -51,15 +51,14 @@ void show_head(void) fg = con_fg(f,FG_GREEN); con_fprintf(f, " Ultimate Packer for eXecutables\n" - " Copyright (C) 1996 - %s\n" - "UPX %-10s Markus Oberhumer, Laszlo Molnar & John Reiser %14s\n\n", - UPX_VERSION_YEAR, -#if (ACC_OS_DOS16 || ACC_OS_DOS32) + " Copyright (C) 1996 - " UPX_VERSION_YEAR "\n" + "UPX %-11s Markus Oberhumer, Laszlo Molnar & John Reiser %14s\n\n", +#if defined(UPX_VERSION_GITREV) + "git-" UPX_VERSION_GITREV, +#elif (ACC_OS_DOS16 || ACC_OS_DOS32) V("d"), #elif (ACC_OS_WIN16 || ACC_OS_WIN32 || ACC_OS_WIN64) V("w"), -#elif 0 && defined(__linux__) - V("l"), #else UPX_VERSION_STRING, #endif @@ -136,7 +135,7 @@ static void show_all_packers(FILE *f, int verbose) { const char *fn = pn.names[i].fname; const char *sn = pn.names[i].sname; - if (verbose) + if (verbose > 0) { con_fprintf(f, " %-36s %s\n", fn, sn); } @@ -155,7 +154,7 @@ static void show_all_packers(FILE *f, int verbose) } } } - if (!verbose && pn.names_count) + if (verbose <= 0 && pn.names_count) con_fprintf(f, "\n"); } diff --git a/src/main.cpp b/src/main.cpp index fab1a985..2ba5bd49 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1285,10 +1285,6 @@ static bool test(void) COMPILE_TIME_ASSERT(__acc_alignof(t1) == 1) COMPILE_TIME_ASSERT(__acc_alignof(t2) == 1) #endif -#if 1 && (ACC_CC_WATCOMC) - test1_t t11; COMPILE_TIME_ASSERT(sizeof(t11.a) <= sizeof(t11.b)) - test2_t t22; COMPILE_TIME_ASSERT(sizeof(t22.a) <= sizeof(t22.b)) -#endif #if 1 && !defined(UPX_OFFICIAL_BUILD) T allbits; allbits = 0; allbits -= 1; //++allbits; allbits++; --allbits; allbits--; @@ -1356,6 +1352,10 @@ __acc_static_noinline void upx_sanity_check(void) assert(strlen(UPX_VERSION_YEAR) == 4); assert(memcmp(UPX_VERSION_DATE_ISO, UPX_VERSION_YEAR, 4) == 0); assert(memcmp(&UPX_VERSION_DATE[sizeof(UPX_VERSION_DATE)-1 - 4], UPX_VERSION_YEAR, 4) == 0); +#if defined(UPX_VERSION_GITREV) + assert(strlen(UPX_VERSION_GITREV) >= 5); + assert(strlen(UPX_VERSION_GITREV) <= 7); +#endif #if 1 assert(TestBELE::test()); @@ -1552,7 +1552,7 @@ int __acc_cdecl_main main(int argc, char *argv[]) set_term(stdout); do_files(i,argc,argv); -#if 0 && (UPX_VERSION_HEX < 0x030000) +#if 1 && defined(UPX_VERSION_GITREV) { FILE *f = stdout; int fg = con_fg(f,FG_RED); diff --git a/src/mem.cpp b/src/mem.cpp index 67727b46..ce1a4347 100644 --- a/src/mem.cpp +++ b/src/mem.cpp @@ -90,40 +90,37 @@ int ptr_diff(const char *p1, const char *p2) /************************************************************************* -// +// bool use_mcheck() **************************************************************************/ -static int use_mcheck = -1; - -static int mcheck_init() +#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() { - if (use_mcheck < 0) - { - use_mcheck = 1; -#if (WITH_VALGRIND) && defined(RUNNING_ON_VALGRIND) - if (RUNNING_ON_VALGRIND) - { - //fprintf(stderr, "upx: detected RUNNING_ON_VALGRIND\n"); - use_mcheck = 0; - } -#endif + use_mcheck_flag = 1; + if (RUNNING_ON_VALGRIND) { + use_mcheck_flag = 0; + //fprintf(stderr, "upx: detected RUNNING_ON_VALGRIND\n"); } - return use_mcheck; } +__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 /************************************************************************* // **************************************************************************/ -MemBuffer::MemBuffer() : - b(NULL), b_size(0) -{ - if (use_mcheck < 0) - mcheck_init(); -} - -MemBuffer::MemBuffer(unsigned size) : +MemBuffer::MemBuffer(upx_uint64_t size) : b(NULL), b_size(0) { alloc(size); @@ -137,10 +134,10 @@ MemBuffer::~MemBuffer() void MemBuffer::dealloc() { - if (b) + if (b != NULL) { checkState(); - if (use_mcheck) + if (use_mcheck()) { // remove magic constants set_be32(b - 8, 0); @@ -220,7 +217,7 @@ void MemBuffer::checkState() const { if (!b) throwInternalError("block not allocated"); - if (use_mcheck) + if (use_mcheck()) { if (get_be32(b - 4) != MAGIC1(b)) throwInternalError("memory clobbered before allocated block 1"); @@ -233,22 +230,19 @@ void MemBuffer::checkState() const } -void MemBuffer::alloc(unsigned size) +void MemBuffer::alloc(upx_uint64_t size) { - if (use_mcheck < 0) - mcheck_init(); - // NOTE: we don't automatically free a used buffer assert(b == NULL); assert(b_size == 0); // assert(size > 0); - size_t bytes = mem_size(1, size, use_mcheck ? 32 : 0); + size_t bytes = mem_size(1, size, use_mcheck() ? 32 : 0); unsigned char *p = (unsigned char *) malloc(bytes); if (!p) throwOutOfMemoryException(); - b_size = size; - if (use_mcheck) + b_size = ACC_ICONV(unsigned, size); + if (use_mcheck()) { b = p + 16; // store magic constants to detect buffer overruns diff --git a/src/mem.h b/src/mem.h index 8fffafef..9941ada2 100644 --- a/src/mem.h +++ b/src/mem.h @@ -37,14 +37,14 @@ class MemBuffer { public: - MemBuffer(); - explicit MemBuffer(unsigned size); + MemBuffer() : b(NULL), b_size(0) { } + explicit MemBuffer(upx_uint64_t size); ~MemBuffer(); static unsigned getSizeForCompression(unsigned uncompressed_size, unsigned extra=0); static unsigned getSizeForUncompression(unsigned uncompressed_size, unsigned extra=0); - void alloc(unsigned size); + void alloc(upx_uint64_t size); void allocForCompression(unsigned uncompressed_size, unsigned extra=0); void allocForUncompression(unsigned uncompressed_size, unsigned extra=0); diff --git a/src/p_lx_elf.h b/src/p_lx_elf.h index fae4f5d9..7a9167dd 100644 --- a/src/p_lx_elf.h +++ b/src/p_lx_elf.h @@ -461,8 +461,9 @@ class PackBSDElf32x86 : public PackLinuxElf32x86 public: PackBSDElf32x86(InputFile *f); virtual ~PackBSDElf32x86(); - virtual int getFormat() const { return UPX_F_BSD_ELF_i386; } - virtual const char *getName() const { return "bsd/i386"; } + virtual int getFormat() const = 0; + virtual const char *getName() const = 0; + virtual const char *getFullName(const options_t *) const = 0; protected: virtual void pack1(OutputFile *, Filter &); // generate executable header @@ -476,6 +477,8 @@ class PackFreeBSDElf32x86 : public PackBSDElf32x86 public: PackFreeBSDElf32x86(InputFile *f); virtual ~PackFreeBSDElf32x86(); + virtual int getFormat() const { return UPX_F_BSD_ELF_i386; } + virtual const char *getName() const { return "freebsd/i386"; } virtual const char *getFullName(const options_t *) const { return "i386-freebsd.elf"; } }; @@ -503,6 +506,7 @@ class PackOpenBSDElf32x86 : public PackBSDElf32x86 public: PackOpenBSDElf32x86(InputFile *f); virtual ~PackOpenBSDElf32x86(); + virtual int getFormat() const { return UPX_F_BSD_ELF_i386; } virtual const char *getName() const { return "openbsd/i386"; } virtual const char *getFullName(const options_t *) const { return "i386-openbsd.elf"; } diff --git a/src/p_mach.cpp b/src/p_mach.cpp index feb94932..e4302aa1 100644 --- a/src/p_mach.cpp +++ b/src/p_mach.cpp @@ -2007,8 +2007,8 @@ int PackMachBase::canUnpack() fi->seek(offLINK - bufsize, SEEK_SET); } else if (392 == style) { // PackHeader follows loader at __LINKEDIT - if (bufsize > (fi->st_size() - offLINK)) { - bufsize = fi->st_size() - offLINK; + if ((off_t)bufsize > (fi->st_size() - offLINK)) { + bufsize = fi->st_size() - offLINK; } fi->seek(offLINK, SEEK_SET); }