| ... | @@ -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 | }; |
| 22 | | 25 | |
| 23 | inner: InnerType, | 26 | inner: InnerType, |
| 24 | | 27 | |
| 25 | pub const Error = ElfDynLib.Error || DlDynLib.Error || WindowsDynLib.Error; | 28 | pub const Error = ElfDynLibError || DlDynLibError || WindowsDynLibError; |
| 26 | | 29 | |
| 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 | } |
| 124 | | 127 | |
| | 128 | /// Separated to avoid referencing `ElfDynLib`, because its field types may not |
| | 129 | /// be valid on other targets. |
| | 130 | const ElfDynLibError = error{ |
| | 131 | FileTooBig, |
| | 132 | NotElfFile, |
| | 133 | NotDynamicLibrary, |
| | 134 | MissingDynamicLinkingInformation, |
| | 135 | ElfStringSectionNotFound, |
| | 136 | ElfSymSectionNotFound, |
| | 137 | ElfHashTableNotFound, |
| | 138 | } || posix.OpenError || posix.MMapError; |
| | 139 | |
| 125 | pub const ElfDynLib = struct { | 140 | pub 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, |
| 132 | | 147 | |
| 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; | | |
| 142 | | 149 | |
| 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 | } |
| 352 | | 359 | |
| | 360 | /// Separated to avoid referencing `WindowsDynLib`, because its field types may not |
| | 361 | /// be valid on other targets. |
| | 362 | const WindowsDynLibError = error{ |
| | 363 | FileNotFound, |
| | 364 | InvalidPath, |
| | 365 | } || windows.LoadLibraryError; |
| | 366 | |
| 353 | pub const WindowsDynLib = struct { | 367 | pub const WindowsDynLib = struct { |
| 354 | pub const Error = error{ | 368 | pub const Error = WindowsDynLibError; |
| 355 | FileNotFound, | | |
| 356 | InvalidPath, | | |
| 357 | } || windows.LoadLibraryError; | | |
| 358 | | 369 | |
| 359 | dll: windows.HMODULE, | 370 | dll: windows.HMODULE, |
| 360 | | 371 | |
| ... | @@ -413,8 +424,12 @@ pub const WindowsDynLib = struct { | ... | @@ -413,8 +424,12 @@ pub const WindowsDynLib = struct { |
| 413 | } | 424 | } |
| 414 | }; | 425 | }; |
| 415 | | 426 | |
| | 427 | /// Separated to avoid referencing `DlDynLib`, because its field types may not |
| | 428 | /// be valid on other targets. |
| | 429 | const DlDynLibError = error{ FileNotFound, NameTooLong }; |
| | 430 | |
| 416 | pub const DlDynLib = struct { | 431 | pub const DlDynLib = struct { |
| 417 | pub const Error = error{ FileNotFound, NameTooLong }; | 432 | pub const Error = DlDynLibError; |
| 418 | | 433 | |
| 419 | handle: *anyopaque, | 434 | handle: *anyopaque, |
| 420 | | 435 | |