forked from jfrijters/Managed.Reflection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodBody.cs
More file actions
162 lines (149 loc) · 6.63 KB
/
Copy pathMethodBody.cs
File metadata and controls
162 lines (149 loc) · 6.63 KB
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
/*
The MIT License (MIT)
Copyright (C) 2009 Jeroen Frijters
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using Managed.Reflection.Reader;
namespace Managed.Reflection
{
public sealed class MethodBody
{
private readonly IList<ExceptionHandlingClause> exceptionClauses;
private readonly IList<LocalVariableInfo> locals;
private readonly bool initLocals;
private readonly int maxStack;
private readonly int localVarSigTok;
private byte[] body;
internal MethodBody(ModuleReader module, int rva, IGenericContext context)
{
const byte CorILMethod_TinyFormat = 0x02;
const byte CorILMethod_FatFormat = 0x03;
const byte CorILMethod_MoreSects = 0x08;
const byte CorILMethod_InitLocals = 0x10;
const byte CorILMethod_Sect_EHTable = 0x01;
const byte CorILMethod_Sect_FatFormat = 0x40;
const byte CorILMethod_Sect_MoreSects = 0x80;
List<ExceptionHandlingClause> exceptionClauses = new List<ExceptionHandlingClause>();
List<LocalVariableInfo> locals = new List<LocalVariableInfo>();
Stream stream = module.GetStream();
module.SeekRVA(rva);
BinaryReader br = new BinaryReader(stream);
byte b = br.ReadByte();
if ((b & 3) == CorILMethod_TinyFormat)
{
initLocals = true;
body = br.ReadBytes(b >> 2);
maxStack = 8;
}
else if ((b & 3) == CorILMethod_FatFormat)
{
initLocals = (b & CorILMethod_InitLocals) != 0;
short flagsAndSize = (short)(b | (br.ReadByte() << 8));
if ((flagsAndSize >> 12) != 3)
{
throw new BadImageFormatException("Fat format method header size should be 3");
}
maxStack = br.ReadUInt16();
int codeLength = br.ReadInt32();
localVarSigTok = br.ReadInt32();
body = br.ReadBytes(codeLength);
if ((b & CorILMethod_MoreSects) != 0)
{
stream.Position = (stream.Position + 3) & ~3;
int hdr = br.ReadInt32();
if ((hdr & CorILMethod_Sect_MoreSects) != 0 || (hdr & CorILMethod_Sect_EHTable) == 0)
{
throw new NotImplementedException();
}
else if ((hdr & CorILMethod_Sect_FatFormat) != 0)
{
int count = ComputeExceptionCount((hdr >> 8) & 0xFFFFFF, 24);
for (int i = 0; i < count; i++)
{
int flags = br.ReadInt32();
int tryOffset = br.ReadInt32();
int tryLength = br.ReadInt32();
int handlerOffset = br.ReadInt32();
int handlerLength = br.ReadInt32();
int classTokenOrFilterOffset = br.ReadInt32();
exceptionClauses.Add(new ExceptionHandlingClause(module, flags, tryOffset, tryLength, handlerOffset, handlerLength, classTokenOrFilterOffset, context));
}
}
else
{
int count = ComputeExceptionCount((hdr >> 8) & 0xFF, 12);
for (int i = 0; i < count; i++)
{
int flags = br.ReadUInt16();
int tryOffset = br.ReadUInt16();
int tryLength = br.ReadByte();
int handlerOffset = br.ReadUInt16();
int handlerLength = br.ReadByte();
int classTokenOrFilterOffset = br.ReadInt32();
exceptionClauses.Add(new ExceptionHandlingClause(module, flags, tryOffset, tryLength, handlerOffset, handlerLength, classTokenOrFilterOffset, context));
}
}
}
if (localVarSigTok != 0)
{
ByteReader sig = module.GetStandAloneSig((localVarSigTok & 0xFFFFFF) - 1);
Signature.ReadLocalVarSig(module, sig, context, locals);
}
}
else
{
throw new BadImageFormatException();
}
this.exceptionClauses = exceptionClauses.AsReadOnly();
this.locals = locals.AsReadOnly();
}
private static int ComputeExceptionCount(int size, int itemLength)
{
// LAMESPEC according to the spec, the count should be calculated as "(size - 4) / itemLength",
// FXBUG but to workaround a VB compiler bug that specifies the size incorrectly,
// we do a truncating division instead.
return size / itemLength;
}
public IList<ExceptionHandlingClause> ExceptionHandlingClauses
{
get { return exceptionClauses; }
}
public bool InitLocals
{
get { return initLocals; }
}
public IList<LocalVariableInfo> LocalVariables
{
get { return locals; }
}
public byte[] GetILAsByteArray()
{
return body;
}
public int LocalSignatureMetadataToken
{
get { return localVarSigTok; }
}
public int MaxStackSize
{
get { return maxStack; }
}
}
}