Skip to content

Commit

Permalink
libguard: Adding user defined exceptions
Browse files Browse the repository at this point in the history
Changes:
-Adding user defined exceptions in libguard so that the caller
can distinguish the type of error at his end.
-Changes in respective UTs.

Test:
Created guard record, tried creating the same.
Deleting a non guarded record.
Ran UTs in docker env.

Signed-off-by: Chirag Sharma <[email protected]>
  • Loading branch information
Chirag Sharma committed Sep 3, 2021
1 parent f5f3937 commit 79cba70
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 48 deletions.
22 changes: 12 additions & 10 deletions libguard/guard_common.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include "guard_exception.hpp"
#include "guard_log.hpp"

#include <array>
#include <cstdint>
#include <stdexcept>
#include <optional>
#include <stdexcept>

namespace openpower
{
namespace guard
{

using namespace openpower::guard::exception;

/* From hostboot: src/include/usr/targeting/common/entitypath.H */
struct EntityPath
{
Expand Down Expand Up @@ -75,15 +79,15 @@ struct EntityPath
GUARD_ERROR,
"Size mismatch. Given buf size[%d] EntityPath sizeof[%d]",
rawData.size(), sizeof(EntityPath));
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath initializer_list constructor failed");
}

if (rawData.size() == 0)
{
openpower::guard::log::guard_log(GUARD_ERROR,
"Given raw data is empty");
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath initializer_list constructor failed");
}

Expand All @@ -93,7 +97,7 @@ struct EntityPath
{
openpower::guard::log::guard_log(
GUARD_ERROR, "PathElement size mismatch in given raw data");
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath initializer_list constructor failed");
}

Expand All @@ -107,7 +111,7 @@ struct EntityPath
openpower::guard::log::guard_log(
GUARD_ERROR,
"Insufficient data for PathElement in given raw data");
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath initializer_list constructor failed");
}

Expand All @@ -122,8 +126,7 @@ struct EntityPath
{
openpower::guard::log::guard_log(GUARD_ERROR,
"Given raw data is empty");
throw std::runtime_error(
"EntityPath conversion constructor failed");
throw InvalidEntityPath("EntityPath conversion constructor failed");
}

type_size = rawData[0];
Expand All @@ -135,8 +138,7 @@ struct EntityPath
GUARD_ERROR,
"Size mismatch. Given path elements size[%d] max[%d]",
pathElementsSize, maxPathElements);
throw std::runtime_error(
"EntityPath conversion constructor failed");
throw InvalidEntityPath("EntityPath conversion constructor failed");
}

for (int i = 0, j = 1; i < pathElementsSize;
Expand All @@ -147,7 +149,7 @@ struct EntityPath
openpower::guard::log::guard_log(
GUARD_ERROR,
"Insufficient data for PathElement in given raw data");
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath conversion constructor failed");
}

Expand Down
83 changes: 83 additions & 0 deletions libguard/guard_exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#pragma once

#include <exception>
#include <sstream>
#include <string>

namespace openpower
{
namespace guard
{
namespace exception
{
// TODO:Issue #3325, to avoid multiple exception classes
class GuardException : public std::exception
{
public:
explicit GuardException(const std::string& message) : message(message){};

const char* what() const noexcept override
{
return message.c_str();
}

private:
std::string message;
};

class GuardFileOpenFailed : public GuardException
{
public:
explicit GuardFileOpenFailed(const std::string& msg) :
GuardException(msg){};
};

class GuardFileReadFailed : public GuardException
{
public:
explicit GuardFileReadFailed(const std::string& msg) :
GuardException(msg){};
};

class GuardFileWriteFailed : public GuardException
{
public:
explicit GuardFileWriteFailed(const std::string& msg) :
GuardException(msg){};
};

class GuardFileSeekFailed : public GuardException
{
public:
explicit GuardFileSeekFailed(const std::string& msg) :
GuardException(msg){};
};

class InvalidEntry : public GuardException
{
public:
explicit InvalidEntry(const std::string& msg) : GuardException(msg){};
};

class AlreadyGuarded : public GuardException
{
public:
explicit AlreadyGuarded(const std::string& msg) : GuardException(msg){};
};

class InvalidEntityPath : public GuardException
{
public:
explicit InvalidEntityPath(const std::string& msg) : GuardException(msg){};
};

class GuardFileOverFlowed : public GuardException
{
public:
explicit GuardFileOverFlowed(const std::string& msg) :
GuardException(msg){};
};

} // namespace exception
} // namespace guard
} // namespace openpower
44 changes: 27 additions & 17 deletions libguard/guard_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "guard_file.hpp"

#include "guard_exception.hpp"
#include "guard_log.hpp"

#include <cstring>
Expand All @@ -12,6 +13,7 @@ namespace openpower
namespace guard
{
using namespace openpower::guard::log;
using namespace openpower::guard::exception;

GuardFile::GuardFile(const fs::path& file) : guardFile(file)
{
Expand All @@ -22,22 +24,27 @@ GuardFile::GuardFile(const fs::path& file) : guardFile(file)
{
guard_log(GUARD_ERROR,
"Failed to open the GUARD file during initailization");
throw std::runtime_error("Failed to get the GUARD file.");
throw GuardFileOpenFailed(
"Exception thrown as failed to open the guard file");
}
file.seekg(0, file.end);
if (file.fail())
{
throw std::runtime_error(
"Failed to move to the last position in the file");
guard_log(GUARD_ERROR,
"Failed to move to last position in guard file");
throw GuardFileSeekFailed(
"Exception thrown as failed to move to the "
"last position in the file");
}
fileSize = file.tellg();
}
catch (const std::exception& ex)
catch (openpower::guard::exception::GuardFileOpenFailed& ex)
{
guard_log(GUARD_ERROR,
"Error caught during initializing the GUARD file %s", ex.what());
throw std::runtime_error(
"Exception thrown during initialize GUARD file.");
throw GuardFileOpenFailed(ex.what());
}
catch (openpower::guard::exception::GuardFileSeekFailed& ex)
{
throw GuardFileSeekFailed(ex.what());
}
}

Expand All @@ -48,7 +55,8 @@ void GuardFile::read(const uint64_t pos, void* dst, const uint64_t len)
{
guard_log(GUARD_ERROR,
"Unable to open the guard file during read operation.");
throw std::runtime_error("Failed to open guard file in read function.");
throw GuardFileOpenFailed(
"Failed to open guard file in read function.");
}
file.seekg(pos, file.beg);
if (file.fail())
Expand All @@ -57,14 +65,16 @@ void GuardFile::read(const uint64_t pos, void* dst, const uint64_t len)
"Unable to move to the position in the guard file at"
" position= 0x%016llx",
pos);
throw std::runtime_error("Failed to move to the position in the file");
throw GuardFileSeekFailed(
"Failed to move"
"to the position during read operation in the guard file");
}
file.read(reinterpret_cast<char*>(dst), len);
if (file.fail())
{
guard_log(GUARD_ERROR,
"Unable to read from guard file at position= 0x%016llx", pos);
throw std::runtime_error("Failed to read from guard file.");
throw GuardFileReadFailed("Failed to read from guard file.");
}
return;
}
Expand All @@ -78,7 +88,7 @@ void GuardFile::write(const uint64_t pos, const void* src, const uint64_t len)
guard_log(
GUARD_ERROR,
"Unable to open guard file while perfoming the write operation");
throw std::runtime_error("Failed to open guard file to write");
throw GuardFileOpenFailed("Failed to open guard file to write");
}

file.seekp(pos, file.beg);
Expand All @@ -88,15 +98,15 @@ void GuardFile::write(const uint64_t pos, const void* src, const uint64_t len)
"Unable to move to the position in the guard file."
" Position= 0x%016llx",
pos);
throw std::runtime_error("Failed to move to the position in the guard file.");
throw GuardFileSeekFailed("Failed to move to the position during write "
"operation in the guard file.");
}

file.write(reinterpret_cast<const char*>(src), len);
if (file.fail())
{
guard_log(GUARD_ERROR,
"Unable to write the record to GUARD file.");
throw std::runtime_error("Failed to write to the guard file.");
guard_log(GUARD_ERROR, "Unable to write the record to GUARD file.");
throw GuardFileWriteFailed("Failed to write to the guard file.");
}
return;
}
Expand All @@ -109,7 +119,7 @@ void GuardFile::erase(const uint64_t pos, const uint64_t len)
if (len <= 0)
{
guard_log(GUARD_ERROR, "Length passed is %d which is not valid", len);
throw std::runtime_error("Not a valid length value");
throw InvalidEntry("Not a valid length value");
}

while (len - rlen > 0)
Expand Down
13 changes: 8 additions & 5 deletions libguard/guard_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "guard_common.hpp"
#include "guard_entity.hpp"
#include "guard_exception.hpp"
#include "guard_file.hpp"
#include "guard_log.hpp"
#include "include/guard_record.hpp"
Expand All @@ -21,6 +22,7 @@ namespace guard
{

using namespace openpower::guard::log;
using namespace openpower::guard::exception;

static fs::path guardFilePath = "";

Expand Down Expand Up @@ -75,7 +77,8 @@ const fs::path& getGuardFilePath()
{
if (guardFilePath.empty())
{
throw std::runtime_error(
guard_log(GUARD_ERROR, "Guard file is not initialised.");
throw GuardFileOpenFailed(
"Guard file is not initialised. "
"Please make sure libguard_init() is called already");
}
Expand Down Expand Up @@ -173,7 +176,7 @@ GuardRecord create(const EntityPath& entityPath, uint32_t eId, uint8_t eType)
guard_log(
GUARD_ERROR,
"Already guard record is available in the GUARD partition");
throw std::runtime_error("Guard record is already exist");
throw AlreadyGuarded("Guard record is already exist");
}
return getHostEndiannessRecord(existGuard);
}
Expand All @@ -197,7 +200,7 @@ GuardRecord create(const EntityPath& entityPath, uint32_t eId, uint8_t eType)
"Guard file size is %d and space remaining in GUARD file "
"is %d\n",
file.size(), avalSize);
throw std::runtime_error(
throw GuardFileOverFlowed(
"Enough size is not available in GUARD file");
}
// No space is left and have invalid record present. Hence using that
Expand Down Expand Up @@ -264,7 +267,7 @@ void clear(const EntityPath& entityPath)
if (!found)
{
guard_log(GUARD_ERROR, "Guard record not found");
throw std::runtime_error("Guard record not found");
throw InvalidEntityPath("Guard record not found");
}
}

Expand Down Expand Up @@ -292,7 +295,7 @@ void clear(const uint32_t recordId)
if (!found)
{
guard_log(GUARD_ERROR, "Guard record not found");
throw std::runtime_error("Guard record not found");
throw InvalidEntityPath("Guard record not found");
}
}

Expand Down
1 change: 1 addition & 0 deletions libguard/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ headers_libguard = [
'guard_entity.hpp',
'guard_log.hpp',
'guard_common.hpp',
'guard_exception.hpp',
]

headers = [
Expand Down
Loading

0 comments on commit 79cba70

Please sign in to comment.