Skip to content
Snippets Groups Projects
OffsetInference.cpp 39.3 KiB
Newer Older
			}
		}
		
		else
		{
			if(verbose_log)
				cerr<<"OffsetInference: FindAllOffsets: No Pattern Match"<<endl;
		}
	} // end for all instructions

//TODO: everything is horribly hacked and messy, redo this function. 

	//if no dealloc is found, set all inferences to null
	//TODO: this was hacked together quickly, one flag is preferable. 
	//TODO: there might be a memory leak here, see the objects deleted
	//at the end of this function.
	if(alloc_count>1 || lea_sanitize)
	{
		if(lea_sanitize)
			cerr<<"OffsetInference: FindAllOffsets: lea_rsp that points to saved regs found "<<endl;
		else if(verbose_log)
			cerr<<"OffsetInference: FindAllOffsets: Multiple integral stack allocations found, returning null inferences"<<endl;
		

		direct[func] = NULL;
		scaled[func] = NULL;
		all_offsets[func] = NULL;
		p1[func] = NULL;
		return;

	}
	else
	{

		if(!dealloc_flag && ret_cnt == 0)
		{
			if(verbose_log)
				cerr<<"OffsetInference: FindAllOffsets: Function is missing stack deallocaiton, but does not return, assuming transformable"<<endl;
			dealloc_flag = true;
		}
		//TODO: I need to revisit this such that you can pass a pointer to PNStackLayout,
		//and handle NULL accordingly.

		//TODO: this has become too hacky, redo. 
		if(!dealloc_flag)
		{
			pn_direct_offsets->SetPaddingSafe(false);
			pn_scaled_offsets->SetPaddingSafe(false);
			pn_all_offsets->SetPaddingSafe(false);
			pn_p1_offsets->SetPaddingSafe(false);
		}

		unsigned int aoi_size = pn_all_offsets->GetRanges().size();
		//TODO: causes a memory leak since I may reset to NULL, redo

		//if the size of aoi is the same as any other inference
		//assume they are the same (insert a null layout entry)
		if(pn_direct_offsets->GetRanges().size() != aoi_size)
			direct[func] = new PNStackLayout(*pn_direct_offsets, func);
		else
			direct[func] = NULL;

		if(pn_scaled_offsets->GetRanges().size() != aoi_size)
			scaled[func] = new PNStackLayout(*pn_scaled_offsets, func);
		else
			scaled[func] = NULL;

		//TODO: BIG TODO: There is quite a delema here. If p1 is the same as
		//AOI, I don't want to generate it to save time, but what if a function
		//has no coverage, so p1 is used, if I set it null here because the
		//layouts are the same, I wont have any modification for that function. 
		p1[func] = new PNStackLayout(*pn_p1_offsets, func);

		all_offsets[func] = new PNStackLayout(*pn_all_offsets, func);

		if(!dealloc_flag)
		{
			if(verbose_log)
				cerr<<"OffsetInference: FindAllOffsets: No Stack Deallocation Found"<<endl;	 
			if(direct[func] != NULL && !direct[func]->IsShuffleSafe())
			{
				if(verbose_log)
					cerr<<"OffsetInference: FindAllOffsets: direct offset inference cannot be shuffled, generating null inference"<<endl;
				direct[func] = NULL;
			}

			if(scaled[func] != NULL && !scaled[func]->IsShuffleSafe())
			{
				if(verbose_log)
					cerr<<"OffsetInference: FindAllOffsets: scaled offset inference cannot be shuffled, generating null inference"<<endl;
				scaled[func] = NULL;
			}

			if(all_offsets[func] != NULL && !all_offsets[func]->IsShuffleSafe())
			{
				if(verbose_log)
					cerr<<"OffsetInference: FindAllOffsets: all offset inference cannot be shuffled, generating null inference"<<endl;
				all_offsets[func] = NULL;
			}

			p1[func] = NULL;
			if(verbose_log)
				cerr<<"OffsetInference: FindAllOffsets: p1 inference by default cannot be shuffled, generating null inference"<<endl;
		}
	
		if(!PN_safe)
		{
			if(verbose_log)
				cerr<<"OffsetInference: FindAllOffsets: Function not pn_safe, using only p1 (p1 may have been previously disabled)"<<endl;
			direct[func] = NULL;
			scaled[func] = NULL;
			all_offsets[func] = NULL;
		}
	}

	//memory clean up
	delete pn_direct_offsets;
	delete pn_scaled_offsets;
	delete pn_all_offsets;
	delete pn_p1_offsets;
} // end of OffsetInference::FindAllOffsets()

//If map entry exists, return it, else perform boundary detection
//If no layout can be made, NULL is returned.
PNStackLayout* OffsetInference::GetPNStackLayout(Function_t *func)
{
	return GetLayout(all_offsets,func);
}

PNStackLayout* OffsetInference::GetDirectAccessLayout(Function_t *func)
{
	return GetLayout(direct,func);
}

PNStackLayout* OffsetInference::GetScaledAccessLayout(Function_t *func)
{
	return GetLayout(scaled,func);
}

PNStackLayout* OffsetInference::GetP1AccessLayout(Function_t *func)
{
	return GetLayout(p1,func);
}


PNStackLayout* OffsetInference::GetLayout(map<Function_t*,PNStackLayout*> &mymap,Function_t *func)
{
	//No layout found, find all offset boundaries
	if (mymap.find(func) == mymap.end())
	{
		FindAllOffsets(func);
	}

	//At this point an entry should be made for the function
	assert(mymap.find(func) != mymap.end());

	return mymap.find(func)->second;
}

string OffsetInference::GetInferenceName() const
{
	return "All Offsets Inference";
}