Skip to content

std.os.posixWrite hits posix.EAGAIN when using debug.warn while reading stdin from a fuzzer called radamsa #1882

Open

Description

@Hejsil

radamsa

a.zig

const std = @import("std");

const debug = std.debug;

pub fn main() !void {
    debug.warn("{}\n", "a" ** (1024 * 1024));
}
$ echo 'a' | radamsa -n inf -s 1 | ./a
...aaaaaaaaaaaaaaaaaaaaaaaaaa^Cstoppingon signal
[1]    23836 done                 echo 'a' |
       23837 exit 42              radamsa -n inf -s 1 |
       23838 abort (core dumped)  ./a

This does not happen if we output our input to a file first and use cat

$ echo 'a' | radamsa -n inf -s 1 > out
^Cstopping on signal
$ cat out | ./a
...aaaaaaa

Here's my stacktrace from the coredump

Program terminated with signal SIGABRT, Aborted.
#0  0x000000000031ca0e in syscall4 (number=14) at /home/jhc/repo/zig/build/stage1/lib/zig/std/os/linux/x86_64.zig:428
428         return asm volatile ("syscall"
(gdb) backtrace
#0  0x000000000031ca0e in syscall4 (number=14) at /home/jhc/repo/zig/build/stage1/lib/zig/std/os/linux/x86_64.zig:428
#1  0x00000000003062f5 in restoreSignals (set=0x7ffec6c075f8) at /home/jhc/repo/zig/build/stage1/lib/zig/std/os/linux/index.zig:1139
#2  0x00000000003058b1 in raise (sig=6) at /home/jhc/repo/zig/build/stage1/lib/zig/std/os/linux/index.zig:1126
#3  0x0000000000305192 in abort () at /home/jhc/repo/zig/build/stage1/lib/zig/std/os/index.zig:184
#4  0x00000000003050d6 in panicExtra () at /home/jhc/repo/zig/build/stage1/lib/zig/std/debug/index.zig:162
#5  0x0000000000305078 in panic (msg=...) at /home/jhc/repo/zig/build/stage1/lib/zig/std/special/panic.zig:22
#6  0x000000000031cad1 in posixWrite (fd=2) at /home/jhc/repo/zig/build/stage1/lib/zig/std/os/index.zig:377
#7  0x0000000000306313 in File_write (self=...) at /home/jhc/repo/zig/build/stage1/lib/zig/std/os/file.zig:426
#8  0x00000000003059d5 in File_OutStream_writeFn (out_stream=0x328020 <stderr_file_out_stream+8>) at /home/jhc/repo/zig/build/stage1/lib/zig/std/os/file.zig:484
#9  0x0000000000306381 in formatType () at /home/jhc/repo/zig/build/stage1/lib/zig/std/fmt/index.zig:237
#10 0x0000000000305a2c in format () at /home/jhc/repo/zig/build/stage1/lib/zig/std/fmt/index.zig:54
#11 0x00000000003052eb in OutStream(error{SystemResources,OperationAborted,BrokenPipe,Unexpected,DiskQuota,FileTooBig,InputOutput,NoSpaceLeft,AccessDenied,})_print ()
    at /home/jhc/repo/zig/build/stage1/lib/zig/std/io.zig:219
#12 0x0000000000305130 in panicExtra () at /home/jhc/repo/zig/build/stage1/lib/zig/std/debug/index.zig:165
#13 0x0000000000305078 in panic (msg=...) at /home/jhc/repo/zig/build/stage1/lib/zig/std/special/panic.zig:22
#14 0x000000000031cad1 in posixWrite (fd=2) at /home/jhc/repo/zig/build/stage1/lib/zig/std/os/index.zig:377
#15 0x0000000000306313 in File_write (self=...) at /home/jhc/repo/zig/build/stage1/lib/zig/std/os/file.zig:426
#16 0x00000000003059d5 in File_OutStream_writeFn (out_stream=0x328020 <stderr_file_out_stream+8>) at /home/jhc/repo/zig/build/stage1/lib/zig/std/os/file.zig:484
#17 0x0000000000324079 in formatText () at /home/jhc/repo/zig/build/stage1/lib/zig/std/fmt/index.zig:372
#18 0x0000000000323fbc in formatType () at /home/jhc/repo/zig/build/stage1/lib/zig/std/fmt/index.zig:242
#19 0x0000000000323e1c in format () at /home/jhc/repo/zig/build/stage1/lib/zig/std/fmt/index.zig:54
#20 0x0000000000323cab in OutStream(error{SystemResources,OperationAborted,BrokenPipe,Unexpected,DiskQuota,FileTooBig,InputOutput,NoSpaceLeft,AccessDenied,})_print ()
    at /home/jhc/repo/zig/build/stage1/lib/zig/std/io.zig:219
#21 0x00000000003234f2 in warn () at /home/jhc/repo/zig/build/stage1/lib/zig/std/debug/index.zig:47

We hit an unreachable in os.posixWrite which then panics again for the same reason (which just aborts the program).

Activity

  1. added
    bug
    standard library
    on Jan 18, 2019
  2. added this to the 0.4.0 milestone on Jan 18, 2019
  3. added
    contributor friendly
    on Jan 18, 2019
  4. modified the milestones: 0.4.0, 0.5.0 on Mar 1, 2019
  5. modified the milestones: 0.5.0, 0.6.0 on Apr 30, 2019
  6. modified the milestones: 0.6.0, 0.7.0 on Jan 3, 2020
  7. marnix commented on Sep 2, 2020

    @marnix

    Information: Cannot reproduce the core dump or EAGAIN with recent zig master (0.6.0 + fc00111) (and Radamsa 0.6) on Ubuntu 18.04.5 using updated reproduction script

    const std = @import("std");
    
    const debug = std.debug;
    
    pub fn main() !void {
        debug.print("{0}\n", .{"a" ** (1024 * 1024)});
    }

    However, the

    $ echo 'a' | radamsa -n inf -s 1 | ./a

    command line now makes ./a wait indefinitely. Replacing inf by something like 5000 makes ./a wait for 5-10 seconds...

  8. modified the milestones: 0.7.0, 0.8.0 on Oct 30, 2020
  9. modified the milestones: 0.8.0, 0.9.0 on Nov 6, 2020
  10. modified the milestones: 0.9.0, 0.10.0 on May 19, 2021
  11. git-bruh commented on Sep 12, 2023

    @git-bruh
    Contributor

    This seems to be fixed now

    testuser@shed radamsa $ cat src/main.zig                                                                     
    const std = @import("std");
    
    const debug = std.debug;
    
    pub fn main() !void {
        debug.print("{s}\n", .{"a" ** (1024 * 1024)});
    }
    testuser@shed radamsa $ zig build-exe src/main.zig                                                           
    LLVMtestuser@shed radamsa $                                                                                      
    testuser@shed radamsa $ cat src/main.zig                                                                     
    const std = @import("std");
    
    const debug = std.debug;
    
    pub fn main() !void {
        debug.print("{s}\n", .{"a" ** (1024 * 1024)});
    }
    testuser@shed radamsa $ zig build-exe src/main.zig                                                           
    testuser@shed radamsa $ time sh -c "echo 'a' | ./bin/radamsa -n inf -s 1 | ./main" 
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
    real	0m 0.03s
    user	0m 0.03s
    sys	0m 0.00s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcontributor friendlystandard library

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions