pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christian Ullrich <chris@chrullrich.net>
To: Andrew Dunstan <andrew@dunslane.net>
To: Magnus Hagander <magnus@hagander.net>
Cc: pgsql-committers <pgsql-committers@postgresql.org>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Subject: Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013
Date: Mon, 25 Apr 2016 22:52:54 +0200
Message-ID: <2c5f9472-d0cc-df61-78e3-bc48909ffb59@chrullrich.net> (raw)
In-Reply-To: <9d5c6a49-774c-44de-83fc-65c5b431f8d9@chrullrich.net>
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>
List-Unsubscribe:  <mailto:majordomo@postgresql.org?body=unsub%20pgsql-committers>

* Christian Ullrich wrote:

> * Andrew Dunstan wrote:

>> What if both are present? Is a release build prevented from loading a
>> debug dll and vice versa?
>
> Debug and release are simply two separate CRTs. If your process contains
> a module that needs the one, and another that needs the other, you will
> have both loaded at once.

pgwin32_putenv() is the gift that keeps giving.


According to its comment, it is not required that all modules exchanging 
calls with postgres.exe have to be built with the same CRT version (100, 
110, 120, etc.). Is it?

If not, the logic that remembers negative results from GetModuleHandle() 
(i.e. gives up forever on each possible CRT once it does not find it) is 
wrong even without considering the debug/release split. If we load a 
compiled extension built with a CRT we have not seen yet, _after_ the 
first call to pgwin32_putenv(), that module's CRT's view of its 
environment will be frozen because we will never attempt to update it.

If that code is in there because it has some noticeable performance 
advantage, the negative results could probably be reset in SQL LOAD, 
rather than just not remembering them anymore.


This comment is also incomplete then:

	/*
	 * Module loaded, but we did not find the function last time.
	 * We're not going to find it this time either...
	 */

This else branch is also taken if the module handle is set to 
INVALID_HANDLE_VALUE because the module was not found in a previous call.


If it can happen that a CRT DLL is unloaded before the process exits, 
and we cached the module handle while it was loaded, and later 
pgwin32_putenv() is called, that won't end well for the process. This 
might be a bit far-fetched; I have to see if I can actually make it happen.

One situation I can think of where this could occur is if an extension 
loaded with LOAD creates a COM in-proc server from a DLL built with yet 
another CRT, and when that object is released, either FreeLibrary() 
(transitively) or CoFreeUnusedLibraries() (directly) boots that CRT (if 
they do; it's possible that a CRT, once loaded, stays loaded.)


Finally: A nonzero handle returned from GetModuleHandle() is not 
something that needs to be CloseHandle()d. It is not actually a handle, 
but a pointer to the base (load) address of the module, although the 
documentation for GetModuleHandle() is careful not to admit that.

The value it is compared against to see whether we have seen the module 
before should be NULL, not 0.


It's getting a bit late for me today, but I will do the necessary 
experimentation and try to come up with a POC patch to fix whatever of 
the above I can actually prove to be real. Should anyone know for sure 
that I'm completely off track on something, better yet, everything, 
please let me know.

I should finish thinking before posting, then I would not have to reply 
to myself so often.

-- 
Christian



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



view thread (30+ messages)  latest in thread

Message-ID: <2c5f9472-d0cc-df61-78e3-bc48909ffb59@chrullrich.net>
Permalink:  ../2c5f9472-d0cc-df61-78e3-bc48909ffb59@chrullrich.net/
Also on:    postgresql.org/message-id/2c5f9472-d0cc-df61-78e3-bc48909ffb59@chrullrich.net

 · 

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, andrew@dunslane.net, magnus@hagander.net, pgsql-hackers@postgresql.org
  Subject: Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013
  In-Reply-To: <2c5f9472-d0cc-df61-78e3-bc48909ffb59@chrullrich.net>
  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>

* 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