authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-07-04 07:00:56+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-07-04 21:01:42+01:00
logeae9aa800e76efb835f81ee5b788890425ee95dd
treec4c0c2d99e7a763d9dcf8635ef2c068bae03087e
parent00da182e6875845d5727c399b3738a13b262832e
signaturelock-open Commit is signed but in an unrecognized format.

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.

3 files changed, 43 insertions(+), 26 deletions(-)

lib/std/dynamic_library.zig+31-16
...@@ -17,12 +17,15 @@ pub const DynLib = struct {...@@ -17,12 +17,15 @@ pub const DynLib = struct {
17 DlDynLib,17 DlDynLib,
18 .windows => WindowsDynLib,18 .windows => WindowsDynLib,
19 .macos, .tvos, .watchos, .ios, .visionos, .freebsd, .netbsd, .openbsd, .dragonfly, .solaris, .illumos => DlDynLib,19 .macos, .tvos, .watchos, .ios, .visionos, .freebsd, .netbsd, .openbsd, .dragonfly, .solaris, .illumos => DlDynLib,
20 else => @compileError("unsupported platform"),20 else => struct {
21 const open = @compileError("unsupported platform");
22 const openZ = @compileError("unsupported platform");
23 },
21 };24 };
2225
23 inner: InnerType,26 inner: InnerType,
2427
25 pub const Error = ElfDynLib.Error || DlDynLib.Error || WindowsDynLib.Error;28 pub const Error = ElfDynLibError || DlDynLibError || WindowsDynLibError;
2629
27 /// Trusts the file. Malicious file will be able to execute arbitrary code.30 /// Trusts the file. Malicious file will be able to execute arbitrary code.
28 pub fn open(path: []const u8) Error!DynLib {31 pub fn open(path: []const u8) Error!DynLib {
...@@ -122,6 +125,18 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) error{InvalidExe}!LinkMap.Iterator {...@@ -122,6 +125,18 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) error{InvalidExe}!LinkMap.Iterator {
122 return .{ .current = link_map_ptr };125 return .{ .current = link_map_ptr };
123}126}
124127
128/// Separated to avoid referencing `ElfDynLib`, because its field types may not
129/// be valid on other targets.
130const ElfDynLibError = error{
131 FileTooBig,
132 NotElfFile,
133 NotDynamicLibrary,
134 MissingDynamicLinkingInformation,
135 ElfStringSectionNotFound,
136 ElfSymSectionNotFound,
137 ElfHashTableNotFound,
138} || posix.OpenError || posix.MMapError;
139
125pub const ElfDynLib = struct {140pub const ElfDynLib = struct {
126 strings: [*:0]u8,141 strings: [*:0]u8,
127 syms: [*]elf.Sym,142 syms: [*]elf.Sym,
...@@ -130,15 +145,7 @@ pub const ElfDynLib = struct {...@@ -130,15 +145,7 @@ pub const ElfDynLib = struct {
130 verdef: ?*elf.Verdef,145 verdef: ?*elf.Verdef,
131 memory: []align(mem.page_size) u8,146 memory: []align(mem.page_size) u8,
132147
133 pub const Error = error{148 pub const Error = ElfDynLibError;
134 FileTooBig,
135 NotElfFile,
136 NotDynamicLibrary,
137 MissingDynamicLinkingInformation,
138 ElfStringSectionNotFound,
139 ElfSymSectionNotFound,
140 ElfHashTableNotFound,
141 } || posix.OpenError || posix.MMapError;
142149
143 /// Trusts the file. Malicious file will be able to execute arbitrary code.150 /// Trusts the file. Malicious file will be able to execute arbitrary code.
144 pub fn open(path: []const u8) Error!ElfDynLib {151 pub fn open(path: []const u8) Error!ElfDynLib {
...@@ -350,11 +357,15 @@ test "ElfDynLib" {...@@ -350,11 +357,15 @@ test "ElfDynLib" {
350 try testing.expectError(error.FileNotFound, ElfDynLib.open("invalid_so.so"));357 try testing.expectError(error.FileNotFound, ElfDynLib.open("invalid_so.so"));
351}358}
352359
360/// Separated to avoid referencing `WindowsDynLib`, because its field types may not
361/// be valid on other targets.
362const WindowsDynLibError = error{
363 FileNotFound,
364 InvalidPath,
365} || windows.LoadLibraryError;
366
353pub const WindowsDynLib = struct {367pub const WindowsDynLib = struct {
354 pub const Error = error{368 pub const Error = WindowsDynLibError;
355 FileNotFound,
356 InvalidPath,
357 } || windows.LoadLibraryError;
358369
359 dll: windows.HMODULE,370 dll: windows.HMODULE,
360371
...@@ -413,8 +424,12 @@ pub const WindowsDynLib = struct {...@@ -413,8 +424,12 @@ pub const WindowsDynLib = struct {
413 }424 }
414};425};
415426
427/// Separated to avoid referencing `DlDynLib`, because its field types may not
428/// be valid on other targets.
429const DlDynLibError = error{ FileNotFound, NameTooLong };
430
416pub const DlDynLib = struct {431pub const DlDynLib = struct {
417 pub const Error = error{ FileNotFound, NameTooLong };432 pub const Error = DlDynLibError;
418433
419 handle: *anyopaque,434 handle: *anyopaque,
420435
lib/std/http.zig+6-6
...@@ -311,13 +311,13 @@ const builtin = @import("builtin");...@@ -311,13 +311,13 @@ const builtin = @import("builtin");
311const std = @import("std.zig");311const std = @import("std.zig");
312312
313test {313test {
314 _ = Client;
315 _ = Method;
316 _ = Server;
317 _ = Status;
318 _ = HeadParser;
319 _ = ChunkParser;
320 if (builtin.os.tag != .wasi) {314 if (builtin.os.tag != .wasi) {
315 _ = Client;
316 _ = Method;
317 _ = Server;
318 _ = Status;
319 _ = HeadParser;
320 _ = ChunkParser;
321 _ = @import("http/test.zig");321 _ = @import("http/test.zig");
322 }322 }
323}323}
lib/std/net.zig+6-4
...@@ -1930,8 +1930,10 @@ pub const Server = struct {...@@ -1930,8 +1930,10 @@ pub const Server = struct {
1930};1930};
19311931
1932test {1932test {
1933 _ = @import("net/test.zig");1933 if (builtin.os.tag != .wasi) {
1934 _ = Server;1934 _ = Server;
1935 _ = Stream;1935 _ = Stream;
1936 _ = Address;1936 _ = Address;
1937 _ = @import("net/test.zig");
1938 }
1937}1939}