Skip to content
Open
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-schema-serde-fuzzing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "aws-cpp-sdk-core",
"contributor": "sbaluja",
"description": "Add fuzz tests for the schema-based serializers and deserializers (ENABLE_SCHEMA_FUZZ_TESTS, run by tools/scripts/run_fuzz.sh) and fix the codec bugs they found."
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,6 @@ src/aws-cpp-sdk-core/include/aws/core/SDKConfig.h

#temp codegen files
tools/code-generation/smithy/codegen/cpp-smoke-tests/codegen_output/

# FuzzTest reproducers (run_fuzz.sh)
tests/fuzz/reproducers/
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ if (LEGACY_BUILD)
option(DISABLE_DNS_REQUIRED_TESTS "Disable unit tests that require DNS lookup to succeed, useful when using a http client that does not perform DNS lookup" OFF)
option(AWS_APPSTORE_SAFE "Remove reference to private Apple APIs for AES GCM in Common Crypto. If set to OFF you application will get rejected from the apple app store." OFF)
option(AWS_ENABLE_CORE_INTEGRATION_TEST "Enables the core integration tests to be built which contains dependencies on other clients for setup and tear down" OFF)
option(ENABLE_SCHEMA_FUZZ_TESTS "Build google/fuzztest fuzz tests for the schema serde (Linux + Clang, static libs)" OFF)


set(AWS_USER_AGENT_CUSTOMIZATION "" CACHE STRING "User agent extension")
Expand Down Expand Up @@ -230,6 +231,11 @@ if (LEGACY_BUILD)
MinSizeRel # Like Release, but optimized for memory rather than speed.
)

if (ENABLE_SCHEMA_FUZZ_TESTS)
include(schema_fuzz_tests)
setup_schema_fuzz_tests()
endif ()

# build third-party targets
if (BUILD_DEPS)
set(CMAKE_INSTALL_RPATH "$ORIGIN")
Expand Down Expand Up @@ -346,6 +352,9 @@ if (LEGACY_BUILD)

add_sdks()
include(tests)
if (ENABLE_SCHEMA_FUZZ_TESTS)
add_schema_fuzz_tests()
endif ()

# Performance tests for services
if (BUILD_PERFORMANCE_TESTS)
Expand Down
45 changes: 45 additions & 0 deletions cmake/schema_fuzz_tests.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.
#

set(SCHEMA_FUZZTEST_GIT_TAG "305251260059201f842e5de6bb10e7b73efb1a6e" CACHE STRING "google/fuzztest commit used by ENABLE_SCHEMA_FUZZ_TESTS")
option(SCHEMA_FUZZ_UNIT_SANITIZERS "Build the non-fuzzing (unit mode) schema fuzz tests with ASan and UBSan" ON)

# Must run before the CRT and core are defined so fuzzing mode instruments them.
macro(setup_schema_fuzz_tests)
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
message(FATAL_ERROR "ENABLE_SCHEMA_FUZZ_TESTS requires Linux (use tools/scripts/run_fuzz.sh)")
endif ()
if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
message(FATAL_ERROR "ENABLE_SCHEMA_FUZZ_TESTS requires Clang; got ${CMAKE_CXX_COMPILER_ID}")
endif ()
if (BUILD_SHARED_LIBS)
message(FATAL_ERROR "ENABLE_SCHEMA_FUZZ_TESTS requires -DBUILD_SHARED_LIBS=OFF")
endif ()
if (ENABLE_TESTING)
message(FATAL_ERROR "ENABLE_SCHEMA_FUZZ_TESTS cannot be combined with ENABLE_TESTING: the vendored gtest and FuzzTest's gtest collide. Use -DENABLE_TESTING=OFF")
endif ()
if (CMAKE_VERSION VERSION_LESS 3.25)
message(FATAL_ERROR "ENABLE_SCHEMA_FUZZ_TESTS requires CMake >= 3.25 (FuzzTest, FetchContent SYSTEM)")
endif ()

include(FetchContent)
FetchContent_Declare(fuzztest
GIT_REPOSITORY https://github.com/google/fuzztest.git
GIT_TAG ${SCHEMA_FUZZTEST_GIT_TAG}
# Keeps FuzzTest and Abseil headers out of the SDK warning set.
SYSTEM)
FetchContent_MakeAvailable(fuzztest)

fuzztest_setup_fuzzing_flags()
if (FUZZTEST_FUZZING_MODE)
# -fno-jump-tables: on AArch64, switch lookup tables of __sancov_cntrs pointers built after SanitizerCoverage
# land outside COMDAT groups and fail to link with "defined in discarded section".
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fno-jump-tables")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fno-jump-tables")
endif ()
endmacro()

function(add_schema_fuzz_tests)
add_subdirectory(${CMAKE_SOURCE_DIR}/tests/fuzz ${CMAKE_BINARY_DIR}/tests/fuzz)
endfunction()
15 changes: 12 additions & 3 deletions src/aws-cpp-sdk-core/include/aws/core/NoResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <aws/core/Core_EXPORTS.h>

#include <type_traits>

namespace Aws
{
namespace Utils
Expand All @@ -25,13 +27,20 @@ namespace Cbor
}
} // namespace Utils

template <typename PAYLOAD_TYPE>
class AmazonWebServiceResult;

class AWS_CORE_API NoResult
{
public:
NoResult() {};
NoResult(const Aws::AmazonWebServiceResult<Aws::Utils::Xml::XmlDocument>&) {};
NoResult(const Aws::AmazonWebServiceResult<Aws::Utils::Json::JsonValue>&) {};
NoResult(const Aws::AmazonWebServiceResult<Aws::Utils::Cbor::CborValue>&) {};
// A template, so that checking whether some other type converts to NoResult does not instantiate
// AmazonWebServiceResult for these payloads, which would need them complete.
template <typename PAYLOAD_TYPE,
typename std::enable_if<std::is_same<PAYLOAD_TYPE, Aws::Utils::Xml::XmlDocument>::value ||
std::is_same<PAYLOAD_TYPE, Aws::Utils::Json::JsonValue>::value ||
std::is_same<PAYLOAD_TYPE, Aws::Utils::Cbor::CborValue>::value, int>::type = 0>
NoResult(const Aws::AmazonWebServiceResult<PAYLOAD_TYPE>&) {}
};

} // namespace Aws
22 changes: 19 additions & 3 deletions src/aws-cpp-sdk-core/include/aws/core/utils/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <aws/common/byte_buf.h>
#include <aws/crt/Optional.h>

#include <locale>


namespace Aws
{
Expand Down Expand Up @@ -176,8 +178,7 @@ namespace Aws


/**
* Parses the entire string as a base-10 int64. Returns empty if the string is empty or has
* characters after the number. Out-of-range values saturate, matching ConvertToInt64.
* Parses the entire string as a base-10 int64. Returns empty if it is malformed or out of range.
*/
static Aws::Crt::Optional<int64_t> ParseInt64(const Aws::String& source);

Expand All @@ -188,6 +189,20 @@ namespace Aws
*/
static Aws::Crt::Optional<double> ParseDouble(const Aws::String& source);

/**
* Formats a double in the classic locale with digits10 (15) significant digits, or max_digits10 (17)
* when 15 do not parse back to the same value, as cJSON does.
*/
static Aws::String FormatLosslessDouble(double value);


/**
* Formats a finite float in the classic locale with digits10 (6) significant digits, or max_digits10 (9)
* when 6 do not parse back to the same value.
*/
static Aws::String FormatLosslessFloat(float value);



#ifdef _WIN32
/**
Expand All @@ -202,12 +217,13 @@ namespace Aws
#endif

/**
* not all platforms (Android) have std::to_string
* not all platforms (Android) have std::to_string. Formats in the classic locale.
*/
template< typename T >
static Aws::String to_string(T value)
{
Aws::OStringStream os;
os.imbue(std::locale::classic());
os << value;
return os.str();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SMITHY_API CborShapeDeserializer final : public ShapeDeserializer {
std::shared_ptr<const Document> ReadDocument(const Schema& schema) override;

bool IsNull() override;
Aws::Crt::Optional<DeserializerError> GetError() const override;

private:
class Impl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class SMITHY_API ClientProtocol {
virtual SerializerOutcome SerializeRequest(const OperationRequestContext& operation, const Schema& inputSchema,
const SerializableStruct& input) const;

virtual void DeserializeResponse(const OperationRequestContext& operation, Aws::Crt::ByteCursor data,
SerializableStruct& output) const;
virtual DeserializerOutcome DeserializeResponse(const OperationRequestContext& operation, Aws::Crt::ByteCursor data,
SerializableStruct& output) const;
};

class SMITHY_API RestJsonProtocol final : public ClientProtocol {
Expand Down Expand Up @@ -95,8 +95,8 @@ class SMITHY_API AwsQueryProtocol final : public ClientProtocol {
Aws::UniquePtr<ShapeDeserializer> CreateOutputDeserializer(Aws::Crt::ByteCursor data) const override;
SerializerOutcome SerializeRequest(const OperationRequestContext& operation, const Schema& inputSchema,
const SerializableStruct& input) const override;
void DeserializeResponse(const OperationRequestContext& operation, Aws::Crt::ByteCursor data,
SerializableStruct& output) const override;
DeserializerOutcome DeserializeResponse(const OperationRequestContext& operation, Aws::Crt::ByteCursor data,
SerializableStruct& output) const override;
};

class SMITHY_API Ec2QueryProtocol final : public ClientProtocol {
Expand Down
7 changes: 4 additions & 3 deletions src/aws-cpp-sdk-core/include/smithy/client/schema/Codec.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <aws/core/NoResult.h>
#include <aws/core/client/AWSError.h>
#include <aws/core/client/CoreErrors.h>
#include <aws/core/utils/Outcome.h>
Expand All @@ -16,6 +17,8 @@ namespace schema {

class Schema;

using DeserializerOutcome = Aws::Utils::Outcome<Aws::NoResult, DeserializerError>;

// Per-protocol timestamp default threaded to a codec's serializer and deserializer. The format is the
// fallback used when a member carries no @timestampFormat trait.
class CodecSettings final {
Expand Down Expand Up @@ -43,9 +46,7 @@ class SMITHY_API Codec {

virtual Aws::UniquePtr<ShapeDeserializer> CreateDeserializer(Aws::Crt::ByteCursor data) const = 0;

void DeserializeShape(Aws::Crt::ByteCursor data, SerializableStruct& shape) const {
shape.Deserialize(*CreateDeserializer(data));
}
DeserializerOutcome DeserializeShape(Aws::Crt::ByteCursor data, SerializableStruct& shape) const;
};

class SMITHY_API JsonCodec final : public Codec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SMITHY_API JsonShapeDeserializer final : public ShapeDeserializer {
std::shared_ptr<const Document> ReadDocument(const Schema& schema) override;

bool IsNull() override;
Aws::Crt::Optional<DeserializerError> GetError() const override;

private:
class Impl;
Expand Down
13 changes: 13 additions & 0 deletions src/aws-cpp-sdk-core/include/smithy/client/schema/SerdeTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
#include <aws/core/utils/DateTime.h>
#include <aws/core/utils/StringUtils.h>
#include <aws/core/utils/memory/stl/AWSString.h>
#include <aws/crt/Optional.h>
#include <smithy/client/schema/Schema.h>
#include <smithy/client/schema/Trait.h>
#include <smithy/client/schema/TraitKey.h>

#include <chrono>
#include <cmath>
#include <cstdint>

namespace smithy {
Expand Down Expand Up @@ -64,6 +67,16 @@ inline Aws::String EpochSecondsText(const Aws::Utils::DateTime& value) {
return out;
}

// Decodes as DateTime(double) does, for parity with legacy clients. Empty where DateTime(double) would overflow.
inline Aws::Crt::Optional<Aws::Utils::DateTime> EpochSecondsToDateTime(double seconds) {
static const double maxSeconds = static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(
(std::chrono::system_clock::duration::max)()).count()) / 1000.0;
if (!std::isfinite(seconds) || std::fabs(seconds) >= maxSeconds) {
return {};
}
return Aws::Utils::DateTime(seconds);
}

inline Aws::String FormatTimestampText(const Aws::Utils::DateTime& value, TimestampFormatTrait::Format format) {
switch (format) {
case TimestampFormatTrait::Format::HTTP_DATE:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <aws/core/client/AWSError.h>
#include <aws/core/client/CoreErrors.h>
#include <aws/core/utils/Array.h>
#include <aws/core/utils/DateTime.h>
#include <aws/core/utils/memory/stl/AWSString.h>
Expand All @@ -15,6 +17,8 @@
namespace smithy {
namespace schema {

using DeserializerError = Aws::Client::AWSError<Aws::Client::CoreErrors>;

class SMITHY_API ShapeDeserializer {
public:
virtual ~ShapeDeserializer() = default;
Expand All @@ -37,10 +41,12 @@ class SMITHY_API ShapeDeserializer {
virtual Aws::Crt::Optional<Aws::String> ReadString(const Schema& schema) = 0;
virtual Aws::Crt::Optional<Aws::Utils::DateTime> ReadTimestamp(const Schema& schema) = 0;
virtual Aws::Crt::Optional<Aws::Utils::ByteBuffer> ReadBlob(const Schema& schema) = 0;
// Null when the member is absent, unparsable, or the protocol does not support documents.
virtual std::shared_ptr<const Document> ReadDocument(const Schema& schema) = 0;

virtual bool IsNull() = 0;

// The first failure, if any; once set, reads return empty.
virtual Aws::Crt::Optional<DeserializerError> GetError() const = 0;
};

} // namespace schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SMITHY_API XmlShapeDeserializer final : public ShapeDeserializer {
std::shared_ptr<const Document> ReadDocument(const Schema& schema) override;

bool IsNull() override;
Aws::Crt::Optional<DeserializerError> GetError() const override;

private:
class Impl;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#pragma once

#include <aws/core/utils/DateTime.h>
#include <aws/core/utils/memory/stl/AWSString.h>
#include <aws/crt/Optional.h>
#include <smithy/client/schema/Codec.h>
#include <smithy/client/schema/Schema.h>
#include <smithy/client/schema/ShapeDeserializer.h>
#include <smithy/client/schema/internal/NestingDepth.h>

#include <cstdint>

namespace smithy {
namespace schema {

// Error and nesting state of one deserialization. The first failure wins; once set, reads return empty.
class DeserializerState final {
public:
const Aws::Crt::Optional<DeserializerError>& GetError() const { return m_error; }
bool HasFailed() const { return m_error.has_value(); }
bool AtRoot() const { return m_depth == 0; }

void Fail(const Aws::String& message);
void FailUnrepresentable(const char* type);

// Call before delivering each non-null modeled member; a second member of a union fails the read.
bool AdmitMember(const Schema& schema, bool& delivered);

Aws::Crt::Optional<int> NarrowToInt(int64_t value);
// Float text printed to 9 digits can parse just above FLT_MAX, so values below the rounding point to infinity clamp.
Aws::Crt::Optional<float> NarrowToFloat(double value);
// Out-of-range integer text fails the read; other non-integer text just returns empty.
Aws::Crt::Optional<int64_t> ParseLong(const Aws::String& text);
Aws::Crt::Optional<Aws::Utils::DateTime> EpochSecondsToTimestamp(double seconds);

class ContainerScope final {
public:
explicit ContainerScope(DeserializerState& state) : m_state(state), m_entered(state.EnterContainer()) {}
~ContainerScope() {
if (m_entered) {
--m_state.m_depth;
}
}
ContainerScope(const ContainerScope&) = delete;
ContainerScope& operator=(const ContainerScope&) = delete;
bool Entered() const { return m_entered; }

private:
DeserializerState& m_state;
bool m_entered;
};

private:
bool EnterContainer();

Aws::Crt::Optional<DeserializerError> m_error;
int m_depth = 0;
};

// Success, or the deserializer's first error.
DeserializerOutcome ToOutcome(const ShapeDeserializer& deserializer);

} // namespace schema
} // namespace smithy
Loading
Loading