From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 35343 invoked by alias); 27 Jun 2017 12:19:39 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 34873 invoked by uid 89); 27 Jun 2017 12:19:38 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.5 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS,URIBL_RED autolearn=ham version=3.3.2 spammy= X-HELO: relay1.mentorg.com Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 27 Jun 2017 12:19:37 +0000 Received: from nat-ies.mentorg.com ([192.94.31.2] helo=SVR-IES-MBX-04.mgc.mentorg.com) by relay1.mentorg.com with esmtp id 1dPpSv-0000vE-Sf from Tom_deVries@mentor.com ; Tue, 27 Jun 2017 05:19:34 -0700 Received: from [127.0.0.1] (137.202.0.87) by SVR-IES-MBX-04.mgc.mentorg.com (139.181.222.4) with Microsoft SMTP Server (TLS) id 15.0.1263.5; Tue, 27 Jun 2017 13:19:29 +0100 Subject: Re: [PATCH, 2/4] Handle GOMP_OPENACC_NVPTX_{DISASM,SAVE_TEMPS} in libgomp nvptx plugin To: Jakub Jelinek , Joseph Myers CC: GCC Patches , Thomas Schwinge References: <2413b0f6-9cb2-243f-d805-08323a9c9a0a@mentor.com> <1de94c83-96da-f380-9964-1472f63270c9@mentor.com> <20170626152911.GK2123@tucnak> From: Tom de Vries Message-ID: Date: Tue, 27 Jun 2017 12:19:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 In-Reply-To: <20170626152911.GK2123@tucnak> Content-Type: multipart/mixed; boundary="------------2F11CB6BF49D3E98086D2A8F" X-ClientProxiedBy: svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) To SVR-IES-MBX-04.mgc.mentorg.com (139.181.222.4) X-SW-Source: 2017-06/txt/msg02016.txt.bz2 --------------2F11CB6BF49D3E98086D2A8F Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit Content-length: 835 On 06/26/2017 05:29 PM, Jakub Jelinek wrote: > On Mon, Jun 26, 2017 at 03:26:57PM +0000, Joseph Myers wrote: >> On Mon, 26 Jun 2017, Tom de Vries wrote: >> >>>> 2. Handle GOMP_OPENACC_NVPTX_{DISASM,SAVE_TEMPS} in libgomp nvptx plugin >>> >>> This patch adds handling of: >>> - GOMP_OPENACC_NVPTX_SAVE_TEMPS=[01], and >>> - GOMP_OPENACC_NVPTX_DISASM=[01] >>> >>> The filename used for dumping the module is plugin-nvptx..cubin. >> >> Are you sure this use of getenv and writing to that file is safe for >> setuid/setgid programs? I'd expect you to need to use secure_getenv as in >> plugin-hsa.c; certainly for anything that could results in writes to a >> file like that. > > Yeah, definitely it should be using secure_getenv/__secure_getenv. > And IMNSHO GOMP_DEBUG too. > Updated patch using secure_getenv.h. Thanks, - Tom --------------2F11CB6BF49D3E98086D2A8F Content-Type: text/x-patch; name="0003-Handle-GOMP_OPENACC_NVPTX_-DISASM-SAVE_TEMPS-in-libgomp-nvptx-plugin.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename*0="0003-Handle-GOMP_OPENACC_NVPTX_-DISASM-SAVE_TEMPS-in-libgomp"; filename*1="-nvptx-plugin.patch" Content-length: 4186 Handle GOMP_OPENACC_NVPTX_{DISASM,SAVE_TEMPS} in libgomp nvptx plugin 2017-06-26 Tom de Vries * plugin/plugin-nvptx.c (do_prog, debug_linkout): New function. (link_ptx): Use debug_linkout. --- libgomp/plugin/plugin-nvptx.c | 105 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/libgomp/plugin/plugin-nvptx.c b/libgomp/plugin/plugin-nvptx.c index 71630b5..7aa2b3b 100644 --- a/libgomp/plugin/plugin-nvptx.c +++ b/libgomp/plugin/plugin-nvptx.c @@ -47,6 +47,9 @@ #include #include #include +#include +#include +#include #if PLUGIN_NVPTX_DYNAMIC # include @@ -138,6 +141,8 @@ init_cuda_lib (void) # define init_cuda_lib() true #endif +#include "secure_getenv.h" + /* Convenience macros for the frequently used CUDA library call and error handling sequence as well as CUDA library calls that do the error checking themselves or don't do it at all. */ @@ -876,6 +881,104 @@ notify_var (const char *var_name, const char *env_var) GOMP_PLUGIN_debug (0, "%s: '%s'\n", var_name, env_var); } +static void +do_prog (const char *prog, const char *arg) +{ + pid_t pid = fork (); + + if (pid == -1) + { + GOMP_PLUGIN_error ("Fork failed"); + return; + } + else if (pid > 0) + { + int status; + waitpid (pid, &status, 0); + if (!WIFEXITED (status)) + GOMP_PLUGIN_error ("Running %s %s failed", prog, arg); + } + else + { + execlp (prog, prog /* argv[0] */, arg, NULL); + abort (); + } +} + +static void +debug_linkout (void *linkout, size_t linkoutsize) +{ + static int gomp_openacc_nvptx_disasm = -1; + if (gomp_openacc_nvptx_disasm == -1) + { + const char *var_name = "GOMP_OPENACC_NVPTX_DISASM"; + const char *env_var = secure_getenv (var_name); + notify_var (var_name, env_var); + gomp_openacc_nvptx_disasm + = ((env_var != NULL && env_var[0] == '1' && env_var[1] == '\0') + ? 1 : 0); + } + + static int gomp_openacc_nvptx_save_temps = -1; + if (gomp_openacc_nvptx_save_temps == -1) + { + const char *var_name = "GOMP_OPENACC_NVPTX_SAVE_TEMPS"; + const char *env_var = secure_getenv (var_name); + notify_var (var_name, env_var); + gomp_openacc_nvptx_save_temps + = ((env_var != NULL && env_var[0] == '1' && env_var[1] == '\0') + ? 1 : 0); + } + + if (gomp_openacc_nvptx_disasm == 0 + && gomp_openacc_nvptx_save_temps == 0) + return; + + const char *prefix = "plugin-nvptx."; + const char *postfix = ".cubin"; + const int len = (strlen (prefix) + + 20 /* %lld. */ + + strlen (postfix) + + 1 /* '\0'. */); + char file_name[len]; + int res = snprintf (file_name, len, "%s%lld%s", prefix, + (long long)getpid (), postfix); + assert (res < len); /* Assert there's no truncation. */ + + GOMP_PLUGIN_debug (0, "Generating %s with size %zu\n", + file_name, linkoutsize); + FILE *cubin_file = fopen (file_name, "wb"); + if (cubin_file == NULL) + { + GOMP_PLUGIN_debug (0, "Opening %s failed\n", file_name); + return; + } + + fwrite (linkout, linkoutsize, 1, cubin_file); + unsigned int write_succeeded = ferror (cubin_file) == 0; + if (!write_succeeded) + GOMP_PLUGIN_debug (0, "Writing %s failed\n", file_name); + + res = fclose (cubin_file); + if (res != 0) + GOMP_PLUGIN_debug (0, "Closing %s failed\n", file_name); + + if (!write_succeeded) + return; + + if (gomp_openacc_nvptx_disasm == 1) + { + GOMP_PLUGIN_debug (0, "Disassembling %s\n", file_name); + do_prog ("nvdisasm", file_name); + } + + if (gomp_openacc_nvptx_save_temps == 0) + { + GOMP_PLUGIN_debug (0, "Removing %s\n", file_name); + remove (file_name); + } +} + static bool link_ptx (CUmodule *module, const struct targ_ptx_obj *ptx_objs, unsigned num_objs) @@ -939,6 +1042,8 @@ link_ptx (CUmodule *module, const struct targ_ptx_obj *ptx_objs, return false; } + debug_linkout (linkout, linkoutsize); + CUDA_CALL (cuModuleLoadData, module, linkout); CUDA_CALL (cuLinkDestroy, linkstate); return true; --------------2F11CB6BF49D3E98086D2A8F--