[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
This commit is contained in:
Leonard Chan 2021-09-23 17:16:47 -07:00
parent c965fde7c2
commit c579c658cd
1 changed files with 20 additions and 4 deletions

View File

@ -19,6 +19,21 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
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); void __llvm_profile_set_file_object(FILE* File, int EnableMerge);
@ -42,10 +57,11 @@ int main(int argc, char** argv) {
if (Buf == MAP_FAILED) if (Buf == MAP_FAILED)
bail("mmap"); bail("mmap");
// We're trying to make the first CounterPtr invalid. __llvm_profile_header *Header = (__llvm_profile_header *)Buf;
// 11 64-bit words as header. __llvm_profile_data *SrcDataStart =
// CounterPtr is the third 64-bit word field. (__llvm_profile_data *)(Buf + sizeof(__llvm_profile_header) +
memset(&Buf[11 * 8 + 2 * 8], 0xAB, 8); Header->BinaryIdsSize);
memset(&SrcDataStart->CounterPtr, 0xAB, sizeof(SrcDataStart->CounterPtr));
if (munmap(Buf, FileSize)) if (munmap(Buf, FileSize))
bail("munmap"); bail("munmap");