Skip to content

Commit

Permalink
macho: use pread syscall when loading tapi file
Browse files Browse the repository at this point in the history
This avoids mixing preads with reads which do not mix well especially
on Windows.
  • Loading branch information
kubkon authored and SammyJames committed Aug 7, 2024
1 parent 357d5c2 commit b3a2728
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/link/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ fn parseInputFileWorker(self: *MachO, file: File) void {
switch (err) {
error.MalformedObject,
error.MalformedDylib,
error.MalformedTbd,
error.InvalidCpuArch,
error.InvalidTarget,
=> {}, // already reported
Expand Down Expand Up @@ -4637,7 +4638,6 @@ const ObjcStubsSection = synthetic.ObjcStubsSection;
const Object = @import("MachO/Object.zig");
const LazyBind = bind.LazyBind;
const LaSymbolPtrSection = synthetic.LaSymbolPtrSection;
const LibStub = tapi.LibStub;
const Liveness = @import("../Liveness.zig");
const LlvmObject = @import("../codegen/llvm.zig").Object;
const Md5 = std.crypto.hash.Md5;
Expand Down
5 changes: 4 additions & 1 deletion src/link/MachO/Dylib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ fn parseTbd(self: *Dylib, macho_file: *MachO) !void {
log.debug("parsing dylib from stub: {s}", .{self.path});

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

Expand Down
12 changes: 9 additions & 3 deletions src/link/tapi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ pub const Tbd = union(enum) {

pub const TapiError = error{
NotLibStub,
FileTooBig,
} || yaml.YamlError || std.fs.File.ReadError;
InputOutput,
} || yaml.YamlError || std.fs.File.PReadError;

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

pub fn loadFromFile(allocator: Allocator, file: fs.File) TapiError!LibStub {
const source = try file.readToEndAlloc(allocator, std.math.maxInt(u32));
const filesize = blk: {
const stat = file.stat() catch break :blk std.math.maxInt(u32);
break :blk @min(stat.size, std.math.maxInt(u32));
};
const source = try allocator.alloc(u8, filesize);
defer allocator.free(source);
const amt = try file.preadAll(source, 0);
if (amt != filesize) return error.InputOutput;

var lib_stub = LibStub{
.yaml = try Yaml.load(allocator, source),
Expand Down

0 comments on commit b3a2728

Please sign in to comment.