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

Enable memory protection on DataSharing readers [13457] (backport #2405) #2475

Merged
merged 2 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void LoanableHelloWorldSubscriber::SubListener::on_subscription_matched(
void LoanableHelloWorldSubscriber::SubListener::on_data_available(
DataReader* reader)
{
FASTDDS_SEQUENCE(DataSeq, LoanableHelloWorld);
FASTDDS_CONST_SEQUENCE(DataSeq, LoanableHelloWorld);

DataSeq data;
SampleInfoSeq infos;
Expand Down
7 changes: 5 additions & 2 deletions include/fastdds/dds/core/LoanableSequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <cassert>
#include <cstdint>
#include <vector>
#include <type_traits>

#include <fastdds/dds/core/LoanableTypedCollection.hpp>
#include <fastdds/dds/log/Log.hpp>
Expand Down Expand Up @@ -57,8 +58,8 @@ namespace dds {
* the sequence is loaning the buffer. The sequence should not be destroyed until the loan is returned.
* @li A sequence with a zero maximum always has has_ownership == true
*/
template<typename T>
class LoanableSequence : public LoanableTypedCollection<T>
template<typename T, typename _NonConstEnabler = std::true_type>
class LoanableSequence : public LoanableTypedCollection<T, _NonConstEnabler>
{
public:

Expand Down Expand Up @@ -226,5 +227,7 @@ class LoanableSequence : public LoanableTypedCollection<T>

// Macro to easily declare a LoanableSequence for a data type
#define FASTDDS_SEQUENCE(FooSeq, Foo) using FooSeq = eprosima::fastdds::dds::LoanableSequence<Foo>
#define FASTDDS_CONST_SEQUENCE(FooSeq, Foo) using FooSeq = eprosima::fastdds::dds::LoanableSequence<Foo, \
std::false_type>

#endif // _FASTDDS_DDS_CORE_LOANABLESEQUENCE_HPP_
6 changes: 4 additions & 2 deletions include/fastdds/dds/core/LoanableTypedCollection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <cassert>
#include <cstdint>
#include <stdexcept>
#include <type_traits>

#include <fastdds/dds/core/LoanableCollection.hpp>

Expand All @@ -34,7 +35,7 @@ namespace dds {
*
* This is an abstract class. See @ref LoanableSequence for details.
*/
template<typename T>
template<typename T, typename _NonConstEnabler = std::true_type>
class LoanableTypedCollection : public LoanableCollection
{
public:
Expand All @@ -54,7 +55,8 @@ class LoanableTypedCollection : public LoanableCollection
*
* @return a reference to the element at position @c n
*/
T& operator [](
template <typename Enabler = _NonConstEnabler>
typename std::enable_if<Enabler::value, T>::type& operator [](
size_type n)
{
if (n >= length_)
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/rtps/DataSharing/ReaderPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ReaderPool : public DataSharingPayloadPool
try
{
segment_ = std::unique_ptr<fastdds::rtps::SharedMemSegment>(
new fastdds::rtps::SharedMemSegment(boost::interprocess::open_only,
new fastdds::rtps::SharedMemSegment(boost::interprocess::open_read_only,
segment_name_.c_str()));
}
catch (const std::exception& e)
Expand Down
9 changes: 9 additions & 0 deletions src/cpp/utils/shared_memory/SharedMemSegment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ class SharedMemSegment
new managed_shared_memory_type(boost::interprocess::open_only, name.c_str()));
}

SharedMemSegment(
boost::interprocess::open_read_only_t,
const std::string& name)
: name_(name)
{
segment_ = std::unique_ptr<managed_shared_memory_type>(
new managed_shared_memory_type(boost::interprocess::open_read_only, name.c_str()));
}

SharedMemSegment(
boost::interprocess::open_or_create_t,
const std::string& name,
Expand Down