Skip to content
Snippets Groups Projects
SMPDBInterface.h 10.7 KiB
Newer Older
/*
 * 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 <string>
#include <utility>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <cstdio>

#ifndef STARS_IRDB_INTERFACE
#ifndef STARS_IDA_INTERFACE
#pragma 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>
#include <interfaces/abstract/STARSInterface.h>
#include <interfaces/abstract/STARSFunction.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

// A maximum register number to use for allocation bitsets,
//  e.g. the DEADREGS debugging bitset that limits DEADREG
//  annotations to only certain register numbers.
//  Register numbers track enum STARS_RegNo from STARSTypes.h.
#define STARS_MAX_REGNUM_FOR_BITSETS 255

// 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 STARS_BADADDR, which matches the IDA Pro BADADDR constant, we can automatically adjust for 32/64-bit systems.
#define STARS_SSA_MARKER_PSEUDO_ID ((STARS_ea_t) STARS_BADADDR - 1)
#define STARS_LIVEIN_PSEUDO_ID ((STARS_ea_t) STARS_BADADDR - 2)
#define STARS_EXTERNAL_FUNC_ADDR STARS_LIVEIN_PSEUDO_ID 
// 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) STARS_BADADDR - STARS_BLOCKNUM_MASK)
#define STARS_PSEUDO_BLOCKNUM_MAX ((STARS_ea_t) STARS_BADADDR - 3)
#define STARS_IsNotPseudoInstID(addr) (STARS_PSEUDO_ID_MIN > addr)
#define STARS_IsLiveInPseudoID(addr) (STARS_LIVEIN_PSEUDO_ID == ((STARS_ea_t) addr))
#define STARS_IsExternalFunc(addr) (STARS_EXTERNAL_FUNC_ADDR == ((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)
clc5q's avatar
clc5q committed
#define STARS_MakeBlockNumPseudoID(BlockNum) (STARS_PSEUDO_ID_MIN + BlockNum)
// 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;

clc5q's avatar
clc5q committed
// Counters for loop analyses.
extern unsigned long LoopInvariantDEFCount;

// 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;

// Counters for safe and unsafe indirect memory writes.
extern unsigned long STARS_SafeIndirectMemWriteCount;
extern unsigned long STARS_UnsafeIndirectMemWriteCount;

clc5q's avatar
clc5q committed
// Counters for basic blocks and blocks with indirect memory writes.
extern unsigned long STARS_BasicBlockCount;
extern unsigned long STARS_IndirectMemWriteBlockCount;

// Counters for function pointer shadowing.
extern unsigned long STARS_FuncPtrShadowPointsAttempted;
extern unsigned long STARS_FuncPtrShadowPointsSucceeded;
extern unsigned long STARS_FuncPtrShadowPointsFailed;
extern unsigned long STARS_FuncPtrShadowPointsSafe;  // not vulnerable to overwrite, so not shadowed.

// Counters for return address set annotations.
extern unsigned long STARS_ReturnSetComplete;
extern unsigned long STARS_ReturnSetIncomplete;
extern unsigned long STARS_ReturnSetIncompleteAddrRange;
extern unsigned long STARS_ReturnSetIncompleteMultiEntry;
extern unsigned long STARS_ReturnSetIncompleteAddrTaken;
extern unsigned long STARS_ReturnSetIncompleteCalledFromOrphanCode;
extern unsigned long STARS_ReturnSetIncompleteTailCallChainFromOrphanCode;
// Per-function counters (above were per-return-inst counters)
extern unsigned long STARS_FuncReturnSetComplete;
extern unsigned long STARS_FuncReturnSetIncomplete;

// Loop analysis counters.
extern unsigned long STARS_LoopInductionVarIDSuccesses;
extern unsigned long STARS_LoopInductionVarIDFailures;
extern unsigned long STARS_LoopIterationExprSuccesses;
extern unsigned long STARS_LoopIterationExprFailures;
// strings for printing ZST_SysCallType
extern const char *CallTypeNames[4];
#define SMP_msg(...) global_stars_interface->STARS_msg(__VA_ARGS__)
#define SMP_strncat(str1, str2, len) global_stars_interface->STARS_strncat(str1, str2, len)
#define SMP_strncpy(str1, str2, len) global_stars_interface->STARS_strncpy(str1, str2, len)
#define SMP_snprintf(...) global_stars_interface->STARS_snprintf(__VA_ARGS__)
#define SMP_fprintf(...) global_stars_interface->STARS_fprintf(__VA_ARGS__)
#define SMP_fscanf(...) global_stars_interface->STARS_fscanf(__VA_ARGS__)
#define SMP_fopen(name, mode) global_stars_interface->STARS_fopen(name, mode)
#define SMP_feof(file) feof(file)
#define SMP_fclose(file) global_stars_interface->STARS_fclose(file)
#define SMP_ftell(file) global_stars_interface->STARS_ftell(file)
#define SMP_fgetc(file) global_stars_interface->STARS_fgetc(file)
#define SMP_fgets(buffer, buflen, file) global_stars_interface->STARS_fgets(buffer, buflen, file)
#define SMP_fseek(file, offset, whence) global_stars_interface->STARS_fseek(file, offset, whence)
#define SMP_getseg(addr)  (global_stars_interface->getseg(addr))
jdh8d's avatar
jdh8d committed
#define SMP_getnseg(index) (global_stars_interface->getnseg(index))
#define SMP_get_segm_qty() (global_stars_interface->get_segm_qty())

jdh8d's avatar
jdh8d committed
#define SMP_get_first_seg() (SMP_getnseg(0)) /* get the first segment a diff way */
jdh8d's avatar
jdh8d committed
#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))

jdh8d's avatar
jdh8d committed
#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))
jdh8d's avatar
jdh8d committed
#define SMP_get_spd(ptr_to_func, ea) (ptr_to_func->get_spd(ea))

#define SMP_generate_disasm_line(InstAddr, CharArray, CharArrayLimit)  global_stars_interface->STARS_generate_disasm_line(InstAddr, CharArray, CharArrayLimit)
clc5q's avatar
clc5q committed
#define SMP_tag_remove(OutBuf, InBuf, OutBufSize)  global_stars_interface->STARS_tag_remove(OutBuf, InBuf, OutBufSize)
jdh8d's avatar
jdh8d committed
#define SMP_AuditTailChunkOwnership() (global_stars_interface->AuditTailChunkOwnership())
#define SMP_AuditCodeTargets() (global_stars_interface->AuditCodeTargets())
jdh8d's avatar
jdh8d committed

// Use SPARK short circuit operators "and then" and "or else" or omit them to speed proofs.
#define ZST_EMIT_SPARK_ADA_SHORT_CIRCUIT_OPERATORS 0