-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[Clang][CodeGen][Sema] Fix crash when compiling naked lambdas #165524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alcxpr
wants to merge
9
commits into
llvm:main
Choose a base branch
from
alcxpr:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+183
−35
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f303f13
[Clang][CodeGen] Fix crash when compiling naked lambdas
alcxpr 54b6d3e
Merge branch 'main' into main
alcxpr 666aed6
[Clang][CodeGen] Apply clang-format
alcxpr b986c74
[Clang][CodeGen] Make naked lambda captures analyzed
alcxpr 6e341cb
Revert file back
alcxpr e5a214d
[Clang][CodeGen] Format
alcxpr 79d9422
[Clang][Sema] Remove naked attribute from lambdas with ODR-used captures
alcxpr ba6ece5
Remove compilation database
alcxpr d57f804
Remove FIXME in the special path
alcxpr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2332,6 +2332,20 @@ ExprResult Sema::BuildLambdaExpr(SourceLocation StartLoc, | |
| maybeAddDeclWithEffects(LSI->CallOperator); | ||
| } | ||
|
|
||
| // This is for GCC compatibility. If any lambda captures are actually used in the | ||
| // function body. GCC silently removes the naked attribute when captures are | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regardless of what gcc does here, silently ignoring the naked attribute is not acceptable: the result will almost certainly be broken. Probably simplest to just reject any usage of naked with captures; if gcc's behavior is broken, probably nobody is using it. |
||
| // ODR-used, as naked functions cannot have prologues to set up the closure. | ||
| if (CallOperator->hasAttr<NakedAttr>() && !Captures.empty()) { | ||
| // If any captures are ODR-used by examining the capture list | ||
| // that was already analyzed during semantic analysis, drop it. | ||
| for (const Capture &Cap : LSI->Captures) { | ||
| if (Cap.isODRUsed()) { | ||
| CallOperator->dropAttr<NakedAttr>(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return MaybeBindToTemporary(Lambda); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm %s -o - | FileCheck %s | ||
|
|
||
| void test_naked_lambda_capture_multi() { | ||
| int x = 42; | ||
| int y = 100; | ||
| auto l = [&x, y]() __attribute__((naked)) { | ||
| asm volatile("retq"); | ||
| }; | ||
| l(); | ||
| } | ||
|
|
||
| // CHECK-LABEL: define {{.*}} @"_ZZ31test_naked_lambda_capture_multivENK3$_0clEv" | ||
| // CHECK-NOT: load i32 | ||
| // CHECK-NOT: load ptr | ||
| // CHECK-NOT: getelementptr | ||
| // CHECK-NOT: alloca | ||
| // CHECK: call void asm sideeffect "retq" | ||
| // CHECK-NEXT: unreachable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm %s -o - | FileCheck %s | ||
| // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -S %s -o - | FileCheck %s --check-prefix=ASM | ||
|
|
||
| struct S { | ||
| int member; | ||
| void test_naked_lambda_capture_this() { | ||
| auto l = [this]() __attribute__((naked)) { | ||
| asm volatile("retq"); | ||
| }; | ||
| l(); | ||
| } | ||
| }; | ||
|
|
||
| void test() { | ||
| S s; | ||
| s.test_naked_lambda_capture_this(); | ||
| } | ||
|
|
||
| // CHECK-LABEL: define {{.*}} @_ZZN1S30test_naked_lambda_capture_thisEvENKUlvE_clEv | ||
| // CHECK-NOT: load ptr | ||
| // CHECK-NOT: getelementptr | ||
| // CHECK-NOT: alloca | ||
| // CHECK: call void asm sideeffect "retq" | ||
|
|
||
| // ASM-LABEL: _ZZN1S30test_naked_lambda_capture_thisEvENKUlvE_clEv: | ||
| // ASM-NOT: push | ||
| // ASM-NOT: mov | ||
| // ASM: retq |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm %s -o - | FileCheck %s | ||
| // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -S %s -o - | FileCheck %s --check-prefix=ASM | ||
|
|
||
| void test_naked_lambda_capture_var() { | ||
| int x = 42; | ||
| auto l = [x]() __attribute__((naked)) { | ||
| asm volatile("retq"); | ||
| }; | ||
| l(); | ||
| } | ||
|
|
||
| // CHECK-LABEL: define {{.*}} @"_ZZ29test_naked_lambda_capture_varvENK3$_0clEv" | ||
| // CHECK-NOT: load i32 | ||
| // CHECK-NOT: alloca | ||
| // CHECK-NOT: getelementptr | ||
| // CHECK: call void asm sideeffect "retq" | ||
|
|
||
| // ASM-LABEL: _ZZ29test_naked_lambda_capture_varvENK3$_0clEv: | ||
| // ASM-NOT: push | ||
| // ASM-NOT: mov | ||
| // ASM: retq |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm %s -o - | FileCheck %s | ||
|
|
||
| // Test that naked attribute is removed when captures are ODR-used (GCC compat) | ||
| void test_odr_used_captures() { | ||
| int x = 42; | ||
| int y = 6; | ||
| auto l = [x, &y]() __attribute__((naked)) { | ||
| asm volatile("movl %0, %%eax\n\tmovl %1, %%ebx\n\tretq" : : "r"(x), "r"(y)); | ||
| }; | ||
| l(); | ||
| } | ||
|
|
||
| // CHECK-LABEL: define internal void @"_ZZ22test_odr_used_capturesvENK3$_0clEv" | ||
| // CHECK-NOT: naked | ||
| // CHECK: alloca | ||
| // CHECK: store | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -emit-llvm %s -o - | FileCheck %s | ||
| // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -S %s -o - | FileCheck %s --check-prefix=ASM | ||
|
|
||
| void test_naked_lambda() { | ||
| auto l = []() __attribute__((naked)) { | ||
| asm volatile("retq"); | ||
| }; | ||
| l(); | ||
| } | ||
|
|
||
| // CHECK: define internal void @"_ZZ17test_naked_lambdavENK3$_0clEv" | ||
| // CHECK-NOT: alloca | ||
| // CHECK-NOT: store | ||
| // CHECK-NOT: call void @_ZN | ||
| // ASM-LABEL: _ZZ17test_naked_lambdavENK3$_0clEv: | ||
| // ASM-NOT: push | ||
| // ASM-NOT: pop | ||
| // ASM: retq |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // RUN: %clang_cc1 -fsyntax-only -verify %s -triple x86_64-pc-linux-gnu | ||
|
|
||
| void uses_capture() { | ||
| int x = 42; | ||
| int y = 6; | ||
| auto l = [x, &y]() __attribute__((naked)) { // expected-no-diagnostics | ||
| asm volatile("movl %0, %%eax\n\tmovl %1, %%ebx\n\tretq" : : "r"(x), "r"(y)); | ||
| }; | ||
| l(); | ||
| } | ||
|
|
||
| void unused_captures() { | ||
| int x = 42; | ||
| auto l = [x]() __attribute__((naked)) { // expected-no-diagnostics | ||
| asm volatile("retq"); | ||
| }; | ||
| l(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we refactor the code so it doesn't have two separate calls to getCaptureFields()?