From c579c658cd42034449d4fa19f28b43f2082c0991 Mon Sep 17 00:00:00 2001 From: Leonard Chan Date: Thu, 23 Sep 2021 17:16:47 -0700 Subject: [PATCH] [compiler-rt][profile] Make corrupted-profile.c more robust This test specifically checks that profiles are not mergeable if there's a change in the CounterPtr in the profile header. The test manually changes CounterPtr by explicitly calling memset on some offset into the profile file. This test would fail if binary IDs were emitted because the offset calculation does not take into account the binary ID sizes. This patch updates the test to use types provided in profile/InstrProfData.inc to make it more resistant to profile layout changes. Differential Revision: https://reviews.llvm.org/D110277 --- compiler-rt/test/profile/Linux/corrupted-profile.c | 24 ++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/compiler-rt/test/profile/Linux/corrupted-profile.c b/compiler-rt/test/profile/Linux/corrupted-profile.c index cbc37ac..bc2ccdb 100644 --- a/compiler-rt/test/profile/Linux/corrupted-profile.c +++ b/compiler-rt/test/profile/Linux/corrupted-profile.c @@ -19,6 +19,21 @@ #include #include +enum ValueKind { +#define VALUE_PROF_KIND(Enumerator, Value, Descr) Enumerator = Value, +#include "profile/InstrProfData.inc" +}; + +typedef struct __llvm_profile_header { +#define INSTR_PROF_RAW_HEADER(Type, Name, Initializer) Type Name; +#include "profile/InstrProfData.inc" +} __llvm_profile_header; + +typedef void *IntPtrT; +typedef struct __llvm_profile_data { +#define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) Type Name; +#include "profile/InstrProfData.inc" +} __llvm_profile_data; void __llvm_profile_set_file_object(FILE* File, int EnableMerge); @@ -42,10 +57,11 @@ int main(int argc, char** argv) { if (Buf == MAP_FAILED) bail("mmap"); - // We're trying to make the first CounterPtr invalid. - // 11 64-bit words as header. - // CounterPtr is the third 64-bit word field. - memset(&Buf[11 * 8 + 2 * 8], 0xAB, 8); + __llvm_profile_header *Header = (__llvm_profile_header *)Buf; + __llvm_profile_data *SrcDataStart = + (__llvm_profile_data *)(Buf + sizeof(__llvm_profile_header) + + Header->BinaryIdsSize); + memset(&SrcDataStart->CounterPtr, 0xAB, sizeof(SrcDataStart->CounterPtr)); if (munmap(Buf, FileSize)) bail("munmap"); -- 2.7.4