Differentiate `inner` pointers representing `None` and `Some(ZST)`
authorMatt Corallo <git@bluematt.me>
Wed, 18 Aug 2021 21:51:28 +0000 (21:51 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 19 Aug 2021 17:03:33 +0000 (17:03 +0000)
commit9a960fb8c9ab355628b08a4c43fe1241caa2fa77
tree7e4604b96c97064acc6c926f40ab89781b203d68
parent036da10cc98c0d3ca093a58a9cd9f20dcba832c7
Differentiate `inner` pointers representing `None` and `Some(ZST)`

For zero-sized-types, rust `Box::into_inner(Box::new(ZST {}))`
returns `1usize as *mut ZST`, which confuses our Java bindings
which check for `None` by checking if `inner < 1024`. While we
could convert the Java bindings to check for `inner == 0`, the
magic value for ZST pointers is not, to my knowledge, an ABI
guarantee Rust provides.

Instead, we add an offset to the `inner` pointers to push them
past the zero page for ZSTs, taking this opportunity to clean up
some of our pointer conversion and push them through a common set
of utility functions.

We also add testing infrastructure to add similar offsets to
non-ZSTs to get good test coverage of the offset addition-removal,
though Rust should largely be ignoring pointer values for ZSTs
anyway so there should be little risk in anything going wrong here.
c-bindings-gen/src/blocks.rs
c-bindings-gen/src/main.rs
c-bindings-gen/src/types.rs
lightning-c-bindings/src/c_types/mod.rs