0

In connection with this question I have another question.

I managed to reproduce it and I do not copy paste the code here again, as you can find the code. I paste only the output of compilation on my computer.

% gcc -std=c11 -O3 -g -Wall -Wextra -Werror -USUPPRESS_BUG  -c  msg_gcc.c
msg_gcc.c: In function 'function_under_test':
msg_gcc.c:30:9: error: 'strncpy' output may be truncated copying 128 bytes from a string of length 128 [-Werror=stringop-truncation]
         strncpy(name, name_in_queue, SERVERNAME_LEN);
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
-root-@ @...| test | stub
% gcc --version
gcc (GCC) 8.2.1 20181127
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

-root-@ @...| test | stub
% cat /proc/version
Linux version 5.0.7-arch1-1-ARCH (builduser@heftig-20167) (gcc version 8.2.1 20181127 (GCC)) #1 SMP PREEMPT Mon Apr 8 10:37:08 UTC 2019
-root-@ @...| test | stub

I wish I would understand in which stage the compiler detects this kind of library issue. I know that the code is transformed in lots of intermediate languages, like generic, gimple, ssa, rtl, combinators, etc, etc but I do not know in which of these representations the code is checked for issues like this one from this compilation command.

If I want to debug the intermediate representations and discover the presence of this issue what parameters should I pass gcc such that it stops the generation of intermediate languages as close as possible before this warning to be generated ?

alinsoar
  • 15,386
  • 4
  • 57
  • 74

1 Answers1

2

Your error/warning seems to be originating from here. If I understand correctly, this is from the SSA form generation phase.

Also this question might provide useful hints on how to dump different intermediate representations for gcc. I know how to do it for clang, but your issue seems be to specific to gcc.

Ajay Brahmakshatriya
  • 8,993
  • 3
  • 26
  • 49
  • You mean, when it arrives in SSA stage the ssa is passed to some semantic analysis tool that is included in GCC to check for `'strncpy' output may be truncated` ? How can I know what tool is it and what functionality that tool has ? – alinsoar May 22 '19 at 17:06
  • There are about 60 files `tree-ssa-*`. They are part of Standard Library ? The Standard Library is part of the compiler ? – alinsoar May 22 '19 at 17:39
  • I asked as I am interested to understand how the semantic checking is done at that level, not necessarily how gcc does in particular. If you can detail the logic/algorithm for semantic checking that is used by clang it's interesting. – alinsoar May 22 '19 at 17:48
  • The files `tree-ssa-*` are not the implementations of the standard library functions. Usually these functions are implemented separately as a part of libc (gcc's version is glibc and clang inherits from the compiler that was used to compile it). The files I pointed to implementations of specially handling these functions because the compiler identifies these functions and tries to put extra checks or tries to optimize it by replacing it with intrinsics. – Ajay Brahmakshatriya May 22 '19 at 18:02
  • Ah, it tries to replace some calls to standard library with some simpler inline sequences that follow the `as-if rule` and simulate the behavior of standard library -- correct ? – alinsoar May 22 '19 at 18:08
  • @alinsoar yes, atleast in clang many standard library functions (including string and memset, memcpy etc) are replaced with builtin intrinsics. The lowering mechanism then decides what is the right way to lower these. For example, for memcpy of fixed size (say 8 bytes), it is better to insert 2 mov instructions rather than insert a call. Or for slightly larger sizes, it sometimes inserts an unrolled loop. All these decisions are based on cost models specific for the target architecture. – Ajay Brahmakshatriya May 22 '19 at 18:28