AkihiroSuda/myaot

glibc: stdout is misbuffered

AkihiroSuda opened this issue · 1 comments

foo.c:

#include <stdio.h>
int main(int argc, char **argv) {
  printf("Hello, world, take 1\n");
  printf("Hello, world, take 2\n");
  printf("Hello, world, take 3\n");
  printf("Hello, world, take 4\n");
  printf("Hello, world, take 5\n");
  printf("Hello, world, take 6\n");
  printf("Hello, world, take 7\n");
  printf("Hello, world, take 8\n");
  printf("Hello, world, take 9\n");
  printf("Hello, world, take 10\n");
}
$ /opt/riscv/bin/riscv32-unknown-linux-gnu-gcc -march=rv32i -mabi=ilp32 -O3 -static -o foo-riscv32 foo.c
$ myaot compile ./foo-riscv32
INFO[0000] Compiling ./foo-riscv32 --> a.out.c          
INFO[0000] Compiling a.out.c --> a.out                  
INFO[0006] Removing a.out.c                             
INFO[0006] Done: a.out
$ ./a.out
Hello, world, take 1
Hel
   o, orl, tke 2
He
  o,or
      , ke 3
Ho
  o,ke 4
Hello, world, take 5
Hel
   o, orl, tke 6
He
  o,or
      , ke 7
Ho
  o,ke 8
Hello, world, take 9
Hel
   o, orl, tke 10

Version: 4d4e7b8

The memory isn't corrupted, but glibc does some buffering of stdout in an unexpected way.
https://atmarkit.itmedia.co.jp/ait/articles/1703/01/news170_3.html

Inserting fflush(stdout) after printf() seems to work as a workaround.