]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: make sure we do not allocate 16-byte aligned objects through lua(jit)
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Thu, 8 Feb 2024 14:28:24 +0000 (15:28 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Thu, 8 Feb 2024 14:30:22 +0000 (15:30 +0100)
luajit aligns only to 8 bytes by default, and some objects require
16 byte alignment.

Fixes #13766

Note that the static assert in LuaContext.hpp is commented out in
one case.  This trips on some platforms, but does not seem to be
harmful right now.

The fundamental solution remains the have luajit agree with C++ on
minimal alignment of its allocators.

ext/luawrapper/include/LuaContext.hpp
pdns/dnsdist-lua-bindings.cc

index ad6c86e556fe126d7013146125bee220a6111dbf..655375e7c9d6a8298cd8eee07bd66e8e1f5f80b1 100644 (file)
@@ -1643,6 +1643,7 @@ private:
               // 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));
             }
@@ -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));
 
@@ -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));
 
index 69638e23ec3a22ba54b92395748003498b26fca5..e1854a280edea3cd3c68d7b98fb557923e3f3540 100644 (file)
@@ -78,7 +78,7 @@ 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},
@@ -86,8 +86,8 @@ void setupLuaBindings(LuaContext& luaCtx, bool client, bool configCheck)
     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 */