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

Fix UB in in-place construction #99

Merged
merged 2 commits into from
May 14, 2024
Merged
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
30 changes: 16 additions & 14 deletions proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifndef _MSFT_PROXY_
#define _MSFT_PROXY_

#include <cstddef>
#include <bit>
#include <concepts>
#include <initializer_list>
Expand Down Expand Up @@ -174,52 +175,53 @@ R invoke_dispatch(Args&&... args) {
}
}
template <class P, class F, class R, class... Args>
R invocation_dispatcher_ref(const char* self, Args... args)
R invocation_dispatcher_ref(const std::byte* self, Args... args)
noexcept(is_invoker_well_formed<
F, typename ptr_traits<P>::target_type, true, R, Args...>()) {
return invoke_dispatch<F, R>(ptr_traits<P>::dereference(*std::launder(
reinterpret_cast<const P*>(self))), std::forward<Args>(args)...);
}
template <class P, class F, class R, class... Args>
R invocation_dispatcher_ptr(const char* self, Args... args)
R invocation_dispatcher_ptr(const std::byte* self, Args... args)
noexcept(is_invoker_well_formed<F, const P, true, R, Args...>()) {
return invoke_dispatch<F, R>(*std::launder(reinterpret_cast<const P*>(self)),
std::forward<Args>(args)...);
}
template <class F, class R, class... Args>
R invocation_dispatcher_void(const char*, Args... args)
R invocation_dispatcher_void(const std::byte*, Args... args)
noexcept(is_invoker_well_formed<F, void, true, R, Args...>())
{ return invoke_dispatch<F, R>(std::forward<Args>(args)...); }
template <class P>
void copying_dispatcher(char* self, const char* rhs)
void copying_dispatcher(std::byte* self, const std::byte* rhs)
noexcept(has_copyability<P>(constraint_level::nothrow)) {
std::construct_at(reinterpret_cast<P*>(self),
*std::launder(reinterpret_cast<const P*>(rhs)));
}
template <std::size_t Len, std::size_t Align>
void copying_default_dispatcher(char* self, const char* rhs) noexcept {
void copying_default_dispatcher(std::byte* self, const std::byte* rhs)
noexcept {
std::uninitialized_copy_n(
std::assume_aligned<Align>(rhs), Len, std::assume_aligned<Align>(self));
}
template <class P>
void relocation_dispatcher(char* self, const char* rhs)
void relocation_dispatcher(std::byte* self, const std::byte* rhs)
noexcept(has_relocatability<P>(constraint_level::nothrow)) {
P* other = std::launder(reinterpret_cast<P*>(const_cast<char*>(rhs)));
P* other = std::launder(reinterpret_cast<P*>(const_cast<std::byte*>(rhs)));
std::construct_at(reinterpret_cast<P*>(self), std::move(*other));
std::destroy_at(other);
}
template <class P>
void destruction_dispatcher(char* self)
void destruction_dispatcher(std::byte* self)
noexcept(has_destructibility<P>(constraint_level::nothrow))
{ std::destroy_at(std::launder(reinterpret_cast<P*>(self))); }
inline void destruction_default_dispatcher(char*) noexcept {}
inline void destruction_default_dispatcher(std::byte*) noexcept {}

template <bool NE, class R, class... Args>
struct overload_traits_impl : applicable_traits {
template <class D>
struct meta_provider {
template <class P>
static constexpr func_ptr_t<NE, R, const char*, Args...> get() {
static constexpr func_ptr_t<NE, R, const std::byte*, Args...> get() {
if constexpr (invocable_dispatch<
D, typename ptr_traits<P>::target_type, NE, R, Args...>) {
return &invocation_dispatcher_ref<P, typename D::template invoker<
Expand Down Expand Up @@ -314,7 +316,7 @@ struct dispatch_traits<D> : instantiated_t<
template <bool NE>
struct copyability_meta_provider {
template <class P>
static constexpr func_ptr_t<NE, void, char*, const char*> get() {
static constexpr func_ptr_t<NE, void, std::byte*, const std::byte*> get() {
if constexpr (has_copyability<P>(constraint_level::trivial)) {
return &copying_default_dispatcher<sizeof(P), alignof(P)>;
} else {
Expand All @@ -325,7 +327,7 @@ struct copyability_meta_provider {
template <bool NE>
struct relocatability_meta_provider {
template <class P>
static constexpr func_ptr_t<NE, void, char*, const char*> get() {
static constexpr func_ptr_t<NE, void, std::byte*, const std::byte*> get() {
if constexpr (has_relocatability<P>(constraint_level::trivial)) {
return &copying_default_dispatcher<sizeof(P), alignof(P)>;
} else {
Expand All @@ -336,7 +338,7 @@ struct relocatability_meta_provider {
template <bool NE>
struct destructibility_meta_provider {
template <class P>
static constexpr func_ptr_t<NE, void, char*> get() {
static constexpr func_ptr_t<NE, void, std::byte*> get() {
if constexpr (has_destructibility<P>(constraint_level::trivial)) {
return &destruction_default_dispatcher;
} else {
Expand Down Expand Up @@ -685,7 +687,7 @@ class proxy : public details::facade_traits<F>::base {
}

details::meta_ptr<typename Traits::meta> meta_;
alignas(F::constraints.max_align) char ptr_[F::constraints.max_size];
alignas(F::constraints.max_align) std::byte ptr_[F::constraints.max_size];
};

constexpr proxiable_ptr_constraints relocatable_ptr_constraints{
Expand Down
Loading