Skip to content
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

Unreachable errors' strings included in binary #13111

Closed
wooster0 opened this issue Oct 9, 2022 · 4 comments
Closed

Unreachable errors' strings included in binary #13111

wooster0 opened this issue Oct 9, 2022 · 4 comments
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module.
Milestone

Comments

@wooster0
Copy link
Contributor

wooster0 commented Oct 9, 2022

Zig Version

0.10.0-dev.4247+3234e8de3

Steps to Reproduce

pub fn main() void {
    x() catch unreachable;
}

fn x() !void {
    var y: u1 = undefined;
    switch (y) {
        0 => return error.SomeError,
        1 => return error.SomeOtherError,
    }
}

Expected Behavior

$ zig build-exe x.zig -O ReleaseSmall --strip
$ strings x | grep SomeError
$

No error strings are found in the binary.

Actual Behavior

$ zig build-exe x.zig -O ReleaseSmall --strip
$ strings x | grep SomeError
SomeError
$

A bunch of error strings are included in the binary, including some other strings:

$ strings x
attempt to unwrap error: {s}
OutOfMemory
InputOutput
TruncatedInput
Timeout
NoSpaceLeft
SystemResources
SomeOtherError
SomeError
ConnectionResetByPeer
Utf8ExpectedContinuation
LockViolation
WouldBlock
NotOpenForWriting
Utf8OverlongEncoding
LimitTooBig
FileTooBig
Utf8EncodesSurrogateHalf
Utf8InvalidStartByte
BrokenPipe
Utf8CodepointTooLarge
OperationNotSupported
MemoryMappingNotSupported
OperationAborted
Unexpected
AccessDenied
PermissionDenied
LockedMemoryLimitExceeded
DiskQuota
Expected closing ]
(msg truncated)
thread {} panic:
Unable to dump stack trace: debug info stripped
Panicked during a panic. Aborting.

It could very well be that I'm just reporting some issue that is very well known or a duplicate and it's still being worked on, but I'm just opening this to be sure.

stage1 does it better:

$ zig build-exe x.zig -O ReleaseSmall --strip -fstage1
$ strings x | grep SomeError
$ strings x
j	Xj
Zj"AZj
Linker: LLD 15.0.0
.rodata
.text
.bss
.comment
.shstrtab

It still includes "Linker: LLD 15.0.0" which ultimately is something that should go away as well, but it's a lot better than stage2 still. Probably if we use our own linker we can fix that. Maybe it's illegal to strip it if we use LLD.

@wooster0 wooster0 added the bug Observed behavior contradicts documented or intended behavior label Oct 9, 2022
@andrewrk andrewrk added this to the 0.11.0 milestone Oct 14, 2022
@andrewrk andrewrk added backend-llvm The LLVM backend outputs an LLVM IR Module. and removed bug Observed behavior contradicts documented or intended behavior labels Oct 14, 2022
@wooster0
Copy link
Contributor Author

wooster0 commented Jan 6, 2023

About the backend-llvm label, doesn't it affect the in-house backends and output formats too?

$ cat minimal.zig
pub fn main() void {}
$ stage3/bin/zig build-exe minimal.zig -ofmt=plan9 -OReleaseSmall
$ wc -c minimal
1959 minimal
$ hexdump -C minimal | grep -n10 featureS
107-000006a0  ff ff fa 00 00 00 00 00  00 00 00 20 00 28 f4 5f  |........... .(._|
108-000006b0  73 74 61 72 74 32 00 00  00 00 00 00 20 00 28 d4  |start2...... .(.|
109-000006c0  5f 73 74 61 72 74 00 00  00 00 00 00 20 00 2f f4  |_start...... ./.|
110-000006d0  63 61 6c 6c 4d 61 69 6e  32 00 00 00 00 00 00 20  |callMain2...... |
111-000006e0  00 6e f4 65 78 69 74 32  00 00 00 00 00 00 00 00  |.n.exit2........|
112-000006f0  01 fa 00 00 01 00 07 00  08 00 09 00 04 00 0a 00  |................|
113-00000700  00 00 00 00 00 7f ff ff  ff fa 00 00 00 00 00 00  |................|
114-00000710  00 00 20 00 8f f4 6d 61  69 6e 00 00 00 00 00 00  |.. ...main......|
115-00000720  00 00 01 fa 00 00 01 00  02 00 03 00 04 00 05 00  |................|
116-00000730  0b 00 00 00 00 00 00 7f  ff ff ff fa 00 00 00 00  |................|
117:00000740  00 00 00 00 20 00 9a f4  66 65 61 74 75 72 65 53  |.... ...featureS|
118-00000750  65 74 00 00 00 00 00 00  20 02 4e f4 65 6d 70 74  |et...... .N.empt|
119-00000760  79 5f 77 6f 72 6b 61 72  6f 75 6e 64 00 00 00 00  |y_workaround....|
120-00000770  00 00 20 03 16 f4 61 64  64 46 65 61 74 75 72 65  |.. ...addFeature|
121-00000780  00 01 02 85 42 04 bd 44  00 00 00 00 4a 9f 00 ff  |....B..D....J...|
122-00000790  ff ff b6 89 06 c0 c0 c0  c0 c0 c0 b2 46 02 c0 c0  |............F...|
123-000007a0  c0 86 42 04 c0 c0 a8                              |..B....|
124-000007a7

All the backends seem to scan all decls and keep all of them instead of keeping only the referenced one.

Or Wasm...

$ stage3/bin/zig build-exe minimal.zig -ofmt=wasm -OReleaseSmall -target wasm32-wasi -fno-LLVM
$ wasm2wat minimal.wasm
(module
  (type (;0;) (func))
  (type (;1;) (func (param i32)))
  (import "wasi_snapshot_preview1" "proc_exit" (func (;0;) (type 1)))
  (func (;1;) (type 0)
    return)
  (func (;2;) (type 0)
    call 1
    i32.const 0
    call 0)
  (func (;3;) (type 1) (param i32)
    (local i32)
    block  ;; label = @1
      local.get 0
      i32.eqz
      local.set 1
      block  ;; label = @2
        local.get 1
        br_if 0 (;@2;)
        br 1 (;@1;)
      end
      unreachable
    end
    return)
  (table (;0;) 0 funcref)
  (memory (;0;) 17)
  (global (;0;) (mut i32) (i32.const 1048576))
  (export "_start" (func 2))
  (export "memory" (memory 0))
  (data (;0;) (i32.const 1048576) "\00\05\00\00\00\00\00\00\04\00\00\00\00\00\00\00$\00\00\00\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa\aa"))

Seems like some pretty fundamental issue.

@Vexu
Copy link
Member

Vexu commented Jan 6, 2023

All the backends seem to scan all decls and keep all of them instead of keeping only the referenced one

That is #6256

@wooster0
Copy link
Contributor Author

wooster0 commented Jan 6, 2023

Will leave this open until either there is a solution or it becomes an acknowledged and permanent flaw in the language.

Would be pretty sad.

@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@wooster0
Copy link
Contributor Author

wooster0 commented Jan 9, 2025

This seems to be fixed. Bloat seems to have gone down.

~$ cat x.zig
pub fn main() void {
    x() catch unreachable;
}

fn x() !void {
    var y: u1 = undefined;
    _ = &y;
    switch (y) {
        0 => return error.SomeError,
        1 => return error.SomeOtherError,
    }
}
~$ zig build-exe x.zig -O ReleaseSmall
~$ strings x | grep Error
~$

There are still other undesired strings included but that is more or less tracked by #22270

@wooster0 wooster0 closed this as completed Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module.
Projects
None yet
Development

No branches or pull requests

3 participants