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
|
//===- NaClBitcodeHeader.cpp ----------------------------------------------===//
// PNaCl bitcode header reader.
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h"
#include "llvm/Bitcode/NaCl/NaClReaderWriter.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/StreamableMemoryObject.h"
#include <limits>
#include <cstring>
#include <iomanip>
using namespace llvm;
NaClBitcodeHeaderField::NaClBitcodeHeaderField()
: ID(kInvalid), FType(kBufferType), Len(0), Data(0) {}
NaClBitcodeHeaderField::NaClBitcodeHeaderField(Tag MyID, uint32_t MyValue)
: ID(MyID), FType(kUInt32Type), Len(4), Data(new uint8_t[4]) {
Data[0] = static_cast<uint8_t>(MyValue & 0xFF);
Data[1] = static_cast<uint8_t>((MyValue >> 8) & 0xFF);
Data[2] = static_cast<uint8_t>((MyValue >> 16) & 0xFF);
Data[3] = static_cast<uint8_t>((MyValue >> 24) & 0xFF);
}
uint32_t NaClBitcodeHeaderField::GetUInt32Value() const {
assert(FType == kUInt32Type && "Header field must be uint32");
return static_cast<uint32_t>(Data[0]) |
(static_cast<uint32_t>(Data[1]) << 8) |
(static_cast<uint32_t>(Data[2]) << 16) |
(static_cast<uint32_t>(Data[2]) << 24);
}
NaClBitcodeHeaderField::NaClBitcodeHeaderField(Tag MyID, size_t MyLen,
uint8_t *MyData)
: ID(MyID), FType(kBufferType), Len(MyLen), Data(new uint8_t[MyLen]) {
for (size_t i = 0; i < MyLen; ++i) {
Data[i] = MyData[i];
}
}
bool NaClBitcodeHeaderField::Write(uint8_t *Buf, size_t BufLen) const {
size_t FieldsLen = kTagLenSize + Len;
size_t PadLen = (WordSize - (FieldsLen & (WordSize-1))) & (WordSize-1);
// Ensure buffer is large enough and that length can be represented
// in 32 bits
if (BufLen < FieldsLen + PadLen ||
Len > std::numeric_limits<FixedSubfield>::max())
return false;
WriteFixedSubfield(EncodeTypedID(), Buf);
WriteFixedSubfield(static_cast<FixedSubfield>(Len),
Buf + sizeof(FixedSubfield));
memcpy(Buf + kTagLenSize, Data, Len);
// Pad out to word alignment
if (PadLen) {
memset(Buf + FieldsLen, 0, PadLen);
}
return true;
}
bool NaClBitcodeHeaderField::Read(const uint8_t *Buf, size_t BufLen) {
if (BufLen < kTagLenSize)
return false;
FixedSubfield IdField;
ReadFixedSubfield(&IdField, Buf);
FixedSubfield LengthField;
ReadFixedSubfield(&LengthField, Buf + sizeof(FixedSubfield));
size_t Length = static_cast<size_t>(LengthField);
if (BufLen < kTagLenSize + Length)
return false;
if (Len != Length) {
// Need to reallocate data buffer.
if (Data)
delete[] Data;
Data = new uint8_t[Length];
}
Len = Length;
DecodeTypedID(IdField, ID, FType);
memcpy(Data, Buf + kTagLenSize, Len);
return true;
}
std::string NaClBitcodeHeaderField::Contents() const {
std::string buffer;
raw_string_ostream ss(buffer);
switch (ID) {
case kPNaClVersion:
ss << "PNaCl Version";
break;
case kInvalid:
ss << "Invalid";
break;
default:
report_fatal_error("PNaCl bitcode file contains unknown field tag");
}
ss << ": ";
switch (FType) {
case kUInt32Type:
ss << GetUInt32Value();
break;
case kBufferType:
ss << "[";
for (size_t i = 0; i < Len; ++i) {
if (i)
ss << " ";
ss << format("%02x", Data[i]);
}
ss << "]";
break;
default:
report_fatal_error("PNaCL bitcode file contains unknown field type");
}
return ss.str();
}
NaClBitcodeHeader::NaClBitcodeHeader()
: HeaderSize(0), UnsupportedMessage(), IsSupportedFlag(false),
IsReadableFlag(false), PNaClVersion(0) {}
NaClBitcodeHeader::~NaClBitcodeHeader() {
for (std::vector<NaClBitcodeHeaderField *>::const_iterator
Iter = Fields.begin(),
IterEnd = Fields.end();
Iter != IterEnd; ++Iter) {
delete *Iter;
}
}
bool NaClBitcodeHeader::ReadPrefix(const unsigned char *BufPtr,
const unsigned char *BufEnd,
unsigned &NumFields, unsigned &NumBytes) {
// Must contain PEXE.
if (!isNaClBitcode(BufPtr, BufEnd))
return true;
BufPtr += WordSize;
// Read #Fields and number of bytes needed for the header.
if (BufPtr + WordSize > BufEnd)
return true;
NumFields = static_cast<unsigned>(BufPtr[0]) |
(static_cast<unsigned>(BufPtr[1]) << 8);
NumBytes = static_cast<unsigned>(BufPtr[2]) |
(static_cast<unsigned>(BufPtr[3]) << 8);
BufPtr += WordSize;
return false;
}
bool NaClBitcodeHeader::ReadFields(const unsigned char *BufPtr,
const unsigned char *BufEnd,
unsigned NumFields, unsigned NumBytes) {
HeaderSize = NumBytes + (2 * WordSize);
// Read in each field.
for (size_t i = 0; i < NumFields; ++i) {
NaClBitcodeHeaderField *Field = new NaClBitcodeHeaderField();
Fields.push_back(Field);
if (!Field->Read(BufPtr, BufEnd - BufPtr))
return true;
size_t FieldSize = Field->GetTotalSize();
BufPtr += FieldSize;
}
return false;
}
bool NaClBitcodeHeader::Read(const unsigned char *&BufPtr,
const unsigned char *&BufEnd) {
unsigned NumFields;
unsigned NumBytes;
if (ReadPrefix(BufPtr, BufEnd, NumFields, NumBytes))
return true;
BufPtr += 2 * WordSize;
if (ReadFields(BufPtr, BufEnd, NumFields, NumBytes))
return true;
BufPtr += NumBytes;
InstallFields();
return false;
}
bool NaClBitcodeHeader::Read(StreamableMemoryObject *Bytes) {
unsigned NumFields;
unsigned NumBytes;
{
unsigned char Buffer[2 * WordSize];
if (Bytes->readBytes(0, sizeof(Buffer), Buffer, NULL) ||
ReadPrefix(Buffer, Buffer + sizeof(Buffer), NumFields, NumBytes))
return true;
}
uint8_t *Header = new uint8_t[NumBytes];
bool failed =
Bytes->readBytes(2 * WordSize, NumBytes, Header, NULL) ||
ReadFields(Header, Header + NumBytes, NumFields, NumBytes);
delete[] Header;
if (failed)
return true;
InstallFields();
return false;
}
NaClBitcodeHeaderField *
NaClBitcodeHeader::GetTaggedField(NaClBitcodeHeaderField::Tag ID) const {
for (std::vector<NaClBitcodeHeaderField *>::const_iterator
Iter = Fields.begin(),
IterEnd = Fields.end();
Iter != IterEnd; ++Iter) {
if ((*Iter)->GetID() == ID) {
return *Iter;
}
}
return 0;
}
NaClBitcodeHeaderField *NaClBitcodeHeader::GetField(size_t index) const {
if (index >= Fields.size())
return 0;
return Fields[index];
}
NaClBitcodeHeaderField *
|