Bug 76594 - Build break in Windows (mingw32)
Summary: Build break in Windows (mingw32)
Status: RESOLVED FIXED
Alias: None
Product: p11-glue
Classification: Unclassified
Component: p11-kit (show other bugs)
Version: unspecified
Hardware: Other Windows (All)
: medium normal
Assignee: Stef Walter
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-25 11:50 UTC by Milan Crha
Modified: 2014-07-08 19:14 UTC (History)
1 user (show)

See Also:
i915 platform:
i915 features:


Attachments
proposed patch (920 bytes, patch)
2014-03-25 11:50 UTC, Milan Crha
Details | Splinter Review
proposed patch ][ (missing strerror_s) (569 bytes, patch)
2014-03-26 07:32 UTC, Milan Crha
Details | Splinter Review
Fix for implementation of strerror_r for WinXP (when streror_s is missing in msvcrt.dll) (901 bytes, patch)
2014-06-30 23:09 UTC, ddbug
Details | Splinter Review

Description Milan Crha 2014-03-25 11:50:43 UTC
Created attachment 96361 [details] [review]
proposed patch

There is no p11-kit-proxy.so when building p11-kit on windows (under mingw32), thus move the install-exec-hook into !OS_WIN32 branch, as is done in the attached patch. (I do not know why I cannot change the attachment's type to patch.)

There is alo a change in AM_INIT_AUTOMAKE(), a removal of the "serial-tests" argument, which is not available under mingw32, but I do not know the actual impact of this argument removal, thus feel free to ignore it (so I didn't not add it within the actual patch file).

diff -upr p11-kit-98292d6bbc.old/configure.ac p11-kit-98292d6bbc/configure.ac
--- p11-kit-98292d6bbc.old/configure.ac	2014-03-24 21:44:56 +0000
+++ p11-kit-98292d6bbc/configure.ac	2014-03-24 20:45:41 +0000
@@ -22,7 +22,7 @@ P11KIT_AGE=0
 AC_CONFIG_HEADERS([config.h])
 AC_CONFIG_MACRO_DIR([build/m4])
 AC_CONFIG_AUX_DIR([build/litter])
-AM_INIT_AUTOMAKE([1.10 foreign serial-tests])
+AM_INIT_AUTOMAKE([1.10 foreign])
 AM_SANITY_CHECK
 AM_MAINTAINER_MODE([enable])
 m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])],)
Comment 1 Milan Crha 2014-03-26 07:32:00 UTC
Created attachment 96402 [details] [review]
proposed patch ][ (missing strerror_s)

My mingw32 installation is also missing strerror_s() function, thus I updated the compat.c accordingly.

I'm also notified about missing asprintf/vasprintf functions (implicit declarations) by the gcc compiler, but that doesn't cause a build break (I'll see later how it'll work in a runtime and report back if there will be any issue/patch for the p11-kit).
Comment 2 Stef Walter 2014-06-20 11:54:12 UTC
Where is it documented that MSVC strerror() is thread-safe? Could you post the link so I can add it to the patch?
Comment 3 Stef Walter 2014-06-20 12:05:02 UTC
(In reply to comment #0)
> Created attachment 96361 [details] [review] [review]
> proposed patch
> 
> There is no p11-kit-proxy.so when building p11-kit on windows (under
> mingw32), thus move the install-exec-hook into !OS_WIN32 branch, as is done
> in the attached patch. (I do not know why I cannot change the attachment's
> type to patch.)

Thanks. I've committed and merged this patch.

> There is alo a change in AM_INIT_AUTOMAKE(), a removal of the "serial-tests"
> argument, which is not available under mingw32, but I do not know the actual
> impact of this argument removal, thus feel free to ignore it (so I didn't
> not add it within the actual patch file).

It probably just affects tests. Upstream we've started to require automake 1.12 so we can get the testing behavior we need. But if you wish to carry a patch removing automake 1.10, that's fine.
Comment 4 Milan Crha 2014-06-23 09:15:19 UTC
(In reply to comment #2)
> Where is it documented that MSVC strerror() is thread-safe? Could you post
> the link so I can add it to the patch?

It is not thread safe [1]. It is just a lame replacement for a missing function during mingw32 compilation.

[1] http://msdn.microsoft.com/en-us/library/zc53h9bh.aspx
Comment 5 Stef Walter 2014-06-23 13:51:48 UTC
(In reply to comment #4)
> (In reply to comment #2)
> > Where is it documented that MSVC strerror() is thread-safe? Could you post
> > the link so I can add it to the patch?
> 
> It is not thread safe [1]. It is just a lame replacement for a missing
> function during mingw32 compilation.
> 
> [1] http://msdn.microsoft.com/en-us/library/zc53h9bh.aspx

Hmmm, the whole point of using our own strerror() is so that we can be thread safe. Could you use strerror_s() in your patch instead?
Comment 6 Milan Crha 2014-06-23 15:23:51 UTC
I would use it if the mingw32 has it available, but it doesn't. Maybe some later version of it will, I do not know, but the version I used ~3 months ago did not have it.
Comment 7 Stef Walter 2014-06-23 15:53:30 UTC
An alternative would be to use strerror() on WIN32 and add a critical section or mutex locking around it.
Comment 8 ddbug 2014-06-23 17:17:36 UTC
Hello,
I am interested in this issue too, so pardon me jumping in...

Regarding thread safety of Microsoft's strerror - its source is available with VC++ installation, in [VS install dir]\VC\crt\src\strerror.c. 
Hope it is OK to refer to it here, for this purpose.

As can be seen from that source, it uses a static per-thread (or thread-local) buffer. This should be thread-safe, in the meaning that every thread gets its own instance of this static buffer. If a thread were preempted in some unusual way (like APC, coroutine, fiber...) this static buffer may be overwritten. But IMHO this is unlikely for a "normal" Windows application, so no additional sync is needed.

Please correct me if my observation is wrong or does not apply to MinGW threads.

Obviously strerror_s is better, as it does not use static buffer at all, but using it breaks compatibility with WinXP.

We could check for availability of strerror_s in runtime (using GetProcAddress) but this seems to me too much for a small compatibility fixup, a proper implementation of strerror_r should be done in the MinGW project.

Regards,
Pavel A.
Comment 9 Stef Walter 2014-06-24 09:22:58 UTC
(In reply to comment #8)
> Hello,
> I am interested in this issue too, so pardon me jumping in...
> 
> Regarding thread safety of Microsoft's strerror - its source is available
> with VC++ installation, in [VS install dir]\VC\crt\src\strerror.c. 
> Hope it is OK to refer to it here, for this purpose.

Is this the same strerror() that MinGW links to? If not, then we could use strerror() in MSVC cases, but not MinGW.
Comment 10 ddbug 2014-06-27 13:47:07 UTC
Stef, Milan,

My understanding (maybe wrong) is that MinGW runtime library can bind to one of Microsoft DLLs: the msvcrt.dll or a "versioned" mcvcrtNN from some VC++ version.

So it looks that strerror() for MinGW app _always_ links to either of MSVCRT libraries, and it is exactly same strerror as for VC++ apps.

If developer of application which uses p11 chooses to use a VC++ runtime, the whole issue won't exist, as strerror_s would be available.

But this does not address the original issue: support WinXP with the default MSVCRT linkage.

Regarding thread safety of MinGW, a search indeed turns over many issues - mostly specific to C++, and IIRC none of these are recent.
Anyway, if a MinGW app suffers from threading problems, strerror is probably the least of them.

Is there a way to add config option to use VC++ runtime for Windows,
or to specify a minimal target Windows version (as MinGW recommends)?
The p11 library users could select the best variant for them, and the compatibility workaround can also test this condition, and use strerror_s or... ?


Regards,
Pavel
Comment 11 ddbug 2014-06-29 17:43:11 UTC
Below is a "thread safe" implementation that behaves like strerror_s, but it is also much heavier...

-- PavelA

#include <string.h>
#include <errno.h>
#include <stdlib.h>

int __cdecl __MINGW_NOTHROW
strerror_r(int errnum, char *buf, size_t sizebuf);

int __cdecl __MINGW_NOTHROW
strerror_r(int errnum, char *buf, size_t sizebuf)
{
    int n = sys_nerr;
    const char *p;
    if (errnum < 0 || errnum >= n)
        p = sys_errlist[n];
    else
        p = sys_errlist[errnum];
    if (buf == NULL || sizebuf == 0)
        return EINVAL;
    strncpy(buf, p, sizebuf);
    buf[sizebuf-1] = 0;
    return 0;
}
Comment 12 Stef Walter 2014-06-30 10:45:23 UTC
(In reply to comment #11)
> Below is a "thread safe" implementation that behaves like strerror_s, but it
> is also much heavier...

This looks great. I'm assuming it builds on Windows? Could someone provide it in patch form, and test it for the build cases reported in this bug? Doing it this way would certainly remove all doubt about thread-safety in implementations of strerror() on Windows.
Comment 13 ddbug 2014-06-30 18:55:19 UTC
(In reply to comment #12)

Stef,
I don't have my original build environment unfortunately, this was written and tested with mingw 3.20 on Windows XP.

Regards,
P.
Comment 14 ddbug 2014-06-30 23:09:25 UTC
Created attachment 102036 [details] [review]
Fix for implementation of strerror_r for WinXP (when streror_s is missing in msvcrt.dll)

When target Windows version has strerror_s
(based on _WIN32_WINNT macro in compat.h)
implement strerror_r via existing strerror_s;
else use own code, similar to strerror_s.
Comment 15 Stef Walter 2014-07-01 06:05:34 UTC
Thanks. Pushed to git master.
Comment 16 ddbug 2014-07-01 11:15:15 UTC
Thank you a lot for your help, Stef and Milan.

--p.
Comment 17 ddbug 2014-07-08 19:14:34 UTC
Pardon for continuing this long thread. Just stumbled upon the resolution of the strerror_s issue in the other MinGW project, mingw-w64:
File mingw-w64-crt/secapi/strerror_s.c

http://sourceforge.net/p/mingw-w64/mingw-w64/ci/d710b9af4038a2c2ff7871cd6181a24a6d5c7e37/

In their CRT library, strerror_s() is always available.
They detect presence of strerror_s in runtime with GetProcAddress; if not present (on WinXP or earlier) call their own implementation - which uses... the not-thread-safe strerror(). Ahem.

-- pa


Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.