Path: blob/main/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingMerge.c
35233 views
/*===- InstrProfilingMerge.c - Profile in-process Merging ---------------===*\1|*2|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3|* See https://llvm.org/LICENSE.txt for license information.4|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5|*6|*===----------------------------------------------------------------------===*7|* This file defines the API needed for in-process merging of profile data8|* stored in memory buffer.9\*===---------------------------------------------------------------------===*/1011#include "InstrProfiling.h"12#include "InstrProfilingInternal.h"13#include "InstrProfilingUtil.h"1415#define INSTR_PROF_VALUE_PROF_DATA16#include "profile/InstrProfData.inc"1718COMPILER_RT_VISIBILITY19void (*VPMergeHook)(ValueProfData *, __llvm_profile_data *);2021COMPILER_RT_VISIBILITY22uint64_t lprofGetLoadModuleSignature(void) {23/* A very fast way to compute a module signature. */24uint64_t Version = __llvm_profile_get_version();25uint64_t NumCounters = __llvm_profile_get_num_counters(26__llvm_profile_begin_counters(), __llvm_profile_end_counters());27uint64_t NumData = __llvm_profile_get_num_data(__llvm_profile_begin_data(),28__llvm_profile_end_data());29uint64_t NamesSize =30(uint64_t)(__llvm_profile_end_names() - __llvm_profile_begin_names());31uint64_t NumVnodes =32(uint64_t)(__llvm_profile_end_vnodes() - __llvm_profile_begin_vnodes());33const __llvm_profile_data *FirstD = __llvm_profile_begin_data();3435return (NamesSize << 40) + (NumCounters << 30) + (NumData << 20) +36(NumVnodes << 10) + (NumData > 0 ? FirstD->NameRef : 0) + Version +37__llvm_profile_get_magic();38}3940#ifdef __GNUC__41#pragma GCC diagnostic push42#pragma GCC diagnostic ignored "-Wcast-qual"43#elif defined(__clang__)44#pragma clang diagnostic push45#pragma clang diagnostic ignored "-Wcast-qual"46#endif4748/* Returns 1 if profile is not structurally compatible. */49COMPILER_RT_VISIBILITY50int __llvm_profile_check_compatibility(const char *ProfileData,51uint64_t ProfileSize) {52__llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;53__llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;54SrcDataStart =55(__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +56Header->BinaryIdsSize);57SrcDataEnd = SrcDataStart + Header->NumData;5859if (ProfileSize < sizeof(__llvm_profile_header))60return 1;6162/* Check the header first. */63if (Header->Magic != __llvm_profile_get_magic() ||64Header->Version != __llvm_profile_get_version() ||65Header->NumData !=66__llvm_profile_get_num_data(__llvm_profile_begin_data(),67__llvm_profile_end_data()) ||68Header->NumCounters !=69__llvm_profile_get_num_counters(__llvm_profile_begin_counters(),70__llvm_profile_end_counters()) ||71Header->NumBitmapBytes !=72__llvm_profile_get_num_bitmap_bytes(__llvm_profile_begin_bitmap(),73__llvm_profile_end_bitmap()) ||74Header->NamesSize !=75__llvm_profile_get_name_size(__llvm_profile_begin_names(),76__llvm_profile_end_names()) ||77Header->ValueKindLast != IPVK_Last)78return 1;7980if (ProfileSize <81sizeof(__llvm_profile_header) + Header->BinaryIdsSize +82Header->NumData * sizeof(__llvm_profile_data) + Header->NamesSize +83Header->NumCounters * __llvm_profile_counter_entry_size() +84Header->NumBitmapBytes)85return 1;8687for (SrcData = SrcDataStart,88DstData = (__llvm_profile_data *)__llvm_profile_begin_data();89SrcData < SrcDataEnd; ++SrcData, ++DstData) {90if (SrcData->NameRef != DstData->NameRef ||91SrcData->FuncHash != DstData->FuncHash ||92SrcData->NumCounters != DstData->NumCounters ||93SrcData->NumBitmapBytes != DstData->NumBitmapBytes)94return 1;95}9697/* Matched! */98return 0;99}100101static uintptr_t signextIfWin64(void *V) {102#ifdef _WIN64103return (uintptr_t)(int32_t)(uintptr_t)V;104#else105return (uintptr_t)V;106#endif107}108109// Skip names section, vtable profile data section and vtable names section110// for runtime profile merge. To merge runtime addresses from multiple111// profiles collected from the same instrumented binary, the binary should be112// loaded at fixed base address (e.g., build with -no-pie, or run with ASLR113// disabled). In this set-up these three sections remain unchanged.114static uint64_t115getDistanceFromCounterToValueProf(const __llvm_profile_header *const Header) {116const uint64_t VTableSectionSize =117Header->NumVTables * sizeof(VTableProfData);118const uint64_t PaddingBytesAfterVTableSection =119__llvm_profile_get_num_padding_bytes(VTableSectionSize);120const uint64_t VNamesSize = Header->VNamesSize;121const uint64_t PaddingBytesAfterVNamesSize =122__llvm_profile_get_num_padding_bytes(VNamesSize);123return Header->NamesSize +124__llvm_profile_get_num_padding_bytes(Header->NamesSize) +125VTableSectionSize + PaddingBytesAfterVTableSection + VNamesSize +126PaddingBytesAfterVNamesSize;127}128129COMPILER_RT_VISIBILITY130int __llvm_profile_merge_from_buffer(const char *ProfileData,131uint64_t ProfileSize) {132if (__llvm_profile_get_version() & VARIANT_MASK_TEMPORAL_PROF) {133PROF_ERR("%s\n",134"Temporal profiles do not support profile merging at runtime. "135"Instead, merge raw profiles using the llvm-profdata tool.");136return 1;137}138139__llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;140__llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;141char *SrcCountersStart, *DstCounter;142const char *SrcCountersEnd, *SrcCounter;143const char *SrcBitmapStart;144const char *SrcNameStart;145const char *SrcValueProfDataStart, *SrcValueProfData;146uintptr_t CountersDelta = Header->CountersDelta;147uintptr_t BitmapDelta = Header->BitmapDelta;148149SrcDataStart =150(__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +151Header->BinaryIdsSize);152SrcDataEnd = SrcDataStart + Header->NumData;153SrcCountersStart = (char *)SrcDataEnd;154SrcCountersEnd = SrcCountersStart +155Header->NumCounters * __llvm_profile_counter_entry_size();156SrcBitmapStart = SrcCountersEnd;157SrcNameStart = SrcBitmapStart + Header->NumBitmapBytes;158SrcValueProfDataStart =159SrcNameStart + getDistanceFromCounterToValueProf(Header);160if (SrcNameStart < SrcCountersStart || SrcNameStart < SrcBitmapStart)161return 1;162163// Merge counters by iterating the entire counter section when data section is164// empty due to correlation.165if (Header->NumData == 0) {166for (SrcCounter = SrcCountersStart,167DstCounter = __llvm_profile_begin_counters();168SrcCounter < SrcCountersEnd;) {169if (__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE) {170*DstCounter &= *SrcCounter;171} else {172*(uint64_t *)DstCounter += *(uint64_t *)SrcCounter;173}174SrcCounter += __llvm_profile_counter_entry_size();175DstCounter += __llvm_profile_counter_entry_size();176}177return 0;178}179180for (SrcData = SrcDataStart,181DstData = (__llvm_profile_data *)__llvm_profile_begin_data(),182SrcValueProfData = SrcValueProfDataStart;183SrcData < SrcDataEnd; ++SrcData, ++DstData) {184// For the in-memory destination, CounterPtr is the distance from the start185// address of the data to the start address of the counter. On WIN64,186// CounterPtr is a truncated 32-bit value due to COFF limitation. Sign187// extend CounterPtr to get the original value.188char *DstCounters =189(char *)((uintptr_t)DstData + signextIfWin64(DstData->CounterPtr));190char *DstBitmap =191(char *)((uintptr_t)DstData + signextIfWin64(DstData->BitmapPtr));192unsigned NVK = 0;193194// SrcData is a serialized representation of the memory image. We need to195// compute the in-buffer counter offset from the in-memory address distance.196// The initial CountersDelta is the in-memory address difference197// start(__llvm_prf_cnts)-start(__llvm_prf_data), so SrcData->CounterPtr -198// CountersDelta computes the offset into the in-buffer counter section.199//200// On WIN64, CountersDelta is truncated as well, so no need for signext.201char *SrcCounters =202SrcCountersStart + ((uintptr_t)SrcData->CounterPtr - CountersDelta);203// CountersDelta needs to be decreased as we advance to the next data204// record.205CountersDelta -= sizeof(*SrcData);206unsigned NC = SrcData->NumCounters;207if (NC == 0)208return 1;209if (SrcCounters < SrcCountersStart || SrcCounters >= SrcNameStart ||210(SrcCounters + __llvm_profile_counter_entry_size() * NC) > SrcNameStart)211return 1;212for (unsigned I = 0; I < NC; I++) {213if (__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE) {214// A value of zero signifies the function is covered.215DstCounters[I] &= SrcCounters[I];216} else {217((uint64_t *)DstCounters)[I] += ((uint64_t *)SrcCounters)[I];218}219}220221const char *SrcBitmap =222SrcBitmapStart + ((uintptr_t)SrcData->BitmapPtr - BitmapDelta);223// BitmapDelta also needs to be decreased as we advance to the next data224// record.225BitmapDelta -= sizeof(*SrcData);226unsigned NB = SrcData->NumBitmapBytes;227// NumBitmapBytes may legitimately be 0. Just keep going.228if (NB != 0) {229if (SrcBitmap < SrcBitmapStart || (SrcBitmap + NB) > SrcNameStart)230return 1;231// Merge Src and Dst Bitmap bytes by simply ORing them together.232for (unsigned I = 0; I < NB; I++)233DstBitmap[I] |= SrcBitmap[I];234}235236/* Now merge value profile data. */237if (!VPMergeHook)238continue;239240for (unsigned I = 0; I <= IPVK_Last; I++)241NVK += (SrcData->NumValueSites[I] != 0);242243if (!NVK)244continue;245246if (SrcValueProfData >= ProfileData + ProfileSize)247return 1;248VPMergeHook((ValueProfData *)SrcValueProfData, DstData);249SrcValueProfData =250SrcValueProfData + ((ValueProfData *)SrcValueProfData)->TotalSize;251}252253return 0;254}255256#ifdef __GNUC__257#pragma GCC diagnostic pop258#elif defined(__clang__)259#pragma clang diagnostic pop260#endif261262263