Clean up NULL object handling.

Currently, hwbinder only allows sending NULL binder
objects (eg HIDL interfaces); all other objects must
be non-null.

The readObject()/writeObject() methods would previously take
a nullMetadata argument, to indicate whether the object
should write meta-data for NULL objects in the objects
array. Not writing the object in the objects array means
the kernel won't touch it, which is fine in some situations
(eg NULL objects).

This handling was complex because traditionally binder used
the flat_binder_object for both binder objects and file
descriptors. This patch cleans up that handling; it writes
file descriptors as binder_fd_objects, and in general fixes
most cases where flat_binder_object was abused for file
descriptors.

File descriptors and file descriptor arrays always need to
be added into the objects array, because the kernel always
has some work to do.

Binder objects are not written into the objects array if the
binder object is nullptr.

Buffer objects are not written into the objects array if the
buffer pointer is nullptr. This allows us to pass compound
structures with embedded nullptr's in them, such as hidl_memory
with a nullptr hidl_handle (which was requested on b/34134129).

With this change, readBuffer() calls can now validly return nullptr;
previously we used nullptr to indicate errors, but this is now
no longer possible. Moreover, we want callers to be very explicit
in which situations they think nullptr return values are valid.

readBuffer() / readEmbeddedBuffer() will now return an
UNEXPECTED_NULL error if the value read was nullptr; two new
methods, readNullableBuffer() and readNullableEmbeddedBuffer(),
allow nullptr values to be returned.

Similarly for reading native handles.

Bug: 34134129
Test: mma, hidl_test
Change-Id: I3dfc6e8437ec6dc39c4c5e6dfac074f566902928
3 files changed