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() {
24   /* A very fast way to compute a module signature.  */
25   uint64_t Version = __llvm_profile_get_version();
26   uint64_t CounterSize = (uint64_t)(__llvm_profile_end_counters() -
27                                     __llvm_profile_begin_counters());
28   uint64_t DataSize = __llvm_profile_get_data_size(__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) + (CounterSize << 30) + (DataSize << 20) +
37          (NumVnodes << 10) + (DataSize > 0 ? FirstD->NameRef : 0) + Version +
38          __llvm_profile_get_magic();
39 }
40 
41 /* Returns 1 if profile is not structurally compatible.  */
42 COMPILER_RT_VISIBILITY
43 int __llvm_profile_check_compatibility(const char *ProfileData,
44                                        uint64_t ProfileSize) {
45   /* Check profile header only for now  */
46   __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;
47   __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;
48   SrcDataStart =
49       (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +
50                               Header->BinaryIdsSize);
51   SrcDataEnd = SrcDataStart + Header->DataSize;
52 
53   if (ProfileSize < sizeof(__llvm_profile_header))
54     return 1;
55 
56   /* Check the header first.  */
57   if (Header->Magic != __llvm_profile_get_magic() ||
58       Header->Version != __llvm_profile_get_version() ||
59       Header->DataSize !=
60           __llvm_profile_get_data_size(__llvm_profile_begin_data(),
61                                        __llvm_profile_end_data()) ||
62       Header->CountersSize != (uint64_t)(__llvm_profile_end_counters() -
63                                          __llvm_profile_begin_counters()) ||
64       Header->NamesSize != (uint64_t)(__llvm_profile_end_names() -
65                                       __llvm_profile_begin_names()) ||
66       Header->ValueKindLast != IPVK_Last)
67     return 1;
68 
69   if (ProfileSize < sizeof(__llvm_profile_header) + Header->BinaryIdsSize +
70                         Header->DataSize * sizeof(__llvm_profile_data) +
71                         Header->NamesSize + Header->CountersSize)
72     return 1;
73 
74   for (SrcData = SrcDataStart,
75        DstData = (__llvm_profile_data *)__llvm_profile_begin_data();
76        SrcData < SrcDataEnd; ++SrcData, ++DstData) {
77     if (SrcData->NameRef != DstData->NameRef ||
78         SrcData->FuncHash != DstData->FuncHash ||
79         SrcData->NumCounters != DstData->NumCounters)
80       return 1;
81   }
82 
83   /* Matched! */
84   return 0;
85 }
86 
87 static uintptr_t signextIfWin64(void *V) {
88 #ifdef _WIN64
89   return (uintptr_t)(int32_t)(uintptr_t)V;
90 #else
91   return (uintptr_t)V;
92 #endif
93 }
94 
95 COMPILER_RT_VISIBILITY
96 int __llvm_profile_merge_from_buffer(const char *ProfileData,
97                                      uint64_t ProfileSize) {
98   if (__llvm_profile_get_version() & VARIANT_MASK_DBG_CORRELATE) {
99     PROF_ERR(
100         "%s\n",
101         "Debug info correlation does not support profile merging at runtime. "
102         "Instead, merge raw profiles using the llvm-profdata tool.");
103     return 1;
104   }
105 
106   __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;
107   __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;
108   uint64_t *SrcCountersStart;
109   const char *SrcNameStart;
110   const char *SrcValueProfDataStart, *SrcValueProfData;
111   uintptr_t CountersDelta = Header->CountersDelta;
112 
113   SrcDataStart =
114       (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +
115                               Header->BinaryIdsSize);
116   SrcDataEnd = SrcDataStart + Header->DataSize;
117   SrcCountersStart = (uint64_t *)SrcDataEnd;
118   SrcNameStart = (const char *)(SrcCountersStart + Header->CountersSize);
119   SrcValueProfDataStart =
120       SrcNameStart + Header->NamesSize +
121       __llvm_profile_get_num_padding_bytes(Header->NamesSize);
122   if (SrcNameStart < (const char *)SrcCountersStart)
123     return 1;
124 
125   for (SrcData = SrcDataStart,
126       DstData = (__llvm_profile_data *)__llvm_profile_begin_data(),
127       SrcValueProfData = SrcValueProfDataStart;
128        SrcData < SrcDataEnd; ++SrcData, ++DstData) {
129     // For the in-memory destination, CounterPtr is the distance from the start
130     // address of the data to the start address of the counter. On WIN64,
131     // CounterPtr is a truncated 32-bit value due to COFF limitation. Sign
132     // extend CounterPtr to get the original value.
133     uint64_t *DstCounters =
134         (uint64_t *)((uintptr_t)DstData + signextIfWin64(DstData->CounterPtr));
135     unsigned NVK = 0;
136 
137     // SrcData is a serialized representation of the memory image. We need to
138     // compute the in-buffer counter offset from the in-memory address distance.
139     // The initial CountersDelta is the in-memory address difference
140     // start(__llvm_prf_cnts)-start(__llvm_prf_data), so SrcData->CounterPtr -
141     // CountersDelta computes the offset into the in-buffer counter section.
142     //
143     // On WIN64, CountersDelta is truncated as well, so no need for signext.
144     uint64_t *SrcCounters =
145         SrcCountersStart +
146         ((uintptr_t)SrcData->CounterPtr - CountersDelta) / sizeof(uint64_t);
147     // CountersDelta needs to be decreased as we advance to the next data
148     // record.
149     CountersDelta -= sizeof(*SrcData);
150     unsigned NC = SrcData->NumCounters;
151     if (NC == 0)
152       return 1;
153     if (SrcCounters < SrcCountersStart ||
154         (const char *)SrcCounters >= SrcNameStart ||
155         (const char *)(SrcCounters + NC) > SrcNameStart)
156       return 1;
157     for (unsigned I = 0; I < NC; I++)
158       DstCounters[I] += SrcCounters[I];
159 
160     /* Now merge value profile data. */
161     if (!VPMergeHook)
162       continue;
163 
164     for (unsigned I = 0; I <= IPVK_Last; I++)
165       NVK += (SrcData->NumValueSites[I] != 0);
166 
167     if (!NVK)
168       continue;
169 
170     if (SrcValueProfData >= ProfileData + ProfileSize)
171       return 1;
172     VPMergeHook((ValueProfData *)SrcValueProfData, DstData);
173     SrcValueProfData =
174         SrcValueProfData + ((ValueProfData *)SrcValueProfData)->TotalSize;
175   }
176 
177   return 0;
178 }
179