1 //===- FuzzerShmem.h - shared memory interface ------------------*- C++ -* ===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_FUZZER_SHMEM_H
13 #define LLVM_FUZZER_SHMEM_H
19 #include "FuzzerDefs.h"
23 class SharedMemoryRegion {
25 bool Create(const char *Name);
26 bool Open(const char *Name);
27 bool Destroy(const char *Name);
28 uint8_t *GetData() { return Data; }
29 void PostServer() {Post(0);}
30 void WaitServer() {Wait(0);}
31 void PostClient() {Post(1);}
32 void WaitClient() {Wait(1);}
34 size_t WriteByteArray(const uint8_t *Bytes, size_t N) {
35 assert(N <= kShmemSize - sizeof(N));
36 memcpy(GetData(), &N, sizeof(N));
37 memcpy(GetData() + sizeof(N), Bytes, N);
38 assert(N == ReadByteArraySize());
41 size_t ReadByteArraySize() {
43 memcpy(&Res, GetData(), sizeof(Res));
46 uint8_t *GetByteArray() { return GetData() + sizeof(size_t); }
48 bool IsServer() const { return Data && IAmServer; }
49 bool IsClient() const { return Data && !IAmServer; }
53 static const size_t kShmemSize = 1 << 22;
55 std::string Path(const char *Name);
56 std::string SemName(const char *Name, int Idx);
61 uint8_t *Data = nullptr;
65 extern SharedMemoryRegion SMR;
69 #endif // LLVM_FUZZER_SHMEM_H