Skip to content

Commit

Permalink
SanderMertens#1436 fixed UBSan error when calling free_obj<T>
Browse files Browse the repository at this point in the history
call to function void flecs::_::free_obj<flecs::_::each_delegate<>> through pointer to incorrect function type 'void (*)(void *)'
  • Loading branch information
jspohr authored Nov 15, 2024
1 parent 6fde571 commit b953fd4
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 46 deletions.
36 changes: 13 additions & 23 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16910,9 +16910,9 @@ namespace _
struct placement_new_tag_t{};
constexpr placement_new_tag_t placement_new_tag{};
template<class Ty> inline void destruct_obj(Ty* _ptr) { _ptr->~Ty(); }
template<class Ty> inline void free_obj(Ty* _ptr) {
template<class Ty> inline void free_obj(void* _ptr) {
if (_ptr) {
destruct_obj(_ptr);
destruct_obj(static_cast<Ty*>(_ptr));
ecs_os_free(_ptr);
}
}
Expand Down Expand Up @@ -26062,7 +26062,7 @@ struct each_delegate : public delegate {

// Function that can be used as callback to free delegate
static void destruct(void *obj) {
_::free_obj<each_delegate>(static_cast<each_delegate*>(obj));
_::free_obj<each_delegate>(obj);
}

// Static function to call for component on_add hook
Expand Down Expand Up @@ -27495,8 +27495,7 @@ struct component : untyped_component {
BindingCtx *ctx = get_binding_ctx(h);
h.on_add = Delegate::run_add;
ctx->on_add = FLECS_NEW(Delegate)(FLECS_FWD(func));
ctx->free_on_add = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<Delegate>);
ctx->free_on_add = _::free_obj<Delegate>;
ecs_set_hooks_id(world_, id_, &h);
return *this;
}
Expand All @@ -27512,8 +27511,7 @@ struct component : untyped_component {
BindingCtx *ctx = get_binding_ctx(h);
h.on_remove = Delegate::run_remove;
ctx->on_remove = FLECS_NEW(Delegate)(FLECS_FWD(func));
ctx->free_on_remove = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<Delegate>);
ctx->free_on_remove = _::free_obj<Delegate>;
ecs_set_hooks_id(world_, id_, &h);
return *this;
}
Expand All @@ -27529,8 +27527,7 @@ struct component : untyped_component {
BindingCtx *ctx = get_binding_ctx(h);
h.on_set = Delegate::run_set;
ctx->on_set = FLECS_NEW(Delegate)(FLECS_FWD(func));
ctx->free_on_set = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<Delegate>);
ctx->free_on_set = _::free_obj<Delegate>;
ecs_set_hooks_id(world_, id_, &h);
return *this;
}
Expand Down Expand Up @@ -27582,8 +27579,7 @@ component<T>& constant(const char *name, T value) {
if (!result) {
result = FLECS_NEW(BindingCtx);
h.binding_ctx = result;
h.binding_ctx_free = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<BindingCtx>);
h.binding_ctx_free = _::free_obj<BindingCtx>;
}
return result;
}
Expand Down Expand Up @@ -30406,8 +30402,7 @@ struct node_builder : IBuilder<Base, Components ...>
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
desc_.run = Delegate::run;
desc_.run_ctx = ctx;
desc_.run_ctx_free = reinterpret_cast<
ecs_ctx_free_t>(_::free_obj<Delegate>);
desc_.run_ctx_free = _::free_obj<Delegate>;
return T(world_, &desc_);
}

Expand All @@ -30419,8 +30414,7 @@ struct node_builder : IBuilder<Base, Components ...>
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
desc_.run = Delegate::run;
desc_.run_ctx = ctx;
desc_.run_ctx_free = reinterpret_cast<
ecs_ctx_free_t>(_::free_obj<Delegate>);
desc_.run_ctx_free = _::free_obj<Delegate>;
return each(FLECS_FWD(each_func));
}

Expand All @@ -30431,8 +30425,7 @@ struct node_builder : IBuilder<Base, Components ...>
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
desc_.callback = Delegate::run;
desc_.callback_ctx = ctx;
desc_.callback_ctx_free = reinterpret_cast<
ecs_ctx_free_t>(_::free_obj<Delegate>);
desc_.callback_ctx_free = _::free_obj<Delegate>;
return T(world_, &desc_);
}

Expand Down Expand Up @@ -30649,8 +30642,7 @@ namespace _ {
{
using Delegate = _::entity_observer_delegate<Func>;
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx,
reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx, _::free_obj<Delegate>);
}

template <typename Evt, if_not_t<is_empty<Evt>::value> = 0>
Expand All @@ -30661,8 +30653,7 @@ namespace _ {
{
using Delegate = _::entity_payload_observer_delegate<Func, Evt>;
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx,
reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx, _::free_obj<Delegate>);
}
};
}
Expand All @@ -30673,8 +30664,7 @@ inline const Self& entity_builder<Self>::observe(flecs::entity_t evt, Func&& f)
using Delegate = _::entity_observer_delegate<Func>;
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));

_::entity_observer_create(world_, evt, id_, Delegate::run, ctx,
reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
_::entity_observer_create(world_, evt, id_, Delegate::run, ctx, _::free_obj<Delegate>);

return to_base();
}
Expand Down
12 changes: 4 additions & 8 deletions include/flecs/addons/cpp/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,7 @@ struct component : untyped_component {
BindingCtx *ctx = get_binding_ctx(h);
h.on_add = Delegate::run_add;
ctx->on_add = FLECS_NEW(Delegate)(FLECS_FWD(func));
ctx->free_on_add = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<Delegate>);
ctx->free_on_add = _::free_obj<Delegate>;
ecs_set_hooks_id(world_, id_, &h);
return *this;
}
Expand All @@ -470,8 +469,7 @@ struct component : untyped_component {
BindingCtx *ctx = get_binding_ctx(h);
h.on_remove = Delegate::run_remove;
ctx->on_remove = FLECS_NEW(Delegate)(FLECS_FWD(func));
ctx->free_on_remove = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<Delegate>);
ctx->free_on_remove = _::free_obj<Delegate>;
ecs_set_hooks_id(world_, id_, &h);
return *this;
}
Expand All @@ -487,8 +485,7 @@ struct component : untyped_component {
BindingCtx *ctx = get_binding_ctx(h);
h.on_set = Delegate::run_set;
ctx->on_set = FLECS_NEW(Delegate)(FLECS_FWD(func));
ctx->free_on_set = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<Delegate>);
ctx->free_on_set = _::free_obj<Delegate>;
ecs_set_hooks_id(world_, id_, &h);
return *this;
}
Expand All @@ -505,8 +502,7 @@ struct component : untyped_component {
if (!result) {
result = FLECS_NEW(BindingCtx);
h.binding_ctx = result;
h.binding_ctx_free = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<BindingCtx>);
h.binding_ctx_free = _::free_obj<BindingCtx>;
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion include/flecs/addons/cpp/delegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ struct each_delegate : public delegate {

// Function that can be used as callback to free delegate
static void destruct(void *obj) {
_::free_obj<each_delegate>(static_cast<each_delegate*>(obj));
_::free_obj<each_delegate>(obj);
}

// Static function to call for component on_add hook
Expand Down
9 changes: 3 additions & 6 deletions include/flecs/addons/cpp/mixins/event/impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ namespace _ {
{
using Delegate = _::entity_observer_delegate<Func>;
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx,
reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx, _::free_obj<Delegate>);
}

template <typename Evt, if_not_t<is_empty<Evt>::value> = 0>
Expand All @@ -64,8 +63,7 @@ namespace _ {
{
using Delegate = _::entity_payload_observer_delegate<Func, Evt>;
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx,
reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx, _::free_obj<Delegate>);
}
};
}
Expand All @@ -76,8 +74,7 @@ inline const Self& entity_builder<Self>::observe(flecs::entity_t evt, Func&& f)
using Delegate = _::entity_observer_delegate<Func>;
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));

_::entity_observer_create(world_, evt, id_, Delegate::run, ctx,
reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
_::entity_observer_create(world_, evt, id_, Delegate::run, ctx, _::free_obj<Delegate>);

return to_base();
}
Expand Down
9 changes: 3 additions & 6 deletions include/flecs/addons/cpp/utils/node_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ struct node_builder : IBuilder<Base, Components ...>
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
desc_.run = Delegate::run;
desc_.run_ctx = ctx;
desc_.run_ctx_free = reinterpret_cast<
ecs_ctx_free_t>(_::free_obj<Delegate>);
desc_.run_ctx_free = _::free_obj<Delegate>;
return T(world_, &desc_);
}

Expand All @@ -50,8 +49,7 @@ struct node_builder : IBuilder<Base, Components ...>
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
desc_.run = Delegate::run;
desc_.run_ctx = ctx;
desc_.run_ctx_free = reinterpret_cast<
ecs_ctx_free_t>(_::free_obj<Delegate>);
desc_.run_ctx_free = _::free_obj<Delegate>;
return each(FLECS_FWD(each_func));
}

Expand All @@ -62,8 +60,7 @@ struct node_builder : IBuilder<Base, Components ...>
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
desc_.callback = Delegate::run;
desc_.callback_ctx = ctx;
desc_.callback_ctx_free = reinterpret_cast<
ecs_ctx_free_t>(_::free_obj<Delegate>);
desc_.callback_ctx_free = _::free_obj<Delegate>;
return T(world_, &desc_);
}

Expand Down
4 changes: 2 additions & 2 deletions include/flecs/addons/cpp/utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ namespace _
struct placement_new_tag_t{};
constexpr placement_new_tag_t placement_new_tag{};
template<class Ty> inline void destruct_obj(Ty* _ptr) { _ptr->~Ty(); }
template<class Ty> inline void free_obj(Ty* _ptr) {
template<class Ty> inline void free_obj(void* _ptr) {
if (_ptr) {
destruct_obj(_ptr);
destruct_obj(static_cast<Ty*>(_ptr));
ecs_os_free(_ptr);
}
}
Expand Down

0 comments on commit b953fd4

Please sign in to comment.