Skip to content

Commit

Permalink
Fix crash when no logging selected (#107)
Browse files Browse the repository at this point in the history
* Fix crash when no logging is enabled
* Workaround a jenkins pipeline error
* fix spelling error and use pair instead of out param
  • Loading branch information
deschuma authored Jun 4, 2020
1 parent dde25eb commit 6b2be92
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .jenkins/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def ACCTest(String label, String version, String compiler, String build_type) {
make
mkdir -p ${WORKSPACE}/openenclave/build
cd ${WORKSPACE}/openenclave/build
git submodule update --recursive --init
cmake ${WORKSPACE}/openenclave -G Ninja -DCMAKE_BUILD_TYPE=${build_type}
ninja -v
LD_LIBRARY_PATH=${WORKSPACE}/src/Linux ctest --output-on-failure
Expand Down Expand Up @@ -62,6 +63,7 @@ def ACCContainerTest(String label, String version) {
sudo dpkg -i ${WORKSPACE}/src/az-dcap-client_*_amd64.deb
mkdir -p ${WORKSPACE}/openenclave/build
cd ${WORKSPACE}/openenclave/build
git submodule update --recursive --init
cmake ${WORKSPACE}/openenclave -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo
ninja -v
ctest --output-on-failure
Expand Down
16 changes: 13 additions & 3 deletions src/UnitTests/test_quote_prov.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ static inline float MeasureFunction(measured_function_t func)
chrono::steady_clock::now() - start).count() / 1000000;
}

void RunQuoteProviderTests(bool caching_enabled = true)
void RunQuoteProviderTests(bool caching_enabled = false)
{
local_cache_clear();

Expand Down Expand Up @@ -431,7 +431,7 @@ void RunQuoteProviderTests(bool caching_enabled = true)
}
}

void ReloadLibrary(libary_type_t *library)
void ReloadLibrary(libary_type_t *library, bool set_logging_callback = true)
{
#if defined __LINUX__
dlclose(*library);
Expand All @@ -440,7 +440,10 @@ void ReloadLibrary(libary_type_t *library)
FreeLibrary(*library);
*library = LoadFunctions();
#endif
assert(SGX_PLAT_ERROR_OK == sgx_ql_set_logging_function(Log));
if (set_logging_callback)
{
assert(SGX_PLAT_ERROR_OK == sgx_ql_set_logging_function(Log));
}
}

#ifndef __LINUX__
Expand Down Expand Up @@ -681,6 +684,13 @@ extern void QuoteProvTests()
RunQuoteProviderTests();
GetQveIdentityTest();

//
// Run tests without logging to make sure library can operate
// even if logging callback isn't set
//
ReloadLibrary(&library, false);
RunQuoteProviderTests();
GetQveIdentityTest();
//
// Run tests to make sure libray can operate
// even if access to filesystem is restricted
Expand Down
10 changes: 10 additions & 0 deletions src/dcap_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ enum class CollateralTypes
PckRootCrl
};

static std::string get_env_variable(std::string env_variable)
{
auto retval = get_env_variable_no_log(env_variable);
if (!retval.second.empty())
{
log(SGX_QL_LOG_ERROR, retval.second.c_str());
}
return retval.first;
}

static std::string get_collateral_version()
{
std::string collateral_version =
Expand Down
49 changes: 28 additions & 21 deletions src/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,56 @@

#define MAX_ENV_VAR_LENGTH 2000

static std::string get_env_variable(std::string env_variable)
#include <sstream>
#include <utility>

static std::pair<std::string, std::string> get_env_variable_no_log(
std::string env_variable)
{
const char* env_value;
std::stringstream error_message_stream;
#ifdef __LINUX__
env_value = getenv(env_variable.c_str());
if (env_value == NULL)
{
return std::string();
error_message_stream << "Could not retreive environment variable for '"
<< env_variable << "'";
return std::make_pair(std::string(), error_message_stream.str());
}
#else
std::unique_ptr<char[]> env_temp =
std::make_unique<char[]>(MAX_ENV_VAR_LENGTH);
if (env_temp == nullptr)
{
log(SGX_QL_LOG_ERROR,
"Failed to allocate memory for environment varible for '%s'",
env_variable.c_str());
error_message_stream
<< "Failed to allocate memory for environment varible for '"
<< env_variable << "'";
return std::make_pair(std::string(), error_message_stream.str());
}

env_value = env_temp.get();
DWORD status = GetEnvironmentVariableA(
env_variable.c_str(), env_temp.get(), MAX_ENV_VAR_LENGTH);
if (status == 0)
{
log(SGX_QL_LOG_ERROR,
"Failed to retreive environment varible for '%s'",
env_variable.c_str());
return std::string();
error_message_stream << "Could not retreive environment variable for '"
<< env_variable << "'";
return std::make_pair(std::string(), error_message_stream.str());
}
#endif
else
auto length = strnlen(env_value, MAX_ENV_VAR_LENGTH);
if (length <= 0 || length == MAX_ENV_VAR_LENGTH)
{
if ((strnlen(env_value, MAX_ENV_VAR_LENGTH) <= 0) ||
(strnlen(env_value, MAX_ENV_VAR_LENGTH) == MAX_ENV_VAR_LENGTH))
{
log(SGX_QL_LOG_ERROR,
"Value specified in environment variable %s is either empty or "
"expected max length '%d'.",
env_variable.c_str());
return std::string();
}

return std::string(env_value);
error_message_stream << "Length of environment variable '"
<< env_variable << "' ";
error_message_stream << "is either empty or equal to expected max "
"length. ";
error_message_stream << "Actual length is: " << length << " ";
error_message_stream << "Max length is " << MAX_ENV_VAR_LENGTH;
return std::make_pair(std::string(), error_message_stream.str());
}

return std::make_pair(env_value, std::string());
}

#endif
14 changes: 11 additions & 3 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,18 @@ void init_debug_log()
std::lock_guard<std::mutex> lock(log_init_mutex);
if (!debug_log_initialized)
{
auto log_level = get_env_variable(ENV_AZDCAP_DEBUG_LOG);
if (!log_level.empty())
auto log_level = get_env_variable_no_log(ENV_AZDCAP_DEBUG_LOG);
if (!log_level.first.empty() && log_level.second.empty())
{
enable_debug_logging(log_level);
enable_debug_logging(log_level.first);
}

if (!log_level.second.empty())
{
printf(
"Azure Quote Provider: libdcap_quoteprov.so [%s]: %s\n",
log_level_string(SGX_QL_LOG_ERROR).c_str(),
log_level.second.c_str());
}
debug_log_initialized = true;
}
Expand Down

0 comments on commit 6b2be92

Please sign in to comment.