lrusecurity_Displayer.cpp 5.13 KB
Newer Older
1
#include <lrupreanalysis/features.h>
2
#include <lrusecurity/features.h>
3 4 5 6 7
#include <elm/sys/Path.h>
#include <elm/sys/System.h>

#include <otawa/proc/BBProcessor.h>
#include <otawa/prog/Process.h>
Valentin Touzeau's avatar
Valentin Touzeau committed
8
#include <lruexact/features.h>
9
#include <set>
10
#include <iterator>
11

Julien Balette-Pape's avatar
Julien Balette-Pape committed
12
#include "Graphic/AccMatching.h"
13 14
#include "Graphic/DotModificator.h"

Valentin Touzeau's avatar
Valentin Touzeau committed
15
//#include "SecCFGOutput.h"
16
using namespace otawa;
17

18
namespace lrusecurity
19 20
{

21
class SecurityDisplayer : public BBProcessor
22 23 24 25
{
public:
	static otawa::p::declare reg;

26
	SecurityDisplayer() :
27 28 29 30
		otawa::BBProcessor(reg),
		_path(""),
		_toFile(false),
		_stream(nullptr),
31 32
		_line(false),
        // Modification pour affichage en dot
Julien Balette-Pape's avatar
Julien Balette-Pape committed
33
        _modificator(new DotModificator("result.dot", new AccMatcher))   //TODO: changer l'accès au fichier
34 35 36 37 38 39
	{
	}

	virtual void configure(const PropList& props) override
	{
		otawa::BBProcessor::configure(props);
Valentin Touzeau's avatar
Valentin Touzeau committed
40 41 42 43
		if(props.hasProp(lruexact::CLASSIFICATION_PATH))
		  _path = lruexact::CLASSIFICATION_PATH(props);
		if(props.hasProp(lruexact::CLASSIFICATION_TO_FILE))
		  _toFile = lruexact::CLASSIFICATION_TO_FILE(props);
44
	}
45 46 47

    inline DotModificator* getModificator(void){return _modificator;};

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
protected:

	virtual void setup(WorkSpace *ws) override
	{
		// Check if source is available
		if(ws->isProvided(SOURCE_LINE_FEATURE))
			_line = true;
		if(logFor(LOG_PROC))
			log << "\tsource/line information " << (_line ? "" : "not ") << "available\n";

		if(!_path && _toFile)
			_path = _ << "access_classification.txt";

		if(_path) {
			try {
				_stream = elm::sys::System::createFile(_path);
			}
			catch(elm::sys::SystemException& e) {
				throw ProcessorException(*this, _ << "cannot open \"" << _path << "\"");
			}

			_out.setStream(*_stream);
		}
71

72
		_out << "ACCESS\t\tKIND\tCATEGORY\t\tBB\t";
73 74 75 76
		if(_line)
			_out << "\tLINE";

		_out << io::endl;
77

78 79 80 81
	}

	virtual void processCFG(WorkSpace* ws, CFG* cfg) override
	{
Julien Balette-Pape's avatar
Julien Balette-Pape committed
82
        AccMatcher *match = getModificator() -> getMatcher();
83 84
        match->setupCFG(cfg);

85 86 87 88 89 90 91 92 93 94 95 96 97
		_out << "FUNCTION " << cfg->label() << io::endl;
		BBProcessor::processCFG(ws, cfg);
	}

	virtual void processBB(WorkSpace* ws, CFG* cfg, Block* b) override
	{
		if(!b->isBasic())
			return;

		BasicBlock* bb = b->toBasic();

		for(Block::EdgeIter edgeIter(bb->ins()); edgeIter; ++edgeIter) {
			const Edge* e = *edgeIter;
98
			const Bag<icache::Access>& bag = otawa::icache::ACCESSES(e);
99 100 101
			processBag(ws, cfg, bag, bb);
		}

102
		const Bag<icache::Access>& bag = otawa::icache::ACCESSES(bb);
103 104 105 106 107 108 109 110 111
		processBag(ws, cfg, bag, bb);
	}

	void processBag(WorkSpace* ws, CFG* cfg, const Bag<icache::Access>& bag, BasicBlock* bb)
	{
		for(int i = 0; i < bag.size(); ++i)
			processAccess(ws, cfg, &bag[i], bb);
	}

112
  void processAccess(WorkSpace* ws, CFG* cfg, const otawa::icache::Access* access, BasicBlock* bb)
113
	{
114 115
	       SecurityCategory cat = SECURITY_CATEGORY(access);
	       if(cat == SecurityCategory::UNSAFE){
116 117
		_out << access->address() << "\t";
		switch(access->kind()){
118 119 120
			case otawa::icache::NONE: _out << "NONE\t"; break;
			case otawa::icache::FETCH: _out << "FETCH\t"; break;
			case otawa::icache::PREFETCH: _out << "PREFETCH\t"; break;
121
		}
122
			_out << "UNSAFE ";
123
			int set = otawa::icat3::LBLOCK(access)->set();
124 125

            int number = 0;
126
			if (bb->hasProp(EXIST_HIT_PREV_IN)) {
127
			  ACSSecurity prev = (*EXIST_HIT_PREV_IN(bb))[set];
128
			  int index = otawa::icat3::LBLOCK(access)->index();
129
              number = prev[index].size();
Julien Balette-Pape's avatar
Julien Balette-Pape committed
130
              std::set<const otawa::icache::Access *>::iterator iter;
131

Julien Balette-Pape's avatar
Julien Balette-Pape committed
132
			  for (iter = prev[index].begin(); iter != prev[index].end(); iter++){
Julien Balette-Pape's avatar
Julien Balette-Pape committed
133 134 135 136 137
                  if (*iter){
			        _out << (**iter).address() << "\n\t\t\t\t\b";
                  } else {
			        _out << "00000000" << "\n\t\t\t\t\b";
                  }
138 139 140

                  DotModificator *mod = getModificator();
                  mod->modify(cfg, *iter, access->address());
Mishigan's avatar
c debug  
Mishigan committed
141
              }
142 143

              // REMONTEE
Mishigan's avatar
Mishigan committed
144
              _out << "\033[" << number << "A";
145

146
			}
147 148

		_out << "\t\t\tBB " << bb->index() << "\t\t";
149
		printLine(ws, bb->address(), bb->topAddress().offset());
150 151 152 153 154 155

      // DESCENTE
      for (int i = 0; i < number-1; i++)
        _out << "\n//";
      _out << io::endl;
    }
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	}

	void printLine(WorkSpace* ws, Address begin, Address::offset_t offset)
	{

		// Display line
		if(_line) {
			bool one = false;
			Pair<cstring, int> old = pair(cstring(""), 0);
			for(Address addr = begin; addr.offset() < offset; addr = addr + 1) {
				Option<Pair<cstring, int> > res = ws->process()->getSourceLine(addr);
				if(res) {
					if((*res).fst != old.fst) {
						old = *res;
			 			if(one)
			 				_out << ", ";
			 			else
			 				one = true;
			 			_out << old.fst << ':' << old.snd;
					}
					else if((*res).snd != old.snd) {
						old = *res;
						_out << ',' << old.snd;
					}
				}
			}
		}
	}

	virtual void cleanup(WorkSpace*) override
	{
		if(_stream)
			delete _stream;
	}

	elm::io::Output _out;
	elm::sys::Path _path;
	bool _toFile;
	elm::io::OutStream* _stream;
	bool _line;
	bool _mcAnalysis;
197 198

    DotModificator *_modificator;
199 200
};

201 202 203
p::declare SecurityDisplayer::reg = p::init("lrusecurity::SecurityDisplayer", Version(1, 0, 0))
	.require(SECURITY_CATEGORY_FEATURE)
	.make<SecurityDisplayer>();
204

205
} // namespace lrusecurity
206