Skip to content

[clang][CodeGen] Don't crash on output whose size is zero. #99849

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

Merged
merged 1 commit into from
Jul 30, 2024

Conversation

yetingk
Copy link
Contributor

@yetingk yetingk commented Jul 22, 2024

This fixes issue #63878 caused by creating an integer with zero bitwidth.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Jul 22, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 22, 2024

@llvm/pr-subscribers-clang-codegen

Author: Yeting Kuo (yetingk)

Changes

This fixes issue #63878 caused by creating an integer with zero bitwidth.


Full diff: https://github.com/llvm/llvm-project/pull/99849.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGStmt.cpp (+4-1)
  • (added) clang/test/CodeGen/inline-asm-sizeless.c (+7)
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index aa97f685ac7a9..92955e6cf4c96 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2751,7 +2751,10 @@ void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
 
       if (RequiresCast) {
         unsigned Size = getContext().getTypeSize(QTy);
-        Ty = llvm::IntegerType::get(getLLVMContext(), Size);
+        if (Size)
+          Ty = llvm::IntegerType::get(getLLVMContext(), Size);
+        else
+          CGM.Error(S.getAsmLoc(), "Output operand is sizeless!");
       }
       ResultRegTypes.push_back(Ty);
       // If this output is tied to an input, and if the input is larger, then
diff --git a/clang/test/CodeGen/inline-asm-sizeless.c b/clang/test/CodeGen/inline-asm-sizeless.c
new file mode 100644
index 0000000000000..29c1fc5b0b8f7
--- /dev/null
+++ b/clang/test/CodeGen/inline-asm-sizeless.c
@@ -0,0 +1,7 @@
+// RUN: not %clang_cc1 -S %s -o /dev/null 2>&1 | FileCheck %s
+
+// CHECK: error: Output operand is sizeless!
+void foo(void) {
+    extern long bar[];
+    asm ("" : "=r"(bar));
+}

@llvmbot
Copy link
Member

llvmbot commented Jul 22, 2024

@llvm/pr-subscribers-clang

Author: Yeting Kuo (yetingk)

Changes

This fixes issue #63878 caused by creating an integer with zero bitwidth.


Full diff: https://github.com/llvm/llvm-project/pull/99849.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGStmt.cpp (+4-1)
  • (added) clang/test/CodeGen/inline-asm-sizeless.c (+7)
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index aa97f685ac7a9..92955e6cf4c96 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2751,7 +2751,10 @@ void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
 
       if (RequiresCast) {
         unsigned Size = getContext().getTypeSize(QTy);
-        Ty = llvm::IntegerType::get(getLLVMContext(), Size);
+        if (Size)
+          Ty = llvm::IntegerType::get(getLLVMContext(), Size);
+        else
+          CGM.Error(S.getAsmLoc(), "Output operand is sizeless!");
       }
       ResultRegTypes.push_back(Ty);
       // If this output is tied to an input, and if the input is larger, then
diff --git a/clang/test/CodeGen/inline-asm-sizeless.c b/clang/test/CodeGen/inline-asm-sizeless.c
new file mode 100644
index 0000000000000..29c1fc5b0b8f7
--- /dev/null
+++ b/clang/test/CodeGen/inline-asm-sizeless.c
@@ -0,0 +1,7 @@
+// RUN: not %clang_cc1 -S %s -o /dev/null 2>&1 | FileCheck %s
+
+// CHECK: error: Output operand is sizeless!
+void foo(void) {
+    extern long bar[];
+    asm ("" : "=r"(bar));
+}

@@ -0,0 +1,7 @@
// RUN: not %clang_cc1 -S %s -o /dev/null 2>&1 | FileCheck %s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use "-verify" to check clang diagnostics, not FileCheck.

Is there any existing file with inline asm diagnostics this can be added to?

Copy link
Contributor Author

@yetingk yetingk Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used "-verify". I think the only one suitable file is clang/test/CodeGen/LoongArch/inline-asm-constraints-error.c, but it is target related.

if (Size)
Ty = llvm::IntegerType::get(getLLVMContext(), Size);
else
CGM.Error(S.getAsmLoc(), "Output operand is sizeless!");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the SourceLocation of the specific operand in question, instead of using S.getAsmLoc(), which points at the beginning of the asm statement?

Diagnostics aren't capitalized, and don't end in punctuation.

Maybe say "size zero" instead of "sizeless".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced to OutExpr->getExprLoc() and change error message to output size should not be zero.

@yetingk yetingk changed the title [clang][CodeGen] Don't crash on sizeless output. [clang][CodeGen] Don't crash on output whose size is zero. Jul 23, 2024
This fixes issue llvm#63878 caused by creating an integer with zero
bitwidth.
@yetingk
Copy link
Contributor Author

yetingk commented Jul 29, 2024

Rebase and ping.

Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yetingk yetingk merged commit 3fcc4f2 into llvm:main Jul 30, 2024
7 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 30, 2024

LLVM Buildbot has detected a new failure on builder openmp-offload-libc-amdgpu-runtime running on omp-vega20-1 while building clang at step 6 "test-openmp".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/73/builds/2870

Here is the relevant piece of the build log for the reference:

Step 6 (test-openmp) failure: test (failure)
******************** TEST 'libomp :: tasking/issue-94260-2.c' FAILED ********************
Exit Code: -11

Command Output (stdout):
--
# RUN: at line 1
/home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/clang -fopenmp   -I /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src -I /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/openmp/runtime/test -L /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src  -fno-omit-frame-pointer -I /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/openmp/runtime/test/ompt /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/openmp/runtime/test/tasking/issue-94260-2.c -o /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/test/tasking/Output/issue-94260-2.c.tmp -lm -latomic && /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/test/tasking/Output/issue-94260-2.c.tmp
# executed command: /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/./bin/clang -fopenmp -I /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src -I /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/openmp/runtime/test -L /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/src -fno-omit-frame-pointer -I /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/openmp/runtime/test/ompt /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.src/openmp/runtime/test/tasking/issue-94260-2.c -o /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/test/tasking/Output/issue-94260-2.c.tmp -lm -latomic
# executed command: /home/ompworker/bbot/openmp-offload-libc-amdgpu-runtime/llvm.build/runtimes/runtimes-bins/openmp/runtime/test/tasking/Output/issue-94260-2.c.tmp
# note: command had no output on stdout or stderr
# error: command failed with exit status: -11

--

********************


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants