Fast-DDS icon indicating copy to clipboard operation
Fast-DDS copied to clipboard

Segfault when adding annotation to dynamic type [13595]

Open evading opened this issue 3 years ago • 1 comments

Expected Behavior

It should be possible to add annotations when building dynamic types.

Current Behavior

Adding builder->apply_annotation("Foo", "Bar", "Baz"); to an otherwise working example leads to segfault when trying to run.

Steps to Reproduce

// main.cpp
#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastrtps/types/DynamicDataFactory.h>
#include <fastrtps/types/DynamicTypeBuilderFactory.h>
#include <fastrtps/types/DynamicTypeBuilderPtr.h>

using namespace eprosima::fastdds::dds;
using namespace eprosima::fastrtps::rtps;
using namespace eprosima::fastrtps::types;

class Annotations {

public:
    Annotations();
    void create_type();
    void create_publisher(void);

private:
    eprosima::fastdds::dds::DomainParticipant* m_dp = nullptr;
    eprosima::fastrtps::types::DynamicData_ptr m_data;
    eprosima::fastdds::dds::Topic* m_topic = nullptr;
    eprosima::fastdds::dds::DataWriter* m_writer = nullptr;
    eprosima::fastdds::dds::Publisher* m_publisher;
};

Annotations::Annotations()
{
    auto dp_qos = DomainParticipantQos {};

    auto config = &dp_qos.wire_protocol().builtin.discovery_config;
    config->discoveryProtocol = DiscoveryProtocol_t::SIMPLE;
    config->use_SIMPLE_EndpointDiscoveryProtocol = true;
    config->m_simpleEDP.use_PublicationReaderANDSubscriptionWriter = true;
    config->m_simpleEDP.use_PublicationWriterANDSubscriptionReader = true;
    config->leaseDuration = eprosima::fastrtps::c_TimeInfinite;
    dp_qos.name("Annotations Participant");

    m_dp = DomainParticipantFactory::get_instance()->create_participant(0, dp_qos, nullptr);
}

void Annotations::create_type()
{
    const auto type_name = "TypeName";
    DynamicTypeBuilder_ptr bldr = DynamicTypeBuilderFactory::get_instance()->create_struct_builder();

    bldr->set_name(type_name);

    bldr->add_member(
        0, "IntMember",
        DynamicTypeBuilderFactory::get_instance()->create_int32_type());

    bldr->add_member(
        1, "StringMember",
        DynamicTypeBuilderFactory::get_instance()->create_string_type());

    //bldr->apply_annotation("Foo", "Bar", "Baz");

    auto dyn_type = bldr->build();

    m_data = DynamicDataFactory::get_instance()->create_data(dyn_type);

    auto t = TypeSupport { new DynamicPubSubType { dyn_type } };
    t.get()->auto_fill_type_information(true);
    t.get()->auto_fill_type_object(true);

    t.register_type(m_dp);

    m_topic = m_dp->create_topic("Topic", type_name, TOPIC_QOS_DEFAULT);
}

void Annotations::create_publisher(void)
{
    m_publisher = this->m_dp->create_publisher(PUBLISHER_QOS_DEFAULT, nullptr);

    auto param_w_qos = DataWriterQos {};
    param_w_qos.history().kind = eprosima::fastrtps::KEEP_ALL_HISTORY_QOS;
    param_w_qos.history().depth = 20;
    param_w_qos.reliability().kind = eprosima::fastrtps::RELIABLE_RELIABILITY_QOS;
    param_w_qos.durability().kind = eprosima::fastrtps::TRANSIENT_LOCAL_DURABILITY_QOS;

    m_writer = m_publisher->create_datawriter(
        this->m_topic,
        param_w_qos,
        nullptr);

    auto val_id = this->m_data->get_member_id_by_name("IntMember");
    this->m_data->set_int32_value(42, val_id);
    val_id = this->m_data->get_member_id_by_name("StringMember");
    this->m_data->set_string_value("FooBarBaz", val_id);

    m_writer->write(this->m_data.get());
}

int main(void)
{
    Annotations a;
    a.create_type();
    a.create_publisher();

    std::cin.ignore();
    return 0;
}

# Makefile
CXXFLAGS += -std=c++17
LDLIBS += -L/usr/local/lib -lfastrtps

all: main

.PHONY: clean
clean:
	rm -f main

Uncommenting //bldr->apply_annotation("Foo", "Bar", "Baz"); leads to segfault when trying to run.

System information

  • Fast-DDS version: 70e2ad09001cbe92eea7bfe66830099b96b6f7d4
  • OS: Linux lnx 4.19.0-14-amd64 #1 SMP Debian 4.19.171-2 (2021-01-30) x86_64 GNU/Linux

Additional context

Additional resources

Starting program: /home/einar/tmp/delme/main
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff6550700 (LWP 9094)]
[New Thread 0x7ffff5d4f700 (LWP 9095)]
[New Thread 0x7ffff554e700 (LWP 9096)]
[New Thread 0x7ffff4d4d700 (LWP 9097)]
[New Thread 0x7ffff454c700 (LWP 9098)]
[New Thread 0x7ffff3d4b700 (LWP 9099)]
[New Thread 0x7ffff354a700 (LWP 9100)]
[New Thread 0x7ffff2c49700 (LWP 9101)]

Thread 1 "main" received signal SIGSEGV, Segmentation fault.
0x00007ffff798435a in eprosima::fastrtps::types::TypeIdentifier::operator= (this=0x7fffffff85e0, x=...) at /home/einar/repos/Fast-DDS/src/cpp/dynamic-types/TypeIdentifier.cpp:147
147         m__d = x.m__d;
(gdb) bt
#0  0x00007ffff798435a in eprosima::fastrtps::types::TypeIdentifier::operator= (this=0x7fffffff85e0, x=...) at /home/einar/repos/Fast-DDS/src/cpp/dynamic-types/TypeIdentifier.cpp:147
#1  0x00007ffff796cc17 in eprosima::fastrtps::types::AppliedAnnotation::annotation_typeid (this=0x7fffffff85e0, _annotation_typeid=...)
    at /home/einar/repos/Fast-DDS/include/fastrtps/types/AnnotationParameterValue.h:1036
#2  0x00007ffff796aa9f in eprosima::fastrtps::types::DynamicTypeBuilderFactory::apply_type_annotations (this=0x55555563a5c0, annotations=std::vector of length 0, capacity 0,
    descriptor=0x5555556a6380) at /home/einar/repos/Fast-DDS/src/cpp/dynamic-types/DynamicTypeBuilderFactory.cpp:2747
#3  0x00007ffff7962677 in eprosima::fastrtps::types::DynamicTypeBuilderFactory::build_struct_type_code (this=0x55555563a5c0, descriptor=0x5555556a6380, object=...,
    members=std::vector of length 2, capacity 2 = {...}, complete=true) at /home/einar/repos/Fast-DDS/src/cpp/dynamic-types/DynamicTypeBuilderFactory.cpp:1677
#4  0x00007ffff795dbd9 in eprosima::fastrtps::types::DynamicTypeBuilderFactory::build_type_object (this=0x55555563a5c0, descriptor=0x5555556a6380, object=..., members=0x7fffffffdda0,
    complete=true, force=false) at /home/einar/repos/Fast-DDS/src/cpp/dynamic-types/DynamicTypeBuilderFactory.cpp:1124
#5  0x00007ffff7874b7d in eprosima::fastdds::dds::DomainParticipantImpl::register_dynamic_type_to_factories (this=0x5555555ef000, type=...)
    at /home/einar/repos/Fast-DDS/src/cpp/fastdds/domain/DomainParticipantImpl.cpp:1079
#6  0x00007ffff787481f in eprosima::fastdds::dds::DomainParticipantImpl::register_type (this=0x5555555ef000, type=..., type_name="TypeName")
    at /home/einar/repos/Fast-DDS/src/cpp/fastdds/domain/DomainParticipantImpl.cpp:1053
#7  0x00007ffff788c025 in eprosima::fastdds::dds::DomainParticipant::register_type (this=0x5555555eefb0, type=..., type_name="TypeName")
    at /home/einar/repos/Fast-DDS/src/cpp/fastdds/domain/DomainParticipant.cpp:401
#8  0x00007ffff784737b in eprosima::fastdds::dds::TypeSupport::register_type (this=0x7fffffffe370, participant=0x5555555eefb0)
    at /home/einar/repos/Fast-DDS/src/cpp/fastdds/topic/TypeSupport.cpp:39
#9  0x000055555557ca74 in Annotations::create_type() ()
#10 0x000055555557d096 in main ()
(gdb) p &x
$1 = (const eprosima::fastrtps::types::TypeIdentifier *) 0x0
(gdb)

evading avatar Mar 05 '21 08:03 evading

There are several known issues related with Dynamic Types in Fast DDS. Thus, a refactor is being undertaken in order to fix the Dynamic Types implementation. It is expected to be released with Fast DDS v2.11.0 scheduled for June 2023.

JLBuenoLopez avatar Apr 14 '23 07:04 JLBuenoLopez