all: minor cleanups

This commit is contained in:
Markus F.X.J. Oberhumer
2024-03-08 11:52:44 +01:00
parent 839a78f2e0
commit 52d9b53b74
14 changed files with 208 additions and 63 deletions
+21 -15
View File
@@ -13,11 +13,11 @@ ifndef top_srcdir
top_srcdir := $(srcdir)/..
endif
#
#***********************************************************************
# redirect to top-level CMake build
#
#***********************************************************************
# NOTE that top-level Makefile .DEFAULT_GOAL is build/release
# NOTE that the top-level Makefile .DEFAULT_GOAL is build/release
.DEFAULT_GOAL = build/all
build/debug: $(top_srcdir)/build/debug
@@ -42,10 +42,10 @@ CTEST = ctest
test:: $(top_srcdir)/build/debug PHONY; cd $< && $(CTEST)
test:: $(top_srcdir)/build/release PHONY; cd $< && $(CTEST)
#
# "make run-testsuite"
#***********************************************************************
# make run-testsuite
# git clone https://github.com/upx/upx-testsuite.git
#
#***********************************************************************
# search for the UPX testsuite
# you also can override upx_testsuite_SRCDIR
@@ -78,24 +78,28 @@ run-testsuite-release: $(top_srcdir)/build/release PHONY
endif
endif
#
# "make check-whitespace"
#
#***********************************************************************
# make check-whitespace
#***********************************************************************
ifneq ($(wildcard /usr/bin/env),) # need Unix utils like bash, perl, sed, xargs, etc.
CHECK_WHITESPACE = bash $(top_srcdir)/misc/scripts/check_whitespace.sh $(top_srcdir)
ifneq ($(wildcard $(top_srcdir)/.git/.),)
CHECK_WHITESPACE = bash $(top_srcdir)/misc/scripts/check_whitespace_git.sh $(top_srcdir)
endif
check-whitespace: PHONY; $(CHECK_WHITESPACE)
endif
#
# "make clang-format"
#
endif # /usr/bin/env
#***********************************************************************
# make clang-format
#***********************************************************************
# automatically format some C++ source code files
ifneq ($(wildcard /usr/bin/env),)
ifeq ($(shell uname),Linux)
# Markus loves clang-format, but John hates it; find a compromise
CLANG_FORMAT_EXCLUDE_FILES += miniacc.h stub/%.h
CLANG_FORMAT_EXCLUDE_FILES += p_lx_% p_mach% p_unix% p_vmlin%
@@ -105,7 +109,9 @@ CLANG_FORMAT_FILES += $(sort $(wildcard ../misc/cmake/try_compile/*.[ch]*))
CLANG_FORMAT_FILES := $(filter-out $(CLANG_FORMAT_EXCLUDE_FILES),$(CLANG_FORMAT_FILES))
clang-format: $(CLANG_FORMAT_FILES) PHONY
@echo "running upx-clang-format"
@$(top_srcdir)/misc/scripts/upx-clang-format.sh -i $(CLANG_FORMAT_FILES)
endif
@bash $(top_srcdir)/misc/scripts/upx-clang-format.sh -i $(CLANG_FORMAT_FILES)
endif # Linux
endif # /usr/bin/env
# vim:set ts=8 sw=8 noet:
+60 -12
View File
@@ -24,6 +24,8 @@
<markus@oberhumer.com>
*/
#include "../headers.h"
#include <cmath> // std::isnan
#include "../conf.h"
/*************************************************************************
@@ -324,7 +326,7 @@ struct TestIntegerWrap {
};
//
// basic exception handling
// basic exception handling checks to early catch toolchain/qemu/wine/etc problems
//
static noinline void throwSomeValue(int x) may_throw {
@@ -347,20 +349,66 @@ static noinline void check_basic_cxx_exception_handling(void (*func)(int)) noexc
}
//
// basic floating point to early catch bad codegen
// (this has happened in the past with some exotic LLVM targets)
// basic floating point checks to early catch toolchain/qemu/wine/etc problems
//
static noinline double sadd_a_b_div(upx_int64_t a, upx_int64_t b) { return (a + b) / 1000000.0; }
static noinline double uadd_a_b_div(upx_uint64_t a, upx_uint64_t b) { return (a + b) / 1000000.0; }
static noinline double ssub_a_b_div(upx_int64_t a, upx_int64_t b) { return (a - b) / 1000000.0; }
static noinline double usub_a_b_div(upx_uint64_t a, upx_uint64_t b) { return (a - b) / 1000000.0; }
static noinline float i64_f32_add_div(upx_int64_t a, upx_int64_t b) { return (a + b) / 1000000.0f; }
static noinline float u64_f32_add_div(upx_uint64_t a, upx_uint64_t b) {
return (a + b) / 1000000.0f;
}
static noinline float i64_f32_sub_div(upx_int64_t a, upx_int64_t b) { return (a - b) / 1000000.0f; }
static noinline float u64_f32_sub_div(upx_uint64_t a, upx_uint64_t b) {
return (a - b) / 1000000.0f;
}
static noinline double i64_f64_add_div(upx_int64_t a, upx_int64_t b) { return (a + b) / 1000000.0; }
static noinline double u64_f64_add_div(upx_uint64_t a, upx_uint64_t b) {
return (a + b) / 1000000.0;
}
static noinline double i64_f64_sub_div(upx_int64_t a, upx_int64_t b) { return (a - b) / 1000000.0; }
static noinline double u64_f64_sub_div(upx_uint64_t a, upx_uint64_t b) {
return (a - b) / 1000000.0;
}
template <class Int, class Float>
struct TestFloat {
static constexpr Int X = 1000000;
static noinline Float div(Int a, Float f) { return a / f; }
static noinline Float add_div(Int a, Int b, Float f) { return Float(a + b) / f; }
static noinline Float sub_div(Int a, Int b, Float f) { return Float(a - b) / f; }
static noinline Float add_div_x(Int a, Int b) { return Float(a + b) / Float(X); }
static noinline Float sub_div_x(Int a, Int b) { return Float(a - b) / Float(X); }
static noinline void check() noexcept {
assert_noexcept(add_div(X, X, Float(X)) == Float(2));
assert_noexcept(add_div_x(X, X) == Float(2));
assert_noexcept(sub_div(3 * X, X, Float(X)) == Float(2));
assert_noexcept(sub_div_x(3 * X, X) == Float(2));
// extra debugging hack
const char *e = getenv("UPX_DEBUG_TEST_FLOAT_DIVISION_BY_ZERO");
if (e && e[0] && strcmp(e, "0") != 0) {
assert_noexcept(std::isnan(div(0, Float(0))));
assert_noexcept(std::isinf(div(X, Float(0))));
}
}
};
static noinline void check_basic_floating_point(void) noexcept {
assert_noexcept(sadd_a_b_div(1000000, 1000000) == 2.0);
assert_noexcept(uadd_a_b_div(1000000, 1000000) == 2.0);
assert_noexcept(ssub_a_b_div(3000000, 1000000) == 2.0);
assert_noexcept(usub_a_b_div(3000000, 1000000) == 2.0);
assert_noexcept(i64_f32_add_div(1000000, 1000000) == 2.0f);
assert_noexcept(u64_f32_add_div(1000000, 1000000) == 2.0f);
assert_noexcept(i64_f32_sub_div(3000000, 1000000) == 2.0f);
assert_noexcept(u64_f32_sub_div(3000000, 1000000) == 2.0f);
assert_noexcept(i64_f64_add_div(1000000, 1000000) == 2.0);
assert_noexcept(u64_f64_add_div(1000000, 1000000) == 2.0);
assert_noexcept(i64_f64_sub_div(3000000, 1000000) == 2.0);
assert_noexcept(u64_f64_sub_div(3000000, 1000000) == 2.0);
TestFloat<upx_int32_t, float>::check();
TestFloat<upx_uint32_t, float>::check();
TestFloat<upx_int64_t, float>::check();
TestFloat<upx_uint64_t, float>::check();
TestFloat<upx_int32_t, double>::check();
TestFloat<upx_uint32_t, double>::check();
TestFloat<upx_int64_t, double>::check();
TestFloat<upx_uint64_t, double>::check();
}
} // namespace
@@ -377,7 +425,7 @@ void upx_compiler_sanity_check(void) noexcept {
// If UPX_DEBUG_DOCTEST_DISABLE is set then we don't want to throw any
// exceptions in order to improve debugging experience.
} else {
// check working C++ exception handling to catch toolchain/qemu/wine/etc problems
// check working C++ exception handling to early catch toolchain/qemu/wine/etc problems
check_basic_cxx_exception_handling(throwSomeValue);
}
+4 -1
View File
@@ -24,6 +24,8 @@
<markus@oberhumer.com>
*/
#include "../util/system_defs.h"
/*************************************************************************
// doctest support code implementation
**************************************************************************/
@@ -50,7 +52,8 @@
#endif
#endif
// aligned_alloc() was added in glibc-2.16
#if defined(__ELF__) && (__GLIBC__ + 0 == 2) && (__GLIBC_MINOR__ + 0 < 16)
#if defined(__ELF__) && defined(__GLIBC__) && defined(__GLIBC_MINOR__) && (__GLIBC__ + 0 == 2) && \
(__GLIBC_MINOR__ + 0 < 16)
#define _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
#endif
+2 -24
View File
@@ -26,6 +26,8 @@
#pragma once
#include "util/system_defs.h"
#if !(__cplusplus + 0 >= 201703L)
#error "C++17 is required"
#endif
@@ -54,30 +56,6 @@ static_assert(sizeof(long) == 4);
static_assert(sizeof(void *) == 8);
#endif
#if !defined(_FILE_OFFSET_BITS)
#define _FILE_OFFSET_BITS 64
#endif
#if defined(_WIN32) && defined(__MINGW32__) && (defined(__clang__) || defined(__GNUC__))
#if !defined(__USE_MINGW_ANSI_STDIO)
#define __USE_MINGW_ANSI_STDIO 1
#endif
#endif
#if defined(_WIN32)
// disable silly warnings about using "deprecated" POSIX functions like fopen()
#if !defined(_CRT_NONSTDC_NO_DEPRECATE)
#define _CRT_NONSTDC_NO_DEPRECATE 1
#endif
#if !defined(_CRT_NONSTDC_NO_WARNINGS)
#define _CRT_NONSTDC_NO_WARNINGS 1
#endif
#if !defined(_CRT_SECURE_NO_DEPRECATE)
#define _CRT_SECURE_NO_DEPRECATE 1
#endif
#if !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS 1
#endif
#endif // _WIN32
// ACC and C system headers
#ifndef ACC_CFG_USE_NEW_STYLE_CASTS
#define ACC_CFG_USE_NEW_STYLE_CASTS 1
+1
View File
@@ -74,6 +74,7 @@ static forceinline constexpr bool use_simple_mcheck() noexcept { return true; }
**************************************************************************/
MemBuffer::MemBuffer(upx_uint64_t bytes) : MemBufferBase<byte>() {
static_assert(element_size == 1);
alloc(bytes);
debug_set(debug.last_return_address_alloc, upx_return_address());
}
+2 -1
View File
@@ -204,7 +204,8 @@ public:
// explicit conversion
void *getVoidPtr() noexcept { return (void *) ptr; }
const void *getVoidPtr() const noexcept { return (const void *) ptr; }
unsigned getSize() const noexcept { return size_in_bytes; }
unsigned getSizeInBytes() const noexcept { return size_in_bytes; }
unsigned getSize() const noexcept { return size_in_bytes; } // note: element_size == 1
// util
noinline void fill(unsigned off, unsigned len, int value) may_throw;
+59
View File
@@ -0,0 +1,59 @@
/* system_defs.h -- system defines
This file is part of the UPX executable compressor.
Copyright (C) 1996-2024 Markus Franz Xaver Johannes Oberhumer
All Rights Reserved.
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.
Markus F.X.J. Oberhumer
<markus@oberhumer.com>
*/
#pragma once
#if !defined(_FILE_OFFSET_BITS)
#define _FILE_OFFSET_BITS 64
#endif
#if !defined(__STDC_FORMAT_MACROS) // this is needed for some older glibc/mingw versions
#define __STDC_FORMAT_MACROS 1
#endif
#if defined(_WIN32) && defined(__MINGW32__) && (defined(__clang__) || defined(__GNUC__))
#if !defined(__USE_MINGW_ANSI_STDIO)
#define __USE_MINGW_ANSI_STDIO 1
#endif
#endif
#if defined(_WIN32)
// disable silly warnings about using "deprecated" POSIX functions like fopen()
#if !defined(_CRT_NONSTDC_NO_DEPRECATE)
#define _CRT_NONSTDC_NO_DEPRECATE 1
#endif
#if !defined(_CRT_NONSTDC_NO_WARNINGS)
#define _CRT_NONSTDC_NO_WARNINGS 1
#endif
#if !defined(_CRT_SECURE_NO_DEPRECATE)
#define _CRT_SECURE_NO_DEPRECATE 1
#endif
#if !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS 1
#endif
#endif // _WIN32
/* vim:set ts=4 sw=4 et: */