Skip to content
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

use ALIGN for Push::alignment in struct types #8398

Merged
merged 2 commits into from
Oct 28, 2024

Conversation

bkietz
Copy link
Contributor

@bkietz bkietz commented Sep 20, 2024

This PR adds an override of the default Push::alignment(), which is incorrectly 1 for structs because they are byte arrays and this doesn't accurately reflect the type's alignment requirements.

fixes #8150

Copy link

google-cla bot commented Sep 20, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@bkietz
Copy link
Contributor Author

bkietz commented Sep 20, 2024

@evgenyx00 @tustvold

Copy link

@alamb alamb left a comment

Choose a reason for hiding this comment

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

I wonder if we should add a test to this PR to make sure there are no regressions

@bkietz
Copy link
Contributor Author

bkietz commented Sep 25, 2024

I was not sure where to add such a test, but I'd be happy to. I opened apache/arrow-rs#6426 since that seemed like a good way to check for regressions (I didn't see any). @aardappel @CasperN any recommendations?

@aardappel
Copy link
Collaborator

@bkietz yes we generally should have tests :) Not too familiar with the Rust tests though, have a look.

@bkietz bkietz force-pushed the push-alignment-for-structs branch 2 times, most recently from 0b25b84 to 7d7ba5e Compare October 4, 2024 14:39
@bkietz bkietz requested a review from alamb October 4, 2024 14:39
@bkietz
Copy link
Contributor Author

bkietz commented Oct 4, 2024

I've regenerated and added a test.

  • struct_vec3_is_written_with_correct_alignment_in_table did not catch this bug before; adding a struct to a Table also goes through the Push trait. I could extend it (for example using a loop over multiple preceding strings to ensure at least one iteration will require padding for alignment)
  • The pull request template suggests that building should also regenerate. However, AFAICT cmake will only regenerate for C++. Perhaps cmake should instead invoke generate_code.py?
  • Could a reviewer please approve the CI workflows so we can see if I've missed anything?

Copy link

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you @bkietz

I am not familiar with this codebase, but I did verify that the test you added fails without the code in this PR

To test I did:

cd flatbuffers/tests/rust_usage_test
cargo test --test integration_test

The test fails like this without the PR

---- flatbuffers_tests::push_impls::push_u8_generated_struct_alignment stdout ----
thread 'flatbuffers_tests::push_impls::push_u8_generated_struct_alignment' panicked at tests/integration_test.rs:2360:9:
assertion `left == right` failed
  left: [10, 0, 20, 0, 0, 1]
 right: [10, 0, 20, 0, 1]

Seem like an improvement to me (the 1u8 is now aligned at a byte boundary rather than a u16 boundary)

@bkietz
Copy link
Contributor Author

bkietz commented Oct 10, 2024

@dbaileychess could you review and/or approve the CI workflows?

@bkietz
Copy link
Contributor Author

bkietz commented Oct 11, 2024

@aardappel whom can we ask to review this?

@aardappel
Copy link
Collaborator

I'd say either @CasperN or @dbaileychess

@bkietz
Copy link
Contributor Author

bkietz commented Oct 13, 2024

Thanks!

@bkietz
Copy link
Contributor Author

bkietz commented Oct 17, 2024

@CasperN could you review please?

@alamb
Copy link

alamb commented Oct 22, 2024

Any update here @aardappel @CasperN or @dbaileychess ? We are waiting on this fix to enable some cross implementation testing in Apache Arrow

@aardappel
Copy link
Collaborator

This looks fine to me and can probably be merged, but I am not qualified to review Rust really.. is going to need someone else to review.

@RuidongWu
Copy link

RuidongWu commented Oct 25, 2024

Well, I have also encountered a similar aligned problem of struct. The generated data from rust cannot be correctly verified by C/C++, which reports unaligend data error.
If this struct is filled into a table, the generated data may be unaligned in binary data when using rust builder.
such as:

struct Something {
value: uint64;
}

table Anything {
name: string;
}

table OneTable {
names: [Anything];
values: Something;
}

@alamb
Copy link

alamb commented Oct 25, 2024

Well, I have also encountered a similar aligned problem of struct. The generated data from rust cannot be correctly verified by C/C++, which reports unaligend data error.

Hi @RuidongWu - thank you for the report. Does this PR fix the problem you are seeing? Or are you reporting a different problem?

@RuidongWu
Copy link

RuidongWu commented Oct 28, 2024

@alamb Thans for your reply. Yes, this PR solves the unaligned problem of data-generated by rust. The key change is the file of src/idl_gen_rust.cpp. Thanks for the contribution of @bkietz

@aardappel
Copy link
Collaborator

Ok, I will merge this for now, any Rustaceans with complaints please follow up.

@aardappel aardappel merged commit 49061f8 into google:master Oct 28, 2024
49 checks passed
@alamb
Copy link

alamb commented Oct 28, 2024

Thank you so much @aardappel 🙏

jochenparm pushed a commit to jochenparm/flatbuffers that referenced this pull request Oct 29, 2024
* use ALIGN for Push::alignment in struct types

* regenerate and add a test for struct alignment
jochenparm pushed a commit to jochenparm/flatbuffers that referenced this pull request Oct 29, 2024
* use ALIGN for Push::alignment in struct types

* regenerate and add a test for struct alignment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ codegen Involving generating code from schema rust
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rust Push::alignment Incorrect for Structs
6 participants