aws / aws-iot-device-sdk-cpp-v2

Next generation AWS IoT Client SDK for C++ using the AWS Common Runtime
Apache License 2.0
185 stars 108 forks source link

Segmentation Fault - Aws::Crt::ApiHandle #676

Closed zlberto closed 10 months ago

zlberto commented 10 months ago

Describe the bug

In my C++ project, I have developed a custom class named AWSIoTCore that serves as a wrapper for the AWS IoT SDK. However, I'm encountering a segmentation fault when this class is used alongside libcurl. The issue only arises when AWSIoTCore.cpp (my custom wrapper implementation) is included in the build. Excluding this file from the build process seems to resolve the segmentation fault, suggesting a potential conflict or resource management issue within my wrapper when used with libcurl.

AWSIoTCore.h

#ifndef AWSIOTCORE_H
#define AWSIOTCORE_H

#include "logging/Logger.h" 

#include <aws/crt/Api.h>
#include <aws/iot/Mqtt5Client.h>

#include <aws/iotidentity/IotIdentityClient.h>
#include <aws/iotidentity/CreateKeysAndCertificateResponse.h>
#include <aws/iotidentity/RegisterThingResponse.h>

#include <string>
#include <memory>

class AWSIoTCore {
public:
    // Constructor
    AWSIoTCore(
        const std::string& serialNumber,
        const std::string& userId,
        Logger& logger
    );

    // Destructor
    ~AWSIoTCore();

    // Public methods
    void Mqtt5Connect();
    void Shutdown();
    void Sleep(int sleeptime);
    Aws::Iotidentity::CreateKeysAndCertificateResponse CreateKeysAndCertificate();
    Aws::Iotidentity::RegisterThingResponse RegisterThing(std::string thingName, std::string thingGroupName);

private:
    std::string serialNumber_;
    std::string userId_;
    Logger& logger_;

    int mqttPort_;
    std::string iotCredEndpoint_;
    std::string fleetProvisioningTemplateName_;
    std::string certPath_;
    std::string privateKeyPath_;
    std::string rootCaPath_;

    std::shared_ptr<Aws::Crt::ApiHandle apiHandle;
    std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> mqtt5Client;
    std::shared_ptr<Aws::Iotidentity::IotIdentityClient> identityClient;

    std::promise<bool> connectionPromise;
    std::promise<void> stoppedPromise;

    Aws::Iotidentity::CreateKeysAndCertificateResponse createKeysAndCertificateResponse_;
    Aws::Iotidentity::RegisterThingResponse registerThingResponse_;
};

#endif // AWSIOTCORE_H

AWSIoTCore.cpp

#include "AWSIoTCore/AWSIoTCore.h"
#include "ConfigData.h" 

#include <aws/crt/Api.h>
#include <aws/crt/JsonObject.h>
#include <aws/crt/UUID.h>
#include <aws/crt/io/HostResolver.h>
#include <aws/crt/mqtt/Mqtt5Packets.h>
#include <aws/iot/Mqtt5Client.h>
#include <aws/iot/MqttClient.h>

#include <aws/iotidentity/CreateCertificateFromCsrRequest.h>
#include <aws/iotidentity/CreateCertificateFromCsrResponse.h>
#include <aws/iotidentity/CreateCertificateFromCsrSubscriptionRequest.h>
#include <aws/iotidentity/CreateKeysAndCertificateRequest.h>
#include <aws/iotidentity/CreateKeysAndCertificateResponse.h>
#include <aws/iotidentity/CreateKeysAndCertificateSubscriptionRequest.h>
#include <aws/iotidentity/ErrorResponse.h>
#include <aws/iotidentity/IotIdentityClient.h>
#include <aws/iotidentity/RegisterThingRequest.h>
#include <aws/iotidentity/RegisterThingResponse.h>
#include <aws/iotidentity/RegisterThingSubscriptionRequest.h>

#include <yaml-cpp/yaml.h>
#include <iostream>
#include <thread>

using namespace Aws::Crt;
using namespace Aws::Iotidentity;
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono;      // nanoseconds, system_clock, seconds

AWSIoTCore::AWSIoTCore(
    const std::string& serialNumber,
    const std::string& userId,
    Logger& logger
) : 
    serialNumber_(serialNumber), 
    userId_(userId),
    logger_(logger) {

    this->mqttPort_ = 8883;
    this->mqtt5Client = nullptr;
    this->identityClient = nullptr;

    // Parse the embedded YAML string
    YAML::Node config = YAML::Load(configData);

    this->fleetProvisioningTemplateName_ = ""XXXXX";";
    this->iotCredEndpoint_ = config["services"]["aws.greengrass.Nucleus"]["configuration"]["iotDataEndpoint"].as<std::string>();
}

CMakeLists.txt

# Add executable
add_executable(${PROJECT_NAME} 
    main.cpp 
    AWSIoTCore/AWSIoTCore.cpp 
    Greengrass/Greengrass.cpp 
    logging/Logger.cpp
    ${SOURCE_OUTPUT_PATH}
)

Expected Behavior

The application should run without a segmentation fault, allowing for the simultaneous use of my AWS IoT SDK wrapper (AWSIoTCore) and libcurl.

Current Behavior

Segmentation fault occurs when AWSIoTCore.cpp is included in the build.

Reproduction Steps

1 - Compile and run the application with AWSIoTCore.cpp included. 2 - Observe the segmentation fault occurring.

Possible Solution

No response

Additional Information/Context

SDK version used

v1.32.1

Environment details (OS name and version, etc.)

Docker Ubuntu:22.04

jmklix commented 10 months ago

Can you provide logs and the stack trace from when you observe this seg fault?

github-actions[bot] commented 10 months ago

Greetings! It looks like this issue hasn’t been active in longer than a week. We encourage you to check if this is still an issue in the latest release. Because it has been longer than a week since the last update on this, and in the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please feel free to provide a comment or add an upvote to prevent automatic closure, or if the issue is already closed, please feel free to open a new one.