From eae9aa800e76efb835f81ee5b788890425ee95dd Mon Sep 17 00:00:00 2001 From: mlugg Date: Thu, 4 Jul 2024 07:00:56 +0100 Subject: [PATCH] std: avoid references that trigger compile errors Note that the `_ = Address` statements in tests previously were a nop, and now actually check that the type is valid. However, on WASI, the type is *not* valid. --- lib/std/dynamic_library.zig | 47 ++++++++++++++++++++++++------------- lib/std/http.zig | 12 +++++----- lib/std/net.zig | 10 ++++---- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/lib/std/dynamic_library.zig b/lib/std/dynamic_library.zig index a1db48b470..f5ce0c8da8 100644 --- a/lib/std/dynamic_library.zig +++ b/lib/std/dynamic_library.zig @@ -17,12 +17,15 @@ pub const DynLib = struct { DlDynLib, .windows => WindowsDynLib, .macos, .tvos, .watchos, .ios, .visionos, .freebsd, .netbsd, .openbsd, .dragonfly, .solaris, .illumos => DlDynLib, - else => @compileError("unsupported platform"), + else => struct { + const open = @compileError("unsupported platform"); + const openZ = @compileError("unsupported platform"); + }, }; inner: InnerType, - pub const Error = ElfDynLib.Error || DlDynLib.Error || WindowsDynLib.Error; + pub const Error = ElfDynLibError || DlDynLibError || WindowsDynLibError; /// Trusts the file. Malicious file will be able to execute arbitrary code. pub fn open(path: []const u8) Error!DynLib { @@ -122,6 +125,18 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) error{InvalidExe}!LinkMap.Iterator { return .{ .current = link_map_ptr }; } +/// Separated to avoid referencing `ElfDynLib`, because its field types may not +/// be valid on other targets. +const ElfDynLibError = error{ + FileTooBig, + NotElfFile, + NotDynamicLibrary, + MissingDynamicLinkingInformation, + ElfStringSectionNotFound, + ElfSymSectionNotFound, + ElfHashTableNotFound, +} || posix.OpenError || posix.MMapError; + pub const ElfDynLib = struct { strings: [*:0]u8, syms: [*]elf.Sym, @@ -130,15 +145,7 @@ pub const ElfDynLib = struct { verdef: ?*elf.Verdef, memory: []align(mem.page_size) u8, - pub const Error = error{ - FileTooBig, - NotElfFile, - NotDynamicLibrary, - MissingDynamicLinkingInformation, - ElfStringSectionNotFound, - ElfSymSectionNotFound, - ElfHashTableNotFound, - } || posix.OpenError || posix.MMapError; + pub const Error = ElfDynLibError; /// Trusts the file. Malicious file will be able to execute arbitrary code. pub fn open(path: []const u8) Error!ElfDynLib { @@ -350,11 +357,15 @@ test "ElfDynLib" { try testing.expectError(error.FileNotFound, ElfDynLib.open("invalid_so.so")); } +/// Separated to avoid referencing `WindowsDynLib`, because its field types may not +/// be valid on other targets. +const WindowsDynLibError = error{ + FileNotFound, + InvalidPath, +} || windows.LoadLibraryError; + pub const WindowsDynLib = struct { - pub const Error = error{ - FileNotFound, - InvalidPath, - } || windows.LoadLibraryError; + pub const Error = WindowsDynLibError; dll: windows.HMODULE, @@ -413,8 +424,12 @@ pub const WindowsDynLib = struct { } }; +/// Separated to avoid referencing `DlDynLib`, because its field types may not +/// be valid on other targets. +const DlDynLibError = error{ FileNotFound, NameTooLong }; + pub const DlDynLib = struct { - pub const Error = error{ FileNotFound, NameTooLong }; + pub const Error = DlDynLibError; handle: *anyopaque, diff --git a/lib/std/http.zig b/lib/std/http.zig index af966d89e7..621c7a5f0d 100644 --- a/lib/std/http.zig +++ b/lib/std/http.zig @@ -311,13 +311,13 @@ const builtin = @import("builtin"); const std = @import("std.zig"); test { - _ = Client; - _ = Method; - _ = Server; - _ = Status; - _ = HeadParser; - _ = ChunkParser; if (builtin.os.tag != .wasi) { + _ = Client; + _ = Method; + _ = Server; + _ = Status; + _ = HeadParser; + _ = ChunkParser; _ = @import("http/test.zig"); } } diff --git a/lib/std/net.zig b/lib/std/net.zig index 79ca71d0e2..b46cc2aece 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1930,8 +1930,10 @@ pub const Server = struct { }; test { - _ = @import("net/test.zig"); - _ = Server; - _ = Stream; - _ = Address; + if (builtin.os.tag != .wasi) { + _ = Server; + _ = Stream; + _ = Address; + _ = @import("net/test.zig"); + } }