Merge "Enforce Treble "wire" ABI by emitting static_asserts for the sizes of" into oc-dev
diff --git a/Android.bp b/Android.bp
index 1c68952..d969017 100644
--- a/Android.bp
+++ b/Android.bp
@@ -40,6 +40,7 @@
"DeathRecipientType.cpp",
"EnumType.cpp",
"HandleType.cpp",
+ "HidlTypeAssertion.cpp",
"Interface.cpp",
"MemoryType.cpp",
"Method.cpp",
diff --git a/FmqType.cpp b/FmqType.cpp
index 989f613..e3a8ae6 100644
--- a/FmqType.cpp
+++ b/FmqType.cpp
@@ -16,6 +16,8 @@
#include "FmqType.h"
+#include "HidlTypeAssertion.h"
+
#include <hidl-util/Formatter.h>
#include <android-base/logging.h>
@@ -137,10 +139,14 @@
return false;
}
+// All MQDescriptor<T, flavor> have the same size.
+static HidlTypeAssertion assertion(
+ "MQDescriptor<char, ::android::hardware::kSynchronizedReadWrite>", 32);
+
void FmqType::getAlignmentAndSize(
size_t *align, size_t *size) const {
*align = 8; // MQDescriptor<>
- *size = 32;
+ *size = assertion.size();
}
bool FmqType::needsEmbeddedReadWrite() const {
diff --git a/HandleType.cpp b/HandleType.cpp
index 10c5f57..2bda535 100644
--- a/HandleType.cpp
+++ b/HandleType.cpp
@@ -16,6 +16,8 @@
#include "HandleType.h"
+#include "HidlTypeAssertion.h"
+
#include <hidl-util/Formatter.h>
#include <android-base/logging.h>
@@ -159,9 +161,10 @@
return false;
}
+static HidlTypeAssertion assertion("hidl_handle", 16 /* size */);
void HandleType::getAlignmentAndSize(size_t *align, size_t *size) const {
*align = 8; // hidl_handle
- *size = 16;
+ *size = assertion.size();
}
status_t HandleType::emitVtsTypeDeclarations(Formatter &out) const {
diff --git a/HidlTypeAssertion.cpp b/HidlTypeAssertion.cpp
new file mode 100644
index 0000000..bcddf9b
--- /dev/null
+++ b/HidlTypeAssertion.cpp
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "HidlTypeAssertion.h"
+
+#include <hidl-util/Formatter.h>
+
+#include <string>
+#include <vector>
+
+namespace android {
+
+typedef std::vector<std::pair<std::string, size_t>> Registry;
+static Registry ®istry() {
+ static Registry sRegistry;
+ return sRegistry;
+}
+
+HidlTypeAssertion::HidlTypeAssertion(const char *name, size_t size)
+ : mSize(size) {
+ registry().push_back(std::make_pair(name, size));
+}
+
+size_t HidlTypeAssertion::size() const {
+ return mSize;
+}
+
+// static
+void HidlTypeAssertion::EmitAll(Formatter &out) {
+ std::sort(
+ registry().begin(),
+ registry().end(),
+ [](const auto &a, const auto &b) {
+ return a.first < b.first;
+ });
+
+ for (auto entry : registry()) {
+ out << "static_assert(sizeof(::android::hardware::"
+ << entry.first
+ << ") == "
+ << entry.second
+ << ", \"wrong size\");\n";
+ }
+}
+
+} // namespace android
+
diff --git a/HidlTypeAssertion.h b/HidlTypeAssertion.h
new file mode 100644
index 0000000..8ac23ff
--- /dev/null
+++ b/HidlTypeAssertion.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef HIDL_TYPE_ASSERTIONS_H_
+
+#define HIDL_TYPE_ASSERTIONS_H_
+
+#include <android-base/macros.h>
+
+namespace android {
+
+struct Formatter;
+
+// Declare a HidlTypeAssertion at static scope to enforce a size requirement
+// on a type (assumed to be declared in namespace ::android::hardware).
+// This will cause the C++ backend of hidl-gen to emit the appropriate
+// static_assert(...) statements at the end of FooAll.cpp.
+struct HidlTypeAssertion {
+ HidlTypeAssertion(const char *name, size_t size);
+
+ size_t size() const;
+
+ static void EmitAll(Formatter &out);
+
+private:
+ size_t mSize;
+
+ DISALLOW_COPY_AND_ASSIGN(HidlTypeAssertion);
+};
+
+} // namespace android
+
+#endif // HIDL_TYPE_ASSERTIONS_H_
diff --git a/MemoryType.cpp b/MemoryType.cpp
index 614f368..f685cfc 100644
--- a/MemoryType.cpp
+++ b/MemoryType.cpp
@@ -16,6 +16,8 @@
#include "MemoryType.h"
+#include "HidlTypeAssertion.h"
+
#include <hidl-util/Formatter.h>
#include <android-base/logging.h>
@@ -140,9 +142,10 @@
return false;
}
+static HidlTypeAssertion assertion("hidl_memory", 40 /* size */);
void MemoryType::getAlignmentAndSize(size_t *align, size_t *size) const {
*align = 8; // hidl_memory
- *size = 40;
+ *size = assertion.size();
}
status_t MemoryType::emitVtsTypeDeclarations(Formatter &out) const {
diff --git a/StringType.cpp b/StringType.cpp
index f3f95ae..5571504 100644
--- a/StringType.cpp
+++ b/StringType.cpp
@@ -16,6 +16,8 @@
#include "StringType.h"
+#include "HidlTypeAssertion.h"
+
#include <hidl-util/Formatter.h>
namespace android {
@@ -208,9 +210,10 @@
return OK;
}
+static HidlTypeAssertion assertion("hidl_string", 16 /* size */);
void StringType::getAlignmentAndSize(size_t *align, size_t *size) const {
*align = 8; // hidl_string
- *size = 16;
+ *size = assertion.size();
}
} // namespace android
diff --git a/VectorType.cpp b/VectorType.cpp
index 6a0542e..237113e 100644
--- a/VectorType.cpp
+++ b/VectorType.cpp
@@ -18,6 +18,7 @@
#include "ArrayType.h"
#include "CompoundType.h"
+#include "HidlTypeAssertion.h"
#include <hidl-util/Formatter.h>
#include <android-base/logging.h>
@@ -740,9 +741,11 @@
return mElementType->containsPointer();
}
+// All hidl_vec<T> have the same size.
+static HidlTypeAssertion assertion("hidl_vec<char>", 16 /* size */);
void VectorType::getAlignmentAndSize(size_t *align, size_t *size) const {
*align = 8; // hidl_vec<T>
- *size = 16;
+ *size = assertion.size();
}
} // namespace android
diff --git a/generateCpp.cpp b/generateCpp.cpp
index d730f0c..c686be2 100644
--- a/generateCpp.cpp
+++ b/generateCpp.cpp
@@ -19,6 +19,7 @@
#include "Coordinator.h"
#include "EnumType.h"
#include "Interface.h"
+#include "HidlTypeAssertion.h"
#include "Method.h"
#include "ScalarType.h"
#include "Scope.h"
@@ -1150,6 +1151,9 @@
}
}
+ HidlTypeAssertion::EmitAll(out);
+ out << "\n";
+
enterLeaveNamespace(out, false /* enter */);
return err;