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.
This commit is contained in:
mlugg 2024-07-04 07:00:56 +01:00
parent 00da182e68
commit eae9aa800e
No known key found for this signature in database
GPG Key ID: 3F5B7DCCBF4AF02E
3 changed files with 43 additions and 26 deletions

View File

@ -17,12 +17,15 @@ pub const DynLib = struct {
DlDynLib, DlDynLib,
.windows => WindowsDynLib, .windows => WindowsDynLib,
.macos, .tvos, .watchos, .ios, .visionos, .freebsd, .netbsd, .openbsd, .dragonfly, .solaris, .illumos => DlDynLib, .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, 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. /// Trusts the file. Malicious file will be able to execute arbitrary code.
pub fn open(path: []const u8) Error!DynLib { 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 }; 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 { pub const ElfDynLib = struct {
strings: [*:0]u8, strings: [*:0]u8,
syms: [*]elf.Sym, syms: [*]elf.Sym,
@ -130,15 +145,7 @@ pub const ElfDynLib = struct {
verdef: ?*elf.Verdef, verdef: ?*elf.Verdef,
memory: []align(mem.page_size) u8, memory: []align(mem.page_size) u8,
pub const Error = error{ pub const Error = ElfDynLibError;
FileTooBig,
NotElfFile,
NotDynamicLibrary,
MissingDynamicLinkingInformation,
ElfStringSectionNotFound,
ElfSymSectionNotFound,
ElfHashTableNotFound,
} || posix.OpenError || posix.MMapError;
/// Trusts the file. Malicious file will be able to execute arbitrary code. /// Trusts the file. Malicious file will be able to execute arbitrary code.
pub fn open(path: []const u8) Error!ElfDynLib { pub fn open(path: []const u8) Error!ElfDynLib {
@ -350,11 +357,15 @@ test "ElfDynLib" {
try testing.expectError(error.FileNotFound, ElfDynLib.open("invalid_so.so")); try testing.expectError(error.FileNotFound, ElfDynLib.open("invalid_so.so"));
} }
pub const WindowsDynLib = struct { /// Separated to avoid referencing `WindowsDynLib`, because its field types may not
pub const Error = error{ /// be valid on other targets.
const WindowsDynLibError = error{
FileNotFound, FileNotFound,
InvalidPath, InvalidPath,
} || windows.LoadLibraryError; } || windows.LoadLibraryError;
pub const WindowsDynLib = struct {
pub const Error = WindowsDynLibError;
dll: windows.HMODULE, 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 DlDynLib = struct {
pub const Error = error{ FileNotFound, NameTooLong }; pub const Error = DlDynLibError;
handle: *anyopaque, handle: *anyopaque,

View File

@ -311,13 +311,13 @@ const builtin = @import("builtin");
const std = @import("std.zig"); const std = @import("std.zig");
test { test {
if (builtin.os.tag != .wasi) {
_ = Client; _ = Client;
_ = Method; _ = Method;
_ = Server; _ = Server;
_ = Status; _ = Status;
_ = HeadParser; _ = HeadParser;
_ = ChunkParser; _ = ChunkParser;
if (builtin.os.tag != .wasi) {
_ = @import("http/test.zig"); _ = @import("http/test.zig");
} }
} }

View File

@ -1930,8 +1930,10 @@ pub const Server = struct {
}; };
test { test {
_ = @import("net/test.zig"); if (builtin.os.tag != .wasi) {
_ = Server; _ = Server;
_ = Stream; _ = Stream;
_ = Address; _ = Address;
_ = @import("net/test.zig");
}
} }