diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/std/c/freebsd.zig | 140 | ||||
| -rw-r--r-- | lib/std/compress.zig | 30 | ||||
| -rw-r--r-- | lib/std/compress/gzip.zig | 32 | ||||
| -rw-r--r-- | lib/std/debug.zig | 2 | ||||
| -rw-r--r-- | lib/std/macho.zig | 2 |
5 files changed, 147 insertions, 59 deletions
diff --git a/lib/std/c/freebsd.zig b/lib/std/c/freebsd.zig index 7a4e30b909..a76f8a4871 100644 --- a/lib/std/c/freebsd.zig +++ b/lib/std/c/freebsd.zig @@ -155,6 +155,8 @@ pub const EAI = enum(c_int) { pub const EAI_MAX = 15; +pub const IFNAMESIZE = 16; + pub const AI = struct { /// get address to use bind() pub const PASSIVE = 0x00000001; @@ -1271,62 +1273,98 @@ pub const siginfo_t = extern struct { }, }; -pub usingnamespace switch (builtin.cpu.arch) { - .x86_64 => struct { - pub const ucontext_t = extern struct { - sigmask: sigset_t, - mcontext: mcontext_t, - link: ?*ucontext_t, - stack: stack_t, - flags: c_int, - __spare__: [4]c_int, - }; - - /// XXX x86_64 specific - pub const mcontext_t = extern struct { - onstack: u64, - rdi: u64, - rsi: u64, - rdx: u64, - rcx: u64, - r8: u64, - r9: u64, - rax: u64, - rbx: u64, - rbp: u64, - r10: u64, - r11: u64, - r12: u64, - r13: u64, - r14: u64, - r15: u64, - trapno: u32, - fs: u16, - gs: u16, - addr: u64, +pub const mcontext_t = switch (builtin.cpu.arch) { + .x86_64 => extern struct { + onstack: u64, + rdi: u64, + rsi: u64, + rdx: u64, + rcx: u64, + r8: u64, + r9: u64, + rax: u64, + rbx: u64, + rbp: u64, + r10: u64, + r11: u64, + r12: u64, + r13: u64, + r14: u64, + r15: u64, + trapno: u32, + fs: u16, + gs: u16, + addr: u64, + flags: u32, + es: u16, + ds: u16, + err: u64, + rip: u64, + cs: u64, + rflags: u64, + rsp: u64, + ss: u64, + len: u64, + fpformat: u64, + ownedfp: u64, + fpstate: [64]u64 align(16), + fsbase: u64, + gsbase: u64, + xfpustate: u64, + xfpustate_len: u64, + spare: [4]u64, + }, + .aarch64 => extern struct { + gpregs: extern struct { + x: [30]u64, + lr: u64, + sp: u64, + elr: u64, + spsr: u32, + _pad: u32, + }, + fpregs: extern struct { + q: [32]u128, + sr: u32, + cr: u32, flags: u32, - es: u16, - ds: u16, - err: u64, - rip: u64, - cs: u64, - rflags: u64, - rsp: u64, - ss: u64, - len: u64, - fpformat: u64, - ownedfp: u64, - fpstate: [64]u64 align(16), - fsbase: u64, - gsbase: u64, - xfpustate: u64, - xfpustate_len: u64, - spare: [4]u64, - }; + _pad: u32, + }, + flags: u32, + _pad: u32, + _spare: [8]u64, + }, + else => struct {}, +}; + +pub const REG = switch (builtin.cpu.arch) { + .aarch64 => struct { + pub const FP = 29; + pub const SP = 31; + pub const PC = 32; + }, + .arm => struct { + pub const FP = 11; + pub const SP = 13; + pub const PC = 15; + }, + .x86_64 => struct { + pub const RBP = 12; + pub const RIP = 21; + pub const RSP = 24; }, else => struct {}, }; +pub const ucontext_t = extern struct { + sigmask: sigset_t, + mcontext: mcontext_t, + link: ?*ucontext_t, + stack: stack_t, + flags: c_int, + __spare__: [4]c_int, +}; + pub const E = enum(u16) { /// No error occurred. SUCCESS = 0, diff --git a/lib/std/compress.zig b/lib/std/compress.zig index 7fa25175d5..3c52002cfc 100644 --- a/lib/std/compress.zig +++ b/lib/std/compress.zig @@ -4,6 +4,36 @@ pub const deflate = @import("compress/deflate.zig"); pub const gzip = @import("compress/gzip.zig"); pub const zlib = @import("compress/zlib.zig"); +pub fn HashedReader( + comptime ReaderType: anytype, + comptime HasherType: anytype, +) type { + return struct { + child_reader: ReaderType, + hasher: HasherType, + + pub const Error = ReaderType.Error; + pub const Reader = std.io.Reader(*@This(), Error, read); + + pub fn read(self: *@This(), buf: []u8) Error!usize { + const amt = try self.child_reader.read(buf); + self.hasher.update(buf); + return amt; + } + + pub fn reader(self: *@This()) Reader { + return .{ .context = self }; + } + }; +} + +pub fn hashedReader( + reader: anytype, + hasher: anytype, +) HashedReader(@TypeOf(reader), @TypeOf(hasher)) { + return .{ .child_reader = reader, .hasher = hasher }; +} + test { _ = deflate; _ = gzip; diff --git a/lib/std/compress/gzip.zig b/lib/std/compress/gzip.zig index e9e57cc550..4d1f8e28f4 100644 --- a/lib/std/compress/gzip.zig +++ b/lib/std/compress/gzip.zig @@ -44,8 +44,11 @@ pub fn GzipStream(comptime ReaderType: type) type { }, fn init(allocator: mem.Allocator, source: ReaderType) !Self { + var hasher = std.compress.hashedReader(source, std.hash.Crc32.init()); + const hashed_reader = hasher.reader(); + // gzip header format is specified in RFC1952 - const header = try source.readBytesNoEof(10); + const header = try hashed_reader.readBytesNoEof(10); // Check the ID1/ID2 fields if (header[0] != 0x1f or header[1] != 0x8b) @@ -66,31 +69,31 @@ pub fn GzipStream(comptime ReaderType: type) type { _ = XFL; const extra = if (FLG & FEXTRA != 0) blk: { - const len = try source.readIntLittle(u16); + const len = try hashed_reader.readIntLittle(u16); const tmp_buf = try allocator.alloc(u8, len); errdefer allocator.free(tmp_buf); - try source.readNoEof(tmp_buf); + try hashed_reader.readNoEof(tmp_buf); break :blk tmp_buf; } else null; errdefer if (extra) |p| allocator.free(p); const filename = if (FLG & FNAME != 0) - try source.readUntilDelimiterAlloc(allocator, 0, max_string_len) + try hashed_reader.readUntilDelimiterAlloc(allocator, 0, max_string_len) else null; errdefer if (filename) |p| allocator.free(p); const comment = if (FLG & FCOMMENT != 0) - try source.readUntilDelimiterAlloc(allocator, 0, max_string_len) + try hashed_reader.readUntilDelimiterAlloc(allocator, 0, max_string_len) else null; errdefer if (comment) |p| allocator.free(p); if (FLG & FHCRC != 0) { - // TODO: Evaluate and check the header checksum. The stdlib has - // no CRC16 yet :( - _ = try source.readIntLittle(u16); + const hash = try source.readIntLittle(u16); + if (hash != @truncate(u16, hasher.hasher.final())) + return error.WrongChecksum; } return Self{ @@ -230,3 +233,16 @@ test "sanity checks" { }, ""), ); } + +test "header checksum" { + try testReader(&[_]u8{ + // GZIP header + 0x1f, 0x8b, 0x08, 0x12, 0x00, 0x09, 0x6e, 0x88, 0x00, 0xff, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x00, + + // header.FHCRC (should cover entire header) + 0x99, 0xd6, + + // GZIP data + 0x01, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }, ""); +} diff --git a/lib/std/debug.zig b/lib/std/debug.zig index f498cb4807..99ddb266b6 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -1986,12 +1986,14 @@ fn handleSegfaultPosix(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const any const ip = switch (native_os) { .macos => @intCast(usize, ctx.mcontext.ss.pc), .netbsd => @intCast(usize, ctx.mcontext.gregs[os.REG.PC]), + .freebsd => @intCast(usize, ctx.mcontext.gpregs.elr), else => @intCast(usize, ctx.mcontext.pc), }; // x29 is the ABI-designated frame pointer const bp = switch (native_os) { .macos => @intCast(usize, ctx.mcontext.ss.fp), .netbsd => @intCast(usize, ctx.mcontext.gregs[os.REG.FP]), + .freebsd => @intCast(usize, ctx.mcontext.gpregs.x[os.REG.FP]), else => @intCast(usize, ctx.mcontext.regs[29]), }; dumpStackTraceFromBase(bp, ip); diff --git a/lib/std/macho.zig b/lib/std/macho.zig index f6a04b2ab8..0c0c1a15cc 100644 --- a/lib/std/macho.zig +++ b/lib/std/macho.zig @@ -2011,6 +2011,7 @@ pub const UNWIND_PERSONALITY_MASK: u32 = 0x30000000; // x86_64 pub const UNWIND_X86_64_MODE_MASK: u32 = 0x0F000000; pub const UNWIND_X86_64_MODE = enum(u4) { + OLD = 0, RBP_FRAME = 1, STACK_IMMD = 2, STACK_IND = 3, @@ -2039,6 +2040,7 @@ pub const UNWIND_X86_64_REG = enum(u3) { // arm64 pub const UNWIND_ARM64_MODE_MASK: u32 = 0x0F000000; pub const UNWIND_ARM64_MODE = enum(u4) { + OLD = 0, FRAMELESS = 2, DWARF = 3, FRAME = 4, |
