Skip to content

Commit

Permalink
Support customizing saved registers
Browse files Browse the repository at this point in the history
  • Loading branch information
mimicji committed Nov 2, 2020
1 parent 166178c commit 323af82
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ debug: all
read_trace: read_trace.o $(LDLIBS)
ifeq ($(HAVE_LIBPEEKABOO_SO), 0)
@# Cannot find peekaboo installed. Static link!
$(CC) -o $@ $(strip $(CFLAGS) -L$(DIR_PEEKABOO) $^)
$(CC) -o $@ $(strip $(CFLAGS) $< $(patsubst -lpeekaboo,$(DIR_PEEKABOO)/libpeekaboo.a,$(LDLIBS)))
else
@# Dynamic link if libpeekaboo has been installed
$(CC) -o $@ $(strip $(CFLAGS) $^)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ peekaboo_dr: [DynamoRIO](https://github.com/DynamoRIO/dynamorio)
Install libpeekaboo:
```
cd libpeekaboo
make
make (DISABLE_SIMD=1 | DISABLE_FXSAVE=1 | ONLY_GPR=1)
sudo make install
```
APIs:
Expand Down
20 changes: 19 additions & 1 deletion libpeekaboo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,20 @@ LDIR ?= .
# Source and object files
ARCH_SRC = $(wildcard $(ARCH_DIR)/*.c)
ARCH_OBJ = $(patsubst %.c,%.o,$(ARCH_SRC))
ARCH_AMD64_CONF = $(ARCH_DIR)/amd64_conf.h
OBJ = $(patsubst %.c,%.o,$(wildcard *.c))
LIBOBJ = $(OBJ) $(ARCH_OBJ)

# Configure
ifeq ($(DISABLE_SIMD),)
endif
ifeq ($(DISABLE_FXSAVE),)
endif


# Targets and Recipes
.PHONY: all
all: static dynamic | installation_hint
all: static | dynamic installation_hint
ifneq ($(IS_DARWIN),0)
static: libpeekaboo.a
dynamic: libpeekaboo.dylib
Expand All @@ -86,6 +94,16 @@ debug: CFLAGS += -DDEBUG -g
debug: all

libpeekaboo.a: $(LIBOBJ)
ifneq ($(DISABLE_SIMD), 1)
$(shell echo "#define _STORE_SIMD" > $(ARCH_AMD64_CONF))
endif
ifneq ($(DISABLE_FXSAVE), 1)
$(shell echo "#define _STORE_FXSAVE" >> $(ARCH_AMD64_CONF))
endif
ifeq ($(ONLY_GPR), 1)
$(shell echo > $(ARCH_AMD64_CONF))
endif

ifneq ($(IS_DARWIN),0)
-$(CP) *.o $(ARCH_DIR)/
endif
Expand Down
10 changes: 10 additions & 0 deletions libpeekaboo/arch/amd64.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
#include <stddef.h>
#include <inttypes.h>

typedef struct storage_option_amd64{
uint32_t has_simd;
uint32_t has_fxsave;
} storage_option_amd64_t;

#include "../libpeekaboo.h"
#include "amd64_conf.h"

#define AMD64_NUM_SIMD_SLOTS 16

Expand Down Expand Up @@ -114,8 +120,12 @@ typedef struct {

typedef struct regfile_amd64{
amd64_cpu_gr_t gpr;
#ifdef _STORE_SIMD
amd64_cpu_simd_t simd;
#endif
#ifdef _STORE_FXSAVE
fxsave_area_t fxsave;
#endif
} regfile_amd64_t;

void amd64_regfile_pp(regfile_amd64_t *regfile);
Expand Down
35 changes: 35 additions & 0 deletions libpeekaboo/libpeekaboo.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,28 @@ void load_trace(char *dir_path, peekaboo_trace_t *trace_ptr)
trace_ptr->internal->version = meta.version;
fprintf(stderr, "libpeekaboo: Trace version: %d\n", meta.version);

if (trace_ptr->internal->version >= 4)
{
// New trace format that can customize which registers to store
if (trace_ptr->internal->arch == ARCH_AMD64)
{
trace_ptr->internal->storage_options.amd64.has_simd = meta.storage_options.amd64.has_simd;
trace_ptr->internal->storage_options.amd64.has_fxsave = meta.storage_options.amd64.has_fxsave;
fprintf(stderr, "Stored register: GPRs ");
if (trace_ptr->internal->storage_options.amd64.has_simd) fprintf(stderr, "SIMD ");
if (trace_ptr->internal->storage_options.amd64.has_fxsave) fprintf(stderr, "FXSAVE ");
fprintf(stderr, "\n");
}
else fprintf(stderr, "Not amd64: %d:%d\n", trace_ptr->internal->arch, ARCH_AMD64);

}
else
{
// Trace version lower than 003, stores everything
trace_ptr->internal->storage_options.amd64.has_simd = 1;
trace_ptr->internal->storage_options.amd64.has_fxsave = 1;
}

switch (meta.arch)
{
case ARCH_AMD64:
Expand Down Expand Up @@ -360,6 +382,19 @@ void write_metadata(peekaboo_trace_t *trace_ptr, enum ARCH arch, uint32_t versio
metadata_hdr_t metadata;
metadata.arch = arch;
metadata.version = version;
if (arch = ARCH_AMD64)
{
#ifdef _STORE_SIMD
metadata.storage_options.amd64.has_simd = 1;
#else
metadata.storage_options.amd64.has_simd = 0;
#endif
#ifdef _STORE_FXSAVE
metadata.storage_options.amd64.has_fxsave = 1;
#else
metadata.storage_options.amd64.has_fxsave = 0;
#endif
}
fwrite(&metadata, sizeof(metadata_hdr_t), 1, trace_ptr->metafile);
fflush(trace_ptr->metafile);
fclose(trace_ptr->metafile);
Expand Down
21 changes: 15 additions & 6 deletions libpeekaboo/libpeekaboo.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <dirent.h>

#define MAX_PATH (256)
#define LIBPEEKABOO_VER 003
#define LIBPEEKABOO_VER 004

#define PEEKABOO_DIE(...) {fprintf(stderr, __VA_ARGS__); exit(1);}

Expand Down Expand Up @@ -54,15 +54,27 @@ enum ARCH {
};
// end of type definitions

//------Supported archs declarations-----------------------
#include "arch/amd64.h"
#include "arch/aarch64.h"
#include "arch/x86.h"
//---------------------------------------------------------

// Misc functions
int create_folder(char *name, char *output, uint32_t max_size);
int create_trace_file(char *dir_path, char *filename, int size, FILE **output);
// end

//-----common structure declaration-----------------------
typedef union {
storage_option_amd64_t amd64;
uint64_t size;
}storage_options_t;

typedef struct {
uint32_t arch;
uint32_t version;
storage_options_t storage_options;
} metadata_hdr_t;

typedef struct insn_ref {
Expand All @@ -88,11 +100,6 @@ typedef struct {
} memfile_t;
//---------------------------------------------------------

//------Supported archs declarations-----------------------
#include "arch/amd64.h"
#include "arch/aarch64.h"
#include "arch/x86.h"
//---------------------------------------------------------

// peekaboo trace definition
typedef struct {
Expand All @@ -119,6 +126,8 @@ typedef struct {
memfile_t *memfile_buf;
memref_t *memref_buf;
uint32_t version;

storage_options_t storage_options;
} peekaboo_internal_t;

typedef struct {
Expand Down
5 changes: 5 additions & 0 deletions peekaboo_dr/peekaboo_dr.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
char *arch_str = "AMD64";
enum ARCH arch = ARCH_AMD64;
typedef regfile_amd64_t regfile_t;
const storage_option_amd64_t storage_option;
void copy_regfile(regfile_t *regfile_ptr, dr_mcontext_t *mc)
{
regfile_ptr->gpr.reg_rdi = mc->rdi;
Expand All @@ -62,10 +63,14 @@
//printf("czl:%p\n", regfile_ptr->gpr.reg_rip);

// here, we cast the simd structure into an array of uint256_t
#ifdef _STORE_SIMD
memcpy(&regfile_ptr->simd, mc->ymm, sizeof(regfile_ptr->simd.ymm0)*MCXT_NUM_SIMD_SLOTS);
#endif

// here we'll call fxsave, that saves into the fxsave area.
#ifdef _STORE_FXSAVE
proc_save_fpstate((byte *)&regfile_ptr->fxsave);
#endif
}
#else
char *arch_str = "X86";
Expand Down

0 comments on commit 323af82

Please sign in to comment.