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

Crash on deallocation of results from AWS service client calls in Unreal Engine 5.1 #2513

Closed
gamerdoku opened this issue May 27, 2023 · 10 comments
Assignees
Labels
bug This issue is a bug. closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days.

Comments

@gamerdoku
Copy link

Describe the bug

I am able to successfully build and link aws cpp sdk for Windows in Unreal Engine 5.1 .

Using CUSTOM_MEMORY_MANAGEMENT=OFF, as with that set ON, I am running into "Entry point not found issue".

I am using Cognito Identity to getId and getCredentialsForId call from cognito identity client.

Expected Behavior

Works without crashing the process.

Current Behavior

mi_free_generic(const mi_segment_s *,bool,void *) alloc.c:438
FMemory::Free(void *) FMemory.inl:80
[Inlined] operator delete(void *,unsigned long long) AWSSDK.cpp:27
[Inlined] std::_Deallocate(void *,unsigned long long) xmemory:255
[Inlined] std::allocator<char>::deallocate(char *const,const unsigned long long) xmemory:797
[Inlined] std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Tidy_deallocate() xstring:4588
std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> >() xstring:2969
Aws::CognitoIdentity::Model::GetCredentialsForIdentityResult::~GetCredentialsForIdentityResult() 0x00007ffc489a77cc
UGetAWSCredentials::GetCredentialsForIdentity(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > &,const std::basic_string<char,std::char_traits<char>,std::allocator<char> > &) GetAWSCredentials.cpp:185
<lambda_deb97d888ffea09318baa53d8f665ff9>::operator()() GetAWSCredentials.cpp:55
[Inlined] UE::Core::Private::Function::TFunctionRefBase<UE::Core::Private::Function::TFunctionStorage<1>,void __cdecl(void)>::operator()() Function.h:602
[Inlined] FAsyncGraphTask::DoTask(Type,const TRefCountPtr<FGraphEvent> &) Async.cpp:26
TGraphTask<FAsyncGraphTask>::ExecuteTask(TArray<FBaseGraphTask *,TSizedDefaultAllocator<32> > &,Type,bool) TaskGraphInterfaces.h:1348
[Inlined] FBaseGraphTask::Execute(TArray<FBaseGraphTask *,TSizedDefaultAllocator<32> > &,Type,bool) TaskGraphInterfaces.h:950
<lambda_13c427d0bfcf321a066cb5a2badfbc27>::operator()() TaskGraph.cpp:2051
[Inlined] LowLevelTasks::FTask::Init::__l15::<lambda_17c904c32264d0348d15245fba0e1bff>::operator()(const bool) Task.h:504
[Inlined] Invoke(LowLevelTasks::FTask::<lambda_17c904c32264d0348d15245fba0e1bff> &,bool &) Invoke.h:47
[Inlined] LowLevelTasks::TTaskDelegate<LowLevelTasks::FTask * __cdecl(bool),48>::TTaskDelegateImpl<<lambda_17c904c32264d0348d15245fba0e1bff>,0>::Call(void *,bool) TaskDelegate.h:162
LowLevelTasks::TTaskDelegate<LowLevelTasks::FTask * __cdecl(bool),48>::TTaskDelegateImpl<<lambda_17c904c32264d0348d15245fba0e1bff>,0>::CallAndMove(LowLevelTasks::TTaskDelegate<LowLevelTasks::FTask * __cdecl(bool),48> &,void *,unsigned int,bool) TaskDelegate.h:171
[Inlined] LowLevelTasks::TTaskDelegate<LowLevelTasks::FTask * __cdecl(bool),48>::CallAndMove(LowLevelTasks::TTaskDelegate<LowLevelTasks::FTask * __cdecl(bool),48> &,bool) TaskDelegate.h:308
LowLevelTasks::FTask::ExecuteTask() Task.h:656
LowLevelTasks::FScheduler::ExecuteTask(LowLevelTasks::FTask *&) Scheduler.cpp:162
LowLevelTasks::FScheduler::TryExecuteTaskFrom<&LowLevelTasks::TLocalQueueRegistry<1024>::TLocalQueue::DequeueGlobal,0>(LowLevelTasks::TLocalQueueRegistry<1024>::TLocalQueue *,LowLevelTasks::TLocalQueueRegistry<1024>::FOutOfWork &,bool,bool) Scheduler.cpp:360
LowLevelTasks::FScheduler::WorkerMain(LowLevelTasks::FSleepEvent *,LowLevelTasks::TLocalQueueRegistry<1024>::TLocalQueue *,unsigned int,bool) Scheduler.cpp:402
[Inlined] LowLevelTasks::FScheduler::CreateWorker::__l2::<lambda_d77f097a212b1e933c49dc814e6fab03>::operator()() Scheduler.cpp:90
[Inlined] Invoke(<lambda_d77f097a212b1e933c49dc814e6fab03> &) Invoke.h:47
UE::Core::Private::Function::TFunctionRefCaller<<lambda_d77f097a212b1e933c49dc814e6fab03>,void __cdecl(void)>::Call(void *) Function.h:474
[Inlined] UE::Core::Private::Function::TFunctionRefBase<UE::Core::Private::Function::TFunctionStorage<1>,void __cdecl(void)>::operator()() Function.h:602
FThreadImpl::Run() Thread.cpp:67
FRunnableThreadWin::Run() WindowsRunnableThread.cpp:146	
FRunnableThreadWin::GuardedRun() WindowsRunnableThread.cpp:71
<unknown> 0x00007ffcfc897614
<unknown> 0x00007ffcfd2a26a1

Reproduction Steps

Build Aws-cpp-sdk for windows using

cmake -S ../aws-sdk-cpp -B . /
      -DBUILD_SHARED_LIBS=ON /
      -DCMAKE_BUILD_TYPE=Release /
      -DENABLE_TESTING=OFF /
      -DCUSTOM_MEMORY_MANAGEMENT=OFF /
      -DCMAKE_CXX_STANDARD=11 /
      -DCPP_STANDARD=11 /
      -DCMAKE_INSTALL_PREFIX="./install_sdk_win64" -DBUILD_ONLY="core;access-management;identity-management;iam;cognito-identity;gamelift;s3;sts;sns;dynamodb;apigateway"

Integrate into Unreal Engine 5.1 as a plugin.
Make a call to cognito identity service getId.

Possible Solution

No response

Additional Information/Context

No response

AWS CPP SDK version used

1.11.67

Compiler and Version used

Visual Studio 17 2022

Operating System and version

Windows 10

@gamerdoku gamerdoku added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 27, 2023
@yasminetalby yasminetalby self-assigned this May 30, 2023
@yasminetalby
Copy link
Contributor

Hello @gamerdoku,

Thank you very much for your submission.

Could you please provide a sample code so i can best understand and reproduce your use case?
On a side note, you can refer to the AWS CPP SDK developer guide CMake parameters section for more information on how to use the CUSTOM_MEMORY_MANAGEMENT cmake parameter as well as Memory Management section which goes over Allocating and Deallocating Memory.

Thank you very much for your time and collaboration.

Sincerely,

Yasmine.

@yasminetalby yasminetalby added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. and removed needs-triage This issue or PR still needs to be triaged. labels May 30, 2023
@gamerdoku
Copy link
Author

I am unable to get aws cpp sdk to link in Unreal when building with CUSTOM_MEMORY_MANAGEMENT=ON similar to #2122 issue where I added a comment as well.

Here is the code that is causing the crash. Its nothing special. All code is written for Unreal Engine 5.1.

#include <aws/core/Aws.h>
#include <aws/auth/credentials.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/cognito-identity/CognitoIdentityClient.h>
#include <aws/cognito-identity/model/GetIdRequest.h>
#include <aws/cognito-identity/model/Credentials.h>
#include <aws/cognito-identity/model/GetIdResult.h>
#include <aws/cognito-identity/model/GetCredentialsForIdentityRequest.h>
#include <aws/cognito-identity/model/GetCredentialsForIdentityResult.h>

#include "OnlineSubsystem.h"
#include "OnlineSubsystemUtils.h"
#include "Interfaces/OnlineIdentityInterface.h"

void UGetAWSCredentials::StartBackgroundTask()
{
	IOnlineSubsystem *Subsystem = Online::GetSubsystem(this->GetWorld());
	IOnlineIdentityPtr Identity = Subsystem->GetIdentityInterface();

	if(Identity->GetLoginStatus(0) == ELoginStatus::NotLoggedIn)
	{
		UE_LOG(LogTemp, Error, TEXT("Not logged in"));
		return;
	}

	if(Identity->GetUniquePlayerId(0).Get()->ToString().Len() == 0)
	{
		UE_LOG(LogTemp, Error, TEXT("No User Id found"));
		return;
	}
	
    AsyncTask(ENamedThreads::AnyThread, [&, Identity, this]()
    {
    	//TODO Make below config variables.
        Aws::String IdentityPoolId = "****";
        Aws::String AccountId = "***";
        Aws::String Region = "***";

        // Get the Cognito Identity ID
        const Aws::String IdentityId = GetCognitoIdentityId(IdentityPoolId, AccountId, Region);
        if (IdentityId.length() == 0)
        {
            OnCognitoIdentityFailed.Broadcast();
            return;
        }

        // Get AWS credentials for the Cognito Identity ID
        const Aws::Auth::AWSCredentials AWSCredentials = GetCredentialsForIdentity(Region, IdentityId);
        if (AWSCredentials.GetAWSAccessKeyId().length() != 0 && AWSCredentials.GetAWSSecretKey().length() != 0 )
        {
            // Use the AWS credentials
            OnCognitoIdentityComplete.Broadcast(AWSCredentials);

        	UE_LOG(LogTemp, Log, TEXT("AWS Credentials for player id: %s \n%hc\n%hc\n%hc"),
        		*Identity->GetUniquePlayerId(0)->ToString(),
        		*AWSCredentials.GetAWSAccessKeyId().c_str(),
        		*AWSCredentials.GetAWSSecretKey().c_str(),
        		*AWSCredentials.GetSessionToken().c_str());
        }
        else
        {
        	UE_LOG(LogTemp, Error, TEXT("AWS Credentials failed"));
            OnCognitoIdentityFailed.Broadcast();
        }
    });
}

Aws::String UGetAWSCredentials::GetCognitoIdentityId(const Aws::String& IdentityPoolId, const Aws::String& AccountId, const Aws::String& Region)
{
	UE_LOG(LogTemp, Log, TEXT("UGetAWSCredentials::GetCognitoIdentityId"));

	Aws::Client::ClientConfiguration ClientConfiguration;
	//Aws::CognitoIdentity::CognitoIdentityClientConfiguration ClientConfiguration;
	ClientConfiguration.region = Region;

	Aws::CognitoIdentity::CognitoIdentityClient IdentityClient = Aws::CognitoIdentity::CognitoIdentityClient(ClientConfiguration);

    Aws::CognitoIdentity::Model::GetIdRequest GetIdRequest;

	// TSharedPtr<Aws::CognitoIdentity::Model::GetIdRequest> GetIdRequest =
	// 	MakeShared<Aws::CognitoIdentity::Model::GetIdRequest>();
	
    GetIdRequest.SetIdentityPoolId(IdentityPoolId);
    GetIdRequest.SetAccountId(AccountId);
	IOnlineSubsystem *Subsystem = Online::GetSubsystem(this->GetWorld());
	IOnlineIdentityPtr Identity = Subsystem->GetIdentityInterface();
	auto UserId = Identity->GetUniquePlayerId(0);
	FUserOnlineAccount* account = Identity->GetUserAccount(*UserId).Get();
	FString accessToken;// = Identity->GetAuthToken(0);
	account->GetAuthAttribute(TEXT("epic.accessToken"), accessToken);
	Aws::String awsAccessToken =  Aws::String(TCHAR_TO_ANSI(*accessToken));
		GetIdRequest.AddLogins("api.epicgames.dev/epic/oauth/v1", awsAccessToken);
	UE_LOG(LogTemp, Log, TEXT("player Auth token: %hs"), awsAccessToken.c_str());
	Aws::CognitoIdentity::Model::GetIdOutcome GetIdOutcome = IdentityClient.GetId(GetIdRequest);
	//GetIdRequest.Reset();
    if (!GetIdOutcome.IsSuccess())
    {
        UE_LOG(LogTemp, Error, TEXT("Failed to get the Cognito Identity ID: %hs"), GetIdOutcome.GetError().GetMessage().c_str());
        return Aws::String();
    }
	
	// TSharedPtr<Aws::CognitoIdentity::Model::GetIdResult> sharedGetObjectResult(
 //    				&const_cast<Aws::CognitoIdentity::Model::GetIdResult&>(result),
 //    				[](Aws::CognitoIdentity::Model::GetIdResult*) { /* Custom deleter, no action required */ }
    			// );

	Aws::String awsIdentityId = GetIdOutcome.GetResult().GetIdentityId();
	UE_LOG(LogTemp, Log, TEXT("Got Cognito Identity ID: %hs"), awsIdentityId.c_str())
	return awsIdentityId;
	// FString IdentityId = FString(ANSI_TO_TCHAR(awsIdentityId.c_str()));
	//
	// UE_LOG(LogTemp, Log, TEXT("UGetAWSCredentials::GetCognitoIdentityId end %s"), *IdentityId);
	// return IdentityId;
}

Aws::Auth::AWSCredentials UGetAWSCredentials::GetCredentialsForIdentity(const Aws::String& Region, const Aws::String& IdentityId)
{
	UE_LOG(LogTemp, Log, TEXT("UGetAWSCredentials::GetCredentialsForIdentity"));

	Aws::Client::ClientConfiguration ClientConfiguration;
	//Aws::CognitoIdentity::CognitoIdentityClientConfiguration ClientConfiguration;
	ClientConfiguration.region = Region;

	Aws::CognitoIdentity::CognitoIdentityClient IdentityClient = Aws::CognitoIdentity::CognitoIdentityClient(ClientConfiguration);

    Aws::CognitoIdentity::Model::GetCredentialsForIdentityRequest GetCredentialsRequest;
	// TSharedPtr<Aws::CognitoIdentity::Model::GetCredentialsForIdentityRequest> GetCredentialsRequest =
	// 	MakeShared<Aws::CognitoIdentity::Model::GetCredentialsForIdentityRequest>();
    GetCredentialsRequest.SetIdentityId(IdentityId);

	IOnlineSubsystem *Subsystem = Online::GetSubsystem(this->GetWorld());
	IOnlineIdentityPtr Identity = Subsystem->GetIdentityInterface();
	auto UserId = Identity->GetUniquePlayerId(0);
	FUserOnlineAccount* account = Identity->GetUserAccount(*UserId).Get();
	
	FString accessToken;// = Identity->GetAuthToken(0);
	account->GetAuthAttribute(TEXT("epic.accessToken"), accessToken);
	Aws::String awsAccessToken =  Aws::String(TCHAR_TO_ANSI(*accessToken));
	GetCredentialsRequest.AddLogins("api.epicgames.dev/epic/oauth/v1", awsAccessToken);
	UE_LOG(LogTemp, Log, TEXT("GetCredentialsForIdentity player Auth token: %hs"), awsAccessToken.c_str());

    auto GetCredentialsOutcome = IdentityClient.GetCredentialsForIdentity(GetCredentialsRequest);
	//GetCredentialsRequest.Reset();
	
    if (!GetCredentialsOutcome.IsSuccess())
    {
        UE_LOG(LogTemp, Error, TEXT("Failed to get AWS credentials for the Cognito Identity ID: %hs"), GetCredentialsOutcome.GetError().GetMessage().c_str());
    	return Aws::Auth::AWSCredentials();

    }

	//auto result = GetCredentialsOutcome.GetResult();
	// const Aws::String awsAccessKey =  result.GetCredentials().GetAccessKeyId();
	// const Aws::String awsSecretKey =  result.GetCredentials().GetSecretKey();
	// const Aws::String awsToken =  result.GetCredentials().GetSessionToken();
	// const Aws::Utils::DateTime awsExpiration = result.GetCredentials().GetExpiration();
	
	UE_LOG(LogTemp, Log, TEXT("UGetAWSCredentials::GetCredentialsForIdentity end"));
	// FAWSCredentials AWSCredentials;
	// AWSCredentials.AccessKey = FString::Printf(TEXT("%hs"), awsAccessKey.c_str());
	// AWSCredentials.SecretKey = FString::Printf(TEXT("%hs"), awsSecretKey.c_str());
	// AWSCredentials.Token = FString::Printf(TEXT("%hs"), awsToken.c_str());

	// AwsCredentials.SetAWSAccessKeyId(awsAccessKey);
	// AwsCredentials.SetAWSSecretKey(awsSecretKey);
	// AwsCredentials.SetSessionToken(awsToken);
	// AwsCredentials.SetExpiration(awsExpiration);
	//Aws::CognitoIdentity::Model::GetCredentialsForIdentityResult result = GetCredentialsOutcome.GetResult();
	// TSharedPtr<Aws::CognitoIdentity::Model::GetCredentialsForIdentityResult> sharedGetObjectResult(
	// 			&const_cast<Aws::CognitoIdentity::Model::GetCredentialsForIdentityResult&>(result),
	// 			[](Aws::CognitoIdentity::Model::GetCredentialsForIdentityResult*) { /* Custom deleter, no action required */ }
	// 		);
	
	
	Aws::CognitoIdentity::Model::Credentials awsCredentials = GetCredentialsOutcome.GetResult().GetCredentials();

	Aws::String jsonizeCreds;
	awsCredentials.Jsonize().AsString(jsonizeCreds);
	UE_LOG(LogTemp, Log, TEXT("Got AWS Credentials: %hs"), jsonizeCreds.c_str());
	return Aws::Auth::AWSCredentials(awsCredentials.GetAccessKeyId(), awsCredentials.GetSecretKey(), awsCredentials.GetSessionToken(), awsCredentials.GetExpiration());
}

The various commented code shows the various trials without success that I have attempted.

@SergeyRyabinin
Copy link
Contributor

Hi @gamerdoku ,

while CUSTOM_MEMORY_MANAGEMENT with a custom memory allocator using Unreal is definitely recommended, based on the information provided, I also suggest to verify that the msvcrt library (aka Microsoft Visual C++ Redistributable) version used by both the SDK build and your instance of Unreal engine is the same.

If you have your toolchain (i.e. MSVS) updated, all applications sharing the MSVS redistributable runtime shall use the same library version in order to avoid ABI issues.
Another option would be to rebuild the SDK in with statically linked MSVCRT runtime (i.e. /MT), however, in this case you might face different linking issues later.
I.e. could you please confirm that you have all environment rebuilt/installed after the latest toolchain update?

Best regards,
Sergey

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. label May 31, 2023
@yasminetalby yasminetalby added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. label Jun 1, 2023
@github-actions
Copy link

github-actions bot commented Jun 2, 2023

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.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jun 2, 2023
@gamerdoku
Copy link
Author

@SergeyRyabinin

Your suggestion helped me a lot. Although I was using the right toolchain I was building using
-DCMAKE_CXX_STANDARD=11 -DCPP_STANDARD=11

I checked UE5.1 and it builds using 17. Rebuilding aws sdk with CUSTOM_MEMORY_MANAGEMENT set and using a custom memory manager class I was able to resolve the issue.

Closing issue as resolved.

@github-actions
Copy link

github-actions bot commented Jun 2, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@JakeDCW
Copy link

JakeDCW commented Sep 28, 2023

@SergeyRyabinin

Your suggestion helped me a lot. Although I was using the right toolchain I was building using -DCMAKE_CXX_STANDARD=11 -DCPP_STANDARD=11

I checked UE5.1 and it builds using 17. Rebuilding aws sdk with CUSTOM_MEMORY_MANAGEMENT set and using a custom memory manager class I was able to resolve the issue.

Closing issue as resolved.

Is it possible you can share your solution in more detail? I am running into something similar when trying to use the AWS SDK with Unreal 5.2

@gamerdoku
Copy link
Author

@SergeyRyabinin
Your suggestion helped me a lot. Although I was using the right toolchain I was building using -DCMAKE_CXX_STANDARD=11 -DCPP_STANDARD=11
I checked UE5.1 and it builds using 17. Rebuilding aws sdk with CUSTOM_MEMORY_MANAGEMENT set and using a custom memory manager class I was able to resolve the issue.
Closing issue as resolved.

Is it possible you can share your solution in more detail? I am running into something similar when trying to use the AWS SDK with Unreal 5.2

Following are the cmake parameters I rely on.

cmake -S ../aws-sdk-cpp -B . /
      -DCMAKE_BUILD_TYPE=Release /
      -DENABLE_TESTING=OFF /
      -DCMAKE_CXX_STANDARD=17 /
      -DCPP_STANDARD=17 /
      -DCUSTOM_MEMORY_MANAGEMENT=ON /
      -DCMAKE_INSTALL_PREFIX="./install_sdk_win64" /
      -DBUILD_ONLY="core;cognito-identity;gamelift;s3;sts"

@JakeDCW
Copy link

JakeDCW commented Oct 3, 2023

@SergeyRyabinin
Your suggestion helped me a lot. Although I was using the right toolchain I was building using -DCMAKE_CXX_STANDARD=11 -DCPP_STANDARD=11
I checked UE5.1 and it builds using 17. Rebuilding aws sdk with CUSTOM_MEMORY_MANAGEMENT set and using a custom memory manager class I was able to resolve the issue.
Closing issue as resolved.

Is it possible you can share your solution in more detail? I am running into something similar when trying to use the AWS SDK with Unreal 5.2

Following are the cmake parameters I rely on.

cmake -S ../aws-sdk-cpp -B . /
      -DCMAKE_BUILD_TYPE=Release /
      -DENABLE_TESTING=OFF /
      -DCMAKE_CXX_STANDARD=17 /
      -DCPP_STANDARD=17 /
      -DCUSTOM_MEMORY_MANAGEMENT=ON /
      -DCMAKE_INSTALL_PREFIX="./install_sdk_win64" /
      -DBUILD_ONLY="core;cognito-identity;gamelift;s3;sts"

Thanks for this, however CMAKE is not letting me build due to a conflict between RTC and O2 flags, did you come across this as well?

@gamerdoku
Copy link
Author

I did not run into this or I do not remember.

Make sure to add --recurse-submodules when you clone. Not sure if I can help more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days.
Projects
None yet
Development

No branches or pull requests

4 participants