Skip to content

std.zig.system.NativeTargetInfo: look for a shebang line in /usr/bin/env, if any #12151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2022
Merged

std.zig.system.NativeTargetInfo: look for a shebang line in /usr/bin/env, if any #12151

merged 1 commit into from
Aug 18, 2022

Conversation

BratishkaErik
Copy link
Contributor

@BratishkaErik BratishkaErik commented Jul 17, 2022

UPD: See #12151 (review)

// reasonably reliable paths to check for.
return result: {
break :result abiAndDynamicLinkerFromPath("/usr/bin/env", cpu, os, ld_info_list, cross_target) catch {
// If we can't parse /usr/bin/env ELF file (for example, if coreutils was builded with --enable-single-binary and /usr/bin/env is actually a script), try /bin/bash
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For example, Gentoo's sys-apps/coreutils with USE flag multicall, or Red Hat Universal Base Image 9's coreutils-single #12156 (comment)

@andrewrk
Copy link
Member

On which system do you observe /usr/bin/env not being an ELF file? And what is it instead?

@BratishkaErik
Copy link
Contributor Author

BratishkaErik commented Jul 19, 2022

On which system do you observe /usr/bin/env not being an ELF file? And what is it instead?

#12151 (comment)

It can be a script instead:

#!/usr/bin/coreutils --coreutils-prog-shebang=env

or similar.

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

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

Thanks for the info!

Here is my serious suggestion: modify the code to look for a shebang line in /usr/bin/env. If it finds one, then instead of using /usr/bin/env as the ELF file to examine, it uses the file it references instead. In the above example, it would therefore examine /usr/bin/coreutils (doing the same logic recursively in case it finds another script).

I think that this will be more robust than checking for hard-coded /usr/bin/bash.

@BratishkaErik BratishkaErik changed the title std.zig.system.NativeTargetInfo: try to read two ELF file std.zig.system.NativeTargetInfo: look for a shebang line in /usr/bin/env, if any Jul 25, 2022
@BratishkaErik BratishkaErik requested a review from andrewrk July 25, 2022 14:43
@topolarity
Copy link
Contributor

I think you're hitting a stage1 compiler bug. This diff seems to get things working again:

index c5b9454c9..d9467fe2a 100644
--- a/lib/std/zig/system/NativeTargetInfo.zig
+++ b/lib/std/zig/system/NativeTargetInfo.zig
@@ -390,19 +390,16 @@ fn detectAbiAndDynamicLinker(

                 else => |e| return e,
             };
-            var should_close = true;
-            defer if (should_close) file.close();

             const line = file.reader().readUntilDelimiter(&buffer, '\n') catch {
-                should_close = false;
                 break :blk file;
             };
             if (mem.startsWith(u8, line, "#!")) {
                 var it = std.mem.tokenize(u8, line[2..], " ");
                 file_name = it.next() orelse return defaultAbiAndDynamicLinker(cpu, os, cross_target);
+                file.close();
                 continue;
             } else {
-                should_close = false;
                 break :blk file;
             }
         }

@BratishkaErik
Copy link
Contributor Author

I think you're hitting a stage1 compiler bug.
This diff seems to get things working again:

index c5b9454c9..d9467fe2a 100644
--- a/lib/std/zig/system/NativeTargetInfo.zig
+++ b/lib/std/zig/system/NativeTargetInfo.zig
@@ -390,19 +390,16 @@ fn detectAbiAndDynamicLinker(

                 else => |e| return e,
             };
-            var should_close = true;
-            defer if (should_close) file.close();

             const line = file.reader().readUntilDelimiter(&buffer, '\n') catch {
-                should_close = false;
                 break :blk file;
             };
             if (mem.startsWith(u8, line, "#!")) {
                 var it = std.mem.tokenize(u8, line[2..], " ");
                 file_name = it.next() orelse return defaultAbiAndDynamicLinker(cpu, os, cross_target);
+                file.close();
                 continue;
             } else {
-                should_close = false;
                 break :blk file;
             }
         }

Thank you!

@BratishkaErik BratishkaErik requested review from topolarity and Vexu July 30, 2022 22:03
Copy link
Contributor

@topolarity topolarity left a comment

Choose a reason for hiding this comment

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

@marler8997 's suggestion is a good one, assuming it doesn't affect upstream error handling

Changes look good though 👍

@BratishkaErik BratishkaErik requested review from marler8997 and Vexu and removed request for marler8997 August 15, 2022 17:54
@Vexu Vexu merged commit b97ae88 into ziglang:master Aug 18, 2022
@BratishkaErik
Copy link
Contributor Author

Thank you for your helpful reviews! :)

@BratishkaErik BratishkaErik deleted the bash-and-env branch August 18, 2022 17:25
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.

5 participants