ndk: Emit aligment values for known types in @FixedSize parcelables
This is only done in the ndk backend. It ensures that the @FixedSize
parcelable is the same size accross different builds.
For example:
uint64_t has an alignment of 4 in 32-bit bit builds, 8 in 64-bit, making
it possible for it to be a different location on some devices.
Using __attribute__((aligned(8))) in all cases ensures the same layout.
Test: atest CtsNdkBinderTestCases aidl_integration_test aidl_unittests
Test: built CtsNdkBinderTestCases for aosp_x86_64-eng before/after this change
Bug: 142326204
Change-Id: I0be49db94e911df37acfc2b0bd3578715975a3d1
diff --git a/aidl_to_ndk.cpp b/aidl_to_ndk.cpp
index 3664e77..c4b1aeb 100644
--- a/aidl_to_ndk.cpp
+++ b/aidl_to_ndk.cpp
@@ -416,6 +416,26 @@
}
}
+size_t NdkAlignmentOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
+ // map from NDK type name to the corresponding alignment size
+ static map<string, int> alignment = {
+ {"bool", 1}, {"int8_t", 1}, {"char16_t", 2}, {"double", 8},
+ {"float", 4}, {"int32_t", 4}, {"int64_t", 8},
+ };
+
+ const string& name = NdkNameOf(types, aidl, StorageMode::STACK);
+ if (alignment.find(name) != alignment.end()) {
+ return alignment[name];
+ } else {
+ const auto& definedType = types.TryGetDefinedType(aidl.GetName());
+ AIDL_FATAL_IF(definedType == nullptr, aidl) << "Failed to resolve type.";
+ if (const auto& enumType = definedType->AsEnumDeclaration(); enumType != nullptr) {
+ return NdkAlignmentOf(types, enumType->GetBackingType());
+ }
+ }
+ return 0;
+}
+
void WriteToParcelFor(const CodeGeneratorContext& c) {
TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
aspect.write_func(c);