Skip to content

Commit

Permalink
assert for null pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
azimafroozeh committed Sep 11, 2024
1 parent 94c7ec8 commit 2aebc59
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 37 deletions.
4 changes: 3 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ misc-throw-by-value-catch-by-reference,
google-global-names-in-headers,
llvm-header-guard,
misc-definitions-in-headers,
readability-container-size-empty'
readability-container-size-empty
clang-analyzer-core.NonNullParamChecker
'

WarningsAsErrors: '*'
AnalyzeTemporaryDtors: false
Expand Down
48 changes: 27 additions & 21 deletions include/fls/common/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@
#ifdef NDEBUG
#define FLS_ASSERT(...) ;
#define DETAILED_FLS_ASSERT(...) ;
#define FLS_ASSERT_POINTER(...) ;

#else
#define FLS_ASSERT_POINTER(p) fastlanes::Assert::NotNullPointer(p)
#define FLS_ASSERT(Expr, Val, Msg) fastlanes::FlsAssert(#Expr, Val, Expr, __FILE__, __LINE__, Msg)
#define DETAILED_FLS_ASSERT(Expr, Val, Msg) fastlanes::DetailedFlsAssert(#Expr, Val, Expr, __FILE__, __LINE__, Msg)
#endif

#define FLS_ASSERT_EXPR(Expr) DETAILED_FLS_ASSERT(Expr, " ", #Expr);
#define FLS_ASSERT_TRUE(Expr) FLS_ASSERT(Expr, " ", #Expr);
#define FLS_ASSERT_FALSE(EXPR) FLS_ASSERT(!(EXPR), " ", #EXPR);
#define FLS_ASSERT_NOT_NULL_POINTER(Expr) FLS_ASSERT(Expr != nullptr, " ", fastlanes::assert::NULL_POINTER);
#define FLS_ASSERT_NOT_NULL_POINTER(pointer) FLS_ASSERT_POINTER(pointer);
#define FLS_ASSERT_NULL_POINTER(Expr) FLS_ASSERT(Expr == nullptr, " ", "");
#define FLS_ASSERT_CORRECT_MIN_MAX(Min, Max) FLS_ASSERT(Max >= Min, " ", fastlanes::assert::NULL_POINTER);
#define FLS_ASSERT_CORRECT_RANGE(Range) FLS_ASSERT(Range >= 0, " ", fastlanes::assert::CORRECT_BW);
#define FLS_ASSERT_CORRECT_BW(Bw) DETAILED_FLS_ASSERT(Bw >= 0 && Bw <= 64, #Bw, fastlanes::assert::NULL_POINTER);
#define FLS_ASSERT_NOT_ZERO(Expr) FLS_ASSERT(Expr != 0, " ", fastlanes::assert::ZERO);
#define FLS_ASSERT_ZERO(Expr) FLS_ASSERT(Expr == 0, " ", fastlanes::assert::NOT_ZERO);
#define FLS_ASSERT_CORRECT_SZ(Expr) FLS_ASSERT(Expr >= 0, " ", fastlanes::assert::NEGATIVE_SZ);
#define FLS_ASSERT_CORRECT_BSZ(Expr) FLS_ASSERT(Expr >= 0, " ", fastlanes::assert::NEGATIVE_SZ);
#define FLS_ASSERT_CORRECT_EXP_T(EXP_T) FLS_ASSERT(EXP_T != 0, #EXP_T, fastlanes::assert::CORRECT_EXP_T_MSG);
#define FLS_ASSERT_CORRECT_N(Expr) FLS_ASSERT(Expr >= 0, " ", fastlanes::assert::NEGATIVE_INDEX);
#define FLS_ASSERT_CORRECT_LMTED_C(C, LMT) FLS_ASSERT(C >= 0 && C = < LMT, " ", fastlanes::assert::NEGATIVE_INDEX);
#define FLS_ASSERT_CORRECT_POS(POS) FLS_ASSERT(POS >= 0 && POS <= 1023, " ", fastlanes::assert::NEGATIVE_INDEX);
#define FLS_ASSERT_CORRECT_MIN_MAX(Min, Max) FLS_ASSERT(Max >= Min, " ", fastlanes::Assert::NULL_POINTER);
#define FLS_ASSERT_CORRECT_RANGE(Range) FLS_ASSERT(Range >= 0, " ", fastlanes::Assert::CORRECT_BW);
#define FLS_ASSERT_CORRECT_BW(Bw) DETAILED_FLS_ASSERT(Bw >= 0 && Bw <= 64, #Bw, fastlanes::Assert::NULL_POINTER);
#define FLS_ASSERT_NOT_ZERO(Expr) FLS_ASSERT(Expr != 0, " ", fastlanes::Assert::ZERO);
#define FLS_ASSERT_ZERO(Expr) FLS_ASSERT(Expr == 0, " ", fastlanes::Assert::NOT_ZERO);
#define FLS_ASSERT_CORRECT_SZ(Expr) FLS_ASSERT(Expr >= 0, " ", fastlanes::Assert::NEGATIVE_SZ);
#define FLS_ASSERT_CORRECT_BSZ(Expr) FLS_ASSERT(Expr >= 0, " ", fastlanes::Assert::NEGATIVE_SZ);
#define FLS_ASSERT_CORRECT_EXP_T(EXP_T) FLS_ASSERT(EXP_T != 0, #EXP_T, fastlanes::Assert::CORRECT_EXP_T_MSG);
#define FLS_ASSERT_CORRECT_N(Expr) FLS_ASSERT(Expr >= 0, " ", fastlanes::Assert::NEGATIVE_INDEX);
#define FLS_ASSERT_CORRECT_LMTED_C(C, LMT) FLS_ASSERT(C >= 0 && C = < LMT, " ", fastlanes::Assert::NEGATIVE_INDEX);
#define FLS_ASSERT_CORRECT_POS(POS) FLS_ASSERT(POS >= 0 && POS <= 1023, " ", fastlanes::Assert::NEGATIVE_INDEX);
#define FLS_ASSERT_CORRECT_SEGMENT_SIZE(Expr) FLS_ASSERT(Expr >= 8, " ", "");
#define FLS_ASSERT_EQUALITY(L_Expr, R_Expr) FLS_ASSERT(L_Expr == R_Expr, " ", " ");
#define FLS_ASSERT_LESS(L_Expr, R_Expr) FLS_ASSERT(L_Expr <= R_Expr, " ", " ");
Expand All @@ -34,22 +36,26 @@
#define FLS_ASSERT_L(L_VAL, R_VAL) FLS_ASSERT(L_VAL < R_VAL, " ", " ");
#define FLS_ASSERT_LE(L_VAL, R_VAL) FLS_ASSERT(L_VAL <= R_VAL, " ", " ");
#define FLS_ASSERT_CORRECT_NUM(NUM, C) FLS_ASSERT(NUM <= C, " ", " ");
#define FLS_ABORT(MSG) FLS_ASSERT(false, MSG, fastlanes::assert::ABORT);
#define FLS_ASSERT_CORRECT_BASE_UB(BASE, UB) FLS_ASSERT(BASE <= UB, " ", fastlanes::assert::BASE_UB);
#define FLS_ASSERT_CORRECT_BASE_LB(BASE, LB) FLS_ASSERT(BASE >= LB, " ", fastlanes::assert::BASE_LB);
#define FLS_ASSERT_CORRECT_OFFSET(Offset) FLS_ASSERT(Offset >= 0, " ", fastlanes::assert::OFFSET);
#define FLS_ASSERT_CORRECT_SMART_OFFSET(OFFSET) FLS_ASSERT(OFFSET >= 0, " ", fastlanes::assert::SMART_OFFSET);
#define FLS_ASSERT_CORRECT_EXC_C(C) FLS_ASSERT(C <= 1024 && C >= 0, " ", fastlanes::assert::ZERO);
#define FLS_ABORT(MSG) FLS_ASSERT(false, MSG, fastlanes::Assert::ABORT);
#define FLS_ASSERT_CORRECT_BASE_UB(BASE, UB) FLS_ASSERT(BASE <= UB, " ", fastlanes::Assert::BASE_UB);
#define FLS_ASSERT_CORRECT_BASE_LB(BASE, LB) FLS_ASSERT(BASE >= LB, " ", fastlanes::Assert::BASE_LB);
#define FLS_ASSERT_CORRECT_OFFSET(Offset) FLS_ASSERT(Offset >= 0, " ", fastlanes::Assert::OFFSET);
#define FLS_ASSERT_CORRECT_SMART_OFFSET(OFFSET) FLS_ASSERT(OFFSET >= 0, " ", fastlanes::Assert::SMART_OFFSET);
#define FLS_ASSERT_CORRECT_EXC_C(C) FLS_ASSERT(C <= 1024 && C >= 0, " ", fastlanes::Assert::ZERO);
#define FLS_ASSERT_NOT_EMPTY_STR(STR) FLS_ASSERT(STR.size() > 0, " ", " ");
#define FLS_ASSERT_E(L_VAL, R_VAL) FLS_ASSERT(L_VAL == R_VAL, " ", " ");
#define FLS_ASSERT_CORRECT_IDX(Expr) FLS_ASSERT(Expr >= 0, " ", fastlanes::assert::IDX);
#define FLS_ASSERT_NOT_EMPTY_VEC(VEC) FLS_ASSERT(!VEC.empty(), " ", fastlanes::assert::EMPTY_VECTOR);
#define FLS_ASSERT_CORRECT_IDX(Expr) FLS_ASSERT(Expr >= 0, " ", fastlanes::Assert::IDX);
#define FLS_ASSERT_NOT_EMPTY_VEC(VEC) FLS_ASSERT(!VEC.empty(), " ", fastlanes::Assert::EMPTY_VECTOR);
#include "alias.hpp"

namespace fastlanes {
void FlsAssert(const char* expr_str, const char* str, bool expr, const char* file, int line, const char* msg);
void DetailedFlsAssert(const char* expr_str, const char* str, bool expr, const char* file, int line, const char* msg);

class assert {
class Assert {
public:
static void NotNullPointer(const void* p);

public:
static constexpr auto CORRECT_EXP_T_MSG = "Exp is invalid.";
static constexpr auto ABORT = "ABORTED.";
Expand Down
4 changes: 2 additions & 2 deletions include/fls/encoder/assert_eq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void assert_eq(Vec* vec, const PT* data_p, idx_t start_idx, ExpT exp) {
auto* str_p = vec->buf_arr[vec->arr_c + 0].mutable_data();
auto* off_arr = vec->buf_arr[vec->arr_c + 1].mutable_data<uint32_t>();

FLS_ASSERT_NOT_NULL_POINTER(vec->fsst_decoder_up)
FLS_ASSERT_NOT_NULL_POINTER(vec->fsst_decoder_up.get())

uint32_t untransposed[1024] = {0};
untranspose_i(off_arr, untransposed);
Expand Down Expand Up @@ -213,7 +213,7 @@ void assert_eq(Vec* vec, const PT* data_p, idx_t start_idx, ExpT exp) {
auto* str_p = vec->buf_arr[vec->arr_c + 0].mutable_data();
auto* off_arr = vec->buf_arr[vec->arr_c + 1].mutable_data<uint32_t>();

FLS_ASSERT_NOT_NULL_POINTER(vec->fsst12_decoder_up)
FLS_ASSERT_NOT_NULL_POINTER(vec->fsst12_decoder_up.get())

uint32_t untransposed[1024] = {0};
untranspose_i(off_arr, untransposed);
Expand Down
4 changes: 3 additions & 1 deletion src/common/assert.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "fls/common/assert.hpp"
#include <cassert>
#include <cstdlib> // for abort
#include <iostream> // for operator<<, char_traits, basic_ostream, cerr

Expand All @@ -11,7 +12,6 @@ void FlsAssert(const char* expr_str, const char* str, bool expr, const char* fil
<< "Values:\t\t" << str << "\n"
<< file << ":" << line << "\n";
abort();
throw std::runtime_error("aborted");
}

void DetailedFlsAssert(const char* expr_str, const char* str, bool expr, const char* file, int line, const char* msg) {
Expand All @@ -23,4 +23,6 @@ void DetailedFlsAssert(const char* expr_str, const char* str, bool expr, const c
<< file << ":" << line << "\n";
abort();
}

void Assert::NotNullPointer(const void* p) { assert(p != nullptr && "Pointer must not be null"); }
} // namespace fastlanes
4 changes: 2 additions & 2 deletions src/cor/eng/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ void Engine<PT>::Compress(Vec& src_vec, EngineState& eng_state) {

template <typename PT>
void Engine<PT>::Decompress(Vec& des) {
FLS_ASSERT_NOT_NULL_POINTER(m_decompressor_up)
FLS_ASSERT_NOT_NULL_POINTER(m_decompressor_up.get())

m_decompressor_up->Decompress(des);
}

template <typename PT>
Vec* Engine<PT>::Decompress(DecompressState& stt) {
FLS_ASSERT_NOT_NULL_POINTER(m_decompressor_up)
FLS_ASSERT_NOT_NULL_POINTER(m_decompressor_up.get())

return m_decompressor_up->Decompress(stt);
}
Expand Down
8 changes: 4 additions & 4 deletions src/cor/eng/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void Exe<T>::Execute(Vec& src_vec, Vec& des_vec, CompressState& compress_state)
compress_state.cur_des_buff = 0;

for (size_t i = 0; i < rpn_p->prm_c; ++i) {
FLS_ASSERT_NOT_NULL_POINTER(cmpr_fun_arr[i])
// FLS_ASSERT_NOT_NULL_POINTER(cmpr_fun_arr[i]) //fixme

cmpr_fun_arr[i](src_vec, des_vec, compress_state);
}
Expand All @@ -26,7 +26,7 @@ void Exe<T>::ResCmpr(sp<TExp<T>> exp) {
for (size_t i = 0; i < rpn_p->prm_c; ++i) {
cmpr_fun_arr[i] = resolver<T>::resolve_cmpr(rpn_p->prm_arr[i], rpn_p->typ_arr[i], exp);

FLS_ASSERT_NOT_NULL_POINTER(cmpr_fun_arr[i])
// FLS_ASSERT_NOT_NULL_POINTER(cmpr_fun_arr[i]) //fixme
}
}

Expand All @@ -36,7 +36,7 @@ void Exe<T>::ResDecmpr(sp<TExp<T>> exp) {
for (size_t i = 0; i < rpn_p->prm_c; ++i) {
de_cmpr_fun_arr[i] = resolver<T>::resolve_de_cmpr(rpn_p->prm_arr[i], rpn_p->typ_arr[i], exp);

FLS_ASSERT_NOT_NULL_POINTER(de_cmpr_fun_arr[i])
// FLS_ASSERT_NOT_NULL_POINTER(de_cmpr_fun_arr[i])
}
}

Expand All @@ -46,7 +46,7 @@ void Exe<T>::Execute(PageParam pg_params, VecParam vec_params, DecompressState&
stt.cur_des_arr = rpn_p->des_buf_c - 1;

for (size_t i = rpn_p->prm_c; i > 0; --i) {
FLS_ASSERT_NOT_NULL_POINTER(de_cmpr_fun_arr[i - 1])
// FLS_ASSERT_NOT_NULL_POINTER(de_cmpr_fun_arr[i - 1])

de_cmpr_fun_arr[i - 1](pg_params, vec_params, stt);
}
Expand Down
11 changes: 5 additions & 6 deletions src/cor/exp/hdr.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "fls/cor/exp/hdr.hpp"
#include "fls/common/assert.hpp" // for FLS_ASSERT
#include <cstddef> // for byte
#include <iosfwd> // for string
#include <string> // for basic_string
#include <cassert>
#include <cstddef> // for byte
#include <string> // for basic_string

namespace fastlanes::exp {
std::string Hdr::ToString() const {
Expand All @@ -16,12 +16,11 @@ std::string& Hdr::operator<<(std::string& lhs) {
}

Hdr Hdr::Load(std::byte* p) {
FLS_ASSERT(p != nullptr, "", "");

FLS_ASSERT_NOT_NULL_POINTER(p);
return *(reinterpret_cast<Hdr*>(p));
}
Hdr Hdr::Load(uint8_t* p) {
FLS_ASSERT(p != nullptr, " ", " ");
FLS_ASSERT_NOT_NULL_POINTER(p);

return *(reinterpret_cast<Hdr*>(p));
}
Expand Down

0 comments on commit 2aebc59

Please sign in to comment.