Enforce Treble "wire" ABI by emitting static_asserts for the sizes of
hidl types, such as hidl_handle, hidl_memory, hidl_string and friends.
Bug: 33846034
Test: make
Change-Id: I859b5a7185e2c3e59ef5665e8f8c1f5bb34bced3
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
+