Skip to content

Commit

Permalink
Merge pull request #868 from edge-classic/ec-memory-check
Browse files Browse the repository at this point in the history
Adding memory check mode and bumping cmake to 3.27
  • Loading branch information
pbdot authored Jan 29, 2025
2 parents a08787b + 691f37e commit 2966856
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
15 changes: 14 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Edge Classic - CMake Script
##########################################

cmake_minimum_required(VERSION 3.20)
cmake_minimum_required(VERSION 3.27)

project(
edge-classic
Expand Down Expand Up @@ -57,6 +57,12 @@ option(EDGE_CLASSIC "Enable default features for EDGE-Classic" ON)
option(EDGE_SANITIZE "Enable code sanitizing" OFF)
option(EDGE_PROFILING "Enable Profiling" OFF)

# Enables exhaustive memory checks, see mimalloc CMakeLists.txt for what is enabled
# Memory error logging goes to debug.txt
option(EDGE_MEMORY_CHECK "Enable Memory Checks" OFF)
# If fatal memory checks are enabled, any memory error will be fatal and provide a callstack
option(EDGE_MEMORY_CHECK_FATAL "Enable Fatal Memory Checks" OFF)

include("${CMAKE_SOURCE_DIR}/cmake/EDGEClassic.cmake")

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
Expand Down Expand Up @@ -202,5 +208,12 @@ if (EDGE_PROFILING)
add_compile_definitions(EDGE_PROFILING TRACY_ENABLE)
endif()

if (EDGE_MEMORY_CHECK)
add_compile_definitions(EDGE_MEMORY_CHECK)
if (EDGE_MEMORY_CHECK_FATAL)
add_compile_definitions(EDGE_MEMORY_CHECK_FATAL)
endif()
endif()

add_subdirectory(libraries)
add_subdirectory(source_files)
8 changes: 5 additions & 3 deletions libraries/mimalloc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
cmake_minimum_required(VERSION 3.18)
#cmake_minimum_required(VERSION 3.18)
project(libmimalloc C CXX)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

option(MI_SECURE "Use full security mitigations (like guard pages, allocation randomization, double-free mitigation, and free-list corruption detection)" OFF)
option(MI_DEBUG_FULL "Use full internal heap invariant checking in DEBUG mode (expensive)" OFF)
include(CMakeDependentOption)

cmake_dependent_option(MI_SECURE "Use full security mitigations (like guard pages, allocation randomization, double-free mitigation, and free-list corruption detection)" ON "EDGE_MEMORY_CHECK" OFF)
cmake_dependent_option(MI_DEBUG_FULL "Use full internal heap invariant checking in DEBUG mode (expensive)" ON "EDGE_MEMORY_CHECK" OFF)
option(MI_PADDING "Enable padding to detect heap block overflow (always on in DEBUG or SECURE mode, or with Valgrind/ASAN)" OFF)
option(MI_OVERRIDE "Override the standard malloc interface (i.e. define entry points for 'malloc', 'free', etc)" ON)
option(MI_XMALLOC "Enable abort() call on memory allocation failure by default" OFF)
Expand Down
54 changes: 49 additions & 5 deletions source_files/edge/e_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@
#include "w_texture.h"
#include "w_wad.h"

#ifdef EDGE_MEMORY_CHECK
#include <mimalloc.h>
#endif

extern ImageData *ReadAsEpiBlock(Image *rim);

extern ConsoleVariable busy_wait;
Expand Down Expand Up @@ -851,8 +855,8 @@ static void AutocolourForMapObject(MapObjectDefinition *mobj)
ImageData *tmp_img_data = ReadAsEpiBlock((Image *)img);
if (tmp_img_data->depth_ == 1)
{
ImageData *rgb_img_data = RGBFromPalettised(tmp_img_data, what_palette ? what_palette :
(const uint8_t *)&playpal_data[0], img->opacity_);
ImageData *rgb_img_data = RGBFromPalettised(
tmp_img_data, what_palette ? what_palette : (const uint8_t *)&playpal_data[0], img->opacity_);
delete tmp_img_data;
tmp_img_data = rgb_img_data;
}
Expand Down Expand Up @@ -885,8 +889,9 @@ static void AutocolourForMapObject(MapObjectDefinition *mobj)
ImageData *tmp_img_data = ReadAsEpiBlock((Image *)img);
if (tmp_img_data->depth_ == 1)
{
ImageData *rgb_img_data = RGBFromPalettised(tmp_img_data, what_palette ? what_palette :
(const uint8_t *)&playpal_data[0], img->opacity_);
ImageData *rgb_img_data = RGBFromPalettised(
tmp_img_data, what_palette ? what_palette : (const uint8_t *)&playpal_data[0],
img->opacity_);
delete tmp_img_data;
tmp_img_data = rgb_img_data;
}
Expand Down Expand Up @@ -2043,7 +2048,7 @@ static void AddCommandLineFiles(void)
// sanity check...
if (epi::StringCaseCompareASCII(ext, ".wad") == 0 || epi::StringCaseCompareASCII(ext, ".pk3") == 0 ||
epi::StringCaseCompareASCII(ext, ".zip") == 0 || epi::StringCaseCompareASCII(ext, ".epk") == 0 ||
epi::StringCaseCompareASCII(ext, ".ddf") == 0 || epi::StringCaseCompareASCII(ext, ".deh") == 0 ||
epi::StringCaseCompareASCII(ext, ".ddf") == 0 || epi::StringCaseCompareASCII(ext, ".deh") == 0 ||
epi::StringCaseCompareASCII(ext, ".bex") == 0)
{
FatalError("Illegal filename for -script: %s\n", program_argument_list[p].c_str());
Expand Down Expand Up @@ -2205,6 +2210,40 @@ void EdgeShutdown(void)
NetworkShutdown();
}

#ifdef EDGE_MEMORY_CHECK
static void MemoryCheckOutput(const char *msg, void *arg)
{
EPI_UNUSED(arg);

int i = 0;
size_t len = strlen(msg);
for (i = 0; i < len; i++)
{
if (msg[i] != '\n' && msg[i] != '\r' && msg[i] != '\t' && msg[i] != ' ')
break;
}
if (len && i != len)
{

LogDebug("%s", msg);
#ifdef EDGE_MEMORY_CHECK_FATAL
// skip the prefix
if (!strcmp(msg, "mimalloc: warning: ") || !strcmp(msg, "mimalloc: error: "))
{
return;
}
FatalError("Memory Check Error: %s", msg);
#endif
}
}

static void InitializeMemoryCheck()
{
mi_option_enable(mi_option_show_errors);
mi_register_output(MemoryCheckOutput, nullptr);
}
#endif

static void EdgeStartup(void)
{
ConsoleInit();
Expand Down Expand Up @@ -2419,6 +2458,11 @@ static void InitialState(void)
//
void EdgeMain(int argc, const char **argv)
{

#ifdef EDGE_MEMORY_CHECK
InitializeMemoryCheck();
#endif

// Seed RandomByte RNG
InitRandomState();

Expand Down
2 changes: 0 additions & 2 deletions source_files/edge/r_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ Image *ImageContainerLookup(ImageType image_type, const char *name, int source_t
default:
FatalError("ImageContainerLookup: Unknown Image Type");
}

return nullptr;
}

static void do_Animate(ImageMap &bucket)
Expand Down
1 change: 0 additions & 1 deletion source_files/edge/rad_pars.cc
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ static bool CheckForBoolean(const char *s)

// Nope, it's an error.
ScriptError("Bad boolean value (should be TRUE or FALSE): %s\n", s);
return false;
}

// AddStateToScript
Expand Down

0 comments on commit 2966856

Please sign in to comment.