llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.31k stars 11.69k forks source link

overrestrictive asm goto gotolabels limitations #45248

Open jshufro opened 4 years ago

jshufro commented 4 years ago
Bugzilla Link 45903
Version 10.0
OS Linux
CC @DougGregor,@isanbard,@jshufro,@nickdesaulniers,@zygoloid

Extended Description

First of all, I apologize if this is the wrong venue for this bug report. The following does not compile, even though I believe it should:

cleanup_cb(*p1) {}

foo(int n) {
  int cond;

  if (({
        asm goto("" ::"r"(cond) : : label0);
        1;
      }))
  label0:;

  int a[n];

  if (({
        asm goto("" ::"r"(cond) : : label1);
        1;
      }))
  label1:;

}

main() {}
jshufro@660:~/asmgoto$ clang-10 main.c
main.c:1:13: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
cleanup_cb(*p1) {}
            ^
main.c:1:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
cleanup_cb(*p1) {}
^
main.c:1:18: warning: non-void function does not return a value [-Wreturn-type]
cleanup_cb(*p1) {}
                 ^
main.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
foo(int n) {
^
main.c:7:9: error: cannot jump from this asm goto statement to one of its possible targets
        asm goto("" ::"r"(cond) : : label0);
        ^
main.c:18:3: note: possible target of asm goto statement
  label1:;
  ^
main.c:12:7: note: jump bypasses initialization of variable length array
  int a[n];
      ^
main.c:22:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main() {}
^
5 warnings and 1 error generated.

This is C-Reduced to be fairly minimal. Replacing the variable length array with an attribute((cleanup)) variable causes a similar issue. It's worth noting that the error is noting a possible target of asm goto that isn't listed in the GoToLabels.

jshufro commented 4 years ago

Also pasted the wrong program for the shown output. It has been a long day.

#include <stdio.h>

foo(int n) {
  int cond;

  printf("'Twas brillig and the slithy toves did gyre and gimble in the wabe.\n");

  asm goto("" ::"r"(cond) : : label0);
  label0:;

  int a[n];
  printf("All mimsy were the borogoves, ");

  asm goto("" ::"r"(cond) : : label1);
  label1:;

  printf("and the mome raths outgrabe.\n");

}

main() {foo(1);}
jshufro commented 4 years ago

Sorry- pasted a bit more of my terminal output than I intended.

jshufro commented 4 years ago
foo(int n) {
  int cond;

  asm goto("" ::"r"(cond) : : label0);
  label0:;

  int a[n];
  printf("'Twas brillig and the slithy toves did gyre and gimble in the wabe. All mimsy were the borogoves, and the mome raths outgrabe.");

  asm goto("" ::"r"(cond) : : label1);
  label1:;

}

main() {}
jacob@Joshua:~/tmp$ clang-10 main.c
main.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
foo(int n) {
^
main.c:8:3: error: cannot jump from this asm goto statement to one of its possible targets
  asm goto("" ::"r"(cond) : : label0);
  ^
main.c:15:3: note: possible target of asm goto statement
  label1:;
  ^
main.c:11:7: note: jump bypasses initialization of variable length array
  int a[n];
      ^
main.c:21:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main() {foo(1);}
^
2 warnings and 1 error generated.
(failed reverse-i-search)`hg': ssh dh_apn5p3@s^Cf.ro
jacob@Joshua:~/tmp$ clang-10 main.c
main.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
foo(int n) {
^
main.c:8:3: error: cannot jump from this asm goto statement to one of its possible targets
  asm goto("" ::"r"(cond) : : label0);
  ^
main.c:15:3: note: possible target of asm goto statement
  label1:;
  ^
main.c:11:7: note: jump bypasses initialization of variable length array
  int a[n];
      ^
main.c:21:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main() {foo(1);}
^
2 warnings and 1 error generated.
jacob@Joshua:~/tmp$ gcc main.c
main.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
    3 | foo(int n) {
      | ^~~
main.c:21:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
   21 | main() {foo(1);}
      | ^~~~
jacob@Joshua:~/tmp$ ./a.out 
'Twas brillig and the slithy toves did gyre and gimble in the wabe.
All mimsy were the borogoves, and the mome raths outgrabe.
jacob@Joshua:~/tmp$ 
isanbard commented 4 years ago

Could you give an example of adding code around the label that still triggers the bug?

jshufro commented 4 years ago

I think that's not accurate, because adding code in and around the labels, or changing the conditionals to evaluate to false still presents this bug.

isanbard commented 4 years ago

It's giving this warning because the "callbr" instruction can't have the default target listed in the indirect target list. My initial guess is that the front-end thinks that "label1" and "label0" are to the same place. Probably because the conditionals for the "if" statements are always true.

nickdesaulniers commented 2 years ago

More discussion of this issue: https://reviews.llvm.org/D129288#3635747 cc @nikic @jyknight