From: River Riddle Date: Sat, 20 Jul 2019 16:22:36 +0000 (-0700) Subject: Ensure that DenseElementAttr data is 64-bit aligned. X-Git-Tag: llvmorg-11-init~1466^2~1132 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a47704e1e17bed5bacca57dfccea557a9b78c8dc;p=platform%2Fupstream%2Fllvm.git Ensure that DenseElementAttr data is 64-bit aligned. This allows for the raw data to be reinterpreted as the derived c++ type, e.g. ArrayRef. This fixes a ubsan error for misaligned-pointer-use. PiperOrigin-RevId: 259128031 --- diff --git a/mlir/lib/IR/AttributeDetail.h b/mlir/lib/IR/AttributeDetail.h index a226a4c..21f8b68 100644 --- a/mlir/lib/IR/AttributeDetail.h +++ b/mlir/lib/IR/AttributeDetail.h @@ -480,15 +480,22 @@ struct DenseElementsAttributeStorage : public AttributeStorage { /// Construct a new storage instance. static DenseElementsAttributeStorage * construct(AttributeStorageAllocator &allocator, KeyTy key) { - // If the data buffer is non-empty, we copy it into the allocator. - ArrayRef data = allocator.copyInto(key.data); - - // If this is a boolean splat, make sure only the first bit is used. - if (key.isSplat && key.type.getElementTypeBitWidth() == 1) - const_cast(data.front()) &= 1; + // If the data buffer is non-empty, we copy it into the allocator with a + // 64-bit alignment. + ArrayRef copy, data = key.data; + if (!data.empty()) { + char *rawData = reinterpret_cast( + allocator.allocate(data.size(), alignof(uint64_t))); + std::memcpy(rawData, data.data(), data.size()); + + // If this is a boolean splat, make sure only the first bit is used. + if (key.isSplat && key.type.getElementTypeBitWidth() == 1) + rawData[0] &= 1; + copy = ArrayRef(rawData, data.size()); + } return new (allocator.allocate()) - DenseElementsAttributeStorage(key.type, data, key.isSplat); + DenseElementsAttributeStorage(key.type, copy, key.isSplat); } ArrayRef data;