Skip to content

Commit c88a8ce

Browse files
committed
revert reader changes, handle align better
1 parent 5c9b817 commit c88a8ce

File tree

6 files changed

+13
-86
lines changed

6 files changed

+13
-86
lines changed

CMakeLists.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ option (FLATCC_TRACE_VERIFY
7979

8080
# Some producers allow empty vectors to be misaligned.
8181
# The following setting will cause the verifier to check for an
82-
# empty vector before checking alignment. This option will also
83-
# ensure accesses to a vector field never materializes misaligned
84-
# pointers.
85-
#
86-
# NOTE: enabling this means empty vectors will not have distinct
87-
# pointer identity.
82+
# empty vector before checking alignment of the vector's elements.
8883
option (FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS
8984
"don't fail verification if empty vectors are misaligned" OFF)
9085

include/flatcc/flatcc_flatbuffers.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,6 @@ extern "C" {
4848
#define FLATBUFFERS_WRAP_NAMESPACE(ns, x) ns ## _ ## x
4949
#endif
5050

51-
#include <stddef.h>
52-
53-
/*
54-
* Produce a pointer with maximum alignment to use as
55-
* a placeholder when FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS
56-
* is specified.
57-
*/
58-
static inline const void* max_aligned_ptr(void)
59-
{
60-
#ifdef __cplusplus
61-
static const max_align_t m{};
62-
#else
63-
static const max_align_t m;
64-
#endif
65-
return &m;
66-
}
67-
6851
#endif /* flatcc_flatbuffers_defined */
6952

7053
#ifdef __cplusplus

include/flatcc/flatcc_rtconfig.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,7 @@ extern "C" {
6666
/*
6767
* Some producers allow empty vectors to be misaligned.
6868
* The following setting will cause the verifier to check for an
69-
* empty vector before checking alignment. This option will also
70-
* ensure accesses to a vector field never materializes misaligned
71-
* pointers.
72-
*
73-
* NOTE: enabling this means empty vectors will not have distinct
74-
* pointer identity.
69+
* empty vector before checking alignment of the vector's elements.
7570
*/
7671
#if !defined(FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS)
7772
#define FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS 0

src/compiler/codegen_c_reader.c

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include "codegen_c.h"
66
#include "codegen_c_sort.h"
77

8-
#include "flatcc/flatcc_rtconfig.h"
9-
108
static inline int match_kw_identifier(fb_symbol_t *sym)
119
{
1210
return (sym->ident->len == 10 &&
@@ -620,34 +618,10 @@ static void gen_helpers(fb_output_t *out)
620618
" return 0;\\\n"
621619
"}\n",
622620
nsc, nsc, nsc, nsc, nsc);
623-
#if FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS
624-
fprintf(out->fp,
625-
"#include <stdio.h>\n"
626-
"#define __%svector_field(T, ID, t, r)\\\n"
627-
"{\\\n"
628-
" T out__tmp;\\\n"
629-
" __%sread_vt(ID, offset__tmp, t)\\\n"
630-
" if (offset__tmp) {\\\n"
631-
" offset__tmp += __%suoffset_read_from_pe((uint8_t *)(t) + offset__tmp);\\\n"
632-
" if (__%suoffset_read_from_pe((uint8_t *)(t) + offset__tmp) == 0) {\\\n"
633-
" printf(\"Falling back for empty vector...\");\\\n"
634-
" return (T)max_aligned_ptr();\\\n"
635-
" }\\\n"
636-
" offset__tmp += sizeof(%suoffset_t);\\\n"
637-
" return (T)((uint8_t *)(t) + offset__tmp);\\\n"
638-
" }\\\n"
639-
" FLATCC_ASSERT(!(r) && \"required field missing\");\\\n"
640-
" return 0;\\\n"
641-
"}\n",
642-
nsc, nsc, nsc, nsc, nsc);
643-
#else
644-
fprintf(out->fp,
645-
"#define __%svector_field(T, ID, t, r) __%soffset_field(T, ID, t, r, sizeof(%suoffset_t))\n",
646-
nsc, nsc, nsc);
647-
#endif
648621
fprintf(out->fp,
622+
"#define __%svector_field(T, ID, t, r) __%soffset_field(T, ID, t, r, sizeof(%suoffset_t))\n"
649623
"#define __%stable_field(T, ID, t, r) __%soffset_field(T, ID, t, r, 0)\n",
650-
nsc, nsc);
624+
nsc, nsc, nsc, nsc, nsc);
651625
fprintf(out->fp,
652626
"#define __%sdefine_struct_field(ID, N, NK, T, r)\\\n"
653627
"static inline T N ## _ ## NK ## _get(N ## _table_t t__tmp)\\\n"

src/runtime/verifier.c

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,6 @@ static inline int check_header(uoffset_t end, uoffset_t base, uoffset_t offset)
135135
return k > base && k + offset_size <= end && !(k & (offset_size - 1));
136136
}
137137

138-
static inline int check_aligned_header(uoffset_t end, uoffset_t base, uoffset_t offset, uint16_t align)
139-
{
140-
uoffset_t k = base + offset;
141-
142-
if (uoffset_size <= voffset_size && k + offset_size < k) {
143-
return 0;
144-
}
145-
/* Alignment refers to element 0 and header must also be aligned. */
146-
align = align < uoffset_size ? uoffset_size : align;
147-
148-
/* Note to self: the builder can also use the mask OR trick to propagate `min_align`. */
149-
return k > base && k + offset_size <= end && !((k + offset_size) & ((offset_size - 1) | (align - 1u)));
150-
}
151-
152138
static inline int verify_struct(uoffset_t end, uoffset_t base, uoffset_t offset, uoffset_t size, uint16_t align)
153139
{
154140
/* Structs can have zero size so `end` is a valid value. */
@@ -276,22 +262,18 @@ static inline int verify_string(const void *buf, uoffset_t end, uoffset_t base,
276262
*/
277263
static inline int verify_vector(const void *buf, uoffset_t end, uoffset_t base, uoffset_t offset, uoffset_t elem_size, uint16_t align, uoffset_t max_count)
278264
{
279-
uoffset_t n;
265+
verify(check_header(end, base, offset), flatcc_verify_error_vector_header_out_of_range_or_unaligned);
266+
base += offset;
267+
268+
uoffset_t n = read_uoffset(buf, base);
269+
base += offset_size;
280270

281271
#if FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS
282-
base += offset;
283-
verify(end - base >= sizeof(n), flatcc_verify_error_vector_header_out_of_range_or_unaligned);
284-
n = read_uoffset(buf, base);
285-
if (n == 0) {
286-
return flatcc_verify_ok;
287-
}
288-
verify(check_aligned_header(end, base - offset, offset, align), flatcc_verify_error_vector_header_out_of_range_or_unaligned);
289-
#else
290-
verify(check_aligned_header(end, base, offset, align), flatcc_verify_error_vector_header_out_of_range_or_unaligned);
291-
base += offset;
292-
n = read_uoffset(buf, base);
272+
align = n == 0 ? uoffset_size : align;
293273
#endif
294-
base += offset_size;
274+
align = align < uoffset_size ? uoffset_size : align;
275+
verify(!(base & (align - 1u)),flatcc_verify_error_vector_header_out_of_range_or_unaligned);
276+
295277
/* `n * elem_size` can overflow uncontrollably otherwise. */
296278
verify(n <= max_count, flatcc_verify_error_vector_count_exceeds_representable_vector_size);
297279
verify(end - base >= n * elem_size, flatcc_verify_error_vector_out_of_range);

test/load_test/load_test.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ int main(int argc, char *argv[])
136136
create_root_monster(B);
137137
flatcc_builder_copy_buffer(B, buffer, size);
138138
mon = ns(Monster_as_root(buffer));
139-
char const *name = ns(Monster_name(mon));
140-
printf("Name was %s\n", name ? name : "<NULL>");
141139
ret |= strcmp(ns(Monster_name(mon)), "root_monster");
142140
assert(ret == 0);
143141
mv = ns(Monster_testarrayoftables(mon));

0 commit comments

Comments
 (0)