# Symmetric ABI
Christof Petig, ~~July~~ October 2024
Updates: highlighted by 💡
---
## Motivation
- Combine C++ and Rust with `Result`, `Option`,
`String` and `Vec`, also `Stream` and `Future`
- More details in presentation at
https://bytecodealliance.zulipchat.com/#narrow/stream/438936-SIG-Embedded/topic/Inaugural.20Meeting/near/445576299
---
## Side constraints
- Avoid dynamic memory allocations
for Functional Safety compliance
- Minimize copying (overhead)
---
# Native compilation
- Use `wit-bindgen` for language bindings
- Modify binding to generate mangled symbols
e.g. `autosarX3AaraX2FcoreX00initialize`
for `autosar:ara/core` . `initialize`
- Normal component when compiled to wasm32
---
## Drawbacks
- Needs host side mesh to connect components
- Duplicate Symbols complicate linking
- `wasm32` vs `x86_64` pointer size
---
## Can we omit the need for a mesh?
- 🤔
- ABI would need to become symmetric
- Imported functions args view,
return owned
- Exported functions args owned,
return view + `cabi_post_`
- 💡 Second plan
- Use import ABI also for exports
- Extend handles to pointer size
---
### Resources
- Use the imported ABI also for exported resources
- `resource-drop` replaces `dtor`
- `resource-new`, `resource-rep` hidden
- ~~Always pass `id`, never `rep`~~
- 💡 make `rep` and `id` identical
---
### Drawbacks
- API / ABI mismatch mandates unnecessary
allocations in the generated code
- Solution: ~~Caller provided buffers?~~
💡 New, [simplified](https://github.com/cpetig/wit-bindgen/blob/main/crates/cpp/DESIGN.md), C++ API
- 💡~~Result ownership is too complicated~~
~~- Vec+String view only,
`own<resource>` is passed~~
---
### Example code
https://github.com/cpetig/wit-bindgen/tree/main/crates/cpp/tests/meshless_strings
https://github.com/cpetig/wit-bindgen/tree/main/crates/cpp/tests/meshless_resources
---
### 💡 Unit tests
https://github.com/cpetig/wit-bindgen/tree/main/crates/cpp/tests/symmetric_tests
modified versions of wit-bindgen runtime tests
---
## Can also provide
- Stable Rust to Rust binary ABI
for native plugin architectures
- Direct linking of components into a
single module or executable
---
## Future plans
- 💡~~Caller provided buffers~~
~~[Lazy value lowering #383](https://github.com/WebAssembly/component-model/issues/383)~~
~~towards zero allocations per call~~
- ~~Still working on `heapless::String/Vec` API~~
- [Flat data types](https://github.com/WebAssembly/component-model/issues/398)
- Integrate with Joel's async prototype
- zero allocations become tough
---
### 💡 Flat data types
- Another ABI variant 🤦 or canonical option
- Works well with zero copy, shared memory, data broadcasting
- Idea from [iceoryx2](https://github.com/eclipse-iceoryx/iceoryx2)
- Enables also network transmission and disk storage
---
### 💡 Embedded specific
- Migration path from native to wasm
- Mixing native and wasm possible
- No distinction between host and guest
---
## 💡 Outlook
- New [BuildTargets.md](https://github.com/WebAssembly/component-model/pull/378/files) (`cm32p2|` prefix)
- Works well for AUTOSAR demo
- Streams and futures very important
{"title":"Symmetric ABI","breaks":true,"description":"View the slide with \"Slide Mode\".","contributors":"[{\"id\":\"de2fe656-f960-4cb7-bf10-766aa1bd039f\",\"add\":3452,\"del\":2481}]"}