1 /*===- InstrProfilingMerge.c - Profile in-process Merging  ---------------===*\
2 |*
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 |* See https://llvm.org/LICENSE.txt for license information.
5 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 |*
7 |*===----------------------------------------------------------------------===*
8 |* This file defines the API needed for in-process merging of profile data
9 |* stored in memory buffer.
10 \*===---------------------------------------------------------------------===*/
11 
12 #include "InstrProfiling.h"
13 #include "InstrProfilingInternal.h"
14 #include "InstrProfilingUtil.h"
15 
16 #define INSTR_PROF_VALUE_PROF_DATA
17 #include "profile/InstrProfData.inc"
18 
19 COMPILER_RT_VISIBILITY
20 void (*VPMergeHook)(ValueProfData *, __llvm_profile_data *);
21 
22 COMPILER_RT_VISIBILITY
23 uint64_t lprofGetLoadModuleSignature(void) {
24   /* A very fast way to compute a module signature.  */
25   uint64_t Version = __llvm_profile_get_version();
26   uint64_t NumCounters = __llvm_profile_get_num_counters(
27       __llvm_profile_begin_counters(), __llvm_profile_end_counters());
28   uint64_t NumData = __llvm_profile_get_num_data(__llvm_profile_begin_data(),
29                                                  __llvm_profile_end_data());
30   uint64_t NamesSize =
31       (uint64_t)(__llvm_profile_end_names() - __llvm_profile_begin_names());
32   uint64_t NumVnodes =
33       (uint64_t)(__llvm_profile_end_vnodes() - __llvm_profile_begin_vnodes());
34   const __llvm_profile_data *FirstD = __llvm_profile_begin_data();
35 
36   return (NamesSize << 40) + (NumCounters << 30) + (NumData << 20) +
37          (NumVnodes << 10) + (NumData > 0 ? FirstD->NameRef : 0) + Version +
38          __llvm_profile_get_magic();
39 }
40 
41 #ifdef __GNUC__
42 #pragma GCC diagnostic push
43 #pragma GCC diagnostic ignored "-Wcast-qual"
44 #endif
45 
46 /* Returns 1 if profile is not structurally compatible.  */
47 COMPILER_RT_VISIBILITY
48 int __llvm_profile_check_compatibility(const char *ProfileData,
49                                        uint64_t ProfileSize) {
50   /* Check profile header only for now  */
51   __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;
52   __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;
53   SrcDataStart =
54       (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +
55                               Header->BinaryIdsSize);
56   SrcDataEnd = SrcDataStart + Header->DataSize;
57 
58   if (ProfileSize < sizeof(__llvm_profile_header))
59     return 1;
60 
61   /* Check the header first.  */
62   if (Header->Magic != __llvm_profile_get_magic() ||
63       Header->Version != __llvm_profile_get_version() ||
64       Header->DataSize !=
65           __llvm_profile_get_num_data(__llvm_profile_begin_data(),
66                                       __llvm_profile_end_data()) ||
67       Header->CountersSize !=
68           __llvm_profile_get_num_counters(__llvm_profile_begin_counters(),
69                                           __llvm_profile_end_counters()) ||
70       Header->NamesSize != (uint64_t)(__llvm_profile_end_names() -
71                                       __llvm_profile_begin_names()) ||
72       Header->ValueKindLast != IPVK_Last)
73     return 1;
74 
75   if (ProfileSize <
76       sizeof(__llvm_profile_header) + Header->BinaryIdsSize +
77           Header->DataSize * sizeof(__llvm_profile_data) + Header->NamesSize +
78           Header->CountersSize * __llvm_profile_counter_entry_size())
79     return 1;
80 
81   for (SrcData = SrcDataStart,
82        DstData = (__llvm_profile_data *)__llvm_profile_begin_data();
83        SrcData < SrcDataEnd; ++SrcData, ++DstData) {
84     if (SrcData->NameRef != DstData->NameRef ||
85         SrcData->FuncHash != DstData->FuncHash ||
86         SrcData->NumCounters != DstData->NumCounters)
87       return 1;
88   }
89 
90   /* Matched! */
91   return 0;
92 }
93 
94 static uintptr_t signextIfWin64(void *V) {
95 #ifdef _WIN64
96   return (uintptr_t)(int32_t)(uintptr_t)V;
97 #else
98   return (uintptr_t)V;
99 #endif
100 }
101 
102 COMPILER_RT_VISIBILITY
103 int __llvm_profile_merge_from_buffer(const char *ProfileData,
104                                      uint64_t ProfileSize) {
105   if (__llvm_profile_get_version() & VARIANT_MASK_DBG_CORRELATE) {
106     PROF_ERR(
107         "%s\n",
108         "Debug info correlation does not support profile merging at runtime. "
109         "Instead, merge raw profiles using the llvm-profdata tool.");
110     return 1;
111   }
112 
113   __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;
114   __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;
115   char *SrcCountersStart;
116   const char *SrcNameStart;
117   const char *SrcValueProfDataStart, *SrcValueProfData;
118   uintptr_t CountersDelta = Header->CountersDelta;
119 
120   SrcDataStart =
121       (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +
122                               Header->BinaryIdsSize);
123   SrcDataEnd = SrcDataStart + Header->DataSize;
124   SrcCountersStart = (char *)SrcDataEnd;
125   SrcNameStart = SrcCountersStart +
126                  Header->CountersSize * __llvm_profile_counter_entry_size();
127   SrcValueProfDataStart =
128       SrcNameStart + Header->NamesSize +
129       __llvm_profile_get_num_padding_bytes(Header->NamesSize);
130   if (SrcNameStart < SrcCountersStart)
131     return 1;
132 
133   for (SrcData = SrcDataStart,
134       DstData = (__llvm_profile_data *)__llvm_profile_begin_data(),
135       SrcValueProfData = SrcValueProfDataStart;
136        SrcData < SrcDataEnd; ++SrcData, ++DstData) {
137     // For the in-memory destination, CounterPtr is the distance from the start
138     // address of the data to the start address of the counter. On WIN64,
139     // CounterPtr is a truncated 32-bit value due to COFF limitation. Sign
140     // extend CounterPtr to get the original value.
141     char *DstCounters =
142         (char *)((uintptr_t)DstData + signextIfWin64(DstData->CounterPtr));
143     unsigned NVK = 0;
144 
145     // SrcData is a serialized representation of the memory image. We need to
146     // compute the in-buffer counter offset from the in-memory address distance.
147     // The initial CountersDelta is the in-memory address difference
148     // start(__llvm_prf_cnts)-start(__llvm_prf_data), so SrcData->CounterPtr -
149     // CountersDelta computes the offset into the in-buffer counter section.
150     //
151     // On WIN64, CountersDelta is truncated as well, so no need for signext.
152     char *SrcCounters =
153         SrcCountersStart + ((uintptr_t)SrcData->CounterPtr - CountersDelta);
154     // CountersDelta needs to be decreased as we advance to the next data
155     // record.
156     CountersDelta -= sizeof(*SrcData);
157     unsigned NC = SrcData->NumCounters;
158     if (NC == 0)
159       return 1;
160     if (SrcCounters < SrcCountersStart || SrcCounters >= SrcNameStart ||
161         (SrcCounters + __llvm_profile_counter_entry_size() * NC) > SrcNameStart)
162       return 1;
163     for (unsigned I = 0; I < NC; I++) {
164       if (__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE) {
165         // A value of zero signifies the function is covered.
166         DstCounters[I] &= SrcCounters[I];
167       } else {
168         ((uint64_t *)DstCounters)[I] += ((uint64_t *)SrcCounters)[I];
169       }
170     }
171 
172     /* Now merge value profile data. */
173     if (!VPMergeHook)
174       continue;
175 
176     for (unsigned I = 0; I <= IPVK_Last; I++)
177       NVK += (SrcData->NumValueSites[I] != 0);
178 
179     if (!NVK)
180       continue;
181 
182     if (SrcValueProfData >= ProfileData + ProfileSize)
183       return 1;
184     VPMergeHook((ValueProfData *)SrcValueProfData, DstData);
185     SrcValueProfData =
186         SrcValueProfData + ((ValueProfData *)SrcValueProfData)->TotalSize;
187   }
188 
189   return 0;
190 }
191 
192 #ifdef __GNUC__
193 #pragma GCC diagnostic pop
194 #endif
195