2017-01-20 21:57:07 +01:00
|
|
|
//===- FuzzerShmemPosix.cpp - Posix shared memory ---------------*- C++ -* ===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SharedMemoryRegion
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "FuzzerDefs.h"
|
2017-01-20 23:49:13 +01:00
|
|
|
#if LIBFUZZER_POSIX
|
2017-01-20 21:57:07 +01:00
|
|
|
|
|
|
|
#include "FuzzerIO.h"
|
|
|
|
#include "FuzzerShmem.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2017-01-28 19:56:05 +01:00
|
|
|
#include <errno.h>
|
2017-01-20 21:57:07 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <stdio.h>
|
2017-02-03 21:26:44 +01:00
|
|
|
#include <stdlib.h>
|
2017-01-20 21:57:07 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace fuzzer {
|
|
|
|
|
|
|
|
std::string SharedMemoryRegion::Path(const char *Name) {
|
|
|
|
return DirPlusFile(TmpDir(), Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string SharedMemoryRegion::SemName(const char *Name, int Idx) {
|
|
|
|
std::string Res(Name);
|
|
|
|
return Res + (char)('0' + Idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SharedMemoryRegion::Map(int fd) {
|
2017-02-01 01:07:47 +01:00
|
|
|
Data =
|
|
|
|
(uint8_t *)mmap(0, kShmemSize, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
|
2017-01-20 21:57:07 +01:00
|
|
|
if (Data == (uint8_t*)-1)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-01 01:07:47 +01:00
|
|
|
bool SharedMemoryRegion::Create(const char *Name) {
|
2017-01-20 21:57:07 +01:00
|
|
|
int fd = open(Path(Name).c_str(), O_CREAT | O_RDWR, 0777);
|
|
|
|
if (fd < 0) return false;
|
2017-02-01 01:07:47 +01:00
|
|
|
if (ftruncate(fd, kShmemSize) < 0) return false;
|
2017-01-20 21:57:07 +01:00
|
|
|
if (!Map(fd))
|
|
|
|
return false;
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
sem_unlink(SemName(Name, i).c_str());
|
|
|
|
Semaphore[i] = sem_open(SemName(Name, i).c_str(), O_CREAT, 0644, 0);
|
|
|
|
if (Semaphore[i] == (void *)-1)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
IAmServer = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SharedMemoryRegion::Open(const char *Name) {
|
|
|
|
int fd = open(Path(Name).c_str(), O_RDWR);
|
|
|
|
if (fd < 0) return false;
|
|
|
|
struct stat stat_res;
|
|
|
|
if (0 != fstat(fd, &stat_res))
|
|
|
|
return false;
|
2017-02-01 01:07:47 +01:00
|
|
|
assert(stat_res.st_size == kShmemSize);
|
2017-01-20 21:57:07 +01:00
|
|
|
if (!Map(fd))
|
|
|
|
return false;
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
Semaphore[i] = sem_open(SemName(Name, i).c_str(), 0);
|
|
|
|
if (Semaphore[i] == (void *)-1)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
IAmServer = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SharedMemoryRegion::Destroy(const char *Name) {
|
|
|
|
return 0 == unlink(Path(Name).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedMemoryRegion::Post(int Idx) {
|
|
|
|
assert(Idx == 0 || Idx == 1);
|
|
|
|
sem_post((sem_t*)Semaphore[Idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedMemoryRegion::Wait(int Idx) {
|
|
|
|
assert(Idx == 0 || Idx == 1);
|
2017-01-27 23:41:30 +01:00
|
|
|
for (int i = 0; i < 10 && sem_wait((sem_t*)Semaphore[Idx]); i++) {
|
|
|
|
// sem_wait may fail if interrupted by a signal.
|
|
|
|
sleep(i);
|
|
|
|
if (i)
|
|
|
|
Printf("%s: sem_wait[%d] failed %s\n", i < 9 ? "WARNING" : "ERROR", i,
|
|
|
|
strerror(errno));
|
|
|
|
if (i == 9) abort();
|
2017-01-20 21:57:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzzer
|
|
|
|
|
|
|
|
#endif // LIBFUZZER_POSIX
|