Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

ASAN build for tests.

Kishore Nallanc5bb229c7980
diff --git a/BUILD b/BUILD
index f47ced69..bc88a7d8 100644
--- a/BUILD
+++ b/BUILD
@@ -136,11 +136,22 @@ TEST_COPTS = [
"-g",
]
+ASAN_COPTS = [
+ "-fsanitize=address",
+ "-fno-omit-frame-pointer",
+ "-DASAN_BUILD"
+]
+
config_setting(
name = "release_mode",
define_values = { "mode": "release" }
)
+config_setting(
+ name = "asan_mode",
+ define_values = { "mode": "asan" }
+)
+
cc_test(
name = "typesense-test",
srcs = [
@@ -149,6 +160,7 @@ cc_test(
],
copts = TEST_COPTS + select({
":release_mode": ["-O2"],
+ ":asan_mode": ["-O0"] + ASAN_COPTS,
"//conditions:default": ["-O0"]
}),
data = [
@@ -163,6 +175,10 @@ cc_test(
defines = [
"ROOT_DIR="
],
+ linkopts = select({
+ ":asan_mode": ["-fsanitize=address"],
+ "//conditions:default": []
+ })
)
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp
index 87902022..11e10e5c 100644
--- a/src/main/typesense_server.cpp
+++ b/src/main/typesense_server.cpp
@@ -4,6 +4,7 @@
#include "stackprinter.h"
#include "backward.hpp"
+#ifndef ASAN_BUILD
extern "C" {
#include "jemalloc.h"
}
@@ -13,6 +14,7 @@ extern "C" {
extern void je_zone_register();
}
#endif
+#endif
void master_server_routes() {
// collection operations
@@ -108,6 +110,7 @@ void crash_callback(int sig, backward::StackTrace& st) {
}
int main(int argc, char **argv) {
+#ifndef ASAN_BUILD
#ifdef __APPLE__
// On OS X, je_zone_register registers jemalloc with the system allocator.
// We have to force the presence of these symbols on macOS by explicitly calling this method.
@@ -116,6 +119,7 @@ int main(int argc, char **argv) {
// - https://github.com/ClickHouse/ClickHouse/pull/11897
je_zone_register();
#endif
+#endif
Config& config = Config::get_instance();
diff --git a/src/system_metrics.cpp b/src/system_metrics.cpp
index 1375faec..972026c9 100644
--- a/src/system_metrics.cpp
+++ b/src/system_metrics.cpp
@@ -15,13 +15,15 @@
#endif
#include "string_utils.h"
-#include "jemalloc.h"
+#ifndef ASAN_BUILD
+#include "jemalloc.h"
#if __APPLE__
#define impl_mallctl je_mallctl
#else
#define impl_mallctl mallctl
#endif
+#endif
uint64_t SystemMetrics::non_proc_mem_last_access = 0;
uint64_t SystemMetrics::non_proc_mem_bytes = 0;
@@ -41,6 +43,7 @@ void SystemMetrics::get(const std::string &data_dir_path, nlohmann::json &result
sz = sizeof(size_t);
uint64_t epoch = 1;
+#ifndef ASAN_BUILD
// See: http://jemalloc.net/jemalloc.3.html#stats.active
impl_mallctl("thread.tcache.flush", nullptr, nullptr, nullptr, 0);
@@ -52,6 +55,7 @@ void SystemMetrics::get(const std::string &data_dir_path, nlohmann::json &result
impl_mallctl("stats.metadata", &metadata, &sz, nullptr, 0);
impl_mallctl("stats.mapped", &mapped, &sz, nullptr, 0);
impl_mallctl("stats.retained", &retained, &sz, nullptr, 0);
+#endif
result["typesense_memory_active_bytes"] = std::to_string(active);
result["typesense_memory_allocated_bytes"] = std::to_string(allocated);
@@ -114,8 +118,10 @@ uint64_t SystemMetrics::get_memory_active_bytes() {
sz = sizeof(size_t);
uint64_t epoch = 1;
+#ifndef ASAN_BUILD
impl_mallctl("epoch", &epoch, &sz, &epoch, sz);
impl_mallctl("stats.active", &memory_active_bytes, &sz, nullptr, 0);
+#endif
return memory_active_bytes;
}
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index 0d9ccad7..1800dc4a 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -20,7 +20,10 @@
#include "typesense_server_utils.h"
#include "file_utils.h"
#include "threadpool.h"
+
+#ifndef ASAN_BUILD
#include "jemalloc.h"
+#endif
#include "stackprinter.h"
@@ -323,7 +326,7 @@ int start_raft_server(ReplicationState& replication_state, const std::string& st
int run_server(const Config & config, const std::string & version, void (*master_server_routes)()) {
LOG(INFO) << "Starting Typesense " << version << std::flush;
-
+#ifndef ASAN_BUILD
if(using_jemalloc()) {
LOG(INFO) << "Typesense is using jemalloc.";
@@ -339,6 +342,7 @@ int run_server(const Config & config, const std::string & version, void (*master
} else {
LOG(WARNING) << "Typesense is NOT using jemalloc.";
}
+#endif
quit_raft_service = false;