CCTLib
Calling-context and data-object attribution library for Intel Pin
zerospy_client.cpp File Reference
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <atomic>
#include <malloc.h>
#include <iostream>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <sys/mman.h>
#include <sstream>
#include <functional>
#include <unordered_set>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <list>
#include "pin.H"
#include "pin_isa_compat.H"
#include "cctlib.H"
#include "shadow_memory.H"
#include <xmmintrin.h>
#include <immintrin.h>
#include "xed-interface.h"
#include "xed-common-hdrs.h"
#include <google/sparse_hash_map>
#include <google/dense_hash_map>
+ Include dependency graph for zerospy_client.cpp:

Go to the source code of this file.

Classes

struct  RedSpyThreadData
 
struct  RedLogs
 
struct  ApproxRedLogs
 
struct  UnrolledConjunction< start, end, incr >
 
struct  UnrolledConjunction< end, end, incr >
 
struct  UnrolledCount< start, end, step >
 
struct  UnrolledCount< end, end, step >
 
union  float_cast
 
union  double_cast
 
struct  UnrolledCountApprox< start, end, step >
 
struct  UnrolledCountApprox< end, end, step >
 
struct  UnrolledConjunctionApprox< start, end, incr >
 
struct  UnrolledConjunctionApprox< end, end, incr >
 
struct  ZeroSpyAnalysis< T, AccessLen, isApprox >
 
struct  LoadSpyInstrument
 
struct  RedundacyData
 
struct  ApproxRedundacyData
 

Macros

#define READ_ACTION   (0)
 
#define WRITE_ACTION   (0xff)
 
#define ONE_BYTE_READ_ACTION   (0)
 
#define TWO_BYTE_READ_ACTION   (0)
 
#define FOUR_BYTE_READ_ACTION   (0)
 
#define EIGHT_BYTE_READ_ACTION   (0)
 
#define ONE_BYTE_WRITE_ACTION   (0xff)
 
#define TWO_BYTE_WRITE_ACTION   (0xffff)
 
#define FOUR_BYTE_WRITE_ACTION   (0xffffffff)
 
#define EIGHT_BYTE_WRITE_ACTION   (0xffffffffffffffff)
 
#define IS_ACCESS_WITHIN_PAGE_BOUNDARY(accessAddr, accessLen)   (PAGE_OFFSET((accessAddr)) <= (PAGE_OFFSET_MASK - (accessLen)))
 
#define MAX_REDUNDANT_CONTEXTS_TO_LOG   (1000)
 
#define THREAD_MAX   (1024)
 
#define ENCODE_ADDRESS_AND_ACCESS_LEN(addr, len)   ((addr) | (((uint64_t)(len)) << 48))
 
#define DECODE_ADDRESS(addrAndLen)   ((addrAndLen) & ((1L << 48) - 1))
 
#define DECODE_ACCESS_LEN(addrAndLen)   ((addrAndLen) >> 48)
 
#define MAX_WRITE_OP_LENGTH   (512)
 
#define MAX_WRITE_OPS_IN_INS   (8)
 
#define MAX_REG_LENGTH   (64)
 
#define MAX_SIMD_LENGTH   (64)
 
#define MAX_SIMD_REGS   (32)
 
#define PAGE_MASK   (~0xfff)
 
#define GET_PAGE_INDEX(x)   ((x)&PAGE_MASK)
 
#define CACHELINE_MASK   (~63)
 
#define GET_CACHELINE_INDEX(x)   ((x)&CACHELINE_MASK)
 
#define DECODE_DEAD(data)   static_cast<uint8_t>(((data)&0xffffffffffffffff) >> 32)
 
#define DECODE_KILL(data)   (static_cast<ContextHandle_t>((data)&0x00000000ffffffff))
 
#define MAKE_CONTEXT_PAIR(a, b)   (((uint64_t)(a) << 32) | ((uint64_t)(b)))
 
#define delta   0.01
 
#define HANDLE_CASE(T, ACCESS_LEN, IS_APPROX)    INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)ZeroSpyAnalysis<T, (ACCESS_LEN), (IS_APPROX)>::CheckNByteValueAfterRead, IARG_MEMORYOP_EA, memOp, IARG_UINT32, opaqueHandle, IARG_THREAD_ID, IARG_INST_PTR, IARG_END)
 
#define HANDLE_LARGE()    INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)CheckAfterLargeRead, IARG_MEMORYOP_EA, memOp, IARG_MEMORYREAD_SIZE, IARG_UINT32, opaqueHandle, IARG_THREAD_ID, IARG_END)
 

Functions

RedSpyThreadDataClientGetTLS (const THREADID threadId)
 
static INT32 Usage ()
 
static void ClientInit (int argc, char *argv[])
 
static void AddToRedTable (uint64_t key, uint16_t value, uint32_t byteMap, uint16_t total, THREADID threadId) __attribute__((always_inline
 
static void AddToApproximateRedTable (uint64_t key, uint32_t byteMapMan, uint32_t byteMapExp, uint32_t byteMapSign, uint16_t total, uint16_t zeros, uint16_t nums, uint8_t size, THREADID threadId) __attribute__((always_inline
 
static bool IsOkToApproximate (xed_decoded_inst_t &xedd)
 
static bool IsFloatInstructionAndOkToApproximate (ADDRINT ip)
 
static bool IsFloatInstructionOld (ADDRINT ip)
 
static uint16_t FloatOperandSize (ADDRINT ip, uint32_t oper)
 
static VOID CheckAfterLargeRead (void *addr, UINT32 accessLen, uint32_t opaqueHandle, THREADID threadId)
 
static int GetNumReadOperandsInIns (INS ins, UINT32 &whichOp)
 
static bool INS_IsIgnorable (INS ins)
 
static VOID InstrumentInsCallback (INS ins, VOID *v, const uint32_t opaqueHandle)
 
VOID Update (uint32_t bytes, THREADID threadId)
 
static void InstrumentTrace (TRACE trace, void *f)
 
static bool RedundacyCompare (const struct RedundacyData &first, const struct RedundacyData &second)
 
static bool ApproxRedundacyCompare (const struct ApproxRedundacyData &first, const struct ApproxRedundacyData &second)
 
static void PrintRedundancyPairs (THREADID threadId)
 
static void PrintApproximationRedundancyPairs (THREADID threadId)
 
static VOID ImageUnload (IMG img, VOID *v)
 
static VOID ThreadFiniFunc (THREADID threadid, const CONTEXT *ctxt, INT32 code, VOID *v)
 
static VOID FiniFunc (INT32 code, VOID *v)
 
static void InitThreadData (RedSpyThreadData *tdata)
 
static VOID ThreadStart (THREADID threadid, CONTEXT *ctxt, INT32 flags, VOID *v)
 
uint64_t computeMetricVal (void *metric)
 
int main (int argc, char *argv[])
 

Variables

struct {
   char   dummy1 [128]
 
   xed_state_t   xedState
 
   char   dummy2 [128]
 
LoadSpyGlobals
 
int redload_metric_id = 0
 
int redload_approx_metric_id = 0
 
uint64_t grandTotBytesLoad
 
uint64_t grandTotBytesRedLoad
 
uint64_t grandTotBytesApproxRedLoad
 
static TLS_KEY client_tls_key
 
static RedSpyThreadDatagSingleThreadedTData
 
static FILE * gTraceFile
 
static const uint64_t READ_ACCESS_STATES [] = { 0, ONE_BYTE_READ_ACTION, TWO_BYTE_READ_ACTION, 0, FOUR_BYTE_READ_ACTION, 0, 0, 0, EIGHT_BYTE_READ_ACTION}
 
static const uint64_t WRITE_ACCESS_STATES [] = { 0, ONE_BYTE_WRITE_ACTION, TWO_BYTE_WRITE_ACTION, 0, FOUR_BYTE_WRITE_ACTION, 0, 0, 0, EIGHT_BYTE_WRITE_ACTION}
 
static const uint8_t OVERFLOW_CHECK [] = { 0, 0, 0, 1, 2, 3, 4, 5, 6}
 
static unordered_map< uint64_t, RedLogsRedMap [THREAD_MAX]
 
static unordered_map< uint64_t, ApproxRedLogsApproxRedMap [THREAD_MAX]
 
static void flatten
 

Macro Definition Documentation

◆ CACHELINE_MASK

#define CACHELINE_MASK   (~63)

Definition at line 76 of file zerospy_client.cpp.

◆ DECODE_ACCESS_LEN

#define DECODE_ACCESS_LEN (   addrAndLen)    ((addrAndLen) >> 48)

Definition at line 63 of file zerospy_client.cpp.

◆ DECODE_ADDRESS

#define DECODE_ADDRESS (   addrAndLen)    ((addrAndLen) & ((1L << 48) - 1))

Definition at line 62 of file zerospy_client.cpp.

◆ DECODE_DEAD

#define DECODE_DEAD (   data)    static_cast<uint8_t>(((data)&0xffffffffffffffff) >> 32)

Definition at line 90 of file zerospy_client.cpp.

◆ DECODE_KILL

#define DECODE_KILL (   data)    (static_cast<ContextHandle_t>((data)&0x00000000ffffffff))

Definition at line 91 of file zerospy_client.cpp.

◆ delta

#define delta   0.01

Definition at line 96 of file zerospy_client.cpp.

◆ EIGHT_BYTE_READ_ACTION

#define EIGHT_BYTE_READ_ACTION   (0)

Definition at line 48 of file zerospy_client.cpp.

◆ EIGHT_BYTE_WRITE_ACTION

#define EIGHT_BYTE_WRITE_ACTION   (0xffffffffffffffff)

Definition at line 53 of file zerospy_client.cpp.

◆ ENCODE_ADDRESS_AND_ACCESS_LEN

#define ENCODE_ADDRESS_AND_ACCESS_LEN (   addr,
  len 
)    ((addr) | (((uint64_t)(len)) << 48))

Definition at line 61 of file zerospy_client.cpp.

◆ FOUR_BYTE_READ_ACTION

#define FOUR_BYTE_READ_ACTION   (0)

Definition at line 47 of file zerospy_client.cpp.

◆ FOUR_BYTE_WRITE_ACTION

#define FOUR_BYTE_WRITE_ACTION   (0xffffffff)

Definition at line 52 of file zerospy_client.cpp.

◆ GET_CACHELINE_INDEX

#define GET_CACHELINE_INDEX (   x)    ((x)&CACHELINE_MASK)

Definition at line 77 of file zerospy_client.cpp.

◆ GET_PAGE_INDEX

#define GET_PAGE_INDEX (   x)    ((x)&PAGE_MASK)

Definition at line 74 of file zerospy_client.cpp.

◆ HANDLE_CASE

#define HANDLE_CASE (   T,
  ACCESS_LEN,
  IS_APPROX 
)     INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)ZeroSpyAnalysis<T, (ACCESS_LEN), (IS_APPROX)>::CheckNByteValueAfterRead, IARG_MEMORYOP_EA, memOp, IARG_UINT32, opaqueHandle, IARG_THREAD_ID, IARG_INST_PTR, IARG_END)

Definition at line 1094 of file zerospy_client.cpp.

◆ HANDLE_LARGE

#define HANDLE_LARGE ( )     INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)CheckAfterLargeRead, IARG_MEMORYOP_EA, memOp, IARG_MEMORYREAD_SIZE, IARG_UINT32, opaqueHandle, IARG_THREAD_ID, IARG_END)

Definition at line 1100 of file zerospy_client.cpp.

◆ IS_ACCESS_WITHIN_PAGE_BOUNDARY

#define IS_ACCESS_WITHIN_PAGE_BOUNDARY (   accessAddr,
  accessLen 
)    (PAGE_OFFSET((accessAddr)) <= (PAGE_OFFSET_MASK - (accessLen)))

Definition at line 55 of file zerospy_client.cpp.

◆ MAKE_CONTEXT_PAIR

#define MAKE_CONTEXT_PAIR (   a,
 
)    (((uint64_t)(a) << 32) | ((uint64_t)(b)))

Definition at line 94 of file zerospy_client.cpp.

◆ MAX_REDUNDANT_CONTEXTS_TO_LOG

#define MAX_REDUNDANT_CONTEXTS_TO_LOG   (1000)

Definition at line 58 of file zerospy_client.cpp.

◆ MAX_REG_LENGTH

#define MAX_REG_LENGTH   (64)

Definition at line 68 of file zerospy_client.cpp.

◆ MAX_SIMD_LENGTH

#define MAX_SIMD_LENGTH   (64)

Definition at line 70 of file zerospy_client.cpp.

◆ MAX_SIMD_REGS

#define MAX_SIMD_REGS   (32)

Definition at line 71 of file zerospy_client.cpp.

◆ MAX_WRITE_OP_LENGTH

#define MAX_WRITE_OP_LENGTH   (512)

Definition at line 66 of file zerospy_client.cpp.

◆ MAX_WRITE_OPS_IN_INS

#define MAX_WRITE_OPS_IN_INS   (8)

Definition at line 67 of file zerospy_client.cpp.

◆ ONE_BYTE_READ_ACTION

#define ONE_BYTE_READ_ACTION   (0)

Definition at line 45 of file zerospy_client.cpp.

◆ ONE_BYTE_WRITE_ACTION

#define ONE_BYTE_WRITE_ACTION   (0xff)

Definition at line 50 of file zerospy_client.cpp.

◆ PAGE_MASK

#define PAGE_MASK   (~0xfff)

Definition at line 73 of file zerospy_client.cpp.

◆ READ_ACTION

#define READ_ACTION   (0)

Definition at line 42 of file zerospy_client.cpp.

◆ THREAD_MAX

#define THREAD_MAX   (1024)

Definition at line 59 of file zerospy_client.cpp.

◆ TWO_BYTE_READ_ACTION

#define TWO_BYTE_READ_ACTION   (0)

Definition at line 46 of file zerospy_client.cpp.

◆ TWO_BYTE_WRITE_ACTION

#define TWO_BYTE_WRITE_ACTION   (0xffff)

Definition at line 51 of file zerospy_client.cpp.

◆ WRITE_ACTION

#define WRITE_ACTION   (0xff)

Definition at line 43 of file zerospy_client.cpp.

Function Documentation

◆ AddToApproximateRedTable()

static void AddToApproximateRedTable ( uint64_t  key,
uint32_t  byteMapMan,
uint32_t  byteMapExp,
uint32_t  byteMapSign,
uint16_t  total,
uint16_t  zeros,
uint16_t  nums,
uint8_t  size,
THREADID  threadId 
)
inlinestatic

◆ AddToRedTable()

static void AddToRedTable ( uint64_t  key,
uint16_t  value,
uint32_t  byteMap,
uint16_t  total,
THREADID  threadId 
)
inlinestatic

◆ ApproxRedundacyCompare()

static bool ApproxRedundacyCompare ( const struct ApproxRedundacyData first,
const struct ApproxRedundacyData second 
)
inlinestatic

Definition at line 1546 of file zerospy_client.cpp.

References ApproxRedundacyData::all0freq.

Referenced by PrintApproximationRedundancyPairs().

◆ CheckAfterLargeRead()

static VOID CheckAfterLargeRead ( void *  addr,
UINT32  accessLen,
uint32_t  opaqueHandle,
THREADID  threadId 
)
inlinestatic

◆ ClientGetTLS()

RedSpyThreadData* ClientGetTLS ( const THREADID  threadId)
inline

◆ ClientInit()

static void ClientInit ( int  argc,
char *  argv[] 
)
static

Definition at line 175 of file zerospy_client.cpp.

References gTraceFile, LoadSpyGlobals, and MAX_FILE_PATH.

Referenced by main().

◆ computeMetricVal()

uint64_t computeMetricVal ( void *  metric)

Definition at line 1991 of file zerospy_client.cpp.

References PinCCTLib::metric.

◆ FiniFunc()

static VOID FiniFunc ( INT32  code,
VOID *  v 
)
static

◆ FloatOperandSize()

static uint16_t FloatOperandSize ( ADDRINT  ip,
uint32_t  oper 
)
inlinestatic

Definition at line 655 of file zerospy_client.cpp.

References ip, and LoadSpyGlobals.

Referenced by LoadSpyInstrument::if().

◆ GetNumReadOperandsInIns()

static int GetNumReadOperandsInIns ( INS  ins,
UINT32 &  whichOp 
)
static

Definition at line 1105 of file zerospy_client.cpp.

Referenced by InstrumentInsCallback().

◆ ImageUnload()

static VOID ImageUnload ( IMG  img,
VOID *  v 
)
static

◆ InitThreadData()

static void InitThreadData ( RedSpyThreadData tdata)
static

◆ INS_IsIgnorable()

static bool INS_IsIgnorable ( INS  ins)
inlinestatic

Definition at line 1209 of file zerospy_client.cpp.

References INS_IsMaskedJump().

Referenced by InstrumentInsCallback(), and InstrumentTrace().

◆ InstrumentInsCallback()

static VOID InstrumentInsCallback ( INS  ins,
VOID *  v,
const uint32_t  opaqueHandle 
)
static

Definition at line 1233 of file zerospy_client.cpp.

References GetNumReadOperandsInIns(), and INS_IsIgnorable().

Referenced by main().

◆ InstrumentTrace()

static void InstrumentTrace ( TRACE  trace,
void *  f 
)
static

Definition at line 1358 of file zerospy_client.cpp.

References INS_IsIgnorable(), INS_MemoryReadSize(), and Update().

Referenced by main().

◆ IsFloatInstructionAndOkToApproximate()

static bool IsFloatInstructionAndOkToApproximate ( ADDRINT  ip)
inlinestatic

Definition at line 376 of file zerospy_client.cpp.

References ip, IsOkToApproximate(), and LoadSpyGlobals.

◆ IsFloatInstructionOld()

static bool IsFloatInstructionOld ( ADDRINT  ip)
inlinestatic

Definition at line 440 of file zerospy_client.cpp.

References ip, and LoadSpyGlobals.

◆ IsOkToApproximate()

static bool IsOkToApproximate ( xed_decoded_inst_t &  xedd)
inlinestatic

Definition at line 358 of file zerospy_client.cpp.

Referenced by IsFloatInstructionAndOkToApproximate().

◆ main()

◆ PrintApproximationRedundancyPairs()

◆ PrintRedundancyPairs()

◆ RedundacyCompare()

static bool RedundacyCompare ( const struct RedundacyData first,
const struct RedundacyData second 
)
inlinestatic

Definition at line 1543 of file zerospy_client.cpp.

References RedundacyData::frequency.

Referenced by PrintRedundancyPairs().

◆ ThreadFiniFunc()

static VOID ThreadFiniFunc ( THREADID  threadid,
const CONTEXT *  ctxt,
INT32  code,
VOID *  v 
)
static

Definition at line 1929 of file zerospy_client.cpp.

References ClientGetTLS(), and grandTotBytesLoad.

Referenced by main().

◆ ThreadStart()

static VOID ThreadStart ( THREADID  threadid,
CONTEXT *  ctxt,
INT32  flags,
VOID *  v 
)
static

Definition at line 1976 of file zerospy_client.cpp.

References ApproxRedMap, client_tls_key, gSingleThreadedTData, InitThreadData(), and RedMap.

Referenced by main().

◆ Update()

VOID Update ( uint32_t  bytes,
THREADID  threadId 
)
inline

Definition at line 1346 of file zerospy_client.cpp.

References RedSpyThreadData::bytesLoad, and ClientGetTLS().

Referenced by InstrumentTrace().

◆ Usage()

static INT32 Usage ( )
static

Definition at line 166 of file zerospy_client.cpp.

Referenced by main().

Variable Documentation

◆ ApproxRedMap

unordered_map<uint64_t, ApproxRedLogs> ApproxRedMap[THREAD_MAX]
static

◆ client_tls_key

TLS_KEY client_tls_key
static

Definition at line 152 of file zerospy_client.cpp.

Referenced by ClientGetTLS(), main(), and ThreadStart().

◆ dummy1

char dummy1[128]

Definition at line 105 of file zerospy_client.cpp.

◆ dummy2

char dummy2[128]

Definition at line 107 of file zerospy_client.cpp.

◆ flatten

void flatten

Definition at line 279 of file zerospy_client.cpp.

◆ grandTotBytesApproxRedLoad

uint64_t grandTotBytesApproxRedLoad

Definition at line 147 of file zerospy_client.cpp.

Referenced by FiniFunc(), and PrintApproximationRedundancyPairs().

◆ grandTotBytesLoad

uint64_t grandTotBytesLoad

Definition at line 145 of file zerospy_client.cpp.

Referenced by FiniFunc(), and ThreadFiniFunc().

◆ grandTotBytesRedLoad

uint64_t grandTotBytesRedLoad

Definition at line 146 of file zerospy_client.cpp.

Referenced by FiniFunc(), and PrintRedundancyPairs().

◆ gSingleThreadedTData

RedSpyThreadData* gSingleThreadedTData
static

Definition at line 153 of file zerospy_client.cpp.

Referenced by ClientGetTLS(), and ThreadStart().

◆ gTraceFile

FILE* gTraceFile
static

◆ 

◆ OVERFLOW_CHECK

const uint8_t OVERFLOW_CHECK[] = { 0, 0, 0, 1, 2, 3, 4, 5, 6}
static

Definition at line 207 of file zerospy_client.cpp.

◆ READ_ACCESS_STATES

const uint64_t READ_ACCESS_STATES[] = { 0, ONE_BYTE_READ_ACTION, TWO_BYTE_READ_ACTION, 0, FOUR_BYTE_READ_ACTION, 0, 0, 0, EIGHT_BYTE_READ_ACTION}
static

Definition at line 205 of file zerospy_client.cpp.

◆ redload_approx_metric_id

int redload_approx_metric_id = 0

Definition at line 142 of file zerospy_client.cpp.

◆ redload_metric_id

int redload_metric_id = 0

Definition at line 141 of file zerospy_client.cpp.

◆ RedMap

unordered_map<uint64_t, RedLogs> RedMap[THREAD_MAX]
static

◆ WRITE_ACCESS_STATES

const uint64_t WRITE_ACCESS_STATES[] = { 0, ONE_BYTE_WRITE_ACTION, TWO_BYTE_WRITE_ACTION, 0, FOUR_BYTE_WRITE_ACTION, 0, 0, 0, EIGHT_BYTE_WRITE_ACTION}
static

Definition at line 206 of file zerospy_client.cpp.

◆ xedState

xed_state_t xedState

Definition at line 106 of file zerospy_client.cpp.