Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check size is correct if specified #520

Merged
merged 2 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions src/snmalloc/ds_core/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,29 +166,46 @@ namespace snmalloc

namespace snmalloc
{
/**
* Forward declaration so that this can be called before the pal header is
* included.
*/
template<size_t BufferSize = 1024, typename... Args>
[[noreturn]] inline void report_fatal_error(Args... args);

/**
* Forward declaration so that this can be called before the pal header is
* included.
*/
template<size_t BufferSize = 1024, typename... Args>
inline void message(Args... args);

template<typename... Args>
SNMALLOC_FAST_PATH_INLINE void UNUSED(Args&&...)
{}

inline SNMALLOC_FAST_PATH void check_client_error(const char* const str)
template<typename... Args>
inline SNMALLOC_FAST_PATH void
check_client_error(const char* const str, Args... args)
{
//[[clang::musttail]]
return snmalloc::error(str);
return snmalloc::report_fatal_error(str, args...);
}

template<typename... Args>
inline SNMALLOC_FAST_PATH void
check_client_impl(bool test, const char* const str)
check_client_impl(bool test, const char* const str, Args... args)
{
if (SNMALLOC_UNLIKELY(!test))
{
if constexpr (DEBUG)
if constexpr (!DEBUG)
{
UNUSED(str);
UNUSED(str, args...);
SNMALLOC_FAST_FAIL();
}
else
{
check_client_error(str);
check_client_error(str, args...);
}
}
}
Expand All @@ -198,25 +215,11 @@ namespace snmalloc
#else
static constexpr bool CHECK_CLIENT = false;
#endif

/**
* Forward declaration so that this can be called before the pal header is
* included.
*/
template<size_t BufferSize = 1024, typename... Args>
[[noreturn]] inline void report_fatal_error(Args... args);

/**
* Forward declaration so that this can be called before the pal header is
* included.
*/
template<size_t BufferSize = 1024, typename... Args>
inline void message(Args... args);
} // namespace snmalloc

#ifdef SNMALLOC_CHECK_CLIENT
# define snmalloc_check_client(test, str) \
snmalloc::check_client_impl(test, str)
# define snmalloc_check_client(test, str, ...) \
snmalloc::check_client_impl(test, str, ##__VA_ARGS__)
#else
# define snmalloc_check_client(test, str)
# define snmalloc_check_client(test, str, ...)
#endif
20 changes: 18 additions & 2 deletions src/snmalloc/mem/localalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -675,16 +675,32 @@ namespace snmalloc
#endif
}

void check_size(void* p, size_t size)
{
#ifdef SNMALLOC_CHECK_CLIENT
size = size == 0 ? 1 : size;
auto sc = size_to_sizeclass_full(size);
auto pm_sc =
Backend::Pagemap::get_metaentry(address_cast(p)).get_sizeclass();
auto rsize = sizeclass_full_to_size(sc);
auto pm_size = sizeclass_full_to_size(pm_sc);
snmalloc_check_client(
sc == pm_sc, "Dealloc rounded size mismatch: {} != {}", rsize, pm_size);
#else
UNUSED(p, size);
#endif
}

SNMALLOC_FAST_PATH void dealloc(void* p, size_t s)
{
UNUSED(s);
check_size(p, s);
dealloc(p);
}

template<size_t size>
SNMALLOC_FAST_PATH void dealloc(void* p)
{
UNUSED(size);
check_size(p, size);
dealloc(p);
}

Expand Down
5 changes: 5 additions & 0 deletions src/snmalloc/mem/sizeclasstable.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ namespace snmalloc
{
return value == 0;
}

constexpr bool operator==(sizeclass_t other)
{
return value == other.value;
}
};

using sizeclass_compress_t = uint8_t;
Expand Down