From 48d133a5fd6bf8b9a3136b2d029713c84a205ea2 Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Sat, 16 May 2020 13:58:14 +0300 Subject: [PATCH] Fix GDNative wrapper type sizes (RID, Variant, Packed*Array), add size checking static asserts. --- modules/gdnative/gdnative/aabb.cpp | 2 ++ modules/gdnative/gdnative/array.cpp | 2 ++ modules/gdnative/gdnative/basis.cpp | 2 ++ modules/gdnative/gdnative/color.cpp | 2 ++ modules/gdnative/gdnative/dictionary.cpp | 2 ++ modules/gdnative/gdnative/node_path.cpp | 2 ++ modules/gdnative/gdnative/plane.cpp | 2 ++ modules/gdnative/gdnative/pool_arrays.cpp | 8 ++++++++ modules/gdnative/gdnative/quat.cpp | 2 ++ modules/gdnative/gdnative/rect2.cpp | 2 ++ modules/gdnative/gdnative/rid.cpp | 2 ++ modules/gdnative/gdnative/string.cpp | 4 ++++ modules/gdnative/gdnative/string_name.cpp | 2 ++ modules/gdnative/gdnative/transform.cpp | 2 ++ modules/gdnative/gdnative/transform2d.cpp | 2 ++ modules/gdnative/gdnative/variant.cpp | 2 ++ modules/gdnative/gdnative/vector2.cpp | 2 ++ modules/gdnative/gdnative/vector3.cpp | 2 ++ modules/gdnative/include/gdnative/pool_arrays.h | 14 +++++++------- modules/gdnative/include/gdnative/rid.h | 2 +- modules/gdnative/include/gdnative/variant.h | 2 +- 21 files changed, 53 insertions(+), 9 deletions(-) diff --git a/modules/gdnative/gdnative/aabb.cpp b/modules/gdnative/gdnative/aabb.cpp index 246e5d4e8d2..7f22c7dfe34 100644 --- a/modules/gdnative/gdnative/aabb.cpp +++ b/modules/gdnative/gdnative/aabb.cpp @@ -37,6 +37,8 @@ extern "C" { #endif +static_assert(sizeof(godot_aabb) == sizeof(AABB), "AABB size mismatch"); + void GDAPI godot_aabb_new(godot_aabb *r_dest, const godot_vector3 *p_pos, const godot_vector3 *p_size) { const Vector3 *pos = (const Vector3 *)p_pos; const Vector3 *size = (const Vector3 *)p_size; diff --git a/modules/gdnative/gdnative/array.cpp b/modules/gdnative/gdnative/array.cpp index 0c764ab8fdd..fb23863dc9e 100644 --- a/modules/gdnative/gdnative/array.cpp +++ b/modules/gdnative/gdnative/array.cpp @@ -41,6 +41,8 @@ extern "C" { #endif +static_assert(sizeof(godot_array) == sizeof(Array), "Array size mismatch"); + void GDAPI godot_array_new(godot_array *r_dest) { Array *dest = (Array *)r_dest; memnew_placement(dest, Array); diff --git a/modules/gdnative/gdnative/basis.cpp b/modules/gdnative/gdnative/basis.cpp index 4f489287b99..990fd3795d1 100644 --- a/modules/gdnative/gdnative/basis.cpp +++ b/modules/gdnative/gdnative/basis.cpp @@ -37,6 +37,8 @@ extern "C" { #endif +static_assert(sizeof(godot_basis) == sizeof(Basis), "Basis size mismatch"); + void GDAPI godot_basis_new_with_rows(godot_basis *r_dest, const godot_vector3 *p_x_axis, const godot_vector3 *p_y_axis, const godot_vector3 *p_z_axis) { const Vector3 *x_axis = (const Vector3 *)p_x_axis; const Vector3 *y_axis = (const Vector3 *)p_y_axis; diff --git a/modules/gdnative/gdnative/color.cpp b/modules/gdnative/gdnative/color.cpp index d79170771ae..c75e74daba6 100644 --- a/modules/gdnative/gdnative/color.cpp +++ b/modules/gdnative/gdnative/color.cpp @@ -37,6 +37,8 @@ extern "C" { #endif +static_assert(sizeof(godot_color) == sizeof(Color), "Color size mismatch"); + void GDAPI godot_color_new_rgba(godot_color *r_dest, const godot_real p_r, const godot_real p_g, const godot_real p_b, const godot_real p_a) { Color *dest = (Color *)r_dest; *dest = Color(p_r, p_g, p_b, p_a); diff --git a/modules/gdnative/gdnative/dictionary.cpp b/modules/gdnative/gdnative/dictionary.cpp index b145b88934b..a1269748151 100644 --- a/modules/gdnative/gdnative/dictionary.cpp +++ b/modules/gdnative/gdnative/dictionary.cpp @@ -39,6 +39,8 @@ extern "C" { #endif +static_assert(sizeof(godot_dictionary) == sizeof(Dictionary), "Dictionary size mismatch"); + void GDAPI godot_dictionary_new(godot_dictionary *r_dest) { Dictionary *dest = (Dictionary *)r_dest; memnew_placement(dest, Dictionary); diff --git a/modules/gdnative/gdnative/node_path.cpp b/modules/gdnative/gdnative/node_path.cpp index 93f43835c88..88ed650ebee 100644 --- a/modules/gdnative/gdnative/node_path.cpp +++ b/modules/gdnative/gdnative/node_path.cpp @@ -37,6 +37,8 @@ extern "C" { #endif +static_assert(sizeof(godot_node_path) == sizeof(NodePath), "NodePath size mismatch"); + void GDAPI godot_node_path_new(godot_node_path *r_dest, const godot_string *p_from) { NodePath *dest = (NodePath *)r_dest; const String *from = (const String *)p_from; diff --git a/modules/gdnative/gdnative/plane.cpp b/modules/gdnative/gdnative/plane.cpp index 923308dc349..663937f9063 100644 --- a/modules/gdnative/gdnative/plane.cpp +++ b/modules/gdnative/gdnative/plane.cpp @@ -37,6 +37,8 @@ extern "C" { #endif +static_assert(sizeof(godot_plane) == sizeof(Plane), "Plane size mismatch"); + void GDAPI godot_plane_new_with_reals(godot_plane *r_dest, const godot_real p_a, const godot_real p_b, const godot_real p_c, const godot_real p_d) { Plane *dest = (Plane *)r_dest; *dest = Plane(p_a, p_b, p_c, p_d); diff --git a/modules/gdnative/gdnative/pool_arrays.cpp b/modules/gdnative/gdnative/pool_arrays.cpp index 589b4d4dfe4..652f59cd07a 100644 --- a/modules/gdnative/gdnative/pool_arrays.cpp +++ b/modules/gdnative/gdnative/pool_arrays.cpp @@ -42,6 +42,14 @@ extern "C" { #endif +static_assert(sizeof(godot_packed_byte_array) == sizeof(Vector), "Vector size mismatch"); +static_assert(sizeof(godot_packed_int_array) == sizeof(Vector), "Vector size mismatch"); +static_assert(sizeof(godot_packed_real_array) == sizeof(Vector), "Vector size mismatch"); +static_assert(sizeof(godot_packed_string_array) == sizeof(Vector), "Vector size mismatch"); +static_assert(sizeof(godot_packed_vector2_array) == sizeof(Vector), "Vector size mismatch"); +static_assert(sizeof(godot_packed_vector3_array) == sizeof(Vector), "Vector size mismatch"); +static_assert(sizeof(godot_packed_color_array) == sizeof(Vector), "Vector size mismatch"); + #define memnew_placement_custom(m_placement, m_class, m_constr) _post_initialize(new (m_placement, sizeof(m_class), "") m_constr) // byte diff --git a/modules/gdnative/gdnative/quat.cpp b/modules/gdnative/gdnative/quat.cpp index 15c04f71916..de6308ad2ab 100644 --- a/modules/gdnative/gdnative/quat.cpp +++ b/modules/gdnative/gdnative/quat.cpp @@ -37,6 +37,8 @@ extern "C" { #endif +static_assert(sizeof(godot_quat) == sizeof(Quat), "Quat size mismatch"); + void GDAPI godot_quat_new(godot_quat *r_dest, const godot_real p_x, const godot_real p_y, const godot_real p_z, const godot_real p_w) { Quat *dest = (Quat *)r_dest; *dest = Quat(p_x, p_y, p_z, p_w); diff --git a/modules/gdnative/gdnative/rect2.cpp b/modules/gdnative/gdnative/rect2.cpp index a2f735172f8..63cbbaa3cfc 100644 --- a/modules/gdnative/gdnative/rect2.cpp +++ b/modules/gdnative/gdnative/rect2.cpp @@ -37,6 +37,8 @@ extern "C" { #endif +static_assert(sizeof(godot_rect2) == sizeof(Rect2), "Rect2 size mismatch"); + void GDAPI godot_rect2_new_with_position_and_size(godot_rect2 *r_dest, const godot_vector2 *p_pos, const godot_vector2 *p_size) { const Vector2 *position = (const Vector2 *)p_pos; const Vector2 *size = (const Vector2 *)p_size; diff --git a/modules/gdnative/gdnative/rid.cpp b/modules/gdnative/gdnative/rid.cpp index 7ea80123a3d..d7a63f33a72 100644 --- a/modules/gdnative/gdnative/rid.cpp +++ b/modules/gdnative/gdnative/rid.cpp @@ -38,6 +38,8 @@ extern "C" { #endif +static_assert(sizeof(godot_rid) == sizeof(RID), "RID size mismatch"); + void GDAPI godot_rid_new(godot_rid *r_dest) { RID *dest = (RID *)r_dest; memnew_placement(dest, RID); diff --git a/modules/gdnative/gdnative/string.cpp b/modules/gdnative/gdnative/string.cpp index a22af89edc9..724a4b56cb4 100644 --- a/modules/gdnative/gdnative/string.cpp +++ b/modules/gdnative/gdnative/string.cpp @@ -40,6 +40,10 @@ extern "C" { #endif +static_assert(sizeof(godot_char_string) == sizeof(CharString), "CharString size mismatch"); +static_assert(sizeof(godot_string) == sizeof(String), "String size mismatch"); +static_assert(sizeof(godot_char_type) == sizeof(CharType), "CharType size mismatch"); + godot_int GDAPI godot_char_string_length(const godot_char_string *p_cs) { const CharString *cs = (const CharString *)p_cs; diff --git a/modules/gdnative/gdnative/string_name.cpp b/modules/gdnative/gdnative/string_name.cpp index 1abb4486b1d..7bbaaeeaa03 100644 --- a/modules/gdnative/gdnative/string_name.cpp +++ b/modules/gdnative/gdnative/string_name.cpp @@ -39,6 +39,8 @@ extern "C" { #endif +static_assert(sizeof(godot_string_name) == sizeof(StringName), "StringName size mismatch"); + void GDAPI godot_string_name_new(godot_string_name *r_dest, const godot_string *p_name) { StringName *dest = (StringName *)r_dest; const String *name = (const String *)p_name; diff --git a/modules/gdnative/gdnative/transform.cpp b/modules/gdnative/gdnative/transform.cpp index c9b3e37fb2e..d19de93e9b8 100644 --- a/modules/gdnative/gdnative/transform.cpp +++ b/modules/gdnative/gdnative/transform.cpp @@ -37,6 +37,8 @@ extern "C" { #endif +static_assert(sizeof(godot_transform) == sizeof(Transform), "Transform size mismatch"); + void GDAPI godot_transform_new_with_axis_origin(godot_transform *r_dest, const godot_vector3 *p_x_axis, const godot_vector3 *p_y_axis, const godot_vector3 *p_z_axis, const godot_vector3 *p_origin) { const Vector3 *x_axis = (const Vector3 *)p_x_axis; const Vector3 *y_axis = (const Vector3 *)p_y_axis; diff --git a/modules/gdnative/gdnative/transform2d.cpp b/modules/gdnative/gdnative/transform2d.cpp index 26a71333b14..c0f7878eb09 100644 --- a/modules/gdnative/gdnative/transform2d.cpp +++ b/modules/gdnative/gdnative/transform2d.cpp @@ -37,6 +37,8 @@ extern "C" { #endif +static_assert(sizeof(godot_transform2d) == sizeof(Transform2D), "Transform2D size mismatch"); + void GDAPI godot_transform2d_new(godot_transform2d *r_dest, const godot_real p_rot, const godot_vector2 *p_pos) { const Vector2 *pos = (const Vector2 *)p_pos; Transform2D *dest = (Transform2D *)r_dest; diff --git a/modules/gdnative/gdnative/variant.cpp b/modules/gdnative/gdnative/variant.cpp index f0fc44ae8ad..29d0f96b97d 100644 --- a/modules/gdnative/gdnative/variant.cpp +++ b/modules/gdnative/gdnative/variant.cpp @@ -37,6 +37,8 @@ extern "C" { #endif +static_assert(sizeof(godot_variant) == sizeof(Variant), "Variant size mismatch"); + // Workaround GCC ICE on armv7hl which was affected GCC 6.0 up to 8.0 (GH-16100). // It was fixed upstream in 8.1, and a fix was backported to 7.4. // This can be removed once no supported distro ships with versions older than 7.4. diff --git a/modules/gdnative/gdnative/vector2.cpp b/modules/gdnative/gdnative/vector2.cpp index b6c3569f422..72c3c0ec353 100644 --- a/modules/gdnative/gdnative/vector2.cpp +++ b/modules/gdnative/gdnative/vector2.cpp @@ -37,6 +37,8 @@ extern "C" { #endif +static_assert(sizeof(godot_vector2) == sizeof(Vector2), "Vector2 size mismatch"); + void GDAPI godot_vector2_new(godot_vector2 *r_dest, const godot_real p_x, const godot_real p_y) { Vector2 *dest = (Vector2 *)r_dest; *dest = Vector2(p_x, p_y); diff --git a/modules/gdnative/gdnative/vector3.cpp b/modules/gdnative/gdnative/vector3.cpp index 3e272ae9df9..16fbdf353a0 100644 --- a/modules/gdnative/gdnative/vector3.cpp +++ b/modules/gdnative/gdnative/vector3.cpp @@ -37,6 +37,8 @@ extern "C" { #endif +static_assert(sizeof(godot_vector3) == sizeof(Vector3), "Vector3 size mismatch"); + void GDAPI godot_vector3_new(godot_vector3 *r_dest, const godot_real p_x, const godot_real p_y, const godot_real p_z) { Vector3 *dest = (Vector3 *)r_dest; *dest = Vector3(p_x, p_y, p_z); diff --git a/modules/gdnative/include/gdnative/pool_arrays.h b/modules/gdnative/include/gdnative/pool_arrays.h index c610377f549..652bd6ae1c8 100644 --- a/modules/gdnative/include/gdnative/pool_arrays.h +++ b/modules/gdnative/include/gdnative/pool_arrays.h @@ -39,7 +39,7 @@ extern "C" { /////// PackedByteArray -#define GODOT_PACKED_BYTE_ARRAY_SIZE sizeof(void *) +#define GODOT_PACKED_BYTE_ARRAY_SIZE (2 * sizeof(void *)) #ifndef GODOT_CORE_API_GODOT_PACKED_BYTE_ARRAY_TYPE_DEFINED #define GODOT_CORE_API_GODOT_PACKED_BYTE_ARRAY_TYPE_DEFINED @@ -50,7 +50,7 @@ typedef struct { /////// PackedInt32Array -#define GODOT_PACKED_INT_ARRAY_SIZE sizeof(void *) +#define GODOT_PACKED_INT_ARRAY_SIZE (2 * sizeof(void *)) #ifndef GODOT_CORE_API_GODOT_PACKED_INT_ARRAY_TYPE_DEFINED #define GODOT_CORE_API_GODOT_PACKED_INT_ARRAY_TYPE_DEFINED @@ -61,7 +61,7 @@ typedef struct { /////// PackedFloat32Array -#define GODOT_PACKED_REAL_ARRAY_SIZE sizeof(void *) +#define GODOT_PACKED_REAL_ARRAY_SIZE (2 * sizeof(void *)) #ifndef GODOT_CORE_API_GODOT_PACKED_REAL_ARRAY_TYPE_DEFINED #define GODOT_CORE_API_GODOT_PACKED_REAL_ARRAY_TYPE_DEFINED @@ -72,7 +72,7 @@ typedef struct { /////// PackedStringArray -#define GODOT_PACKED_STRING_ARRAY_SIZE sizeof(void *) +#define GODOT_PACKED_STRING_ARRAY_SIZE (2 * sizeof(void *)) #ifndef GODOT_CORE_API_GODOT_PACKED_STRING_ARRAY_TYPE_DEFINED #define GODOT_CORE_API_GODOT_PACKED_STRING_ARRAY_TYPE_DEFINED @@ -83,7 +83,7 @@ typedef struct { /////// PackedVector2Array -#define GODOT_PACKED_VECTOR2_ARRAY_SIZE sizeof(void *) +#define GODOT_PACKED_VECTOR2_ARRAY_SIZE (2 * sizeof(void *)) #ifndef GODOT_CORE_API_GODOT_PACKED_VECTOR2_ARRAY_TYPE_DEFINED #define GODOT_CORE_API_GODOT_PACKED_VECTOR2_ARRAY_TYPE_DEFINED @@ -94,7 +94,7 @@ typedef struct { /////// PackedVector3Array -#define GODOT_PACKED_VECTOR3_ARRAY_SIZE sizeof(void *) +#define GODOT_PACKED_VECTOR3_ARRAY_SIZE (2 * sizeof(void *)) #ifndef GODOT_CORE_API_GODOT_PACKED_VECTOR3_ARRAY_TYPE_DEFINED #define GODOT_CORE_API_GODOT_PACKED_VECTOR3_ARRAY_TYPE_DEFINED @@ -105,7 +105,7 @@ typedef struct { /////// PackedColorArray -#define GODOT_PACKED_COLOR_ARRAY_SIZE sizeof(void *) +#define GODOT_PACKED_COLOR_ARRAY_SIZE (2 * sizeof(void *)) #ifndef GODOT_CORE_API_GODOT_PACKED_COLOR_ARRAY_TYPE_DEFINED #define GODOT_CORE_API_GODOT_PACKED_COLOR_ARRAY_TYPE_DEFINED diff --git a/modules/gdnative/include/gdnative/rid.h b/modules/gdnative/include/gdnative/rid.h index 04661cedc82..73b601dc047 100644 --- a/modules/gdnative/include/gdnative/rid.h +++ b/modules/gdnative/include/gdnative/rid.h @@ -37,7 +37,7 @@ extern "C" { #include -#define GODOT_RID_SIZE sizeof(void *) +#define GODOT_RID_SIZE sizeof(uint64_t) #ifndef GODOT_CORE_API_GODOT_RID_TYPE_DEFINED #define GODOT_CORE_API_GODOT_RID_TYPE_DEFINED diff --git a/modules/gdnative/include/gdnative/variant.h b/modules/gdnative/include/gdnative/variant.h index 934e856fbfb..682c3e3ba8c 100644 --- a/modules/gdnative/include/gdnative/variant.h +++ b/modules/gdnative/include/gdnative/variant.h @@ -37,7 +37,7 @@ extern "C" { #include -#define GODOT_VARIANT_SIZE (16 + sizeof(void *)) +#define GODOT_VARIANT_SIZE (16 + sizeof(int64_t)) #ifndef GODOT_CORE_API_GODOT_VARIANT_TYPE_DEFINED #define GODOT_CORE_API_GODOT_VARIANT_TYPE_DEFINED