Package: src:luabind
Version: 1.1.1-1
Severity: important
Tags: patch
User: [email protected]
Usertags: origin-ubuntu stonking ubuntu-patch
Hi,

luabind 1.1.1-1 fails to build in Ubuntu on amd64: tests in
the static build segfault. Ubuntu builds with -flto=auto. The bug itself
is in the code, and Debian only escapes it because it doesn't build with
LTO.

object_rep is placement-new'ed into memory from lua_newuserdata()
(src/object_rep.cpp, push_new_instance()). That memory is only aligned
like LUAI_USER_ALIGNMENT_T (double/void*/long), i.e. 8 bytes. object_rep
starts with

    boost::aligned_storage<instance_buffer_size> m_instance_buffer;

which requires alignof(max_align_t), i.e. 16 on x86_64. With LTO, GCC 15
inlines the object_rep constructor into push_new_instance() and
initialises m_instance/m_classrep with an aligned 16-byte store:

    movaps %xmm0,0x20(%rax)    ; rax = 0x...558 from lua_newuserdata()

That faults on x86. arm64 also uses LTO in Ubuntu but tolerates the
misaligned store.

The same bug was reported and fixed in the Oberon00 luabind fork:
  https://github.com/Oberon00/luabind/issues/42
  https://github.com/Oberon00/luabind/pull/50

https://github.com/Oberon00/luabind/commit/49814f6b47ed99e273edc5198a6ebd7fa19e813a

The attached patch is a cherry-pick of that commit. It declares the
buffer with lua_newuserdata()'s alignment. With it, Ubuntu's LTO build
passes tests in both the static and shared builds. We are carrying
it in Ubuntu as 1.1.1-1ubuntu1 (LP: #2170791).

Cheers,
Sebastien
From 49814f6b47ed99e273edc5198a6ebd7fa19e813a Mon Sep 17 00:00:00 2001
From: Jan Boon <[email protected]>
Date: Sat, 19 Feb 2022 17:30:38 +0100
Subject: [PATCH] Fix bad alignment assumption in `object_rep` broken by
 lua_newuserdata (#50)

* Match alignment of `object_rep` to `lua_newuserdata`. Fixes alignment assumption in placement new
* Compatibility with lua 5.3

Origin: other, https://github.com/Oberon00/luabind/commit/49814f6b47ed99e273edc5198a6ebd7fa19e813a
Bug: https://github.com/Oberon00/luabind/issues/42
Forwarded: not-needed
Last-Update: 2026-10-14

---
 luabind/detail/object_rep.hpp | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/luabind/detail/object_rep.hpp b/luabind/detail/object_rep.hpp
index 07272e64..519f7140 100644
--- a/luabind/detail/object_rep.hpp
+++ b/luabind/detail/object_rep.hpp
@@ -97,8 +97,14 @@ namespace luabind { namespace detail
 
         void operator=(object_rep const&);
 
+#ifdef LUAI_MAXALIGN
+        struct cD { char c; union { LUAI_MAXALIGN; } u; };
+#else
+        struct cD { char c; union { lua_Number n; double u; void *s; lua_Integer i; long l; } u; };
+#endif
         BOOST_STATIC_CONSTANT(std::size_t, instance_buffer_size=32);
-        boost::aligned_storage<instance_buffer_size> m_instance_buffer;
+        BOOST_STATIC_CONSTANT(std::size_t, instance_buffer_align=offsetof(struct cD, u));
+        boost::aligned_storage<instance_buffer_size, instance_buffer_align> m_instance_buffer;
         instance_holder* m_instance;
         class_rep* m_classrep; // the class information about this object's type
         std::size_t m_dependency_cnt; // counts dependencies

Reply via email to