aws / aws-sdk-cpp

AWS SDK for C++
Apache License 2.0
1.96k stars 1.05k forks source link

LexRuntimeService segmentation fault #684

Closed kowalikm closed 4 years ago

kowalikm commented 7 years ago

Hello, I'm using gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4). Trying to build Application using Kitt.ai Snowboy Hotword Detection and AWS Lex.

When I build demo lex application - everything seems to be ok (it returns valid intent):

int main(int argc, char *argv[]) {

    if(argc != 2) {
        std::cerr << "Wrong args!" << std::endl;
        return -1;
    }
    Aws::SDKOptions options;
    Aws::InitAPI(options);

    Aws::LexRuntimeService::LexRuntimeServiceClient client;
    Aws::LexRuntimeService::Model::PostContentRequest request;
    request.SetBotName("SmartCook");
    request.SetBotAlias("$LATEST");
    request.SetUserId("***");
    request.SetContentType("audio/l16; rate=16000; channels=1");
    auto body = Aws::MakeShared<Aws::FStream>("command.tmp", "command.tmp", std::ios::in | std::ios::binary);

    if(!body) {
        std::cerr << std::string("Cannot open file ") + std::string(argv[1])  + std::string("!") << std::endl;
        return -2;
    } else {
        body->seekg(0, std::ios::end);
        unsigned long size = body->tellg();
        body->seekg(0);
        std::cout << "Body length: " << size << std::endl;
    }

    request.SetBody(body);

    auto outcome = client.PostContent(request);
    Aws::LexRuntimeService::Model::PostContentResult& result = outcome.GetResult();
    auto error = outcome.GetError();

    std::cout << "PostContent done: " << outcome.IsSuccess() << std::endl;
    std::cerr << "Error: " << error.GetMessage() << std::endl;
    std::cout << "Intent: " << result.GetIntentName() << std::endl;
    std::cout << "Slots: " << result.GetSlots() << std::endl;
    Aws::ShutdownAPI(options);

    return 0;
}

But when I use same code with my other application I get following SIGSEGV:

Thread #1 [SmartVoice] 11001 [core: 1] (Suspended : Signal : SIGSEGV:Segmentation fault)
gnu_cxx::exchange_and_add() at atomicity.h:49 0x409e4e
gnu_cxx::
exchange_and_add_dispatch() at atomicity.h:82 0x409ee5 std::_Sp_counted_base<(gnu_cxx::_Lock_policy)2>::_M_release at shared_ptr_base.h:147 0x40bc8d std::__shared_count<(gnu_cxx::_Lock_policy)2>::~shared_count at shared_ptr_base.h:659 0x40b435
std::__shared_ptr<Aws::Utils::RateLimits::RateLimiterInterface, (
gnu_cxx::_Lock_policy)2>::~shared_ptr at shared_ptr_base.h:925 0x40a9c0
std::shared_ptr::~shared_ptr() at shared_ptr.h:93 0x40a9dc
Aws::Client::ClientConfiguration::~ClientConfiguration() at ClientConfiguration.h:48 0x40a9fe
Aws::Internal::AWSHttpResourceClient::AWSHttpResourceClient() at 0x7ffff7371872 Aws::Internal::EC2MetadataClient::EC2MetadataClient() at 0x7ffff7371ee3 void
gnu_cxx::new_allocator::construct() at 0x7ffff737bfb5
std::enable_if<std::and_<std::allocator_traits<Aws::Allocator >::construct_helper::type>::value, void>::type std::allocator_traits<Aws::Allocator >::_S_construct() at 0x7ffff737bd67
decltype (_S_construct({parm#1}, {parm#2})) std::allocator_traits<Aws::Allocator >::construct at 0x7ffff737bb2b std::_Sp_counted_ptr_inplace<Aws::Internal::EC2MetadataClient, Aws::Allocator, (gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<> at 0x7ffff737b7c3
std::__shared_count<(
gnu_cxx::_Lock_policy)2>::shared_count<Aws::Internal::EC2MetadataClient, Aws::Allocator> at 0x7ffff737b25c
std::__shared_ptr<Aws::Internal::EC2MetadataClient, (
gnu_cxx::_Lock_policy)2>::shared_ptr<Aws::Allocator> at 0x7ffff737aea8
std::shared_ptr::shared_ptr<Aws::Allocator>() at 0x7ffff737a826 std::shared_ptr std::allocate_shared<Aws::Internal::EC2MetadataClient, Aws::Allocator>() at 0x7ffff7379bfe
std::shared_ptr Aws::MakeShared() at 0x7ffff7379050 Aws::Config::EC2InstanceProfileConfigLoader::EC2InstanceProfileConfigLoader() at 0x7ffff7376e1c void
gnu_cxx::new_allocator::construct() at 0x7ffff73547b6
std::enable_if<std::and_<std::allocator_traits<Aws::Allocator >::construct_helper::type>::value, void>::type std::allocator_traits<Aws::Allocator >::_S_construct() at 0x7ffff73544e5
decltype (_S_construct({parm#1}, {parm#2})) std::allocator_traits<Aws::Allocator >::construct at 0x7ffff7354107
std::_Sp_counted_ptr_inplace<Aws::Config::EC2InstanceProfileConfigLoader, Aws::Allocator, (gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<> at 0x7ffff7353b27 std::__shared_count<(gnu_cxx::_Lock_policy)2>::shared_count<Aws::Config::EC2InstanceProfileConfigLoader, Aws::Allocator> at 0x7ffff73534d4 std::__shared_ptr<Aws::Config::EC2InstanceProfileConfigLoader, (gnu_cxx::_Lock_policy)2>::shared_ptr<Aws::Allocator> at 0x7ffff7353088 std::shared_ptr::shared_ptr<Aws::Allocator>() at 0x7ffff7352d76
std::shared_ptr std::allocate_shared<Aws::Config::EC2InstanceProfileConfigLoader, Aws::Allocator>() at 0x7ffff7352738 std::shared_ptr Aws::MakeShared() at 0x7ffff7351ffe
Aws::Auth::InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider() at 0x7ffff7350713
void
gnu_cxx::new_allocator::construct() at 0x7ffff7361c4c
std::enable_if<std::and_<std::allocator_traits<Aws::Allocator >::construct_helper::type>::value, void>::type std::allocator_traits<Aws::Allocator >::_S_construct() at 0x7ffff7361a7d
decltype (_S_construct({parm#1}, {parm#2})) std::allocator_traits<Aws::Allocator >::construct at 0x7ffff7361687
std::_Sp_counted_ptr_inplace<Aws::Auth::InstanceProfileCredentialsProvider, Aws::Allocator, (gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<> at 0x7ffff7360f7d std::__shared_count<(gnu_cxx::_Lock_policy)2>::shared_count<Aws::Auth::InstanceProfileCredentialsProvider, Aws::Allocator> at 0x7ffff736068e std::__shared_ptr<Aws::Auth::InstanceProfileCredentialsProvider, (gnu_cxx::_Lock_policy)2>::shared_ptr<Aws::Allocator> at 0x7ffff73600b0 std::shared_ptr::shared_ptr<Aws::Allocator>() at 0x7ffff735fc4a
std::shared_ptr std::allocate_shared<Aws::Auth::InstanceProfileCredentialsProvider, Aws::Allocator>() at 0x7ffff735f78e
std::shared_ptr Aws::MakeShared() at 0x7ffff735f14d
Aws::Auth::DefaultAWSCredentialsProviderChain::DefaultAWSCredentialsProviderChain() at 0x7ffff735ea9f
void
gnu_cxx::new_allocator::construct() at 0x7ffff6ecd446
std::enable_if<std::and_<std::allocator_traits<Aws::Allocator >::construct_helper::type>::value, void>::type std::allocator_traits<Aws::Allocator >::_S_construct() at 0x7ffff6ecce09
decltype (_S_construct({parm#1}, {parm#2})) std::allocator_traits<Aws::Allocator >::construct at 0x7ffff6ecc423
std::_Sp_counted_ptr_inplace<Aws::Auth::DefaultAWSCredentialsProviderChain, Aws::Allocator, (gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<> at 0x7ffff6ecb687 std::__shared_count<(gnu_cxx::_Lock_policy)2>::shared_count<Aws::Auth::DefaultAWSCredentialsProviderChain, Aws::Allocator> at 0x7ffff6eca9e2 std::__shared_ptr<Aws::Auth::DefaultAWSCredentialsProviderChain, (gnu_cxx::_Lock_policy)2>::__shared_ptr<Aws::Allocator> at 0x7ffff6ec9bae std::shared_ptr::shared_ptr<Aws::Allocator>() at 0x7ffff6ec8cf2
std::shared_ptr std::allocate_shared<Aws::Auth::DefaultAWSCredentialsProviderChain, Aws::Allocator>() at 0x7ffff6ec7118
std::shared_ptr Aws::MakeShared() at 0x7ffff6ec50d9
Aws::LexRuntimeService::LexRuntimeServiceClient::LexRuntimeServiceClient() at 0x7ffff6ebb555
SmartVoice::AwsLexWrapper::recognize() at AwsLexWrapper.cpp:32 0x40a07a SmartVoice::CommandDetector::commandSilenceDetected() at CommandDetector.cpp:115 0x40eea9
SmartVoice::CommandDetector::startDetection() at CommandDetector.cpp:57 0x40eaa6
main() at main.cpp:26 0x413602

kowalikm commented 6 years ago

Hello, I tried to build application again with QtFramework. Here is code:

lexwrapper.h

#ifndef LEXWRAPPER_H
#define LEXWRAPPER_H

#include <QObject>
#include <aws/core/Aws.h>

class LexWrapper : public QObject
{
    Q_OBJECT
public:
    explicit LexWrapper(QObject *parent = nullptr);

    ~LexWrapper();

signals:

public slots:
    void recognizeCommand(const QString& path);

private:
    Aws::SDKOptions m_options;
};

#endif // LEXWRAPPER_H

lexwrapper.cpp

#include "lexwrapper.h"

#include <aws/lex/LexRuntimeServiceClient.h>
#include <aws/lex/model/PostContentRequest.h>
#include <aws/core/utils/Outcome.h>
#include <fstream>

#include <QDebug>

LexWrapper::LexWrapper(QObject *parent)
    : QObject(parent)
{
    Aws::InitAPI(m_options);
}

LexWrapper::~LexWrapper()
{
    Aws::ShutdownAPI(m_options);
}

void LexWrapper::recognizeCommand(const QString &path)
{
    using namespace Aws::LexRuntimeService;

    QScopedPointer<Model::PostContentRequest> request(new Model::PostContentRequest);
    request->SetBotName("SmartCook");
    request->SetBotAlias("$LATEST");
    request->SetUserId("***");
    request->SetContentType("audio/l16; rate=16000; channels=1");

    auto body = Aws::MakeShared<Aws::FStream>("voice.tmp", path.toStdString(), std::ios::in | std::ios::binary);

    if(!body) {
        qCritical() << "Cannot open file stream: " << path;
        return ;
    }

    request->SetBody(body);

    QScopedPointer<LexRuntimeServiceClient> client(new LexRuntimeServiceClient);

    auto outcome = client->PostContent(*request);

    if(outcome.IsSuccess()) {
        Model::PostContentResult& result = outcome.GetResult();

        qDebug() << "PostContent success: Intent[" << result.GetIntentName().c_str()
                 << "] Slots[" << result.GetSlots().c_str() << "]";
    } else {
        auto error = outcome.GetError();

        qDebug() << "PostContent failed: Error[" << error.GetMessage().c_str() << "]";
    }
}

Here is usage of LexWrapper in my application:

void VoiceCommandDetector::commandSilenceDetected()
{
    if(m_isCommandSpeakingStarted) {
        qDebug() << "Speaking ended, no command recognized.";

        QFile tempFile("voice.tmp");
        tempFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
        tempFile.write(m_commandCache);
        tempFile.close();

        qDebug() << "Using Lex for NLU recognition...";

        QScopedPointer<LexWrapper> lexWrapper(new LexWrapper);
        lexWrapper->recognizeCommand(tempFile.fileName());

        qDebug() << "After lex recognition.";

        resetCommandRecognition();
    }
}

Used libs: -lsnowboy-detect -lcblas -latlas -laws-cpp-sdk-core -laws-cpp-sdk-lex

Tried on g++-4.8 and g++-5.4.0 (with _GLIBCXX_USE_CXX11_ABI=0)

Crashed in bits/basic_string.h:3121:

      // Capacity:
      ///  Returns the number of characters in the string, not including any
      ///  null-termination.
      size_type
      size() const _GLIBCXX_NOEXCEPT
      { return _M_rep()->_M_length; }  // <- HERE

Stack:

1 std::basic_string<char, std::char_traits, Aws::Allocator>::size basic_string.h 3121 0x40db52
2 std::basic_string<char, std::char_traits, Aws::Allocator>::assign basic_string.tcc 706 0x40d2c3
3 std::basic_string<char, std::char_traits, Aws::Allocator>::assign basic_string.h 3547 0x40cba2
4 Aws::LexRuntimeService::Model::PostContentRequest::SetBotName PostContentRequest.h 66 0x40c34b
5 LexWrapper::recognizeCommand lexwrapper.cpp 26 0x40b624
6 VoiceCommandDetector::commandSilenceDetected voicecommanddetector.cpp 80 0x40ab9d
7 VoiceCommandDetector::reciveData voicecommanddetector.cpp 36 0x40a7cb
8 QtPrivate::FunctorCall<QtPrivate::IndexesList<0>, QtPrivate::List<QByteArray const&>, void, void (VoiceCommandDetector:: )(QByteArray const&)>::call qobjectdefs_impl.h 136 0x40918a
9 QtPrivate::FunctionPointer<void (VoiceCommandDetector::
)(QByteArray const&)>::call<QtPrivate::List<QByteArray const&>, void> qobjectdefs_impl.h 169 0x4090ff
10 QtPrivate::QSlotObject<void (VoiceCommandDetector:: )(QByteArray const&), QtPrivate::List<QByteArray const&>, void>::impl qobject_impl.h 120 0x409069
11 QMetaObject::activate(QObject
, int, int, void ) 0x7f5e501bdb56 12 VoiceRecorder::chunkRecorded moc_voicerecorder.cpp 140 0x40fd64
13 VoiceRecorder::<lambda(QByteArray)>::operator()(QByteArray) const voicerecorder.cpp 36 0x409502
14 QtPrivate::FunctorCall<QtPrivate::IndexesList<0>, QtPrivate::List, void, VoiceRecorder::startRecording()::<lambda(QByteArray)>>::call(VoiceRecorder::<lambda(QByteArray)> &, void ) qobjectdefs_impl.h 130 0x409929
15 QtPrivate::Functor<VoiceRecorder::startRecording()::<lambda(QByteArray)>, 1>::call<QtPrivate::List, void>(VoiceRecorder::<lambda(QByteArray)> &, void , void ) qobjectdefs_impl.h 240 0x4098d9
16 QtPrivate::QFunctorSlotObject<VoiceRecorder::startRecording()::<lambda(QByteArray)>, 1, QtPrivate::List, void>::impl(int, QtPrivate::QSlotObjectBase
, QObject , void , bool ) qobject_impl.h 168 0x4098a7
17 QMetaObject::activate(QObject , int, int, void ) 0x7f5e501bdb56 18 VoiceDevice::dataWritten moc_voicedevice.cpp 127 0x410006
19 VoiceDevice::writeData voicedevice.cpp 31 0x409ff5
20 QIODevice::write(const char
, long long) 0x7f5e500b91ad 21 ?? 0x7f5e510daf86 22 ?? 0x7f5e510db581 23 QMetaObject::activate(QObject , int, int, void ) 0x7f5e501bd829 24 QTimer::timeout(QTimer::QPrivateSignal) 0x7f5e501ca0b7 25 QTimer::timerEvent(QTimerEvent ) 0x7f5e501ca3b8 26 QObject::event(QEvent ) 0x7f5e501be823 27 QCoreApplication::notifyInternal2(QObject , QEvent *) 0x7f5e5019139b 28 QTimerInfoList::activateTimers() 0x7f5e501e590e 29 ?? 0x7f5e501e61c9 30 g_main_context_dispatch 0x7f5e4c8e9197 31 ?? 0x7f5e4c8e93f0 32 g_main_context_iteration 0x7f5e4c8e949c 33 QEventDispatcherGlib::processEvents(QFlags) 0x7f5e501e653f 34 QEventLoop::exec(QFlags) 0x7f5e5018f3da 35 QCoreApplication::exec() 0x7f5e50198314 36 main main.cpp 20 0x408c2a

github-actions[bot] commented 4 years ago

Greetings! Sorry to say but this is a very old issue that is probably not getting as much attention as it deservers. We encourage you to check if this is still an issue in the latest release and if you find that this is still a problem, please feel free to open a new one.