folly: ubsan: replace undefined call through reinterpret-cast fn-pointer with std...
authorLucian Grijincu <lucian@fb.com>
Thu, 28 Jan 2016 19:07:44 +0000 (11:07 -0800)
committerfacebook-github-bot-0 <folly-bot@fb.com>
Thu, 28 Jan 2016 19:20:27 +0000 (11:20 -0800)
commite2c9fd6c6f57777bf9d3fe09038bc714124d2060
treed91e4476c30bd7182a9e834f6b6b9d23e0631b1a
parent08dba5714790020d2fa677e34e624eb4f34a20ca
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
folly/io/async/EventBase.cpp
folly/io/async/EventBase.h