aboutsummaryrefslogtreecommitdiff
path: root/unit-tests/testfile.h
blob: 7100d9ea6e110ebef909a71ead0653358eafa6bf (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#ifndef TESTFILE_H
#define TESTFILE_H

template<class TTestConfig> class TestSegmentListT;

template<class TTestConfig> 
class TestSegmentT : public TTestConfig::Segment
{
public:
	explicit TestSegmentT(typename TTestConfig::SegmentList *owner, uint64_t address, uint32_t size, const char *name, uint32_t type)
		: TTestConfig::Segment(owner), address_(address), size_(size), name_(name), type_(type), physical_size_(0), physical_offset_(0) {}
	virtual uint64_t address() const { return address_; }
	virtual uint64_t size() const { return size_; }
	virtual uint32_t physical_offset() const { return physical_offset_; }
	virtual uint32_t physical_size() const { return physical_size_; }
	virtual std::string name() const { return name_; }
	virtual uint32_t memory_type() const { return type_; }
	void set_physical_size(uint32_t physical_size) { physical_size_ = physical_size; }
	void set_physical_offset(uint32_t physical_offset) { physical_offset_ = physical_offset; }
	virtual void update_type(uint32_t mt) { }
	virtual uint32_t flags() const { return 0; }
	virtual void Rebase(uint64_t delta_base) { address_ += delta_base; }
	virtual TestSegmentT<TTestConfig> *Clone(ISectionList *owner) const { return new TestSegmentT<TTestConfig>(dynamic_cast<typename TTestConfig::SegmentList *>(owner), address_, size_, name_.c_str(), type_); }
private:
	uint64_t address_;
	uint32_t size_;
	std::string name_;
	uint32_t type_;
	uint32_t physical_size_;
	uint32_t physical_offset_;
};

template<class TTestConfig> 
class TestSegmentListT : public TTestConfig::SegmentList
{
public:
	explicit TestSegmentListT(typename TTestConfig::Architecture *owner)
		: TTestConfig::SegmentList(owner) {}

	TestSegmentT<TTestConfig> *Add(uint64_t address, uint32_t size, const char *name, uint32_t type)
	{
		TestSegmentT<TTestConfig> *seg = new TestSegmentT<TTestConfig>(this, address, size, name, type);
		this->AddObject(seg);
		return seg;
	}
};

template<class TTestConfig> 
class TestExportListT : public TTestConfig::ExportList
{
public:
	explicit TestExportListT(typename TTestConfig::Architecture *owner)
		: TTestConfig::ExportList(owner) {}
	virtual std::string name() const { return ""; }
protected:
	virtual typename TTestConfig::Export *Add(uint64_t /*address*/) { return NULL; }
};

template<class TTestConfig> 
class TestFixupT : public TTestConfig::Fixup
{
public:
	explicit TestFixupT(typename TTestConfig::FixupList *owner);
	virtual uint64_t address() const { return 0; }
	virtual FixupType type() const { return ftUnknown; }
	virtual OperandSize size() const { return osDWord; }
	virtual void set_address(uint64_t /*address*/) { return; }
	virtual void Rebase(IArchitecture & /*file*/, uint64_t /*delta_base*/) { return; }
	virtual TestFixupT<TTestConfig> *Clone(IFixupList *owner) const { return new TestFixupT<TTestConfig>(reinterpret_cast<typename TTestConfig::FixupList *>(owner)); }
};

template<class TTestConfig> 
class TestFixupListT : public TTestConfig::FixupList
{
public:
	explicit TestFixupListT()
		: TTestConfig::FixupList() {}
	virtual IFixup *AddDefault(OperandSize /*cpu_address_size*/,  bool /*is_code*/) 
	{ 
		TestFixupT<TTestConfig> *fixup = new TestFixupT<TTestConfig>(this);
		this->AddObject(fixup);
		return fixup; 
	}
};

template<class TTestConfig> 
class TestImportListT : public TTestConfig::ImportList
{
public:
	explicit TestImportListT(typename TTestConfig::Architecture *owner)
		: TTestConfig::ImportList(owner) {}
protected:
	virtual typename TTestConfig::Import *AddSDK() { return NULL; }
};

class TestMapFile : public MapFile
{
public:
	bool ParseEx(const char *file_name, const std::vector<uint64_t> &segments, uint64_t time_stamp)
	{
		bool res = MapFile::Parse(file_name, segments);
		set_time_stamp(time_stamp);
		return res;
	}
};

template<class TTestConfig>
class TestArchitectureT : public TTestConfig::Architecture
{
public:
	explicit TestArchitectureT(typename TTestConfig::File *owner, OperandSize cpu_address_size) 
		: TTestConfig::Architecture(owner, 0, -1), function_list_(NULL), cpu_address_size_(cpu_address_size), virtual_machine_list_(NULL)
	{
		segment_list_ = new TestSegmentListT<TTestConfig>(this);
		export_list_ = new TestExportListT<TTestConfig>(this);
		fixup_list_ = new TestFixupListT<TTestConfig>();
		import_list_ = new TestImportListT<TTestConfig>(this);
		time_stamp_ = 1;

		function_list_ = new typename TTestConfig::FunctionList(this);
		runtime_function_list_ = new typename TTestConfig::RuntimeFunctionList();
		virtual_machine_list_ = new typename TTestConfig::VirtualMachineList();
	}

	virtual ~TestArchitectureT()
	{
		delete segment_list_;
		delete export_list_;
		delete function_list_;
		delete fixup_list_;
		delete import_list_;
		delete virtual_machine_list_;
		delete runtime_function_list_;
	}; 

	virtual std::string format_name() const { return "TEST"; }
	virtual std::string name() const { return "TEST"; }
	virtual uint32_t type() const { return 0; }
	virtual OperandSize cpu_address_size() const { return cpu_address_size_; }
	virtual uint64_t entry_point() const { return 0; };
	virtual uint32_t segment_alignment() const { return 0x1000; }
	virtual TestSegmentListT<TTestConfig> *segment_list() const { return segment_list_; }
	virtual typename TTestConfig::SectionList *section_list() const { return NULL; }
	virtual TestImportListT<TTestConfig> *import_list() const { return import_list_; }
	virtual TestExportListT<TTestConfig> *export_list() const { return export_list_; }
	virtual TestFixupListT<TTestConfig> *fixup_list() const { return fixup_list_; }
	virtual typename TTestConfig::RelocationList *relocation_list() const { return NULL; }
	virtual typename TTestConfig::ResourceList *resource_list() const { return NULL; }
	virtual IFunctionList *function_list() const { return function_list_; }
	virtual IVirtualMachineList *virtual_machine_list() const { return virtual_machine_list_; }
	virtual typename TTestConfig::SEHandlerList *seh_handler_list() const { return NULL; }
	virtual typename TTestConfig::RuntimeFunctionList *runtime_function_list() const { return runtime_function_list_; }
	virtual uint64_t image_base() const { return 0; }
	virtual CallingConvention calling_convention() const { return ccStdcall; }
	virtual uint64_t time_stamp() const { return time_stamp_; }

	void ReadTestMapFile(const char *file_name)
	{
		TestMapFile map_file;
		std::vector<uint64_t> segments;
		for (size_t i = 0; i < segment_list()->count(); i++) {
			segments.push_back(segment_list()->item(i)->address());
		}
		if (std::find(segments.begin(), segments.end(), 0) == segments.end())
			segments.insert(segments.begin(), 0);
		if (map_file.ParseEx(file_name, segments, time_stamp()))
			this->ReadMapFile(map_file);
		this->map_function_list()->ReadFromFile(*this);
	};
	virtual void Save(CompileContext & /*c*/) 
	{ 
		ISection *last_section = segment_list_->item(0);
		uint64_t address = AlignValue(last_section->address() + last_section->size(), segment_alignment());
		uint64_t pos = this->size();
		auto *vmp_section = segment_list_->Add(address, -1, ".vmp", mtReadable | mtExecutable);
		vmp_section->set_physical_size(-1);
		vmp_section->set_physical_offset(static_cast<uint32_t>(pos));

		for (size_t i = 0; i < function_list_->count(); i++) {
			function_list_->item(i)->WriteToFile(*this);
		}
	};

	bool Prepare(CompileContext &ctx)
	{
		ISectionList *seg_list = segment_list();
		if (seg_list->count() == 0)
			return false;

		ISection *section = seg_list->item(seg_list->count() - 1);
		ctx.manager->Add(AlignValue(section->address() + section->size(), segment_alignment()), -1, mtReadable | mtExecutable | mtWritable | mtNotPaged);

		return true;
	};
protected:
	virtual bool ReadHeader(uint32_t /*open_mode*/) { return true; }
private:
	TestSegmentListT<TTestConfig> *segment_list_;
	TestExportListT<TTestConfig> *export_list_;
	IFunctionList *function_list_;
	TestFixupListT<TTestConfig> *fixup_list_;
	TestImportListT<TTestConfig> *import_list_;
	OperandSize cpu_address_size_;
	IVirtualMachineList *virtual_machine_list_;
	typename TTestConfig::RuntimeFunctionList *runtime_function_list_;
	uint64_t time_stamp_;

	// no copy ctr or assignment op
	TestArchitectureT(const TestArchitectureT &);
	TestArchitectureT &operator =(const TestArchitectureT &);
};

template<class TTestConfig>
class TestFileT : public TTestConfig::File
{
public:
	explicit TestFileT(OperandSize cpu_address_size) 
		: TTestConfig::File(NULL)
	{
		Add(cpu_address_size);
	}

	explicit TestFileT(const typename TTestConfig::File &src, const char *file_name)
		: TTestConfig::File(src, file_name) {}

	void OpenFromMemory(const void *buf, uint32_t len)
	{
		this->CloseStream();
		this->stream_ = new MemoryStream;
		/*size_t res = */this->stream_->Write(buf, len);

		ISection *segment = item(0)->segment_list()->item(0);
		uint8_t b = 0xcc;
		for (size_t i = len; i < segment->physical_size(); i++) {
			this->stream_->Write(&b, sizeof(b));
		}

		typename TTestConfig::FileHelper helper;
		helper.Parse(*item(0));
	}

	TestArchitectureT<TTestConfig> *item(size_t index) const { return reinterpret_cast<TestArchitectureT<TTestConfig> *>(TTestConfig::File::item(index)); }

	IArchitecture *Add(OperandSize cpu_address_size) 
	{
		auto arch = new TestArchitectureT<TTestConfig>(this, cpu_address_size);
		this->AddObject(arch);
		return arch;
	}

	TestFileT *Clone(const char *file_name) const
	{
		TestFileT *file = new TestFileT(*this, file_name);
		return file;
	}

protected:
	virtual OpenStatus ReadHeader(uint32_t /*open_mode*/) { return osSuccess; };
	virtual IFile *runtime() const { return NULL; }
private:
	std::string map_file_name_;
};

class TestLog: public ILog
{
public:
	virtual void AddMessage(MessageType /*type*/, IObject * /*sender*/, const std::string & /*message*/) { return; }
	virtual void StartProgress(const std::string & /*caption*/, unsigned long long /*max*/) { return; }
	virtual void StepProgress(unsigned long long /*value*/ = 1ull, bool /*is_project*/ = false) { return; }
	virtual void EndProgress() { return; }
};

#endif