Skip to content

Conversation

@osamakader
Copy link
Contributor

@osamakader osamakader commented Sep 21, 2025

Fixes #153948

@github-actions
Copy link

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.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:bytecode Issues for the clang bytecode constexpr interpreter labels Sep 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 21, 2025

@llvm/pr-subscribers-clang

Author: Osama Abdelkader (osamakader)

Changes

This fixes issue #153948 where clang crashes with assertion failure 'Array of unknown size' when evaluating strlen() on external const char[] declarations.

The issue was in evaluateStrlen() which called getNumElems() on unknown size arrays, leading to an assertion in Descriptor::getSize().

Fix: Add check for isUnknownSizeArray() before calling getNumElems() to gracefully handle unknown size arrays by returning false (indicating strlen cannot be evaluated at compile time).

Tested with the reproducer from the GitHub issue.


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

1 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Context.cpp (+5)
diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp
index cfda6e8ded760..f9bc3906beec1 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -245,6 +245,11 @@ bool Context::evaluateStrlen(State &Parent, const Expr *E, uint64_t &Result) {
     if (!FieldDesc->isPrimitiveArray())
       return false;
 
+    // Handle unknown size arrays - we can't determine the length at compile time
+    if (Ptr.isUnknownSizeArray()) {
+      return false;
+    }
+
     unsigned N = Ptr.getNumElems();
     if (Ptr.elemSize() == 1) {
       Result = strnlen(reinterpret_cast<const char *>(Ptr.getRawAddress()), N);

Copy link
Contributor

@Fznamznon Fznamznon left a comment

Choose a reason for hiding this comment

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

Hi, thank you for the fix, could you please add a test? I think a test using __builtin_strlen and a similar array would be appropriate. I think it can be similar to tests that reside in clang/test/AST/ByteCode/builtin-functions.cpp test file.

@Fznamznon Fznamznon requested a review from tbaederr September 22, 2025 09:09
@tbaederr
Copy link
Contributor

Tested with the reproducer from the GitHub issue.

Add it as a test then?

@osamakader osamakader force-pushed the fix-bytecode-unknown-size-array-crash branch 2 times, most recently from 37f712c to b8dc9fe Compare September 22, 2025 10:53
@osamakader
Copy link
Contributor Author

The test is added, thanks for the review.

@tbaederr
Copy link
Contributor

This works:

extern const char s[];
void foo(char *x)
{
    char *r;
    __builtin_strcpy(r, s);
}

@osamakader osamakader force-pushed the fix-bytecode-unknown-size-array-crash branch from 578c7df to 89a19cb Compare September 22, 2025 12:27
@tbaederr
Copy link
Contributor

The description of this issue should say "Fixes #153948" at the end so it gets properly closed.

@osamakader osamakader force-pushed the fix-bytecode-unknown-size-array-crash branch from 43f105d to 0e22b9d Compare September 22, 2025 15:25
@osamakader osamakader requested a review from tbaederr September 22, 2025 15:26
@osamakader osamakader force-pushed the fix-bytecode-unknown-size-array-crash branch from 5f8493e to 2d5d215 Compare September 22, 2025 15:30
This fixes issue llvm#153948 where clang crashes with assertion failure
'Array of unknown size' when evaluating strlen() on external const char[]
declarations.

The issue was in evaluateStrlen() which called getNumElems() on unknown
size arrays, leading to an assertion in Descriptor::getSize().

Fix: Add check for isDummy() || isUnknownSizeArray() before calling getNumElems() to
gracefully handle unknown size arrays by returning false (indicating
strlen cannot be evaluated at compile time).

Tested with the reproducer from the GitHub issue and added test case.

Signed-off-by: Osama Abdelkader <[email protected]>
@osamakader osamakader force-pushed the fix-bytecode-unknown-size-array-crash branch from 2d5d215 to f906f9b Compare September 22, 2025 15:34
@Fznamznon
Copy link
Contributor

Do you need us to merge that for you?

@osamakader
Copy link
Contributor Author

@Fznamznon yes please. I'm not able to do it myself :) Thanks.

@Fznamznon Fznamznon enabled auto-merge (squash) September 23, 2025 12:52
@Fznamznon Fznamznon merged commit 004e462 into llvm:main Sep 23, 2025
9 checks passed
@github-actions
Copy link

@osamakader 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. 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.

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. 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:bytecode Issues for the clang bytecode constexpr interpreter clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang][bytecode] Assertion `!isUnknownSizeArray() && "Array of unknown size"' failed.

4 participants