diff --git a/unified-runtime/source/common/logger/ur_sinks.hpp b/unified-runtime/source/common/logger/ur_sinks.hpp index 1e49cddac5d9d..c15e8b2cb0bd2 100644 --- a/unified-runtime/source/common/logger/ur_sinks.hpp +++ b/unified-runtime/source/common/logger/ur_sinks.hpp @@ -35,13 +35,16 @@ class Sink { << "[" << level_to_str(level) << "]: "; } - format(buffer, filename, lineno, fmt, std::forward(args)...); + format(buffer, filename, lineno, fmt, std::forward(args)...); if (add_fileline) { buffer << " <" << filename << ":" << lineno << ">"; } if (!skip_linebreak) { buffer << "\n"; } + + std::string message = buffer.str(); + // This is a temporary workaround on windows, where UR adapter is teardowned // before the UR loader, which will result in access violation when we use print // function as the overrided print function was already released with the UR @@ -50,12 +53,12 @@ class Sink { // using thier own sink class that inherit from logger::Sink. #if defined(_WIN32) if (isTearDowned) { - std::cerr << buffer.str() << "\n"; + std::cerr << message << "\n"; } else { - print(level, buffer.str()); + print(level, message); } #else - print(level, buffer.str()); + print(level, message); #endif } @@ -155,7 +158,7 @@ class Sink { } } - format(buffer, filename, lineno, ++fmt, std::forward(args)...); + format(buffer, filename, lineno, ++fmt, std::forward(args)...); } }; diff --git a/unified-runtime/source/common/ur_util.hpp b/unified-runtime/source/common/ur_util.hpp index 7af5eedc1ec1e..4266b4876eb39 100644 --- a/unified-runtime/source/common/ur_util.hpp +++ b/unified-runtime/source/common/ur_util.hpp @@ -167,14 +167,14 @@ getenv_to_vec(const char *env_var_name) { return std::nullopt; } - auto is_quoted = [](std::string &str) { - return (str.front() == '\'' && str.back() == '\'') || - (str.front() == '"' && str.back() == '"'); + auto is_quoted = [](const std::string &str) { + return str.size() >= 2 && ((str.front() == '\'' && str.back() == '\'') || + (str.front() == '"' && str.back() == '"')); }; - auto has_colon = [](std::string &str) { + auto has_colon = [](const std::string &str) { return str.find(':') != std::string::npos; }; - auto has_semicolon = [](std::string &str) { + auto has_semicolon = [](const std::string &str) { return str.find(';') != std::string::npos; }; @@ -188,8 +188,7 @@ getenv_to_vec(const char *env_var_name) { } if (is_quoted(value)) { - value.erase(value.cbegin()); - value.erase(value.cend() - 1); + value = value.substr(1, value.length() - 2); } values_vec.push_back(value); @@ -238,27 +237,29 @@ inline std::optional getenv_to_map(const char *env_var_name, return std::nullopt; } - auto is_quoted = [](std::string &str) { - return (str.front() == '\'' && str.back() == '\'') || - (str.front() == '"' && str.back() == '"'); + auto is_quoted = [](const std::string &str) { + return str.size() >= 2 && ((str.front() == '\'' && str.back() == '\'') || + (str.front() == '"' && str.back() == '"')); }; - auto has_colon = [](std::string &str) { + auto has_colon = [](const std::string &str) { return str.find(':') != std::string::npos; }; std::stringstream ss(*env_var); std::string key_value; while (std::getline(ss, key_value, main_delim)) { - std::string key; - std::string values; - std::stringstream kv_ss(key_value); - if (reject_empty && !has_colon(key_value)) { throw_wrong_format_map(env_var_name, *env_var); } - std::getline(kv_ss, key, key_value_delim); - std::getline(kv_ss, values); + size_t colon_pos = key_value.find(key_value_delim); + if (colon_pos == std::string::npos) { + throw_wrong_format_map(env_var_name, *env_var); + } + + std::string key = key_value.substr(0, colon_pos); + std::string values = key_value.substr(colon_pos + 1); + if (key.empty() || (reject_empty && values.empty()) || (map.find(key) != map.end() && !allow_duplicate)) { throw_wrong_format_map(env_var_name, *env_var); @@ -269,18 +270,28 @@ inline std::optional getenv_to_map(const char *env_var_name, } std::vector values_vec; - std::stringstream values_ss(values); - std::string value; - while (std::getline(values_ss, value, values_delim)) { + + size_t start = 0; + size_t pos = 0; + while ((pos = values.find(values_delim, start)) != std::string::npos || + start < values.length()) { + // No delimiter found, process the last value + if (pos == std::string::npos) { + pos = values.length(); + } + + std::string value = values.substr(start, pos - start); if (value.empty() || (has_colon(value) && !is_quoted(value))) { throw_wrong_format_map(env_var_name, *env_var); } if (is_quoted(value)) { - value.erase(value.cbegin()); - value.pop_back(); + value = value.substr(1, value.length() - 2); } - values_vec.push_back(value); + values_vec.push_back(std::move(value)); + + start = pos + 1; } + if (map.find(key) != map.end()) { map[key].insert(map[key].end(), values_vec.begin(), values_vec.end()); } else { diff --git a/unified-runtime/source/loader/ur_lib.cpp b/unified-runtime/source/loader/ur_lib.cpp index 9a074b5a9b3af..1e040433de6a8 100644 --- a/unified-runtime/source/loader/ur_lib.cpp +++ b/unified-runtime/source/loader/ur_lib.cpp @@ -38,14 +38,14 @@ context_t::context_t() { parseEnvEnabledLayers(); } context_t::~context_t() {} void context_t::parseEnvEnabledLayers() { - auto maybeEnableEnvVarMap = getenv_to_map("UR_ENABLE_LAYERS", false); - if (!maybeEnableEnvVarMap.has_value()) { + auto maybeEnableEnvVarVec = getenv_to_vec("UR_ENABLE_LAYERS"); + if (!maybeEnableEnvVarVec.has_value()) { return; } - auto enableEnvVarMap = maybeEnableEnvVarMap.value(); + auto enableEnvVarVec = maybeEnableEnvVarVec.value(); - for (auto &key : enableEnvVarMap) { - enabledLayerNames.insert(key.first); + for (auto &layer : enableEnvVarVec) { + enabledLayerNames.insert(layer); } }