1// RUN: rm -f %t.profraw
2// RUN: touch %t.profraw
3// RUN: %clang_profgen -o %t %s
4// RUN: %t %t.profraw
5// RUN: %t %t.profraw modifyfile
6// RUN: cp %t.profraw %t.profraw.old
7// RUN: %t %t.profraw 2>&1 | FileCheck %s
8// RUN: diff %t.profraw %t.profraw.old
9// CHECK: Invalid profile data to merge
10
11#include <errno.h>
12#include <stdio.h>
13#include <stdint.h>
14#include <stdlib.h>
15#include <string.h>
16#include <sys/mman.h>
17#include <sys/stat.h>
18#include <sys/stat.h>
19#include <fcntl.h>
20#include <unistd.h>
21
22enum ValueKind {
23#define VALUE_PROF_KIND(Enumerator, Value, Descr) Enumerator = Value,
24#include "profile/InstrProfData.inc"
25};
26
27typedef struct __llvm_profile_header {
28#define INSTR_PROF_RAW_HEADER(Type, Name, Initializer) Type Name;
29#include "profile/InstrProfData.inc"
30} __llvm_profile_header;
31
32typedef void *IntPtrT;
33typedef struct __llvm_profile_data {
34#define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) Type Name;
35#include "profile/InstrProfData.inc"
36} __llvm_profile_data;
37
38void __llvm_profile_set_file_object(FILE* File, int EnableMerge);
39
40void bail(const char* str) {
41 fprintf(stderr, format: "%s %s\n", str, strerror(errno));
42 exit(status: 1);
43}
44
45int main(int argc, char** argv) {
46 if (argc == 3) {
47 int fd = open(file: argv[1], O_RDWR);
48 if (fd == -1)
49 bail(str: "open");
50
51 struct stat st;
52 if (stat(file: argv[1], buf: &st))
53 bail(str: "stat");
54 uint64_t FileSize = st.st_size;
55
56 char* Buf = (char *) mmap(NULL, len: FileSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd: fd, offset: 0);
57 if (Buf == MAP_FAILED)
58 bail(str: "mmap");
59
60 __llvm_profile_header *Header = (__llvm_profile_header *)Buf;
61 __llvm_profile_data *SrcDataStart =
62 (__llvm_profile_data *)(Buf + sizeof(__llvm_profile_header) +
63 Header->BinaryIdsSize);
64 memset(s: &SrcDataStart->CounterPtr, c: 0xAB, n: sizeof(SrcDataStart->CounterPtr));
65
66 if (munmap(addr: Buf, len: FileSize))
67 bail(str: "munmap");
68
69 if (close(fd: fd))
70 bail(str: "close");
71 } else {
72 FILE* f = fopen(filename: argv[1], modes: "r+b");
73 if (!f)
74 bail(str: "fopen");
75 __llvm_profile_set_file_object(File: f, EnableMerge: 1);
76 }
77}
78

source code of compiler-rt/test/profile/Linux/corrupted-profile.c