Newer
Older
else if (IsEqType(this->OpType , GLOBALPTR))
clc5q
committed
SMP_msg("G ");
else if (IsEqType(this->OpType , HEAPPTR))
clc5q
committed
SMP_msg("H ");
else if (IsEqType(this->OpType , PTROFFSET))
clc5q
committed
SMP_msg("O ");
clc5q
committed
else if (IsEqType(this->OpType , NEGATEDPTR))
clc5q
committed
SMP_msg("NegP ");
else if (IsEqType(this->OpType , UNKNOWN))
clc5q
committed
SMP_msg("U ");
/* emit the profile bit */
clc5q
committed
SMP_msg("Pr ");
// Don't write anything for UNINIT OpType
// Emit the metadata status.
if (DEF_METADATA_UNUSED == this->MetadataStatus)
clc5q
committed
SMP_msg("Mn ");
else if (DEF_METADATA_USED == this->MetadataStatus)
clc5q
committed
SMP_msg("Mu ");
else if (DEF_METADATA_REDUNDANT == this->MetadataStatus)
clc5q
committed
SMP_msg("Mr ");
// Is the DEF possibly aliased because of an indirect write in
// the DEF-USE chain?
if (this->IndWrite)
clc5q
committed
SMP_msg("Al* ");
return;
} // end of DefOrUse::Dump()
// *****************************************************************
// Class DefOrUseSet
// *****************************************************************
// Default constructor.
DefOrUseSet::DefOrUseSet(void) {
this->Refs.clear();
clc5q
committed
// Destructor.
DefOrUseSet::~DefOrUseSet() {
this->Refs.clear();
return;
clc5q
committed
}
// Find the reference for a given operand type.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::FindRef(op_t SearchOp) {
set<DefOrUse, LessDefUse>::iterator CurrRef;
DefOrUse DummyRef(SearchOp);
CurrRef = this->Refs.find(DummyRef);
return CurrRef;
}
// Insert a new DEF or USE; must be new, insert must succeed else we assert.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::InsertRef(DefOrUse Ref) {
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(Ref);
assert(InsertResult.second);
return InsertResult.first;
}
// Set a Def or Use into the list, along with its type.
void DefOrUseSet::SetRef(op_t Ref, SMPOperandType Type, int SSASub) {
DefOrUse CurrRef(Ref, Type, SSASub);
this->Refs.insert(CurrRef);
return;
}
// Change the indirect write status for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetOp(set<DefOrUse, LessDefUse>::iterator CurrRef, op_t NewOp) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
assert(CurrRef != this->Refs.end());
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetOp(NewOp);
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
return InsertResult.first;
} // end of DefOrUseSet::SetOp()
// Change the SSA subscript for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetSSANum(op_t CurrOp, int NewSSASub) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
set<DefOrUse, LessDefUse>::iterator NextRef = CurrRef;
++NextRef;
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetSSANum(NewSSASub);
this->Refs.erase(CurrRef);
CurrRef = this->Refs.insert(NextRef, NewCopy);
return CurrRef;
} // end of DefOrUseSet::SetSSANum()
// Change the operand type for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetType(op_t CurrOp, SMPOperandType Type, const SMPInstr* Instr) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
#if 1
if (o_imm == CurrOp.type) {
if (UNINIT != CurrRef->GetType() && Type != CurrRef->GetType()) {
clc5q
committed
SMP_msg("ERROR: Changing type of immediate from %d to %d at %x: ", CurrRef->GetType(), Type, Instr->GetAddr());
clc5q
committed
SMP_msg("\n");
SMPInstr InstCopy = (*Instr);
InstCopy.Dump();
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetType(Type,Instr);
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
CurrRef = InsertResult.first;
} // end of DefOrUseSet::SetType()
// Change the Metadata type for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetMetadata(op_t CurrOp, SMPMetadataType Status) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetMetadataStatus(Status);
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
CurrRef = InsertResult.first;
return CurrRef;
} // end of DefOrUseSet::SetMetadata()
// Change the indirect write status for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetIndWrite(op_t CurrOp, bool IndWriteFlag) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetIndWrite(IndWriteFlag);
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
CurrRef = InsertResult.first;
return CurrRef;
} // end of DefOrUseSet::SetIndWrite()
// Change the ignore apparent truncation flag for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetNoTruncation(op_t CurrOp, bool NoTruncFlag) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetNoTruncation(NoTruncFlag);
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
CurrRef = InsertResult.first;
return CurrRef;
} // end of DefOrUseSet::SetNoTruncation()
clc5q
committed
// Change the ignore apparent overflow flag for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetNoOverflow(op_t CurrOp, bool NoOverflowFlag) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetNoOverflow(NoOverflowFlag);
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
CurrRef = InsertResult.first;
return CurrRef;
} // end of DefOrUseSet::SetNoOverflow()
clc5q
committed
void DefOrUseSet::Dump(void) const {
set<DefOrUse, LessDefUse>::iterator CurrRef;
for (CurrRef = this->Refs.begin(); CurrRef != this->Refs.end(); ++CurrRef) {
CurrRef->Dump();
}
clc5q
committed
SMP_msg("\n");
// Do all types agree, ignoring any flags registers in the set? This is used
// for conditional move instructions; if all types agree, it does not matter
// whether the move happens or not.
clc5q
committed
bool DefOrUseSet::TypesAgreeNoFlags(void) {
bool FoundFirstUse = false;
set<DefOrUse, LessDefUse>::iterator CurrUse;
SMPOperandType UseType = UNINIT;
for (CurrUse = this->Refs.begin(); CurrUse != this->Refs.end(); ++CurrUse) {
if (!(CurrUse->GetOp().is_reg(X86_FLAGS_REG))) { // ignore flags
if (!FoundFirstUse) {
FoundFirstUse = true;
UseType = CurrUse->GetType();
}
else {
clc5q
committed
if (IsNotEqType(CurrUse->GetType(), UseType)) {
clc5q
committed
return false; // inconsistent types
}
}
}
}
return true;
} // end of DefOrUseSet::TypesAgreeNoFlags()
// *****************************************************************
// Class DefOrUseList
// *****************************************************************
// Default constructor.
DefOrUseList::DefOrUseList(void) {
this->Refs.clear();
return;
}
// Set a Def or Use into the list, along with its type.
void DefOrUseList::SetRef(op_t Ref, SMPOperandType Type, int SSASub) {
DefOrUse CurrRef(Ref, Type, SSASub);
this->Refs.push_back(CurrRef);
DefOrUse DefOrUseList::GetRef(size_t index) const {
// Change the SSA subscript for a reference.
void DefOrUseList::SetSSANum(size_t index, int NewSSASub) {
this->Refs[index].SetSSANum(NewSSASub);
return;
}
// Change the operand type for a reference.
void DefOrUseList::SetType(size_t index, SMPOperandType Type, const SMPInstr* Instr) {
this->Refs[index].SetType(Type,Instr);
return;
}
// Debug printing.
void DefOrUseList::Dump(void) const {
for (size_t index = 0; index < this->Refs.size(); ++index) {
Refs[index].Dump();
}
clc5q
committed
SMP_msg("\n");
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
return;
}
// Erase duplicate entries, in case SMPInstr::MDFixupDefUseLists() adds one.
void DefOrUseList::EraseDuplicates(void) {
set<op_t, LessOp> TempRefs; // Use STL set to find duplicates
set<op_t, LessOp>::iterator TempIter;
vector<DefOrUse>::iterator RefIter;
RefIter = this->Refs.begin();
while (RefIter != this->Refs.end()) {
TempIter = TempRefs.find(RefIter->GetOp());
if (TempIter == TempRefs.end()) { // not already in set
TempRefs.insert(RefIter->GetOp());
++RefIter;
}
else { // found it in set already
RefIter = this->Refs.erase(RefIter);
}
}
return;
} // end of DefOrUseList::EraseDuplicates()
// *****************************************************************
// Class SMPPhiFunction
// *****************************************************************
// Constructor
SMPPhiFunction::SMPPhiFunction(int GlobIndex, const DefOrUse &Def) {
this->DefName = Def;
clc5q
committed
this->SubscriptedOps.clear();
DefOrUse SMPPhiFunction::GetDefCopy(void) const {
DefOrUse DefCopy(this->DefName);
return DefCopy;
}
// Add a phi item to the list
void SMPPhiFunction::PushBack(DefOrUse Ref) {
this->SubscriptedOps.SetRef(Ref.GetOp(), Ref.GetType(), Ref.GetSSANum());
return;
}
// Set the SSA number of the defined variable.
void SMPPhiFunction::SetSSADef(int NewSSASub) {
this->DefName.SetSSANum(NewSSASub);
return;
}
// Set the SSA number of the input variable.
void SMPPhiFunction::SetSSARef(size_t index, int NewSSASub) {
this->SubscriptedOps.SetSSANum(index, NewSSASub);
return;
}
// Set the type of the defined variable.
void SMPPhiFunction::SetDefType(SMPOperandType Type, const SMPInstr* Instr) {
this->DefName.SetType(Type, Instr);
return;
}
// Set the type of the input variable.
void SMPPhiFunction::SetRefType(size_t index, SMPOperandType Type, const SMPInstr* Instr) {
this->SubscriptedOps.SetType(index, Type, Instr);
// Set the metadata status of the DEF variable.
void SMPPhiFunction::SetDefMetadata(SMPMetadataType Status) {
this->DefName.SetMetadataStatus(Status);
return;
} // end of SMPPhiFunction::SetDefMetadata()
// Does at least one USE have a type other than UNINIT?
bool SMPPhiFunction::HasTypedUses(void) {
size_t index;
for (index = 0; index < this->GetPhiListSize(); ++index) {
if (UNINIT != this->GetUseType(index))
return true;
}
return false;
} // end of SMPPhiFunction::HasTypedUses()
// Return the result of applying the conditional type propagation meet operator
// over all the USE types.
SMPOperandType SMPPhiFunction::ConditionalMeetType(void) const {
SMPOperandType MeetType;
SMPOperandType PtrType = UNINIT;
SMPOperandType NumericType = UNINIT; // can end up NUMERIC or CODEPTR
bool FoundUNINIT = false; // any USE type UNINIT?
bool FoundNUMERIC = false; // any USE type NUMERIC?
bool FoundPOINTER = false; // includes all POINTER subtypes
bool FoundUNKNOWN = false; // any USE type UNKNOWN?
clc5q
committed
bool FoundPTROFFSET = false; // any USE type PTROFFSET?
bool FoundNEGATEDPTR = false; // any USE type NEGATEDPTR?
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
bool ProfilerDerived = false; // was any USE type Profiler-derived?
for (size_t index = 0; index < this->GetPhiListSize(); ++index) {
SMPOperandType UseType = this->GetUseType(index);
if (IsEqType(UseType, UNINIT))
FoundUNINIT = true;
else if (IsNumeric(UseType)) {
FoundNUMERIC = true;
if (IsEqType(NumericType, CODEPTR)) {
// Already refined. If current type agrees, leave it
// alone, else revert to generic type NUMERIC.
if (IsNotEqType(UseType, NumericType))
NumericType = NUMERIC;
}
else {
// Have not yet refined NumericType; might still be UNINIT.
if (IsEqType(UNINIT, NumericType))
NumericType = UseType;
else { // NumericType is NUMERIC; leave it as NUMERIC.
assert(IsEqType(NUMERIC, NumericType));
}
}
}
else if (IsDataPtr(UseType)) {
FoundPOINTER = true;
// Perform a meet over the pointer types.
if (IsRefinedDataPtr(PtrType)) {
// Already refined. If current type agrees, leave it
// alone, else revert to generic type POINTER.
if (IsNotEqType(UseType, PtrType))
PtrType = POINTER;
}
else {
// Have not yet refined PtrType; might still be UNINIT.
if (IsEqType(UNINIT, PtrType))
PtrType = UseType;
else { // PtrType is POINTER because we saw POINTER or
// had a conflict between pointer refinements; leave
// it as POINTER.
assert(IsEqType(POINTER, PtrType));
}
}
}
clc5q
committed
else if (IsEqType(PTROFFSET, UseType))
FoundPTROFFSET = true;
else if (IsEqType(NEGATEDPTR, UseType))
FoundNEGATEDPTR = true;
else if (IsUnknown(UseType))
FoundUNKNOWN = true;
if (IsProfDerived(UseType))
ProfilerDerived = true;
}
// Use the boolean flags to compute the meet function.
clc5q
committed
if (FoundUNKNOWN || (FoundNUMERIC && FoundPOINTER)
|| ((FoundNUMERIC || FoundPOINTER || FoundNEGATEDPTR) && FoundPTROFFSET)
|| ((FoundNUMERIC || FoundPOINTER || FoundPTROFFSET) && FoundNEGATEDPTR))
MeetType = UNKNOWN;
else if (FoundNUMERIC)
MeetType = NumericType;
else if (FoundPOINTER)
MeetType = PtrType;
clc5q
committed
else if (FoundPTROFFSET)
MeetType = PTROFFSET;
else if (FoundNEGATEDPTR)
MeetType = NEGATEDPTR;
else {
assert(FoundUNINIT);
MeetType = UNINIT;
}
if (ProfilerDerived)
MeetType = MakeProfDerived(MeetType);
return MeetType;
} // end of SMPPhiFunction::ConditionalMeetType()
// Debug printing.
void SMPPhiFunction::Dump(void) const {
clc5q
committed
SMP_msg(" DEF: ");
this->DefName.Dump();
clc5q
committed
SMP_msg(" USEs: ");
this->SubscriptedOps.Dump();
return;
}
// *****************************************************************
// Class SMPDefUseChain
// *****************************************************************
// Constructors
SMPDefUseChain::SMPDefUseChain(void) {
this->SSAName.type = o_void;
clc5q
committed
this->RefInstrs.clear();
this->RefInstrs.push_back((unsigned short) BADADDR);
this->IndWrite = false;
return;
}
SMPDefUseChain::SMPDefUseChain(op_t Name, ea_t Def) {
this->SetName(Name);
this->RefInstrs.push_back(Def);
this->IndWrite = false;
return;
}
// Set the variable name.
void SMPDefUseChain::SetName(op_t Name) {
if (o_reg == Name.type) {
// We want to map AH, AL, and AX to EAX, etc. throughout our data flow analysis
// and type inference systems.
CanonicalizeOpnd(Name);
this->SSAName = Name;
return;
}
// Set the DEF instruction.
void SMPDefUseChain::SetDef(ea_t Def) {
this->RefInstrs[0] = (unsigned short) Def;
return;
}
// Push a USE onto the list
void SMPDefUseChain::PushUse(ea_t Use) {
this->RefInstrs.push_back((unsigned short) Use);
return;
}
// Set the indirect memory write flag.
void SMPDefUseChain::SetIndWrite(bool IndMemWrite) {
this->IndWrite = IndMemWrite;
return;
}
// DEBUG dump.
clc5q
committed
void SMPDefUseChain::Dump(int SSANum) const {
clc5q
committed
SMP_msg("DEF-USE chain for: ");
PrintListOperand(this->SSAName, SSANum);
if (this->RefInstrs.size() < 1) {
clc5q
committed
SMP_msg(" no references.\n");
return;
}
clc5q
committed
SMP_msg("\n DEF: %x USEs: ", this->RefInstrs.at(0));
size_t index;
for (index = 1; index < this->RefInstrs.size(); ++index)
clc5q
committed
SMP_msg("%x ", this->RefInstrs.at(index));
SMP_msg("\n");
return;
} // end of SMPDefUseChain::Dump()
// *****************************************************************
// Class SMPDUChainArray
// *****************************************************************
SMPDUChainArray::SMPDUChainArray(void) {
this->SSAName.type = o_void;
this->DUChains.clear();
return;
}
SMPDUChainArray::SMPDUChainArray(op_t Name, ea_t FirstAddrMinusOne) {
if (o_reg == Name.type) {
// We want to map AH, AL, and AX to EAX, etc. throughout our data flow analysis
// and type inference systems.
CanonicalizeOpnd(Name);
this->SSAName = Name;
this->BaseAddr = FirstAddrMinusOne;
this->DUChains.clear();
return;
}
ea_t SMPDUChainArray::GetLastUse(int SSANum) const {
ea_t TempAddr = DUChains.at(SSANum).GetLastUse();
if (BADADDR != TempAddr) {
// If BADADDR, leave it as BADADDR. Otherwise, add in BaseAddr.
TempAddr += this->BaseAddr;
}
return TempAddr;
}
void SMPDUChainArray::SetName(op_t Name, ea_t FirstAddrMinusOne) {
if (o_reg == Name.type) {
// We want to map AH, AL, and AX to EAX, etc. throughout our data flow analysis
// and type inference systems.
CanonicalizeOpnd(Name);
this->SSAName = Name;
this->BaseAddr = FirstAddrMinusOne;
return;
}
// DEBUG dump.
clc5q
committed
void SMPDUChainArray::Dump(void) const {
size_t index;
for (index = 0; index < this->GetSize(); ++index) {
this->DUChains.at(index).Dump((int) index);
}
return;
}
// *****************************************************************
// Class SMPCompleteDUChains
// *****************************************************************
// DEBUG dump.
clc5q
committed
void SMPCompleteDUChains::Dump(void) const {
size_t index;
for (index = 0; index < this->ChainsByName.size(); ++index) {
this->ChainsByName.at(index).Dump();
}
return;
} // end of SMPCompleteDUChains::Dump()
// *****************************************************************
// Class STARSBitSet
// *****************************************************************
// Constructors.
STARSBitSet::STARSBitSet() {
this->BitLimit = 0;
}
// Get methods
bool STARSBitSet::GetBit(size_t BitIndex) const {
size_t ByteIndex = BitIndex / 8;
size_t BitNumber = BitIndex % 8;
clc5q
committed
assert(BitIndex <= this->BitLimit);
return (0 != (this->STARSBits.at(ByteIndex) & STARSBitMasks[BitNumber]));
}
// Set methods
void STARSBitSet::AllocateBits(size_t Size) {
size_t Bytes = Size / 8;
size_t ExtraBits = Size % 8;
clc5q
committed
this->BitLimit = Size;
if (0 != ExtraBits) {
}
else {
}
for (Bytes = 0; Bytes < this->STARSBits.size(); ++Bytes) {
this->STARSBits[Bytes] = 0;
}
}
void STARSBitSet::SetBit(size_t BitIndex) {
size_t ByteIndex = BitIndex / 8;
size_t BitNumber = BitIndex % 8;
clc5q
committed
assert(BitIndex <= this->BitLimit);
this->STARSBits[ByteIndex] |= STARSBitMasks[BitNumber];
return;
}
void STARSBitSet::ResetBit(size_t BitIndex) {
size_t ByteIndex = BitIndex / 8;
size_t BitNumber = BitIndex % 8;
clc5q
committed
assert(BitIndex <= this->BitLimit);
this->STARSBits[ByteIndex] &= (~STARSBitMasks[BitNumber]);
return;
}
// Query methods
// Returns false if all bits are zero, true otherwise.
bool STARSBitSet::IsAnyBitSet(void) const {
bool FoundSetBit = false;
size_t ByteIndex;
for (ByteIndex = 0; ByteIndex < this->STARSBits.size(); ++ByteIndex) {
if (0 != this->STARSBits[ByteIndex]) {
FoundSetBit = true;
break;
}
}
return FoundSetBit;
}
clc5q
committed
// Map system or library call name to FG info about its return value.
static map<string, struct FineGrainedInfo> ReturnRegisterTypeMap;
clc5q
committed
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
// Map system or library call name to the annotation substring that
// guides saturating arithmetic or other continuation policies in
// the case of integer error detection of a value passed to that call.
// If we don't care about a certain call, we return an empty string.
static map<string, string> IntegerErrorCallSinkMap;
void InitIntegerErrorCallSinkMap(void) {
pair<string, string> MapEntry;
pair<map<string, string>::iterator, bool> InsertResult;
MapEntry.first = string("malloc");
MapEntry.second = string("SINKMALLOC");
InsertResult = IntegerErrorCallSinkMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("calloc");
MapEntry.second = string("SINKMALLOC");
InsertResult = IntegerErrorCallSinkMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("realloc");
MapEntry.second = string("SINKMALLOC");
InsertResult = IntegerErrorCallSinkMap.insert(MapEntry);
assert(InsertResult.second);
return;
}
// Return sink string for call name from the sink map.
// If we don't care find the call name, we return an empty string.
void GetSinkStringForCallName(string CalleeName, string &SinkString) {
map<string, string>::iterator MapIter;
SinkString.clear(); // empty string, append map string if found later
MapIter = IntegerErrorCallSinkMap.find(CalleeName);
if (MapIter != IntegerErrorCallSinkMap.end()) { // found it
SinkString.append(MapIter->second);
}
return;
}
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
// Map system or library call name to the argument number that
// should have an unsigned value and should be guarded from the
// signedness error that results from copying a signed value
// into the outgoing argument. Argument numbers are zero-based.
// We will return 0 when there is no argument to worry about
// for a particular library or system call name.
static map<string, unsigned int> UnsignedArgPositionMap;
void InitUnsignedArgPositionMap(void) {
pair<string, unsigned int> MapEntry;
pair<map<string, unsigned int>::iterator, bool> InsertResult;
// <string.h>
MapEntry.first = string("memchr");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("memcmp");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("memcpy");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("memmove");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("memset");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("strncat");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("strncmp");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("strncpy");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("strxfrm");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
// <stdlib.h>
MapEntry.first = string("malloc");
MapEntry.second = STARS_ARG_POS_0;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("calloc");
MapEntry.second = (STARS_ARG_POS_0 | STARS_ARG_POS_1);
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("realloc");
MapEntry.second = STARS_ARG_POS_1;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("bsearch");
MapEntry.second = (STARS_ARG_POS_2 | STARS_ARG_POS_3);
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("qsort");
MapEntry.second = (STARS_ARG_POS_1 | STARS_ARG_POS_2);
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("mblen");
MapEntry.second = STARS_ARG_POS_1;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("mbtowc");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("mbstowcs");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("wcstombs");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
// <stdio.h>
MapEntry.first = string("setvbuf");
MapEntry.second = STARS_ARG_POS_3;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
return;
}
// Return unsigned arg position bitset for call name from the sink map.
// If we don't care find the call name, we return 0.
void GetUnsignedArgPositionsForCallName(string CalleeName, unsigned int &ArgPosBits) {
map<string, unsigned int>::iterator MapIter;
ArgPosBits = 0; // Change if found later
MapIter = UnsignedArgPositionMap.find(CalleeName);
if (MapIter != UnsignedArgPositionMap.end()) { // found it
ArgPosBits = MapIter->second;
}
return;
}
// Utility to count bits set in an unsigned int, e.g. ArgPosBits.
unsigned int CountBitsSet(unsigned int ArgPosBits) {
unsigned int count; // count accumulates the total bits set in ArgPosBits
for (count = 0; ArgPosBits; ++count) {
ArgPosBits &= (ArgPosBits - 1); // clear the least significant bit set
}
// Brian Kernighan's method goes through as many iterations as there are set bits.
// So if we have a 32-bit word with only the high bit set, then it will only go once through the loop.
// Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie) mentions this in exercise 2-9.
// On April 19, 2006 Don Knuth pointed out to me that this method "was first published by Peter Wegner in CACM 3 (1960), 322.
// (Also discovered independently by Derrick Lehmer and published in 1964 in a book edited by Beckenbach.)"
return count;
}
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
// Initialize the FG info for the return register from any library function
// whose name implies that we know certain return values (e.g. atoi() returns
// a signed integer, while strtoul() returns an unsigned long).
void GetLibFuncFGInfo(string FuncName, struct FineGrainedInfo &InitFGInfo) {
map<string, struct FineGrainedInfo>::iterator FindIter;
FindIter = ReturnRegisterTypeMap.find(FuncName);
if (FindIter == ReturnRegisterTypeMap.end()) { // not found
InitFGInfo.SignMiscInfo = 0;
InitFGInfo.SizeInfo = 0;
}
else { // found
InitFGInfo = FindIter->second;
}
return;
} // end of GetLibFuncFGInfo()
// Initialize the lookup maps that are used to define the FG info that can
// be inferred from a library function name.
void InitLibFuncFGInfoMaps(void) {
op_t DummyOp = InitOp;
struct FineGrainedInfo FGEntry;
pair<string, struct FineGrainedInfo> MapEntry;
pair<map<string, struct FineGrainedInfo>::iterator, bool> InsertResult;
// Add functions that return signed integers.
FGEntry.SignMiscInfo = FG_MASK_SIGNED;
FGEntry.SizeInfo = (FG_MASK_INTEGER | ComputeOperandBitWidthMask(DummyOp, sizeof(int)));
MapEntry.second = FGEntry;
MapEntry.first = "atoi";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "strcmp";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "strncmp";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "memcmp";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "isalnum";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "isalpha";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "islower";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "isupper";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "isdigit";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "isxdigit";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "iscntrl";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "isgraph";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "isblank";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "isspace";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "isprint";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "ispunct";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
// Functions that return signed longs.
if (sizeof(long int) != sizeof(int)) {
FGEntry.SizeInfo = (FG_MASK_INTEGER | ComputeOperandBitWidthMask(DummyOp, sizeof(long int)));
MapEntry.second = FGEntry;
}
MapEntry.first = "atol";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "strtol";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
// Functions that return signed long longs.
if (sizeof(long long int) != sizeof(long int)) {
FGEntry.SizeInfo = (FG_MASK_INTEGER | ComputeOperandBitWidthMask(DummyOp, sizeof(long long int)));
MapEntry.second = FGEntry;
}
MapEntry.first = "atoll";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "strtoll";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
// Functions that return unsigned long longs.
FGEntry.SignMiscInfo = FG_MASK_UNSIGNED;
MapEntry.second = FGEntry;
MapEntry.first = "strtoull";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
// Functions that return unsigned longs.
if (sizeof(long long int) != sizeof(long int)) {
FGEntry.SizeInfo = (FG_MASK_INTEGER | ComputeOperandBitWidthMask(DummyOp, sizeof(long int)));
MapEntry.second = FGEntry;
}
MapEntry.first = "strtoul";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
// Functions that return size_t.
FGEntry.SizeInfo = (FG_MASK_INTEGER | ComputeOperandBitWidthMask(DummyOp, sizeof(size_t)));
FGEntry.SignMiscInfo = FG_MASK_UNSIGNED;
MapEntry.second = FGEntry;
MapEntry.first = "strlen";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "strxfrm";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "strspn";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "strcspn";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
// Functions that return (char *).
FGEntry.SizeInfo = (FG_MASK_DATAPOINTER | ComputeOperandBitWidthMask(DummyOp, sizeof(char *)));
FGEntry.SignMiscInfo = FG_MASK_UNSIGNED;
MapEntry.second = FGEntry;
MapEntry.first = "strcpy";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = "strncpy";
InsertResult = ReturnRegisterTypeMap.insert(MapEntry);
assert(InsertResult.second);