Skip to content

Commit a62dd71

Browse files
committed
added envvars source files
1 parent 4f082c7 commit a62dd71

6 files changed

Lines changed: 114 additions & 90 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ set (OPENCL_ICD_LOADER_SOURCES
4646
if (WIN32)
4747
list (APPEND OPENCL_ICD_LOADER_SOURCES
4848
loader/windows/icd_windows.c
49+
loader/windows/icd_windows_envvars.c
4950
loader/windows/icd_windows_hkr.c
5051
loader/windows/OpenCL.def
5152
loader/windows/OpenCL.rc)
@@ -58,6 +59,7 @@ if (WIN32)
5859
else ()
5960
list (APPEND OPENCL_ICD_LOADER_SOURCES
6061
loader/linux/icd_linux.c
62+
loader/linux/icd_linux_envvars.c
6163
loader/linux/icd_exports.map)
6264
endif ()
6365

loader/icd.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#ifndef _ICD_H_
2020
#define _ICD_H_
2121

22-
#include "icd_cmake_config.h"
23-
2422
#ifndef CL_USE_DEPRECATED_OPENCL_1_0_APIS
2523
#define CL_USE_DEPRECATED_OPENCL_1_0_APIS
2624
#endif

loader/icd_envvars.h

Lines changed: 3 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -19,88 +19,8 @@
1919
#ifndef _ICD_ENVVARS_H_
2020
#define _ICD_ENVVARS_H_
2121

22-
// Environment variables
23-
#if defined(__linux__) || defined(__APPLE__)
22+
char *khrIcd_getenv(const char *name);
23+
char *khrIcd_secure_getenv(const char *name);
24+
void khrIcd_free_getenv(char *val);
2425

25-
static inline char *khrIcd_getenv(const char *name) {
26-
// No allocation of memory necessary for Linux.
27-
return getenv(name);
28-
}
29-
30-
static inline char *khrIcd_secure_getenv(const char *name) {
31-
#if defined(__APPLE__)
32-
// Apple does not appear to have a secure getenv implementation.
33-
// The main difference between secure getenv and getenv is that secure getenv
34-
// returns NULL if the process is being run with elevated privileges by a normal user.
35-
// The idea is to prevent the reading of malicious environment variables by a process
36-
// that can do damage.
37-
// This algorithm is derived from glibc code that sets an internal
38-
// variable (__libc_enable_secure) if the process is running under setuid or setgid.
39-
return geteuid() != getuid() || getegid() != getgid() ? NULL : khrIcd_getenv(name);
40-
#else
41-
// Linux
42-
#ifdef HAVE_SECURE_GETENV
43-
return secure_getenv(name);
44-
#elif defined(HAVE___SECURE_GETENV)
45-
return __secure_getenv(name);
46-
#else
47-
#pragma message( \
48-
"Warning: Falling back to non-secure getenv for environmental lookups! Consider" \
49-
" updating to a different libc.")
50-
return khrIcd_getenv(name);
51-
#endif
5226
#endif
53-
}
54-
55-
static inline void khrIcd_free_getenv(char *val) {
56-
// No freeing of memory necessary for Linux, but we should at least touch
57-
// val to get rid of compiler warnings.
58-
(void)val;
59-
}
60-
61-
#elif defined(WIN32)
62-
63-
static inline char *khrIcd_getenv(const char *name) {
64-
char *retVal;
65-
DWORD valSize;
66-
67-
valSize = GetEnvironmentVariableA(name, NULL, 0);
68-
69-
// valSize DOES include the null terminator, so for any set variable
70-
// will always be at least 1. If it's 0, the variable wasn't set.
71-
if (valSize == 0) return NULL;
72-
73-
// Allocate the space necessary for the registry entry
74-
retVal = (char *)malloc(valSize);
75-
76-
if (NULL != retVal) {
77-
GetEnvironmentVariableA(name, retVal, valSize);
78-
}
79-
80-
return retVal;
81-
}
82-
83-
static inline char *khrIcd_secure_getenv(const char *name) {
84-
// No secure version for Windows as far as I know
85-
return khrIcd_getenv(name);
86-
}
87-
88-
static inline void khrIcd_free_getenv(char *val) {
89-
free((void *)val);
90-
}
91-
92-
#else
93-
94-
static inline char *khrIcd_getenv(const char *name) {
95-
// stub func
96-
(void)name;
97-
return NULL;
98-
}
99-
static inline void khrIcd_free_getenv(char *val) {
100-
// stub func
101-
(void)val;
102-
}
103-
104-
#endif
105-
106-
#endif

loader/linux/icd_linux.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
1717
*/
1818

19-
// for secure_getenv():
20-
#ifndef _GNU_SOURCE
21-
#define _GNU_SOURCE
22-
#endif
23-
2419
#include "icd.h"
2520
#include "icd_envvars.h"
2621

loader/linux/icd_linux_envvars.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2016-2019 The Khronos Group Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
17+
*/
18+
19+
// for secure_getenv():
20+
#ifndef _GNU_SOURCE
21+
#define _GNU_SOURCE
22+
#endif
23+
24+
#include "icd_cmake_config.h"
25+
26+
#include <stdlib.h>
27+
28+
char *khrIcd_getenv(const char *name) {
29+
// No allocation of memory necessary for Linux.
30+
return getenv(name);
31+
}
32+
33+
char *khrIcd_secure_getenv(const char *name) {
34+
#if defined(__APPLE__)
35+
// Apple does not appear to have a secure getenv implementation.
36+
// The main difference between secure getenv and getenv is that secure getenv
37+
// returns NULL if the process is being run with elevated privileges by a normal user.
38+
// The idea is to prevent the reading of malicious environment variables by a process
39+
// that can do damage.
40+
// This algorithm is derived from glibc code that sets an internal
41+
// variable (__libc_enable_secure) if the process is running under setuid or setgid.
42+
return geteuid() != getuid() || getegid() != getgid() ? NULL : khrIcd_getenv(name);
43+
#else
44+
// Linux
45+
#ifdef HAVE_SECURE_GETENV
46+
return secure_getenv(name);
47+
#elif defined(HAVE___SECURE_GETENV)
48+
return __secure_getenv(name);
49+
#else
50+
#pragma message( \
51+
"Warning: Falling back to non-secure getenv for environmental lookups! Consider" \
52+
" updating to a different libc.")
53+
return khrIcd_getenv(name);
54+
#endif
55+
#endif
56+
}
57+
58+
void khrIcd_free_getenv(char *val) {
59+
// No freeing of memory necessary for Linux, but we should at least touch
60+
// val to get rid of compiler warnings.
61+
(void)val;
62+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2016-2019 The Khronos Group Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
17+
*/
18+
19+
#include <Windows.h>
20+
21+
char *khrIcd_getenv(const char *name) {
22+
char *retVal;
23+
DWORD valSize;
24+
25+
valSize = GetEnvironmentVariableA(name, NULL, 0);
26+
27+
// valSize DOES include the null terminator, so for any set variable
28+
// will always be at least 1. If it's 0, the variable wasn't set.
29+
if (valSize == 0) return NULL;
30+
31+
// Allocate the space necessary for the registry entry
32+
retVal = (char *)malloc(valSize);
33+
34+
if (NULL != retVal) {
35+
GetEnvironmentVariableA(name, retVal, valSize);
36+
}
37+
38+
return retVal;
39+
}
40+
41+
char *khrIcd_secure_getenv(const char *name) {
42+
return khrIcd_getenv(name);
43+
}
44+
45+
void khrIcd_free_getenv(char *val) {
46+
free((void *)val);
47+
}

0 commit comments

Comments
 (0)