summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorSteven Moreland <smoreland@google.com>2019-04-18 12:54:56 -0700
committerandroid-build-team Robot <android-build-team-robot@google.com>2019-05-14 04:58:21 +0000
commitf265872ab865a8cb6a423b0be8e6db019d87267d (patch)
tree5e4a71ff0a624f771c2541d0595821a90982b7c0 /base
parente9bbc22db47c26e3f97813dedc1ebdc919c70b47 (diff)
downloadlibhidl-f265872ab865a8cb6a423b0be8e6db019d87267d.tar.gz
Zero-init HIDL core types (all)
hidl_pointer - already zero initialized hidl_string - now memset to 0 hidl_array - has no pad to initialize, default initialize since we now expect structs to be default initialized to sane values. hidl_vec - now memset to 0 hidl_memory - has three aligned(8) items which are always set hidl_version - unused, but has two uint16_t entries Zero-init HIDL core types (hidl_handle). Has 7 padded bits at the end. Since they are passed across processes. Bug: 131356202 Test: print out values Change-Id: I3979232879bb437d17d3a6f6013b53b2951a2138 Merged-In: I56bacf9ca7ac51d73449d11883c6224e214b8773 Merged-In: I8dd52e196e1585028d91d97f00861021c21ec09c (cherry picked from commit aa79ac59c8907ea608392488f2d24889cf6778c8)
Diffstat (limited to 'base')
-rw-r--r--base/HidlSupport.cpp20
-rw-r--r--base/include/hidl/HidlInternal.h1
-rw-r--r--base/include/hidl/HidlSupport.h26
3 files changed, 28 insertions, 19 deletions
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp
index 8f3c057..e08bf93 100644
--- a/base/HidlSupport.cpp
+++ b/base/HidlSupport.cpp
@@ -36,27 +36,28 @@ bool debuggable() {
} // namespace details
hidl_handle::hidl_handle() {
- mHandle = nullptr;
- mOwnsHandle = false;
+ memset(this, 0, sizeof(*this));
+ // mHandle = nullptr;
+ // mOwnsHandle = false;
}
hidl_handle::~hidl_handle() {
freeHandle();
}
-hidl_handle::hidl_handle(const native_handle_t *handle) {
+hidl_handle::hidl_handle(const native_handle_t* handle) : hidl_handle() {
mHandle = handle;
mOwnsHandle = false;
}
// copy constructor.
-hidl_handle::hidl_handle(const hidl_handle &other) {
+hidl_handle::hidl_handle(const hidl_handle& other) : hidl_handle() {
mOwnsHandle = false;
*this = other;
}
// move constructor.
-hidl_handle::hidl_handle(hidl_handle &&other) {
+hidl_handle::hidl_handle(hidl_handle&& other) : hidl_handle() {
mOwnsHandle = false;
*this = std::move(other);
}
@@ -137,10 +138,11 @@ void hidl_handle::freeHandle() {
static const char *const kEmptyString = "";
-hidl_string::hidl_string()
- : mBuffer(kEmptyString),
- mSize(0),
- mOwnsBuffer(false) {
+hidl_string::hidl_string() {
+ memset(this, 0, sizeof(*this));
+ // mSize is zero
+ // mOwnsBuffer is false
+ mBuffer = kEmptyString;
}
hidl_string::~hidl_string() {
diff --git a/base/include/hidl/HidlInternal.h b/base/include/hidl/HidlInternal.h
index 5a08bc3..ed9f02c 100644
--- a/base/include/hidl/HidlInternal.h
+++ b/base/include/hidl/HidlInternal.h
@@ -59,6 +59,7 @@ template<typename T>
struct hidl_pointer {
hidl_pointer()
: _pad(0) {
+ static_assert(sizeof(*this) == 8, "wrong size");
}
hidl_pointer(T* ptr) : hidl_pointer() { mPointer = ptr; }
hidl_pointer(const hidl_pointer<T>& other) : hidl_pointer() { mPointer = other.mPointer; }
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index f09eb63..f5d9e71 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -316,11 +316,15 @@ protected:
template<typename T>
struct hidl_vec {
- hidl_vec()
- : mBuffer(nullptr),
- mSize(0),
- mOwnsBuffer(true) {
+ hidl_vec() {
static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset");
+
+ memset(this, 0, sizeof(*this));
+ // mSize is 0
+ // mBuffer is nullptr
+
+ // this is for consistency with the original implementation
+ mOwnsBuffer = true;
}
// Note, does not initialize primitive types.
@@ -330,18 +334,17 @@ struct hidl_vec {
*this = other;
}
- hidl_vec(hidl_vec<T> &&other) noexcept
- : mOwnsBuffer(false) {
+ hidl_vec(hidl_vec<T> &&other) noexcept : hidl_vec() {
*this = std::move(other);
}
- hidl_vec(const std::initializer_list<T> list)
- : mOwnsBuffer(true) {
+ hidl_vec(const std::initializer_list<T> list) : hidl_vec() {
if (list.size() > UINT32_MAX) {
details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
}
mSize = static_cast<uint32_t>(list.size());
mBuffer = new T[mSize];
+ mOwnsBuffer = true;
size_t idx = 0;
for (auto it = list.begin(); it != list.end(); ++it) {
@@ -357,7 +360,7 @@ struct hidl_vec {
typename = typename std::enable_if<std::is_convertible<
typename std::iterator_traits<InputIterator>::iterator_category,
std::input_iterator_tag>::value>::type>
- hidl_vec(InputIterator first, InputIterator last) : mOwnsBuffer(true) {
+ hidl_vec(InputIterator first, InputIterator last) : hidl_vec() {
auto size = std::distance(first, last);
if (size > static_cast<int64_t>(UINT32_MAX)) {
details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
@@ -367,6 +370,7 @@ struct hidl_vec {
}
mSize = static_cast<uint32_t>(size);
mBuffer = new T[mSize];
+ mOwnsBuffer = true;
size_t idx = 0;
for (; first != last; ++first) {
@@ -833,7 +837,9 @@ private:
// Version functions
struct hidl_version {
public:
- constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
+ constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {
+ static_assert(sizeof(*this) == 4, "wrong size");
+ }
bool operator==(const hidl_version& other) const {
return (mMajor == other.get_major() && mMinor == other.get_minor());