Skip to content

Conversation

@shreyas-omkar
Copy link

Fixes #3523

Problem Description

Invoking the crab1 front-end without an input source file currently results in an Internal Compiler Error (ICE) due to a segmentation fault. This was caused by two different code paths attempting to process a NULL filename pointer.

Solution

This patch fixes both crash scenarios by making the Rust front-end gracefully default to reading from standard input (stdin), aligning its behavior with other GCC front-ends.

  1. Session::handle_input_files (in gcc/rust/rust-session-manager.cc) is updated to handle the num_files == 0 case by setting the filename to "-" (stdin).
  2. grs_langhook_post_options (in gcc/rust/rust-lang.cc) is updated to check for cases where flags are present but no positional file is given, also setting the filename pointer to "-" to prevent the original crash in init_asm_output.

Testing

The fix was verified by testing the two scenarios that previously caused a crash. Both now correctly default to stdin and exit gracefully without a segfault.

1. Running ./gcc/crab1 with no arguments:
(Shows the experimental compiler warning and exits safely)
image

2. Running ./gcc/crab1 with flags but no file:
(Correctly defaults to stdin and waits for input)
image
image

Regression testing by compiling a file normally (e.g., gccrs test.rs) was also performed and passed, confirming the fix does not break existing functionality. A full make check-rust also passes locally.

@shreyas-omkar shreyas-omkar changed the title gcc: Default Rust frontend to stdin when no input file is provided gcc: Fix ICE in no input file Oct 19, 2025
@shreyas-omkar shreyas-omkar force-pushed the fix-no-input-segfault-v2 branch 4 times, most recently from 076a227 to 92edd40 Compare October 19, 2025 14:12
@shreyas-omkar shreyas-omkar marked this pull request as ready for review October 19, 2025 15:30
@shreyas-omkar shreyas-omkar force-pushed the fix-no-input-segfault-v2 branch from 92edd40 to 0e2e175 Compare October 19, 2025 18:51
@shreyas-omkar
Copy link
Author

@dkm Please review the changes.

@shreyas-omkar shreyas-omkar changed the title gcc: Fix ICE in no input file gccrs: Fix ICE in no input file Oct 19, 2025
Copy link
Member

@CohenArthur CohenArthur left a comment

Choose a reason for hiding this comment

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

The changes look okay to me but I'm not sure I understand all of them. I don't see why we have to modify grs_langhook_post_options and grs_langhook_init - could you explain how you arrived to these changes? Did you take inspiration from other frontends? Why are those particular changes?

The changes to handle_input_files seem completely fine

Comment on lines +113 to +114
if (num_in_fnames == 0)
main_input_filename = "-";
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed? Is it how the other frontends handle that case?

Copy link
Author

Choose a reason for hiding this comment

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

In grs_langhook_init, I'm covering the case when the global variable num_in_fnames is zero before main_input_filename is used.

This is like a safety check if the grs_langhook_post_options didnt catch the error.

Comment on lines +292 to +294
// check for input file
if (!*pfilename && num_in_fnames == 0)
*pfilename = "-";
Copy link
Member

Choose a reason for hiding this comment

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

Likewise here

Copy link
Author

Choose a reason for hiding this comment

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

In grs_langhook_post_options, I'm covering the case when no filename was passed in CLI and the filename pointer is null.

@shreyas-omkar
Copy link
Author

The changes to grs_langhook_post_options and grs_langhook_init ensure the Rust frontend handles the “no input file” case the same way as other GCC frontends (e.g., D and Go).

Both hooks are updated because GCC’s initialization sequence references these variables at different stages post_options handles the option-parsing phase, while init covers later setup before session creation.

If my approach isnt right, I am down for inputs.

This fixes an Internal Compiler Error (ICE) where the `crab1` frontend
would segmentation fault if run without any input source files.

gcc/rust/ChangeLog:

	* rust-lang.cc (grs_langhook_post_options): Set pfilename
	to "-" if no input file is provided and num_in_fnames is 0.
	* rust-session-manager.cc (Session::handle_input_files):
	Handle num_files == 0 case explicitly by setting filename to "-".

Signed-off-by: shreyas-omkar <shreyashegdeplus06@gmail.com>
@shreyas-omkar shreyas-omkar force-pushed the fix-no-input-segfault-v2 branch from 1f210e3 to 948e559 Compare November 1, 2025 19:17
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.

ICE: segfault when no source file is passed

2 participants