Skip to content

Commit 87107a1

Browse files
authored
Merge pull request #2473 from DARMA-tasking/2472-argvcontainer-leaks-c-string-from-strdup
2472 argvcontainer leaks c string from strdup
2 parents 64053f2 + ace7857 commit 87107a1

File tree

11 files changed

+565
-211
lines changed

11 files changed

+565
-211
lines changed

src/vt/collective/collective_ops.cc

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "vt/runtime/runtime.h"
4747
#include "vt/scheduler/scheduler.h"
4848
#include "vt/runtime/runtime_inst.h"
49+
#include "vt/configs/arguments/args.h"
4950

5051
#include <memory>
5152
#include <cstdlib>
@@ -216,7 +217,7 @@ void printOverwrittens(
216217
template <runtime::RuntimeInstType instance>
217218
RuntimePtrType CollectiveAnyOps<instance>::initialize(
218219
int& argc, char**& argv, bool is_interop, MPI_Comm* comm,
219-
arguments::AppConfig const* appConfig
220+
arguments::AppConfig const* appConfig, bool print_startup_banner
220221
) {
221222
using vt::runtime::RuntimeInst;
222223
using vt::runtime::Runtime;
@@ -235,7 +236,7 @@ RuntimePtrType CollectiveAnyOps<instance>::initialize(
235236
::vt::rt = rt_ptr;
236237
curRT = rt_ptr;
237238
}
238-
RuntimeInst<instance>::rt->initialize();
239+
RuntimeInst<instance>::rt->initialize(false, print_startup_banner);
239240

240241
// If appConfig is not nullptr, compare CLI arguments with user-defined ones,
241242
// and report overwritten ones.
@@ -246,6 +247,33 @@ RuntimePtrType CollectiveAnyOps<instance>::initialize(
246247
return runtime::makeRuntimePtr(rt_ptr);
247248
}
248249

250+
template <runtime::RuntimeInstType instance>
251+
/*static*/ RuntimePtrType CollectiveAnyOps<instance>::initializePreconfigured(
252+
std::unique_ptr<StartupConfig> startup_config, MPI_Comm* comm,
253+
arguments::AppConfig const* app_config, bool print_startup_banner
254+
) {
255+
using vt::runtime::RuntimeInst;
256+
using vt::runtime::Runtime;
257+
using vt::runtime::eRuntimeInstance;
258+
259+
MPI_Comm resolved_comm = comm not_eq nullptr ? *comm : MPI_COMM_WORLD;
260+
261+
RuntimeInst<instance>::rt = std::make_unique<Runtime>(
262+
std::move(startup_config), resolved_comm, app_config,
263+
eRuntimeInstance::DefaultInstance
264+
);
265+
266+
auto rt_ptr = RuntimeInst<instance>::rt.get();
267+
if (instance == runtime::RuntimeInstType::DefaultInstance) {
268+
// Set global variable for default instance for backward compatibility
269+
::vt::rt = rt_ptr;
270+
curRT = rt_ptr;
271+
}
272+
RuntimeInst<instance>::rt->initialize(false, print_startup_banner);
273+
274+
return runtime::makeRuntimePtr(rt_ptr);
275+
}
276+
249277
template <runtime::RuntimeInstType instance>
250278
void CollectiveAnyOps<instance>::setCurrentRuntimeTLS(RuntimeUnsafePtrType in) {
251279
bool const has_rt = in != nullptr;

src/vt/collective/collective_ops.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,26 @@ static constexpr runtime::RuntimeInstType const collective_default_inst =
6060
template <runtime::RuntimeInstType instance = collective_default_inst>
6161
struct CollectiveAnyOps {
6262
// The general methods that interact with the managed runtime holder
63+
64+
/**
65+
* \brief Initialize VT
66+
*
67+
* \param[in] argc (to modify)
68+
* \param[in] argv (to modify)
69+
* \param[in] is_interop use interop mode (don't initialize MPI)
70+
* \param[in] comm optional communicator
71+
* \param[in] app_config (optional) base VT configuration to use
72+
* \param[in] print_startup_banner (optional) whether to print startup banner
73+
*
74+
* \return runtime pointer
75+
*/
6376
static RuntimePtrType initialize(
6477
int& argc, char**& argv, bool is_interop = false, MPI_Comm* comm = nullptr,
65-
arguments::AppConfig const* appConfig = nullptr
78+
arguments::AppConfig const* appConfig = nullptr,
79+
bool print_startup_banner = true
6680
);
81+
82+
//// Old version of initialize with number of worker threads
6783
[[deprecated]] static RuntimePtrType initialize(
6884
int& argc, char**& argv, PhysicalResourceType const /* num_workers */,
6985
bool is_interop = false, MPI_Comm* comm = nullptr,
@@ -72,6 +88,24 @@ struct CollectiveAnyOps {
7288
{
7389
return initialize(argc, argv, is_interop, comm, appConfig);
7490
}
91+
92+
/**
93+
* \brief Initialize a VT runtime with arguments preconfigured
94+
*
95+
* \param[in] startup_config startup config returned from \c preconfigure
96+
* \param[in] comm the communicator for VT to use
97+
* \param[in] app_config the app config for overriding the config
98+
* \param[in] print_startup_banner (optional) whether to print startup banner
99+
*
100+
* \return the runtime pointer
101+
*/
102+
static RuntimePtrType initializePreconfigured(
103+
std::unique_ptr<StartupConfig> startup_config,
104+
MPI_Comm* comm = nullptr,
105+
arguments::AppConfig const* app_config = nullptr,
106+
bool print_startup_banner = true
107+
);
108+
75109
static void finalize(RuntimePtrType in_rt = nullptr);
76110
static void scheduleThenFinalize(RuntimePtrType in_rt = nullptr);
77111
static void setCurrentRuntimeTLS(RuntimeUnsafePtrType in_rt = nullptr);

src/vt/collective/startup.cc

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,37 +46,39 @@
4646
#include "vt/collective/collective_ops.h"
4747
#include "vt/runtime/runtime_headers.h"
4848
#include "vt/context/context.h"
49+
#include "vt/configs/arguments/args.h"
4950

5051
namespace vt {
5152

52-
std::unique_ptr<arguments::ArgvContainer>
53-
preconfigure(int& argc, char**& argv) {
54-
return std::make_unique<arguments::ArgvContainer>(argc, argv);
53+
std::unique_ptr<StartupConfig> preconfigure(
54+
int& argc, char**& argv
55+
) {
56+
auto arg_config = std::make_unique<arguments::ArgConfig>();
57+
auto parse_input_holder = arg_config->setupInputHolder(argc, argv);
58+
return std::make_unique<StartupConfig>(
59+
std::move(arg_config), std::move(parse_input_holder)
60+
);
5561
}
5662

5763
RuntimePtrType initializePreconfigured(
58-
MPI_Comm* comm, arguments::AppConfig const* appConfig,
59-
arguments::ArgvContainer const* preconfigure_args
64+
std::unique_ptr<StartupConfig> startup_config,
65+
MPI_Comm* comm,
66+
arguments::AppConfig const* app_config,
67+
bool print_startup_banner
6068
) {
61-
arguments::ArgvContainer args =
62-
preconfigure_args ? *preconfigure_args : arguments::ArgvContainer{};
63-
64-
auto argc = args.getArgc();
65-
auto argv_container = args.getArgvDeepCopy();
66-
auto argv = argv_container.get();
67-
bool const is_interop = comm != nullptr;
68-
return CollectiveOps::initialize(
69-
argc, argv, is_interop, comm, appConfig
69+
return CollectiveOps::initializePreconfigured(
70+
std::move(startup_config), comm, app_config, print_startup_banner
7071
);
7172
}
7273

7374
// vt::{initialize,finalize} for main ::vt namespace
7475
RuntimePtrType initialize(
75-
int& argc, char**& argv, MPI_Comm* comm, arguments::AppConfig const* appConfig
76+
int& argc, char**& argv, MPI_Comm* comm, arguments::AppConfig const* appConfig,
77+
bool print_startup_banner
7678
) {
7779
bool const is_interop = comm != nullptr;
7880
return CollectiveOps::initialize(
79-
argc, argv, is_interop, comm, appConfig
81+
argc, argv, is_interop, comm, appConfig, print_startup_banner
8082
);
8183
}
8284

src/vt/collective/startup.h

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,73 @@
4646

4747
#include "vt/config.h"
4848
#include "vt/runtime/runtime_headers.h"
49-
#include "vt/configs/arguments/argv_container.h"
49+
#include "vt/collective/startup_config.h"
5050
#include <mpi.h>
5151

52+
#include <memory>
53+
5254
namespace vt {
5355

54-
std::unique_ptr<arguments::ArgvContainer>
55-
preconfigure(int& argc, char**& argv);
56+
/**
57+
* \brief Preconfigure VT with argc/argv. This will remove all VT arguments and
58+
* create a \c StartupConfig for VT that should be passed to \c
59+
* initializePreconfigured. Optionally, one may specify an MPI communicator to
60+
* use (otherwise, it defaults to \c MPI_COMM_WORLD).
61+
*
62+
* \note MPI must be initialized to call this function because if an error
63+
* occurs it uses MPI rank to limit how many times the error text gets printed.
64+
*
65+
* \param[in] argc argc (modifies it to remove VT arguments)
66+
* \param[in] argv argv (modifies it to remove VT arguments)
67+
*
68+
* \return the \c StartupConfig to pass to VT
69+
*/
70+
std::unique_ptr<StartupConfig> preconfigure(
71+
int& argc, char**& argv
72+
);
73+
74+
/**
75+
* \brief Initialize VT after it has been preconfigured
76+
*
77+
* \param[in] startup_config the arg config
78+
* \param[in] comm optional communicator
79+
* \param[in] app_config (optional) base VT configuration to use
80+
* \param[in] print_startup_banner (optional) whether to print startup banner
81+
*
82+
* \return the runtime pointer
83+
*/
5684
RuntimePtrType initializePreconfigured(
85+
std::unique_ptr<StartupConfig> startup_config,
5786
MPI_Comm* comm = nullptr,
58-
arguments::AppConfig const* appConfig = nullptr,
59-
arguments::ArgvContainer const* preconfigure_args = nullptr);
87+
arguments::AppConfig const* app_config = nullptr,
88+
bool print_startup_banner = true
89+
);
6090

91+
/**
92+
* \brief Initialize VT
93+
*
94+
* \param[in] argc (to modify)
95+
* \param[in] argv (to modify)
96+
* \param[in] comm optional communicator
97+
* \param[in] app_config (optional) base VT configuration to use
98+
* \param[in] print_startup_banner (optional) whether to print startup banner
99+
*
100+
* \return the runtime pointer
101+
*/
61102
RuntimePtrType initialize(
62103
int& argc, char**& argv, MPI_Comm* comm = nullptr,
63-
arguments::AppConfig const* appConfig = nullptr
104+
arguments::AppConfig const* appConfig = nullptr,
105+
bool print_startup_banner = true
64106
);
107+
65108
RuntimePtrType initialize(MPI_Comm* comm = nullptr);
109+
66110
RuntimePtrType initialize(
67111
int& argc, char**& argv, arguments::AppConfig const* appConfig
68112
);
69113

70114
void finalize(RuntimePtrType in_rt);
115+
71116
void finalize();
72117

73118
} /* end namespace vt */
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
//@HEADER
3+
// *****************************************************************************
4+
//
5+
// startup_config.cc
6+
// DARMA/vt => Virtual Transport
7+
//
8+
// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC
9+
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
10+
// Government retains certain rights in this software.
11+
//
12+
// Redistribution and use in source and binary forms, with or without
13+
// modification, are permitted provided that the following conditions are met:
14+
//
15+
// * Redistributions of source code must retain the above copyright notice,
16+
// this list of conditions and the following disclaimer.
17+
//
18+
// * Redistributions in binary form must reproduce the above copyright notice,
19+
// this list of conditions and the following disclaimer in the documentation
20+
// and/or other materials provided with the distribution.
21+
//
22+
// * Neither the name of the copyright holder nor the names of its
23+
// contributors may be used to endorse or promote products derived from this
24+
// software without specific prior written permission.
25+
//
26+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36+
// POSSIBILITY OF SUCH DAMAGE.
37+
//
38+
// Questions? Contact [email protected]
39+
//
40+
// *****************************************************************************
41+
//@HEADER
42+
*/
43+
44+
#include "vt/collective/startup_config.h"
45+
#include "vt/configs/arguments/args.h"
46+
47+
namespace vt {
48+
49+
StartupConfig::StartupConfig(
50+
std::unique_ptr<arguments::ArgConfig> in_arg_config,
51+
std::unique_ptr<ParseInputHolder> in_parse_input_holder
52+
) : arg_config_(std::move(in_arg_config)),
53+
parse_input_holder_(std::move(in_parse_input_holder))
54+
{ }
55+
56+
StartupConfig::~StartupConfig() = default;
57+
58+
} /* end namespace vt */

0 commit comments

Comments
 (0)