File: Offset.cpp

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (62 lines) | stat: -rw-r--r-- 1,461 bytes parent folder | download
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
#include "stdafx.h"
#include "Code/Binary.h"
#include "Code/Listing.h"
#include "Code/OffsetSource.h"
#include "Code/OffsetReference.h"

using namespace code;

BEGIN_TEST(OffsetRefTest, Code) {
	Engine &e = gEngine();
	Arena *arena = code::arena(e);

	Int data[10] = { 0 };

	StrOffsetSource *ref0 = new (e) StrOffsetSource(S("ref0"));
	StrOffsetSource *ref1 = new (e) StrOffsetSource(S("ref1"));
	StrOffsetSource *ref2 = new (e) StrOffsetSource(S("ref2"));
	ref0->set(Offset::sInt);
	ref1->set(Offset::sByte);
	ref2->set(Offset::sInt * 5);

	Listing *l = new (e) Listing();
	Var param = l->createParam(ptrDesc(e));

	*l << prolog();

	*l << mov(ptrA, param);
	*l << mov(intRel(ptrA, ref2), intConst(10));

	*l << mov(eax, intConst(ref0));
	*l << add(eax, intConst(ref1));

	l->result = intDesc(e);
	*l << fnRet(eax);

	Binary *b = new (e) Binary(arena, l);
	typedef Int (*Fn)(Int *);
	Fn fn = (Fn)b->address();

	Int r = (*fn)(data);
	CHECK_EQ(r, 5);
	CHECK_EQ(data[5], 10);

	ref0->set(Offset::sLong);
	ref1->set(Offset::sInt);
	ref2->set(Offset::sInt * 2);

	{
		// Invalidate caches since we modify machine code.
		// Note: When running on Apple ARM, failure to invalidate caches
		// actually causes the tests below to fail sporadically.
		void *start = (void *)b->address();
		void *end = runtime::codeRefs(start);
		invalidateDCache(start, end);
		invalidateICache(start, end);
	}

	r = (*fn)(data);
	CHECK_EQ(r, 12);
	CHECK_EQ(data[2], 10);

} END_TEST