[XRay] Add a static assertion on size of metadata payload (NFC)
authorDean Michael Berris <dberris@google.com>
Fri, 9 Nov 2018 07:16:45 +0000 (07:16 +0000)
committerDean Michael Berris <dberris@google.com>
Fri, 9 Nov 2018 07:16:45 +0000 (07:16 +0000)
This change adds a static check to ensure that all data metadata record
payloads don't go past the available buffers in Metadata records.

llvm-svn: 346476

compiler-rt/lib/xray/xray_fdr_log_writer.h

index c18ff7bbf0bed9cf1bd8e88b67dca442e7c74e1d..9ab44fcb00193c4ec8610262facf9e98c04af0da 100644 (file)
@@ -46,8 +46,27 @@ template <size_t Index> struct SerializerImpl {
 
 using Serializer = SerializerImpl<0>;
 
+template <class Tuple, size_t Index> struct AggregateSizesImpl {
+  static constexpr size_t value =
+      sizeof(typename std::tuple_element<Index, Tuple>::type) +
+      AggregateSizesImpl<Tuple, Index - 1>::value;
+};
+
+template <class Tuple> struct AggregateSizesImpl<Tuple, 0> {
+  static constexpr size_t value =
+      sizeof(typename std::tuple_element<0, Tuple>::type);
+};
+
+template <class Tuple> struct AggregateSizes {
+  static constexpr size_t value =
+      AggregateSizesImpl<Tuple, std::tuple_size<Tuple>::value - 1>::value;
+};
+
 template <MetadataRecord::RecordKinds Kind, class... DataTypes>
 MetadataRecord createMetadataRecord(DataTypes &&... Ds) {
+  static_assert(AggregateSizes<std::tuple<DataTypes...>>::value <=
+                    sizeof(MetadataRecord) - 1,
+                "Metadata payload longer than metadata buffer!");
   MetadataRecord R;
   R.Type = 1;
   R.RecordKind = static_cast<uint8_t>(Kind);