From 2f88628f7763e352f3ca1db89e9c6847ef81c3e0 Mon Sep 17 00:00:00 2001 From: Pavlo Bashmakov <157482+bexcite@users.noreply.github.com> Date: Sat, 25 Mar 2023 12:45:56 -0700 Subject: [PATCH 01/10] Rare fix: kick fix test --- tests/alignment_test.cpp | 31 ++++++++++++++++++++++++++----- tests/alignment_test.fbs | 6 +++--- tests/test.cpp | 24 ++++++++++++------------ 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/tests/alignment_test.cpp b/tests/alignment_test.cpp index 731c328666d..e3706a31c5d 100644 --- a/tests/alignment_test.cpp +++ b/tests/alignment_test.cpp @@ -3,26 +3,47 @@ #include "tests/alignment_test_generated.h" #include "flatbuffers/flatbuffer_builder.h" #include "test_assert.h" +#include namespace flatbuffers { namespace tests { void AlignmentTest() { + std::cout << "uo! 1\n"; + FlatBufferBuilder builder; BadAlignmentLarge large; Offset outer_large = CreateOuterLarge(builder, &large); - BadAlignmentSmall *small; - Offset> small_offset = - builder.CreateUninitializedVectorOfStructs(9, &small); - (void)small; // We do not have to write data to trigger the test failure + // BadAlignmentSmall *small; + // Offset> small_offset = + // builder.CreateUninitializedVectorOfStructs(9, &small); + // (void)small; // We do not have to write data to trigger the test failure + + // Offset> small_offset = + // builder.CreateUninitializedVectorOfStructs(9, &small); + + // Offset root = + // CreateBadAlignmentRoot(builder, outer_large, small_offset); + + std::vector small_vector{{2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}}; Offset root = - CreateBadAlignmentRoot(builder, outer_large, small_offset); + CreateBadAlignmentRootDirect(builder, outer_large, &small_vector); builder.Finish(root); + auto root_msg = + flatbuffers::GetRoot(builder.GetBufferPointer()); + + std::cout << "msg.size = " << root_msg->small()->size() << std::endl; + for (size_t i = 0; i < root_msg->small()->size(); ++i) { + std::cout << "msg.small[" << i + << "] = " << (int)root_msg->small()->Get(i)->var_0() << ":" + << (int)root_msg->small()->Get(i)->var_1() << std::endl; + } + Verifier verifier(builder.GetBufferPointer(), builder.GetSize()); TEST_ASSERT(VerifyBadAlignmentRootBuffer(verifier)); } diff --git a/tests/alignment_test.fbs b/tests/alignment_test.fbs index 3fc08bd7ff5..cd407cb472e 100644 --- a/tests/alignment_test.fbs +++ b/tests/alignment_test.fbs @@ -1,9 +1,9 @@ // sizeof(BadAlignmentSmall) == 12 // alignof(BadAlignmentSmall) == 4 struct BadAlignmentSmall { - var_0: uint; - var_1: uint; - var_2: uint; + var_0: uint8; + var_1: uint8; + // var_2: uint; } // sizeof(BadAlignmentLarge) == 8 diff --git a/tests/test.cpp b/tests/test.cpp index be2811efee4..50ad1fe8e5a 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1601,23 +1601,23 @@ static void Offset64Tests() { int FlatBufferTests(const std::string &tests_data_path) { // Run our various test suites: - std::string rawbuf; - auto flatbuf1 = CreateFlatBufferTest(rawbuf); - auto flatbuf = std::move(flatbuf1); // Test move assignment. + // std::string rawbuf; + // auto flatbuf1 = CreateFlatBufferTest(rawbuf); + // auto flatbuf = std::move(flatbuf1); // Test move assignment. - AccessFlatBufferTest(reinterpret_cast(rawbuf.c_str()), - rawbuf.length()); - AccessFlatBufferTest(flatbuf.data(), flatbuf.size()); + // AccessFlatBufferTest(reinterpret_cast(rawbuf.c_str()), + // rawbuf.length()); + // AccessFlatBufferTest(flatbuf.data(), flatbuf.size()); - MutateFlatBuffersTest(flatbuf.data(), flatbuf.size()); + // MutateFlatBuffersTest(flatbuf.data(), flatbuf.size()); - ObjectFlatBuffersTest(flatbuf.data()); - UnPackTo(flatbuf.data()); + // ObjectFlatBuffersTest(flatbuf.data()); + // UnPackTo(flatbuf.data()); - MiniReflectFlatBuffersTest(flatbuf.data()); - MiniReflectFixedLengthArrayTest(); + // MiniReflectFlatBuffersTest(flatbuf.data()); + // MiniReflectFixedLengthArrayTest(); - SizePrefixedTest(); + // SizePrefixedTest(); AlignmentTest(); From 95201bb3681925d951deb9bea3252450823e485a Mon Sep 17 00:00:00 2001 From: Pavlo Bashmakov <157482+bexcite@users.noreply.github.com> Date: Tue, 28 Mar 2023 17:07:45 -0700 Subject: [PATCH 02/10] Rare fix: real fix --- include/flatbuffers/flatbuffer_builder.h | 6 ++---- tests/alignment_test.cpp | 10 +++++++--- tests/alignment_test.fbs | 6 ++++++ tests/test.cpp | 24 ++++++++++++------------ 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/include/flatbuffers/flatbuffer_builder.h b/include/flatbuffers/flatbuffer_builder.h index a33c8966976..f3ffeb0d70a 100644 --- a/include/flatbuffers/flatbuffer_builder.h +++ b/include/flatbuffers/flatbuffer_builder.h @@ -913,8 +913,7 @@ template class FlatBufferBuilderImpl { typedef typename VectorT::size_type LenT; typedef typename OffsetT>::offset_type offset_type; - StartVector(len * sizeof(T) / AlignOf(), sizeof(T), - AlignOf()); + StartVector(len, sizeof(T), AlignOf()); if (len > 0) { PushBytes(reinterpret_cast(v), sizeof(T) * len); } @@ -1384,8 +1383,7 @@ template class FlatBufferBuilderImpl { // Must be completed with EndVectorOfStructs(). template class OffsetT = Offset> T *StartVectorOfStructs(size_t vector_size) { - StartVector(vector_size * sizeof(T) / AlignOf(), sizeof(T), - AlignOf()); + StartVector(vector_size, sizeof(T), AlignOf()); return reinterpret_cast(buf_.make_space(vector_size * sizeof(T))); } diff --git a/tests/alignment_test.cpp b/tests/alignment_test.cpp index e3706a31c5d..39a3d320e60 100644 --- a/tests/alignment_test.cpp +++ b/tests/alignment_test.cpp @@ -8,9 +8,7 @@ namespace flatbuffers { namespace tests { -void AlignmentTest() { - std::cout << "uo! 1\n"; - +void AlignmentTest() { FlatBufferBuilder builder; BadAlignmentLarge large; @@ -32,6 +30,12 @@ void AlignmentTest() { Offset root = CreateBadAlignmentRootDirect(builder, outer_large, &small_vector); + // builder.ForceVectorAlignment(small_vector.size(), sizeof(BadAlignmentSmall), 8); + // auto small__ = builder.CreateVectorOfStructs(small_vector); + // auto small__ = CreateVectorOfStructs(builder, small_vector); + // Offset root = + // CreateBadAlignmentRoot(builder, outer_large, small__); + builder.Finish(root); auto root_msg = diff --git a/tests/alignment_test.fbs b/tests/alignment_test.fbs index cd407cb472e..0a73b9bd90a 100644 --- a/tests/alignment_test.fbs +++ b/tests/alignment_test.fbs @@ -16,9 +16,15 @@ table OuterLarge { large: BadAlignmentLarge; } +struct JustSmallStruct { + var_0: uint8; + var_1: uint8; +} + table BadAlignmentRoot { large: OuterLarge; small: [BadAlignmentSmall]; + small_structs: [JustSmallStruct]; } root_type BadAlignmentRoot; diff --git a/tests/test.cpp b/tests/test.cpp index 50ad1fe8e5a..be2811efee4 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1601,23 +1601,23 @@ static void Offset64Tests() { int FlatBufferTests(const std::string &tests_data_path) { // Run our various test suites: - // std::string rawbuf; - // auto flatbuf1 = CreateFlatBufferTest(rawbuf); - // auto flatbuf = std::move(flatbuf1); // Test move assignment. + std::string rawbuf; + auto flatbuf1 = CreateFlatBufferTest(rawbuf); + auto flatbuf = std::move(flatbuf1); // Test move assignment. - // AccessFlatBufferTest(reinterpret_cast(rawbuf.c_str()), - // rawbuf.length()); - // AccessFlatBufferTest(flatbuf.data(), flatbuf.size()); + AccessFlatBufferTest(reinterpret_cast(rawbuf.c_str()), + rawbuf.length()); + AccessFlatBufferTest(flatbuf.data(), flatbuf.size()); - // MutateFlatBuffersTest(flatbuf.data(), flatbuf.size()); + MutateFlatBuffersTest(flatbuf.data(), flatbuf.size()); - // ObjectFlatBuffersTest(flatbuf.data()); - // UnPackTo(flatbuf.data()); + ObjectFlatBuffersTest(flatbuf.data()); + UnPackTo(flatbuf.data()); - // MiniReflectFlatBuffersTest(flatbuf.data()); - // MiniReflectFixedLengthArrayTest(); + MiniReflectFlatBuffersTest(flatbuf.data()); + MiniReflectFixedLengthArrayTest(); - // SizePrefixedTest(); + SizePrefixedTest(); AlignmentTest(); From 507d3ad3267ec2b1f2ae6a0d5cafd5d1965a746f Mon Sep 17 00:00:00 2001 From: Pavlo Bashmakov <157482+bexcite@users.noreply.github.com> Date: Tue, 28 Mar 2023 21:37:53 -0700 Subject: [PATCH 03/10] Rare fix: separate test --- tests/alignment_test.cpp | 61 ++++++++++++++++++++++------------------ tests/alignment_test.fbs | 15 ++++++---- 2 files changed, 43 insertions(+), 33 deletions(-) diff --git a/tests/alignment_test.cpp b/tests/alignment_test.cpp index 39a3d320e60..de1bfc326e9 100644 --- a/tests/alignment_test.cpp +++ b/tests/alignment_test.cpp @@ -14,42 +14,49 @@ void AlignmentTest() { BadAlignmentLarge large; Offset outer_large = CreateOuterLarge(builder, &large); - // BadAlignmentSmall *small; - // Offset> small_offset = - // builder.CreateUninitializedVectorOfStructs(9, &small); - // (void)small; // We do not have to write data to trigger the test failure + BadAlignmentSmall *small; + Offset> small_offset = + builder.CreateUninitializedVectorOfStructs(9, &small); + (void)small; // We do not have to write data to trigger the test failure - // Offset> small_offset = - // builder.CreateUninitializedVectorOfStructs(9, &small); + Offset root = + CreateBadAlignmentRoot(builder, outer_large, small_offset); - // Offset root = - // CreateBadAlignmentRoot(builder, outer_large, small_offset); + builder.Finish(root); - std::vector small_vector{{2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}}; + Verifier verifier(builder.GetBufferPointer(), builder.GetSize()); + TEST_ASSERT(VerifyBadAlignmentRootBuffer(verifier)); - Offset root = - CreateBadAlignmentRootDirect(builder, outer_large, &small_vector); + builder.Clear(); - // builder.ForceVectorAlignment(small_vector.size(), sizeof(BadAlignmentSmall), 8); - // auto small__ = builder.CreateVectorOfStructs(small_vector); - // auto small__ = CreateVectorOfStructs(builder, small_vector); - // Offset root = - // CreateBadAlignmentRoot(builder, outer_large, small__); + std::vector small_vector = { + { 2, 1 }, { 3, 1 }, { 4, 1 }, { 5, 1 }, { 6, 1 } + }; + Offset small_structs_root = + CreateSmallStructsDirect(builder, &small_vector); - builder.Finish(root); + builder.Finish(small_structs_root); - auto root_msg = - flatbuffers::GetRoot(builder.GetBufferPointer()); + Verifier verifier_small_structs(builder.GetBufferPointer(), + builder.GetSize()); + TEST_ASSERT(verifier_small_structs.VerifyBuffer(nullptr)); - std::cout << "msg.size = " << root_msg->small()->size() << std::endl; - for (size_t i = 0; i < root_msg->small()->size(); ++i) { - std::cout << "msg.small[" << i - << "] = " << (int)root_msg->small()->Get(i)->var_0() << ":" - << (int)root_msg->small()->Get(i)->var_1() << std::endl; + auto root_msg = + flatbuffers::GetRoot(builder.GetBufferPointer()); + + std::cout << "msg.size = " << root_msg->small_structs()->size() << std::endl; + TEST_EQ(root_msg->small_structs()->size(), small_vector.size()); + + for (size_t i = 0; i < root_msg->small_structs()->size(); ++i) { + std::cout << "msg.small_structs[" << i + << "] = " << (int)root_msg->small_structs()->Get(i)->var_0() + << ":" << (int)root_msg->small_structs()->Get(i)->var_1() + << std::endl; + TEST_EQ(root_msg->small_structs()->Get(i)->var_0(), + small_vector[i].var_0()); + TEST_EQ(root_msg->small_structs()->Get(i)->var_1(), + small_vector[i].var_1()); } - - Verifier verifier(builder.GetBufferPointer(), builder.GetSize()); - TEST_ASSERT(VerifyBadAlignmentRootBuffer(verifier)); } } // namespace tests diff --git a/tests/alignment_test.fbs b/tests/alignment_test.fbs index 0a73b9bd90a..ff69337d65c 100644 --- a/tests/alignment_test.fbs +++ b/tests/alignment_test.fbs @@ -1,9 +1,9 @@ // sizeof(BadAlignmentSmall) == 12 // alignof(BadAlignmentSmall) == 4 struct BadAlignmentSmall { - var_0: uint8; - var_1: uint8; - // var_2: uint; + var_0: uint; + var_1: uint; + var_2: uint; } // sizeof(BadAlignmentLarge) == 8 @@ -16,14 +16,17 @@ table OuterLarge { large: BadAlignmentLarge; } +table BadAlignmentRoot { + large: OuterLarge; + small: [BadAlignmentSmall]; +} + struct JustSmallStruct { var_0: uint8; var_1: uint8; } -table BadAlignmentRoot { - large: OuterLarge; - small: [BadAlignmentSmall]; +table SmallStructs { small_structs: [JustSmallStruct]; } From 54f6099ac5f33893f9392a20722027329d8fedd7 Mon Sep 17 00:00:00 2001 From: Pavlo Bashmakov <157482+bexcite@users.noreply.github.com> Date: Wed, 29 Mar 2023 09:13:38 -0700 Subject: [PATCH 04/10] Rare fix: remove comments --- tests/alignment_test.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/alignment_test.cpp b/tests/alignment_test.cpp index de1bfc326e9..a84a334c478 100644 --- a/tests/alignment_test.cpp +++ b/tests/alignment_test.cpp @@ -27,8 +27,13 @@ void AlignmentTest() { Verifier verifier(builder.GetBufferPointer(), builder.GetSize()); TEST_ASSERT(VerifyBadAlignmentRootBuffer(verifier)); + + // ============= Test Small Structs Vector misalignment ======== + builder.Clear(); + // creating 5 structs with 2 bytes each + // 10 bytes in total for Vector data is needed std::vector small_vector = { { 2, 1 }, { 3, 1 }, { 4, 1 }, { 5, 1 }, { 6, 1 } }; @@ -41,17 +46,12 @@ void AlignmentTest() { builder.GetSize()); TEST_ASSERT(verifier_small_structs.VerifyBuffer(nullptr)); + // Reading SmallStructs vector values back and compare with original auto root_msg = flatbuffers::GetRoot(builder.GetBufferPointer()); - std::cout << "msg.size = " << root_msg->small_structs()->size() << std::endl; TEST_EQ(root_msg->small_structs()->size(), small_vector.size()); - for (size_t i = 0; i < root_msg->small_structs()->size(); ++i) { - std::cout << "msg.small_structs[" << i - << "] = " << (int)root_msg->small_structs()->Get(i)->var_0() - << ":" << (int)root_msg->small_structs()->Get(i)->var_1() - << std::endl; TEST_EQ(root_msg->small_structs()->Get(i)->var_0(), small_vector[i].var_0()); TEST_EQ(root_msg->small_structs()->Get(i)->var_1(), From e65640bd48a611b1165219c5b00073a3da8df38e Mon Sep 17 00:00:00 2001 From: Pavlo Bashmakov <157482+bexcite@users.noreply.github.com> Date: Wed, 29 Mar 2023 09:24:19 -0700 Subject: [PATCH 05/10] Rare fix: updates --- tests/alignment_test.cpp | 9 ++++++--- tests/alignment_test.fbs | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/alignment_test.cpp b/tests/alignment_test.cpp index a84a334c478..f2ef3a9d763 100644 --- a/tests/alignment_test.cpp +++ b/tests/alignment_test.cpp @@ -3,12 +3,11 @@ #include "tests/alignment_test_generated.h" #include "flatbuffers/flatbuffer_builder.h" #include "test_assert.h" -#include namespace flatbuffers { namespace tests { -void AlignmentTest() { +void AlignmentTest() { FlatBufferBuilder builder; BadAlignmentLarge large; @@ -37,8 +36,12 @@ void AlignmentTest() { std::vector small_vector = { { 2, 1 }, { 3, 1 }, { 4, 1 }, { 5, 1 }, { 6, 1 } }; + // CreateVectorOfStructs is used in the generated CreateSmallStructsDirect() + // method, but we test it directly + Offset> small_structs_offset = + builder.CreateVectorOfStructs(small_vector); Offset small_structs_root = - CreateSmallStructsDirect(builder, &small_vector); + CreateSmallStructs(builder, small_structs_offset); builder.Finish(small_structs_root); diff --git a/tests/alignment_test.fbs b/tests/alignment_test.fbs index ff69337d65c..43597208600 100644 --- a/tests/alignment_test.fbs +++ b/tests/alignment_test.fbs @@ -21,6 +21,8 @@ table BadAlignmentRoot { small: [BadAlignmentSmall]; } +// sizeof(JustSmallStruct) == 2 +// alignof(JustSmallStruct) == 1 struct JustSmallStruct { var_0: uint8; var_1: uint8; From 934f75fc154a11dd2950215cc37ab6e9e55691bb Mon Sep 17 00:00:00 2001 From: Pavlo Bashmakov <157482+bexcite@users.noreply.github.com> Date: Wed, 29 Mar 2023 09:42:23 -0700 Subject: [PATCH 06/10] Rare fix: less --- tests/alignment_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/alignment_test.cpp b/tests/alignment_test.cpp index f2ef3a9d763..f18d87755d6 100644 --- a/tests/alignment_test.cpp +++ b/tests/alignment_test.cpp @@ -34,7 +34,7 @@ void AlignmentTest() { // creating 5 structs with 2 bytes each // 10 bytes in total for Vector data is needed std::vector small_vector = { - { 2, 1 }, { 3, 1 }, { 4, 1 }, { 5, 1 }, { 6, 1 } + { 2, 1 }, { 3, 1 }, { 4, 1 } }; // CreateVectorOfStructs is used in the generated CreateSmallStructsDirect() // method, but we test it directly From 8433c165ca83e3d8dbcbea127ba8ae3f40f72dbf Mon Sep 17 00:00:00 2001 From: Pavlo Bashmakov <157482+bexcite@users.noreply.github.com> Date: Thu, 27 Apr 2023 08:38:10 -0700 Subject: [PATCH 07/10] Rare fix: size_t switch to uoffset_t --- tests/alignment_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/alignment_test.cpp b/tests/alignment_test.cpp index f18d87755d6..bc99de95be1 100644 --- a/tests/alignment_test.cpp +++ b/tests/alignment_test.cpp @@ -54,7 +54,8 @@ void AlignmentTest() { flatbuffers::GetRoot(builder.GetBufferPointer()); TEST_EQ(root_msg->small_structs()->size(), small_vector.size()); - for (size_t i = 0; i < root_msg->small_structs()->size(); ++i) { + for (flatbuffers::uoffset_t i = 0; i < root_msg->small_structs()->size(); + ++i) { TEST_EQ(root_msg->small_structs()->Get(i)->var_0(), small_vector[i].var_0()); TEST_EQ(root_msg->small_structs()->Get(i)->var_1(), From 9a7d0ea2b962d1ce25d24a1b9316c33a2a1a9a13 Mon Sep 17 00:00:00 2001 From: Pavlo Bashmakov <157482+bexcite@users.noreply.github.com> Date: Wed, 17 May 2023 09:57:15 -0700 Subject: [PATCH 08/10] Rare fix: swap exp/val --- tests/alignment_test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/alignment_test.cpp b/tests/alignment_test.cpp index bc99de95be1..bf8fe53c143 100644 --- a/tests/alignment_test.cpp +++ b/tests/alignment_test.cpp @@ -56,10 +56,10 @@ void AlignmentTest() { TEST_EQ(root_msg->small_structs()->size(), small_vector.size()); for (flatbuffers::uoffset_t i = 0; i < root_msg->small_structs()->size(); ++i) { - TEST_EQ(root_msg->small_structs()->Get(i)->var_0(), - small_vector[i].var_0()); - TEST_EQ(root_msg->small_structs()->Get(i)->var_1(), - small_vector[i].var_1()); + TEST_EQ(small_vector[i].var_0(), + root_msg->small_structs()->Get(i)->var_0()); + TEST_EQ(small_vector[i].var_1(), + root_msg->small_structs()->Get(i)->var_1()); } } From f8a8a9d44f398f19c5dfca53800403107f2571de Mon Sep 17 00:00:00 2001 From: Pavlo Bashmakov <157482+bexcite@users.noreply.github.com> Date: Thu, 18 May 2023 09:50:17 -0700 Subject: [PATCH 09/10] Rare fix: add annotated before/after --- tests/alignment_test.cpp | 11 +++++++++- tests/alignment_test.fbs | 2 +- tests/alignment_test.json | 16 ++++++++++++++ tests/alignment_test_after_fix.afb | 31 ++++++++++++++++++++++++++++ tests/alignment_test_after_fix.bin | Bin 0 -> 32 bytes tests/alignment_test_before_fix.afb | 31 ++++++++++++++++++++++++++++ tests/alignment_test_before_fix.bin | Bin 0 -> 32 bytes 7 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 tests/alignment_test.json create mode 100644 tests/alignment_test_after_fix.afb create mode 100644 tests/alignment_test_after_fix.bin create mode 100644 tests/alignment_test_before_fix.afb create mode 100644 tests/alignment_test_before_fix.bin diff --git a/tests/alignment_test.cpp b/tests/alignment_test.cpp index bf8fe53c143..9534078424e 100644 --- a/tests/alignment_test.cpp +++ b/tests/alignment_test.cpp @@ -2,6 +2,7 @@ #include "tests/alignment_test_generated.h" #include "flatbuffers/flatbuffer_builder.h" +#include "flatbuffers/util.h" #include "test_assert.h" namespace flatbuffers { @@ -24,7 +25,7 @@ void AlignmentTest() { builder.Finish(root); Verifier verifier(builder.GetBufferPointer(), builder.GetSize()); - TEST_ASSERT(VerifyBadAlignmentRootBuffer(verifier)); + TEST_ASSERT(verifier.VerifyBuffer(nullptr)); // ============= Test Small Structs Vector misalignment ======== @@ -45,6 +46,14 @@ void AlignmentTest() { builder.Finish(small_structs_root); + // Save the binary that we later can annotate with `flatc --annotate` command + // NOTE: the conversion of the JSON data to --binary via `flatc --binary` + // command is not changed with that fix and was always producing the + // correct binary data. + // SaveFile("alignment_test_{before,after}_fix.bin", + // reinterpret_cast(builder.GetBufferPointer()), + // builder.GetSize(), true); + Verifier verifier_small_structs(builder.GetBufferPointer(), builder.GetSize()); TEST_ASSERT(verifier_small_structs.VerifyBuffer(nullptr)); diff --git a/tests/alignment_test.fbs b/tests/alignment_test.fbs index 43597208600..11f5eb79c72 100644 --- a/tests/alignment_test.fbs +++ b/tests/alignment_test.fbs @@ -32,4 +32,4 @@ table SmallStructs { small_structs: [JustSmallStruct]; } -root_type BadAlignmentRoot; +root_type SmallStructs; diff --git a/tests/alignment_test.json b/tests/alignment_test.json new file mode 100644 index 00000000000..7831d15e854 --- /dev/null +++ b/tests/alignment_test.json @@ -0,0 +1,16 @@ +{ + "small_structs": [ + { + "var_0": 2, + "var_1": 1 + }, + { + "var_0": 3, + "var_1": 1 + }, + { + "var_0": 4, + "var_1": 1 + } + ] +} \ No newline at end of file diff --git a/tests/alignment_test_after_fix.afb b/tests/alignment_test_after_fix.afb new file mode 100644 index 00000000000..b8312e0ed43 --- /dev/null +++ b/tests/alignment_test_after_fix.afb @@ -0,0 +1,31 @@ +// Annotated Flatbuffer Binary +// +// Schema file: alignment_test.fbs +// Binary file: alignment_test_after_fix.bin + +header: + +0x00 | 0C 00 00 00 | UOffset32 | 0x0000000C (12) Loc: 0x0C | offset to root table `SmallStructs` + +padding: + +0x04 | 00 00 | uint8_t[2] | .. | padding + +vtable (SmallStructs): + +0x06 | 06 00 | uint16_t | 0x0006 (6) | size of this vtable + +0x08 | 08 00 | uint16_t | 0x0008 (8) | size of referring table + +0x0A | 04 00 | VOffset16 | 0x0004 (4) | offset to field `small_structs` (id: 0) + +root_table (SmallStructs): + +0x0C | 06 00 00 00 | SOffset32 | 0x00000006 (6) Loc: 0x06 | offset to vtable + +0x10 | 04 00 00 00 | UOffset32 | 0x00000004 (4) Loc: 0x14 | offset to field `small_structs` (vector) + +vector (SmallStructs.small_structs): + +0x14 | 03 00 00 00 | uint32_t | 0x00000003 (3) | length of vector (# items) + +0x18 | 02 | uint8_t | 0x02 (2) | struct field `[0].var_0` of 'JustSmallStruct' (UByte) + +0x19 | 01 | uint8_t | 0x01 (1) | struct field `[0].var_1` of 'JustSmallStruct' (UByte) + +0x1A | 03 | uint8_t | 0x03 (3) | struct field `[1].var_0` of 'JustSmallStruct' (UByte) + +0x1B | 01 | uint8_t | 0x01 (1) | struct field `[1].var_1` of 'JustSmallStruct' (UByte) + +0x1C | 04 | uint8_t | 0x04 (4) | struct field `[2].var_0` of 'JustSmallStruct' (UByte) + +0x1D | 01 | uint8_t | 0x01 (1) | struct field `[2].var_1` of 'JustSmallStruct' (UByte) + +padding: + +0x1E | 00 00 | uint8_t[2] | .. | padding diff --git a/tests/alignment_test_after_fix.bin b/tests/alignment_test_after_fix.bin new file mode 100644 index 0000000000000000000000000000000000000000..e9c1ffd0892b912cee0aca60cfbc5285e921cb30 GIT binary patch literal 32 gcmd;K00A}z4h9w=$p9o77?^>WiIJI+g^_^)00eOWH~;_u literal 0 HcmV?d00001 diff --git a/tests/alignment_test_before_fix.afb b/tests/alignment_test_before_fix.afb new file mode 100644 index 00000000000..c2665310ae1 --- /dev/null +++ b/tests/alignment_test_before_fix.afb @@ -0,0 +1,31 @@ +// Annotated Flatbuffer Binary +// +// Schema file: alignment_test.fbs +// Binary file: alignment_test_before_fix.bin + +header: + +0x00 | 0C 00 00 00 | UOffset32 | 0x0000000C (12) Loc: 0x0C | offset to root table `SmallStructs` + +padding: + +0x04 | 00 00 | uint8_t[2] | .. | padding + +vtable (SmallStructs): + +0x06 | 06 00 | uint16_t | 0x0006 (6) | size of this vtable + +0x08 | 08 00 | uint16_t | 0x0008 (8) | size of referring table + +0x0A | 04 00 | VOffset16 | 0x0004 (4) | offset to field `small_structs` (id: 0) + +root_table (SmallStructs): + +0x0C | 06 00 00 00 | SOffset32 | 0x00000006 (6) Loc: 0x06 | offset to vtable + +0x10 | 04 00 00 00 | UOffset32 | 0x00000004 (4) Loc: 0x14 | offset to field `small_structs` (vector) + +vector (SmallStructs.small_structs): + +0x14 | 03 00 00 00 | uint32_t | 0x00000003 (3) | length of vector (# items) + +0x18 | 00 | uint8_t | 0x00 (0) | struct field `[0].var_0` of 'JustSmallStruct' (UByte) + +0x19 | 00 | uint8_t | 0x00 (0) | struct field `[0].var_1` of 'JustSmallStruct' (UByte) + +0x1A | 02 | uint8_t | 0x02 (2) | struct field `[1].var_0` of 'JustSmallStruct' (UByte) + +0x1B | 01 | uint8_t | 0x01 (1) | struct field `[1].var_1` of 'JustSmallStruct' (UByte) + +0x1C | 03 | uint8_t | 0x03 (3) | struct field `[2].var_0` of 'JustSmallStruct' (UByte) + +0x1D | 01 | uint8_t | 0x01 (1) | struct field `[2].var_1` of 'JustSmallStruct' (UByte) + +unknown (no known references): + +0x1E | 04 01 | ?uint8_t[2] | .. | WARN: could be corrupted padding region. diff --git a/tests/alignment_test_before_fix.bin b/tests/alignment_test_before_fix.bin new file mode 100644 index 0000000000000000000000000000000000000000..d914f1880dfbc5950fa376d54db0ab356b9987bb GIT binary patch literal 32 fcmd;K00A}z4h9w=$p9o77??pq3`~s7j4X@*1X%z$ literal 0 HcmV?d00001 From 2cf39f12c838f1c2f3c93bd1c0b370e542e0b455 Mon Sep 17 00:00:00 2001 From: Pavlo Bashmakov <157482+bexcite@users.noreply.github.com> Date: Thu, 18 May 2023 09:52:05 -0700 Subject: [PATCH 10/10] Rare fix: remove unnecessary changes --- tests/alignment_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/alignment_test.cpp b/tests/alignment_test.cpp index 9534078424e..1d981e10ed4 100644 --- a/tests/alignment_test.cpp +++ b/tests/alignment_test.cpp @@ -17,7 +17,7 @@ void AlignmentTest() { BadAlignmentSmall *small; Offset> small_offset = builder.CreateUninitializedVectorOfStructs(9, &small); - (void)small; // We do not have to write data to trigger the test failure + (void)small; // We do not have to write data to trigger the test failure Offset root = CreateBadAlignmentRoot(builder, outer_large, small_offset);