munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
9.03k stars 1.05k forks source link

Cannot compile clox under gcc 11.1.0 #996

Closed jovenlin0527 closed 2 years ago

jovenlin0527 commented 3 years ago

When I make clox, my GCC gives this error message:

In function ‘concatenate’,
    inlined from ‘run’ at c/vm.c:656:11:
c/vm.c:384:17: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
  384 |   chars[length] = '\0';
      |                 ^
lto1: all warnings being treated as errors
lto-wrapper: fatal error: cc returned 1 exit status

clang does not give this error message. This message also doesn't show up when I remove -flto from the Makefile.

HuyNguyenAu commented 3 years ago

I also run into this error on gcc version 11.2.1.

CC version:

 cc -v
Using built-in specs.
COLLECT_GCC=cc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-11.2.1-20210728/obj-x86_64-redhat-linux/isl-install --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.1 20210728 (Red Hat 11.2.1-1) (GCC) 

Make version:

$ make -v
GNU Make 4.3
Built for x86_64-redhat-linux-gnu
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Attempting to compile:

$ make
- site/index.css
- site/style.css
✓ Crafting Interpreters (14 words)
✓ Dedication (23 words)
✓ Acknowledgements (309 words)
✓ Table of Contents (14 words)
✓ I. Welcome (140 words)
  ✓ 1. Introduction (3913 words)
  ✓ 2. A Map of the Territory (5181 words)
  ✓ 3. The Lox Language (6595 words)
✓ II. A Tree-Walk Interpreter (185 words)
  ✓ 4. Scanning (7193 words, 294 loc)
  ✓ 5. Representing Code (7758 words, 169 loc)
  ✓ 6. Parsing Expressions (7546 words, 169 loc)
  ✓ 7. Evaluating Expressions (5546 words, 138 loc)
  ✓ 8. Statements and State (10223 words, 202 loc)
  ✓ 9. Control Flow (5481 words, 122 loc)
  ✓ 10. Functions (7976 words, 180 loc)
  ✓ 11. Resolving and Binding (7787 words, 231 loc)
  ✓ 12. Classes (9559 words, 235 loc)
  ✓ 13. Inheritance (5130 words, 98 loc)
✓ III. A Bytecode Virtual Machine (161 words)
  ✓ 14. Chunks of Bytecode (9236 words, 225 loc)
  ✓ 15. A Virtual Machine (7176 words, 142 loc)
  ✓ 16. Scanning on Demand (7238 words, 332 loc)
  ✓ 17. Compiling Expressions (8025 words, 248 loc)
  ✓ 18. Types of Values (5847 words, 143 loc)
  ✓ 19. Strings (6520 words, 164 loc)
  ✓ 20. Hash Tables (10058 words, 192 loc)
  ✓ 21. Global Variables (6006 words, 177 loc)
  ✓ 22. Local Variables (5667 words, 138 loc)
  ✓ 23. Jumping Back and Forth (7504 words, 165 loc)
  ✓ 24. Calls and Functions (11683 words, 295 loc)
  ✓ 25. Closures (12775 words, 226 loc)
  ✓ 26. Garbage Collection (10960 words, 213 loc)
  ✓ 27. Classes and Instances (4785 words, 134 loc)
  ✓ 28. Methods and Initializers (10076 words, 196 loc)
  ✓ 29. Superclasses (5556 words, 95 loc)
  ✓ 30. Optimization (10253 words, 72 loc)
✓ Backmatter (61 words)
  ✓ A1. Appendix I (614 words)
  ✓ A2. Appendix II (1611 words, 341 loc)
Built 207,252 words and 5,336 lines of code (232,385 total words) in 0.61 seconds
make[1]: Entering directory '/home/taro/Github/craftinginterpreters'
      cc build/clox                               -std=c99 -Wall -Wextra -Werror -Wno-unused-parameter -O3 -flto
In function ‘concatenate’,
    inlined from ‘run’ at c/vm.c:656:11:
c/vm.c:384:17: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
  384 |   chars[length] = '\0';
      |                 ^
lto1: all warnings being treated as errors
lto-wrapper: fatal error: cc returned 1 exit status
compilation terminated.
/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
make[1]: *** [util/c.make:45: build/clox] Error 1
make[1]: Leaving directory '/home/taro/Github/craftinginterpreters'
make: *** [Makefile:66: clox] Error 2
stefanos82 commented 3 years ago

I have the same issue; seems like it's a bug in GCC.

To remedy this, just reduce the -O3 flag to -O2 and will work just fine.

stefanos82 commented 3 years ago

OK, I have solve it and now works with -O3 too.

Open vm.c file, go to concatenate() and change

char* chars = ALLOCATE(char, length + 1);

to

char* volatile chars = ALLOCATE(char, length + 1);.

jovenlin0527 commented 2 years ago

Fixed in gcc 11.3.