-
Notifications
You must be signed in to change notification settings - Fork 74
flatbuffers: Add opt-in buffer identification mechanism #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OliverHeilwagen
wants to merge
4
commits into
eclipse-score:main
Choose a base branch
from
etas-contrib:flatbuffers_common_buffer_identification
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f403172
flatbuffers: Add starlark rules for versioned buffer serialization
OliverHeilwagen b6049f5
flatbuffers: Add buffer version identification and verification
OliverHeilwagen de049c7
flatbuffers: Add buffer version identification and verification
OliverHeilwagen 2a9bf95
flatbuffers: Add starlark rules for versioned buffer serialization
OliverHeilwagen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
examples/integration/flatbuffers/config_usecase/demo_config_versioned_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2026 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Apache License Version 2.0 which is available at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| /// @file demo_config_versioned_test.cpp | ||
| /// @brief Demonstrates loading of a versioned FlatBuffer binary config, | ||
| /// verifying its version via score::flatbuffers::VerifyVersion before | ||
| /// accessing any data, and then reading the config fields. | ||
|
|
||
| #include "score/flatbuffers/buffer_version_info.hpp" | ||
| #include "score/flatbuffers/load_buffer.hpp" | ||
| #include "score/flatbuffers/version_reader.hpp" | ||
| // Generated C++ accessor header produced by generate_cpp() for demo_versioned.fbs | ||
| #include "flatbuffers/config_usecase/component_config_versioned.h" | ||
|
|
||
| #include "flatbuffers/verifier.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <string_view> | ||
|
|
||
| TEST(DemoConfigVersionedTest, VerifiesVersionThenLoadsAndReadsBuffer) | ||
| { | ||
| // ------------------------------------------------------------------------- | ||
| // Step 1: Load the binary FlatBuffer file from disk | ||
| // ------------------------------------------------------------------------- | ||
| const std::string_view bin_path = "flatbuffers/config_usecase/demo_config_versioned.bin"; | ||
| const auto buffer = score::flatbuffers::LoadBuffer(bin_path); | ||
| ASSERT_TRUE(buffer.has_value()) << buffer.error().ToString(); | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // Step 2: Verify buffer version before accessing any data | ||
| // | ||
| // The expected BufferVersionInfo must match: | ||
| // - file_identifier : "DEMO" (set in demo_versioned.fbs) | ||
| // - major_version : 1 (set in serialize_versioned_buffer rule) | ||
| // - minor_version : 0 (set in serialize_versioned_buffer rule) | ||
| // ------------------------------------------------------------------------- | ||
| const score::flatbuffers::BufferVersionInfo expected{"DEMO", 1U, 0U}; | ||
| // Free function variant is used for VerifyVersion. | ||
| // Use score::flatbuffers::VersionReader (implements IVersionReader) instead | ||
| // when the caller needs to inject or mock the reader. | ||
| const auto version_result = score::flatbuffers::VerifyVersion(buffer.value(), expected); | ||
| ASSERT_TRUE(version_result.has_value()) << version_result.error(); | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // Step 3: Verify structural integrity of the FlatBuffer | ||
| // ------------------------------------------------------------------------- | ||
| flatbuffers::Verifier verifier(buffer.value().data(), buffer.value().size()); | ||
| ASSERT_TRUE(my_component::demo::VerifyMyComponentConfigBuffer(verifier)) | ||
| << "FlatBuffer structural verification failed for '" << bin_path << "'"; | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
| // Step 4: Access config values via the generated APIs | ||
| // ------------------------------------------------------------------------- | ||
| const my_component::demo::MyComponentConfig* config = | ||
| my_component::demo::GetMyComponentConfig(buffer.value().data()); | ||
|
|
||
| ASSERT_NE(config, nullptr); | ||
| EXPECT_EQ(config->component_name()->str(), "demo_component"); | ||
| EXPECT_EQ(config->component_id(), 42U); | ||
| EXPECT_EQ(config->thread_pool_size(), 4U); | ||
| EXPECT_EQ(config->max_memory_mb(), 64U); | ||
|
|
||
| const my_component::demo::AdvancedSettings* adv = config->advanced_settings(); | ||
| ASSERT_NE(adv, nullptr); | ||
| EXPECT_EQ(adv->mode(), my_component::demo::OperationMode::PERIODIC); | ||
|
|
||
| ASSERT_NE(adv->allowed_priorities(), nullptr); | ||
| EXPECT_EQ(adv->allowed_priorities()->size(), 2U); | ||
| EXPECT_EQ(adv->allowed_priorities()->Get(0), 1U); | ||
| EXPECT_EQ(adv->allowed_priorities()->Get(1), 2U); | ||
|
|
||
| ASSERT_NE(adv->feature_flags(), nullptr); | ||
| ASSERT_EQ(adv->feature_flags()->size(), 1U); | ||
| const my_component::demo::FeatureFlag* flag = adv->feature_flags()->Get(0); | ||
| EXPECT_EQ(flag->name()->str(), "enable_logging"); | ||
| EXPECT_TRUE(flag->enabled()); | ||
| } |
85 changes: 85 additions & 0 deletions
85
examples/integration/flatbuffers/config_usecase/demo_versioned.fbs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /// Versioned variant of the demo component config schema. | ||
| /// | ||
| /// Follows the common buffer identification convention: | ||
| /// - includes buffer_version.fbs | ||
| /// - declares version_info:score.flatbuffers.BufferVersion as the FIRST field | ||
| /// of the root table (vtable slot 0) | ||
| /// | ||
| /// The binary buffer is produced by serialize_versioned_buffer, which injects | ||
| /// the major/minor version into the JSON data before calling flatc --binary. | ||
| /// At runtime, score::flatbuffers::VerifyVersion() reads the embedded version | ||
| /// and compares it against the caller-supplied BufferVersionInfo. | ||
| /// | ||
| /// Explicit field IDs (id: N) are used throughout this schema. | ||
| /// Benefits: field order in the .fbs source can be changed freely without | ||
| /// breaking binary compatibility with buffers produced by older schema versions, | ||
| /// because the vtable slot is determined by the id attribute, not by declaration | ||
| /// order. This makes schema evolution safer: new fields can be inserted anywhere | ||
| /// and deprecated fields can be removed without affecting existing binaries. | ||
|
|
||
| include "buffer_version.fbs"; | ||
|
|
||
| namespace my_component.demo; | ||
|
|
||
| /// Generic enum for component run modes | ||
| enum OperationMode:uint8 { | ||
| EVENT_DRIVEN = 0, | ||
| POLLING = 1, | ||
| PERIODIC = 2, | ||
| } | ||
|
|
||
| /// Union for flexible data types | ||
| union DataPayloadUnion { | ||
| StringData, | ||
| BinaryData | ||
| } | ||
|
|
||
| table MyComponentConfig { | ||
| /// MUST be field 0 – required by the universal buffer identification convention. | ||
| version_info:score.flatbuffers.BufferVersion (id: 0); | ||
|
|
||
| /// Component identification | ||
| component_name:string (required, id: 1); | ||
| component_id:uint32 (id: 2); // Implicit default is 0 | ||
|
|
||
| thread_pool_size:uint8 = 4 (id: 3); // Explicit default value | ||
| max_memory_mb:uint16 = 64 (id: 4); | ||
|
|
||
| /// Nested table | ||
| advanced_settings:AdvancedSettings (required, id: 5); | ||
| } | ||
|
|
||
| table AdvancedSettings { | ||
| /// Enum example: operational mode (defaults to first enum value: EVENT_DRIVEN) | ||
| mode:OperationMode (id: 0); | ||
|
|
||
| /// Vector examples | ||
| allowed_priorities:[uint8] (id: 1); // Vector of primitives | ||
| feature_flags:[FeatureFlag] (id: 2); // Vector of tables | ||
|
|
||
| /// Union example: flexible data payload | ||
| /// Note: unions occupy two consecutive IDs – one for the type discriminator | ||
| /// and one for the value offset. data_type takes id: 3, data takes id: 4. | ||
| data:DataPayloadUnion (id: 4); | ||
| } | ||
|
|
||
| /// Table used in vector | ||
| table FeatureFlag { | ||
| name:string (required, id: 0); | ||
| enabled:bool = false (id: 1); | ||
| } | ||
|
|
||
| table StringData { | ||
| value:string (id: 0); | ||
| } | ||
|
|
||
| table BinaryData { | ||
| value:[uint8] (id: 0); | ||
| } | ||
|
|
||
| // Root configuration object | ||
| root_type MyComponentConfig; | ||
|
|
||
| // Component specific file identifier – distinct from the unversioned "DEMO" | ||
| // so that VerifyVersion can distinguish the two buffer formats. | ||
| file_identifier "DEMO"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@4og
This was found by the black-box testing.
It is necessary for the inject_buffer_version (py_binary) which will only be build for the host environment.
The alternative to just export the python file and expect a python installation on the host, is from my point of view not valid.
Atm i don't have concerns with switching rules_python to non dev, it should behave like the other rules_*.
The consuming module is allowed to override the toolchain (python use_extension).