Merge pull request #7516 from kubkon/macho-better-space-alloc

macho: space preallocation, and various cleanups and fixes
This commit is contained in:
Jakub Konka
2020-12-22 14:23:55 +01:00
committed by GitHub
4 changed files with 578 additions and 325 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -126,7 +126,7 @@ pub fn calcAdhocSignature(
Sha256.hash(buffer[0..fsize], &hash, .{});
cdir.data.appendSliceAssumeCapacity(hash[0..]);
cdir.data.appendSliceAssumeCapacity(&hash);
cdir.inner.nCodeSlots += 1;
}
@@ -174,10 +174,10 @@ test "CodeSignature header" {
defer code_sig.deinit();
var buffer: [@sizeOf(macho.SuperBlob)]u8 = undefined;
code_sig.writeHeader(buffer[0..]);
code_sig.writeHeader(&buffer);
const expected = &[_]u8{ 0xfa, 0xde, 0x0c, 0xc0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0 };
testing.expect(mem.eql(u8, expected[0..], buffer[0..]));
testing.expect(mem.eql(u8, expected, &buffer));
}
pub fn calcCodeSignaturePadding(id: []const u8, file_size: u64) u32 {

View File

@@ -531,14 +531,14 @@ test "write Trie to a byte stream" {
{
const nwritten = try trie.write(stream.writer());
testing.expect(nwritten == trie.size);
testing.expect(mem.eql(u8, buffer, exp_buffer[0..]));
testing.expect(mem.eql(u8, buffer, &exp_buffer));
}
{
// Writing finalized trie again should yield the same result.
try stream.seekTo(0);
const nwritten = try trie.write(stream.writer());
testing.expect(nwritten == trie.size);
testing.expect(mem.eql(u8, buffer, exp_buffer[0..]));
testing.expect(mem.eql(u8, buffer, &exp_buffer));
}
}
@@ -556,7 +556,7 @@ test "parse Trie from byte stream" {
0x3, 0x0, 0x80, 0x20, 0x0, // terminal node
};
var in_stream = std.io.fixedBufferStream(in_buffer[0..]);
var in_stream = std.io.fixedBufferStream(&in_buffer);
var trie = Trie.init(gpa);
defer trie.deinit();
const nread = try trie.read(in_stream.reader());
@@ -571,5 +571,5 @@ test "parse Trie from byte stream" {
const nwritten = try trie.write(out_stream.writer());
testing.expect(nwritten == trie.size);
testing.expect(mem.eql(u8, in_buffer[0..], out_buffer));
testing.expect(mem.eql(u8, &in_buffer, out_buffer));
}

View File

@@ -7,7 +7,7 @@ const macho = std.macho;
const testing = std.testing;
const Allocator = std.mem.Allocator;
const makeName = @import("../MachO.zig").makeStaticString;
const makeStaticString = @import("../MachO.zig").makeStaticString;
pub const LoadCommand = union(enum) {
Segment: SegmentCommand,
@@ -26,9 +26,9 @@ pub const LoadCommand = union(enum) {
const header = try reader.readStruct(macho.load_command);
var buffer = try allocator.alloc(u8, header.cmdsize);
defer allocator.free(buffer);
mem.copy(u8, buffer[0..], mem.asBytes(&header));
mem.copy(u8, buffer, mem.asBytes(&header));
try reader.readNoEof(buffer[@sizeOf(macho.load_command)..]);
var stream = io.fixedBufferStream(buffer[0..]);
var stream = io.fixedBufferStream(buffer);
return switch (header.cmd) {
macho.LC_SEGMENT_64 => LoadCommand{
@@ -155,6 +155,12 @@ pub const SegmentCommand = struct {
return .{ .inner = inner };
}
pub fn addSection(self: *SegmentCommand, alloc: *Allocator, section: macho.section_64) !void {
try self.sections.append(alloc, section);
self.inner.cmdsize += @sizeOf(macho.section_64);
self.inner.nsects += 1;
}
pub fn read(alloc: *Allocator, reader: anytype) !SegmentCommand {
const inner = try reader.readStruct(macho.segment_command_64);
var segment = SegmentCommand{
@@ -210,7 +216,7 @@ pub fn GenericCommandWithData(comptime Cmd: type) type {
const inner = try reader.readStruct(Cmd);
var data = try allocator.alloc(u8, inner.cmdsize - @sizeOf(Cmd));
errdefer allocator.free(data);
try reader.readNoEof(data[0..]);
try reader.readNoEof(data);
return Self{
.inner = inner,
.data = data,
@@ -277,7 +283,7 @@ test "read-write segment command" {
.inner = .{
.cmd = macho.LC_SEGMENT_64,
.cmdsize = 152,
.segname = makeName("__TEXT"),
.segname = makeStaticString("__TEXT"),
.vmaddr = 4294967296,
.vmsize = 294912,
.fileoff = 0,
@@ -289,8 +295,8 @@ test "read-write segment command" {
},
};
try cmd.sections.append(gpa, .{
.sectname = makeName("__text"),
.segname = makeName("__TEXT"),
.sectname = makeStaticString("__text"),
.segname = makeStaticString("__TEXT"),
.addr = 4294983680,
.size = 448,
.offset = 16384,
@@ -303,10 +309,10 @@ test "read-write segment command" {
.reserved3 = 0,
});
defer cmd.deinit(gpa);
try testRead(gpa, in_buffer[0..], LoadCommand{ .Segment = cmd });
try testRead(gpa, in_buffer, LoadCommand{ .Segment = cmd });
var out_buffer: [in_buffer.len]u8 = undefined;
try testWrite(out_buffer[0..], LoadCommand{ .Segment = cmd }, in_buffer[0..]);
try testWrite(&out_buffer, LoadCommand{ .Segment = cmd }, in_buffer);
}
test "read-write generic command with data" {
@@ -342,10 +348,10 @@ test "read-write generic command with data" {
cmd.data[5] = 0x0;
cmd.data[6] = 0x0;
cmd.data[7] = 0x0;
try testRead(gpa, in_buffer[0..], LoadCommand{ .Dylib = cmd });
try testRead(gpa, in_buffer, LoadCommand{ .Dylib = cmd });
var out_buffer: [in_buffer.len]u8 = undefined;
try testWrite(out_buffer[0..], LoadCommand{ .Dylib = cmd }, in_buffer[0..]);
try testWrite(&out_buffer, LoadCommand{ .Dylib = cmd }, in_buffer);
}
test "read-write C struct command" {
@@ -362,8 +368,8 @@ test "read-write C struct command" {
.entryoff = 16644,
.stacksize = 0,
};
try testRead(gpa, in_buffer[0..], LoadCommand{ .Main = cmd });
try testRead(gpa, in_buffer, LoadCommand{ .Main = cmd });
var out_buffer: [in_buffer.len]u8 = undefined;
try testWrite(out_buffer[0..], LoadCommand{ .Main = cmd }, in_buffer[0..]);
try testWrite(&out_buffer, LoadCommand{ .Main = cmd }, in_buffer);
}