{{ message }}
Repository navigation
Expand file tree
/
Copy pathattributes.zig
More file actions
593 lines (486 loc) · 20 KB
/
Copy pathattributes.zig
File metadata and controls
593 lines (486 loc) · 20 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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
const std = @import("std");
const ConstantPool = @import("ConstantPool.zig");
const logger = std.log.scoped(.cf_attributes);
// TODO: Implement all attribute types
pub const AttributeInfo = union(enum) {
constant_value: ConstantValueAttribute,
runtime_visible_annotations: RuntimeVisibleAnnotationsAttribute,
deprecated: DeprecatedAttribute,
code: CodeAttribute,
line_number_table: LineNumberTableAttribute,
source_file: SourceFileAttribute,
exceptions: ExceptionsAttribute,
unknown: void,
pub fn decode(constant_pool: *ConstantPool, allocator: std.mem.Allocator, reader: anytype) anyerror!AttributeInfo {
var attribute_name_index = try reader.readIntBig(u16);
var attribute_length = try reader.readIntBig(u32);
var info = try allocator.alloc(u8, attribute_length);
defer allocator.free(info);
_ = try reader.readAll(info);
var fbs = std.io.fixedBufferStream(info);
var name = constant_pool.get(attribute_name_index).utf8.bytes;
inline for (std.meta.fields(AttributeInfo)) |field| {
if (field.type == void) {} else {
if (std.mem.eql(u8, @field(field.type, "name"), name)) {
return @unionInit(AttributeInfo, field.name, try @field(field.type, "decode")(constant_pool, allocator, fbs.reader()));
}
}
}
logger.err("Could not decode attribute: {s}", .{name});
return .unknown;
}
pub fn calcAttrLen(self: AttributeInfo) u32 {
inline for (std.meta.fields(AttributeInfo)) |field| {
if (field.type == void) continue;
if (std.meta.activeTag(self) == @field(std.meta.Tag(AttributeInfo), field.name)) {
return @field(self, field.name).calcAttrLen() + 6; // 6 intro bytes!
}
}
unreachable;
}
pub fn deinit(self: *AttributeInfo) void {
inline for (std.meta.fields(AttributeInfo)) |field| {
if (field.type == void) continue;
if (std.meta.activeTag(self.*) == @field(std.meta.Tag(AttributeInfo), field.name)) {
@field(self, field.name).deinit();
}
}
}
pub fn encode(self: AttributeInfo, writer: anytype) !void {
inline for (std.meta.fields(AttributeInfo)) |field| {
if (field.type == void) continue;
if (std.meta.activeTag(self) == @field(std.meta.Tag(AttributeInfo), field.name)) {
var attr = @field(self, field.name);
try writer.writeIntBig(u16, try attr.constant_pool.locateUtf8Entry(@field(field.type, "name")));
try writer.writeIntBig(u32, attr.calcAttrLen());
try attr.encode(writer);
}
}
}
};
pub const ExceptionTableEntry = packed struct {
/// Where the exception handler becomes active (inclusive)
start_pc: u16,
/// Where it becomes inactive (exclusive)
end_pc: u16,
/// Start of handler
handler_pc: u16,
/// Index into constant pool
catch_type: u16,
pub fn decode(reader: anytype) !ExceptionTableEntry {
var entry: ExceptionTableEntry = undefined;
entry.start_pc = try reader.readIntBig(u16);
entry.end_pc = try reader.readIntBig(u16);
entry.handler_pc = try reader.readIntBig(u16);
entry.catch_type = try reader.readIntBig(u16);
return entry;
}
pub fn encode(self: ExceptionTableEntry, writer: anytype) !void {
try writer.writeIntBig(u16, self.start_pc);
try writer.writeIntBig(u16, self.end_pc);
try writer.writeIntBig(u16, self.handler_pc);
try writer.writeIntBig(u16, self.catch_type);
}
};
pub const CodeAttribute = struct {
pub const name = "Code";
allocator: std.mem.Allocator,
constant_pool: *ConstantPool,
max_stack: u16,
max_locals: u16,
code: std.ArrayListUnmanaged(u8),
exception_table: std.ArrayListUnmanaged(ExceptionTableEntry),
attributes: std.ArrayListUnmanaged(AttributeInfo),
pub fn decode(constant_pool: *ConstantPool, allocator: std.mem.Allocator, reader: anytype) !CodeAttribute {
var max_stack = try reader.readIntBig(u16);
var max_locals = try reader.readIntBig(u16);
var code_length = try reader.readIntBig(u32);
var code = try std.ArrayListUnmanaged(u8).initCapacity(allocator, code_length);
code.items.len = code_length;
_ = try reader.readAll(code.items);
var exception_table_len = try reader.readIntBig(u16);
var exception_table = try std.ArrayListUnmanaged(ExceptionTableEntry).initCapacity(allocator, exception_table_len);
exception_table.items.len = exception_table_len;
for (exception_table.items) |*et| et.* = try ExceptionTableEntry.decode(reader);
var attributes_length = try reader.readIntBig(u16);
var attributes_index: usize = 0;
var attributes = try std.ArrayListUnmanaged(AttributeInfo).initCapacity(allocator, attributes_length);
while (attributes_index < attributes_length) : (attributes_index += 1) {
var decoded = try AttributeInfo.decode(constant_pool, allocator, reader);
if (decoded == .unknown) {
attributes_length -= 1;
continue;
}
try attributes.append(allocator, decoded);
}
return CodeAttribute{
.allocator = allocator,
.constant_pool = constant_pool,
.max_stack = max_stack,
.max_locals = max_locals,
.code = code,
.exception_table = exception_table,
.attributes = attributes,
};
}
pub fn calcAttrLen(self: CodeAttribute) u32 {
var len: u32 = 2 + 2 + 4 + @intCast(u32, self.code.items.len) + 2 + 2;
for (self.attributes.items) |att| len += att.calcAttrLen();
len += 8 * @intCast(u32, self.exception_table.items.len);
return len;
}
pub fn encode(self: CodeAttribute, writer: anytype) anyerror!void {
try writer.writeIntBig(u16, self.max_stack);
try writer.writeIntBig(u16, self.max_locals);
try writer.writeIntBig(u32, @intCast(u32, self.code.items.len));
try writer.writeAll(self.code.items);
try writer.writeIntBig(u16, @intCast(u16, self.exception_table.items.len));
for (self.exception_table.items) |et| try et.encode(writer);
try writer.writeIntBig(u16, @intCast(u16, self.attributes.items.len));
for (self.attributes.items) |at| try at.encode(writer);
}
pub fn deinit(self: *CodeAttribute) void {
self.code.deinit(self.allocator);
self.exception_table.deinit(self.allocator);
for (self.attributes.items) |*attr| {
attr.deinit();
}
self.attributes.deinit(self.allocator);
}
};
pub const LineNumberTableEntry = struct {
/// The index into the code array at which the code for a new line in the original source file begins
start_pc: u16,
/// The corresponding line number in the original source file
line_number: u16,
pub fn decode(reader: anytype) !LineNumberTableEntry {
var entry: LineNumberTableEntry = undefined;
entry.start_pc = try reader.readIntBig(u16);
entry.line_number = try reader.readIntBig(u16);
return entry;
}
pub fn encode(self: LineNumberTableEntry, writer: anytype) !void {
try writer.writeIntBig(u16, self.start_pc);
try writer.writeIntBig(u16, self.line_number);
}
};
pub const LineNumberTableAttribute = struct {
pub const name = "LineNumberTable";
allocator: std.mem.Allocator,
constant_pool: *ConstantPool,
line_number_table: std.ArrayListUnmanaged(LineNumberTableEntry),
pub fn decode(constant_pool: *ConstantPool, allocator: std.mem.Allocator, reader: anytype) !LineNumberTableAttribute {
var line_number_table_length = try reader.readIntBig(u16);
var line_number_table = try std.ArrayListUnmanaged(LineNumberTableEntry).initCapacity(allocator, line_number_table_length);
line_number_table.items.len = line_number_table_length;
for (line_number_table.items) |*entry| entry.* = try LineNumberTableEntry.decode(reader);
return LineNumberTableAttribute{
.allocator = allocator,
.constant_pool = constant_pool,
.line_number_table = line_number_table,
};
}
pub fn calcAttrLen(self: LineNumberTableAttribute) u32 {
var len: u32 = 2 + 4 * @intCast(u32, self.line_number_table.items.len);
return len;
}
pub fn encode(self: LineNumberTableAttribute, writer: anytype) anyerror!void {
try writer.writeIntBig(u16, @intCast(u16, self.line_number_table.items.len));
for (self.line_number_table.items) |entry| try entry.encode(writer);
}
pub fn deinit(self: *LineNumberTableAttribute) void {
self.line_number_table.deinit(self.allocator);
}
};
pub const SourceFileAttribute = struct {
pub const name = "SourceFile";
allocator: std.mem.Allocator,
constant_pool: *ConstantPool,
source_file_index: u16,
pub fn decode(constant_pool: *ConstantPool, allocator: std.mem.Allocator, reader: anytype) !SourceFileAttribute {
return SourceFileAttribute{
.allocator = allocator,
.constant_pool = constant_pool,
.source_file_index = try reader.readIntBig(u16),
};
}
pub fn calcAttrLen(self: SourceFileAttribute) u32 {
_ = self;
return 2;
}
pub fn encode(self: SourceFileAttribute, writer: anytype) anyerror!void {
try writer.writeIntBig(u16, self.source_file_index);
}
pub fn deinit(self: *SourceFileAttribute) void {
_ = self;
}
};
pub const ExceptionsAttribute = struct {
pub const name = "Exceptions";
allocator: std.mem.Allocator,
constant_pool: *ConstantPool,
exception_index_table: std.ArrayListUnmanaged(u16),
pub fn decode(constant_pool: *ConstantPool, allocator: std.mem.Allocator, reader: anytype) !ExceptionsAttribute {
var exception_index_table_length = try reader.readIntBig(u16);
var exception_index_table = try std.ArrayListUnmanaged(u16).initCapacity(allocator, exception_index_table_length);
exception_index_table.items.len = exception_index_table_length;
for (exception_index_table.items) |*entry| entry.* = try reader.readIntBig(u16);
return ExceptionsAttribute{
.allocator = allocator,
.constant_pool = constant_pool,
.exception_index_table = exception_index_table,
};
}
pub fn calcAttrLen(self: ExceptionsAttribute) u32 {
var len: u32 = 2 + 2 * @intCast(u32, self.exception_index_table.items.len);
return len;
}
pub fn encode(self: ExceptionsAttribute, writer: anytype) anyerror!void {
try writer.writeIntBig(u16, @intCast(u16, self.exception_index_table.items.len));
for (self.exception_index_table.items) |entry| try writer.writeIntBig(u16, entry);
}
pub fn deinit(self: *ExceptionsAttribute) void {
self.exception_index_table.deinit(self.allocator);
}
};
pub const ConstantValueAttribute = struct {
pub const name = "ConstantValue";
allocator: std.mem.Allocator,
constant_pool: *ConstantPool,
constantvalue_index: u16,
pub fn decode(constant_pool: *ConstantPool, allocator: std.mem.Allocator, reader: anytype) !ConstantValueAttribute {
return ConstantValueAttribute{
.allocator = allocator,
.constant_pool = constant_pool,
.constantvalue_index = try reader.readIntBig(u16),
};
}
pub fn calcAttrLen(self: ConstantValueAttribute) u32 {
_ = self;
return 2;
}
pub fn encode(self: ConstantValueAttribute, writer: anytype) anyerror!void {
try writer.writeIntBig(u16, self.constantvalue_index);
}
pub fn deinit(self: *ConstantValueAttribute) void {
_ = self;
}
};
pub const DeprecatedAttribute = struct {
pub const name = "Deprecated";
allocator: std.mem.Allocator,
constant_pool: *ConstantPool,
pub fn decode(constant_pool: *ConstantPool, allocator: std.mem.Allocator, reader: anytype) !DeprecatedAttribute {
_ = reader;
return DeprecatedAttribute{
.allocator = allocator,
.constant_pool = constant_pool,
};
}
pub fn calcAttrLen(self: DeprecatedAttribute) u32 {
_ = self;
return 0;
}
pub fn encode(self: DeprecatedAttribute, writer: anytype) anyerror!void {
_ = self;
_ = writer;
}
pub fn deinit(self: *DeprecatedAttribute) void {
_ = self;
}
};
// Annotations
pub const ElementTag = enum(u8) {
// Primitive types
Byte = 'B',
Char = 'C',
Double = 'D',
Float = 'F',
Int = 'I',
Long = 'J',
Short = 'S',
Boolean = 'Z',
// Other legal values
String = 's',
EnumConstant = 'e',
Class = 'c',
AnnotationType = '@',
Array = '[',
};
pub const ElementValue = union(ElementTag) {
Byte: u16,
Char: u16,
Double: u16,
Float: u16,
Int: u16,
Long: u16,
Short: u16,
Boolean: u16,
String: u16,
EnumConstant: struct {
type_name_index: u16,
const_name_index: u16,
},
Class: u16,
AnnotationType: *Annotation,
Array: struct {
num_values: u16,
values: []ElementValue,
},
pub fn decode(allocator: std.mem.Allocator, reader: anytype) anyerror!ElementValue {
const value_tag = try reader.readEnum(ElementTag, .Big);
const value: ElementValue = switch (value_tag) {
.Byte => .{ .Byte = try reader.readIntBig(u16) },
.Char => .{ .Char = try reader.readIntBig(u16) },
.Double => .{ .Double = try reader.readIntBig(u16) },
.Float => .{ .Float = try reader.readIntBig(u16) },
.Int => .{ .Int = try reader.readIntBig(u16) },
.Long => .{ .Long = try reader.readIntBig(u16) },
.Short => .{ .Short = try reader.readIntBig(u16) },
.Boolean => .{ .Boolean = try reader.readIntBig(u16) },
.String => .{ .String = try reader.readIntBig(u16) },
.EnumConstant => .{ .EnumConstant = .{
.type_name_index = try reader.readIntBig(u16),
.const_name_index = try reader.readIntBig(u16),
} },
.Class => .{ .Class = try reader.readIntBig(u16) },
.AnnotationType => annotation: {
const annotation = try allocator.create(Annotation);
annotation.* = try Annotation.decode(allocator, reader);
break :annotation .{ .AnnotationType = annotation };
},
.Array => array: {
const num_values = try reader.readIntBig(u16);
const values = try allocator.alloc(ElementValue, num_values);
for (values) |*value| {
value.* = try ElementValue.decode(allocator, reader);
}
break :array .{ .Array = .{
.num_values = num_values,
.values = values,
} };
},
};
return value;
}
pub fn encode(self: ElementValue, writer: anytype) anyerror!void {
try writer.writeIntBig(u16, @enumToInt(self));
switch (self) {
.EnumConstant => |econst| {
try writer.writeIntBig(u16, econst.type_name_index);
try writer.writeIntBig(u16, econst.const_name_index);
},
.AnnotationType => |anno| {
try anno.encode(writer);
},
.Array => |array| {
try writer.writeIntBig(u16, array.num_values);
for (array.values) |value| try value.encode(writer);
},
inline else => |value| try writer.writeIntBig(u16, value),
}
}
pub fn calcAttrLen(self: ElementValue) u32 {
return switch (self) {
.EnumConstant => 4,
.AnnotationType => |anno| anno.calcAttrLen(),
.Array => |array| 2 + array.num_values * 2,
else => 2,
};
}
pub fn deinit(self: ElementValue, allocator: std.mem.Allocator) void {
switch (self) {
.AnnotationType => |anno| anno.deinit(allocator),
.Array => |array| allocator.free(array.values),
else => {},
}
}
};
pub const AnnotationValuePair = struct {
element_name_index: u16,
value: ElementValue,
pub fn decode(allocator: std.mem.Allocator, reader: anytype) !AnnotationValuePair {
const element_name_index = try reader.readIntBig(u16);
return AnnotationValuePair{
.element_name_index = element_name_index,
.value = try ElementValue.decode(allocator, reader),
};
}
pub fn encode(self: AnnotationValuePair, writer: anytype) !void {
try writer.writeIntBig(u16, self.element_name_index);
try self.value.encode(writer);
}
pub fn calcAttrLen(self: AnnotationValuePair) u32 {
return 2 + self.value.calcAttrLen();
}
pub fn deinit(self: AnnotationValuePair, allocator: std.mem.Allocator) void {
self.value.deinit(allocator);
}
};
pub const Annotation = struct {
type_index: u16,
num_element_value_pairs: u16,
element_value_pairs: []AnnotationValuePair,
pub fn decode(allocator: std.mem.Allocator, reader: anytype) !Annotation {
const type_index = try reader.readIntBig(u16);
const num_element_value_pairs = try reader.readIntBig(u16);
const element_value_pairs = try allocator.alloc(AnnotationValuePair, num_element_value_pairs);
for (element_value_pairs) |*value_pair| {
value_pair.* = try AnnotationValuePair.decode(allocator, reader);
}
return Annotation{
.type_index = type_index,
.num_element_value_pairs = num_element_value_pairs,
.element_value_pairs = element_value_pairs,
};
}
pub fn encode(self: Annotation, writer: anytype) !void {
try writer.writeIntBig(u16, self.type_index);
try writer.writeIntBig(u16, self.num_element_value_pairs);
for (self.element_value_pairs) |pair| try pair.encode(writer);
}
pub fn calcAttrLen(self: Annotation) u32 {
var len: u32 = 4;
for (self.element_value_pairs) |pair| len += pair.calcAttrLen();
return len;
}
pub fn deinit(self: Annotation, allocator: std.mem.Allocator) void {
for (self.element_value_pairs) |value_pair| {
value_pair.deinit(allocator);
}
allocator.free(self.element_value_pairs);
}
};
pub const RuntimeVisibleAnnotationsAttribute = struct {
pub const name = "RuntimeVisibleAnnotations";
allocator: std.mem.Allocator,
constant_pool: *ConstantPool,
num_annotations: u16,
annotations: []Annotation,
pub fn decode(constant_pool: *ConstantPool, allocator: std.mem.Allocator, reader: anytype) !RuntimeVisibleAnnotationsAttribute {
const num_annotations = try reader.readIntBig(u16);
const annotations = try allocator.alloc(Annotation, num_annotations);
for (annotations) |*annotation| {
annotation.* = try Annotation.decode(allocator, reader);
}
return RuntimeVisibleAnnotationsAttribute{
.allocator = allocator,
.constant_pool = constant_pool,
.num_annotations = num_annotations,
.annotations = annotations,
};
}
pub fn calcAttrLen(self: RuntimeVisibleAnnotationsAttribute) u32 {
var len: u32 = 2;
for (self.annotations) |anno| len += anno.calcAttrLen();
return len;
}
pub fn encode(self: RuntimeVisibleAnnotationsAttribute, writer: anytype) anyerror!void {
try writer.writeIntBig(u16, self.num_annotations);
for (self.annotations) |annotation| try annotation.encode(writer);
}
pub fn deinit(self: *RuntimeVisibleAnnotationsAttribute) void {
for (self.annotations) |annotation| annotation.deinit(self.allocator);
self.allocator.free(self.annotations);
}
};
You can’t perform that action at this time.
