authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-17 17:22:32-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-17 17:22:32-05:00
logb53afc510c763f5da57bc654a6cc42a233304938
treef980146ed639175484c33b5a14227f82aed16b96
parent8fe636dafdc0e4dfeb0e3913148b21df8c066443
signature Commit is signed but in an unrecognized format.

update dl_iterate_phdr test case to new API


1 files changed, 13 insertions(+), 12 deletions(-)

lib/std/os/test.zig+13-12
......@@ -165,16 +165,19 @@ test "sigaltstack" {
165165// analyzed
166166const dl_phdr_info = if (@hasDecl(os, "dl_phdr_info")) os.dl_phdr_info else c_void;
167167
168fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) callconv(.C) i32 {
169 if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx)
170 return 0;
171
172 var counter = data.?;
168const IterFnError = error{
169 MissingPtLoadSegment,
170 MissingLoad,
171 BadElfMagic,
172 FailedConsistencyCheck,
173};
174
175fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void {
173176 // Count how many libraries are loaded
174177 counter.* += @as(usize, 1);
175178
176179 // 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;
178181
179182 // Quick & dirty validation of the phdr pointers, make sure we're not
180183 // pointing to some random gibberish
......@@ -189,17 +192,15 @@ fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) callconv(.C) i32 {
189192 // Find the ELF header
190193 const elf_header = @intToPtr(*elf.Ehdr, reloc_addr - phdr.p_offset);
191194 // 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;
193196 // 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;
195198
196199 found_load = true;
197200 break;
198201 }
199202
200 if (!found_load) return -1;
201
202 return 42;
203 if (!found_load) return error.MissingLoad;
203204}
204205
205206test "dl_iterate_phdr" {
......@@ -207,7 +208,7 @@ test "dl_iterate_phdr" {
207208 return error.SkipZigTest;
208209
209210 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);
211212 expect(counter != 0);
212213}
213214