Skip to content

Incorrect initialization of global arrays that are 2^32 bytes or larger #57353

@nlsandler

Description

@nlsandler

C programs with partially initialized arrays that are larger than 2^32 bytes aren't compiled correctly:

  • char arrays get initialized entirely to zero, even if they have a non-zero initializer in the original source code
  • when initializing arrays of other types, LLVM emits a .space or .zero directive with a negative byte count, which is rejected by the assembler.
char bad_char[4294967296] = {1};
char ok_char[4294967295u] = {1};

int bad_int[1073741824] = {1};
int ok_int[1073741823] = {1};

Compiling this with clang -S -arch x86_64 bad_static_arrays.c produces:

	.section	__TEXT,__text,regular,pure_instructions
	.build_version macos, 10, 15	sdk_version 10, 15
	.globl	_bad_char               ## @bad_char
.zerofill __DATA,__common,_bad_char,4294967296,4
	.section	__DATA,__data
	.globl	_ok_char                ## @ok_char
	.p2align	4
_ok_char:
	.byte	1                       ## 0x1
	.space	4294967294

	.globl	_bad_int                ## @bad_int
	.p2align	4
_bad_int:
	.long	1                       ## 0x1
	.space	4294967292
	.space	-4294967296

	.globl	_ok_int                 ## @ok_int
	.p2align	4
_ok_int:
	.long	1                       ## 0x1
	.space	4294967288


.subsections_via_symbols

That's from compiling with Clang 11.0, but it looks like the issue exists in the most recent version too: https://godbolt.org/z/eobvhbf7s

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:codegenIR generation bugs: mangling, exceptions, etc.good first issuehttps://github.com/llvm/llvm-project/contributemiscompilation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions