ctf.js
5.7 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
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
/*
* ctf.js
*
* Understand and parse all of the different JSON formats of CTF data and
* translate that into a series of node-ctype friendly pieces. The reason for
* the abstraction is to handle different changes in the file format.
*
* We have to be careful here that we don't end up using a name that is already
* a built in type.
*/
var mod_assert = require('assert');
var ASSERT = mod_assert.ok;
var ctf_versions = [ '1.0' ];
var ctf_entries = [ 'integer', 'float', 'typedef', 'struct' ];
var ctf_deftypes = [ 'int8_t', 'uint8_t', 'int16_t', 'uint16_t', 'int32_t',
'uint32_t', 'float', 'double' ];
function ctfParseInteger(entry, ctype)
{
var name, sign, len, type;
name = entry['name'];
if (!('signed' in entry['integer']))
throw (new Error('Malformed CTF JSON: integer missing ' +
'signed value'));
if (!('length' in entry['integer']))
throw (new Error('Malformed CTF JSON: integer missing ' +
'length value'));
sign = entry['integer']['signed'];
len = entry['integer']['length'];
type = null;
if (sign && len == 1)
type = 'int8_t';
else if (len == 1)
type = 'uint8_t';
else if (sign && len == 2)
type = 'int16_t';
else if (len == 2)
type = 'uint16_t';
else if (sign && len == 4)
type = 'int32_t';
else if (len == 4)
type = 'uint32_t';
else if (sign && len == 8)
type = 'int64_t';
else if (len == 8)
type = 'uint64_t';
if (type === null)
throw (new Error('Malformed CTF JSON: integer has ' +
'unsupported length and sign - ' + len + '/' + sign));
/*
* This means that this is the same as one of our built in types. If
* that's the case defining it would be an error. So instead of trying
* to typedef it, we'll return here.
*/
if (name == type)
return;
if (name == 'char') {
ASSERT(type == 'int8_t');
return;
}
ctype.typedef(name, type);
}
function ctfParseFloat(entry, ctype)
{
var name, len;
name = entry['name'];
if (!('length' in entry['float']))
throw (new Error('Malformed CTF JSON: float missing ' +
'length value'));
len = entry['float']['length'];
if (len != 4 && len != 8)
throw (new Error('Malformed CTF JSON: float has invalid ' +
'length value'));
if (len == 4) {
if (name == 'float')
return;
ctype.typedef(name, 'float');
} else if (len == 8) {
if (name == 'double')
return;
ctype.typedef(name, 'double');
}
}
function ctfParseTypedef(entry, ctype)
{
var name, type, ii;
name = entry['name'];
if (typeof (entry['typedef']) != 'string')
throw (new Error('Malformed CTF JSON: typedef value in not ' +
'a string'));
type = entry['typedef'];
/*
* We need to ensure that we're not looking at type that's one of our
* built in types. Traditionally in C a uint32_t would be a typedef to
* some kind of integer. However, those size types are built ins.
*/
for (ii = 0; ii < ctf_deftypes.length; ii++) {
if (name == ctf_deftypes[ii])
return;
}
ctype.typedef(name, type);
}
function ctfParseStruct(entry, ctype)
{
var name, type, ii, val, index, member, push;
member = [];
if (!Array.isArray(entry['struct']))
throw (new Error('Malformed CTF JSON: struct value is not ' +
'an array'));
for (ii = 0; ii < entry['struct'].length; ii++) {
val = entry['struct'][ii];
if (!('name' in val))
throw (new Error('Malformed CTF JSON: struct member ' +
'missing name'));
if (!('type' in val))
throw (new Error('Malformed CTF JSON: struct member ' +
'missing type'));
if (typeof (val['name']) != 'string')
throw (new Error('Malformed CTF JSON: struct member ' +
'name isn\'t a string'));
if (typeof (val['type']) != 'string')
throw (new Error('Malformed CTF JSON: struct member ' +
'type isn\'t a string'));
/*
* CTF version 2 specifies array names as <type> [<num>] where
* as node-ctype does this as <type>[<num>].
*/
name = val['name'];
type = val['type'];
index = type.indexOf(' [');
if (index != -1) {
type = type.substring(0, index) +
type.substring(index + 1, type.length);
}
push = {};
push[name] = { 'type': type };
member.push(push);
}
name = entry['name'];
ctype.typedef(name, member);
}
function ctfParseEntry(entry, ctype)
{
var ii, found;
if (!('name' in entry))
throw (new Error('Malformed CTF JSON: entry missing "name" ' +
'section'));
for (ii = 0; ii < ctf_entries.length; ii++) {
if (ctf_entries[ii] in entry)
found++;
}
if (found === 0)
throw (new Error('Malformed CTF JSON: found no entries'));
if (found >= 2)
throw (new Error('Malformed CTF JSON: found more than one ' +
'entry'));
if ('integer' in entry) {
ctfParseInteger(entry, ctype);
return;
}
if ('float' in entry) {
ctfParseFloat(entry, ctype);
return;
}
if ('typedef' in entry) {
ctfParseTypedef(entry, ctype);
return;
}
if ('struct' in entry) {
ctfParseStruct(entry, ctype);
return;
}
ASSERT(false, 'shouldn\'t reach here');
}
function ctfParseJson(json, ctype)
{
var version, ii;
ASSERT(json);
ASSERT(ctype);
if (!('metadata' in json))
throw (new Error('Invalid CTF JSON: missing metadata section'));
if (!('ctf2json_version' in json['metadata']))
throw (new Error('Invalid CTF JSON: missing ctf2json_version'));
version = json['metadata']['ctf2json_version'];
for (ii = 0; ii < ctf_versions.length; ii++) {
if (ctf_versions[ii] == version)
break;
}
if (ii == ctf_versions.length)
throw (new Error('Unsuported ctf2json_version: ' + version));
if (!('data' in json))
throw (new Error('Invalid CTF JSON: missing data section'));
if (!Array.isArray(json['data']))
throw (new Error('Malformed CTF JSON: data section is not ' +
'an array'));
for (ii = 0; ii < json['data'].length; ii++)
ctfParseEntry(json['data'][ii], ctype);
}
exports.ctfParseJson = ctfParseJson;