folly: ubsan: replace undefined call through reinterpret-cast fn-pointer with std::function
Summary:
This code casts function pointers to void(*fn)(void*) and calls
functions through that type. Calling functions through a different
pointer type is undefined behavior. Casts were used to avoid memory
allocations.
`std::bind` needs a memory allocation - so it's avoided in EventBase.
std::function<void()> x = std::bind(fn, args);
`std::function` should use small object optimizations when possible and embed the functor in the body of `std::function`.
On GCC 5.0 and forward small lambdas don't need memory allocations
(lambdas being tiny structures - two pointers in this case - and a
function operator).
std::function<void()> y = [=] { fn(args); };
On GCC 4.7 .. 4.9 functors for which __is_location_invariant<Func> is
true also don't need memory allocations.
Remove undefined behavior by using a `SmallFunctor` which leverages `std::function`'s small object optimization.
Reviewed By: philippv
Differential Revision:
D2864895
fb-gh-sync-id:
ab40f60b3519ce38f43fecebf88ccdbf09d9bea9