| ... | @@ -165,16 +165,19 @@ test "sigaltstack" { | ... | @@ -165,16 +165,19 @@ test "sigaltstack" { |
| 165 | // analyzed | 165 | // analyzed |
| 166 | const dl_phdr_info = if (@hasDecl(os, "dl_phdr_info")) os.dl_phdr_info else c_void; | 166 | const dl_phdr_info = if (@hasDecl(os, "dl_phdr_info")) os.dl_phdr_info else c_void; |
| 167 | | 167 | |
| 168 | fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) callconv(.C) i32 { | 168 | const IterFnError = error{ |
| 169 | if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx) | 169 | MissingPtLoadSegment, |
| 170 | return 0; | 170 | MissingLoad, |
| 171 | | 171 | BadElfMagic, |
| 172 | var counter = data.?; | 172 | FailedConsistencyCheck, |
| | 173 | }; |
| | 174 | |
| | 175 | fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void { |
| 173 | // Count how many libraries are loaded | 176 | // Count how many libraries are loaded |
| 174 | counter.* += @as(usize, 1); | 177 | counter.* += @as(usize, 1); |
| 175 | | 178 | |
| 176 | // The image should contain at least a PT_LOAD segment | 179 | // The image should contain at least a PT_LOAD segment |
| 177 | if (info.dlpi_phnum < 1) return -1; | 180 | if (info.dlpi_phnum < 1) return error.MissingPtLoadSegment; |
| 178 | | 181 | |
| 179 | // Quick & dirty validation of the phdr pointers, make sure we're not | 182 | // Quick & dirty validation of the phdr pointers, make sure we're not |
| 180 | // pointing to some random gibberish | 183 | // pointing to some random gibberish |
| ... | @@ -189,17 +192,15 @@ fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) callconv(.C) i32 { | ... | @@ -189,17 +192,15 @@ fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) callconv(.C) i32 { |
| 189 | // Find the ELF header | 192 | // Find the ELF header |
| 190 | const elf_header = @intToPtr(*elf.Ehdr, reloc_addr - phdr.p_offset); | 193 | const elf_header = @intToPtr(*elf.Ehdr, reloc_addr - phdr.p_offset); |
| 191 | // Validate the magic | 194 | // Validate the magic |
| 192 | if (!mem.eql(u8, elf_header.e_ident[0..4], "\x7fELF")) return -1; | 195 | if (!mem.eql(u8, elf_header.e_ident[0..4], "\x7fELF")) return error.BadElfMagic; |
| 193 | // Consistency check | 196 | // Consistency check |
| 194 | if (elf_header.e_phnum != info.dlpi_phnum) return -1; | 197 | if (elf_header.e_phnum != info.dlpi_phnum) return error.FailedConsistencyCheck; |
| 195 | | 198 | |
| 196 | found_load = true; | 199 | found_load = true; |
| 197 | break; | 200 | break; |
| 198 | } | 201 | } |
| 199 | | 202 | |
| 200 | if (!found_load) return -1; | 203 | if (!found_load) return error.MissingLoad; |
| 201 | | | |
| 202 | return 42; | | |
| 203 | } | 204 | } |
| 204 | | 205 | |
| 205 | test "dl_iterate_phdr" { | 206 | test "dl_iterate_phdr" { |
| ... | @@ -207,7 +208,7 @@ test "dl_iterate_phdr" { | ... | @@ -207,7 +208,7 @@ test "dl_iterate_phdr" { |
| 207 | return error.SkipZigTest; | 208 | return error.SkipZigTest; |
| 208 | | 209 | |
| 209 | var counter: usize = 0; | 210 | var counter: usize = 0; |
| 210 | expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0); | 211 | try os.dl_iterate_phdr(&counter, IterFnError, iter_fn); |
| 211 | expect(counter != 0); | 212 | expect(counter != 0); |
| 212 | } | 213 | } |
| 213 | | 214 | |