Skip to content

Commit

Permalink
Bug report for issue dvidelabs#179 (vector alignment issues).
Browse files Browse the repository at this point in the history
  • Loading branch information
benvanik committed Apr 16, 2021
1 parent e327f24 commit 017023d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
6 changes: 6 additions & 0 deletions samples/bugreport/eclectic.fbs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
namespace Eclectic;

table Buffer {
data:[uint8];
}

enum Fruit : byte { Banana = -1, Orange = 42 }
table FooBar {
meal : Fruit = Banana;
density : long (deprecated);
say : string;
height : short;

buffers:[Buffer];
}
file_identifier "NOOB";
root_type FooBar;
32 changes: 28 additions & 4 deletions samples/bugreport/myissue.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "build/myissue_generated.h"
#include "flatcc/support/hexdump.h"

#include <stdio.h>

int main(int argc, char *argv[])
{
int ret;
Expand All @@ -15,16 +17,38 @@ int main(int argc, char *argv[])
B = &builder;
flatcc_builder_init(B);

#define BUFFER_COUNT 5
const int sizes[BUFFER_COUNT] = {
1,
16,
22,
55,
123,
};
Eclectic_Buffer_ref_t buffer_refs[BUFFER_COUNT];
for (int i = 0; i < BUFFER_COUNT; ++i) {
Eclectic_Buffer_start(B);
flatcc_builder_start_vector(B, 1, /*align=*/16, FLATBUFFERS_COUNT_MAX(1));
uint8_t* p = flatcc_builder_extend_vector(B, sizes[i]);
memset(p, 'x', sizes[i]);
flatcc_builder_ref_t data = flatcc_builder_end_vector(B);
Eclectic_Buffer_data_add(B, data);
buffer_refs[i] = Eclectic_Buffer_end(B);
}
flatcc_builder_ref_t buffers_vec = flatcc_builder_create_offset_vector(B, buffer_refs, BUFFER_COUNT);

Eclectic_FooBar_start_as_root(B);
Eclectic_FooBar_say_create_str(B, "hello");
Eclectic_FooBar_meal_add(B, Eclectic_Fruit_Orange);
Eclectic_FooBar_height_add(B, -8000);
Eclectic_FooBar_buffers_add(B, buffers_vec);
Eclectic_FooBar_end_as_root(B);
buf = flatcc_builder_get_direct_buffer(B, &size);
#if defined(PROVOKE_ERROR) || 0
/* Provoke error for testing. */
((char*)buf)[0] = 42;
#endif

FILE* f = fopen("output.bin", "wb");
fwrite(buf,1, size, f);
fclose(f);

ret = Eclectic_FooBar_verify_as_root(buf, size);
if (ret) {
hexdump("Eclectic.FooBar buffer for myissue", buf, size, stdout);
Expand Down

2 comments on commit 017023d

@mikkelfj
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please state the result of the run - does the verifier complain?

Note that tables are not structs. A vector of tables is a vector of 4 byte offsets to individually placed tables. A table is aligned to 4 bytes (but can contain payload with padding and higher alignment). A byte vector in a table is stored as a 4 byte offset the vector. The vector itself would be 4 byte aligned due to the size prefix field of the vector, but otherwise only be required to align to 1 byte.

If you want to test large aligment, use a struct instead of table. A vector of structs will store the structs inline instead of as offsets, and the vectors first element should be aligned to the size of the struct.

@mikkelfj
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm probably missing something here. The low-level builder calls.

Please sign in to comment.