[Mingw-w64-public] [PATCH] crt: Provide fcloseall as alias to _fcloseall

2024-03-04 Thread Martin Storsjö
MSVC does provide this function without an underscore prefix (via
the oldnames library) just like a number of other functions.

In mingw-w64 headers, we already provide a declaration of this
function, just like many others, but it was missing from the
import libraries.

Provide it as an alias, just like we do for many other similar
functions that only exist with a leading underscore in the runtime
DLLs.

Signed-off-by: Martin Storsjö 
---
 mingw-w64-crt/def-include/msvcrt-common.def.in   | 1 +
 mingw-w64-crt/lib-common/api-ms-win-crt-stdio-l1-1-0.def | 1 +
 2 files changed, 2 insertions(+)

diff --git a/mingw-w64-crt/def-include/msvcrt-common.def.in 
b/mingw-w64-crt/def-include/msvcrt-common.def.in
index ac56a6e21..9728b5ab8 100644
--- a/mingw-w64-crt/def-include/msvcrt-common.def.in
+++ b/mingw-w64-crt/def-include/msvcrt-common.def.in
@@ -35,6 +35,7 @@ ADD_UNDERSCORE(execv)
 ADD_UNDERSCORE(execve)
 ADD_UNDERSCORE(execvp)
 ADD_UNDERSCORE(execvpe)
+ADD_UNDERSCORE(fcloseall)
 ADD_UNDERSCORE(fcvt)
 ADD_UNDERSCORE(fdopen)
 ADD_UNDERSCORE(fgetchar)
diff --git a/mingw-w64-crt/lib-common/api-ms-win-crt-stdio-l1-1-0.def 
b/mingw-w64-crt/lib-common/api-ms-win-crt-stdio-l1-1-0.def
index d59859ced..cdfc2362b 100644
--- a/mingw-w64-crt/lib-common/api-ms-win-crt-stdio-l1-1-0.def
+++ b/mingw-w64-crt/lib-common/api-ms-win-crt-stdio-l1-1-0.def
@@ -39,6 +39,7 @@ _eof
 eof == _eof
 _fclose_nolock
 _fcloseall
+fcloseall == _fcloseall
 _fflush_nolock
 _fgetc_nolock
 _fgetchar
-- 
2.34.1



___
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] [PATCH] Use rand_s in mkstemp function

2024-03-04 Thread Martin Storsjö

On Mon, 4 Mar 2024, Martin Storsjö wrote:


Hi,

On Mon, 4 Mar 2024, Mateusz Mikuła wrote:


rand is not random enough and may lead to clashing temporary directories
with multiple parallel link processes as it was observed on Rust's CI.

It can be reproduced with these commands (run them all in without long 
pauses):



for n in {1..15000}; do rm -f lib/libLLVMAVRAsmParser.a && \
ar qc lib/libLLVMAVRAsmParser.a 
lib/Target/AVR/AsmParser/CMakeFiles/LLVMAVRAsmParser.dir/AVRAsmParser.cpp.obj 
&& \

ranlib.exe lib/libLLVMAVRAsmParser.a; done &

for n in {1..15000}; do rm -f lib/libLLVMSparcCodeGen.a && \
ar qc lib/libLLVMSparcCodeGen.a 
lib/Target/Sparc/CMakeFiles/LLVMSparcCodeGen.dir/*.obj && \

ranlib.exe lib/libLLVMSparcCodeGen.a; done

echo "done"
fg

Before the patch it will fail with an error: ranlib.exe: could not create 
temporary file whilst writing archive: no more archived files.


Thanks, I've run into this issue occasionally when building LLVM on msys2 as 
well, but I've failed to reproduce it when I've tried to look closer at it 
(as I've missed the issue that one needs to build two archives at the same 
time in order to trigger it).


If the issue is that the randomness clashes, shouldn't that be something 
that, as part of the contract of mkstemp, the function should retry until it 
finds a non-conflicting combination? But, thinking further, is the issue that 
two processes end up trying the same sequence of pseudo random files, which 
all then end up clashing, and mkstemp returns an error as it was unable to 
find a unique file name? I guess that's plausible. In that case, I guess this 
patch is fine (with Liu Hao's suggestion), as a way to reduce the risk of 
running into this.


Looking closer at our mkstemp implementation, we have this loop:

/*
Like OpenBSD, mkstemp() will try at least 2 ** 31 combinations before
giving up.
 */
for (i = 0; i >= 0; i++) {
for(j = index; j < len; j++) {
template_name[j] = letters[rand () % 62];
}
fd = _sopen(template_name,
_O_RDWR | _O_CREAT | _O_EXCL | _O_BINARY,
_SH_DENYNO, _S_IREAD | _S_IWRITE);
if (fd != -1) return fd;
if (fd == -1 && errno != EEXIST) return -1;
}

This should retry an absolutely insane number of times, so as long as one 
process finds a unique file name and stops iterating, the other parallel 
process should also find a unique one soon after, one would expect.


So if this fails, it looks like something is fishy here; if we have this 
clash, do we hit the "if (fd == -1 && errno != EEXIST) return -1;" case 
directly on the first iteration?


(Separately, it looks like the loop relies on undefined behaviour, signed 
wraparound, in order to exit the loop.)


// Martin

___
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] [PATCH] crt: Provide fcloseall as alias to _fcloseall

2024-03-04 Thread LIU Hao

在 2024-03-04 16:03, Martin Storsjö 写道:

MSVC does provide this function without an underscore prefix (via
the oldnames library) just like a number of other functions.

In mingw-w64 headers, we already provide a declaration of this
function, just like many others, but it was missing from the
import libraries.

Provide it as an alias, just like we do for many other similar
functions that only exist with a leading underscore in the runtime
DLLs.

Signed-off-by: Martin Storsjö 
---
  mingw-w64-crt/def-include/msvcrt-common.def.in   | 1 +
  mingw-w64-crt/lib-common/api-ms-win-crt-stdio-l1-1-0.def | 1 +
  2 files changed, 2 insertions(+)


This patch looks good to me.

--
Best regards,
LIU Hao



OpenPGP_signature.asc
Description: OpenPGP digital signature
___
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] [PATCH] crt: Provide fcloseall as alias to _fcloseall

2024-03-04 Thread Martin Storsjö

On Mon, 4 Mar 2024, LIU Hao wrote:


在 2024-03-04 16:03, Martin Storsjö 写道:

MSVC does provide this function without an underscore prefix (via
the oldnames library) just like a number of other functions.

In mingw-w64 headers, we already provide a declaration of this
function, just like many others, but it was missing from the
import libraries.

Provide it as an alias, just like we do for many other similar
functions that only exist with a leading underscore in the runtime
DLLs.

Signed-off-by: Martin Storsjö 
---
  mingw-w64-crt/def-include/msvcrt-common.def.in   | 1 +
  mingw-w64-crt/lib-common/api-ms-win-crt-stdio-l1-1-0.def | 1 +
  2 files changed, 2 insertions(+)


This patch looks good to me.


Thanks, pushed.

// Martin

___
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public