Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add xrt::elf raw elf data constructor #7963

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/runtime_src/core/common/api/xrt_elf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <stdexcept>
#include <string>
#include <vector>
#include <sstream>

namespace xrt {

Expand All @@ -28,6 +29,14 @@ class elf_impl
throw std::runtime_error(fnm + " is not found or is not a valid ELF file");
}

elf_impl(const std::vector<char>& data)
{
std::istringstream elf_stream;
elf_stream.rdbuf()->pubsetbuf(const_cast<char*>(data.data()), data.size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The const_cast looks evil here. Can you add a comment why we are doing it and the fact that elf_stream is input stream (read only) so it cannot accidentally change data.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since data is const type so data.data() return const char*
https://cplusplus.com/reference/vector/vector/data/
If the vector object is const-qualified, the function returns a pointer to const value_type. Otherwise, it returns a pointer to value_type.
while
streambuf* pubsetbuf (char* s, streamsize n);
https://cplusplus.com/reference/streambuf/streambuf/pubsetbuf/

if (!m_elf.load(elf_stream))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are copying the vector here. Can we write a simple adapter for std::vector to make it look like std::istream?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

throw std::runtime_error("not a valid ELF data");
}

const ELFIO::elfio&
get_elfio() const
{
Expand Down Expand Up @@ -85,6 +94,11 @@ elf(const std::string& fnm)
: detail::pimpl<elf_impl>{std::make_shared<elf_impl>(fnm)}
{}

elf::
elf(const std::vector<char>& data)
: detail::pimpl<elf_impl>{std::make_shared<elf_impl>(data)}
{}

xrt::uuid
elf::
get_cfg_uuid() const
Expand Down
13 changes: 12 additions & 1 deletion src/runtime_src/core/include/experimental/xrt_elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#ifdef __cplusplus
# include <string>
# include <vector>
#endif

#ifdef __cplusplus
Expand All @@ -32,7 +33,17 @@ class elf : public detail::pimpl<elf_impl>
public:
XRT_API_EXPORT
elf(const std::string& fnm);


/**
* elf() - Constructor from raw ELF data
*
* @param data
* Raw data of elf
*
*/
XRT_API_EXPORT
elf(const std::vector<char>& data);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A constructor from std::vector isn't great IMO. What if data is not held in a vector? A std::span would be better, but that's c++20. I would opt for const char*, size_t but you are still faced with the problem of how to create the istream without copying the in-memory buffer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stsoe
Updated the PR with changes discussed offline.


XRT_API_EXPORT
xrt::uuid
get_cfg_uuid() const;
Expand Down
Loading