From 74db1cbcaf27b317cb1e1e15029d717b99080627 Mon Sep 17 00:00:00 2001 From: liberize Date: Thu, 5 Dec 2024 21:20:09 +0800 Subject: [PATCH] don't opt crc magic --- src/main.cpp | 11 +++++++++-- src/utils.h | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e25a812..4becc5d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,14 @@ enum ScriptFormat { LUA, }; +#ifdef VERIFY_CHECKSUM +DISABLE_OPTIMIZATION FORCE_INLINE uint32_t* get_cksum_data() { + static uint32_t cksum_data[2]; + memcpy(cksum_data, "ssccksum", 8); + return cksum_data; +} +#endif + int main(int argc, char* argv[]) { #ifdef UNTRACEABLE check_debugger(true); @@ -50,8 +58,7 @@ int main(int argc, char* argv[]) { std::string exe_path = get_exe_path(); #ifdef VERIFY_CHECKSUM - static uint32_t cksum_data[2]; // use static to prevent compiler from optimizing - memcpy(cksum_data, "ssccksum", 8); + auto cksum_data = get_cksum_data(); if (is_big_endian()) { cksum_data[0] = byteswap32(cksum_data[0]); cksum_data[1] = byteswap32(cksum_data[1]); diff --git a/src/utils.h b/src/utils.h index 59b58a9..700c354 100644 --- a/src/utils.h +++ b/src/utils.h @@ -22,6 +22,14 @@ #define FORCE_INLINE __attribute__((always_inline)) inline +#if defined(__GNUC__) +#define DISABLE_OPTIMIZATION __attribute__((optimize("O0"))) +#elif defined(__clang__) +#define DISABLE_OPTIMIZATION [[clang::optnone]] +#else +#define DISABLE_OPTIMIZATION +#endif + #define LOGD(fmt, ...) /* fprintf(stdout, OBF(fmt "\n"), ##__VA_ARGS__) */ #define LOGE(fmt, ...) fprintf(stderr, OBF(fmt "\n"), ##__VA_ARGS__)