forked from llvm-mirror/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libFuzzer] experimental support for 'equivalance fuzzing'
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@292646 91177308-0d34-0410-b5e6-96231b3b80d8
- Loading branch information
Showing
10 changed files
with
243 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//===- FuzzerShmem.h - shared memory interface ------------------*- C++ -* ===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// SharedMemoryRegion | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_FUZZER_SHMEM_H | ||
#define LLVM_FUZZER_SHMEM_H | ||
|
||
#include <algorithm> | ||
#include <cstring> | ||
#include <string> | ||
|
||
#include "FuzzerDefs.h" | ||
|
||
namespace fuzzer { | ||
|
||
class SharedMemoryRegion { | ||
public: | ||
bool Create(const char *Name, size_t Size); | ||
bool Open(const char *Name); | ||
bool Destroy(const char *Name); | ||
size_t GetSize() const { return Size; } | ||
uint8_t *GetData() { return Data; } | ||
void PostServer() {Post(0);} | ||
void WaitServer() {Wait(0);} | ||
void PostClient() {Post(1);} | ||
void WaitClient() {Wait(1);} | ||
|
||
size_t WriteByteArray(const uint8_t *Bytes, size_t N) { | ||
N = std::min(N, GetSize() - sizeof(N)); | ||
memcpy(GetData(), &N, sizeof(N)); | ||
memcpy(GetData() + sizeof(N), Bytes, N); | ||
assert(N == ReadByteArraySize()); | ||
return N; | ||
} | ||
size_t ReadByteArraySize() { | ||
size_t Res; | ||
memcpy(&Res, GetData(), sizeof(Res)); | ||
return Res; | ||
} | ||
uint8_t *GetByteArray() { return GetData() + sizeof(size_t); } | ||
|
||
bool IsServer() const { return Data && IAmServer; } | ||
bool IsClient() const { return Data && !IAmServer; } | ||
|
||
private: | ||
bool IAmServer; | ||
std::string Path(const char *Name); | ||
std::string SemName(const char *Name, int Idx); | ||
void Post(int Idx); | ||
void Wait(int Idx); | ||
|
||
bool Map(int fd); | ||
size_t Size = 0; | ||
uint8_t *Data = nullptr; | ||
void *Semaphore[2]; | ||
}; | ||
|
||
extern SharedMemoryRegion SMR; | ||
|
||
} // namespace fuzzer | ||
|
||
#endif // LLVM_FUZZER_SHMEM_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
//===- 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" | ||
#ifdef LIBFUZZER_POSIX | ||
|
||
#include "FuzzerIO.h" | ||
#include "FuzzerShmem.h" | ||
|
||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
#include <sys/mman.h> | ||
#include <semaphore.h> | ||
#include <stdio.h> | ||
#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) { | ||
Data = (uint8_t *)mmap(0, Size, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0); | ||
if (Data == (uint8_t*)-1) | ||
return false; | ||
return true; | ||
} | ||
|
||
bool SharedMemoryRegion::Create(const char *Name, size_t Size) { | ||
int fd = open(Path(Name).c_str(), O_CREAT | O_RDWR, 0777); | ||
if (fd < 0) return false; | ||
if (ftruncate(fd, Size) < 0) return false; | ||
this->Size = Size; | ||
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; | ||
Size = stat_res.st_size; | ||
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); | ||
if (sem_wait((sem_t*)Semaphore[Idx])) { | ||
Printf("ERROR: sem_wait failed\n"); | ||
exit(1); | ||
} | ||
} | ||
|
||
} // namespace fuzzer | ||
|
||
#endif // LIBFUZZER_POSIX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
RUN: LLVMFuzzer-EquivalenceATest -run_equivalence_server=EQUIV_TEST & export APID=$! | ||
RUN: not LLVMFuzzer-EquivalenceBTest -use_equivalence_server=EQUIV_TEST 2>&1 | FileCheck %s | ||
CHECK: ERROR: libFuzzer: equivalence-mismatch. Sizes: {{.*}}; offset 2 | ||
CHECK: SUMMARY: libFuzzer: equivalence-mismatch | ||
RUN: kill -9 $APID |