llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

jump into statement expression in unevaluated context

Open ChristopheSev opened this issue 3 years ago • 10 comments

The C program

int main(void) {
  goto L;
  sizeof (int){({ L:; 1; })};
  return 0;
}

makes Clang crash

PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
[...]
clang-12: error: unable to execute command: Segmentation fault (core dumped)
clang-12: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 12.0.0 (https://github.com/llvm/llvm-project.git d28af7c654d8db0b68c175db5ce212d74fb5e9bc)
Target: x86_64-unknown-linux-gnu
Thread model: posix

ChristopheSev avatar Jul 05 '23 09:07 ChristopheSev

Still happens in trunk: https://godbolt.org/z/qnxq9a3WP

tbaederr avatar Jul 05 '23 14:07 tbaederr

@llvm/issue-subscribers-clang-frontend

llvmbot avatar Jul 05 '23 14:07 llvmbot

Still problem in main: https://godbolt.org/z/MP3v77vPP

EugeneZelenko avatar Jul 05 '23 14:07 EugeneZelenko

I feel like we should diagnose this like this case:

void f() {
label:
  {}
}

void g() {
    goto label;
}

but looking at how err_undeclared_label_use is handled for this case it is not obvious.

shafik avatar Jul 05 '23 18:07 shafik

@shafik agreed, we are probably not supposed to jump contexts!

cor3ntin avatar Jul 06 '23 19:07 cor3ntin

gcc produces "error: jump into statement expression". We probably want something similar. (I think we need to continue support jumping out of statement expressions, i.e. a goto inside a statement expression with a label outside it.)

efriedma-quic avatar Jul 06 '23 20:07 efriedma-quic

https://reviews.llvm.org/D154696

Note that this is technically a breaking change. I wonder if anyone cares about jumping into expression statements though (it's not supported by GCC)

cor3ntin avatar Jul 07 '23 09:07 cor3ntin

I had to revert.

The issue is that VerifyIndirectOrAsmJumps does not consider that labels may be local labels at all, I'm not exactly sure how to improve that. The fact it works currently seems kind of brittle, what prevent incorrect jumps to be ill-formed is that we looked if a label is defined in the same scope as the __local__ label introduction, but for the purpose of checking jumps __local__ labels are not considered at all, it also fail in other cases, for example using try blocks or any kind of blocks https://compiler-explorer.com/z/Mrxonn9dc

@jyu2-git any idea?

cor3ntin avatar Jul 12 '23 20:07 cor3ntin

I had to revert.

The issue is that VerifyIndirectOrAsmJumps does not consider that labels may be local labels at all, I'm not exactly sure how to improve that. The fact it works currently seems kind of brittle, what prevent incorrect jumps to be ill-formed is that we looked if a label is defined in the same scope as the __local__ label introduction, but for the purpose of checking jumps __local__ labels are not considered at all, it also fail in other cases, for example using try blocks or any kind of blocks https://compiler-explorer.com/z/Mrxonn9dc

@jyu2-git any idea?

Sorry I don't really remember this. It looks like the problem related to ScopeChecking with HasBranchProtectedScope.

jyu2-git avatar Jul 13 '23 16:07 jyu2-git

The problem seems to be the targets is not asm_goto's label. The error message is wrong. I think the check only needed if the asm_goto's label == taregetLabel. I will fix this.

jyu2-git avatar Jul 14 '23 01:07 jyu2-git