From ca921e5a53fdd3412f5f81e3a739f54d68cb63a7 Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Mon, 27 Nov 2017 01:24:08 +0100 Subject: CMake: Explicitly ask for C99 support Otherwise using a compiler that defaults to -std=c89 or -std=gnu89 will fail. Example: http://www.cpantesters.org/cpan/report/abb85066-d283-11e7-9926-b2f4efb9c382 Apparently, -m32 Travis CI build was broken: -m32 was overridden by -std=gnu99. This fixes that. --- CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 0dfa0857..057481bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,14 @@ cmake_minimum_required(VERSION 3.0) set(BUILD_EXAMPLES ON CACHE BOOL "Build the examples.") set(BUILD_GAMES ON CACHE BOOL "Build the example games.") +if(CMAKE_VERSION VERSION_LESS "3.1") + if(CMAKE_C_COMPILER_ID STREQUAL "GNU") + set(CMAKE_C_FLAGS "-std=gnu99 ${CMAKE_C_FLAGS}") + endif() +else() + set (CMAKE_C_STANDARD 99) +endif() + add_subdirectory(src release) if (${BUILD_EXAMPLES}) -- cgit v1.2.3 From 30ef3f3122dc05e8a950d21c4b098208d73e456b Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Thu, 18 Jan 2018 13:22:41 +0100 Subject: GCC/Clang: Treat void pointer arithmetic as error As an extension, GNU C treats sizeof(void) as 1. MSVC doesn't. Make it an error on GCC/Clang to avoid accidental MSVC breakage. --- CMakeLists.txt | 5 +++++ src/Makefile | 21 +++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 057481bd..82a1ad32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,11 @@ if(CMAKE_VERSION VERSION_LESS "3.1") else() set (CMAKE_C_STANDARD 99) endif() +include(CheckCCompilerFlag) +CHECK_C_COMPILER_FLAG("-Werror=pointer-arith" COMPILER_HAS_POINTER_ARITH_TOGGLE) +if(COMPILER_HAS_POINTER_ARITH_TOGGLE) + set(CMAKE_C_FLAGS "-Werror=pointer-arith ${CMAKE_C_FLAGS}") +endif() add_subdirectory(src release) diff --git a/src/Makefile b/src/Makefile index fbab6725..f27ec575 100644 --- a/src/Makefile +++ b/src/Makefile @@ -250,16 +250,17 @@ ifeq ($(PLATFORM),PLATFORM_ANDROID) endif # Define compiler flags: -# -O1 defines optimization level -# -g enable debugging -# -s strip unnecessary data from build -# -Wall turns on most, but not all, compiler warnings -# -std=c99 defines C language mode (standard C from 1999 revision) -# -std=gnu99 defines C language mode (GNU C from 1999 revision) -# -fgnu89-inline declaring inline functions support (GCC optimized) -# -Wno-missing-braces ignore invalid warning (GCC bug 53119) -# -D_DEFAULT_SOURCE use with -std=c99 on Linux and PLATFORM_WEB, required for timespec -CFLAGS += -O1 -Wall -std=c99 -D_DEFAULT_SOURCE -fgnu89-inline -Wno-missing-braces +# -O1 defines optimization level +# -g enable debugging +# -s strip unnecessary data from build +# -Wall turns on most, but not all, compiler warnings +# -std=c99 defines C language mode (standard C from 1999 revision) +# -std=gnu99 defines C language mode (GNU C from 1999 revision) +# -fgnu89-inline declaring inline functions support (GCC optimized) +# -Wno-missing-braces ignore invalid warning (GCC bug 53119) +# -D_DEFAULT_SOURCE use with -std=c99 on Linux and PLATFORM_WEB, required for timespec +# -Werror=pointer-arith catch unportable code that does direct arithmetic on void pointers +CFLAGS += -O1 -Wall -std=c99 -D_DEFAULT_SOURCE -fgnu89-inline -Wno-missing-braces -Werror=pointer-arith # Additional flags for compiler (if desired) #CFLAGS += -Wextra -Wmissing-prototypes -Wstrict-prototypes -- cgit v1.2.3 From 8be93762d9a94a56bf430e0e177bdefb5b1fa0dd Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Thu, 25 Jan 2018 22:34:29 +0100 Subject: Make function calls without prior declaration an error which is the default behavior on C99 and up. --- CMakeLists.txt | 14 ++++++++++---- src/Makefile | 3 ++- 2 files changed, 12 insertions(+), 5 deletions(-) mode change 100644 => 100755 CMakeLists.txt (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt old mode 100644 new mode 100755 index 82a1ad32..11db057e --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,10 +12,16 @@ else() set (CMAKE_C_STANDARD 99) endif() include(CheckCCompilerFlag) -CHECK_C_COMPILER_FLAG("-Werror=pointer-arith" COMPILER_HAS_POINTER_ARITH_TOGGLE) -if(COMPILER_HAS_POINTER_ARITH_TOGGLE) - set(CMAKE_C_FLAGS "-Werror=pointer-arith ${CMAKE_C_FLAGS}") -endif() +foreach(option -Werror=pointer-arith;-Werror=implicit-function-declaration) + CHECK_C_COMPILER_FLAG("${option}" COMPILER_HAS_THOSE_TOGGLES) + set(outcome "Failed") + if(COMPILER_HAS_THOSE_TOGGLES) + set(CMAKE_C_FLAGS "${option} ${CMAKE_C_FLAGS}") + set(outcome "works") + endif() + message(STATUS "Testing if ${option} can be used -- ${outcome}") +endforeach() + add_subdirectory(src release) diff --git a/src/Makefile b/src/Makefile index c88aa6dd..fc85ea0a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -262,7 +262,8 @@ endif # -Wno-missing-braces ignore invalid warning (GCC bug 53119) # -D_DEFAULT_SOURCE use with -std=c99 on Linux and PLATFORM_WEB, required for timespec # -Werror=pointer-arith catch unportable code that does direct arithmetic on void pointers -CFLAGS += -O1 -Wall -std=c99 -D_DEFAULT_SOURCE -fgnu89-inline -Wno-missing-braces -Werror=pointer-arith +# -Werror=implicit-function-declaration catch function calls without prior declaration +CFLAGS += -O1 -Wall -std=c99 -D_DEFAULT_SOURCE -fgnu89-inline -Wno-missing-braces -Werror=pointer-arith -Werror=implicit-function-declaration ifeq ($(RAYLIB_BUILD_MODE), DEBUG) CFLAGS += -g -- cgit v1.2.3 From c9043b5a872015bab4dc1b0606504341e9ce5b2b Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Sat, 24 Feb 2018 15:26:13 +0100 Subject: CMake: Add options to use -fsanitize={address,undefined} To make bugs like #485, #486, #487 and #488 easier to find in future. --- CMakeLists.txt | 31 +++++++++++++++++++++++-------- src/CMakeLists.txt | 8 ++++---- 2 files changed, 27 insertions(+), 12 deletions(-) (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 11db057e..0e70eab4 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,10 @@ cmake_minimum_required(VERSION 3.0) # Config options -set(BUILD_EXAMPLES ON CACHE BOOL "Build the examples.") -set(BUILD_GAMES ON CACHE BOOL "Build the example games.") +option(BUILD_EXAMPLES "Build the examples." ON) +option(BUILD_GAMES "Build the example games." ON) +option(ENABLE_ASAN "Enable AddressSanitizer (ASAN) for debugging (degrades performance)" OFF) +option(ENABLE_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan) for debugging" OFF) if(CMAKE_VERSION VERSION_LESS "3.1") if(CMAKE_C_COMPILER_ID STREQUAL "GNU") @@ -11,17 +13,31 @@ if(CMAKE_VERSION VERSION_LESS "3.1") else() set (CMAKE_C_STANDARD 99) endif() + include(CheckCCompilerFlag) -foreach(option -Werror=pointer-arith;-Werror=implicit-function-declaration) - CHECK_C_COMPILER_FLAG("${option}" COMPILER_HAS_THOSE_TOGGLES) +function(add_if_flag_works flag) + CHECK_C_COMPILER_FLAG("${flag}" COMPILER_HAS_THOSE_TOGGLES) set(outcome "Failed") if(COMPILER_HAS_THOSE_TOGGLES) - set(CMAKE_C_FLAGS "${option} ${CMAKE_C_FLAGS}") + foreach(var ${ARGN}) + set(${var} "${flag} ${${var}}" PARENT_SCOPE) + endforeach() set(outcome "works") endif() - message(STATUS "Testing if ${option} can be used -- ${outcome}") -endforeach() + message(STATUS "Testing if ${flag} can be used -- ${outcome}") +endfunction() +add_if_flag_works(-Werror=pointer-arith CMAKE_C_FLAGS) +add_if_flag_works(-Werror=implicit-function-declaration CMAKE_C_FLAGS) + +if (ENABLE_ASAN) + add_if_flag_works(-fno-omit-frame-pointer CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) + add_if_flag_works(-fsanitize=address CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) +endif() +if (ENABLE_UBSAN) + add_if_flag_works(-fno-omit-frame-pointer CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) + add_if_flag_works(-fsanitize=undefined CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) +endif() add_subdirectory(src release) @@ -32,4 +48,3 @@ endif() if (${BUILD_GAMES}) add_subdirectory(games) endif() - diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6f759324..db5d55c3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,11 +8,11 @@ set(RAYLIB raylib) # Name of the generated library ### Config options ### # Shared library is always PIC. Static library should be PIC too if linked into a shared library -set(WITH_PIC OFF CACHE BOOL "Compile static library as position-independent code" OFF) +option(WITH_PIC "Compile static library as position-independent code" OFF) # Build a static and/or shared raylib? -set(SHARED OFF CACHE BOOL "Build raylib as a dynamic library") -set(STATIC ON CACHE BOOL "Build raylib as a static library") -set(MACOS_FATLIB ON CACHE BOOL "Build fat library for both i386 and x86_64 on macOS") +option(SHARED "Build raylib as a dynamic library" OFF) +option(STATIC "Build raylib as a static library" ON) +option(MACOS_FATLIB "Build fat library for both i386 and x86_64 on macOS" ON) if(NOT (STATIC OR SHARED)) message(FATAL_ERROR "Nothing to do if both -DSHARED=OFF and -DSTATIC=OFF...") -- cgit v1.2.3 From 1430d0190684c6f78e1bce759eae240c94fba61c Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Sat, 24 Feb 2018 23:46:27 +0100 Subject: jar_xm: Workaround for unaligned pointer accesses jar_xm.h does some shady pointer casts leading to unaligned accesses and breaking strict aliasing. x86 has special circuitry for doing unaligned accesses, but on other architectures, it may trap and require kernel fix-up or crash outright. With this patch, one obstacle in porting raylib to the GameBoy Advance has been removed. Go for it ;-) To avoid having to rewrite that `mempool' code, insert padding before structs and instruct the compiler (GCC, most importantly), to be gentle when optimizing. This fixes #490 (Unless we got ourselves 256-bit pointers, if so, hello future!) --- CMakeLists.txt | 2 ++ src/Makefile | 4 +++- src/external/jar_xm.h | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e70eab4..1d98ffea 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,8 @@ endfunction() add_if_flag_works(-Werror=pointer-arith CMAKE_C_FLAGS) add_if_flag_works(-Werror=implicit-function-declaration CMAKE_C_FLAGS) +# src/external/jar_xm.h does shady stuff +add_if_flag_works(-fno-strict-aliasing CMAKE_C_FLAGS) if (ENABLE_ASAN) add_if_flag_works(-fno-omit-frame-pointer CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) diff --git a/src/Makefile b/src/Makefile index e001b4ef..53551174 100644 --- a/src/Makefile +++ b/src/Makefile @@ -291,7 +291,9 @@ endif # -Wno-missing-braces ignore invalid warning (GCC bug 53119) # -D_DEFAULT_SOURCE use with -std=c99 on Linux and PLATFORM_WEB, required for timespec # -Werror=pointer-arith catch unportable code that does direct arithmetic on void pointers -CFLAGS += -O1 -Wall -std=c99 -D_DEFAULT_SOURCE -Wno-missing-braces -Werror=pointer-arith +# -fno-strict-aliasing jar_xm.h does shady stuff (breaks strict aliasing) +CFLAGS += -O1 -Wall -std=c99 -D_DEFAULT_SOURCE -Wno-missing-braces -Werror=pointer-arith +-fno-strict-aliasing ifeq ($(RAYLIB_BUILD_MODE), DEBUG) CFLAGS += -g diff --git a/src/external/jar_xm.h b/src/external/jar_xm.h index 15b31fd0..df199e06 100644 --- a/src/external/jar_xm.h +++ b/src/external/jar_xm.h @@ -612,6 +612,8 @@ int jar_xm_create_context(jar_xm_context_t** ctxp, const char* moddata, uint32_t return jar_xm_create_context_safe(ctxp, moddata, SIZE_MAX, rate); } +#define ALIGN(x, b) (((x) + ((b) - 1)) & ~((b) - 1)) +#define ALIGN_PTR(x, b) (void*)(((uintptr_t)(x) + ((b) - 1)) & ~((b) - 1)) int jar_xm_create_context_safe(jar_xm_context_t** ctxp, const char* moddata, size_t moddata_length, uint32_t rate) { #if JAR_XM_DEFENSIVE int ret; @@ -644,9 +646,11 @@ int jar_xm_create_context_safe(jar_xm_context_t** ctxp, const char* moddata, siz ctx->rate = rate; mempool = jar_xm_load_module(ctx, moddata, moddata_length, mempool); + mempool = ALIGN_PTR(mempool, 16); ctx->channels = (jar_xm_channel_context_t*)mempool; mempool += ctx->module.num_channels * sizeof(jar_xm_channel_context_t); + mempool = ALIGN_PTR(mempool, 16); ctx->global_volume = 1.f; ctx->amplification = .25f; /* XXX: some bad modules may still clip. Find out something better. */ @@ -671,6 +675,7 @@ int jar_xm_create_context_safe(jar_xm_context_t** ctxp, const char* moddata, siz ch->actual_panning = .5f; } + mempool = ALIGN_PTR(mempool, 16); ctx->row_loop_count = (uint8_t*)mempool; mempool += MAX_NUM_ROWS * sizeof(uint8_t); @@ -856,9 +861,11 @@ size_t jar_xm_get_memory_needed_for_context(const char* moddata, size_t moddata_ num_patterns = READ_U16(offset + 10); memory_needed += num_patterns * sizeof(jar_xm_pattern_t); + memory_needed = ALIGN(memory_needed, 16); num_instruments = READ_U16(offset + 12); memory_needed += num_instruments * sizeof(jar_xm_instrument_t); + memory_needed = ALIGN(memory_needed, 16); memory_needed += MAX_NUM_ROWS * READ_U16(offset + 4) * sizeof(uint8_t); /* Module length */ @@ -875,6 +882,7 @@ size_t jar_xm_get_memory_needed_for_context(const char* moddata, size_t moddata_ /* Pattern header length + packed pattern data size */ offset += READ_U32(offset) + READ_U16(offset + 7); } + memory_needed = ALIGN(memory_needed, 16); /* Read instrument headers */ for(uint16_t i = 0; i < num_instruments; ++i) { @@ -940,9 +948,11 @@ char* jar_xm_load_module(jar_xm_context_t* ctx, const char* moddata, size_t modd mod->patterns = (jar_xm_pattern_t*)mempool; mempool += mod->num_patterns * sizeof(jar_xm_pattern_t); + mempool = ALIGN_PTR(mempool, 16); mod->instruments = (jar_xm_instrument_t*)mempool; mempool += mod->num_instruments * sizeof(jar_xm_instrument_t); + mempool = ALIGN_PTR(mempool, 16); uint16_t flags = READ_U32(offset + 14); mod->frequency_type = (flags & (1 << 0)) ? jar_xm_LINEAR_FREQUENCIES : jar_xm_AMIGA_FREQUENCIES; @@ -1032,6 +1042,7 @@ char* jar_xm_load_module(jar_xm_context_t* ctx, const char* moddata, size_t modd offset += packed_patterndata_size; } + mempool = ALIGN_PTR(mempool, 16); /* Read instruments */ for(uint16_t i = 0; i < ctx->module.num_instruments; ++i) { -- cgit v1.2.3 From 82491fcf6c70288652449f4ddeb8bdbe9628dd81 Mon Sep 17 00:00:00 2001 From: "maficccc@gmail.com" Date: Wed, 14 Mar 2018 00:13:23 +0100 Subject: Add memory sanitizer for better debug --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d98ffea..84977abb 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ option(BUILD_EXAMPLES "Build the examples." ON) option(BUILD_GAMES "Build the example games." ON) option(ENABLE_ASAN "Enable AddressSanitizer (ASAN) for debugging (degrades performance)" OFF) option(ENABLE_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan) for debugging" OFF) +option(ENABLE_MSAN "Enable MemorySanitizer (MSan) for debugging (not recommended for run with ASAN)" OFF) if(CMAKE_VERSION VERSION_LESS "3.1") if(CMAKE_C_COMPILER_ID STREQUAL "GNU") @@ -40,6 +41,10 @@ if (ENABLE_UBSAN) add_if_flag_works(-fno-omit-frame-pointer CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) add_if_flag_works(-fsanitize=undefined CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) endif() +if (ENABLE_MSAN) + add_if_flag_works(-fno-omit-frame-pointer CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) + add_if_flag_works(-fsanitize=memory CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) +endif() add_subdirectory(src release) -- cgit v1.2.3 From 4c0925067a93d6b011eda47f2204a926a76d37ed Mon Sep 17 00:00:00 2001 From: "maficccc@gmail.com" Date: Wed, 14 Mar 2018 15:23:14 +0100 Subject: Add message warning for msan --- CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 84977abb..6d5994f4 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,10 @@ if (ENABLE_MSAN) add_if_flag_works(-fsanitize=memory CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) endif() +if (ENABLE_MSAN AND ENABLE_ASAN) + MESSAGE(WARNING "Compiling with both AddressSanitizer and MemorySanitizer is not recommended") +endif() + add_subdirectory(src release) if (${BUILD_EXAMPLES}) -- cgit v1.2.3