aboutsummaryrefslogtreecommitdiff
path: root/src/zencore
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2025-11-07 14:49:13 +0100
committerGitHub Enterprise <[email protected]>2025-11-07 14:49:13 +0100
commit24e43a913f29ac3b314354e8ce5175f135bcc64f (patch)
treeca442937ceeb63461012b33a4576e9835099f106 /src/zencore
parentget oplog attachments (#622) (diff)
downloadzen-24e43a913f29ac3b314354e8ce5175f135bcc64f.tar.xz
zen-24e43a913f29ac3b314354e8ce5175f135bcc64f.zip
switch to xmake for package management (#611)
This change removes our dependency on vcpkg for package management, in favour of bringing some code in-tree in the `thirdparty` folder as well as using the xmake build-in package management feature. For the latter, all the package definitions are maintained in the zen repo itself, in the `repo` folder. It should now also be easier to build the project as it will no longer depend on having the right version of vcpkg installed, which has been a common problem for new people coming in to the codebase. Now you should only need xmake to build. * Bumps xmake requirement on github runners to 2.9.9 to resolve an issue where xmake on Windows invokes cmake with `v144` toolchain which does not exist * BLAKE3 is now in-tree at `thirdparty/blake3` * cpr is now in-tree at `thirdparty/cpr` * cxxopts is now in-tree at `thirdparty/cxxopts` * fmt is now in-tree at `thirdparty/fmt` * robin-map is now in-tree at `thirdparty/robin-map` * ryml is now in-tree at `thirdparty/ryml` * sol2 is now in-tree at `thirdparty/sol2` * spdlog is now in-tree at `thirdparty/spdlog` * utfcpp is now in-tree at `thirdparty/utfcpp` * xmake package repo definitions is in `repo` * implemented support for sanitizers. ASAN is supported on windows, TSAN, UBSAN, MSAN etc are supported on Linux/MacOS though I have not yet tested it extensively on MacOS * the zencore encryption implementation also now supports using mbedTLS which is used on MacOS, though for now we still use openssl on Linux * crashpad * bumps libcurl to 8.11.0 (from 8.8.0) which should address a rare build upload bug
Diffstat (limited to 'src/zencore')
-rw-r--r--src/zencore/compactbinaryyaml.cpp4
-rw-r--r--src/zencore/crypto.cpp127
-rw-r--r--src/zencore/include/zencore/memory/fmalloc.h47
-rw-r--r--src/zencore/include/zencore/memory/newdelete.h45
-rw-r--r--src/zencore/include/zencore/xxhash.h2
-rw-r--r--src/zencore/memory/memory.cpp38
-rw-r--r--src/zencore/xmake.lua36
7 files changed, 257 insertions, 42 deletions
diff --git a/src/zencore/compactbinaryyaml.cpp b/src/zencore/compactbinaryyaml.cpp
index 3a9705684..5122e952a 100644
--- a/src/zencore/compactbinaryyaml.cpp
+++ b/src/zencore/compactbinaryyaml.cpp
@@ -15,8 +15,8 @@
#include <vector>
ZEN_THIRD_PARTY_INCLUDES_START
-#include <ryml/ryml.hpp>
-#include <ryml/ryml_std.hpp>
+#include <ryml.hpp>
+#include <ryml_std.hpp>
ZEN_THIRD_PARTY_INCLUDES_END
namespace zen {
diff --git a/src/zencore/crypto.cpp b/src/zencore/crypto.cpp
index 78bea0c17..09eebb6ae 100644
--- a/src/zencore/crypto.cpp
+++ b/src/zencore/crypto.cpp
@@ -9,14 +9,37 @@
#include <string>
#include <string_view>
+// On Windows we can use the built-in BCrypt API, on other platforms we currently
+// support either OpenSSL or mbedTLS. The preference is to use OpenSSL if available
+// mostly for historical reasons. We should investigate making mbedTLS the default
+// on non-Windows platforms in future as it is more lightweight.
+
+#if ZEN_PLATFORM_WINDOWS
+# define ZEN_USE_BCRYPT 1
+#else
+# define ZEN_USE_BCRYPT 0
+#endif
+
#ifndef ZEN_USE_OPENSSL
-# if ZEN_PLATFORM_WINDOWS
+# if ZEN_USE_BCRYPT
# define ZEN_USE_OPENSSL 0
# else
# define ZEN_USE_OPENSSL 1
# endif
#endif
+#ifndef ZEN_USE_MBEDTLS
+# if ZEN_PLATFORM_WINDOWS
+# define ZEN_USE_MBEDTLS 0
+# elif !ZEN_USE_OPENSSL
+# define ZEN_USE_MBEDTLS 1
+# else
+# define ZEN_USE_MBEDTLS 0
+# endif
+#endif
+
+static_assert(ZEN_USE_OPENSSL + ZEN_USE_MBEDTLS + ZEN_USE_BCRYPT <= 1, "Only one crypto backend can be selected");
+
ZEN_THIRD_PARTY_INCLUDES_START
#include <fmt/format.h>
@@ -24,6 +47,8 @@ ZEN_THIRD_PARTY_INCLUDES_START
# include <openssl/conf.h>
# include <openssl/err.h>
# include <openssl/evp.h>
+#elif ZEN_USE_MBEDTLS
+# include <mbedtls/cipher.h>
#else
# include <zencore/windows.h>
# include <bcrypt.h>
@@ -43,8 +68,104 @@ namespace crypto {
Encrypt
};
-#if ZEN_USE_OPENSSL
+#if ZEN_USE_MBEDTLS
+
+ class MbedCipherCtx
+ {
+ public:
+ MbedCipherCtx() { mbedtls_cipher_init(&m_Ctx); }
+ ~MbedCipherCtx() { mbedtls_cipher_free(&m_Ctx); }
+
+ mbedtls_cipher_context_t* operator&() { return &m_Ctx; }
+ mbedtls_cipher_context_t* get() { return &m_Ctx; }
+
+ private:
+ mbedtls_cipher_context_t m_Ctx;
+ };
+
+ MemoryView Transform(TransformMode Mode,
+ MemoryView Key,
+ MemoryView IV,
+ MemoryView In,
+ MutableMemoryView Out,
+ std::optional<std::string>& Reason)
+ {
+ const mbedtls_cipher_info_t* CipherInfo = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CBC);
+ if (CipherInfo == nullptr)
+ {
+ Reason = "failed to get mbedTLS cipher info"sv;
+ return MemoryView();
+ }
+
+ MbedCipherCtx Ctx;
+ int ret = mbedtls_cipher_setup(Ctx.get(), CipherInfo);
+ if (ret != 0)
+ {
+ Reason = fmt::format("mbedTLS cipher setup failed, ret={}", ret);
+ return MemoryView();
+ }
+
+ // key length in bits
+ ret = mbedtls_cipher_setkey(Ctx.get(),
+ reinterpret_cast<const unsigned char*>(Key.GetData()),
+ static_cast<int>(Key.GetSize() * 8),
+ (Mode == TransformMode::Encrypt) ? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT);
+ if (ret != 0)
+ {
+ Reason = fmt::format("mbedTLS setkey failed, ret={}", ret);
+ return MemoryView();
+ }
+
+ ret = mbedtls_cipher_set_iv(Ctx.get(), reinterpret_cast<const unsigned char*>(IV.GetData()), static_cast<size_t>(IV.GetSize()));
+ if (ret != 0)
+ {
+ Reason = fmt::format("mbedTLS set_iv failed, ret={}", ret);
+ return MemoryView();
+ }
+
+ ret = mbedtls_cipher_set_padding_mode(Ctx.get(), MBEDTLS_PADDING_PKCS7);
+
+ if (ret != 0)
+ {
+ Reason = fmt::format("mbedTLS padding mode configuration failed, ret={}", ret);
+ return MemoryView();
+ }
+ ret = mbedtls_cipher_reset(Ctx.get());
+ if (ret != 0)
+ {
+ Reason = fmt::format("mbedTLS reset failed, ret={}", ret);
+ return MemoryView();
+ }
+
+ size_t olen = 0;
+ size_t total = 0;
+
+ ret = mbedtls_cipher_update(Ctx.get(),
+ reinterpret_cast<const unsigned char*>(In.GetData()),
+ static_cast<size_t>(In.GetSize()),
+ reinterpret_cast<unsigned char*>(Out.GetData()),
+ &olen);
+ if (ret != 0)
+ {
+ Reason = fmt::format("mbedTLS update failed, ret={}", ret);
+ return MemoryView();
+ }
+
+ total = olen;
+
+ ret = mbedtls_cipher_finish(Ctx.get(), reinterpret_cast<unsigned char*>(Out.GetData()) + total, &olen);
+ if (ret != 0)
+ {
+ Reason = fmt::format("mbedTLS finish failed, ret={}", ret);
+ return MemoryView();
+ }
+
+ total += olen;
+
+ return Out.Left(static_cast<size_t>(total));
+ }
+#elif ZEN_USE_OPENSSL
class EvpContext
{
public:
@@ -368,7 +489,9 @@ TEST_CASE("crypto.aes")
DecryptionBuffer.resize(PlainText.size() + Aes::BlockSize);
MemoryView EncryptedView = Aes::Encrypt(Key, IV, MakeMemoryView(PlainText), MakeMutableMemoryView(EncryptionBuffer), Reason);
+ CHECK(Reason.has_value() == false);
MemoryView DecryptedView = Aes::Decrypt(Key, IV, EncryptedView, MakeMutableMemoryView(DecryptionBuffer), Reason);
+ CHECK(Reason.has_value() == false);
std::string_view EncryptedDecryptedText =
std::string_view(reinterpret_cast<const char*>(DecryptedView.GetData()), DecryptedView.GetSize());
diff --git a/src/zencore/include/zencore/memory/fmalloc.h b/src/zencore/include/zencore/memory/fmalloc.h
index 5b476429e..0c183cfd0 100644
--- a/src/zencore/include/zencore/memory/fmalloc.h
+++ b/src/zencore/include/zencore/memory/fmalloc.h
@@ -6,6 +6,53 @@
#include <zenbase/zenbase.h>
+// Detect if any sanitizers are enabled.
+
+#if defined(__has_feature)
+# if __has_feature(address_sanitizer)
+# define ZEN_ADDRESS_SANITIZER 1
+# endif
+# if __has_feature(thread_sanitizer)
+# define ZEN_THREAD_SANITIZER 1
+# endif
+# if __has_feature(memory_sanitizer)
+# define ZEN_MEMORY_SANITIZER 1
+# endif
+# if __has_feature(leak_sanitizer)
+# define ZEN_LEAK_SANITIZER 1
+# endif
+#elif defined(__SANITIZE_ADDRESS__) // For Windows
+# define ZEN_ADDRESS_SANITIZER 1
+#endif
+
+#if !defined(ZEN_ADDRESS_SANITIZER)
+# define ZEN_ADDRESS_SANITIZER 0
+#endif
+
+#if !defined(ZEN_THREAD_SANITIZER)
+# define ZEN_THREAD_SANITIZER 0
+#endif
+
+#if !defined(ZEN_MEMORY_SANITIZER)
+# define ZEN_MEMORY_SANITIZER 0
+#endif
+
+#if !defined(ZEN_LEAK_SANITIZER)
+# define ZEN_LEAK_SANITIZER 0
+#endif
+
+// If sanitizers are enabled, we cannot override new/delete ourselves
+
+#if !defined(ZEN_OVERRIDE_NEW_DELETE)
+# if ZEN_ADDRESS_SANITIZER || ZEN_THREAD_SANITIZER || ZEN_MEMORY_SANITIZER || ZEN_LEAK_SANITIZER
+# define ZEN_OVERRIDE_NEW_DELETE 0
+# endif
+#endif
+
+#if !defined(ZEN_OVERRIDE_NEW_DELETE)
+# define ZEN_OVERRIDE_NEW_DELETE 1
+#endif
+
namespace zen {
enum
diff --git a/src/zencore/include/zencore/memory/newdelete.h b/src/zencore/include/zencore/memory/newdelete.h
index 2ec92b91b..a5b65ccbb 100644
--- a/src/zencore/include/zencore/memory/newdelete.h
+++ b/src/zencore/include/zencore/memory/newdelete.h
@@ -2,18 +2,25 @@
#pragma once
+// This header overrides global new/delete operators to route them through Zen's memory
+// allocation system. This header should only be included once, from the program's main
+// compilation unit
+
#include <zenbase/zenbase.h>
+#include <zencore/memory/fmalloc.h>
#include <new>
-#if defined(_MSC_VER)
-# if (_MSC_VER >= 1900) && !defined(__EDG__)
-# define ZEN_RESTRICT __declspec(allocator) __declspec(restrict)
+#if ZEN_OVERRIDE_NEW_DELETE
+
+# if defined(_MSC_VER)
+# if (_MSC_VER >= 1900) && !defined(__EDG__)
+# define ZEN_RESTRICT __declspec(allocator) __declspec(restrict)
+# else
+# define ZEN_RESTRICT __declspec(restrict)
+# endif
# else
-# define ZEN_RESTRICT __declspec(restrict)
+# define ZEN_RESTRICT
# endif
-#else
-# define ZEN_RESTRICT
-#endif
//////////////////////////////////////////////////////////////////////////
@@ -29,13 +36,13 @@ void zen_free_aligned(void* p, size_t alignment) noexcept;
//////////////////////////////////////////////////////////////////////////
-#if defined(_MSC_VER) && defined(_Ret_notnull_) && defined(_Post_writable_byte_size_)
-# define zen_decl_new(n) [[nodiscard]] _VCRT_ALLOCATOR _Ret_notnull_ _Post_writable_byte_size_(n)
-# define zen_decl_new_nothrow(n) [[nodiscard]] _VCRT_ALLOCATOR _Ret_maybenull_ _Success_(return != NULL) _Post_writable_byte_size_(n)
-#else
-# define zen_decl_new(n) [[nodiscard]]
-# define zen_decl_new_nothrow(n) [[nodiscard]]
-#endif
+# if defined(_MSC_VER) && defined(_Ret_notnull_) && defined(_Post_writable_byte_size_)
+# define zen_decl_new(n) [[nodiscard]] _VCRT_ALLOCATOR _Ret_notnull_ _Post_writable_byte_size_(n)
+# define zen_decl_new_nothrow(n) [[nodiscard]] _VCRT_ALLOCATOR _Ret_maybenull_ _Success_(return != NULL) _Post_writable_byte_size_(n)
+# else
+# define zen_decl_new(n) [[nodiscard]]
+# define zen_decl_new_nothrow(n) [[nodiscard]]
+# endif
void
operator delete(void* p) noexcept
@@ -87,7 +94,7 @@ operator new[](std::size_t n, const std::nothrow_t& tag) noexcept
return zen_new_nothrow(n);
}
-#if (__cplusplus >= 201402L || _MSC_VER >= 1916)
+# if (__cplusplus >= 201402L || _MSC_VER >= 1916)
void
operator delete(void* p, std::size_t n) noexcept
{
@@ -98,9 +105,9 @@ operator delete[](void* p, std::size_t n) noexcept
{
zen_free_size(p, n);
};
-#endif
+# endif
-#if (__cplusplus > 201402L || defined(__cpp_aligned_new))
+# if (__cplusplus > 201402L || defined(__cpp_aligned_new))
void
operator delete(void* p, std::align_val_t al) noexcept
{
@@ -152,7 +159,9 @@ operator new[](std::size_t n, std::align_val_t al, const std::nothrow_t&) noexce
{
return zen_new_aligned_nothrow(n, static_cast<size_t>(al));
}
-#endif
+# endif
+
+#endif // ZEN_OVERRIDE_NEW_DELETE
// EASTL operator new
diff --git a/src/zencore/include/zencore/xxhash.h b/src/zencore/include/zencore/xxhash.h
index 1616e5f93..fc55b513b 100644
--- a/src/zencore/include/zencore/xxhash.h
+++ b/src/zencore/include/zencore/xxhash.h
@@ -6,7 +6,9 @@
#include <zencore/memoryview.h>
+ZEN_THIRD_PARTY_INCLUDES_START
#include <xxh3.h>
+ZEN_THIRD_PARTY_INCLUDES_END
#include <compare>
#include <string_view>
diff --git a/src/zencore/memory/memory.cpp b/src/zencore/memory/memory.cpp
index fced2a4d3..fc8de5d02 100644
--- a/src/zencore/memory/memory.cpp
+++ b/src/zencore/memory/memory.cpp
@@ -39,19 +39,23 @@ InitGMalloc()
FMalloc* InitMalloc = GMalloc;
// Pick a default base allocator based on availability/platform
+ // when using sanitizers, we should use the default/ansi allocator
-#if ZEN_MIMALLOC_ENABLED
+#if ZEN_OVERRIDE_NEW_DELETE
+
+# if ZEN_MIMALLOC_ENABLED
if (Malloc == MallocImpl::None)
{
Malloc = MallocImpl::Mimalloc;
}
-#endif
+# endif
-#if ZEN_RPMALLOC_ENABLED
+# if ZEN_RPMALLOC_ENABLED
if (Malloc == MallocImpl::None)
{
Malloc = MallocImpl::Rpmalloc;
}
+# endif
#endif
// Process any command line overrides
@@ -287,6 +291,8 @@ zen_free_aligned(void* Ptr, size_t Alignment) noexcept
// EASTL operator new
+#if ZEN_OVERRIDE_NEW_DELETE
+
void*
operator new[](size_t size, const char* pName, int flags, unsigned debugFlags, const char* file, int line)
{
@@ -310,3 +316,29 @@ operator new[](size_t size,
return zen_new_aligned(size, alignment);
}
+
+#else
+
+void*
+operator new[](size_t size, const char* pName, int flags, unsigned debugFlags, const char* file, int line)
+{
+ ZEN_UNUSED(pName, flags, debugFlags, file, line);
+ return ::operator new[](size);
+}
+
+void*
+operator new[](size_t size,
+ size_t alignment,
+ size_t alignmentOffset,
+ const char* pName,
+ int flags,
+ unsigned debugFlags,
+ const char* file,
+ int line)
+{
+ ZEN_UNUSED(alignmentOffset, pName, flags, debugFlags, file, line);
+ ZEN_ASSERT_SLOW(alignmentOffset == 0); // currently not supported
+ return ::operator new[](size, std::align_val_t(alignment));
+}
+
+#endif
diff --git a/src/zencore/xmake.lua b/src/zencore/xmake.lua
index 24011d300..ef462ad03 100644
--- a/src/zencore/xmake.lua
+++ b/src/zencore/xmake.lua
@@ -21,36 +21,38 @@ target('zencore')
end
if has_config("zenmimalloc") then
- add_packages("vcpkg::mimalloc")
+ add_packages("mimalloc")
end
add_deps("zenbase")
add_deps("spdlog")
add_deps("utfcpp")
add_deps("oodle")
+ add_deps("blake3")
add_deps("ue-trace")
add_deps("timesinceprocessstart")
+ add_deps("doctest")
+ add_deps("fmt")
+ add_deps("ryml")
+
+ add_packages("json11")
+ if is_plat("linux") then
+ add_packages("openssl3") -- required for crypto
+ elseif is_plat("macosx") then
+ add_packages("mbedtls") -- required for crypto
+ end
+
add_packages(
- "vcpkg::blake3",
- "vcpkg::json11",
- "vcpkg::ryml",
- "vcpkg::c4core",
- "vcpkg::openssl" -- required for crypto
- )
-
- add_packages(
- "vcpkg::doctest",
- "vcpkg::eastl",
- "vcpkg::fmt",
- "vcpkg::gsl-lite",
- "vcpkg::lz4",
- "vcpkg::xxhash",
+ "gsl-lite",
+ "eastl",
+ "lz4",
+ "xxhash",
{public=true}
)
if has_config("zensentry") then
- add_packages("vcpkg::sentry-native")
+ add_packages("sentry-native")
if is_os("windows") then
add_cxxflags("/wd4996")
@@ -67,7 +69,7 @@ target('zencore')
-- be specified after the former with GCC-like toolchains. xmake however
-- is unaware of this and simply globs files from vcpkg's output. The
-- line below forces breakpad_client to be to the right of sentry_native
- add_syslinks("breakpad_client")
+ -- add_syslinks("breakpad_client")
end
if is_plat("macosx") then