[Clang] Assertion failure in clang::StringLiteral::getString() const when passing wide string literal to __builtin_nanf
Description: Hi LLVM team,
I recently encountered an assertion failure in Clang (assertions trunk) when evaluating __builtin_nanf with a wide string literal argument. It seems that while emitting the expected diagnostic warning/error for pointer incompatibility, the constant evaluator still attempts to extract the string content, leading to a crash.
Thank you very much for your time and for maintaining this amazing project! Please let me know if there is any additional information I can provide.
Steps to Reproduce: Source Code (crash.c):
char hello = __builtin_nanf(L"");
Compiler Version: x86-64 clang (assertions trunk)
Command Line:
Bash clang -c crash.c (Note: This can be consistently reproduced on Compiler Explorer with the assertions trunk).
Actual Behavior: Clang successfully emits the diagnostic regarding incompatible pointer types but immediately crashes with an assertion failure in Expr.h. The evaluator (FloatExprEvaluator::VisitCallExpr) seems to assume a standard string literal and calls getString(), which crashes on the wide string L"".
<source>:1:29: error: incompatible pointer types passing 'int[1]' to parameter of type 'const char *'
1 | char hello = __builtin_nanf(L"");
| ^~~
clang: /root/llvm-project/llvm/tools/clang/include/clang/AST/Expr.h:1874: llvm::StringRef clang::StringLiteral::getString() const: Assertion `...' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang ...
1. <source>:1:33: current parser token ';'
...
#11 0x000000000800c6f0 (anonymous namespace)::FloatExprEvaluator::VisitCallExpr(clang::CallExpr const*)
#12 0x000000000800d3d0 clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::FloatExprEvaluator, bool, ...
#13 0x000000000800f1b8 EvaluateFloat(clang::Expr const*, llvm::APFloat&, (anonymous namespace)::EvalInfo&)
#14 0x000000000802484a Evaluate(clang::APValue&, (anonymous namespace)::EvalInfo&, clang::Expr const*)
#15 0x00000000080253f2 EvaluateAsRValue((anonymous namespace)::EvalInfo&, clang::Expr const*, clang::APValue&)
#16 0x000000000802bc2e clang::Expr::EvaluateAsRValue(clang::Expr::EvalResult&, clang::ASTContext const&, bool) const
#17 0x000000000802bee9 clang::Expr::EvaluateAsFloat(llvm::APFloat&, clang::ASTContext const&) const
#18 0x0000000006eb9df3 clang::Sema::CheckImplicitConversion(clang::Expr*, clang::QualType, clang::SourceLocation, bool*, bool)
#19 0x0000000006ebdf97 AnalyzeImplicitConversions(clang::Sema&, clang::Expr*, clang::SourceLocation)
#20 0x0000000006ec02f5 clang::Sema::CheckCompletedExpr(clang::Expr*, clang::SourceLocation, bool)
Expected Behavior: Clang should gracefully reject the code by emitting the incompatible pointer type error (and potentially a warning about the invalid argument to __builtin_nanf), but it should not crash during the semantic analysis/constant evaluation phase.
A simple reproducer https://godbolt.org/z/d7986E4od
@llvm/issue-subscribers-clang-frontend
Author: JhonJ-J
I recently encountered an assertion failure in Clang (assertions trunk) when evaluating __builtin_nanf with a wide string literal argument. It seems that while emitting the expected diagnostic warning/error for pointer incompatibility, the constant evaluator still attempts to extract the string content, leading to a crash.
Thank you very much for your time and for maintaining this amazing project! Please let me know if there is any additional information I can provide.
Steps to Reproduce: Source Code (crash.c):
char hello = __builtin_nanf(L"");
Compiler Version: x86-64 clang (assertions trunk)
Command Line:
Bash clang -c crash.c (Note: This can be consistently reproduced on Compiler Explorer with the assertions trunk).
Actual Behavior: Clang successfully emits the diagnostic regarding incompatible pointer types but immediately crashes with an assertion failure in Expr.h. The evaluator (FloatExprEvaluator::VisitCallExpr) seems to assume a standard string literal and calls getString(), which crashes on the wide string L"".
<source>:1:29: error: incompatible pointer types passing 'int[1]' to parameter of type 'const char *'
1 | char hello = __builtin_nanf(L"");
| ^~~
clang: /root/llvm-project/llvm/tools/clang/include/clang/AST/Expr.h:1874: llvm::StringRef clang::StringLiteral::getString() const: Assertion `...' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang ...
1. <source>:1:33: current parser token ';'
...
#<!-- -->11 0x000000000800c6f0 (anonymous namespace)::FloatExprEvaluator::VisitCallExpr(clang::CallExpr const*)
#<!-- -->12 0x000000000800d3d0 clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::FloatExprEvaluator, bool, ...
#<!-- -->13 0x000000000800f1b8 EvaluateFloat(clang::Expr const*, llvm::APFloat&, (anonymous namespace)::EvalInfo&)
#<!-- -->14 0x000000000802484a Evaluate(clang::APValue&, (anonymous namespace)::EvalInfo&, clang::Expr const*)
#<!-- -->15 0x00000000080253f2 EvaluateAsRValue((anonymous namespace)::EvalInfo&, clang::Expr const*, clang::APValue&)
#<!-- -->16 0x000000000802bc2e clang::Expr::EvaluateAsRValue(clang::Expr::EvalResult&, clang::ASTContext const&, bool) const
#<!-- -->17 0x000000000802bee9 clang::Expr::EvaluateAsFloat(llvm::APFloat&, clang::ASTContext const&) const
#<!-- -->18 0x0000000006eb9df3 clang::Sema::CheckImplicitConversion(clang::Expr*, clang::QualType, clang::SourceLocation, bool*, bool)
#<!-- -->19 0x0000000006ebdf97 AnalyzeImplicitConversions(clang::Sema&, clang::Expr*, clang::SourceLocation)
#<!-- -->20 0x0000000006ec02f5 clang::Sema::CheckCompletedExpr(clang::Expr*, clang::SourceLocation, bool)
Expected Behavior: Clang should gracefully reject the code by emitting the incompatible pointer type error (and potentially a warning about the invalid argument to __builtin_nanf), but it should not crash during the semantic analysis/constant evaluation phase.
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
- Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
- Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.
@llvm/issue-subscribers-good-first-issue
Author: JhonJ-J
I recently encountered an assertion failure in Clang (assertions trunk) when evaluating __builtin_nanf with a wide string literal argument. It seems that while emitting the expected diagnostic warning/error for pointer incompatibility, the constant evaluator still attempts to extract the string content, leading to a crash.
Thank you very much for your time and for maintaining this amazing project! Please let me know if there is any additional information I can provide.
Steps to Reproduce: Source Code (crash.c):
char hello = __builtin_nanf(L"");
Compiler Version: x86-64 clang (assertions trunk)
Command Line:
Bash clang -c crash.c (Note: This can be consistently reproduced on Compiler Explorer with the assertions trunk).
Actual Behavior: Clang successfully emits the diagnostic regarding incompatible pointer types but immediately crashes with an assertion failure in Expr.h. The evaluator (FloatExprEvaluator::VisitCallExpr) seems to assume a standard string literal and calls getString(), which crashes on the wide string L"".
<source>:1:29: error: incompatible pointer types passing 'int[1]' to parameter of type 'const char *'
1 | char hello = __builtin_nanf(L"");
| ^~~
clang: /root/llvm-project/llvm/tools/clang/include/clang/AST/Expr.h:1874: llvm::StringRef clang::StringLiteral::getString() const: Assertion `...' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang ...
1. <source>:1:33: current parser token ';'
...
#<!-- -->11 0x000000000800c6f0 (anonymous namespace)::FloatExprEvaluator::VisitCallExpr(clang::CallExpr const*)
#<!-- -->12 0x000000000800d3d0 clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::FloatExprEvaluator, bool, ...
#<!-- -->13 0x000000000800f1b8 EvaluateFloat(clang::Expr const*, llvm::APFloat&, (anonymous namespace)::EvalInfo&)
#<!-- -->14 0x000000000802484a Evaluate(clang::APValue&, (anonymous namespace)::EvalInfo&, clang::Expr const*)
#<!-- -->15 0x00000000080253f2 EvaluateAsRValue((anonymous namespace)::EvalInfo&, clang::Expr const*, clang::APValue&)
#<!-- -->16 0x000000000802bc2e clang::Expr::EvaluateAsRValue(clang::Expr::EvalResult&, clang::ASTContext const&, bool) const
#<!-- -->17 0x000000000802bee9 clang::Expr::EvaluateAsFloat(llvm::APFloat&, clang::ASTContext const&) const
#<!-- -->18 0x0000000006eb9df3 clang::Sema::CheckImplicitConversion(clang::Expr*, clang::QualType, clang::SourceLocation, bool*, bool)
#<!-- -->19 0x0000000006ebdf97 AnalyzeImplicitConversions(clang::Sema&, clang::Expr*, clang::SourceLocation)
#<!-- -->20 0x0000000006ec02f5 clang::Sema::CheckCompletedExpr(clang::Expr*, clang::SourceLocation, bool)
Expected Behavior: Clang should gracefully reject the code by emitting the incompatible pointer type error (and potentially a warning about the invalid argument to __builtin_nanf), but it should not crash during the semantic analysis/constant evaluation phase.
The issue can be fixed by a quite small patch, so I added good first issue label.
Thanks a million.
这个问题可以通过一个很小的补丁解决,所以我加了标签。
good first issue
Hello, I'd like to work on this as my first contribution. I will open a PR.
Of note, the behavior of other builtins that take strings, i.e
bool x = __builtin_cpu_supports((char*)L"ssse3");
<source>:1:10: error: expression is not a string literal
1 | bool x = __builtin_cpu_supports((char*)L"ssse3");
| ^ ~~~~~~~~~~~~~~~
1 error generated.
https://godbolt.org/z/va5vKs85c
and builtins (like nanf) that have stdlib fallbacks:
int x = __builtin_strlen((const char*)L"foo");
Compiler returned: 0
https://godbolt.org/z/fs9obrx6h
@TheMixas are you still working on this or is it cool if I pick this one up? Thanks.
I want to work on this
I want to work on this. Please assign me to the task.
Please look for duplicates before filing new bug reports. Make sure you provide a godbolt link and obtain the backtrace from godbolt, it will be more complete and helpful.