Browse Source

Make a copy of the user configuration, to avoid crashes when the config structure is freed by the user

Samuel Thibault 9 years ago
parent
commit
56bb0f1bf3

+ 1 - 1
src/core/detect_combined_workers.c

@@ -355,7 +355,7 @@ void _starpu_sched_find_worker_combinations(int *workerids, int nworkers)
 {
 	struct _starpu_machine_config *config = _starpu_get_machine_config();
 
-	if (config->conf->single_combined_worker > 0)
+	if (config->conf.single_combined_worker > 0)
 		combine_all_cpu_workers(workerids, nworkers);
 	else
 	{

+ 1 - 1
src/core/perfmodel/perfmodel_bus.c

@@ -1561,7 +1561,7 @@ static void check_bus_config_file(void)
 
         get_config_path(path, sizeof(path));
         res = access(path, F_OK);
-	if (res || config->conf->bus_calibrate > 0)
+	if (res || config->conf.bus_calibrate > 0)
 	{
 		if (res)
 			_STARPU_DISP("No performance model for the bus, calibrating...\n");

+ 2 - 2
src/core/sched_policy.c

@@ -147,7 +147,7 @@ static void display_sched_help_message(void)
 struct starpu_sched_policy *_starpu_select_sched_policy(struct _starpu_machine_config *config, const char *required_policy)
 {
 	struct starpu_sched_policy *selected_policy = NULL;
-	struct starpu_conf *user_conf = config->conf;
+	struct starpu_conf *user_conf = &config->conf;
 
 	if(required_policy)
 		selected_policy = find_sched_policy_from_name(required_policy);
@@ -184,7 +184,7 @@ void _starpu_init_sched_policy(struct _starpu_machine_config *config, struct _st
 		use_prefetch = 1;
 
 	/* Set calibrate flag */
-	_starpu_set_calibrate_flag(config->conf->calibrate);
+	_starpu_set_calibrate_flag(config->conf.calibrate);
 
 	load_sched_policy(selected_policy, sched_ctx);
 

+ 12 - 12
src/core/topology.c

@@ -160,7 +160,7 @@ static void
 _starpu_initialize_workers_cuda_gpuid (struct _starpu_machine_config *config)
 {
 	struct _starpu_machine_topology *topology = &config->topology;
-	struct starpu_conf *uconf = config->conf;
+	struct starpu_conf *uconf = &config->conf;
 
         _starpu_initialize_workers_deviceid (
 		uconf->use_explicit_workers_cuda_gpuid == 0
@@ -188,7 +188,7 @@ static void
 _starpu_initialize_workers_opencl_gpuid (struct _starpu_machine_config*config)
 {
 	struct _starpu_machine_topology *topology = &config->topology;
-	struct starpu_conf *uconf = config->conf;
+	struct starpu_conf *uconf = &config->conf;
 
         _starpu_initialize_workers_deviceid(
 		uconf->use_explicit_workers_opencl_gpuid == 0
@@ -276,7 +276,7 @@ _starpu_get_next_opencl_gpuid (struct _starpu_machine_config *config)
 static void _starpu_initialize_workers_mic_deviceid(struct _starpu_machine_config *config)
 {
 	struct _starpu_machine_topology *topology = &config->topology;
-	struct starpu_conf *uconf = config->conf;
+	struct starpu_conf *uconf = &config->conf;
 
 	_starpu_initialize_workers_deviceid(
 		uconf->use_explicit_workers_mic_deviceid == 0
@@ -295,7 +295,7 @@ static void _starpu_initialize_workers_mic_deviceid(struct _starpu_machine_confi
 static void _starpu_initialize_workers_scc_deviceid(struct _starpu_machine_config *config)
 {
 	struct _starpu_machine_topology *topology = &config->topology;
-	struct starpu_conf *uconf = config->conf;
+	struct starpu_conf *uconf = &config->conf;
 
 	_starpu_initialize_workers_deviceid(
 		uconf->use_explicit_workers_scc_deviceid == 0
@@ -351,7 +351,7 @@ _starpu_init_mic_node (struct _starpu_machine_config *config, int mic_idx,
 {
 	/* Initialize the MIC node of index MIC_IDX. */
 
-	struct starpu_conf *user_conf = config->conf;
+	struct starpu_conf *user_conf = &config->conf;
 
 	char ***argv = _starpu_get_argv();
 	const char *suffixes[] = {"-mic", "_mic", NULL};
@@ -559,11 +559,11 @@ _starpu_initialize_workers_bindid (struct _starpu_machine_config *config)
 			}
 		}
 	}
-	else if (config->conf->use_explicit_workers_bindid)
+	else if (config->conf.use_explicit_workers_bindid)
 	{
 		/* we use the explicit value from the user */
 		memcpy(topology->workers_bindid,
-			config->conf->workers_bindid,
+			config->conf.workers_bindid,
 			STARPU_NMAXWORKERS*sizeof(unsigned));
 	}
 	else
@@ -833,7 +833,7 @@ _starpu_init_machine_config(struct _starpu_machine_config *config, int no_mp_con
 	_starpu_initialize_workers_bindid(config);
 
 #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
-	int ncuda = config->conf->ncuda;
+	int ncuda = config->conf.ncuda;
 	int nworker_per_cuda = starpu_get_env_number_default("STARPU_NWORKER_PER_CUDA", 1);
 
 	STARPU_ASSERT_MSG(nworker_per_cuda > 0, "STARPU_NWORKER_PER_CUDA has to be > 0");
@@ -909,7 +909,7 @@ _starpu_init_machine_config(struct _starpu_machine_config *config, int no_mp_con
 #endif
 
 #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
-	int nopencl = config->conf->nopencl;
+	int nopencl = config->conf.nopencl;
 
 	if (nopencl != 0)
 	{
@@ -979,7 +979,7 @@ _starpu_init_machine_config(struct _starpu_machine_config *config, int no_mp_con
 #endif
 
 #ifdef STARPU_USE_SCC
-	int nscc = config->conf->nscc;
+	int nscc = config->conf.nscc;
 
 	unsigned nb_scc_nodes = _starpu_scc_src_get_device_count();
 
@@ -1051,13 +1051,13 @@ _starpu_init_machine_config(struct _starpu_machine_config *config, int no_mp_con
 	 * ones of the mp nodes. */
 #ifdef STARPU_USE_MIC
 	if (! no_mp_config)
-	    _starpu_init_mp_config (config, config->conf);
+	    _starpu_init_mp_config (config, &config->conf);
 #endif
 
 /* we put the CPU section after the accelerator : in case there was an
  * accelerator found, we devote one cpu */
 #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
-	int ncpu = config->conf->ncpus;
+	int ncpu = config->conf.ncpus;
 
 	if (ncpu != 0)
 	{

+ 46 - 29
src/core/workers.c

@@ -662,7 +662,7 @@ static void _starpu_launch_drivers(struct _starpu_machine_config *pconfig)
 #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
 			case STARPU_CPU_WORKER:
 				driver.id.cpu_id = workerarg->devid;
-				if (_starpu_may_launch_driver(pconfig->conf, &driver))
+				if (_starpu_may_launch_driver(&pconfig->conf, &driver))
 				{
 					STARPU_PTHREAD_CREATE_ON(
 						workerarg->name,
@@ -714,7 +714,7 @@ static void _starpu_launch_drivers(struct _starpu_machine_config *pconfig)
 				worker_set->workers = workerarg;
 				worker_set->set_is_initialized = 0;
 
-				if (!_starpu_may_launch_driver(pconfig->conf, &driver))
+				if (!_starpu_may_launch_driver(&pconfig->conf, &driver))
 				{
 					workerarg->run_by_starpu = 0;
 					break;
@@ -746,7 +746,7 @@ static void _starpu_launch_drivers(struct _starpu_machine_config *pconfig)
 			case STARPU_OPENCL_WORKER:
 #ifndef STARPU_SIMGRID
 				starpu_opencl_get_device(workerarg->devid, &driver.id.opencl_id);
-				if (!_starpu_may_launch_driver(pconfig->conf, &driver))
+				if (!_starpu_may_launch_driver(&pconfig->conf, &driver))
 				{
 					workerarg->run_by_starpu = 0;
 					break;
@@ -845,7 +845,7 @@ static void _starpu_launch_drivers(struct _starpu_machine_config *pconfig)
 		{
 			case STARPU_CPU_WORKER:
 				driver.id.cpu_id = workerarg->devid;
-				if (!_starpu_may_launch_driver(pconfig->conf, &driver))
+				if (!_starpu_may_launch_driver(&pconfig->conf, &driver))
 					break;
 				_STARPU_DEBUG("waiting for worker %u initialization\n", worker);
 				STARPU_PTHREAD_MUTEX_LOCK(&workerarg->mutex);
@@ -860,7 +860,7 @@ static void _starpu_launch_drivers(struct _starpu_machine_config *pconfig)
 			case STARPU_OPENCL_WORKER:
 #ifndef STARPU_SIMGRID
 				starpu_opencl_get_device(workerarg->devid, &driver.id.opencl_id);
-				if (!_starpu_may_launch_driver(pconfig->conf, &driver))
+				if (!_starpu_may_launch_driver(&pconfig->conf, &driver))
 					break;
 #endif
 				_STARPU_DEBUG("waiting for worker %u initialization\n", worker);
@@ -1180,24 +1180,37 @@ int starpu_initialize(struct starpu_conf *user_conf, int *argc, char ***argv)
 	/* store the pointer to the user explicit configuration during the
 	 * initialization */
 	if (user_conf == NULL)
+		 starpu_conf_init(&config.conf);
+	else
 	{
-	     struct starpu_conf *conf = malloc(sizeof(struct starpu_conf));
-	     starpu_conf_init(conf);
-	     config.conf = conf;
-	     config.default_conf = 1;
+		if (user_conf->magic != 42)
+		{
+			_STARPU_DISP("starpu_conf structure needs to be initialized with starpu_conf_init\n");
+			return -EINVAL;
+		}
+		config.conf = *user_conf;
 	}
-	else
+	/* Make a copy of arrays */
+	if (config.conf.sched_policy_name)
+		config.conf.sched_policy_name = strdup(config.conf.sched_policy_name);
+	if (config.conf.mic_sink_program_path)
+		config.conf.mic_sink_program_path = strdup(config.conf.mic_sink_program_path);
+	if (config.conf.n_cuda_opengl_interoperability)
 	{
-	     if (user_conf->magic != 42)
-	     {
-		  _STARPU_DISP("starpu_conf structure needs to be initialized with starpu_conf_init\n");
-		  return -EINVAL;
-	     }
-	     config.conf = user_conf;
-	     config.default_conf = 0;
+		size_t size = config.conf.n_cuda_opengl_interoperability * sizeof(*config.conf.cuda_opengl_interoperability);
+		unsigned *copy = malloc(size);
+		memcpy(copy, config.conf.cuda_opengl_interoperability, size);
+		config.conf.cuda_opengl_interoperability = copy;
+	}
+	if (config.conf.n_not_launched_drivers)
+	{
+		size_t size = config.conf.n_not_launched_drivers * sizeof(*config.conf.not_launched_drivers);
+		struct starpu_driver *copy = malloc(size);
+		memcpy(copy, config.conf.not_launched_drivers, size);
+		config.conf.not_launched_drivers = copy;
 	}
 
-	_starpu_conf_check_environment(config.conf);
+	_starpu_conf_check_environment(&config.conf);
 
 	_starpu_init_all_sched_ctxs(&config);
 	_starpu_init_progression_hooks();
@@ -1205,7 +1218,7 @@ int starpu_initialize(struct starpu_conf *user_conf, int *argc, char ***argv)
 	_starpu_init_tags();
 
 #ifdef STARPU_USE_FXT
-	_starpu_fxt_init_profiling(config.conf->trace_buffer_size);
+	_starpu_fxt_init_profiling(config.conf.trace_buffer_size);
 #endif
 
 	_starpu_open_debug_logfile();
@@ -1254,8 +1267,8 @@ int starpu_initialize(struct starpu_conf *user_conf, int *argc, char ***argv)
 
 	if (!is_a_sink)
 	{
-		struct starpu_sched_policy *selected_policy = _starpu_select_sched_policy(&config, config.conf->sched_policy_name);
-		_starpu_create_sched_ctx(selected_policy, NULL, -1, 1, "init", (config.conf->global_sched_ctx_min_priority != -1), config.conf->global_sched_ctx_min_priority, (config.conf->global_sched_ctx_min_priority != -1), config.conf->global_sched_ctx_max_priority, 1, config.conf->sched_policy_init);
+		struct starpu_sched_policy *selected_policy = _starpu_select_sched_policy(&config, config.conf.sched_policy_name);
+		_starpu_create_sched_ctx(selected_policy, NULL, -1, 1, "init", (config.conf.global_sched_ctx_min_priority != -1), config.conf.global_sched_ctx_min_priority, (config.conf.global_sched_ctx_min_priority != -1), config.conf.global_sched_ctx_max_priority, 1, config.conf.sched_policy_init);
 	}
 
 	_starpu_initialize_registered_performance_models();
@@ -1436,7 +1449,7 @@ unsigned _starpu_worker_can_block(unsigned memnode STARPU_ATTRIBUTE_UNUSED, stru
 	default:
 		goto always_launch;
 	}
-	if (!_starpu_may_launch_driver(config.conf, &driver))
+	if (!_starpu_may_launch_driver(&config.conf, &driver))
 		return 0;
 
 always_launch:
@@ -1560,9 +1573,13 @@ void starpu_shutdown(void)
 	STARPU_PTHREAD_COND_SIGNAL(&init_cond);
 	STARPU_PTHREAD_MUTEX_UNLOCK(&init_mutex);
 
-	/* Clear memory if it was allocated by StarPU */
-	if (config.default_conf)
-	     free(config.conf);
+	/* Clear memory */
+	free((char*) config.conf.sched_policy_name);
+	free(config.conf.mic_sink_program_path);
+	if (config.conf.n_cuda_opengl_interoperability)
+		free(config.conf.cuda_opengl_interoperability);
+	if (config.conf.n_not_launched_drivers)
+		free(config.conf.not_launched_drivers);
 
 #ifdef HAVE_AYUDAME_H
 	if (AYU_event) AYU_event(AYU_FINISH, 0, NULL);
@@ -1638,22 +1655,22 @@ unsigned starpu_opencl_worker_get_count(void)
 
 int starpu_asynchronous_copy_disabled(void)
 {
-	return config.conf->disable_asynchronous_copy;
+	return config.conf.disable_asynchronous_copy;
 }
 
 int starpu_asynchronous_cuda_copy_disabled(void)
 {
-	return config.conf->disable_asynchronous_cuda_copy;
+	return config.conf.disable_asynchronous_cuda_copy;
 }
 
 int starpu_asynchronous_opencl_copy_disabled(void)
 {
-	return config.conf->disable_asynchronous_opencl_copy;
+	return config.conf.disable_asynchronous_opencl_copy;
 }
 
 int starpu_asynchronous_mic_copy_disabled(void)
 {
-	return config.conf->disable_asynchronous_mic_copy;
+	return config.conf.disable_asynchronous_mic_copy;
 }
 
 unsigned starpu_mic_worker_get_count(void)

+ 1 - 5
src/core/workers.h

@@ -324,11 +324,7 @@ struct _starpu_machine_config
 	uint32_t worker_mask;
 
         /* either the user given configuration passed to starpu_init or a default configuration */
-	struct starpu_conf *conf;
-	/* set to 1 if no conf has been given by the user, it
-	 * indicates the memory allocated for the default
-	 * configuration should be freed on shutdown */
-	int default_conf;
+	struct starpu_conf conf;
 
 	/* this flag is set until the runtime is stopped */
 	unsigned running;

+ 1 - 1
src/drivers/cuda/driver_cuda.c

@@ -197,7 +197,7 @@ void starpu_cuda_set_device(unsigned devid STARPU_ATTRIBUTE_UNUSED)
 	STARPU_ABORT();
 #else
 	cudaError_t cures;
-	struct starpu_conf *conf = _starpu_get_machine_config()->conf;
+	struct starpu_conf *conf = &_starpu_get_machine_config()->conf;
 #if !defined(HAVE_CUDA_MEMCPY_PEER) && defined(HAVE_CUDA_GL_INTEROP_H)
 	unsigned i;
 #endif