Skip to content

Commit

Permalink
revert reader changes, handle align better
Browse files Browse the repository at this point in the history
  • Loading branch information
bkietz committed Aug 17, 2024
1 parent 5c9b817 commit c88a8ce
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 86 deletions.
7 changes: 1 addition & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,7 @@ option (FLATCC_TRACE_VERIFY

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

Expand Down
17 changes: 0 additions & 17 deletions include/flatcc/flatcc_flatbuffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,6 @@ extern "C" {
#define FLATBUFFERS_WRAP_NAMESPACE(ns, x) ns ## _ ## x
#endif

#include <stddef.h>

/*
* Produce a pointer with maximum alignment to use as
* a placeholder when FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS
* is specified.
*/
static inline const void* max_aligned_ptr(void)
{
#ifdef __cplusplus
static const max_align_t m{};
#else
static const max_align_t m;
#endif
return &m;
}

#endif /* flatcc_flatbuffers_defined */

#ifdef __cplusplus
Expand Down
7 changes: 1 addition & 6 deletions include/flatcc/flatcc_rtconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,7 @@ extern "C" {
/*
* Some producers allow empty vectors to be misaligned.
* The following setting will cause the verifier to check for an
* empty vector before checking alignment. This option will also
* ensure accesses to a vector field never materializes misaligned
* pointers.
*
* NOTE: enabling this means empty vectors will not have distinct
* pointer identity.
* empty vector before checking alignment of the vector's elements.
*/
#if !defined(FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS)
#define FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS 0
Expand Down
30 changes: 2 additions & 28 deletions src/compiler/codegen_c_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "codegen_c.h"
#include "codegen_c_sort.h"

#include "flatcc/flatcc_rtconfig.h"

static inline int match_kw_identifier(fb_symbol_t *sym)
{
return (sym->ident->len == 10 &&
Expand Down Expand Up @@ -620,34 +618,10 @@ static void gen_helpers(fb_output_t *out)
" return 0;\\\n"
"}\n",
nsc, nsc, nsc, nsc, nsc);
#if FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS
fprintf(out->fp,
"#include <stdio.h>\n"
"#define __%svector_field(T, ID, t, r)\\\n"
"{\\\n"
" T out__tmp;\\\n"
" __%sread_vt(ID, offset__tmp, t)\\\n"
" if (offset__tmp) {\\\n"
" offset__tmp += __%suoffset_read_from_pe((uint8_t *)(t) + offset__tmp);\\\n"
" if (__%suoffset_read_from_pe((uint8_t *)(t) + offset__tmp) == 0) {\\\n"
" printf(\"Falling back for empty vector...\");\\\n"
" return (T)max_aligned_ptr();\\\n"
" }\\\n"
" offset__tmp += sizeof(%suoffset_t);\\\n"
" return (T)((uint8_t *)(t) + offset__tmp);\\\n"
" }\\\n"
" FLATCC_ASSERT(!(r) && \"required field missing\");\\\n"
" return 0;\\\n"
"}\n",
nsc, nsc, nsc, nsc, nsc);
#else
fprintf(out->fp,
"#define __%svector_field(T, ID, t, r) __%soffset_field(T, ID, t, r, sizeof(%suoffset_t))\n",
nsc, nsc, nsc);
#endif
fprintf(out->fp,
"#define __%svector_field(T, ID, t, r) __%soffset_field(T, ID, t, r, sizeof(%suoffset_t))\n"
"#define __%stable_field(T, ID, t, r) __%soffset_field(T, ID, t, r, 0)\n",
nsc, nsc);
nsc, nsc, nsc, nsc, nsc);
fprintf(out->fp,
"#define __%sdefine_struct_field(ID, N, NK, T, r)\\\n"
"static inline T N ## _ ## NK ## _get(N ## _table_t t__tmp)\\\n"
Expand Down
36 changes: 9 additions & 27 deletions src/runtime/verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,6 @@ static inline int check_header(uoffset_t end, uoffset_t base, uoffset_t offset)
return k > base && k + offset_size <= end && !(k & (offset_size - 1));
}

static inline int check_aligned_header(uoffset_t end, uoffset_t base, uoffset_t offset, uint16_t align)
{
uoffset_t k = base + offset;

if (uoffset_size <= voffset_size && k + offset_size < k) {
return 0;
}
/* Alignment refers to element 0 and header must also be aligned. */
align = align < uoffset_size ? uoffset_size : align;

/* Note to self: the builder can also use the mask OR trick to propagate `min_align`. */
return k > base && k + offset_size <= end && !((k + offset_size) & ((offset_size - 1) | (align - 1u)));
}

static inline int verify_struct(uoffset_t end, uoffset_t base, uoffset_t offset, uoffset_t size, uint16_t align)
{
/* Structs can have zero size so `end` is a valid value. */
Expand Down Expand Up @@ -276,22 +262,18 @@ static inline int verify_string(const void *buf, uoffset_t end, uoffset_t base,
*/
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)
{
uoffset_t n;
verify(check_header(end, base, offset), flatcc_verify_error_vector_header_out_of_range_or_unaligned);
base += offset;

uoffset_t n = read_uoffset(buf, base);
base += offset_size;

#if FLATCC_TOLERATE_MISALIGNED_EMPTY_VECTORS
base += offset;
verify(end - base >= sizeof(n), flatcc_verify_error_vector_header_out_of_range_or_unaligned);
n = read_uoffset(buf, base);
if (n == 0) {
return flatcc_verify_ok;
}
verify(check_aligned_header(end, base - offset, offset, align), flatcc_verify_error_vector_header_out_of_range_or_unaligned);
#else
verify(check_aligned_header(end, base, offset, align), flatcc_verify_error_vector_header_out_of_range_or_unaligned);
base += offset;
n = read_uoffset(buf, base);
align = n == 0 ? uoffset_size : align;
#endif
base += offset_size;
align = align < uoffset_size ? uoffset_size : align;
verify(!(base & (align - 1u)),flatcc_verify_error_vector_header_out_of_range_or_unaligned);

/* `n * elem_size` can overflow uncontrollably otherwise. */
verify(n <= max_count, flatcc_verify_error_vector_count_exceeds_representable_vector_size);
verify(end - base >= n * elem_size, flatcc_verify_error_vector_out_of_range);
Expand Down
2 changes: 0 additions & 2 deletions test/load_test/load_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ int main(int argc, char *argv[])
create_root_monster(B);
flatcc_builder_copy_buffer(B, buffer, size);
mon = ns(Monster_as_root(buffer));
char const *name = ns(Monster_name(mon));
printf("Name was %s\n", name ? name : "<NULL>");
ret |= strcmp(ns(Monster_name(mon)), "root_monster");
assert(ret == 0);
mv = ns(Monster_testarrayoftables(mon));
Expand Down

0 comments on commit c88a8ce

Please sign in to comment.