Skip to content

Commit 0d42153

Browse files
Arithmetic overflow in fragment size calculations (backport #5464
Signed-off-by: Eugenio Collado <[email protected]>
1 parent 411fc60 commit 0d42153

File tree

5 files changed

+157
-10
lines changed

5 files changed

+157
-10
lines changed

src/cpp/rtps/history/WriterHistory.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,16 @@ void WriterHistory::set_fragments(
358358
// If inlineqos for related_sample_identity is required, then remove its size from the final fragment size.
359359
if (0 < inline_qos_size)
360360
{
361-
final_high_mark_for_frag -= (
362-
fastdds::dds::ParameterSerializer<Parameter_t>::PARAMETER_SENTINEL_SIZE +
363-
inline_qos_size);
361+
uint32_t overhead = fastdds::dds::ParameterSerializer<Parameter_t>::PARAMETER_SENTINEL_SIZE + inline_qos_size;
362+
constexpr uint32_t min_fragment_size = 4;
363+
if (final_high_mark_for_frag < (overhead + min_fragment_size))
364+
{
365+
final_high_mark_for_frag = min_fragment_size;
366+
}
367+
else
368+
{
369+
final_high_mark_for_frag -= overhead;
370+
}
364371
}
365372

366373
// If it is big data, fragment it.

src/cpp/rtps/participant/RTPSParticipantImpl.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,20 +2573,22 @@ uint32_t RTPSParticipantImpl::getMaxDataSize()
25732573
uint32_t RTPSParticipantImpl::calculateMaxDataSize(
25742574
uint32_t length)
25752575
{
2576-
uint32_t maxDataSize = length;
2577-
2576+
// RTPS header
2577+
uint32_t overhead = RTPSMESSAGE_HEADER_SIZE;
25782578
#if HAVE_SECURITY
25792579
// If there is rtps messsage protection, reduce max size for messages,
25802580
// because extra data is added on encryption.
25812581
if (security_attributes_.is_rtps_protected)
25822582
{
2583-
maxDataSize -= m_security_manager.calculate_extra_size_for_rtps_message();
2583+
overhead += m_security_manager.calculate_extra_size_for_rtps_message();
25842584
}
25852585
#endif // if HAVE_SECURITY
25862586

2587-
// RTPS header
2588-
maxDataSize -= RTPSMESSAGE_HEADER_SIZE;
2589-
return maxDataSize;
2587+
if (length <= overhead)
2588+
{
2589+
return 0;
2590+
}
2591+
return length - overhead;
25902592
}
25912593

25922594
bool RTPSParticipantImpl::networkFactoryHasRegisteredTransports() const

test/blackbox/common/DDSBlackboxTestsListeners.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2942,7 +2942,7 @@ TEST(DDSStatus, sample_rejected_waitset)
29422942
.disable_heartbeat_piggyback(true)
29432943
.asynchronously(eprosima::fastdds::dds::PublishModeQosPolicyKind::ASYNCHRONOUS_PUBLISH_MODE)
29442944
.add_throughput_controller_descriptor_to_pparams( // Be sure are sent in separate submessage each DATA.
2945-
eprosima::fastdds::rtps::FlowControllerSchedulerPolicy::FIFO, 100, 50)
2945+
eprosima::fastdds::rtps::FlowControllerSchedulerPolicy::FIFO, 300, 300)
29462946
.init();
29472947

29482948
reader.history_kind(eprosima::fastdds::dds::KEEP_ALL_HISTORY_QOS)

test/unittest/rtps/history/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ set(TOPICPAYLOADPOOLTESTS_SOURCE
8484
${PROJECT_SOURCE_DIR}/src/cpp/utils/IPLocator.cpp
8585
${PROJECT_SOURCE_DIR}/src/cpp/utils/SystemInfo.cpp)
8686

87+
set(WRITERHISTORYTESTS_SOURCE WriterHistoryTests.cpp)
88+
8789
if(WIN32)
8890
add_definitions(-D_WIN32_WINNT=0x0601)
8991
endif()
@@ -164,3 +166,20 @@ target_link_libraries(TopicPayloadPoolTests
164166
GTest::gtest
165167
${CMAKE_DL_LIBS})
166168
gtest_discover_tests(TopicPayloadPoolTests)
169+
170+
171+
172+
add_executable(WriterHistoryTests ${WRITERHISTORYTESTS_SOURCE})
173+
target_compile_definitions(WriterHistoryTests PRIVATE
174+
BOOST_ASIO_STANDALONE
175+
ASIO_STANDALONE
176+
$<$<AND:$<NOT:$<BOOL:${WIN32}>>,$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">>:__DEBUG>
177+
$<$<BOOL:${INTERNAL_DEBUG}>:__INTERNALDEBUG> # Internal debug activated.
178+
)
179+
target_link_libraries(WriterHistoryTests
180+
fastcdr
181+
fastdds
182+
foonathan_memory
183+
GTest::gtest
184+
${CMAKE_DL_LIBS})
185+
gtest_discover_tests(WriterHistoryTests)
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright 2020 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <gtest/gtest.h>
16+
17+
#include <fastdds/rtps/RTPSDomain.hpp>
18+
#include <fastdds/rtps/participant/RTPSParticipant.hpp>
19+
#include <fastdds/rtps/writer/RTPSWriter.hpp>
20+
#include <fastdds/rtps/history/IPayloadPool.hpp>
21+
#include <fastdds/rtps/history/WriterHistory.hpp>
22+
23+
24+
namespace eprosima {
25+
namespace fastdds {
26+
namespace rtps {
27+
28+
using namespace testing;
29+
30+
#define MAX_MESSAGE_SIZE 300
31+
32+
void cache_change_fragment(
33+
uint32_t max_message_size,
34+
uint32_t inline_qos_length,
35+
bool expected_fragmentation)
36+
{
37+
uint32_t domain_id = 0;
38+
uint32_t initial_reserved_caches = 10;
39+
std::string max_message_size_str = std::to_string(max_message_size);
40+
41+
RTPSParticipantAttributes p_attr;
42+
p_attr.properties.properties().emplace_back("fastdds.max_message_size", max_message_size_str);
43+
RTPSParticipant* participant = RTPSDomain::createParticipant(
44+
domain_id, true, p_attr);
45+
46+
ASSERT_NE(participant, nullptr);
47+
48+
HistoryAttributes h_attr;
49+
h_attr.memoryPolicy = DYNAMIC_RESERVE_MEMORY_MODE;
50+
h_attr.initialReservedCaches = initial_reserved_caches;
51+
h_attr.payloadMaxSize = 250;
52+
WriterHistory* history = new WriterHistory(h_attr);
53+
54+
WriterAttributes w_attr;
55+
RTPSWriter* writer = RTPSDomain::createRTPSWriter(participant, w_attr, history);
56+
57+
ASSERT_NE(writer, nullptr);
58+
59+
CacheChange_t* change = history->create_change(ALIVE);
60+
if (expected_fragmentation)
61+
{
62+
change->serializedPayload.length = 3 * max_message_size;
63+
}
64+
else
65+
{
66+
change->serializedPayload.length = max_message_size / 3;
67+
}
68+
change->inline_qos.length = inline_qos_length;
69+
history->add_change(change);
70+
71+
auto result = change->getFragmentSize();
72+
std::cout << "Fragment size: " << result << std::endl;
73+
if (expected_fragmentation)
74+
{
75+
ASSERT_NE(result, 0);
76+
}
77+
else
78+
{
79+
ASSERT_EQ(result, 0);
80+
}
81+
}
82+
83+
/**
84+
* This test checks the get_max_allowed_payload_size() method of the BaseWriter class.
85+
* When setting the RTPS Participant Attribute property fastdds.max_message_size to a value lower than the
86+
* message overhead, if the method does not overflow the fragment size will be set.
87+
* If the max_message_size is big enough for the overhead, inline_qos and serializedPayload,
88+
* then no fragmentation will occur.
89+
*/
90+
TEST(WriterHistoryTests, get_max_allowed_payload_size_overflow)
91+
{
92+
cache_change_fragment(100, 0, true);
93+
cache_change_fragment(MAX_MESSAGE_SIZE, 0, false);
94+
}
95+
96+
/**
97+
* This test checks the fragment size calculation for a cache change depending on the inline qos length.
98+
* The change.serializedPayload.length is set to 3 times the max_allowed_payload_size, so the fragment size should always be set.
99+
* In case of an overflow in the attribute high_mark_for_frag_ the fragment size will not be set, which is an error.
100+
*/
101+
TEST(WriterHistoryTests, final_high_mark_for_frag_overflow)
102+
{
103+
for (uint32_t inline_qos_length = 0; inline_qos_length < MAX_MESSAGE_SIZE; inline_qos_length += 40)
104+
{
105+
cache_change_fragment(MAX_MESSAGE_SIZE, inline_qos_length, true);
106+
}
107+
}
108+
109+
} // namespace rtps
110+
} // namespace fastdds
111+
} // namespace eprosima
112+
113+
int main(
114+
int argc,
115+
char** argv)
116+
{
117+
testing::InitGoogleTest(&argc, argv);
118+
return RUN_ALL_TESTS();
119+
}

0 commit comments

Comments
 (0)