Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1aunVQ-0003n5-B4 for pgsql-committers@arkaria.postgresql.org; Mon, 25 Apr 2016 20:53:20 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1aunVP-0007aV-6v for pgsql-committers@arkaria.postgresql.org; Mon, 25 Apr 2016 20:53:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1aunVB-0006v7-OQ; Mon, 25 Apr 2016 20:53:05 +0000 Received: from mail-db3on0075.outbound.protection.outlook.com ([157.55.234.75] helo=emea01-db3-obe.outbound.protection.outlook.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1aunV7-00033r-LE; Mon, 25 Apr 2016 20:53:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gloomberg.onmicrosoft.com; s=selector1-chrullrich-net; h=From:To:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=LOF1HrxZB926XjbUBb0I5IYi2+MuqvQ4FaanQX0dlso=; b=c2cMchmB0lKs+V9Cy7GtcLNmfnn7ne97/gP7eeet+6tzWi7CfxNLJ/4qHLWYEDNyUzbOAqn9Ibrxk3ta0LuYr2j8lIe/lGlQ7qY9cFahnPJOkcx6R7g5NoiP9V2/bRR5pxQMtKUim3RVhNssmp2RV+N8YFYDtUyP84IdOs8YTDk= Authentication-Results: postgresql.org; dkim=none (message not signed) header.d=none;postgresql.org; dmarc=none action=none header.from=chrullrich.net; Received: from [192.168.92.135] (217.31.70.82) by DB3PR06MB0698.eurprd06.prod.outlook.com (10.161.54.139) with Microsoft SMTP Server (TLS) id 15.1.466.19; Mon, 25 Apr 2016 20:52:58 +0000 Subject: Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013 To: Andrew Dunstan , Magnus Hagander References: <34dd7956-3324-0305-288d-edc7d56fba76@chrullrich.net> <571E6D90.7050903@dunslane.net> <9d5c6a49-774c-44de-83fc-65c5b431f8d9@chrullrich.net> CC: pgsql-committers , PostgreSQL-development From: Christian Ullrich Message-ID: <2c5f9472-d0cc-df61-78e3-bc48909ffb59@chrullrich.net> Date: Mon, 25 Apr 2016 22:52:54 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.0 MIME-Version: 1.0 In-Reply-To: <9d5c6a49-774c-44de-83fc-65c5b431f8d9@chrullrich.net> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [217.31.70.82] X-ClientProxiedBy: AM3PR07CA0020.eurprd07.prod.outlook.com (10.141.45.148) To DB3PR06MB0698.eurprd06.prod.outlook.com (10.161.54.139) X-MS-Office365-Filtering-Correlation-Id: f8c2f4b6-4655-4ec5-5b34-08d36d4b958c X-Microsoft-Exchange-Diagnostics: 1;DB3PR06MB0698;2:Xt8pKcFTTlrYWhKwHFwog7fD2OM/Ba3i1Lq+E6McFqJgkEsJPJjdKPt6alrRyIw2r0KkWaiHG0wfXuqZ+2z/392iy8jvgGxaYPdOa1dZXoaLiX9+dXfTH2ng+W5z5H5RSbdmnIhFTyyUbscreWsUKWrr9T1LCOXKIKMY4kkQA2f2xah78m7dfO+HCsYB6KOS;3:XsoMEbnmqzBsSqEN61/KF3PaNkUsT4h5vOjWTdTv0xKOWWJXMVLKNVfL4K73/G3TbGlw/G6sDJJagjYtlisFnmw8BW0ky4dFKEa6ZbOnvIGe4PVpXDxr94TH4zeV3O3Q X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:DB3PR06MB0698; X-Microsoft-Exchange-Diagnostics: 1;DB3PR06MB0698;25:rjhfUwom26XiIBaaF1besiBg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zA+s42NzJLPzh1W5timR1SJSM7oHGFCM++P8UE8kkcWv/AZq/+s8Zj9JXK688t3IPhamIBwupf2SBSSZfCBsTF4i1WXQ9LkcC91xT9SGuxN6i;4:t2c1uPmNL0/2WraPwYpRwUW47ASltHS6BOWGrS8YphfuhtWFql5k/02cKpv4N3N//lZXiUq22rEeehywZrwcxShDsoqNBK7gV59y745ETLkmc/pVFEW5OtRuRxnqar0+evB1mZ6yhWzBwFcDamfSbHwzJebrFBJZaLJPKknWpFfoSltq++ShDDjyxQzjME0PsKvzN3y3RMYOiSp39G8qsDEoHo6R9RotmfT2WhAAFCNCpBTGMSMShersuJwmNvDKBKXSqqJ6fatDYJjTKo9oje5xYHnzSYLEDHKCS7VJ/rc6KEKP4eT9+DaEkW0PgsETkF0g0/vScWNFkBYAH9Bpsc6UYSc44fyDFlZwD7SXyACdwEikWJDKhf7BqmziPrkCgEsmZVnEduPR5JnO/7pirQ== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(9101521072)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046);SRVR:DB3PR06MB0698;BCL:0;PCL:0;RULEID:;SRVR:DB3PR06MB0698; X-Forefront-PRVS: 0923977CCA X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(6049001)(24454002)(86362001)(50466002)(189998001)(50986999)(54356999)(76176999)(77096005)(31696002)(81166005)(5004730100002)(230700001)(64126003)(2950100001)(93886004)(47776003)(66066001)(65806001)(65956001)(586003)(31686004)(19580395003)(4001350100001)(92566002)(3846002)(6116002)(36756003)(5001770100001)(2906002)(5008740100001)(4326007)(23676002)(33646002)(83506001)(1096002)(42186005)(65826006)(40753002)(460985005);DIR:OUT;SFP:1101;SCL:1;SRVR:DB3PR06MB0698;H:[192.168.92.135];FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MT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/Qg2V7huyGKiSrw4iPMiDX526vsIO2s/xjCAv22sP6M0=;7:r0kqtuTw70JK77ejpWnMgAMqXs1jy/M59CWqG+KjFFtX9SAU0Ibr4IyZp/5CyinMX9FPxeeU8E7PMDsn5CrNhbG2xJ01sW5yti+jqOaATkp+ZzKuA8q+L4tacbjPTeiCbyMSJTRzBiwcYbLMsz6jsoNxdh1mavT//lsba5sYswyXlUSDM1h3zMBeGL4p0UOZfG9smQdeYrHVmqbj6HTzOrwqnqkv6QH9irFQwwnKFTQ= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: chrullrich.net X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Apr 2016 20:52:58.5850 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB3PR06MB0698 X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-committers Precedence: bulk Sender: pgsql-committers-owner@postgresql.org * 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