Commit d8f0024b by Frederik Harwath Committed by Frederik Harwath

Validate acc_device_t uses

Check that function arguments of type acc_device_t
are valid enumeration values in all publicly visible
functions from oacc-init.c.

2019-12-03  Frederik Harwath  <frederik@codesourcery.com>

	libgomp/
	* oacc-init.c (acc_known_device_type): Add function.
	(unknown_device_type_error): Add function.
 	(name_of_acc_device_t): Change to call unknown_device_type_error
	on unknown type.
	(resolve_device): Use acc_known_device_type.
 	(acc_init): Fail if acc_device_t argument is not valid.
 	(acc_shutdown): Likewise.
 	(acc_get_num_devices): Likewise.
 	(acc_set_device_type): Likewise.
	(acc_get_device_num): Likewise.
	(acc_set_device_num): Likewise.
 	(acc_on_device): Add comment that argument validity is not checked.

Reviewed-by: Thomas Schwinge <thomas@codesourcery.com>

From-SVN: r278937
parent 83caa34e
2019-12-03 Frederik Harwath <frederik@codesourcery.com>
* oacc-init.c (acc_known_device_type): Add function.
(unknown_device_type_error): Add function.
(name_of_acc_device_t): Change to call unknown_device_type_error
on unknown type.
(resolve_device): Use acc_known_device_type.
(acc_init): Fail if acc_device_t argument is not valid.
(acc_shutdown): Likewise.
(acc_get_num_devices): Likewise.
(acc_set_device_type): Likewise.
(acc_get_device_num): Likewise.
(acc_set_device_num): Likewise.
(acc_on_device): Add comment that argument validity is not checked.
2019-12-03 Andrew Stubbs <ams@codesourcery.com> 2019-12-03 Andrew Stubbs <ams@codesourcery.com>
* testsuite/lib/libgomp.exp (offload_target_to_openacc_device_type): * testsuite/lib/libgomp.exp (offload_target_to_openacc_device_type):
......
...@@ -82,6 +82,18 @@ goacc_register (struct gomp_device_descr *disp) ...@@ -82,6 +82,18 @@ goacc_register (struct gomp_device_descr *disp)
gomp_mutex_unlock (&acc_device_lock); gomp_mutex_unlock (&acc_device_lock);
} }
static bool
known_device_type_p (acc_device_t d)
{
return d >= 0 && d < _ACC_device_hwm;
}
static void
unknown_device_type_error (acc_device_t invalid_type)
{
gomp_fatal ("unknown device type %u", invalid_type);
}
/* OpenACC names some things a little differently. */ /* OpenACC names some things a little differently. */
static const char * static const char *
...@@ -103,8 +115,9 @@ name_of_acc_device_t (enum acc_device_t type) ...@@ -103,8 +115,9 @@ name_of_acc_device_t (enum acc_device_t type)
case acc_device_host: return "host"; case acc_device_host: return "host";
case acc_device_not_host: return "not_host"; case acc_device_not_host: return "not_host";
case acc_device_nvidia: return "nvidia"; case acc_device_nvidia: return "nvidia";
default: gomp_fatal ("unknown device type %u", (unsigned) type); default: unknown_device_type_error (type);
} }
__builtin_unreachable ();
} }
/* ACC_DEVICE_LOCK must be held before calling this function. If FAIL_IS_ERROR /* ACC_DEVICE_LOCK must be held before calling this function. If FAIL_IS_ERROR
...@@ -123,7 +136,7 @@ resolve_device (acc_device_t d, bool fail_is_error) ...@@ -123,7 +136,7 @@ resolve_device (acc_device_t d, bool fail_is_error)
if (goacc_device_type) if (goacc_device_type)
{ {
/* Lookup the named device. */ /* Lookup the named device. */
while (++d != _ACC_device_hwm) while (known_device_type_p (++d))
if (dispatchers[d] if (dispatchers[d]
&& !strcasecmp (goacc_device_type, && !strcasecmp (goacc_device_type,
get_openacc_name (dispatchers[d]->name)) get_openacc_name (dispatchers[d]->name))
...@@ -147,7 +160,7 @@ resolve_device (acc_device_t d, bool fail_is_error) ...@@ -147,7 +160,7 @@ resolve_device (acc_device_t d, bool fail_is_error)
case acc_device_not_host: case acc_device_not_host:
/* Find the first available device after acc_device_not_host. */ /* Find the first available device after acc_device_not_host. */
while (++d != _ACC_device_hwm) while (known_device_type_p (++d))
if (dispatchers[d] && dispatchers[d]->get_num_devices_func () > 0) if (dispatchers[d] && dispatchers[d]->get_num_devices_func () > 0)
goto found; goto found;
if (d_arg == acc_device_default) if (d_arg == acc_device_default)
...@@ -168,7 +181,7 @@ resolve_device (acc_device_t d, bool fail_is_error) ...@@ -168,7 +181,7 @@ resolve_device (acc_device_t d, bool fail_is_error)
break; break;
default: default:
if (d > _ACC_device_hwm) if (!known_device_type_p (d))
{ {
if (fail_is_error) if (fail_is_error)
goto unsupported_device; goto unsupported_device;
...@@ -505,6 +518,9 @@ goacc_attach_host_thread_to_device (int ord) ...@@ -505,6 +518,9 @@ goacc_attach_host_thread_to_device (int ord)
void void
acc_init (acc_device_t d) acc_init (acc_device_t d)
{ {
if (!known_device_type_p (d))
unknown_device_type_error (d);
gomp_init_targets_once (); gomp_init_targets_once ();
gomp_mutex_lock (&acc_device_lock); gomp_mutex_lock (&acc_device_lock);
...@@ -519,6 +535,9 @@ ialias (acc_init) ...@@ -519,6 +535,9 @@ ialias (acc_init)
void void
acc_shutdown (acc_device_t d) acc_shutdown (acc_device_t d)
{ {
if (!known_device_type_p (d))
unknown_device_type_error (d);
gomp_init_targets_once (); gomp_init_targets_once ();
gomp_mutex_lock (&acc_device_lock); gomp_mutex_lock (&acc_device_lock);
...@@ -533,6 +552,9 @@ ialias (acc_shutdown) ...@@ -533,6 +552,9 @@ ialias (acc_shutdown)
int int
acc_get_num_devices (acc_device_t d) acc_get_num_devices (acc_device_t d)
{ {
if (!known_device_type_p (d))
unknown_device_type_error (d);
int n = 0; int n = 0;
struct gomp_device_descr *acc_dev; struct gomp_device_descr *acc_dev;
...@@ -564,6 +586,9 @@ ialias (acc_get_num_devices) ...@@ -564,6 +586,9 @@ ialias (acc_get_num_devices)
void void
acc_set_device_type (acc_device_t d) acc_set_device_type (acc_device_t d)
{ {
if (!known_device_type_p (d))
unknown_device_type_error (d);
struct gomp_device_descr *base_dev, *acc_dev; struct gomp_device_descr *base_dev, *acc_dev;
struct goacc_thread *thr = goacc_thread (); struct goacc_thread *thr = goacc_thread ();
...@@ -647,12 +672,12 @@ ialias (acc_get_device_type) ...@@ -647,12 +672,12 @@ ialias (acc_get_device_type)
int int
acc_get_device_num (acc_device_t d) acc_get_device_num (acc_device_t d)
{ {
if (!known_device_type_p (d))
unknown_device_type_error (d);
const struct gomp_device_descr *dev; const struct gomp_device_descr *dev;
struct goacc_thread *thr = goacc_thread (); struct goacc_thread *thr = goacc_thread ();
if (d >= _ACC_device_hwm)
gomp_fatal ("unknown device type %u", (unsigned) d);
acc_prof_info prof_info; acc_prof_info prof_info;
acc_api_info api_info; acc_api_info api_info;
bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info); bool profiling_p = GOACC_PROFILING_SETUP_P (thr, &prof_info, &api_info);
...@@ -682,6 +707,9 @@ ialias (acc_get_device_num) ...@@ -682,6 +707,9 @@ ialias (acc_get_device_num)
void void
acc_set_device_num (int ord, acc_device_t d) acc_set_device_num (int ord, acc_device_t d)
{ {
if (!known_device_type_p (d))
unknown_device_type_error (d);
struct gomp_device_descr *base_dev, *acc_dev; struct gomp_device_descr *base_dev, *acc_dev;
int num_devices; int num_devices;
...@@ -728,7 +756,10 @@ ialias (acc_set_device_num) ...@@ -728,7 +756,10 @@ ialias (acc_set_device_num)
version. version.
Compile this with optimization, so that the compiler expands Compile this with optimization, so that the compiler expands
this, rather than generating infinitely recursive code. */ this, rather than generating infinitely recursive code.
The function just forwards its argument to __builtin_acc_on_device. It does
not verify that the argument is a valid acc_device_t enumeration value. */
int __attribute__ ((__optimize__ ("O2"))) int __attribute__ ((__optimize__ ("O2")))
acc_on_device (acc_device_t dev) acc_on_device (acc_device_t dev)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment