Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changelog/feature-aws-cpp-sdk-s3-crt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "aws-cpp-sdk-s3-crt",
"contributor": "",
"description": "Add `GetObjectRequest::SetRecvBuffer()` to `S3CrtClient`, downloading directly into a caller-owned buffer instead of the CRT's pooled buffer, via aws-c-s3's `recv_buffer` option. Continues the discussion in [#3993](https://github.com/aws/aws-sdk-cpp/issues/3993)."
}
Original file line number Diff line number Diff line change
Expand Up @@ -8955,6 +8955,7 @@ class AWS_S3CRT_API S3CrtClient : public Aws::Client::AWSXMLClient,
std::shared_ptr<Aws::Http::HttpResponse> response;
std::shared_ptr<Aws::Crt::Http::HttpRequest> crtHttpRequest;
Aws::UniquePtr<struct aws_s3_checksum_config> checksumConfig;
struct aws_byte_buf recvBuffer {};
};

Aws::Client::XmlOutcome GenerateXmlOutcome(const std::shared_ptr<Http::HttpResponse>& response) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,27 @@ class GetObjectRequest : public S3CrtRequest {
return *this;
}
///@}

///@{
/**
* <p>Zero-copy download: writes the response body directly into this caller-owned buffer
* instead of copying through the client's pooled buffer. Not supported together with
* <code>PartNumber</code>. The buffer must stay valid and not shared with another
* in-flight request until the outcome handler fires; its contents are undefined on
* failure, and its capacity must cover the full requested range.</p>
*/
inline const Aws::Crt::ByteBuf& GetRecvBuffer() const { return m_recvBuffer; }
inline bool RecvBufferHasBeenSet() const { return m_recvBuffer.buffer != nullptr; }
inline void SetRecvBuffer(Aws::Crt::ByteBuf buffer) { m_recvBuffer = buffer; }
inline GetObjectRequest& WithRecvBuffer(Aws::Crt::ByteBuf buffer) {
SetRecvBuffer(buffer);
return *this;
}
///@}

private:
Aws::Crt::ByteBuf m_recvBuffer{};

Aws::String m_bucket;

Aws::String m_ifMatch;
Expand Down
8 changes: 8 additions & 0 deletions generated/src/aws-cpp-sdk-s3-crt/source/S3CrtClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@ void S3CrtClient::CopyObjectAsync(const CopyObjectRequest& request, const CopyOb

aws_s3_meta_request* meta_request = aws_s3_client_make_meta_request(m_s3CrtClient, &options);
if (meta_request == nullptr) {
Aws::Delete(userData);
return handler(this, request,
CopyObjectOutcome(Aws::Client::AWSError<S3CrtErrors>(S3CrtErrors::INTERNAL_FAILURE, "INTERNAL_FAILURE",
"Unable to create s3 meta request", false)),
Expand Down Expand Up @@ -1327,6 +1328,11 @@ void S3CrtClient::GetObjectAsync(const GetObjectRequest& request, const GetObjec
}
options.shutdown_callback = GetObjectRequestShutdownCallback;
options.type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT;
if (request.RecvBufferHasBeenSet()) {
userData->recvBuffer = request.GetRecvBuffer();
options.body_callback = nullptr;
options.recv_buffer = &userData->recvBuffer;
}
struct aws_signing_config_aws signing_config_override = m_s3CrtSigningConfig;
if (endpointResolutionOutcome.GetResult().GetAttributes() &&
endpointResolutionOutcome.GetResult().GetAttributes()->authScheme.GetSigningRegion()) {
Expand Down Expand Up @@ -1365,6 +1371,7 @@ void S3CrtClient::GetObjectAsync(const GetObjectRequest& request, const GetObjec

aws_s3_meta_request* meta_request = aws_s3_client_make_meta_request(m_s3CrtClient, &options);
if (meta_request == nullptr) {
Aws::Delete(userData);
return handler(this, request,
GetObjectOutcome(Aws::Client::AWSError<S3CrtErrors>(S3CrtErrors::INTERNAL_FAILURE, "INTERNAL_FAILURE",
"Unable to create s3 meta request", false)),
Expand Down Expand Up @@ -1562,6 +1569,7 @@ void S3CrtClient::PutObjectAsync(const PutObjectRequest& request, const PutObjec

aws_s3_meta_request* meta_request = aws_s3_client_make_meta_request(m_s3CrtClient, &options);
if (meta_request == nullptr) {
Aws::Delete(userData);
return handler(this, request,
PutObjectOutcome(Aws::Client::AWSError<S3CrtErrors>(S3CrtErrors::INTERNAL_FAILURE, "INTERNAL_FAILURE",
"Unable to create s3 meta request", false)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include <aws/testing/platform/PlatformTesting.h>
#include <aws/testing/TestingEnvironment.h>
#include <atomic>
#include <cstring>
#include <fstream>
#include <functional>
#include <future>
Expand Down Expand Up @@ -1765,6 +1766,66 @@ namespace
AWS_ASSERT_SUCCESS(Client->GetObject(getObjectRequest));
}

TEST_F(BucketAndObjectOperationTest, NoAuthPublicBucketRecvBuffer) {
Aws::S3Crt::ClientConfiguration s3ClientConfig;
s3ClientConfig.region = Aws::Region::US_EAST_1;
s3ClientConfig.scheme = Scheme::HTTPS;
s3ClientConfig.executor = Aws::MakeShared<Aws::Utils::Threading::PooledThreadExecutor>(ALLOCATION_TAG, 4);
s3ClientConfig.throughputTargetGbps = 2.0;
s3ClientConfig.partSize = 5 * 1024 * 1024;

Client = Aws::MakeShared<S3CrtClient>(ALLOCATION_TAG,
Aws::Auth::AWSCredentials{"", ""},
s3ClientConfig,
Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never);

const char* bucket = "aws-sdk-cpp-docs";
const char* key = "cpp/api/LATEST/index.html";

HeadObjectRequest headObjectRequest;
headObjectRequest.SetBucket(bucket);
headObjectRequest.SetKey(key);
HeadObjectOutcome headObjectOutcome = Client->HeadObject(headObjectRequest);
AWS_ASSERT_SUCCESS(headObjectOutcome);
const size_t contentLength = static_cast<size_t>(headObjectOutcome.GetResult().GetContentLength());

// GET into a caller-owned buffer (recv_buffer): bytes must match a normal GET of the same
// object, and the response body stream must stay empty since the CRT wrote directly into it.
GetObjectRequest normalRequest;
normalRequest.SetBucket(bucket);
normalRequest.SetKey(key);
GetObjectOutcome normalOutcome = Client->GetObject(normalRequest);
AWS_ASSERT_SUCCESS(normalOutcome);
Aws::String normalBody((std::istreambuf_iterator<char>(normalOutcome.GetResult().GetBody())), std::istreambuf_iterator<char>());
ASSERT_EQ(contentLength, normalBody.size());

Aws::Vector<uint8_t> recvBuffer(contentLength);
GetObjectRequest recvBufferRequest;
recvBufferRequest.SetBucket(bucket);
recvBufferRequest.SetKey(key);
recvBufferRequest.SetRecvBuffer(Aws::Crt::ByteBufFromEmptyArray(recvBuffer.data(), recvBuffer.size()));
GetObjectOutcome recvBufferOutcome = Client->GetObject(recvBufferRequest);
AWS_ASSERT_SUCCESS(recvBufferOutcome);
EXPECT_EQ(std::char_traits<char>::eof(), recvBufferOutcome.GetResult().GetBody().peek());
EXPECT_EQ(0, memcmp(normalBody.data(), recvBuffer.data(), recvBuffer.size()));

// recv_buffer is rejected for a part-number GET and for a buffer too small for the range;
// both must fail the outcome cleanly rather than corrupt memory or crash.
GetObjectRequest partNumberRequest;
partNumberRequest.SetBucket(bucket);
partNumberRequest.SetKey(key);
partNumberRequest.SetPartNumber(1);
partNumberRequest.SetRecvBuffer(Aws::Crt::ByteBufFromEmptyArray(recvBuffer.data(), recvBuffer.size()));
EXPECT_FALSE(Client->GetObject(partNumberRequest).IsSuccess());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this partNumber case fails when the meta request is created, and the meta_request == nullptr branch in GetObjectAsync returns without freeing userData. that leak was already there, but this change makes it much easier to hit, and with the request owning the buffer it would leak the whole buffer each time. could you free userData before returning? it's the same block in the S3CrtSpecificOperations templates you're already editing, so the fix covers PutObject and CopyObject too.

if (meta_request == nullptr) {
  Aws::Delete(userData);
  return handler(this, request,
                 GetObjectOutcome(Aws::Client::AWSError<S3CrtErrors>(S3CrtErrors::INTERNAL_FAILURE, "INTERNAL_FAILURE",
                                                                     "Unable to create s3 meta request", false)),
                 handlerContext);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed as suggested, applied to all three (GetObject, PutObject, CopyObject) — pushed.


Aws::Vector<uint8_t> shortBuffer(1);
GetObjectRequest shortBufferRequest;
shortBufferRequest.SetBucket(bucket);
shortBufferRequest.SetKey(key);
shortBufferRequest.SetRecvBuffer(Aws::Crt::ByteBufFromEmptyArray(shortBuffer.data(), shortBuffer.size()));
EXPECT_FALSE(Client->GetObject(shortBufferRequest).IsSuccess());
}

TEST_F(BucketAndObjectOperationTest, LocalHostCrtError) {
Aws::S3Crt::ClientConfiguration s3ClientConfig;
s3ClientConfig.region = Aws::Region::US_EAST_1;
Expand Down
19 changes: 19 additions & 0 deletions tests/aws-cpp-sdk-s3-crt-unit-tests/S3CrtUnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <aws/s3-crt/ClientConfiguration.h>
#include <aws/s3-crt/S3CrtClient.h>
#include <aws/s3-crt/S3CrtEndpointProvider.h>
#include <aws/s3-crt/model/GetObjectRequest.h>
#include <aws/testing/MemoryTesting.h>
#include <aws/testing/platform/PlatformTesting.h>
#include <gtest/gtest.h>
Expand Down Expand Up @@ -155,3 +156,21 @@ TEST_F(S3CrtUnitTest, DisabledMultiRegionAccessPointsRejectsMrapArn) {
EXPECT_TRUE(!outcome.IsSuccess());
EXPECT_EQ("Invalid configuration: Multi-Region Access Point ARNs are disabled.", outcome.GetError().GetMessage());
}

TEST_F(S3CrtUnitTest, GetObjectRequestRecvBufferDefaultsUnset) {
Model::GetObjectRequest request;
EXPECT_FALSE(request.RecvBufferHasBeenSet());
EXPECT_EQ(nullptr, request.GetRecvBuffer().buffer);
EXPECT_EQ(0u, request.GetRecvBuffer().capacity);
}

TEST_F(S3CrtUnitTest, GetObjectRequestRecvBufferRoundTrips) {
Model::GetObjectRequest request;
Aws::Vector<uint8_t> buffer(128);

request.SetRecvBuffer(Aws::Crt::ByteBufFromEmptyArray(buffer.data(), buffer.size()));

EXPECT_TRUE(request.RecvBufferHasBeenSet());
EXPECT_EQ(static_cast<uint8_t*>(buffer.data()), request.GetRecvBuffer().buffer);
EXPECT_EQ(buffer.size(), request.GetRecvBuffer().capacity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,38 @@ namespace Model

public:
#end
#if($serviceNamespace == "S3Crt" && $operation.name == "GetObject")
///@{
/**
* <p>Zero-copy download: writes the response body straight into this caller-owned buffer
* instead of copying through the client's pooled buffer. Not supported together with
* <code>PartNumber</code>. <code>data</code> must stay valid, and must not overlap another
* request in flight, until the outcome handler fires; its contents are undefined on failure.
* <code>capacity</code> must cover the whole requested range.</p>
*/
inline void* GetRecvBuffer() const { return m_recvBuffer; }
inline size_t GetRecvBufferCapacity() const { return m_recvBufferCapacity; }
inline bool RecvBufferHasBeenSet() const { return m_recvBufferHasBeenSet; }
inline void SetRecvBuffer(void* data, size_t capacity) {
m_recvBufferHasBeenSet = true;
m_recvBuffer = data;
m_recvBufferCapacity = capacity;
}
inline ${operation.request.shape.name}& WithRecvBuffer(void* data, size_t capacity) {
SetRecvBuffer(data, capacity);
return *this;
}
///@}

#end
#parse("com/amazonaws/util/awsclientgenerator/velocity/cpp/common/model/ModelGetEndpointRulesContextParamDeclaration.vm")
#set($useRequiredField = true)
#parse("com/amazonaws/util/awsclientgenerator/velocity/cpp/ModelClassMembersAndInlines.vm")
#if($serviceNamespace == "S3Crt" && $operation.name == "GetObject")
void* m_recvBuffer = nullptr;
size_t m_recvBufferCapacity = 0;
bool m_recvBufferHasBeenSet = false;
#end
};

} // namespace Model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ namespace ${rootNamespace}
std::shared_ptr<Aws::Http::HttpResponse> response;
std::shared_ptr<Aws::Crt::Http::HttpRequest> crtHttpRequest;
Aws::UniquePtr<struct aws_s3_checksum_config> checksumConfig;
Aws::UniquePtr<struct aws_byte_buf> recvBuffer;
};

Aws::Client::XmlOutcome GenerateXmlOutcome(const std::shared_ptr<Http::HttpResponse>& response) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ namespace ${rootNamespace}
std::shared_ptr<Aws::Http::HttpResponse> response;
std::shared_ptr<Aws::Crt::Http::HttpRequest> crtHttpRequest;
Aws::UniquePtr<struct aws_s3_checksum_config> checksumConfig;
Aws::UniquePtr<struct aws_byte_buf> recvBuffer;
};

Aws::Client::XmlOutcome GenerateXmlOutcome(const std::shared_ptr<Http::HttpResponse>& response) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,14 @@ void ${className}::${operation.name}Async(${constText}${operation.request.shape.
options.type = AWS_S3_META_REQUEST_TYPE_COPY_OBJECT;
#else
options.type = AWS_S3_META_REQUEST_TYPE_DEFAULT;
#end
#if($operation.name == "GetObject")
if (request.RecvBufferHasBeenSet()) {
userData->recvBuffer = Aws::MakeUnique<struct aws_byte_buf>(ALLOCATION_TAG);
*userData->recvBuffer = aws_byte_buf_from_empty_array(request.GetRecvBuffer(), request.GetRecvBufferCapacity());
options.body_callback = nullptr;
options.recv_buffer = userData->recvBuffer.get();
}
#end
struct aws_signing_config_aws signing_config_override = m_s3CrtSigningConfig;
#if($serviceModel.endpointRules)
Expand Down Expand Up @@ -830,6 +838,14 @@ void ${className}::${operation.name}Async(${constText}${operation.name}ResponseR
options.type = AWS_S3_META_REQUEST_TYPE_COPY_OBJECT;
#else
options.type = AWS_S3_META_REQUEST_TYPE_DEFAULT;
#end
#if($operation.name == "GetObject")
if (request.RecvBufferHasBeenSet()) {
userData->recvBuffer = Aws::MakeUnique<struct aws_byte_buf>(ALLOCATION_TAG);
*userData->recvBuffer = aws_byte_buf_from_empty_array(request.GetRecvBuffer(), request.GetRecvBufferCapacity());
options.body_callback = nullptr;
options.recv_buffer = userData->recvBuffer.get();
}
#end
struct aws_signing_config_aws signing_config_override = m_s3CrtSigningConfig;
signing_config_override.region = Aws::Crt::ByteCursorFromCString(computeEndpointOutcome.GetResult().signerRegion.c_str());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,14 @@ void ${className}::${operation.name}Async(${constText}${operation.request.shape.
options.type = AWS_S3_META_REQUEST_TYPE_COPY_OBJECT;
#else
options.type = AWS_S3_META_REQUEST_TYPE_DEFAULT;
#end
#if($operation.name == "GetObject")
if (request.RecvBufferHasBeenSet()) {
userData->recvBuffer = Aws::MakeUnique<struct aws_byte_buf>(ALLOCATION_TAG);
*userData->recvBuffer = aws_byte_buf_from_empty_array(request.GetRecvBuffer(), request.GetRecvBufferCapacity());
options.body_callback = nullptr;
options.recv_buffer = userData->recvBuffer.get();
}
#end
struct aws_signing_config_aws signing_config_override = m_s3CrtSigningConfig;
#if($serviceModel.endpointRules)
Expand Down Expand Up @@ -848,6 +856,14 @@ void ${className}::${operation.name}Async(${constText}${operation.name}ResponseR
options.type = AWS_S3_META_REQUEST_TYPE_COPY_OBJECT;
#else
options.type = AWS_S3_META_REQUEST_TYPE_DEFAULT;
#end
#if($operation.name == "GetObject")
if (request.RecvBufferHasBeenSet()) {
userData->recvBuffer = Aws::MakeUnique<struct aws_byte_buf>(ALLOCATION_TAG);
*userData->recvBuffer = aws_byte_buf_from_empty_array(request.GetRecvBuffer(), request.GetRecvBufferCapacity());
options.body_callback = nullptr;
options.recv_buffer = userData->recvBuffer.get();
}
#end
struct aws_signing_config_aws signing_config_override = m_s3CrtSigningConfig;
signing_config_override.region = Aws::Crt::ByteCursorFromCString(computeEndpointOutcome.GetResult().signerRegion.c_str());
Expand Down