File: XADRARVirtualMachine.m

package info (click to toggle)
unar 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,664 kB
  • sloc: ansic: 52,939; objc: 39,563; cpp: 4,074; makefile: 99; perl: 10
file content (388 lines) | stat: -rw-r--r-- 9,670 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#import "XADRARVirtualMachine.h"
#import "XADException.h"
#import "CRC.h"




uint32_t CSInputNextRARVMNumber(CSInputBuffer *input)
{ 
	switch(CSInputNextBitString(input,2))
	{
		case 0: return CSInputNextBitString(input,4);
		case 1:
		{
			int val=CSInputNextBitString(input,8);
			if(val>=16) return val;
			else return 0xffffff00|(val<<4)|CSInputNextBitString(input,4);
		}
		case 2: return CSInputNextBitString(input,16);
		default: return CSInputNextLongBitString(input,32);
	}
}



@implementation XADRARVirtualMachine

-(id)init
{
	if((self=[super init]))
	{
		InitializeRARVirtualMachine(&vm);
	}
	return self;
}

-(void)dealloc
{
	//CleanupRARVirutalMachine(&vm);
	[super dealloc];
}

-(uint8_t *)memory { return vm.memory; }

-(void)setRegisters:(uint32_t *)newregisters
{
	SetRARVirtualMachineRegisters(&vm,newregisters);
}

-(void)readMemoryAtAddress:(uint32_t)address length:(int)length toBuffer:(uint8_t *)buffer
{
	memcpy(buffer,&vm.memory[address],length);
}

-(void)readMemoryAtAddress:(uint32_t)address length:(int)length toMutableData:(NSMutableData *)data
{
	[self readMemoryAtAddress:address length:length toBuffer:[data mutableBytes]];
}

-(void)writeMemoryAtAddress:(uint32_t)address length:(int)length fromBuffer:(const uint8_t *)buffer
{
	memcpy(&vm.memory[address],buffer,length);
}

-(void)writeMemoryAtAddress:(uint32_t)address length:(int)length fromData:(NSData *)data
{
	[self writeMemoryAtAddress:address length:length fromBuffer:[data bytes]];
}

-(uint32_t)readWordAtAddress:(uint32_t)address
{
	return RARVirtualMachineRead32(&vm,address);
}

-(void)writeWordAtAddress:(uint32_t)address value:(uint32_t)value
{
	RARVirtualMachineWrite32(&vm,address,value);
}

-(BOOL)executeProgramCode:(XADRARProgramCode *)code
{
	return ExecuteRARCode(&vm,[code opcodes],[code numberOfOpcodes]);
}

@end





@implementation XADRARProgramCode

-(id)initWithByteCode:(const uint8_t *)bytes length:(int)length
{
	if((self=[super init]))
	{
		opcodes=[NSMutableData new];
		staticdata=nil;
		globalbackup=[NSMutableData new];

		if([self parseByteCode:bytes length:length]) return self;

		[self release];
	}

	return nil;
}

-(void)dealloc
{
	[opcodes release];
	[staticdata release];
	[globalbackup release];
	[super dealloc];
}

-(BOOL)parseByteCode:(const uint8_t *)bytes length:(int)length
{
	// TODO: deal with exceptions causing memory leaks

	if(length==0) return NO;

	// Check XOR sum.
	uint8_t xor=0;
	for(int i=1;i<length;i++) xor^=bytes[i];
	if(xor!=bytes[0]) return NO;

	// Calculate CRC for fast native path replacements.
	fingerprint=XADCalculateCRC(0xffffffff,bytes,length,XADCRCTable_edb88320)^0xffffffff;
	fingerprint|=(uint64_t)length<<32;

	CSInputBuffer *input=CSInputBufferAllocWithBuffer(&bytes[1],length-1,0);

	// Read static data, if any.
	if(CSInputNextBit(input))
	{
		int length=CSInputNextRARVMNumber(input)+1;
		NSMutableData *data=[NSMutableData dataWithLength:length];
		uint8_t *databytes=[data mutableBytes];

		for(int i=0;i<length;i++) databytes[i]=CSInputNextBitString(input,8);

		staticdata=data;
	}

	// Read instructions.
	while(CSInputBitsLeftInBuffer(input)>=8)
	{
		[opcodes increaseLengthBy:sizeof(RAROpcode)];
		RAROpcode *opcodearray=[self opcodes];
		int currinstruction=[self numberOfOpcodes]-1;
		RAROpcode *opcode=&opcodearray[currinstruction];

		int instruction=CSInputNextBitString(input,4);
		if(instruction&0x08) instruction=((instruction<<2)|CSInputNextBitString(input,2))-24;

		BOOL bytemode=NO;
		if(RARInstructionHasByteMode(instruction)) bytemode=CSInputNextBitString(input,1);

		SetRAROpcodeInstruction(opcode,instruction,bytemode);

		int numargs=NumberOfRARInstructionOperands(instruction);

		if(numargs>=1)
		{
			unsigned int addressingmode;
			uint32_t value;
			[self parseOperandFromBuffer:input addressingMode:&addressingmode value:&value
			byteMode:bytemode isRelativeJump:RARInstructionIsRelativeJump(instruction)
			currentInstructionOffset:currinstruction];
			SetRAROpcodeOperand1(opcode,addressingmode,value);
		}
		if(numargs==2)
		{
			unsigned int addressingmode;
			uint32_t value;
			[self parseOperandFromBuffer:input addressingMode:&addressingmode value:&value
			 byteMode:bytemode isRelativeJump:NO currentInstructionOffset:0];
			SetRAROpcodeOperand2(opcode,addressingmode,value);
		}
	}

	// Check if program is properly terminated, if not, add a ret opcode.
	if(!IsProgramTerminated([self opcodes],[self numberOfOpcodes]))
	{
		[opcodes increaseLengthBy:sizeof(RAROpcode)];
		RAROpcode *opcodearray=[self opcodes];
		int currinstruction=[self numberOfOpcodes]-1;
		RAROpcode *opcode=&opcodearray[currinstruction];

		SetRAROpcodeInstruction(opcode,RARRetInstruction,false);
	}

	CSInputBufferFree(input);

	return PrepareRAROpcodes([self opcodes],[self numberOfOpcodes]);
}

-(void)parseOperandFromBuffer:(CSInputBuffer *)input addressingMode:(unsigned int *)modeptr
value:(uint32_t *)valueptr byteMode:(BOOL)bytemode isRelativeJump:(BOOL)isrel currentInstructionOffset:(int)instructionoffset
{
	if(CSInputNextBit(input))
	{
		int reg=CSInputNextBitString(input,3);
		*modeptr=RARRegisterAddressingMode(reg);
	}
	else
	{
		if(CSInputNextBit(input))
		{
			if(CSInputNextBit(input))
			{
				if(CSInputNextBit(input))
				{
					*valueptr=CSInputNextRARVMNumber(input);
					*modeptr=RARAbsoluteAddressingMode;
				}
				else
				{
					int reg=CSInputNextBitString(input,3);
					*valueptr=CSInputNextRARVMNumber(input);
					*modeptr=RARIndexedAbsoluteAddressingMode(reg);
				}
			}
			else
			{
				int reg=CSInputNextBitString(input,3);
				*modeptr=RARRegisterIndirectAddressingMode(reg);
			}
		}
		else
		{
			if(bytemode)
			{
				*valueptr=CSInputNextBitString(input,8);
				*modeptr=RARImmediateAddressingMode;
				//return [NSString stringWithFormat:@"%d",val];
			}
			else
			{
				*valueptr=CSInputNextRARVMNumber(input);
				*modeptr=RARImmediateAddressingMode;
			}

			if(isrel)
			{
				if(*valueptr>=256) *valueptr-=256; // Absolute address
				else
				{
					// Relative address
					if(*valueptr>=136) *valueptr-=264;
					else if(*valueptr>=16) *valueptr-=8;
					else if(*valueptr>=8) *valueptr-=16;
					*valueptr+=instructionoffset;
				}
			}
		}
	}
}

-(RAROpcode *)opcodes { return [opcodes mutableBytes]; }

-(int)numberOfOpcodes { return [opcodes length]/sizeof(RAROpcode); }

-(NSData *)staticData { return staticdata; }

-(NSMutableData *)globalBackup { return globalbackup; }

-(uint64_t)fingerprint { return fingerprint; }

-(NSString *)disassemble
{
	RAROpcode *opcodearray=[self opcodes];
	int numopcodes=[self numberOfOpcodes];

	NSMutableString *disassembly=[NSMutableString string];

	for(int i=0;i<numopcodes;i++)
	{
		[disassembly appendFormat:@"%04x\t%s\n",i,DescribeRAROpcode(&opcodearray[i])];
	}

	return disassembly;
}

@end



@implementation XADRARProgramInvocation

-(id)initWithProgramCode:(XADRARProgramCode *)code globalData:(NSData *)data registers:(uint32_t *)registers
{
	if((self=[super init]))
	{
		programcode=[code retain];

		if(data)
		{
			globaldata=[[NSMutableData alloc] initWithData:data];
			if([globaldata length]<RARProgramSystemGlobalSize) [globaldata setLength:RARProgramSystemGlobalSize];
		}
		else globaldata=[[NSMutableData alloc] initWithLength:RARProgramSystemGlobalSize];

		if(registers) memcpy(initialregisters,registers,sizeof(initialregisters));
		else memset(initialregisters,0,sizeof(initialregisters));
	}
	return self;
}

-(void)dealloc
{
	[programcode release];
	[globaldata release];
	[super dealloc];
}

-(XADRARProgramCode *)programCode { return programcode; }

-(NSData *)globalData { return globaldata; }

-(uint32_t)initialRegisterState:(int)n
{
	if(n<0||n>=8) [NSException raise:NSRangeException format:@"Attempted to set non-existent register"];

	return initialregisters[n];
}

-(void)setInitialRegisterState:(int)n toValue:(uint32_t)val
{
	if(n<0||n>=8) [NSException raise:NSRangeException format:@"Attempted to set non-existent register"];

	initialregisters[n]=val;
}

-(void)setGlobalValueAtOffset:(int)offs toValue:(uint32_t)val
{
	if(offs<0||offs+4>[globaldata length]) [NSException raise:NSRangeException format:@"Attempted to write outside global memory"];

	uint8_t *bytes=[globaldata mutableBytes];
	CSSetUInt32LE(&bytes[offs],val);
}

-(void)backupGlobalData
{
	NSMutableData *backup=[programcode globalBackup];
	if([globaldata length]>RARProgramSystemGlobalSize) [backup setData:globaldata];
	else [backup setLength:0];
}

-(void)restoreGlobalDataIfAvailable
{
	NSMutableData *backup=[programcode globalBackup];
	if([backup length]>RARProgramSystemGlobalSize) [globaldata setData:backup];
}

-(BOOL)executeOnVitualMachine:(XADRARVirtualMachine *)vm
{
	int globallength=[globaldata length];
	if(globallength>RARProgramGlobalSize) globallength=RARProgramGlobalSize;
	[vm writeMemoryAtAddress:RARProgramGlobalAddress length:globallength fromData:globaldata];

	NSData *staticdata=[programcode staticData];
	if(staticdata)
	{
		int staticlength=[staticdata length];
		if(staticlength>RARProgramGlobalSize-globallength) staticlength=RARProgramGlobalSize-globallength;
		[vm writeMemoryAtAddress:RARProgramGlobalAddress length:staticlength fromData:staticdata];
	}

	[vm setRegisters:initialregisters];

	if(![vm executeProgramCode:programcode]) return NO;

	uint32_t newgloballength=[vm readWordAtAddress:RARProgramGlobalAddress+0x30];
	if(newgloballength>RARProgramUserGlobalSize) newgloballength=RARProgramUserGlobalSize;
	if(newgloballength>0)
	{
		[vm readMemoryAtAddress:RARProgramGlobalAddress
		length:newgloballength+RARProgramSystemGlobalSize
		toMutableData:[globaldata mutableBytes]];
	}
	else [globaldata setLength:0];

	return YES;
}

@end