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" {...@@ -165,16 +165,19 @@ test "sigaltstack" {
165// analyzed165// analyzed
166const dl_phdr_info = if (@hasDecl(os, "dl_phdr_info")) os.dl_phdr_info else c_void;166const 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 {168const IterFnError = error{
169 if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx)169 MissingPtLoadSegment,
170 return 0;170 MissingLoad,
171171 BadElfMagic,
172 var counter = data.?;172 FailedConsistencyCheck,
173};
174
175fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void {
173 // Count how many libraries are loaded176 // Count how many libraries are loaded
174 counter.* += @as(usize, 1);177 counter.* += @as(usize, 1);
175178
176 // The image should contain at least a PT_LOAD segment179 // 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
179 // Quick & dirty validation of the phdr pointers, make sure we're not182 // Quick & dirty validation of the phdr pointers, make sure we're not
180 // pointing to some random gibberish183 // 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 header192 // 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 magic194 // 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 check196 // 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
196 found_load = true;199 found_load = true;
197 break;200 break;
198 }201 }
199202
200 if (!found_load) return -1;203 if (!found_load) return error.MissingLoad;
201
202 return 42;
203}204}
204205
205test "dl_iterate_phdr" {206test "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;
208209
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}
213214