pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christian Ullrich <chris@chrullrich.net>
To: Michael Paquier <michael.paquier@gmail.com>
To: PostgreSQL-development <pgsql-hackers@postgresql.org>
Cc: Noah Misch <noah@leadboat.com>
Cc: Andrew Dunstan <andrew@dunslane.net>
Cc: Magnus Hagander <magnus@hagander.net>
Subject: Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013
Date: Wed, 30 Nov 2016 16:24:34 +0000
Message-ID: <DB6PR0602MB29818313F0C5DCD2C7C65DDED48C0@DB6PR0602MB2981.eurprd06.prod.outlook.com> (raw)
In-Reply-To: <CAB7nPqStLLt9k2SQj_gVhRfbCYUpAUz5OFYu2yeVa_x3na2E6w@mail.gmail.com>
References: <E1atXIK-0003un-G0@gemulon.postgresql.org>
	<b0c66170-5711-20dd-2de3-408429ca0f48@chrullrich.net>
	<CABUevEzicpYAUOF9ZHkee1-c-gvokKAmr708sqta4H71bOZOWQ@mail.gmail.com>
	<34dd7956-3324-0305-288d-edc7d56fba76@chrullrich.net>
	<571E6D90.7050903@dunslane.net>
	<9d5c6a49-774c-44de-83fc-65c5b431f8d9@chrullrich.net>
	<2c5f9472-d0cc-df61-78e3-bc48909ffb59@chrullrich.net>
	<784524b2-bd50-1f5e-dad9-f8840f97753a@chrullrich.net>
	<20161116072246.GA2297058@tornado.leadboat.com>
	<DB6PR0602MB2981431251C29DBAB293A6CAD4BE0@DB6PR0602MB2981.eurprd06.prod.outlook.com>
	<CAB7nPqQctCCw+FjAVn72KkPGExEyFmR6R8rAznr6i78Gjw=HPw@mail.gmail.com>
	<f8fef181-ef6b-2b9b-7038-23849c9f6a73@chrullrich.net>
	<CAB7nPqStLLt9k2SQj_gVhRfbCYUpAUz5OFYu2yeVa_x3na2E6w@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>

* From: Michael Paquier

> With 0005 I am seeing a compilation failure: you need to have the
> declarations in the _MSC_VER block at the beginning of the routine. It

Sorry, too used to C++.

> would be nice to mention in a code comment that this what Noah has
> mentioned upthread: if a CRT loads while pgwin32_putenv() is
> executing, the newly-loaded CRT will always have the latest value.

I fixed the existing comment by removing the last sentence that is in the wrong place now, but I don't see the point in suddenly starting to explain why it is done this way and not the other.

> Based on that 0006 will need a rebase, nothing huge though.

Done, new revisions attached.

-- 
Christian



-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Attachments: download all (2 files, .tar.gz)

0005-Do-the-process-environment-update-first.patch (application/octet-stream, 3.3K) download From 21019e77e85a65b8f362426f269a748ff21baa43 Mon Sep 17 00:00:00 2001 From: Christian Ullrich <christian.ullrich@traditionsa.lu> Date: Wed, 30 Nov 2016 16:19:55 +0100 Subject: [PATCH 5/7] Do the process environment update first. This closes a potential race that can happen if a new CRT is loaded while pgwin32_putenv() is running. Per Noah Misch. --- src/port/win32env.c | 89 ++++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/src/port/win32env.c b/src/port/win32env.c index 7bccee6..60bf013 100644 --- a/src/port/win32env.c +++ b/src/port/win32env.c @@ -23,19 +23,6 @@ pgwin32_putenv(const char *envval) char *envcpy; char *cp; - /* - * Each version of MSVCRT has its own _putenv() call in the runtime - * library. - * - * mingw always uses MSVCRT.DLL, but if we are in a Visual C++ - * environment, attempt to update the environment in all MSVCRT modules - * that are currently loaded, to work properly with any third party - * libraries linked against a different MSVCRT but still relying on - * environment variables. - * - * Also separately update the system environment that gets inherited by - * subprocesses. - */ #ifdef _MSC_VER typedef int (_cdecl * PUTENVPROC) (const char *); static struct @@ -66,7 +53,51 @@ pgwin32_putenv(const char *envval) { NULL, NULL, NULL } }; int i; +#endif /* _MSC_VER */ + + /* + * Update the process environment - to make modifications visible to child + * processes. + * + * Need a copy of the string so we can modify it. + */ + envcpy = strdup(envval); + if (!envcpy) + return -1; + cp = strchr(envcpy, '='); + if (cp == NULL) + { + free(envcpy); + return -1; + } + *cp = '\0'; + cp++; + if (strlen(cp)) + { + /* + * Only call SetEnvironmentVariable() when we are adding a variable, + * not when removing it. Calling it on both crashes on at least + * certain versions of MingW. + */ + if (!SetEnvironmentVariable(envcpy, cp)) + { + free(envcpy); + return -1; + } + } + free(envcpy); + /* + * Each version of MSVCRT has its own _putenv() call in the runtime + * library. + * + * mingw always uses MSVCRT.DLL, but if we are in a Visual C++ + * environment, attempt to update the environment in all MSVCRT modules + * that are currently loaded, to work properly with any third party + * libraries linked against a different MSVCRT but still relying on + * environment variables. + */ +#ifdef _MSC_VER for (i = 0; rtmodules[i].modulename; i++) { if (rtmodules[i].putenvFunc == NULL) @@ -102,38 +133,6 @@ pgwin32_putenv(const char *envval) } #endif /* _MSC_VER */ - /* - * Update the process environment - to make modifications visible to child - * processes. - * - * Need a copy of the string so we can modify it. - */ - envcpy = strdup(envval); - if (!envcpy) - return -1; - cp = strchr(envcpy, '='); - if (cp == NULL) - { - free(envcpy); - return -1; - } - *cp = '\0'; - cp++; - if (strlen(cp)) - { - /* - * Only call SetEnvironmentVariable() when we are adding a variable, - * not when removing it. Calling it on both crashes on at least - * certain versions of MingW. - */ - if (!SetEnvironmentVariable(envcpy, cp)) - { - free(envcpy); - return -1; - } - } - free(envcpy); - /* Finally, update our "own" cache */ return _putenv(envval); } -- 2.10.2.windows.1
0006-Fix-the-unload-race-as-best-we-can.patch (application/octet-stream, 4.0K) download From a331eff9c278de51d543a2fcbb83357c838fecb5 Mon Sep 17 00:00:00 2001 From: Christian Ullrich <christian.ullrich@traditionsa.lu> Date: Wed, 30 Nov 2016 16:35:26 +0100 Subject: [PATCH 6/7] Fix the unload race as best we can. The fix is, unfortunately, to get rid of all caching. After fixing the load race, a situation was possible *) where a CRT is loaded, pgwin32_putenv() is called, and then later that CRT is unloaded again. The next call to pgwin32_putenv() would then attempt to use the cached function address and crash. *) It had been possible for untold years, but the probability was very low because the first call to pgwin32_putenv() occurs early in the process and used to finalize the list of CRTs the function will admit exist -- whatever is loaded at that point is likely to stay loaded. --- src/port/win32env.c | 84 ++++++++++++++++++++--------------------------------- 1 file changed, 32 insertions(+), 52 deletions(-) diff --git a/src/port/win32env.c b/src/port/win32env.c index 60bf013..65523f4 100644 --- a/src/port/win32env.c +++ b/src/port/win32env.c @@ -25,32 +25,27 @@ pgwin32_putenv(const char *envval) #ifdef _MSC_VER typedef int (_cdecl * PUTENVPROC) (const char *); - static struct + static const char *modulenames[] = { - char *modulename; - HMODULE hmodule; - PUTENVPROC putenvFunc; - } rtmodules[] = - { - { "msvcrt", NULL, NULL }, - { "msvcrtd", NULL, NULL }, /* Visual Studio 6.0 / mingw */ - { "msvcr70", NULL, NULL }, - { "msvcr70d", NULL, NULL }, /* Visual Studio 2002 */ - { "msvcr71", NULL, NULL }, - { "msvcr71d", NULL, NULL }, /* Visual Studio 2003 */ - { "msvcr80", NULL, NULL }, - { "msvcr80d", NULL, NULL }, /* Visual Studio 2005 */ - { "msvcr90", NULL, NULL }, - { "msvcr90d", NULL, NULL }, /* Visual Studio 2008 */ - { "msvcr100", NULL, NULL }, - { "msvcr100d", NULL, NULL }, /* Visual Studio 2010 */ - { "msvcr110", NULL, NULL }, - { "msvcr110d", NULL, NULL }, /* Visual Studio 2012 */ - { "msvcr120", NULL, NULL }, - { "msvcr120d", NULL, NULL }, /* Visual Studio 2013 */ - { "ucrtbase", NULL, NULL }, - { "ucrtbased", NULL, NULL }, /* Visual Studio 2015 and later */ - { NULL, NULL, NULL } + "msvcrt", + "msvcrtd", /* Visual Studio 6.0 / mingw */ + "msvcr70", + "msvcr70d", /* Visual Studio 2002 */ + "msvcr71", + "msvcr71d", /* Visual Studio 2003 */ + "msvcr80", + "msvcr80d", /* Visual Studio 2005 */ + "msvcr90", + "msvcr90d", /* Visual Studio 2008 */ + "msvcr100", + "msvcr100d", /* Visual Studio 2010 */ + "msvcr110", + "msvcr110d", /* Visual Studio 2012 */ + "msvcr120", + "msvcr120d", /* Visual Studio 2013 */ + "ucrtbase", + "ucrtbased", /* Visual Studio 2015 and later */ + NULL }; int i; #endif /* _MSC_VER */ @@ -98,38 +93,23 @@ pgwin32_putenv(const char *envval) * environment variables. */ #ifdef _MSC_VER - for (i = 0; rtmodules[i].modulename; i++) + /* + * Call the _putenv() function in all loaded CRTs. + * We cannot cache any information about loaded DLLs or + * export addresses in them because this can change at + * runtime. + */ + for (i = 0; modulenames[i]; i++) { - if (rtmodules[i].putenvFunc == NULL) + HMODULE hmodule = GetModuleHandle(modulenames[i]); + if (hmodule) { - if (rtmodules[i].hmodule == NULL) - { - /* Try to find this DLL */ - rtmodules[i].hmodule = GetModuleHandle(rtmodules[i].modulename); - if (rtmodules[i].hmodule == NULL) - { - continue; - } - else - { - rtmodules[i].putenvFunc = (PUTENVPROC) GetProcAddress(rtmodules[i].hmodule, "_putenv"); - if (rtmodules[i].putenvFunc == NULL) - { - continue; - } - } - } - else + PUTENVPROC putenvFunc = (PUTENVPROC) GetProcAddress(hmodule, "_putenv"); + if (putenvFunc) { - /* - * Module loaded, but we did not find the function last time. - * We're not going to find it this time either... - */ - continue; + putenvFunc(envval); } } - /* At this point, putenvFunc is set or we have exited the loop */ - rtmodules[i].putenvFunc(envval); } #endif /* _MSC_VER */ -- 2.10.2.windows.1

view thread (30+ messages)  latest in thread

Message-ID: <DB6PR0602MB29818313F0C5DCD2C7C65DDED48C0@DB6PR0602MB2981.eurprd06.prod.outlook.com>
Permalink:  ../DB6PR0602MB29818313F0C5DCD2C7C65DDED48C0@DB6PR0602MB2981.eurprd06.prod.outlook.com/
Also on:    postgresql.org/message-id/DB6PR0602MB29818313F0C5DCD2C7C65DDED48C0@DB6PR0602MB2981.eurprd06.prod.outlook.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: chris@chrullrich.net, michael.paquier@gmail.com, pgsql-hackers@postgresql.org, noah@leadboat.com, andrew@dunslane.net, magnus@hagander.net
  Subject: Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013
  In-Reply-To: <DB6PR0602MB29818313F0C5DCD2C7C65DDED48C0@DB6PR0602MB2981.eurprd06.prod.outlook.com>
  References: <E1atXIK-0003un-G0@gemulon.postgresql.org> <b0c66170-5711-20dd-2de3-408429ca0f48@chrullrich.net> <CABUevEzicpYAUOF9ZHkee1-c-gvokKAmr708sqta4H71bOZOWQ@mail.gmail.com> <34dd7956-3324-0305-288d-edc7d56fba76@chrullrich.net> <571E6D90.7050903@dunslane.net> <9d5c6a49-774c-44de-83fc-65c5b431f8d9@chrullrich.net> <2c5f9472-d0cc-df61-78e3-bc48909ffb59@chrullrich.net> <784524b2-bd50-1f5e-dad9-f8840f97753a@chrullrich.net> <20161116072246.GA2297058@tornado.leadboat.com> <DB6PR0602MB2981431251C29DBAB293A6CAD4BE0@DB6PR0602MB2981.eurprd06.prod.outlook.com> <CAB7nPqQctCCw+FjAVn72KkPGExEyFmR6R8rAznr6i78Gjw=HPw@mail.gmail.com> <f8fef181-ef6b-2b9b-7038-23849c9f6a73@chrullrich.net> <CAB7nPqStLLt9k2SQj_gVhRfbCYUpAUz5OFYu2yeVa_x3na2E6w@mail.gmail.com> <DB6PR0602MB29818313F0C5DCD2C7C65DDED48C0@DB6PR0602MB2981.eurprd06.prod.outlook.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox