Skip to content

Commit f1af53f

Browse files
committed
macho: use pread syscall when loading tapi file
This avoids mixing preads with reads which do not mix well especially on Windows.
1 parent 7c37c55 commit f1af53f

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/link/MachO.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,7 @@ fn parseInputFileWorker(self: *MachO, file: File) void {
914914
switch (err) {
915915
error.MalformedObject,
916916
error.MalformedDylib,
917+
error.MalformedTbd,
917918
error.InvalidCpuArch,
918919
error.InvalidTarget,
919920
=> {}, // already reported
@@ -4637,7 +4638,6 @@ const ObjcStubsSection = synthetic.ObjcStubsSection;
46374638
const Object = @import("MachO/Object.zig");
46384639
const LazyBind = bind.LazyBind;
46394640
const LaSymbolPtrSection = synthetic.LaSymbolPtrSection;
4640-
const LibStub = tapi.LibStub;
46414641
const Liveness = @import("../Liveness.zig");
46424642
const LlvmObject = @import("../codegen/llvm.zig").Object;
46434643
const Md5 = std.crypto.hash.Md5;

src/link/MachO/Dylib.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,10 @@ fn parseTbd(self: *Dylib, macho_file: *MachO) !void {
270270
log.debug("parsing dylib from stub: {s}", .{self.path});
271271

272272
const file = macho_file.getFileHandle(self.file_handle);
273-
var lib_stub = LibStub.loadFromFile(gpa, file) catch return error.NotLibStub;
273+
var lib_stub = LibStub.loadFromFile(gpa, file) catch |err| {
274+
try macho_file.reportParseError2(self.index, "failed to parse TBD file: {s}", .{@errorName(err)});
275+
return error.MalformedTbd;
276+
};
274277
defer lib_stub.deinit();
275278
const umbrella_lib = lib_stub.inner[0];
276279

src/link/tapi.zig

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ pub const Tbd = union(enum) {
129129

130130
pub const TapiError = error{
131131
NotLibStub,
132-
FileTooBig,
133-
} || yaml.YamlError || std.fs.File.ReadError;
132+
InputOutput,
133+
} || yaml.YamlError || std.fs.File.PReadError;
134134

135135
pub const LibStub = struct {
136136
/// Underlying memory for stub's contents.
@@ -140,8 +140,14 @@ pub const LibStub = struct {
140140
inner: []Tbd,
141141

142142
pub fn loadFromFile(allocator: Allocator, file: fs.File) TapiError!LibStub {
143-
const source = try file.readToEndAlloc(allocator, std.math.maxInt(u32));
143+
const filesize = blk: {
144+
const stat = file.stat() catch break :blk std.math.maxInt(u32);
145+
break :blk @min(stat.size, std.math.maxInt(u32));
146+
};
147+
const source = try allocator.alloc(u8, filesize);
144148
defer allocator.free(source);
149+
const amt = try file.preadAll(source, 0);
150+
if (amt != filesize) return error.InputOutput;
145151

146152
var lib_stub = LibStub{
147153
.yaml = try Yaml.load(allocator, source),

0 commit comments

Comments
 (0)