[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2025-01-07 Thread via cfe-commits

github-actions[bot] wrote:



@amane-ame Congratulations on having your first Pull Request (PR) merged into 
the LLVM Project!

Your changes will be combined with recent changes from other authors, then 
tested by our [build bots](https://lab.llvm.org/buildbot/). If there is a 
problem with a build, you may receive a report in an email or a comment on this 
PR.

Please check whether problems have been caused by your change specifically, as 
the builds can include changes from many authors. It is not uncommon for your 
change to be included in a build that fails due to someone else's changes, or 
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail 
[here](https://llvm.org/docs/MyFirstTypoFix.html#myfirsttypofix-issues-after-landing-your-pr).

If your change does cause a problem, it may be reverted, or you can revert it 
yourself. This is a normal part of [LLVM 
development](https://llvm.org/docs/DeveloperPolicy.html#patch-reversion-policy).
 You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are 
working as expected, well done!


https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2025-01-07 Thread Aaron Ballman via cfe-commits

https://github.com/AaronBallman closed 
https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2025-01-06 Thread via cfe-commits

https://github.com/amane-ame updated 
https://github.com/llvm/llvm-project/pull/119563

From 659eda3ec76b63418f8b621b004728d9d7bf26ad Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Wed, 11 Dec 2024 22:17:51 +0800
Subject: [PATCH 01/11] [clang] Fix crashes when passing VLA to va_arg

---
 clang/lib/CodeGen/CGExprAgg.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
+  assert(Ty->isVariableArrayType());
+  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From 5937db790ff0a59ea5bf18cb008d38a4524dc7dc Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:50:13 +0800
Subject: [PATCH 02/11] [clang] Add a testcase for passing VLA to va_arg

---
 clang/test/CodeGen/varargs.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/CodeGen/varargs.c b/clang/test/CodeGen/varargs.c
index 625399b87f7ad7..b7b1b52156be37 100644
--- a/clang/test/CodeGen/varargs.c
+++ b/clang/test/CodeGen/varargs.c
@@ -20,4 +20,7 @@ void vla(int n, ...)
   __builtin_va_list ap;
   void *p;
   p = __builtin_va_arg(ap, typeof (int (*)[++n])); // CHECK: add nsw i32 
{{.*}}, 1
+  // Don't crash on some undefined behaviors.
+  p = __builtin_va_arg(ap, typeof (int [++n]));
+  p = __builtin_va_arg(ap, typeof (int [n][n]));
 }

From df9f8f61ee21b81c9cfd300d113afea9298b8067 Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:52:59 +0800
Subject: [PATCH 03/11] [clang] Move the parsing of VLA in va_arg to EmitVAArg

---
 clang/lib/CodeGen/CGCall.cpp| 2 ++
 clang/lib/CodeGen/CGExprAgg.cpp | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 3cefc9da66ddb8..4e2812c62f4357 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6121,6 +6121,8 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address 
&VAListAddr,
   VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
 : EmitVAListRef(VE->getSubExpr());
   QualType Ty = VE->getType();
+  if (Ty->isVariableArrayType())
+EmitVariablyModifiedType(Ty);
   if (VE->isMicrosoftABI())
 return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
   return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index a4111cb65c8b1c..2ad6587089f101 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,8 +2201,6 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
-  assert(Ty->isVariableArrayType());
-  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From b38c1d1ee20d3308a4120c3b95a167a936314a6b Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 15:43:35 +0800
Subject: [PATCH 04/11] [clang] Emit an undefined-behavior warning for passing
 VLA to va_arg

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 
 clang/lib/Sema/SemaExpr.cpp  | 7 +++
 clang/test/CodeGen/varargs.c | 6 +++---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0a245e2077f68f..3a352f23faa353 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10497,6 +10497,10 @@ def 
warn_second_parameter_to_va_arg_ownership_qualified : Warning<
 def warn_second_parameter_to_va_arg_never_compatible : Warning<
   "second argument to 'va_arg' is of promotable type %0; this va_arg has "
   "undefined behavior because arguments will be promoted to %1">, 
InGroup;
+def warn_second_parameter_to_va_arg_vla : Warning<
+  "second argument to 'va_arg' is of variable length array type %0; "
+  "this va_arg has undefined behavior because arguments will never "
+  "be compatible with variable length array type">, InGroup;
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp

[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2025-01-06 Thread via cfe-commits

https://github.com/amane-ame updated 
https://github.com/llvm/llvm-project/pull/119563

From 659eda3ec76b63418f8b621b004728d9d7bf26ad Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Wed, 11 Dec 2024 22:17:51 +0800
Subject: [PATCH 01/11] [clang] Fix crashes when passing VLA to va_arg

---
 clang/lib/CodeGen/CGExprAgg.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
+  assert(Ty->isVariableArrayType());
+  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From 5937db790ff0a59ea5bf18cb008d38a4524dc7dc Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:50:13 +0800
Subject: [PATCH 02/11] [clang] Add a testcase for passing VLA to va_arg

---
 clang/test/CodeGen/varargs.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/CodeGen/varargs.c b/clang/test/CodeGen/varargs.c
index 625399b87f7ad7..b7b1b52156be37 100644
--- a/clang/test/CodeGen/varargs.c
+++ b/clang/test/CodeGen/varargs.c
@@ -20,4 +20,7 @@ void vla(int n, ...)
   __builtin_va_list ap;
   void *p;
   p = __builtin_va_arg(ap, typeof (int (*)[++n])); // CHECK: add nsw i32 
{{.*}}, 1
+  // Don't crash on some undefined behaviors.
+  p = __builtin_va_arg(ap, typeof (int [++n]));
+  p = __builtin_va_arg(ap, typeof (int [n][n]));
 }

From df9f8f61ee21b81c9cfd300d113afea9298b8067 Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:52:59 +0800
Subject: [PATCH 03/11] [clang] Move the parsing of VLA in va_arg to EmitVAArg

---
 clang/lib/CodeGen/CGCall.cpp| 2 ++
 clang/lib/CodeGen/CGExprAgg.cpp | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 3cefc9da66ddb8..4e2812c62f4357 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6121,6 +6121,8 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address 
&VAListAddr,
   VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
 : EmitVAListRef(VE->getSubExpr());
   QualType Ty = VE->getType();
+  if (Ty->isVariableArrayType())
+EmitVariablyModifiedType(Ty);
   if (VE->isMicrosoftABI())
 return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
   return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index a4111cb65c8b1c..2ad6587089f101 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,8 +2201,6 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
-  assert(Ty->isVariableArrayType());
-  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From b38c1d1ee20d3308a4120c3b95a167a936314a6b Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 15:43:35 +0800
Subject: [PATCH 04/11] [clang] Emit an undefined-behavior warning for passing
 VLA to va_arg

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 
 clang/lib/Sema/SemaExpr.cpp  | 7 +++
 clang/test/CodeGen/varargs.c | 6 +++---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0a245e2077f68f..3a352f23faa353 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10497,6 +10497,10 @@ def 
warn_second_parameter_to_va_arg_ownership_qualified : Warning<
 def warn_second_parameter_to_va_arg_never_compatible : Warning<
   "second argument to 'va_arg' is of promotable type %0; this va_arg has "
   "undefined behavior because arguments will be promoted to %1">, 
InGroup;
+def warn_second_parameter_to_va_arg_vla : Warning<
+  "second argument to 'va_arg' is of variable length array type %0; "
+  "this va_arg has undefined behavior because arguments will never "
+  "be compatible with variable length array type">, InGroup;
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp

[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2025-01-06 Thread Eli Friedman via cfe-commits

https://github.com/efriedma-quic approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-31 Thread via cfe-commits

amane-ame wrote:

cc @efriedma-quic @tbaederr.
Could anyone please review this?

https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-24 Thread via cfe-commits

amane-ame wrote:

Ping.

https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-20 Thread via cfe-commits

https://github.com/amane-ame updated 
https://github.com/llvm/llvm-project/pull/119563

From 659eda3ec76b63418f8b621b004728d9d7bf26ad Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Wed, 11 Dec 2024 22:17:51 +0800
Subject: [PATCH 01/10] [clang] Fix crashes when passing VLA to va_arg

---
 clang/lib/CodeGen/CGExprAgg.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
+  assert(Ty->isVariableArrayType());
+  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From 5937db790ff0a59ea5bf18cb008d38a4524dc7dc Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:50:13 +0800
Subject: [PATCH 02/10] [clang] Add a testcase for passing VLA to va_arg

---
 clang/test/CodeGen/varargs.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/CodeGen/varargs.c b/clang/test/CodeGen/varargs.c
index 625399b87f7ad7..b7b1b52156be37 100644
--- a/clang/test/CodeGen/varargs.c
+++ b/clang/test/CodeGen/varargs.c
@@ -20,4 +20,7 @@ void vla(int n, ...)
   __builtin_va_list ap;
   void *p;
   p = __builtin_va_arg(ap, typeof (int (*)[++n])); // CHECK: add nsw i32 
{{.*}}, 1
+  // Don't crash on some undefined behaviors.
+  p = __builtin_va_arg(ap, typeof (int [++n]));
+  p = __builtin_va_arg(ap, typeof (int [n][n]));
 }

From df9f8f61ee21b81c9cfd300d113afea9298b8067 Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:52:59 +0800
Subject: [PATCH 03/10] [clang] Move the parsing of VLA in va_arg to EmitVAArg

---
 clang/lib/CodeGen/CGCall.cpp| 2 ++
 clang/lib/CodeGen/CGExprAgg.cpp | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 3cefc9da66ddb8..4e2812c62f4357 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6121,6 +6121,8 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address 
&VAListAddr,
   VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
 : EmitVAListRef(VE->getSubExpr());
   QualType Ty = VE->getType();
+  if (Ty->isVariableArrayType())
+EmitVariablyModifiedType(Ty);
   if (VE->isMicrosoftABI())
 return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
   return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index a4111cb65c8b1c..2ad6587089f101 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,8 +2201,6 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
-  assert(Ty->isVariableArrayType());
-  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From b38c1d1ee20d3308a4120c3b95a167a936314a6b Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 15:43:35 +0800
Subject: [PATCH 04/10] [clang] Emit an undefined-behavior warning for passing
 VLA to va_arg

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 
 clang/lib/Sema/SemaExpr.cpp  | 7 +++
 clang/test/CodeGen/varargs.c | 6 +++---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0a245e2077f68f..3a352f23faa353 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10497,6 +10497,10 @@ def 
warn_second_parameter_to_va_arg_ownership_qualified : Warning<
 def warn_second_parameter_to_va_arg_never_compatible : Warning<
   "second argument to 'va_arg' is of promotable type %0; this va_arg has "
   "undefined behavior because arguments will be promoted to %1">, 
InGroup;
+def warn_second_parameter_to_va_arg_vla : Warning<
+  "second argument to 'va_arg' is of variable length array type %0; "
+  "this va_arg has undefined behavior because arguments will never "
+  "be compatible with variable length array type">, InGroup;
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp

[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-17 Thread via cfe-commits

https://github.com/amane-ame edited 
https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-17 Thread via cfe-commits

https://github.com/amane-ame updated 
https://github.com/llvm/llvm-project/pull/119563

From 659eda3ec76b63418f8b621b004728d9d7bf26ad Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Wed, 11 Dec 2024 22:17:51 +0800
Subject: [PATCH 01/10] [clang] Fix crashes when passing VLA to va_arg

---
 clang/lib/CodeGen/CGExprAgg.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
+  assert(Ty->isVariableArrayType());
+  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From 5937db790ff0a59ea5bf18cb008d38a4524dc7dc Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:50:13 +0800
Subject: [PATCH 02/10] [clang] Add a testcase for passing VLA to va_arg

---
 clang/test/CodeGen/varargs.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/CodeGen/varargs.c b/clang/test/CodeGen/varargs.c
index 625399b87f7ad7..b7b1b52156be37 100644
--- a/clang/test/CodeGen/varargs.c
+++ b/clang/test/CodeGen/varargs.c
@@ -20,4 +20,7 @@ void vla(int n, ...)
   __builtin_va_list ap;
   void *p;
   p = __builtin_va_arg(ap, typeof (int (*)[++n])); // CHECK: add nsw i32 
{{.*}}, 1
+  // Don't crash on some undefined behaviors.
+  p = __builtin_va_arg(ap, typeof (int [++n]));
+  p = __builtin_va_arg(ap, typeof (int [n][n]));
 }

From df9f8f61ee21b81c9cfd300d113afea9298b8067 Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:52:59 +0800
Subject: [PATCH 03/10] [clang] Move the parsing of VLA in va_arg to EmitVAArg

---
 clang/lib/CodeGen/CGCall.cpp| 2 ++
 clang/lib/CodeGen/CGExprAgg.cpp | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 3cefc9da66ddb8..4e2812c62f4357 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6121,6 +6121,8 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address 
&VAListAddr,
   VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
 : EmitVAListRef(VE->getSubExpr());
   QualType Ty = VE->getType();
+  if (Ty->isVariableArrayType())
+EmitVariablyModifiedType(Ty);
   if (VE->isMicrosoftABI())
 return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
   return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index a4111cb65c8b1c..2ad6587089f101 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,8 +2201,6 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
-  assert(Ty->isVariableArrayType());
-  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From b38c1d1ee20d3308a4120c3b95a167a936314a6b Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 15:43:35 +0800
Subject: [PATCH 04/10] [clang] Emit an undefined-behavior warning for passing
 VLA to va_arg

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 
 clang/lib/Sema/SemaExpr.cpp  | 7 +++
 clang/test/CodeGen/varargs.c | 6 +++---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0a245e2077f68f..3a352f23faa353 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10497,6 +10497,10 @@ def 
warn_second_parameter_to_va_arg_ownership_qualified : Warning<
 def warn_second_parameter_to_va_arg_never_compatible : Warning<
   "second argument to 'va_arg' is of promotable type %0; this va_arg has "
   "undefined behavior because arguments will be promoted to %1">, 
InGroup;
+def warn_second_parameter_to_va_arg_vla : Warning<
+  "second argument to 'va_arg' is of variable length array type %0; "
+  "this va_arg has undefined behavior because arguments will never "
+  "be compatible with variable length array type">, InGroup;
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp

[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-17 Thread via cfe-commits

https://github.com/amane-ame updated 
https://github.com/llvm/llvm-project/pull/119563

From 3e2e04fbf1978d657bb6968c16f68ef7c4adfbdb Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Wed, 11 Dec 2024 22:17:51 +0800
Subject: [PATCH 01/10] [clang] Fix crashes when passing VLA to va_arg

---
 clang/lib/CodeGen/CGExprAgg.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
+  assert(Ty->isVariableArrayType());
+  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From 9cda4ee46cdcaa6e5353fa97482640ab7f8f6368 Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:50:13 +0800
Subject: [PATCH 02/10] [clang] Add a testcase for passing VLA to va_arg

---
 clang/test/CodeGen/varargs.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/CodeGen/varargs.c b/clang/test/CodeGen/varargs.c
index 625399b87f7ad7..b7b1b52156be37 100644
--- a/clang/test/CodeGen/varargs.c
+++ b/clang/test/CodeGen/varargs.c
@@ -20,4 +20,7 @@ void vla(int n, ...)
   __builtin_va_list ap;
   void *p;
   p = __builtin_va_arg(ap, typeof (int (*)[++n])); // CHECK: add nsw i32 
{{.*}}, 1
+  // Don't crash on some undefined behaviors.
+  p = __builtin_va_arg(ap, typeof (int [++n]));
+  p = __builtin_va_arg(ap, typeof (int [n][n]));
 }

From 62e0069330d03191151df8f5709160c543bb82d0 Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:52:59 +0800
Subject: [PATCH 03/10] [clang] Move the parsing of VLA in va_arg to EmitVAArg

---
 clang/lib/CodeGen/CGCall.cpp| 2 ++
 clang/lib/CodeGen/CGExprAgg.cpp | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 50b9dfbbab083a..a278d7c5ecfa36 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6121,6 +6121,8 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address 
&VAListAddr,
   VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
 : EmitVAListRef(VE->getSubExpr());
   QualType Ty = VE->getType();
+  if (Ty->isVariableArrayType())
+EmitVariablyModifiedType(Ty);
   if (VE->isMicrosoftABI())
 return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
   return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index a4111cb65c8b1c..2ad6587089f101 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,8 +2201,6 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
-  assert(Ty->isVariableArrayType());
-  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From edef80bc2a81858dd4b9ade94659e4aeb69d2e2d Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 15:43:35 +0800
Subject: [PATCH 04/10] [clang] Emit an undefined-behavior warning for passing
 VLA to va_arg

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 
 clang/lib/Sema/SemaExpr.cpp  | 7 +++
 clang/test/CodeGen/varargs.c | 6 +++---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index d67a81f8564a8e..18e350d5e41192 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10497,6 +10497,10 @@ def 
warn_second_parameter_to_va_arg_ownership_qualified : Warning<
 def warn_second_parameter_to_va_arg_never_compatible : Warning<
   "second argument to 'va_arg' is of promotable type %0; this va_arg has "
   "undefined behavior because arguments will be promoted to %1">, 
InGroup;
+def warn_second_parameter_to_va_arg_vla : Warning<
+  "second argument to 'va_arg' is of variable length array type %0; "
+  "this va_arg has undefined behavior because arguments will never "
+  "be compatible with variable length array type">, InGroup;
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp

[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-16 Thread via cfe-commits

https://github.com/amane-ame updated 
https://github.com/llvm/llvm-project/pull/119563

From 659eda3ec76b63418f8b621b004728d9d7bf26ad Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Wed, 11 Dec 2024 22:17:51 +0800
Subject: [PATCH 1/9] [clang] Fix crashes when passing VLA to va_arg

---
 clang/lib/CodeGen/CGExprAgg.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
+  assert(Ty->isVariableArrayType());
+  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From 5937db790ff0a59ea5bf18cb008d38a4524dc7dc Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:50:13 +0800
Subject: [PATCH 2/9] [clang] Add a testcase for passing VLA to va_arg

---
 clang/test/CodeGen/varargs.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/CodeGen/varargs.c b/clang/test/CodeGen/varargs.c
index 625399b87f7ad7..b7b1b52156be37 100644
--- a/clang/test/CodeGen/varargs.c
+++ b/clang/test/CodeGen/varargs.c
@@ -20,4 +20,7 @@ void vla(int n, ...)
   __builtin_va_list ap;
   void *p;
   p = __builtin_va_arg(ap, typeof (int (*)[++n])); // CHECK: add nsw i32 
{{.*}}, 1
+  // Don't crash on some undefined behaviors.
+  p = __builtin_va_arg(ap, typeof (int [++n]));
+  p = __builtin_va_arg(ap, typeof (int [n][n]));
 }

From df9f8f61ee21b81c9cfd300d113afea9298b8067 Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:52:59 +0800
Subject: [PATCH 3/9] [clang] Move the parsing of VLA in va_arg to EmitVAArg

---
 clang/lib/CodeGen/CGCall.cpp| 2 ++
 clang/lib/CodeGen/CGExprAgg.cpp | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 3cefc9da66ddb8..4e2812c62f4357 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6121,6 +6121,8 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address 
&VAListAddr,
   VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
 : EmitVAListRef(VE->getSubExpr());
   QualType Ty = VE->getType();
+  if (Ty->isVariableArrayType())
+EmitVariablyModifiedType(Ty);
   if (VE->isMicrosoftABI())
 return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
   return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index a4111cb65c8b1c..2ad6587089f101 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,8 +2201,6 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
-  assert(Ty->isVariableArrayType());
-  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From b38c1d1ee20d3308a4120c3b95a167a936314a6b Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 15:43:35 +0800
Subject: [PATCH 4/9] [clang] Emit an undefined-behavior warning for passing
 VLA to va_arg

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 
 clang/lib/Sema/SemaExpr.cpp  | 7 +++
 clang/test/CodeGen/varargs.c | 6 +++---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0a245e2077f68f..3a352f23faa353 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10497,6 +10497,10 @@ def 
warn_second_parameter_to_va_arg_ownership_qualified : Warning<
 def warn_second_parameter_to_va_arg_never_compatible : Warning<
   "second argument to 'va_arg' is of promotable type %0; this va_arg has "
   "undefined behavior because arguments will be promoted to %1">, 
InGroup;
+def warn_second_parameter_to_va_arg_vla : Warning<
+  "second argument to 'va_arg' is of variable length array type %0; "
+  "this va_arg has undefined behavior because arguments will never "
+  "be compatible with variable length array type">, InGroup;
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 15

[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-16 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff f4081711f0884ec7afe93577e118ecc89cb7b1cf 
7dcd400df3670d749902ab04485974ba843415f2 --extensions c,cpp -- 
clang/lib/CodeGen/CGCall.cpp clang/lib/CodeGen/CGExprScalar.cpp 
clang/lib/Sema/SemaExpr.cpp clang/test/CodeGen/xcore-abi.c 
clang/test/Sema/varargs.c
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index fca678e024..4c1cb82219 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -16540,9 +16540,9 @@ ExprResult Sema::BuildVAArgExpr(SourceLocation 
BuiltinLoc,
 
 if (TInfo->getType()->isArrayType()) {
   DiagRuntimeBehavior(TInfo->getTypeLoc().getBeginLoc(), E,
-  PDiag(diag::warn_second_parameter_to_va_arg_array)
-  << TInfo->getType()
-  << TInfo->getTypeLoc().getSourceRange());
+  PDiag(diag::warn_second_parameter_to_va_arg_array)
+  << TInfo->getType()
+  << TInfo->getTypeLoc().getSourceRange());
 }
 
 // Check for va_arg where arguments of the given type will be promoted

``




https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-14 Thread via cfe-commits

https://github.com/amane-ame updated 
https://github.com/llvm/llvm-project/pull/119563

From 659eda3ec76b63418f8b621b004728d9d7bf26ad Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Wed, 11 Dec 2024 22:17:51 +0800
Subject: [PATCH 1/8] [clang] Fix crashes when passing VLA to va_arg

---
 clang/lib/CodeGen/CGExprAgg.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
+  assert(Ty->isVariableArrayType());
+  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From 5937db790ff0a59ea5bf18cb008d38a4524dc7dc Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:50:13 +0800
Subject: [PATCH 2/8] [clang] Add a testcase for passing VLA to va_arg

---
 clang/test/CodeGen/varargs.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/CodeGen/varargs.c b/clang/test/CodeGen/varargs.c
index 625399b87f7ad7..b7b1b52156be37 100644
--- a/clang/test/CodeGen/varargs.c
+++ b/clang/test/CodeGen/varargs.c
@@ -20,4 +20,7 @@ void vla(int n, ...)
   __builtin_va_list ap;
   void *p;
   p = __builtin_va_arg(ap, typeof (int (*)[++n])); // CHECK: add nsw i32 
{{.*}}, 1
+  // Don't crash on some undefined behaviors.
+  p = __builtin_va_arg(ap, typeof (int [++n]));
+  p = __builtin_va_arg(ap, typeof (int [n][n]));
 }

From df9f8f61ee21b81c9cfd300d113afea9298b8067 Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:52:59 +0800
Subject: [PATCH 3/8] [clang] Move the parsing of VLA in va_arg to EmitVAArg

---
 clang/lib/CodeGen/CGCall.cpp| 2 ++
 clang/lib/CodeGen/CGExprAgg.cpp | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 3cefc9da66ddb8..4e2812c62f4357 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6121,6 +6121,8 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address 
&VAListAddr,
   VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
 : EmitVAListRef(VE->getSubExpr());
   QualType Ty = VE->getType();
+  if (Ty->isVariableArrayType())
+EmitVariablyModifiedType(Ty);
   if (VE->isMicrosoftABI())
 return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
   return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index a4111cb65c8b1c..2ad6587089f101 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,8 +2201,6 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
-  assert(Ty->isVariableArrayType());
-  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From b38c1d1ee20d3308a4120c3b95a167a936314a6b Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 15:43:35 +0800
Subject: [PATCH 4/8] [clang] Emit an undefined-behavior warning for passing
 VLA to va_arg

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 
 clang/lib/Sema/SemaExpr.cpp  | 7 +++
 clang/test/CodeGen/varargs.c | 6 +++---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0a245e2077f68f..3a352f23faa353 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10497,6 +10497,10 @@ def 
warn_second_parameter_to_va_arg_ownership_qualified : Warning<
 def warn_second_parameter_to_va_arg_never_compatible : Warning<
   "second argument to 'va_arg' is of promotable type %0; this va_arg has "
   "undefined behavior because arguments will be promoted to %1">, 
InGroup;
+def warn_second_parameter_to_va_arg_vla : Warning<
+  "second argument to 'va_arg' is of variable length array type %0; "
+  "this va_arg has undefined behavior because arguments will never "
+  "be compatible with variable length array type">, InGroup;
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 15

[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-13 Thread via cfe-commits

https://github.com/amane-ame updated 
https://github.com/llvm/llvm-project/pull/119563

From 659eda3ec76b63418f8b621b004728d9d7bf26ad Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Wed, 11 Dec 2024 22:17:51 +0800
Subject: [PATCH 1/7] [clang] Fix crashes when passing VLA to va_arg

---
 clang/lib/CodeGen/CGExprAgg.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
+  assert(Ty->isVariableArrayType());
+  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From 5937db790ff0a59ea5bf18cb008d38a4524dc7dc Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:50:13 +0800
Subject: [PATCH 2/7] [clang] Add a testcase for passing VLA to va_arg

---
 clang/test/CodeGen/varargs.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/CodeGen/varargs.c b/clang/test/CodeGen/varargs.c
index 625399b87f7ad7..b7b1b52156be37 100644
--- a/clang/test/CodeGen/varargs.c
+++ b/clang/test/CodeGen/varargs.c
@@ -20,4 +20,7 @@ void vla(int n, ...)
   __builtin_va_list ap;
   void *p;
   p = __builtin_va_arg(ap, typeof (int (*)[++n])); // CHECK: add nsw i32 
{{.*}}, 1
+  // Don't crash on some undefined behaviors.
+  p = __builtin_va_arg(ap, typeof (int [++n]));
+  p = __builtin_va_arg(ap, typeof (int [n][n]));
 }

From df9f8f61ee21b81c9cfd300d113afea9298b8067 Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:52:59 +0800
Subject: [PATCH 3/7] [clang] Move the parsing of VLA in va_arg to EmitVAArg

---
 clang/lib/CodeGen/CGCall.cpp| 2 ++
 clang/lib/CodeGen/CGExprAgg.cpp | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 3cefc9da66ddb8..4e2812c62f4357 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6121,6 +6121,8 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address 
&VAListAddr,
   VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
 : EmitVAListRef(VE->getSubExpr());
   QualType Ty = VE->getType();
+  if (Ty->isVariableArrayType())
+EmitVariablyModifiedType(Ty);
   if (VE->isMicrosoftABI())
 return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
   return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index a4111cb65c8b1c..2ad6587089f101 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,8 +2201,6 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
-  assert(Ty->isVariableArrayType());
-  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From b38c1d1ee20d3308a4120c3b95a167a936314a6b Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 15:43:35 +0800
Subject: [PATCH 4/7] [clang] Emit an undefined-behavior warning for passing
 VLA to va_arg

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 
 clang/lib/Sema/SemaExpr.cpp  | 7 +++
 clang/test/CodeGen/varargs.c | 6 +++---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0a245e2077f68f..3a352f23faa353 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10497,6 +10497,10 @@ def 
warn_second_parameter_to_va_arg_ownership_qualified : Warning<
 def warn_second_parameter_to_va_arg_never_compatible : Warning<
   "second argument to 'va_arg' is of promotable type %0; this va_arg has "
   "undefined behavior because arguments will be promoted to %1">, 
InGroup;
+def warn_second_parameter_to_va_arg_vla : Warning<
+  "second argument to 'va_arg' is of variable length array type %0; "
+  "this va_arg has undefined behavior because arguments will never "
+  "be compatible with variable length array type">, InGroup;
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 15

[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-13 Thread via cfe-commits

https://github.com/amane-ame updated 
https://github.com/llvm/llvm-project/pull/119563

From 659eda3ec76b63418f8b621b004728d9d7bf26ad Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Wed, 11 Dec 2024 22:17:51 +0800
Subject: [PATCH 1/7] [clang] Fix crashes when passing VLA to va_arg

---
 clang/lib/CodeGen/CGExprAgg.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
+  assert(Ty->isVariableArrayType());
+  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From 5937db790ff0a59ea5bf18cb008d38a4524dc7dc Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:50:13 +0800
Subject: [PATCH 2/7] [clang] Add a testcase for passing VLA to va_arg

---
 clang/test/CodeGen/varargs.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/CodeGen/varargs.c b/clang/test/CodeGen/varargs.c
index 625399b87f7ad7..b7b1b52156be37 100644
--- a/clang/test/CodeGen/varargs.c
+++ b/clang/test/CodeGen/varargs.c
@@ -20,4 +20,7 @@ void vla(int n, ...)
   __builtin_va_list ap;
   void *p;
   p = __builtin_va_arg(ap, typeof (int (*)[++n])); // CHECK: add nsw i32 
{{.*}}, 1
+  // Don't crash on some undefined behaviors.
+  p = __builtin_va_arg(ap, typeof (int [++n]));
+  p = __builtin_va_arg(ap, typeof (int [n][n]));
 }

From df9f8f61ee21b81c9cfd300d113afea9298b8067 Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:52:59 +0800
Subject: [PATCH 3/7] [clang] Move the parsing of VLA in va_arg to EmitVAArg

---
 clang/lib/CodeGen/CGCall.cpp| 2 ++
 clang/lib/CodeGen/CGExprAgg.cpp | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 3cefc9da66ddb8..4e2812c62f4357 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6121,6 +6121,8 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address 
&VAListAddr,
   VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
 : EmitVAListRef(VE->getSubExpr());
   QualType Ty = VE->getType();
+  if (Ty->isVariableArrayType())
+EmitVariablyModifiedType(Ty);
   if (VE->isMicrosoftABI())
 return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
   return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index a4111cb65c8b1c..2ad6587089f101 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,8 +2201,6 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
-  assert(Ty->isVariableArrayType());
-  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From b38c1d1ee20d3308a4120c3b95a167a936314a6b Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 15:43:35 +0800
Subject: [PATCH 4/7] [clang] Emit an undefined-behavior warning for passing
 VLA to va_arg

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 
 clang/lib/Sema/SemaExpr.cpp  | 7 +++
 clang/test/CodeGen/varargs.c | 6 +++---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0a245e2077f68f..3a352f23faa353 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10497,6 +10497,10 @@ def 
warn_second_parameter_to_va_arg_ownership_qualified : Warning<
 def warn_second_parameter_to_va_arg_never_compatible : Warning<
   "second argument to 'va_arg' is of promotable type %0; this va_arg has "
   "undefined behavior because arguments will be promoted to %1">, 
InGroup;
+def warn_second_parameter_to_va_arg_vla : Warning<
+  "second argument to 'va_arg' is of variable length array type %0; "
+  "this va_arg has undefined behavior because arguments will never "
+  "be compatible with variable length array type">, InGroup;
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 15

[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-13 Thread Eli Friedman via cfe-commits


@@ -20,4 +20,7 @@ void vla(int n, ...)
   __builtin_va_list ap;
   void *p;
   p = __builtin_va_arg(ap, typeof (int (*)[++n])); // CHECK: add nsw i32 
{{.*}}, 1
+  // Don't crash on some undefined behaviors.
+  p = __builtin_va_arg(ap, typeof (int [++n])); // expected-warning{{second 
argument to 'va_arg' is of variable length array type 'typeof(int[++n])'}}

efriedma-quic wrote:

Maybe put the warning checks into some test in clang/test/Sema.

https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-13 Thread Eli Friedman via cfe-commits


@@ -16538,6 +16538,13 @@ ExprResult Sema::BuildVAArgExpr(SourceLocation 
BuiltinLoc,
 << TInfo->getTypeLoc().getSourceRange();
 }
 
+if (TInfo->getType()->isVariableArrayType()) {

efriedma-quic wrote:

This should check for any array type; the issue with compatibility isn't 
specific to variadic arrays.

https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-13 Thread Eli Friedman via cfe-commits


@@ -6121,6 +6121,8 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address 
&VAListAddr,
   VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
 : EmitVAListRef(VE->getSubExpr());
   QualType Ty = VE->getType();
+  if (Ty->isVariableArrayType())
+EmitVariablyModifiedType(Ty);

efriedma-quic wrote:

You can drop the call to EmitVariablyModifiedType in 
ScalarExprEmitter::VisitVAArgExpr, since it's now redundant.

https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-11 Thread via cfe-commits

https://github.com/amane-ame updated 
https://github.com/llvm/llvm-project/pull/119563

From 659eda3ec76b63418f8b621b004728d9d7bf26ad Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Wed, 11 Dec 2024 22:17:51 +0800
Subject: [PATCH 1/4] [clang] Fix crashes when passing VLA to va_arg

---
 clang/lib/CodeGen/CGExprAgg.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
+  assert(Ty->isVariableArrayType());
+  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From 5937db790ff0a59ea5bf18cb008d38a4524dc7dc Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:50:13 +0800
Subject: [PATCH 2/4] [clang] Add a testcase for passing VLA to va_arg

---
 clang/test/CodeGen/varargs.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/CodeGen/varargs.c b/clang/test/CodeGen/varargs.c
index 625399b87f7ad7..b7b1b52156be37 100644
--- a/clang/test/CodeGen/varargs.c
+++ b/clang/test/CodeGen/varargs.c
@@ -20,4 +20,7 @@ void vla(int n, ...)
   __builtin_va_list ap;
   void *p;
   p = __builtin_va_arg(ap, typeof (int (*)[++n])); // CHECK: add nsw i32 
{{.*}}, 1
+  // Don't crash on some undefined behaviors.
+  p = __builtin_va_arg(ap, typeof (int [++n]));
+  p = __builtin_va_arg(ap, typeof (int [n][n]));
 }

From df9f8f61ee21b81c9cfd300d113afea9298b8067 Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:52:59 +0800
Subject: [PATCH 3/4] [clang] Move the parsing of VLA in va_arg to EmitVAArg

---
 clang/lib/CodeGen/CGCall.cpp| 2 ++
 clang/lib/CodeGen/CGExprAgg.cpp | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 3cefc9da66ddb8..4e2812c62f4357 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6121,6 +6121,8 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address 
&VAListAddr,
   VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
 : EmitVAListRef(VE->getSubExpr());
   QualType Ty = VE->getType();
+  if (Ty->isVariableArrayType())
+EmitVariablyModifiedType(Ty);
   if (VE->isMicrosoftABI())
 return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
   return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index a4111cb65c8b1c..2ad6587089f101 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,8 +2201,6 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
-  assert(Ty->isVariableArrayType());
-  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From b38c1d1ee20d3308a4120c3b95a167a936314a6b Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 15:43:35 +0800
Subject: [PATCH 4/4] [clang] Emit an undefined-behavior warning for passing
 VLA to va_arg

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 
 clang/lib/Sema/SemaExpr.cpp  | 7 +++
 clang/test/CodeGen/varargs.c | 6 +++---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0a245e2077f68f..3a352f23faa353 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10497,6 +10497,10 @@ def 
warn_second_parameter_to_va_arg_ownership_qualified : Warning<
 def warn_second_parameter_to_va_arg_never_compatible : Warning<
   "second argument to 'va_arg' is of promotable type %0; this va_arg has "
   "undefined behavior because arguments will be promoted to %1">, 
InGroup;
+def warn_second_parameter_to_va_arg_vla : Warning<
+  "second argument to 'va_arg' is of variable length array type %0; "
+  "this va_arg has undefined behavior because arguments will never "
+  "be compatible with variable length array type">, InGroup;
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 15

[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-11 Thread via cfe-commits

https://github.com/amane-ame updated 
https://github.com/llvm/llvm-project/pull/119563

From 659eda3ec76b63418f8b621b004728d9d7bf26ad Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Wed, 11 Dec 2024 22:17:51 +0800
Subject: [PATCH 1/3] [clang] Fix crashes when passing VLA to va_arg

---
 clang/lib/CodeGen/CGExprAgg.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
+  assert(Ty->isVariableArrayType());
+  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

From 5937db790ff0a59ea5bf18cb008d38a4524dc7dc Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:50:13 +0800
Subject: [PATCH 2/3] [clang] Add a testcase for passing VLA to va_arg

---
 clang/test/CodeGen/varargs.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/CodeGen/varargs.c b/clang/test/CodeGen/varargs.c
index 625399b87f7ad7..b7b1b52156be37 100644
--- a/clang/test/CodeGen/varargs.c
+++ b/clang/test/CodeGen/varargs.c
@@ -20,4 +20,7 @@ void vla(int n, ...)
   __builtin_va_list ap;
   void *p;
   p = __builtin_va_arg(ap, typeof (int (*)[++n])); // CHECK: add nsw i32 
{{.*}}, 1
+  // Don't crash on some undefined behaviors.
+  p = __builtin_va_arg(ap, typeof (int [++n]));
+  p = __builtin_va_arg(ap, typeof (int [n][n]));
 }

From df9f8f61ee21b81c9cfd300d113afea9298b8067 Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Thu, 12 Dec 2024 13:52:59 +0800
Subject: [PATCH 3/3] [clang] Move the parsing of VLA in va_arg to EmitVAArg

---
 clang/lib/CodeGen/CGCall.cpp| 2 ++
 clang/lib/CodeGen/CGExprAgg.cpp | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 3cefc9da66ddb8..4e2812c62f4357 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6121,6 +6121,8 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address 
&VAListAddr,
   VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
 : EmitVAListRef(VE->getSubExpr());
   QualType Ty = VE->getType();
+  if (Ty->isVariableArrayType())
+EmitVariablyModifiedType(Ty);
   if (VE->isMicrosoftABI())
 return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
   return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index a4111cb65c8b1c..2ad6587089f101 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,8 +2201,6 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
-  assert(Ty->isVariableArrayType());
-  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-11 Thread Eli Friedman via cfe-commits

efriedma-quic wrote:

C standard rules for va_arg: "[...] if *type* is not compatible with the type 
of the actual next argument [...], the behavior is undefined [...]".  A struct 
is never compatible with an array, so yes , it's undefined.  (See 6.2.7 for 
what constitutes a "compatible type".)

As a practical matter, breaking the "compatible type" rule can cause crashes or 
data corruption.  Modern ABIs don't just pass everything on the stack.

https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-11 Thread Robear Selwans via cfe-commits

mo7sen wrote:

> While you're here, maybe look at emitting an undefined-behavior warning for 
> this construct? A VLA is never compatible with a function argument: if you 
> try to write an array in an function type, it gets promoted to a pointer. So 
> this construct is guaranteed to produce broken results (which is why nobody 
> has tripped over this before).

Assuming the actual argument is a struct and the `va_arg(args, 
uint8_t[sizeof(struct)])` is just to get the struct bytes, would that still be 
considered undefined behaviour?

https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-11 Thread Eli Friedman via cfe-commits

efriedma-quic wrote:

Please add a testcase to clang/test/CodeGen/ .  Put it in an existing file if 
there's already some related test.  (See also 
https://llvm.org/docs/Contributing.html#how-to-submit-a-patch )

I don't think EmitAggregateCopy is the right place to call 
EmitVariablyModifiedType: we want to call EmitVariablyModifiedType exactly once 
for every VLA written in the source code.  So the call should be located 
somewhere that's tightly related to the expression itself: 
CodeGenFunction::EmitVAArg, or something like that.

While you're here, maybe look at emitting an undefined-behavior warning for 
this construct?  A VLA is never compatible with a function argument: if you try 
to write an array in an function type, it gets promoted to a pointer.  So this 
construct is guaranteed to produce broken results (which is why nobody has 
tripped over this before).

https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-11 Thread via cfe-commits

llvmbot wrote:



@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: 天音あめ (amane-ame)


Changes

Closes #119360.

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


1 Files Affected:

- (modified) clang/lib/CodeGen/CGExprAgg.cpp (+2) 


``diff
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
+  assert(Ty->isVariableArrayType());
+  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

``




https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-11 Thread via cfe-commits

github-actions[bot] wrote:



Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this 
page.

If this is not working for you, it is probably because you do not have write 
permissions for the repository. In which case you can instead tag reviewers by 
name in a comment by using `@` followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a 
review by "ping"ing the PR by adding a comment “Ping”. The common courtesy 
"ping" rate is once a week. Please remember that you are asking for valuable 
time from other developers.

If you have further questions, they may be answered by the [LLVM GitHub User 
Guide](https://llvm.org/docs/GitHub.html).

You can also ask questions in a comment on this PR, on the [LLVM 
Discord](https://discord.com/invite/xS7Z362) or on the 
[forums](https://discourse.llvm.org/).

https://github.com/llvm/llvm-project/pull/119563
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix crashes when passing VLA to va_arg (PR #119563)

2024-12-11 Thread via cfe-commits

https://github.com/amane-ame created 
https://github.com/llvm/llvm-project/pull/119563

Closes #119360.

From 659eda3ec76b63418f8b621b004728d9d7bf26ad Mon Sep 17 00:00:00 2001
From: amane-ame 
Date: Wed, 11 Dec 2024 22:17:51 +0800
Subject: [PATCH] [clang] Fix crashes when passing VLA to va_arg

---
 clang/lib/CodeGen/CGExprAgg.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 2ad6587089f101..a4111cb65c8b1c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -2201,6 +2201,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, 
LValue Src, QualType Ty,
 // But note that getTypeInfo returns 0 for a VLA.
 if (auto *VAT = dyn_cast_or_null(
 getContext().getAsArrayType(Ty))) {
+  assert(Ty->isVariableArrayType());
+  EmitVariablyModifiedType(Ty);
   QualType BaseEltTy;
   SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
   TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits