Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Commit

Permalink
Make GRR build on macOS. grrshot itself is not usable on macOS, though.
Browse files Browse the repository at this point in the history
  • Loading branch information
pgoodman committed Aug 29, 2019
1 parent 8413395 commit d6202d3
Show file tree
Hide file tree
Showing 18 changed files with 309 additions and 134 deletions.
11 changes: 5 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ set(GRANARY_SRC_FILES
"./granary/base/interrupt.cc"
"./granary/os/schedule.cc"
"./granary/os/process.cc"
"./granary/os/decree_user/syscall.cc"
"./granary/os/decree_user/snapshot.cc"
"./granary/os/decree_user/syscall.cc"
"./granary/os/snapshot.cc"
"./granary/os/file.cc"
"./granary/code/instruction.cc"
Expand Down Expand Up @@ -168,20 +168,20 @@ set(DUMP_SRC_FILES
"./granary/base/breakpoint.cc"
"./granary/base/interrupt.cc"
"./third_party/xxhash/xxhash.c"
)
granary/os/user.h)

set(PLAY_SRC_FILES
"${GRANARY_SRC_DIR}/play.cc"
"${GRANARY_SRC_FILES}"
)
granary/os/user.h)

set(SNAPSHOT_SRC_FILES
"${GRANARY_SRC_DIR}/snapshot.cc"
"${GRANARY_SRC_DIR}/granary/os/snapshot.cc"
"${GRANARY_SRC_DIR}/granary/os/decree_user/snapshot.cc"
"${GRANARY_SRC_DIR}/granary/base/breakpoint.cc"
"${GRANARY_SRC_DIR}/granary/base/interrupt.cc"
)
granary/os/user.h)

# Build the actual executables
add_executable(grrplay ${PLAY_SRC_FILES})
Expand All @@ -198,5 +198,4 @@ install(TARGETS grrplay grrshot grrcov
DESTINATION "${GRANARY_PREFIX_DIR}/bin"
PERMISSIONS OWNER_READ OWNER_EXECUTE
GROUP_READ GROUP_EXECUTE
WORLD_READ WORLD_EXECUTE
)
WORLD_READ WORLD_EXECUTE)
16 changes: 10 additions & 6 deletions granary/arch/x86/branch_tracer.S
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/* Copyright 2015 Peter Goodman, all rights reserved. */

#ifdef __APPLE__
# define SYMBOL(x) _ ## x
#else
# define SYMBOL(x) x
#endif

.file "granary/arch/x86/branch_tracer.S"
.intel_syntax noprefix
.text

.extern TraceBranchImpl
.extern SYMBOL(TraceBranchImpl)

// r15 os::Process32 * Process32 object.
// r14 index::Value Meta-data about this block.
Expand All @@ -16,9 +22,8 @@
// r8 Addr64 64-bit base of 32-bit address space.
//
.align 16
.globl TraceBranch
.type TraceBranch, @function
TraceBranch:
.globl SYMBOL(TraceBranch)
SYMBOL(TraceBranch):
.cfi_startproc

pushfq
Expand All @@ -41,7 +46,7 @@ TraceBranch:
mov edi, dword ptr [r15 + 52]
mov esi, r14d
mov edx, r10d
call TraceBranchImpl
call SYMBOL(TraceBranchImpl)

pop r15
pop r14
Expand All @@ -62,5 +67,4 @@ TraceBranch:

ret
.cfi_endproc
.size TraceBranch, .-TraceBranch
ud2
34 changes: 21 additions & 13 deletions granary/arch/x86/cache.S
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
/* Copyright 2015 Peter Goodman, all rights reserved. */

#ifdef __APPLE__
# define SYMBOL(x) _ ## x
#else
# define SYMBOL(x) x
#endif

.file "granary/arch/x86/cache.S"
.intel_syntax noprefix

.data
granary_stack_pointer:
SYMBOL(granary_stack_pointer):
.quad 0

.extern gInlineCache
.extern SYMBOL(gInlineCache)


// Used to return from the code cache with a bad block.
.align 16
.globl granary_bad_block
.type granary_bad_block, @function
granary_bad_block:
.globl SYMBOL(granary_bad_block)
SYMBOL(granary_bad_block):
.cfi_startproc
xor r14, r14
not r14
mov r11, [RIP + granary_stack_pointer]
mov r11, [RIP + SYMBOL(granary_stack_pointer)]
sub r11, 8
mov rsp, r11
ret
.cfi_endproc
.size _ZN7granary5cache4CallEPNS_2os9Process32EPh, .-_ZN7granary5cache4CallEPNS_2os9Process32EPh
ud2

// CachePC cache::Call(os::Process32 *process, CachePC block);
.align 16
.globl _ZN7granary5cache4CallEPNS_2os9Process32EPh;
.type _ZN7granary5cache4CallEPNS_2os9Process32EPh, @function
_ZN7granary5cache4CallEPNS_2os9Process32EPh:
.globl SYMBOL(_ZN7granary5cache4CallEPNS_2os9Process32EPh);
SYMBOL(_ZN7granary5cache4CallEPNS_2os9Process32EPh):
.cfi_startproc

/* Save the Linux ABI callee-saved regs */
Expand Down Expand Up @@ -65,7 +69,7 @@ _ZN7granary5cache4CallEPNS_2os9Process32EPh:
/* Don't restore the emulated pc */

// So that we can jump back into the top-level cache call.
mov [RIP + granary_stack_pointer], rsp
mov [RIP + SYMBOL(granary_stack_pointer)], rsp

/* Call into the block */
.Lenter_cache:
Expand All @@ -91,7 +95,12 @@ _ZN7granary5cache4CallEPNS_2os9Process32EPh:
mov r11d, dword ptr [r15 + 52]
and r11, 0x7ff
shl r11, 3 /* Scale by 8 bytes, the size of each entry. */
lea r11, [r11 + gInlineCache]

/* Get first probe point into the inline cache. */
push r12
lea r12, [RIP + SYMBOL(gInlineCache)]
lea r11, [r11 + r12]
pop r12

#define CHECK_CACHE(entry) \
cmp dword ptr [r11 + (entry * 8)], r10d ; \
Expand Down Expand Up @@ -145,5 +154,4 @@ _ZN7granary5cache4CallEPNS_2os9Process32EPh:

ret
.cfi_endproc
.size _ZN7granary5cache4CallEPNS_2os9Process32EPh, .-_ZN7granary5cache4CallEPNS_2os9Process32EPh
ud2
44 changes: 24 additions & 20 deletions granary/arch/x86/coverage.S
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
/* Copyright 2015 Peter Goodman, all rights reserved. */

#ifdef __APPLE__
# define SYMBOL(x) _ ## x
#else
# define SYMBOL(x) x
#endif

.file "granary/arch/x86/coverage.S"

.intel_syntax noprefix
.text

.extern gPathEntries
.extern gNextPathEntry
.extern UpdateCoverageSet
.extern gInputIndex
.extern SYMBOL(gPathEntries)
.extern SYMBOL(gNextPathEntry)
.extern SYMBOL(UpdateCoverageSet)
.extern SYMBOL(gInputIndex)

// r15 os::Process32 * Process32 object.
// r14 index::Value Meta-data about this block.
Expand All @@ -25,23 +31,22 @@
// Add an entry to the path coverage list. If the list get full, then
// call into UpdateCoverageSet to flush it and reset.
.align 16
.globl CoverPath
.type CoverPath, @function
CoverPath:
.globl SYMBOL(CoverPath)
SYMBOL(CoverPath):
.cfi_startproc
pushfq

// If we haven't read any input then it's not possible to have any
// input-dependent code coverage.
cmp qword ptr [RIP + gInputIndex], 0
jz done
cmp qword ptr [RIP + SYMBOL(gInputIndex)], 0
jz .Ldone

mov r13d, dword ptr [RIP + gNextPathEntry]
mov r13d, dword ptr [RIP + SYMBOL(gNextPathEntry)]
cmp r13, 4096 * 3 * 4
jz update_coverage_map
jz .Lupdate_coverage_map

add_entry:
lea r11, [RIP + gPathEntries]
.Ladd_entry:
lea r11, [RIP + SYMBOL(gPathEntries)]
mov r12d, dword ptr [r15 + 52] // Prev branch EIP

mov dword ptr [r11 + r13 + 0], r12d // Prev branch EIP.
Expand All @@ -50,13 +55,13 @@ add_entry:
mov dword ptr [r11 + r13 + 12], 1 // Exec count

// Move to the next path entry.
add dword ptr [RIP + gNextPathEntry], 4 * 4
add dword ptr [RIP + SYMBOL(gNextPathEntry)], 4 * 4

done:
.Ldone:
popfq
ret

update_coverage_map:
.Lupdate_coverage_map:
push rax
push rbx
push rcx
Expand All @@ -79,7 +84,7 @@ update_coverage_map:
// push [rsp]
// and rsp, -16

call UpdateCoverageSet
call SYMBOL(UpdateCoverageSet)

// pop rsp
pop r15
Expand All @@ -100,11 +105,10 @@ update_coverage_map:
pop rbx
pop rax

mov dword ptr [RIP + gNextPathEntry], 0
mov dword ptr [RIP + SYMBOL(gNextPathEntry)], 0
xor r13, r13
jmp add_entry
jmp .Ladd_entry

.cfi_endproc
.size CoverPath, .-CoverPath
ud2

8 changes: 8 additions & 0 deletions granary/arch/x86/patch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@

#include <gflags/gflags.h>

#ifndef MAP_32BIT
# define MAP_32BIT 0
#endif

#ifndef O_LARGEFILE
# define O_LARGEFILE 0
#endif

DECLARE_bool(persist);
DECLARE_string(persist_dir);

Expand Down
17 changes: 17 additions & 0 deletions granary/arch/x86/process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ bool Process32::DoTryRead(const uint8_t *ptr, uint8_t *val) const {
bool Process32::RecoverFromTryReadWrite(ucontext_t *context) const {
if (!fault_can_recover) return false;
fault_can_recover = false;
#ifdef __APPLE__
auto &pc = context->uc_mcontext->__ss.__rip;
#else
auto &pc = context->uc_mcontext.gregs[REG_RIP];
#endif
GRANARY_ASSERT(pc == (pc & ~15LL) && "Crash wasn't in a TryRead/TryWrite.");
pc += 16LL;
return true;
Expand All @@ -164,6 +168,18 @@ void Process32::InitRegs(const Snapshot32 *snapshot) {
}

void Process32::SynchronizeRegState(ucontext_t *context) {
#ifdef __APPLE__
regs.edi = static_cast<uint32_t>(context->uc_mcontext->__ss.__rdi);
regs.esi = static_cast<uint32_t>(context->uc_mcontext->__ss.__rsi);
regs.ebp = static_cast<uint32_t>(context->uc_mcontext->__ss.__rbp);
regs.ebx = static_cast<uint32_t>(context->uc_mcontext->__ss.__rbx);
regs.edx = static_cast<uint32_t>(context->uc_mcontext->__ss.__rdx);
regs.ecx = static_cast<uint32_t>(context->uc_mcontext->__ss.__rcx);
regs.eax = static_cast<uint32_t>(context->uc_mcontext->__ss.__rax);
regs.esp = static_cast<uint32_t>(context->uc_mcontext->__ss.__rsp);
regs.eip = static_cast<uint32_t>(context->uc_mcontext->__ss.__rip);
regs.eflags = static_cast<uint32_t>(context->uc_mcontext->__ss.__rflags);
#else
regs.edi = static_cast<uint32_t>(context->uc_mcontext.gregs[REG_RDI]);
regs.esi = static_cast<uint32_t>(context->uc_mcontext.gregs[REG_RSI]);
regs.ebp = static_cast<uint32_t>(context->uc_mcontext.gregs[REG_RBP]);
Expand All @@ -174,6 +190,7 @@ void Process32::SynchronizeRegState(ucontext_t *context) {
regs.esp = static_cast<uint32_t>(context->uc_mcontext.gregs[REG_R9]);
regs.eip = static_cast<uint32_t>(context->uc_mcontext.gregs[REG_R10]);
regs.eflags = static_cast<uint32_t>(context->uc_mcontext.gregs[REG_EFL]);
#endif
}

void Process32::RestoreFPUState(void) const {
Expand Down
12 changes: 8 additions & 4 deletions granary/arch/x86/syscall.S
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
/* Copyright 2015 Peter Goodman, all rights reserved. */

#ifdef __APPLE__
# define SYMBOL(x) _ ## x
#else
# define SYMBOL(x) x
#endif

.file "granary/arch/x86/syscall.S"
.intel_syntax noprefix
.text

// NextAppPC32 cache::Call(os::Process32 *process, CachePC block);
.align 16
.globl sys_sigreturn;
.type sys_sigreturn, @function
sys_sigreturn:
.globl SYMBOL(sys_sigreturn);
SYMBOL(sys_sigreturn):
.cfi_startproc
mov eax, 15 // `__NR_rt_sigreturn`.
syscall
.cfi_endproc
.size sigreturn, .-sigreturn
ud2
Loading

0 comments on commit d6202d3

Please sign in to comment.