Path: blob/main/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingBuffer.c
35233 views
/*===- InstrProfilingBuffer.c - Write instrumentation to a memory buffer --===*\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\*===----------------------------------------------------------------------===*/78// Note: This is linked into the Darwin kernel, and must remain compatible9// with freestanding compilation. See `darwin_add_builtin_libraries`.1011#include "InstrProfiling.h"12#include "InstrProfilingInternal.h"13#include "InstrProfilingPort.h"1415/* When continuous mode is enabled (%c), this parameter is set to 1.16*17* This parameter is defined here in InstrProfilingBuffer.o, instead of in18* InstrProfilingFile.o, to sequester all libc-dependent code in19* InstrProfilingFile.o. The test `instrprof-without-libc` will break if this20* layering is violated. */21static int ContinuouslySyncProfile = 0;2223/* The system page size. Only valid when non-zero. If 0, the page size is24* unavailable. */25static unsigned PageSize = 0;2627COMPILER_RT_VISIBILITY int __llvm_profile_is_continuous_mode_enabled(void) {28return ContinuouslySyncProfile && PageSize;29}3031COMPILER_RT_VISIBILITY void __llvm_profile_enable_continuous_mode(void) {32ContinuouslySyncProfile = 1;33}3435COMPILER_RT_VISIBILITY void __llvm_profile_disable_continuous_mode(void) {36ContinuouslySyncProfile = 0;37}3839COMPILER_RT_VISIBILITY void __llvm_profile_set_page_size(unsigned PS) {40PageSize = PS;41}4243COMPILER_RT_VISIBILITY44uint64_t __llvm_profile_get_size_for_buffer(void) {45const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();46const __llvm_profile_data *DataEnd = __llvm_profile_end_data();47const char *CountersBegin = __llvm_profile_begin_counters();48const char *CountersEnd = __llvm_profile_end_counters();49const char *BitmapBegin = __llvm_profile_begin_bitmap();50const char *BitmapEnd = __llvm_profile_end_bitmap();51const char *NamesBegin = __llvm_profile_begin_names();52const char *NamesEnd = __llvm_profile_end_names();53const VTableProfData *VTableBegin = __llvm_profile_begin_vtables();54const VTableProfData *VTableEnd = __llvm_profile_end_vtables();55const char *VNamesBegin = __llvm_profile_begin_vtabnames();56const char *VNamesEnd = __llvm_profile_end_vtabnames();5758return __llvm_profile_get_size_for_buffer_internal(59DataBegin, DataEnd, CountersBegin, CountersEnd, BitmapBegin, BitmapEnd,60NamesBegin, NamesEnd, VTableBegin, VTableEnd, VNamesBegin, VNamesEnd);61}6263// NOTE: Caller should guarantee that `Begin` and `End` specifies a half-open64// interval [Begin, End). Namely, `End` is one-byte past the end of the array.65COMPILER_RT_VISIBILITY66uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin,67const __llvm_profile_data *End) {68intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;69return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) /70sizeof(__llvm_profile_data);71}7273COMPILER_RT_VISIBILITY74uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,75const __llvm_profile_data *End) {76return __llvm_profile_get_num_data(Begin, End) * sizeof(__llvm_profile_data);77}7879// Counts the number of `VTableProfData` elements within the range of [Begin,80// End). Caller should guarantee that End points to one byte past the inclusive81// range.82// FIXME: Add a compiler-rt test to make sure the number of vtables in the83// raw profile is the same as the number of vtable elements in the instrumented84// binary.85COMPILER_RT_VISIBILITY86uint64_t __llvm_profile_get_num_vtable(const VTableProfData *Begin,87const VTableProfData *End) {88// Convert pointers to intptr_t to use integer arithmetic.89intptr_t EndI = (intptr_t)End, BeginI = (intptr_t)Begin;90return (EndI - BeginI) / sizeof(VTableProfData);91}9293COMPILER_RT_VISIBILITY94uint64_t __llvm_profile_get_vtable_section_size(const VTableProfData *Begin,95const VTableProfData *End) {96return (intptr_t)(End) - (intptr_t)(Begin);97}9899COMPILER_RT_VISIBILITY size_t __llvm_profile_counter_entry_size(void) {100if (__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE)101return sizeof(uint8_t);102return sizeof(uint64_t);103}104105COMPILER_RT_VISIBILITY106uint64_t __llvm_profile_get_num_counters(const char *Begin, const char *End) {107intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;108return ((EndI + __llvm_profile_counter_entry_size() - 1) - BeginI) /109__llvm_profile_counter_entry_size();110}111112COMPILER_RT_VISIBILITY113uint64_t __llvm_profile_get_counters_size(const char *Begin, const char *End) {114return __llvm_profile_get_num_counters(Begin, End) *115__llvm_profile_counter_entry_size();116}117118COMPILER_RT_VISIBILITY119uint64_t __llvm_profile_get_num_bitmap_bytes(const char *Begin,120const char *End) {121return (End - Begin);122}123124COMPILER_RT_VISIBILITY125uint64_t __llvm_profile_get_name_size(const char *Begin, const char *End) {126return End - Begin;127}128129/// Calculate the number of padding bytes needed to add to \p Offset in order130/// for (\p Offset + Padding) to be page-aligned.131static uint64_t calculateBytesNeededToPageAlign(uint64_t Offset) {132uint64_t OffsetModPage = Offset % PageSize;133if (OffsetModPage > 0)134return PageSize - OffsetModPage;135return 0;136}137138static int needsCounterPadding(void) {139#if defined(__APPLE__)140return __llvm_profile_is_continuous_mode_enabled();141#else142return 0;143#endif144}145146COMPILER_RT_VISIBILITY147int __llvm_profile_get_padding_sizes_for_counters(148uint64_t DataSize, uint64_t CountersSize, uint64_t NumBitmapBytes,149uint64_t NamesSize, uint64_t VTableSize, uint64_t VNameSize,150uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters,151uint64_t *PaddingBytesAfterBitmapBytes, uint64_t *PaddingBytesAfterNames,152uint64_t *PaddingBytesAfterVTable, uint64_t *PaddingBytesAfterVName) {153// Counter padding is needed only if continuous mode is enabled.154if (!needsCounterPadding()) {155*PaddingBytesBeforeCounters = 0;156*PaddingBytesAfterCounters =157__llvm_profile_get_num_padding_bytes(CountersSize);158*PaddingBytesAfterBitmapBytes =159__llvm_profile_get_num_padding_bytes(NumBitmapBytes);160*PaddingBytesAfterNames = __llvm_profile_get_num_padding_bytes(NamesSize);161if (PaddingBytesAfterVTable != NULL)162*PaddingBytesAfterVTable =163__llvm_profile_get_num_padding_bytes(VTableSize);164if (PaddingBytesAfterVName != NULL)165*PaddingBytesAfterVName = __llvm_profile_get_num_padding_bytes(VNameSize);166return 0;167}168169// Value profiling not supported in continuous mode at profile-write time.170// Return -1 to alert the incompatibility.171if (VTableSize != 0 || VNameSize != 0)172return -1;173174// In continuous mode, the file offsets for headers and for the start of175// counter sections need to be page-aligned.176*PaddingBytesBeforeCounters =177calculateBytesNeededToPageAlign(sizeof(__llvm_profile_header) + DataSize);178*PaddingBytesAfterCounters = calculateBytesNeededToPageAlign(CountersSize);179*PaddingBytesAfterBitmapBytes =180calculateBytesNeededToPageAlign(NumBitmapBytes);181*PaddingBytesAfterNames = calculateBytesNeededToPageAlign(NamesSize);182// Set these two variables to zero to avoid uninitialized variables183// even if VTableSize and VNameSize are known to be zero.184if (PaddingBytesAfterVTable != NULL)185*PaddingBytesAfterVTable = 0;186if (PaddingBytesAfterVName != NULL)187*PaddingBytesAfterVName = 0;188return 0;189}190191COMPILER_RT_VISIBILITY192uint64_t __llvm_profile_get_size_for_buffer_internal(193const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,194const char *CountersBegin, const char *CountersEnd, const char *BitmapBegin,195const char *BitmapEnd, const char *NamesBegin, const char *NamesEnd,196const VTableProfData *VTableBegin, const VTableProfData *VTableEnd,197const char *VNamesBegin, const char *VNamesEnd) {198/* Match logic in __llvm_profile_write_buffer(). */199const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);200uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);201uint64_t CountersSize =202__llvm_profile_get_counters_size(CountersBegin, CountersEnd);203const uint64_t NumBitmapBytes =204__llvm_profile_get_num_bitmap_bytes(BitmapBegin, BitmapEnd);205const uint64_t VTableSize =206__llvm_profile_get_vtable_section_size(VTableBegin, VTableEnd);207const uint64_t VNameSize =208__llvm_profile_get_name_size(VNamesBegin, VNamesEnd);209210/* Determine how much padding is needed before/after the counters and after211* the names. */212uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters,213PaddingBytesAfterNames, PaddingBytesAfterBitmapBytes,214PaddingBytesAfterVTable, PaddingBytesAfterVNames;215__llvm_profile_get_padding_sizes_for_counters(216DataSize, CountersSize, NumBitmapBytes, NamesSize, 0 /* VTableSize */,2170 /* VNameSize */, &PaddingBytesBeforeCounters,218&PaddingBytesAfterCounters, &PaddingBytesAfterBitmapBytes,219&PaddingBytesAfterNames, &PaddingBytesAfterVTable,220&PaddingBytesAfterVNames);221222return sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) +223DataSize + PaddingBytesBeforeCounters + CountersSize +224PaddingBytesAfterCounters + NumBitmapBytes +225PaddingBytesAfterBitmapBytes + NamesSize + PaddingBytesAfterNames +226VTableSize + PaddingBytesAfterVTable + VNameSize +227PaddingBytesAfterVNames;228}229230COMPILER_RT_VISIBILITY231void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer) {232BufferWriter->Write = lprofBufferWriter;233BufferWriter->WriterCtx = Buffer;234}235236COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {237ProfDataWriter BufferWriter;238initBufferWriter(&BufferWriter, Buffer);239return lprofWriteData(&BufferWriter, 0, 0);240}241242COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(243char *Buffer, const __llvm_profile_data *DataBegin,244const __llvm_profile_data *DataEnd, const char *CountersBegin,245const char *CountersEnd, const char *BitmapBegin, const char *BitmapEnd,246const char *NamesBegin, const char *NamesEnd) {247ProfDataWriter BufferWriter;248initBufferWriter(&BufferWriter, Buffer);249// Set virtual table arguments to NULL since they are not supported yet.250return lprofWriteDataImpl(251&BufferWriter, DataBegin, DataEnd, CountersBegin, CountersEnd,252BitmapBegin, BitmapEnd, /*VPDataReader=*/0, NamesBegin, NamesEnd,253/*VTableBegin=*/NULL, /*VTableEnd=*/NULL, /*VNamesBegin=*/NULL,254/*VNamesEnd=*/NULL, /*SkipNameDataWrite=*/0);255}256257258