More stringently verify expected alignment of fields in hidl compound types.
Bug: 33846034
Test: make
Change-Id: Iaadddeabe04e0c721aa9398a1bb748fd6adcac51
diff --git a/CompoundType.cpp b/CompoundType.cpp
index 332bada..25f0f8b 100644
--- a/CompoundType.cpp
+++ b/CompoundType.cpp
@@ -345,7 +345,7 @@
Scope::emitTypeDeclarations(out);
- if (!isJavaCompatible()) {
+ if (containsPointer()) {
for (const auto &field : *mFields) {
out << field->type().getCppStackType()
<< " "
@@ -387,7 +387,9 @@
<< ", \"wrong offset\");\n";
}
- offset += fieldSize;
+ if (mStyle == STYLE_STRUCT) {
+ offset += fieldSize;
+ }
}
if (pass == 0) {
@@ -1049,8 +1051,23 @@
return true;
}
+bool CompoundType::containsPointer() const {
+ if (Scope::containsPointer()) {
+ return true;
+ }
+
+ for (const auto &field : *mFields) {
+ if (field->type().containsPointer()) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
void CompoundType::getAlignmentAndSize(size_t *align, size_t *size) const {
*align = 1;
+ *size = 0;
size_t offset = 0;
for (const auto &field : *mFields) {
@@ -1066,20 +1083,26 @@
offset += fieldAlign - pad;
}
- offset += fieldSize;
+ if (mStyle == STYLE_STRUCT) {
+ offset += fieldSize;
+ } else {
+ *size = std::max(*size, fieldSize);
+ }
if (fieldAlign > (*align)) {
*align = fieldAlign;
}
}
- // Final padding to account for the structure's alignment.
- size_t pad = offset % (*align);
- if (pad > 0) {
- offset += (*align) - pad;
+ if (mStyle == STYLE_STRUCT) {
+ *size = offset;
}
- *size = offset;
+ // Final padding to account for the structure's alignment.
+ size_t pad = (*size) % (*align);
+ if (pad > 0) {
+ (*size) += (*align) - pad;
+ }
if (*size == 0) {
// An empty struct still occupies a byte of space in C++.