[compiler-rt] r346476 - [XRay] Add a static assertion on size of metadata payload (NFC)

Dean Michael Berris via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 8 23:16:46 PST 2018


Author: dberris
Date: Thu Nov  8 23:16:45 2018
New Revision: 346476

URL: http://llvm.org/viewvc/llvm-project?rev=346476&view=rev
Log:
[XRay] Add a static assertion on size of metadata payload (NFC)

This change adds a static check to ensure that all data metadata record
payloads don't go past the available buffers in Metadata records.

Modified:
    compiler-rt/trunk/lib/xray/xray_fdr_log_writer.h

Modified: compiler-rt/trunk/lib/xray/xray_fdr_log_writer.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_fdr_log_writer.h?rev=346476&r1=346475&r2=346476&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_fdr_log_writer.h (original)
+++ compiler-rt/trunk/lib/xray/xray_fdr_log_writer.h Thu Nov  8 23:16:45 2018
@@ -46,8 +46,27 @@ template <size_t Index> struct Serialize
 
 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);




More information about the llvm-commits mailing list