OpenXiangShan / NEMU

Other
236 stars 90 forks source link

fix(snprintf),refactor(small/fast log): fix truncated writing warning, refactor output to log file #531

Closed xyyy1420 closed 1 month ago

xyyy1420 commented 1 month ago

When snprintf dest buffer is a static array with fixed length, and src also a static array with fixed length, compiler will tell if there will be a write truncation and warn about it. This commit could fix this warn.

The logic for outputting the log to a file was a bit hard to understand, refactored this part of the code.

shinezyy commented 1 month ago

LGTM

For others to understand this patch, can you attach a log or screenshot of the error before this patch?

xyyy1420 commented 1 month ago
In file included from /nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/common.h:26,
                 from /nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/src/isa/riscv64/include/isa-def.h:20,
                 from /nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/isa.h:21,
                 from /nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/cpu/decode.h:20,
                 from /nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/rtl/rtl.h:19,
                 from src/isa/riscv64/instr/../local-include/rtl.h:20,
                 from src/isa/riscv64/instr/decode.c:17:
src/isa/riscv64/instr/decode.c:26:1: error: ‘%s’ directive output may be truncated writing up to 39 bytes into a region of size between 33 and 72 [-Werror=format-truncation=]
   26 | def_all_THelper();
      | ^~~~~~~~~~~~~~~~~~
/nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/macro.h:69:21: note: in definition of macro ‘__KEEP’
   69 | #define __KEEP(...) __VA_ARGS__
      |                     ^~~~~~~~~~~
/nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/cpu/decode.h:183:24: note: in expansion of macro ‘IFDEF’
  183 | #define print_asm(...) IFDEF(CONFIG_DEBUG, snprintf(log_asmbuf, sizeof(log_asmbuf), __VA_ARGS__))
      |                        ^~~~~
/nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/cpu/decode.h:199:3: note: in expansion of macro ‘print_asm’
  199 |   print_asm(str(instr) "%c %s,%s,%s", suffix_char(id_dest->width), id_dest->str, id_src1->str, id_src2->str)
      |   ^~~~~~~~~
/nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/macro.h:23:16: note: in expansion of macro ‘str_temp’
   23 | #define str(x) str_temp(x)
      |                ^~~~~~~~
/nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/cpu/decode.h:199:13: note: in expansion of macro ‘str’
  199 |   print_asm(str(instr) "%c %s,%s,%s", suffix_char(id_dest->width), id_dest->str, id_src1->str, id_src2->str)
      |             ^~~
/nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/macro.h:32:27: note: in expansion of macro ‘print_asm_template3’
   32 | #define concat_temp(x, y) x ## y
      |                           ^
/nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/macro.h:33:22: note: in expansion of macro ‘concat_temp’
   33 | #define concat(x, y) concat_temp(x, y)
      |                      ^~~~~~~~~~~
/nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/cpu/decode.h:103:23: note: in expansion of macro ‘concat’
  103 |   def_THelper(name) { concat(print_asm_template, arity)(name); return concat(EXEC_ID_, name); }
      |                       ^~~~~~
/nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/cpu/decode.h:107:35: note: in expansion of macro ‘def_THelper_arity’
  107 | #define def_THelper_ternary(name) def_THelper_arity(name, 3)
      |                                   ^~~~~~~~~~~~~~~~~
src/isa/riscv64/instr/../include/isa-all-instr.h:362:3: note: in expansion of macro ‘def_THelper_ternary’
  362 |   f(p_inc) f(p_dec) \
      |   ^
/nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/macro.h:84:19: note: in expansion of macro ‘INSTR_TERNARY’
   84 | #define MAP(c, f) c(f)
      |                   ^
/nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/cpu/decode.h:117:3: note: in expansion of macro ‘MAP’
  117 |   MAP(INSTR_TERNARY,      def_THelper_ternary) \
      |   ^~~
src/isa/riscv64/instr/decode.c:26:1: note: in expansion of macro ‘def_all_THelper’
   26 | def_all_THelper();
      | ^~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:894,
                 from /nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/debug.h:20,
                 from /nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/common.h:56,
                 from /nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/src/isa/riscv64/include/isa-def.h:20,
                 from /nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/isa.h:21,
                 from /nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/cpu/decode.h:20,
                 from /nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/include/rtl/rtl.h:19,
                 from src/isa/riscv64/instr/../local-include/rtl.h:20,
                 from src/isa/riscv64/instr/decode.c:17:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:10: note: ‘__builtin___snprintf_chk’ output between 10 and 127 bytes into a destination of size 80
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make: *** [/nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/scripts/build.mk:58: /nfs/home/jiaxiaoyu/fix_fs_vs/NEMU/build/obj-riscv64-nemu-interpreter-so/src/isa/riscv64/instr/decode.o] Error 1

before this patch, when i use riscv64-xs-ref_defconfig and enable debug feature, trace instructions, rich log config, i meet some errors like this