LLCPP/Wire tables becoming immutable

26 views
Skip to first unread message

Ian McKellar

unread,
Mar 17, 2022, 8:31:44 PM3/17/22
to fidl-dev, anno...@fuchsia.dev
If you don't use the C++ low-level / wire C++ bindings there's nothing to see here...

Howdy,

As part of our work to stabilize the FIDL C++ Wire-style bindings (née LLCPP) we did an API review. One of the big pain points that came up was the interaction of tables, arenas and memory management. It's been very easy to write code that looks perfectly good to both C++ developers and compilers but that would crash (or worse have mostly silent memory bugs) at runtime.

To address this we've changed the API of FIDL tables from being a mutable type that must be correctly initialized and then configured carefully to being immutable where the only way to create them is through a builder pattern. The machine code produced by the C++ compiler will be roughly equivalent but it will be able to catch many more mistakes at compile time.

Along the way we managed to squeeze in some ergonomic improvements, like automatically passing an arena along to member constructors that need them.

So for example code that was:
fidl::Arena arena;
fuchsia_examples::wire::User user;
user.Allocate(allocator);
user.set_age(30);
user.set_name(allocator, allocator, "jdoe");

becomes:
fidl::Arena arena;
auto user = fuchsia_examples::wire::User::Builder(arena)
   .age(30)
   .name("joe")
   .Build();

If you need fine-grained control over table frame or member allocation you can still do that but for most users this will be a lot simpler and safer.

This change lands in https://fxrev.dev/652348 and updates some test code but the old API is available behind the config //build/cpp:fidl-wire-deprecated-mutable-tables but using that is allow-listed. New code should use the new API. I will update some of the code in-tree but I'm hoping code owners will take this chance to make their code simpler and safer.

Let us know if there are use cases we aren't catering for. We think it's in good shape but the old API was flexible enough that we might have missed something important.

Ian

Suraj Malhotra

unread,
Mar 17, 2022, 8:53:45 PM3/17/22
to Ian McKellar, fidl-dev, anno...@fuchsia.dev
This is amazing, thank you for this much needed improvement!

--
All posts must follow the Fuchsia Code of Conduct https://fuchsia.dev/fuchsia-src/CODE_OF_CONDUCT or may be removed.
---
You received this message because you are subscribed to the Google Groups "fidl-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fidl-dev+u...@fuchsia.dev.
To view this discussion on the web visit https://groups.google.com/a/fuchsia.dev/d/msgid/fidl-dev/CA%2BYAivhkh8_jRPdbNrhep5kB2xgFDKKCWsPPMDRELKN6bUC_qw%40mail.gmail.com.

Adam Barth

unread,
Mar 18, 2022, 4:16:01 AM3/18/22
to Suraj Malhotra, Ian McKellar, fidl-dev, anno...@fuchsia.dev

Tom Bergan

unread,
Mar 18, 2022, 1:02:16 PM3/18/22
to Adam Barth, Suraj Malhotra, Ian McKellar, fidl-dev, anno...@fuchsia.dev
Reply all
Reply to author
Forward
0 new messages