Skip to content

cxx-qt-lib: ensure correct sizes for wasm 32bit #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Support for generating correct C++ code for `Pin<T>` Rust types
- Support namespace attribute on shared types, QObject struct, and extern blocks
- Asserts for 32bit platforms such as Wasm

## [0.4.1](https://github.com/KDAB/cxx-qt/compare/v0.4.0...v0.4.1) - 2022-11-18

Expand Down
7 changes: 3 additions & 4 deletions crates/cxx-qt-lib/src/core/qmodelindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

#include "../assertion_utils.h"

// QModelIndex has two ints, a uint pointer, and a pointer.
// This results in 4 + 4 + 4 + 8 = 20, then due to compiler padding this results
// in size of 24 or three pointers.
// QModelIndex has two ints, a quint pointer (same as size_t), and a pointer.
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/itemmodels/qabstractitemmodel.h?h=v5.15.6-lts-lgpl#n93
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/itemmodels/qabstractitemmodel.h?h=v6.2.4#n195
assert_alignment_and_size(QModelIndex,
alignof(::std::size_t),
sizeof(::std::size_t[3]));
(sizeof(::std::int32_t) * 2) + sizeof(::std::size_t) +
sizeof(::std::size_t));

static_assert(::std::is_trivially_copyable<QModelIndex>::value);
5 changes: 4 additions & 1 deletion crates/cxx-qt-lib/src/core/qmodelindex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ mod ffi {
#[derive(Clone)]
#[repr(C)]
pub struct QModelIndex {
_space: MaybeUninit<[usize; 3]>,
_r: MaybeUninit<i32>,
_c: MaybeUninit<i32>,
_i: MaybeUninit<usize>,
_m: MaybeUninit<usize>,
}

impl Default for QModelIndex {
Expand Down
16 changes: 14 additions & 2 deletions crates/cxx-qt-lib/src/core/qvariant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,22 @@ pub struct QVariant {
///
/// Qt5 QVariant has one member, which contains three uints (but they are optimised to a size of 8) and a union
/// Qt6 QVariant has one member, which contains three pointers and a union (pointer largest)
_data: MaybeUninit<f64>,

// Compiler optimisations reduce the size of the uint to a ushort
#[cfg(qt_version_major = "5")]
_type: MaybeUninit<u16>,
#[cfg(qt_version_major = "5")]
_is_shared: MaybeUninit<u16>,
#[cfg(qt_version_major = "5")]
_space: MaybeUninit<[usize; 2]>,
_is_null: MaybeUninit<u16>,

#[cfg(qt_version_major = "6")]
_is_shared: MaybeUninit<usize>,
#[cfg(qt_version_major = "6")]
_is_null: MaybeUninit<usize>,
#[cfg(qt_version_major = "6")]
_space: MaybeUninit<[usize; 4]>,
_packed_type: MaybeUninit<usize>,
}

impl Clone for QVariant {
Expand Down
43 changes: 32 additions & 11 deletions crates/cxx-qt-lib/src/core/qvariant/qvariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,45 @@

// The layout has changed between Qt 5 and Qt 6
//
// Qt5 QVariant has one member, which contains three uints and a union.
// The three uints are optimised to a reduced size, resulting in a combined size
// of two pointers.
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/kernel/qvariant.h?h=v5.15.6-lts-lgpl#n491
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/kernel/qvariant.h?h=v5.15.6-lts-lgpl#n411
//
// Qt6 QVariant has one member, which contains three pointers and a union
// (with a pointer as the largest member)
// (with a pointer / double as the largest member)
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/kernel/qvariant.h?h=v6.2.4#n540
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/kernel/qvariant.h?h=v6.2.4#n474
//
// Qt5 QVariant has one member, which contains three uints and a union
// (with a pointer / double as the largest member)
// The three uints are optimised to a reduced size of ushorts
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/kernel/qvariant.h?h=v5.15.6-lts-lgpl#n491
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/kernel/qvariant.h?h=v5.15.6-lts-lgpl#n411
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))

#if (QT_POINTER_SIZE == 4)
// 32bit is 3 * 32bit ptr (12) + union with double (8) + 4 bytes padding
// alignment is 8 byte on 32bit systems as well due to the double
assert_alignment_and_size(QVariant,
alignof(::std::size_t),
sizeof(::std::size_t[4]));
alignof(double),
(sizeof(::std::size_t) * 3) + sizeof(double) +
4 /* compiler padding */);
#else
// 64bit is 3 * 64ptr ptr (16) + union with double (8)
// alignment is 8 bytes from the double or the pointer on 64bit systems
assert_alignment_and_size(QVariant,
alignof(::std::size_t),
sizeof(::std::size_t[2]));
alignof(double),
(sizeof(::std::size_t) * 3) + sizeof(double));
#endif

#else

// 3 * uint (12) + union with double (8)
// but due to compiler optimisation it ends up as
// 3 * ushort (6) + union with double (8) + 2 bytes padding
// alignment is 8 byte on 32bit systems as well due to the double
assert_alignment_and_size(
QVariant,
alignof(double),
(sizeof(::std::uint16_t /* compiler optimised from ::std::uint32_t */) * 3) +
sizeof(double) + 2 /* compiler padding */);

#endif

static_assert(!::std::is_trivially_copy_assignable<QVariant>::value);
Expand Down
8 changes: 5 additions & 3 deletions crates/cxx-qt-lib/src/gui/qcolor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
#include "../assertion_utils.h"

// QColor has an enum with six values and a union with the largest being five
// ushorts. This results in (5 * std::uint16) + std::uint32_t = 14, then due to
// compiler padding this results in a sizeof 16 or two pointers.
// ushorts. This results in std::int32_t + (5 * std::uint16) = 14, then due to
// compiler padding this results in a sizeof 16.
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/painting/qcolor.h?h=v5.15.6-lts-lgpl#n262
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/painting/qcolor.h?h=v6.2.4#n237
assert_alignment_and_size(QColor,
alignof(::std::size_t),
sizeof(::std::size_t[2]));
sizeof(::std::int32_t) +
(sizeof(::std::uint16_t) * 5) +
2 /* compiler padding */);

// QColor still had copy & move constructors in Qt 5 but they were basically
// trivial.
Expand Down
4 changes: 3 additions & 1 deletion crates/cxx-qt-lib/src/gui/qcolor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ mod ffi {
#[derive(Clone)]
#[repr(C)]
pub struct QColor {
_space: MaybeUninit<[usize; 2]>,
_cspec: MaybeUninit<i32>,
_ct: MaybeUninit<[u16; 5]>,
_padding: MaybeUninit<u16>,
}

impl QColor {
Expand Down