blob: c87407bb1d61f4e205cde4bb244fefe5cbe7e01b [file] [log] [blame]
Kostya Serebryany98d592c2017-01-20 20:57:07 +00001//===- FuzzerShmemPosix.cpp - Posix shared memory ---------------*- C++ -* ===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// SharedMemoryRegion
10//===----------------------------------------------------------------------===//
11#include "FuzzerDefs.h"
Marcos Pividori764b65c2017-01-20 22:49:13 +000012#if LIBFUZZER_POSIX
Kostya Serebryany98d592c2017-01-20 20:57:07 +000013
14#include "FuzzerIO.h"
15#include "FuzzerShmem.h"
16
17#include <sys/types.h>
18#include <sys/stat.h>
Kostya Serebryanyac2a6332017-01-28 18:56:05 +000019#include <errno.h>
Kostya Serebryany98d592c2017-01-20 20:57:07 +000020#include <fcntl.h>
21#include <sys/mman.h>
22#include <semaphore.h>
23#include <stdio.h>
24#include <unistd.h>
25
26namespace fuzzer {
27
28std::string SharedMemoryRegion::Path(const char *Name) {
29 return DirPlusFile(TmpDir(), Name);
30}
31
32std::string SharedMemoryRegion::SemName(const char *Name, int Idx) {
33 std::string Res(Name);
34 return Res + (char)('0' + Idx);
35}
36
37bool SharedMemoryRegion::Map(int fd) {
38 Data = (uint8_t *)mmap(0, Size, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
39 if (Data == (uint8_t*)-1)
40 return false;
41 return true;
42}
43
44bool SharedMemoryRegion::Create(const char *Name, size_t Size) {
45 int fd = open(Path(Name).c_str(), O_CREAT | O_RDWR, 0777);
46 if (fd < 0) return false;
47 if (ftruncate(fd, Size) < 0) return false;
48 this->Size = Size;
49 if (!Map(fd))
50 return false;
51 for (int i = 0; i < 2; i++) {
52 sem_unlink(SemName(Name, i).c_str());
53 Semaphore[i] = sem_open(SemName(Name, i).c_str(), O_CREAT, 0644, 0);
54 if (Semaphore[i] == (void *)-1)
55 return false;
56 }
57 IAmServer = true;
58 return true;
59}
60
61bool SharedMemoryRegion::Open(const char *Name) {
62 int fd = open(Path(Name).c_str(), O_RDWR);
63 if (fd < 0) return false;
64 struct stat stat_res;
65 if (0 != fstat(fd, &stat_res))
66 return false;
67 Size = stat_res.st_size;
68 if (!Map(fd))
69 return false;
70 for (int i = 0; i < 2; i++) {
71 Semaphore[i] = sem_open(SemName(Name, i).c_str(), 0);
72 if (Semaphore[i] == (void *)-1)
73 return false;
74 }
75 IAmServer = false;
76 return true;
77}
78
79bool SharedMemoryRegion::Destroy(const char *Name) {
80 return 0 == unlink(Path(Name).c_str());
81}
82
83void SharedMemoryRegion::Post(int Idx) {
84 assert(Idx == 0 || Idx == 1);
85 sem_post((sem_t*)Semaphore[Idx]);
86}
87
88void SharedMemoryRegion::Wait(int Idx) {
89 assert(Idx == 0 || Idx == 1);
Kostya Serebryany6d58dbb2017-01-27 22:41:30 +000090 for (int i = 0; i < 10 && sem_wait((sem_t*)Semaphore[Idx]); i++) {
91 // sem_wait may fail if interrupted by a signal.
92 sleep(i);
93 if (i)
94 Printf("%s: sem_wait[%d] failed %s\n", i < 9 ? "WARNING" : "ERROR", i,
95 strerror(errno));
96 if (i == 9) abort();
Kostya Serebryany98d592c2017-01-20 20:57:07 +000097 }
98}
99
100} // namespace fuzzer
101
102#endif // LIBFUZZER_POSIX