llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Clang] OOM crash in InitListExpr::resizeInits when using large designated initializer index in struct array

Open JhonJ-J opened this issue 5 months ago • 6 comments

Description: Hi LLVM team,

I encountered an Out-Of-Memory (OOM) error in Clang (trunk) when compiling a static array of structs with very large designated initializer indices (e.g., [0x80000000]).

According to the crash backtrace, it appears that clang::InitListExpr::resizeInits attempts to allocate a massive ASTVector to accommodate the indices up to 0x80000000, which immediately exhausts available memory.

Thank you once again for your hard work and for maintaining this incredible project!

Steps to Reproduce: Source Code (crash.c):

struct Point { int x, y; };
void trigger_bug(void) {
    static struct Point pts[] = {
        [0x80000000] = { .x = 10, .y = 20 },
        [0x80000001] = { .x = 30, .y = 40 }
    };
}

Compiler Version: x86-64 clang (trunk)

Command Line:

clang -c crash.c

(Note: Easily reproducible on Compiler Explorer using the trunk version).

Actual Behavior: Clang terminates with an out of memory LLVM ERROR during AST construction.

LLVM ERROR: out of memory
Allocation failed
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-trunk/bin/clang ...
1.      <source>:6:6: current parser token ';'
2.      <source>:2:24: parsing function body 'trigger_bug'
3.      <source>:2:24: in compound statement ('{}')

...
#11 0x0000000003bd7e80 llvm::allocate_buffer(unsigned long, unsigned long)
#12 0x0000000000f3a3ed llvm::BumpPtrAllocatorImpl<...>::AllocateSlow(unsigned long, unsigned long, llvm::Align)
#13 0x0000000007534d2f clang::ASTVector<clang::Stmt*>::grow(clang::ASTContext const&, unsigned long)
#14 0x0000000007534dbc clang::InitListExpr::resizeInits(clang::ASTContext const&, unsigned int)
...

Expected Behavior: Instead of crashing and exhausting system memory, Clang should gracefully reject the code. If the requested array size via the designated initializer exceeds target or compiler limits, it should emit a clear diagnostic error (such as "array is too large" or "initializer list is too large").

JhonJ-J avatar Jun 24 '26 02:06 JhonJ-J

@llvm/issue-subscribers-clang-frontend

Author: JhonJ-J

Description: Hi LLVM team,

I encountered an Out-Of-Memory (OOM) error in Clang (trunk) when compiling a static array of structs with very large designated initializer indices (e.g., [0x80000000]).

According to the crash backtrace, it appears that clang::InitListExpr::resizeInits attempts to allocate a massive ASTVector to accommodate the indices up to 0x80000000, which immediately exhausts available memory.

Thank you once again for your hard work and for maintaining this incredible project!

Steps to Reproduce: Source Code (crash.c):

struct Point { int x, y; };
void trigger_bug(void) {
    static struct Point pts[] = {
        [0x80000000] = { .x = 10, .y = 20 },
        [0x80000001] = { .x = 30, .y = 40 }
    };
}

Compiler Version: x86-64 clang (trunk)

Command Line:

clang -c crash.c

(Note: Easily reproducible on Compiler Explorer using the trunk version).

Actual Behavior: Clang terminates with an out of memory LLVM ERROR during AST construction.

LLVM ERROR: out of memory
Allocation failed
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-trunk/bin/clang ...
1.      &lt;source&gt;:6:6: current parser token ';'
2.      &lt;source&gt;:2:24: parsing function body 'trigger_bug'
3.      &lt;source&gt;:2:24: in compound statement ('{}')

...
#<!-- -->11 0x0000000003bd7e80 llvm::allocate_buffer(unsigned long, unsigned long)
#<!-- -->12 0x0000000000f3a3ed llvm::BumpPtrAllocatorImpl&lt;...&gt;::AllocateSlow(unsigned long, unsigned long, llvm::Align)
#<!-- -->13 0x0000000007534d2f clang::ASTVector&lt;clang::Stmt*&gt;::grow(clang::ASTContext const&amp;, unsigned long)
#<!-- -->14 0x0000000007534dbc clang::InitListExpr::resizeInits(clang::ASTContext const&amp;, unsigned int)
...

Expected Behavior: Instead of crashing and exhausting system memory, Clang should gracefully reject the code. If the requested array size via the designated initializer exceeds target or compiler limits, it should emit a clear diagnostic error (such as "array is too large" or "initializer list is too large").

@JhonJ-J: Please provide Compiler Explorer link and highlight code and stack trace with code block syntax.

EugeneZelenko avatar Jun 24 '26 03:06 EugeneZelenko

I can work on this. I'll add a regression test for the large designated-initializer index case and diagnose oversized inferred arrays before InitListExpr::resizeInits attempts the allocation.

maxmanolov avatar Jun 24 '26 07:06 maxmanolov

: 请提供编译器资源管理器链接,并高亮代码及栈跟踪及代码块语法。

https://godbolt.org/z/7bcKP3738

JhonJ-J avatar Jun 24 '26 08:06 JhonJ-J

If the reproducer was generated by a fuzzer, please state that. Same for your other issues, if they were also generated by a fuzzer.

zwuis avatar Jun 24 '26 15:06 zwuis

If the reproducer was generated by a fuzzer, please state that. Same for your other issues, if they were also generated by a fuzzer.

As well, please always include a godbolt link, also use assertions build, and verify how far back the bug goes. if this is fuzzing we only want crashes that started in the last five releases.

Also something along the lines of this format is better: https://github.com/llvm/llvm-project/issues/205484 short and concise and no fluff.

shafik avatar Jun 24 '26 16:06 shafik