Skip to content

Explicitly closing STDOUT at end of script on Windows fails always #46

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

Closed
qikdauie opened this issue Dec 2, 2022 · 7 comments
Closed

Comments

@qikdauie
Copy link

qikdauie commented Dec 2, 2022

Hello everyone. I am currently trying to build OpenSSL on Windows. Technically Im trying to build BoringSSL+OQS (open quantum safe).
Anyways, these projects contain a python script called generate_build_files.py, which, in-turn, calls out to Perl to write ASM files for different platforms.

When Perl goes to explicitly close the file descriptor at the end of the script, it fails and reports:
error closing STDOUT: Bad file descriptor at src\crypto\fipsmodule\aes\asm\aesp8-ppc.pl line 3807.

Line 3807 of aesp8-ppc.pl is:
close STDOUT or die "error closing STDOUT: $!";

Which is best practice in Perl, in order to catch errors from closing a FD, rather than having the shutdown of the process take care of it implicitly.

STDOUT is not being closed multiple times. The script is simply closing it once at the end of the routine. This seems like an issue with Perl on Windows, where Perl is not waiting for the descriptor to be fully flushed and closed.

I have tried multiple versions of Strawberry Perl in hopes that this is just a fluke; however, 5.32, 5.30, & 5.24 contain this issue as well. I did not continue testing other versions of Strawberry Perl because I found others reporting this same issue on earlier versions here:

https://www.nntp.perl.org/group/perl.perl5.porters/2016/05/msg236739.html
https://groups.google.com/a/chromium.org/g/boringssl-dev/c/X3DA59J1GM4

Any advice/help would be greatly appreciated <3. Thank you in advance.

@shawnlaffan
Copy link
Contributor

I don't think this is a strawberry perl issue per se. The script is opening its own file handle called STDOUT, which overrides or conflicts with the existing STDOUT, and which then causes issues when the close is called.

I assume this is the full script? https://github.com/dot-asm/cryptogams/blob/master/ppc/aesp8-ppc.pl

Changing the script to use a lexical file handle and printing to that might fix things. (A general update to use lexical variables, strict and warnings would also be useful).

This should only need changes at four locations to use a lexical file handle, print to that file handle and then close it.

#L67
open my $stdout, "| $^X $xlate $flavour ".shift || die "can't call $xlate: $!";
# L3777
print {$stdout} ".byte\t",join(',',map (sprintf("0x%02x",$_),@bytes)),"\n";
#L3798
print {$stdout} print $_,"\n";
#L3801
close $stdout;

If you want to ensure all prints are flushed immediately then you can set local $|=1 near the top of the script before any print statements.

@sisyphus
Copy link

sisyphus commented Dec 2, 2022

This is still an open Issue on github for anyone interested:
Perl/perl5#4106

Cheers,
Rob

@qikdauie
Copy link
Author

qikdauie commented Dec 2, 2022

Hey @shawnlaffan, thanks for such a quick response. Yes, this is the script in question.
Oh, I didn't realize that the STDOUT in this script was not the canonical STDOUT...
I've never really used Perl before, so let me digest what you're suggesting and make these changes. Again, thank you for your assistance.

@shawnlaffan
Copy link
Contributor

Thanks @sisyphus

@qikdauie - Some other approaches are given at https://www.perl.com/article/45/2013/10/27/How-to-redirect-and-restore-STDOUT/

@qikdauie
Copy link
Author

qikdauie commented Dec 3, 2022

Okay I just made the changes to aesp8-ppc.pl that you suggested @shawnlaffan. The script was able to complete after the changes. One other script failed afterwards, boringssl/src/crypto/fipsmodules/modes/asm/ghashp8-pcc.pl. I applied the same changes to this script, which then passed through as well.
Both of these scripts utilize xlate pipes.
So, is this pointing towards an issue with Perl + xlate pipe + typeglob filehandles?

@shawnlaffan
Copy link
Contributor

shawnlaffan commented Dec 3, 2022

@qikdauie - good to hear you have it working now.

The underlying issue is a perl one, as noted by @sisyphus in #46 (comment) , although it is not specific to Strawberry perl.

However it is easily avoided by using lexical file handles directly rather than making changes to global file handles such as STDOUT. Lexical file handles have been available since Perl version 5.6, released more than 20 years ago. Ideally the scripts would be updated in their repo.

@shawnlaffan
Copy link
Contributor

Issue resolved, so closing.

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

No branches or pull requests

3 participants