Skip to content

Commit

Permalink
dnsdist: make sure we do not allocate 16-byte aligned objects through…
Browse files Browse the repository at this point in the history
… lua(jit)

luajit aligns only to 8 bytes by default, and some objects require 16 byte alignment.
Fixes PowerDNS#13766
  • Loading branch information
omoerbeek committed Feb 8, 2024
1 parent c04764a commit be864c7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 3 additions & 0 deletions ext/luawrapper/include/LuaContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,7 @@ class LuaContext {
// creating the object
// lua_newuserdata allocates memory in the internals of the lua library and returns it so we can fill it
// and that's what we do with placement-new
static_assert(alignof(TType) <= 8);
const auto pointerLocation = static_cast<TType*>(lua_newuserdata(state, sizeof(TType)));
new (pointerLocation) TType(std::forward<TType2>(value));
}
Expand Down Expand Up @@ -2292,6 +2293,7 @@ struct LuaContext::Pusher<TReturnType (TParameters...)>
// creating the object
// lua_newuserdata allocates memory in the internals of the lua library and returns it so we can fill it
// and that's what we do with placement-new
// static_assert(alignof(TFunctionObject) <= 8); XXX trips on at least c++lib 17, see #13766
const auto functionLocation = static_cast<TFunctionObject*>(lua_newuserdata(state, sizeof(TFunctionObject)));
new (functionLocation) TFunctionObject(std::move(fn));

Expand Down Expand Up @@ -2335,6 +2337,7 @@ struct LuaContext::Pusher<TReturnType (TParameters...)>
};

// we copy the function object onto the stack
static_assert(alignof(TFunctionObject) <= 8);
const auto functionObjectLocation = static_cast<TFunctionObject*>(lua_newuserdata(state, sizeof(TFunctionObject)));
new (functionObjectLocation) TFunctionObject(std::move(fn));

Expand Down
6 changes: 3 additions & 3 deletions pdns/dnsdist-lua-bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ void setupLuaBindings(LuaContext& luaCtx, bool client, bool configCheck)
luaCtx.registerFunction("toString", &ServerPolicy::toString);
luaCtx.registerFunction("__tostring", &ServerPolicy::toString);

ServerPolicy policies[] = {
static const std::array<ServerPolicy, 6> policies = {
ServerPolicy{"firstAvailable", firstAvailable, false},
ServerPolicy{"roundrobin", roundrobin, false},
ServerPolicy{"wrandom", wrandom, false},
ServerPolicy{"whashed", whashed, false},
ServerPolicy{"chashed", chashed, false},
ServerPolicy{"leastOutstanding", leastOutstanding, false}
};
for (auto& policy : policies) {
luaCtx.writeVariable(policy.d_name, policy);
for (const auto& policy : policies) {
luaCtx.writeVariable(policy.d_name, &policy);
}

#endif /* DISABLE_POLICIES_BINDINGS */
Expand Down

0 comments on commit be864c7

Please sign in to comment.