Skip to content

Commit

Permalink
Several stability fixes
Browse files Browse the repository at this point in the history
- Fixed: ini CommandPrefix being overwritten by data from other ini settings.
- Fixed: function call stack sometimes being subject to a race condition between two writing threads, corrupting it.
- Fixed: lots of memory corruption errors leaving the server unstable when loading a broken save file and recovering by loading a backup save.
- Fixed: CCacheableScriptFile sometimes not reading the last character.
- Fixed: .clang_format.
- Changed: RES_GET_INDEX macro to ResGetIndex function, allowing more integer type safety. Same for RES_GET_TYPE.
- Changed: Renamed THREAD_* macros to MT_*. Split them in always active macros (MT_*) and macros active only when MT_ENGINE is set to 1 (MT_ENGINE_*). There are some situations where we still need a mutex
even if not working with multithreaded script/networking engines (which are NOT complete).
- Changed: Disable signal handlers under Linux if running with ASAN or a debugger.
- Added: NOEXCEPT_DEBUG, it's simply the noexcept specifier, but it's ignored if running in a debug build (useful if we're using DEBUG_ASSERT inside a function which wouldn't otherwise throw an exception).
- Fixed: some undefined behaviors, like signed to unsigned conversion and vice versa.
- Added: additional checks for GCC and Clang ASAN builds.
  • Loading branch information
cbnolok authored and cbnolok committed Jul 30, 2024
1 parent e550fe5 commit c1f8e44
Show file tree
Hide file tree
Showing 107 changed files with 1,322 additions and 893 deletions.
23 changes: 12 additions & 11 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BasedOnStyle: LLVM
Language: Cpp
Standard: Cpp20
Standard: c++20
TabWidth: 4
IndentWidth: 4
UseTab: Never
Expand Down Expand Up @@ -30,18 +30,19 @@ AllowShortLoopsOnASingleLine: false
##-- Braces
BreakBeforeBraces: Custom
BraceWrapping:
AfterEnum: false #Allman
AfterFunction: false #Allman
AfterNamespace: false #Allman
AfterStruct: false #Allman
AfterUnion: false #Allman
BeforeCatch: false #Allman
#Allman
AfterControlStatement: MultiLine
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeLambdaBody: true
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
AfterControlStatement: Multiline
Cpp11BracedListStyle: false

##-- Line breaks
Expand All @@ -62,7 +63,7 @@ IndentCaseBlocks: false
IndentCaseLabels: true
IndentGotoLabels: true
IndentPPDirectives: AfterHash
LambdaBodyIndentationKind: Signature
LambdaBodyIndentation: Signature
NamespaceIndentation: Inner

##-- Spaces in/between statements/etc
Expand All @@ -84,16 +85,16 @@ SpacesInSquareBrackets: false
##-- Misc
AllowAllParametersOfDeclarationOnNextLine: true
AccessModifierOffset: -4
AttributeBreakingStyle: Always #Leave
BinPackArguments: false
BinPackParameters: false
BreakAfterAttributes: Always
ExperimentalAutoDetectBinPacking: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
DisableFormat: false
InsertNewlineAtEOF: true
KeepEmptyLinesAtTheStartOfBlocks: false
ReflowComments: false
SeparateDefinitionBlocks: true
SeparateDefinitionBlocks: Always

#-- Include order
#IncludeIsMainRegex: '$?'
Expand Down
34 changes: 24 additions & 10 deletions cmake/toolchains/include/Linux-Clang_common.inc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@ function (toolchain_exe_stuff_common)


IF (${USE_ASAN})
SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} -fsanitize=address -fno-sanitize-recover=address -fsanitize-address-use-after-scope)
SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} # -fsanitize=safe-stack # Can't be used with asan!
-fsanitize=address -fno-sanitize-recover=address
-fsanitize-address-use-after-scope -fsanitize=pointer-compare -fsanitize=pointer-subtract
# Flags for additional instrumentation not strictly needing asan to be enabled
-fcf-protection=full -fstack-check -fstack-protector-all -fstack-clash-protection
# Not supported by Clang, but supported by GCC
#-fvtable-verify=preinit -fharden-control-flow-redundancy -fhardcfr-check-exceptions
# Other
#-fsanitize-trap=all
)
set (CMAKE_EXE_LINKER_FLAGS_EXTRA ${CMAKE_EXE_LINKER_FLAGS_EXTRA} -fsanitize=address)
SET (ENABLED_SANITIZER true)
ENDIF ()
Expand All @@ -72,9 +81,12 @@ See comments in the toolchain and: https://github.com/google/sanitizers/wiki/Mem
ENDIF ()
IF (${USE_UBSAN})
SET (UBSAN_FLAGS
-fsanitize=undefined,shift,integer-divide-by-zero,vla-bound,null,signed-integer-overflow,bounds
-fsanitize=float-divide-by-zero,float-cast-overflow,pointer-overflow,unreachable,nonnull-attribute,returns-nonnull-attribute
-fno-sanitize=enum)
-fsanitize=undefined,float-divide-by-zero,local-bounds
-fno-sanitize=enum
# Supported?
-fsanitize=unsigned-integer-overflow #Unlike signed integer overflow, this is not undefined behavior, but it is often unintentional.
-fsanitize=implicit-conversion
)
SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} ${UBSAN_FLAGS} -fsanitize=return,vptr)
set (CMAKE_EXE_LINKER_FLAGS_EXTRA ${CMAKE_EXE_LINKER_FLAGS_EXTRA} -fsanitize=undefined)
SET (ENABLED_SANITIZER true)
Expand All @@ -91,9 +103,11 @@ See comments in the toolchain and: https://github.com/google/sanitizers/wiki/Mem
set (cxx_local_opts_warnings
-Wall -Wextra -Wno-unknown-pragmas -Wno-switch -Wno-implicit-fallthrough
-Wno-parentheses -Wno-misleading-indentation -Wno-conversion-null -Wno-unused-result
# clang-specific:
-Wno-format-security
)
-Wno-format-security # TODO: disable that when we'll have time to fix every printf format issue

# clang -specific:
# -fforce-emit-vtables
)
set (cxx_local_opts
-std=c++20 -pthread -fexceptions -fnon-call-exceptions
-pipe -ffast-math
Expand Down Expand Up @@ -122,17 +136,17 @@ See comments in the toolchain and: https://github.com/google/sanitizers/wiki/Mem
SET (COMPILE_OPTIONS_EXTRA -fno-omit-frame-pointer -fno-inline)
ENDIF ()
IF (TARGET spheresvr_release)
TARGET_COMPILE_OPTIONS ( spheresvr_release PUBLIC -O3 ${COMPILE_OPTIONS_EXTRA})
TARGET_COMPILE_OPTIONS ( spheresvr_release PUBLIC -O3 -flto=full -fvirtual-function-elimination ${COMPILE_OPTIONS_EXTRA})
ENDIF ()
IF (TARGET spheresvr_nightly)
IF (ENABLED_SANITIZER)
TARGET_COMPILE_OPTIONS ( spheresvr_nightly PUBLIC -ggdb3 -O1 ${COMPILE_OPTIONS_EXTRA})
ELSE ()
TARGET_COMPILE_OPTIONS ( spheresvr_nightly PUBLIC -O3 ${COMPILE_OPTIONS_EXTRA})
TARGET_COMPILE_OPTIONS ( spheresvr_nightly PUBLIC -O3 -flto=full -fvirtual-function-elimination ${COMPILE_OPTIONS_EXTRA})
ENDIF ()
ENDIF ()
IF (TARGET spheresvr_debug)
TARGET_COMPILE_OPTIONS ( spheresvr_debug PUBLIC -ggdb3 -Og ${COMPILE_OPTIONS_EXTRA})
TARGET_COMPILE_OPTIONS ( spheresvr_debug PUBLIC -ggdb3 -O1 ${COMPILE_OPTIONS_EXTRA})
ENDIF ()


Expand Down
23 changes: 18 additions & 5 deletions cmake/toolchains/include/Linux-GNU_common.inc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ function (toolchain_exe_stuff_common)
SET (ENABLED_SANITIZER false)

IF (${USE_ASAN})
SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} -fsanitize=address -fno-sanitize-recover=address -fsanitize-address-use-after-scope)
SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} -fsanitize=address -fno-sanitize-recover=address
-fsanitize-address-use-after-scope -fsanitize=pointer-compare -fsanitize=pointer-subtract
# Flags for additional instrumentation not strictly needing asan to be enabled
#-fvtable-verify=preinit # This causes a GCC internal error! Tested with 13.2.0
-fstack-check -fstack-protector-all
-fcf-protection=full
# GCC 14?
#-fharden-control-flow-redundancy -fhardcfr-check-exceptions
# Other
#-fsanitize-trap=all
)
set (CMAKE_EXE_LINKER_FLAGS_EXTRA ${CMAKE_EXE_LINKER_FLAGS_EXTRA} -fsanitize=address $<$<BOOL:${RUNTIME_STATIC_LINK}>:-static-libasan>)
SET (ENABLED_SANITIZER true)
ENDIF ()
Expand All @@ -69,9 +79,11 @@ function (toolchain_exe_stuff_common)
ENDIF ()
IF (${USE_UBSAN})
SET (UBSAN_FLAGS
-fsanitize=undefined,shift,integer-divide-by-zero,vla-bound,null,signed-integer-overflow,bounds-strict
-fsanitize=float-divide-by-zero,float-cast-overflow,pointer-overflow,unreachable,nonnull-attribute,returns-nonnull-attribute
-fsanitize=undefined,float-divide-by-zero
-fno-sanitize=enum
# Unsupported (yet?) by GCC 13
#-fsanitize=unsigned-integer-overflow #Unlike signed integer overflow, this is not undefined behavior, but it is often unintentional.
#-fsanitize=implicit-conversion, local-bounds
)
SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} ${UBSAN_FLAGS} -fsanitize=return,vptr)
set (CMAKE_EXE_LINKER_FLAGS_EXTRA ${CMAKE_EXE_LINKER_FLAGS_EXTRA} -fsanitize=undefined $<$<BOOL:${RUNTIME_STATIC_LINK}>:-static-libubsan>)
Expand All @@ -87,7 +99,8 @@ function (toolchain_exe_stuff_common)

set (cxx_local_opts_warnings
-Wall -Wextra -Wno-nonnull-compare -Wno-unknown-pragmas -Wno-switch -Wno-implicit-fallthrough
-Wno-parentheses -Wno-format-security -Wno-misleading-indentation -Wno-conversion-null -Wno-unused-result
-Wno-parentheses -Wno-misleading-indentation -Wno-conversion-null -Wno-unused-result
-Wno-format-security # TODO: disable that when we'll have time to fix every printf format issue
)
set (cxx_local_opts
-std=c++20 -pthread -fexceptions -fnon-call-exceptions
Expand All @@ -114,7 +127,7 @@ function (toolchain_exe_stuff_common)
ENDIF ()
ENDIF ()
IF (TARGET spheresvr_debug)
TARGET_COMPILE_OPTIONS ( spheresvr_debug PUBLIC -ggdb3 -Og ${COMPILE_OPTIONS_EXTRA})
TARGET_COMPILE_OPTIONS ( spheresvr_debug PUBLIC -ggdb3 -O1 ${COMPILE_OPTIONS_EXTRA})
ENDIF ()


Expand Down
25 changes: 18 additions & 7 deletions cmake/toolchains/include/OSX-AppleClang_common.inc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ function (toolchain_exe_stuff_common)
# -static-libsan Statically link the sanitizer runtime (Not supported for ASan, TSan or UBSan on darwin)

IF (${USE_ASAN})
SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} -fsanitize=address -fno-sanitize-recover=address -fsanitize-address-use-after-scope)
SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} # -fsanitize=safe-stack # Can't be used with asan!
-fsanitize=address -fno-sanitize-recover=address
-fsanitize-address-use-after-scope -fsanitize=pointer-compare -fsanitize=pointer-subtract
# Flags for additional instrumentation not strictly needing asan to be enabled
-fcf-protection=full -fstack-check -fstack-protector-all -fstack-clash-protection
# Not supported by Clang, but supported by GCC
#-fvtable-verify=preinit -fharden-control-flow-redundancy -fhardcfr-check-exceptions
# Other
#-fsanitize-trap=all
)
set (CMAKE_EXE_LINKER_FLAGS_EXTRA ${CMAKE_EXE_LINKER_FLAGS_EXTRA} -fsanitize=address )
SET (ENABLED_SANITIZER true)
ENDIF ()
Expand All @@ -55,11 +64,13 @@ function (toolchain_exe_stuff_common)
SET (ENABLED_SANITIZER true)
ENDIF ()
IF (${USE_UBSAN})
SET (UBSAN_FLAGS
-fsanitize=undefined,shift,integer-divide-by-zero,vla-bound,null,signed-integer-overflow,bounds
-fsanitize=float-divide-by-zero,float-cast-overflow,pointer-overflow,unreachable,nonnull-attribute,returns-nonnull-attribute
-fno-sanitize=enum
)
SET (UBSAN_FLAGS
-fsanitize=undefined,float-divide-by-zero,local-bounds
-fno-sanitize=enum
# Supported?
-fsanitize=unsigned-integer-overflow #Unlike signed integer overflow, this is not undefined behavior, but it is often unintentional.
-fsanitize=implicit-conversion
)
SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} ${UBSAN_FLAGS} -fsanitize=return,vptr)
set (CMAKE_EXE_LINKER_FLAGS_EXTRA ${CMAKE_EXE_LINKER_FLAGS_EXTRA} -fsanitize=undefined)
SET (ENABLED_SANITIZER true)
Expand All @@ -75,8 +86,8 @@ function (toolchain_exe_stuff_common)
set (cxx_local_opts_warnings
-Wall -Wextra -Wno-unknown-pragmas -Wno-switch -Wno-implicit-fallthrough
-Wno-parentheses -Wno-misleading-indentation -Wno-conversion-null -Wno-unused-result
-Wno-format-security # TODO: disable that when we'll have time to fix every printf format issue
# clang-specific:
-Wno-format-security
-Wno-deprecated-declarations # spams so much warnings for the use of sprintf
)
set (cxx_local_opts
Expand Down
40 changes: 28 additions & 12 deletions cmake/toolchains/include/Windows-Clang_common.inc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,16 @@ function (toolchain_exe_stuff_common)
# -static-libsan Statically link the sanitizer runtime (Not supported for ASan, TSan or UBSan on darwin)

IF (${USE_ASAN})
SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} -fsanitize=address -fno-sanitize-recover=address -fsanitize-address-use-after-scope)
SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} # -fsanitize=safe-stack # Can't be used with asan!
-fsanitize=address -fno-sanitize-recover=address
-fsanitize-address-use-after-scope -fsanitize=pointer-compare -fsanitize=pointer-subtract
# Flags for additional instrumentation not strictly needing asan to be enabled
-fcf-protection=full -fstack-check -fstack-protector-all -fstack-clash-protection
# Not supported by Clang, but supported by GCC
#-fvtable-verify=preinit -fharden-control-flow-redundancy -fhardcfr-check-exceptions
# Other
#-fsanitize-trap=all
)
IF (${CLANG_USE_GCC_LINKER})
set (CMAKE_EXE_LINKER_FLAGS_EXTRA ${CMAKE_EXE_LINKER_FLAGS_EXTRA} -fsanitize=address)
ENDIF ()
Expand All @@ -69,11 +78,13 @@ function (toolchain_exe_stuff_common)
#SET (ENABLED_SANITIZER true)
ENDIF ()
IF (${USE_UBSAN})
SET (UBSAN_FLAGS
-fsanitize=undefined,shift,integer-divide-by-zero,vla-bound,null,signed-integer-overflow,bounds
-fsanitize=float-divide-by-zero,float-cast-overflow,pointer-overflow,unreachable,nonnull-attribute,returns-nonnull-attribute
-fno-sanitize=enum
)
SET (UBSAN_FLAGS
-fsanitize=undefined,float-divide-by-zero,local-bounds
-fno-sanitize=enum
# Supported?
-fsanitize=unsigned-integer-overflow #Unlike signed integer overflow, this is not undefined behavior, but it is often unintentional.
-fsanitize=implicit-conversion
)
SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} ${UBSAN_FLAGS} -fsanitize=return)
#IF (${CLANG_USE_GCC_LINKER})
#set (CMAKE_EXE_LINKER_FLAGS_EXTRA ${CMAKE_EXE_LINKER_FLAGS_EXTRA} -fsanitize=undefined)
Expand All @@ -93,18 +104,23 @@ function (toolchain_exe_stuff_common)
#-- Store compiler flags common to all builds.

SET (cxx_local_opts_warnings
-Wall -Wextra -Wno-unknown-pragmas -Wno-switch -Wno-parentheses -Wno-conversion-null
-Wno-misleading-indentation -Wno-implicit-fallthrough
)
-Wall -Wextra -Wno-unknown-pragmas -Wno-switch -Wno-implicit-fallthrough
-Wno-parentheses -Wno-misleading-indentation -Wno-conversion-null -Wno-unused-result
-Wno-format-security # TODO: disable that when we'll have time to fix every printf format issue

# clang -specific:
# -fforce-emit-vtables
)
SET (cxx_local_opts
-std=c++20 -fexceptions -fnon-call-exceptions
-pipe -ffast-math
-mno-ms-bitfields
# -mno-ms-bitfields is needed to fix structure packing;
# -pthread unused here? we only need to specify that to the linker?
-Wno-format-security # TODO: disable that when we'll have time to fix every printf format issue

# clang-specific:
-Wno-format-security
#

# Flags supported by GCC but not by Clang: -fno-expensive-optimizations, -Wno-error=maybe-uninitialized
)
Expand All @@ -119,7 +135,7 @@ function (toolchain_exe_stuff_common)
SET (COMPILE_OPTIONS_EXTRA -fno-omit-frame-pointer -fno-inline)
ENDIF ()
IF (TARGET spheresvr_release)
TARGET_COMPILE_OPTIONS ( spheresvr_release PUBLIC -O3 ${COMPILE_OPTIONS_EXTRA})
TARGET_COMPILE_OPTIONS ( spheresvr_release PUBLIC -O3 -flto=full -fvirtual-function-elimination ${COMPILE_OPTIONS_EXTRA})
#[[
IF (NOT ${CLANG_USE_GCC_LINKER})
if (${RUNTIME_STATIC_LINK})
Expand All @@ -134,7 +150,7 @@ function (toolchain_exe_stuff_common)
IF (ENABLED_SANITIZER)
TARGET_COMPILE_OPTIONS ( spheresvr_nightly PUBLIC -ggdb3 -O1 ${COMPILE_OPTIONS_EXTRA} )
ELSE ()
TARGET_COMPILE_OPTIONS ( spheresvr_nightly PUBLIC -O3 ${COMPILE_OPTIONS_EXTRA} )
TARGET_COMPILE_OPTIONS ( spheresvr_nightly PUBLIC -O3 -flto=full -fvirtual-function-elimination ${COMPILE_OPTIONS_EXTRA} )
ENDIF ()
#[[
IF (NOT ${CLANG_USE_GCC_LINKER})
Expand Down
30 changes: 24 additions & 6 deletions cmake/toolchains/include/Windows-GNU_common.inc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ function (toolchain_exe_stuff_common)
IF (${USE_ASAN})
MESSAGE (FATAL_ERROR "MinGW-GCC doesn't yet support ASAN")
SET (USE_ASAN false)
#SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} -fsanitize=address -fsanitize-address-use-after-scope)
#[[
SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} -fsanitize=address -fno-sanitize-recover=address
-fsanitize-address-use-after-scope -fsanitize=pointer-compare -fsanitize=pointer-subtract
# Flags for additional instrumentation not strictly needing asan to be enabled
#-fvtable-verify=preinit # This causes a GCC internal error! Tested with 13.2.0
-fstack-check -fstack-protector-all
-fcf-protection=full
# GCC 14?
#-fharden-control-flow-redundancy -fhardcfr-check-exceptions
# Other
#-fsanitize-trap=all
)
]]
#set (CMAKE_EXE_LINKER_FLAGS_EXTRA ${CMAKE_EXE_LINKER_FLAGS_EXTRA} -fsanitize=address $<$<BOOL:${RUNTIME_STATIC_LINK}>:-static-libasan>)
#SET (ENABLED_SANITIZER true)
ENDIF ()
Expand All @@ -52,10 +64,15 @@ function (toolchain_exe_stuff_common)
IF (${USE_UBSAN})
MESSAGE (FATAL_ERROR "MinGW-GCC doesn't yet support UBSAN")
SET (USE_UBSAN false)
# SET (UBSAN_FLAGS
# -fsanitize=undefined,#shift,integer-divide-by-zero,vla-bound,null,signed-integer-overflow,bounds
# -fsanitize=float-divide-by-zero,float-cast-overflow,pointer-overflow,unreachable,nonnull-attribute,returns-nonnull-attribute
# -fno-sanitize=enum)
#[[
SET (UBSAN_FLAGS
-fsanitize=undefined,float-divide-by-zero
-fno-sanitize=enum
# Unsupported (yet?) by GCC 13
#-fsanitize=unsigned-integer-overflow #Unlike signed integer overflow, this is not undefined behavior, but it is often unintentional.
#-fsanitize=implicit-conversion, local-bounds
)
]]
#SET (CXX_FLAGS_EXTRA ${CXX_FLAGS_EXTRA} ${UBSAN_FLAGS} -fsanitize=return,vptr)
#set (CMAKE_EXE_LINKER_FLAGS_EXTRA ${CMAKE_EXE_LINKER_FLAGS_EXTRA} -fsanitize=undefined #$<$<BOOL:${RUNTIME_STATIC_LINK}>:-static-libubsan>)
#SET (ENABLED_SANITIZER true)
Expand All @@ -69,7 +86,8 @@ function (toolchain_exe_stuff_common)

set (cxx_local_opts_warnings
-Wall -Wextra -Wno-nonnull-compare -Wno-unknown-pragmas -Wno-switch -Wno-implicit-fallthrough
-Wno-parentheses -Wno-format-security -Wno-misleading-indentation -Wno-conversion-null -Wno-unused-result
-Wno-parentheses -Wno-misleading-indentation -Wno-conversion-null -Wno-unused-result
-Wno-format-security # TODO: disable that when we'll have time to fix every printf format issue
)
set (cxx_local_opts
-std=c++20 -pthread -fexceptions -fnon-call-exceptions -mno-ms-bitfields
Expand Down
Loading

0 comments on commit c1f8e44

Please sign in to comment.