Skip to content

[object] Fix crash when input file has no contents#1166

Open
deepakshirkem wants to merge 1 commit into
qualcomm:mainfrom
deepakshirkem:fix/sysroot-script-crash-818
Open

[object] Fix crash when input file has no contents#1166
deepakshirkem wants to merge 1 commit into
qualcomm:mainfrom
deepakshirkem:fix/sysroot-script-crash-818

Conversation

@deepakshirkem
Copy link
Copy Markdown
Contributor

Problem

ELD crashes with a segfault when a reproduce tarball contains a linker script that references a file not found in the sysroot.

Testing

Added test SysrootScriptCrash that verifies ELD errors out gracefully instead of segfaulting when a linker script references an unreadable file.

Screenshot

Screenshot from 2026-05-12 18-52-19

Fixes #818

cc @quic-seaswara , @parth-07 , @quic-areg

Comment thread lib/Object/ObjectLinker.cpp Outdated
return true;
if (!Input->getSize())
if (!Input->getSize()) {
ThisConfig.raise(Diag::input_file_has_zero_size) << Input->decoratedPath();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are we emitting this diagnostic for non-existing file? If so, that is incorrect. Also with this modification, the link will fail for existing but 0-sized files. Is this an intended behavior change?

Copy link
Copy Markdown
Contributor Author

@deepakshirkem deepakshirkem May 12, 2026

Choose a reason for hiding this comment

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

Hi @parth-07, I think you are right in both cases. I will update the checks.

# Verify that ELD does not crash when a linker script references a
# file that cannot be read (e.g. not found in sysroot).
# ELD should error out gracefully instead of segfaulting.
# Fix for issue #818.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

github specific references can probably be left out.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @quic-areg, Thank You ::)). Will remove it.

@deepakshirkem deepakshirkem force-pushed the fix/sysroot-script-crash-818 branch 2 times, most recently from ea77759 to 02f9cdf Compare May 12, 2026 16:02
@deepakshirkem
Copy link
Copy Markdown
Contributor Author

Hi @parth-07, I have updated the fix. Here is the verified behavior
Screenshot from 2026-05-12 21-30-50

Hi @quic-areg , Updated the test. Please go one more time.

Comment thread lib/Object/ObjectLinker.cpp
@deepakshirkem deepakshirkem force-pushed the fix/sysroot-script-crash-818 branch from 02f9cdf to b8fb315 Compare May 13, 2026 09:59
@deepakshirkem
Copy link
Copy Markdown
Contributor Author

Hi @Parth / @quic-areg / @quic-seaswara , Updated the suggested test. Please review when get a chance and also the run pipeline.

RUN: %clang %clangopts -o %t1.o %p/Inputs/1.c -c
RUN: touch %t1.empty.o
RUN: llvm-ar rcs %t1.a %t1.o %t1.empty.o
RUN: llvm-ar rcsT %t1.thin.a %t1.o %t1.empty.o
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use the lit variables, do we know if this will work on windows ?

RUN: %clang %clangopts -o %t1.o %p/Inputs/main.c -c
RUN: %not %link %linkopts %t1.o -T %p/Inputs/script.t 2>&1 | %filecheck %s
#END_TEST
CHECK: cannot read file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

which file ?

Comment thread lib/Object/ObjectLinker.cpp
@deepakshirkem deepakshirkem force-pushed the fix/sysroot-script-crash-818 branch from b8fb315 to fdfcc03 Compare May 13, 2026 19:03
@deepakshirkem
Copy link
Copy Markdown
Contributor Author

Hi @quic-seaswara, addressed your comments.

@deepakshirkem deepakshirkem force-pushed the fix/sysroot-script-crash-818 branch from fdfcc03 to 62a09af Compare May 13, 2026 20:10
@deepakshirkem
Copy link
Copy Markdown
Contributor Author

Hi @quic-seaswara, Can you re-run the pipeline one more time.

ELD crashes with a segfault when a linker script references a file
that cannot be read (e.g. not found in sysroot during reproduce
tarball re-run). The crash occurs because readAndProcessInput()
raises the input_file_has_zero_size diagnostic but continues
processing the empty file, eventually crashing in ScriptLexer
when it tries to dereference a null MemoryArea.

This avoids a behavior change for existing 0-sized files.

Fixes qualcomm#818.

Signed-off-by: deepakshirkem <deepakshirke509@gmail.com>
@deepakshirkem deepakshirkem force-pushed the fix/sysroot-script-crash-818 branch from 62a09af to 937fa19 Compare May 13, 2026 21:29
@deepakshirkem
Copy link
Copy Markdown
Contributor Author

Hi @quic-seaswara, I am not sure why the CI failed for that test case, as it is passing locally for me. I think it might be because my branch was out of date. I have now rebased it with the latest top/main branch. Could you please run the CI one more time? Sorry

// 2. File does not exist (MemArea = null):
// fatal_cannot_read_input already raised. Return false to
// prevent crash in downstream processing.
if (Input->getMemArea())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should not reach readAndProcessInput for an invalid Input *. We resolve the input path and create the MemoryArea for the input in Input::resolvePath(). This function should report the error and return false if the file does not exist.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reproducer crashes when the link contains a script that is found in the sysroot

4 participants