Skip to content

ELF linker code crashes instead of reporting malformed archive #18205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andrewrk opened this issue Dec 5, 2023 · 0 comments · Fixed by #18207
Closed

ELF linker code crashes instead of reporting malformed archive #18205

andrewrk opened this issue Dec 5, 2023 · 0 comments · Fixed by #18207
Labels
bug Observed behavior contradicts documented or intended behavior linking
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Dec 5, 2023

Zig Version

0.12.0-dev.1784+c357b902b

Steps to Reproduce and Observed Behavior

$ wget https://andrewkelley.me/temp/bad_libcompiler_rt.a
$ stage4/bin/zig build-obj ../test/standalone/hello_world/hello.zig -fno-llvm -fno-lld
$ stage4/bin/zig build-obj -fno-llvm -fno-lld hello.o bad_libcompiler_rt.a
thread 2810416 panic: index out of bounds: index 6148914691236517204, len 1126320
/home/andy/Downloads/zig/src/link/Elf/Object.zig:816:39: 0x6cf8334 in shdrContents (zig)
    return self.data[shdr.sh_offset..][0..shdr.sh_size];
                                      ^
/home/andy/Downloads/zig/src/link/Elf/Object.zig:72:55: 0x773ba68 in parse (zig)
    try self.strtab.appendSlice(gpa, self.shdrContents(self.header.?.e_shstrndx));
                                                      ^
/home/andy/Downloads/zig/src/link/Elf.zig:1845:25: 0x727c8ff in parseArchive (zig)
        try object.parse(self);
                        ^
/home/andy/Downloads/zig/src/link/Elf.zig:1789:30: 0x6d54e12 in parseLibrary (zig)
        try self.parseArchive(lib.path, must_link, ctx);
                             ^
/home/andy/Downloads/zig/src/link/Elf.zig:1780:30: 0x6d544f1 in parsePositional (zig)
        try self.parseLibrary(.{ .path = path }, must_link, ctx);
                             ^
/home/andy/Downloads/zig/src/link/Elf.zig:1436:29: 0x6d537f5 in flushObject (zig)
        self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch |err|
                            ^
/home/andy/Downloads/zig/src/link/Elf.zig:979:49: 0x68e6f89 in flushModule (zig)
    if (self.isObject()) return self.flushObject(comp, module_obj_path);
                                                ^
/home/andy/Downloads/zig/src/link/Elf.zig:932:25: 0x65cbbfe in flush (zig)
    try self.flushModule(comp, prog_node);
                        ^
/home/andy/Downloads/zig/src/link.zig:823:68: 0x62992e3 in flush (zig)
            .elf => return @fieldParentPtr(Elf, "base", base).flush(comp, prog_node),
                                                                   ^
/home/andy/Downloads/zig/src/Compilation.zig:2657:24: 0x6298b45 in flush (zig)
    comp.bin_file.flush(comp, prog_node) catch |err| switch (err) {
                       ^
/home/andy/Downloads/zig/src/Compilation.zig:2614:23: 0x62dcd3e in update (zig)
        try comp.flush(main_progress_node);
                      ^
/home/andy/Downloads/zig/src/main.zig:4283:24: 0x630c692 in updateModule (zig)
        try comp.update(main_progress_node);
                       ^
/home/andy/Downloads/zig/src/main.zig:3684:17: 0x632f25a in buildOutputType (zig)
    updateModule(comp) catch |err| switch (err) {
                ^
/home/andy/Downloads/zig/src/main.zig:282:31: 0x613823f in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .{ .build = .Obj });
                              ^
/home/andy/Downloads/zig/src/main.zig:222:20: 0x6135315 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/home/andy/Downloads/zig/lib/std/start.zig:585:37: 0x6134d96 in main (zig)
            const result = root.main() catch |err| {
                                    ^
???:?:?: 0x7f15c7fe7acd in ??? (libc.so.6)
Unwind information for `libc.so.6:0x7f15c7fe7acd` was not available, trace may be incomplete

Aborted (core dumped)

That's if you're lucky! A release build of the compiler has much more misleading outcomes.

Expected Behavior

It should report an error that the archive is malformed. If, for example, I run nm on that file:

$ nm -a bad_libcompiler_rt.a
nm: compiler_rt.o: file format not recognized

This is much more helpful when diagnosing problems, especially when working on the compiler!

In general, the linker should not assert that input files are well-formed.

@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior linking labels Dec 5, 2023
@andrewrk andrewrk added this to the 0.13.0 milestone Dec 5, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior linking
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant