Newer
Older
clc5q
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
* SMPDBInterface.h - <see below>.
*
* Copyright (c) 2000, 2001, 2010 - University of Virginia
*
* This file is part of the Memory Error Detection System (MEDS) infrastructure.
* This file may be used and modified for non-commercial purposes as long as
* all copyright, permission, and nonwarranty notices are preserved.
* Redistribution is prohibited without prior written consent from the University
* of Virginia.
*
* Please contact the authors for restrictions applying to commercial use.
*
* THIS SOURCE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Author: University of Virginia
* e-mail: jwd@virginia.com
* URL : http://www.cs.virginia.edu/
*
* Additional copyrights 2010, 2011 by Zephyr Software LLC
* e-mail: {clc,jwd}@zephyr-software.com
* URL : http://www.zephyr-software.com/
*
*/
#ifndef SMPDBINTERFACE_H
#define SMPDBINTERFACE_H 1
// SMPDBInterface.h
//
// This header defines the interfaces needed for obtaining program information
// from a disassembler or other program database, e.g. IDA Pro, the UVa IRDB,
// another disassembler, etc.
//
#include <memory>
clc5q
committed
#include <string>
#include <utility>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <cstddef>
#include <cstdint>
clc5q
committed
#if 0
clc5q
committed
#include <ida.hpp>
#include <funcs.hpp>
#include <frame.hpp>
#endif
clc5q
committed
#ifndef STARS_IRDB_INTERFACE
#ifndef STARS_IDA_INTERFACE
#warning No INTERFACE symbol defined: defaulting to STARS_IDA_INTERFACE
#define STARS_IDA_INTERFACE
#endif
#endif
#ifdef STARS_IDA_INTERFACE
#ifdef STARS_IRDB_INTERFACE
#error Cannot define both STARS_IDA_INTERFACE and STARS_IRDB_INTERFACE
#endif
#else
#ifndef STARS_IRDB_INTERFACE
#error Must define either STARS_IDA_INTERFACE or STARS_IRDB_INTERFACE
#endif
#endif
#include "interfaces/STARSTypes.h"
// A maximum string length for use when SMP needs to use less space than
// the IDA Pro MAXSTR, which is 1024 at present.
#define MAXSMPSTR 256
// Use shrink to fit C++ STL idiom to reduce memory wastage?
#define SMP_SHRINK_TO_FIT 1
// Pseudo-addresses that signify special cases in STARS, as opposed to actual addresses.
// By using the IDA Pro BADADDR constant, we can automatically adjust for 32/64-bit systems.
#define STARS_SSA_MARKER_PSEUDO_ID ((STARS_ea_t) BADADDR - 1)
#define STARS_LIVEIN_PSEUDO_ID ((STARS_ea_t) BADADDR - 2)
// All actual instructions should be on addresses below STARS_PSEUDO_ID_MIN
// Between STARS_PSEUDO_ID_MIN and STARS_PSEUDO_BLOCKNUM_MAX can be basic block numbers as fake addresses
#define STARS_BLOCKNUM_MASK 0xffff
#define STARS_PSEUDO_ID_MIN ((STARS_ea_t) BADADDR - STARS_BLOCKNUM_MASK)
#define STARS_PSEUDO_BLOCKNUM_MAX ((STARS_ea_t) BADADDR - 3)
#define STARS_IsLiveInPseudoID(addr) (STARS_LIVEIN_PSEUDO_ID == ((STARS_ea_t) addr))
#define STARS_IsSSAMarkerPseudoID(addr) (STARS_SSA_MARKER_PSEUDO_ID == ((STARS_ea_t) addr))
#define STARS_IsBlockNumPseudoID(addr) ((STARS_PSEUDO_ID_MIN <= ((STARS_ea_t) addr)) && (STARS_PSEUDO_BLOCKNUM_MAX >= ((STARS_ea_t) addr)))
#define STARS_GetBlockNumFromPseudoID(addr) (((STARS_ea_t) addr) & STARS_BLOCKNUM_MASK)
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Indentation level when emitting SPARK Ada translation of the RTLs.
extern unsigned short STARS_SPARK_IndentCount;
// Counters for analyzing memory use for allocated but unused capacity in vectors.
extern unsigned long UnusedStructCount; // various structs
extern unsigned long UnusedIntCount; // int, ea_t, ptr, other 4-byte objects
// Counters for dead metadata analysis.
extern unsigned long DeadMetadataCount;
extern unsigned long LiveMetadataCount;
// Counters for indirect jump resolution.
extern unsigned long ResolvedIndirectJumpCount;
extern unsigned long UnresolvedIndirectJumpCount;
// Counters for measuring SCCP success in finding constant DEFs.
extern unsigned long ConstantDEFCount;
extern unsigned long AlwaysTakenBranchCount;
extern unsigned long NeverTakenBranchCount;
// Counters for accessing less than machine register width.
extern unsigned long SubwordRegCount;
extern unsigned long SubwordMemCount;
extern unsigned long SubwordAddressRegCount;
extern unsigned long SPARKOperandCount; // total operands printed
// Counters for numeric error annotations cases.
#define SMP_MEASURE_NUMERIC_ANNOTATIONS 0
#if SMP_MEASURE_NUMERIC_ANNOTATIONS
extern unsigned long NumericAnnotationsCount12; // cases 1 and 2
extern unsigned long NumericAnnotationsCount3; // case 3
extern unsigned long TruncationAnnotationsCount; // case 4
extern unsigned long SignednessWithoutTruncationCount; // case 5
extern unsigned long LeaInstOverflowCount; // case 6
extern unsigned long WidthDoublingTruncationCount; // case 7
extern unsigned long BenignOverflowInstCount;
extern unsigned long BenignOverflowDefCount;
extern unsigned long SuppressStackPtrOverflowCount;
extern unsigned long SuppressLiveFlagsOverflowCount;
extern unsigned long LiveMultiplyBitsCount;
extern unsigned long BenignTruncationCount;
extern unsigned long SuppressTruncationRegPiecesAllUsed;
extern unsigned long SuppressSignednessOnTruncation;
#endif
#define SMP_COUNT_MEMORY_ALLOCATIONS 0
#if SMP_COUNT_MEMORY_ALLOCATIONS
// Counters for analyzing memory use for allocated and used objects.
extern unsigned long SMPInstCount;
extern unsigned long SMPBlockCount;
extern unsigned long SMPDefUseChainCount;
extern unsigned long SMPFuncCount;
extern unsigned long SMPGlobalVarCount;
extern unsigned long SMPLocalVarCount;
extern unsigned long SMPInstBytes;
extern unsigned long SMPDefUseChainBytes;
#define SMP_DU_ADDR_SIZE sizeof(STARS_ea_t)
#endif
#define STARS_SCCP_GATHER_STATISTICS 1
#if STARS_SCCP_GATHER_STATISTICS
// Counters for analyzing Sparse Conditional Constant Propagation effectiveness.
extern unsigned long SCCPFuncsWithArgWriteCount;
extern unsigned long SCCPFuncsWithConstantArgWriteCount;
extern unsigned long SCCPOutgoingArgWriteCount;
extern unsigned long SCCPConstantOutgoingArgWriteCount;
#endif
// Counter for max # of basic blocks seen in one function.
extern unsigned long STARS_MaxBlockCount;
// strings for printing ZST_SysCallType
extern const char *CallTypeNames[4];
clc5q
committed
// Need instruction xref info from IRDB
// Need redefinitions for:
// Need to deal with shared chunks
#ifdef STARS_IDA_INTERFACE
#include <pro.h>
#include <ua.hpp>
#include <fpro.h>
#include <xref.hpp>
#include <interfaces/abstract/STARSInterface.h>
#include <interfaces/idapro/STARSFunction.h>
// Translate RTLs to SPARK-Ada?
#define ZST_EMIT_SPARK_ADA_TRANSLATION 0
bool SMPGetCmd(STARS_ea_t InstAddr, insn_t &SMPcmd, uint32_t &SMPfeatures);
clc5q
committed
// Globals, typedefs and macros for STARS_IDA_INTERFACE only
#define SMP_getseg(addr) (global_stars_interface->getseg(addr))
#define SMP_getnseg(index) (global_stars_interface->getnseg(index))
#define SMP_get_segm_qty() (global_stars_interface->get_segm_qty())
#define SMP_get_first_seg() (SMP_getnseg(0)) /* get the first segment a diff way */
#define SMP_get_next_seg(addr) (global_stars_interface->get_next_seg(addr))
#define SMP_get_segm_name(seg, name, size) (seg->GetSegmentName(name, size))
#define SMP_get_func_qty() (global_stars_interface->get_func_qty())
#define SMP_getn_func(index) (global_stars_interface->getn_func(index))
#define SMP_get_func(addr) (global_stars_interface->get_func(addr))
#define SMP_get_func_name(addr, NameBuf, NameBufSize) (global_stars_interface->get_func_name(addr,NameBuf,NameBufSize))
#define SMP_get_spd(ptr_to_func, ea) (ptr_to_func->get_spd(ea))
#define SMP_AuditTailChunkOwnership() (global_stars_interface->AuditTailChunkOwnership())
#define SMP_AuditCodeTargets() (global_stars_interface->AuditCodeTargets())
clc5q
committed
#define SMP_get_item_end(addr) get_item_end(addr)
#define SMP_getFlags(addr) getFlags(addr)
#define SMP_msg(...) msg(__VA_ARGS__)
#define SMP_strncat(str1, str2, len) qstrncat(str1, str2, len)
#define SMP_strncpy(str1, str2, len) qstrncpy(str1, str2, len)
#define SMP_snprintf(...) qsnprintf(__VA_ARGS__)
#define SMP_fprintf(...) qfprintf(__VA_ARGS__)
clc5q
committed
#define SMP_fopen(name, mode) qfopen(name, mode)
#define SMP_feof(file) feof(file)
#define SMP_fclose(file) qfclose(file)
#define SMP_get_true_name(from, ea, buf, bufsize) get_true_name(from, ea, buf, bufsize)
#define SMP_next_head(ea, maxea) next_head(ea, maxea)
#define SMP_get_flags_novalue(ea) get_flags_novalue(ea)
#if 0
#define SMP_get_frame(ptr_to_func_t) get_frame((func_t*)(ptr_to_func_t))
#else
#define SMP_get_frame(ptr_to_func_t) get_frame((func_t*)*dynamic_cast<STARS_IDA_Function_t*>(ptr_to_func_t))
#define SMP_get_member_name(mid, buf, bufsize) get_member_name(mid, buf, bufsize)
#define SMP_isHead(flags) isHead(flags)
#define SMP_isCode(flags) isCode(flags)
#define SMP_add_cref(from, to, type) add_cref(from, to, type)
#define SMP_generate_disasm_line(InstAddr, CharArray, CharArrayLimit) generate_disasm_line(InstAddr, CharArray, CharArrayLimit)
#define SMP_tag_remove(InBuf, OutBuf, OutBufSize) tag_remove(InBuf, OutBuf, OutBufSize)
clc5q
committed
struct SMP_xref_t {
xrefblk_t CurrSMP_xref;
STARS_ea_t GetFrom() { return CurrSMP_xref.from; };
STARS_ea_t GetTo() { return CurrSMP_xref.to; };
clc5q
committed
uchar GetIscode() { return CurrSMP_xref.iscode; }; // 1-is code reference; 0-is data reference
uchar GetType() { return CurrSMP_xref.type; }; // type of the last retured reference (cref_t & dref_t)
uchar GetUser() { return CurrSMP_xref.user; }; // 1-is used defined xref, 0-defined by ida
bool SMP_first_from(STARS_ea_t from, int flags) // get first reference from...
clc5q
committed
{ return xrefblk_t_first_from(&CurrSMP_xref, from, flags); }
bool SMP_next_from(void) // get next reference from...
{ return xrefblk_t_next_from(&CurrSMP_xref); }
bool SMP_first_to(STARS_ea_t to, int flags) // get first reference to...
clc5q
committed
{ return xrefblk_t_first_to(&CurrSMP_xref, to, flags); }
bool SMP_next_to(void) // get next reference to....
{ return xrefblk_t_next_to(&CurrSMP_xref); }
};
#else
// Globals, typedefs and macros for STARS_IRDB_INTERFACE only
// Translate RTLs to SPARK-Ada?
#define ZST_EMIT_SPARK_ADA_TRANSLATION 0
clc5q
committed
#define SMP_strncat(str1, str2, len) strncat(str1, str2, len)
#define SMP_strncpy(str1, str2, len) strncpy(str1, str2, len)
#define SMP_snprintf(...) snprintf(__VA_ARGS__)
#define SMP_fprintf(...) fprintf(__VA_ARGS__)
#define SMP_fopen(name, mode) fopen(name, mode)
#define SMP_feof(file) feof(file)
#define SMP_fclose(file) fclose(file)
extern FILE *SMPLogFile;
#define SMP_msg(...) fprintf(SMPLogFile, __VA_ARGS__)
#endif
#endif