Skip to content

Add FuzzTest-based fuzzing for schema serde and fix the codec bugs it… - #3973

Open
sbaluja wants to merge 13 commits into
release/1.12from
fuzz-testing
Open

sbaluja wants to merge 13 commits into
release/1.12from
fuzz-testing

Conversation

@sbaluja

@sbaluja sbaluja commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Issue #, if available:

Description of changes:
Framework (tests/fuzz, see its README): google/fuzztest targets for the JSON, CBOR and XML codecs, awsQuery/ec2Query and JSON documents, built with -DENABLE_SCHEMA_FUZZ_TESTS=ON (Linux, Clang, static) and run in a container by tools/scripts/run_fuzz.sh (unit, fuzz, campaign, repro, replay). A fixed schema registry covers every serde trait; a choice-tape generator builds values for any schema. Oracles: no crash or hang on arbitrary bytes; whatever is read is written and read back unchanged, and the second write is a byte-for-byte fixed point; generated values round-trip; query requests carry exactly the fields the awsQuery and ec2Query naming rules imply, with their exact text (numbers parse back to the same value, timestamps to the same instant, also under a ',' decimal locale). Known codec losses are named CodecTolerance flags, each pinned by a core test.

Codec fixes:

  • floats near FLT_MAX round to FLT_MAX; XML and query floats are written with round-trip precision, non-finite XML attributes as NaN/Infinity
  • epoch-seconds timestamps decode exactly as DateTime(double) does, for parity with legacy clients
  • JSON: unpaired surrogates, bad \u hex and unknown escapes are errors; a leading byte-order mark is skipped; non-object roots are errors and a null root is an empty struct
  • XML: numeric character references decode, CDATA is literal, comments and processing instructions are not text, line ends normalize as tinyxml2 does, CR is written as ; invalid names, duplicate or unquoted attributes are errors; attribute members are delivered only when present; number, boolean and timestamp text is trimmed and booleans are read in any case, as legacy clients read them
  • CBOR: negative integers convert to double with one rounding; text and byte strings, including keys, may be indefinite-length
  • query: floating-point values are URL-encoded; awsQuery sends an empty list as a bare key (EmptyQueryLists), ec2Query omits it

Locale: StringUtils::ParseDouble and the FormatRoundTripDouble/Float reparse read '.' whatever the C numeric locale, and FormatRoundTrip* and StringUtils::to_string format in the classic locale.

Check all that applies:

  • Did a review by yourself.
  • Added proper tests to cover this PR. (If tests are not applicable, explain.)
  • Checked if this PR is a breaking (APIs have been changed) change.
  • Checked if this PR will not introduce cross-platform inconsistent behavior.
  • Checked if this PR would require a ReadMe/Wiki update.

Check which platforms you have built SDK on to verify the correctness of this PR.

  • Linux
  • Windows
  • Android
  • MacOS
  • IOS
  • Other Platforms

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

… found

Framework (tests/fuzz, see its README): google/fuzztest targets for the
JSON, CBOR and XML codecs, awsQuery/ec2Query and JSON documents, built
with -DENABLE_SCHEMA_FUZZ_TESTS=ON (Linux, Clang, static) and run in a
container by tools/scripts/run_fuzz.sh (unit, fuzz, campaign, repro,
replay). A fixed schema registry covers every serde trait; a choice-tape
generator builds values for any schema. Oracles: no crash or hang on
arbitrary bytes; whatever is read is written and read back unchanged,
and the second write is a byte-for-byte fixed point; generated values
round-trip; query requests carry exactly the fields the awsQuery and
ec2Query naming rules imply, with their exact text (numbers parse back
to the same value, timestamps to the same instant, also under a ','
decimal locale). Known codec losses
are named CodecTolerance flags, each pinned by a core test.

Reference parity: ReferenceParity.cpp walks the cJSON (via the CRT,
cJSON_ParseWithOpts so trailing data is rejected) or tinyxml2 11.0.0
tree along a schema by the protocol rules and records what the codec
must read, whether it must fail, and what the reference cannot judge.
The codecs must accept exactly what the reference accepts, read the
same tree, and have their own output read back by the reference;
awsQuery responses are judged the same way. Scalar text is read as
legacy clients read it (XML numbers, booleans and timestamps trimmed),
with the oracles' own parsers (ScalarText.cpp) rather than the codecs'.
Reference leniency is modeled, not copied: only input holding markup
tinyxml2 tolerates is excused from the accept/reject check. There is no
CBOR reference.

Robustness: unit mode runs under ASan and UBSan; deep seeds sit at
MAX_NESTING_DEPTH, so fuzz runs check that depth fits the stack
limit; SCHEMA_FUZZ_LOCALE runs every target under a ',' decimal
locale; campaigns size their job pool from memory and cap RSS per
target.
Failing inputs are kept as corpus files (.b64 for binary) or findings
tapes and replayed on every unit run.

Read contract: deserializers report failures through
Optional<DeserializerError> and share MAX_NESTING_DEPTH with the
serializers. A read fails on structural malformation anywhere in the
payload, including skipped values and trailing data after the root; on
a value the target type cannot represent (int/long overflow, a
non-integral long, float/double overflow, a non-finite document number,
epoch seconds beyond the system clock); and on a union with more than
one non-null modeled member. A well-formed value of the wrong type
leaves the member absent. StringUtils::ParseInt64 rejects out-of-range
input instead of saturating.

Codec fixes:
- floats near FLT_MAX round to FLT_MAX; XML and query floats are written
  with round-trip precision, non-finite XML attributes as NaN/Infinity
- epoch-seconds timestamps decode exactly as DateTime(double) does, for
  parity with legacy clients
- JSON: unpaired surrogates, bad \u hex and unknown escapes are errors;
  a leading byte-order mark is skipped; non-object roots are errors and
  a null root is an empty struct
- XML: numeric character references decode, CDATA is literal, comments
  and processing instructions are not text, line ends normalize as
  tinyxml2 does, CR is written as &#xD;; invalid names, duplicate or
  unquoted attributes are errors; attribute members are delivered only
  when present; number, boolean and timestamp text is trimmed and
  booleans are read in any case, as legacy clients read them
- CBOR: negative integers convert to double with one rounding; text and
  byte strings, including keys, may be indefinite-length
- query: floating-point values are URL-encoded; awsQuery sends an empty
  list as a bare key (EmptyQueryLists), ec2Query omits it

Locale: StringUtils::ParseDouble and the FormatRoundTripDouble/Float
reparse read '.' whatever the C numeric locale, and FormatRoundTrip* and
StringUtils::to_string format in the classic locale.

Build conventions: the fuzz binary compiles with set_compiler_warnings;
FuzzTest is fetched as SYSTEM (CMake >= 3.25) so its headers stay out of
that warning set. The image lives in
tools/CI/docker-file/Ubuntu/24.04-schema-fuzz/.
Comment thread tools/scripts/run_fuzz.sh
if [ -n "${FUZZTEST_REPRODUCERS_OUT_DIR}" ]; then mkdir -p "${FUZZTEST_REPRODUCERS_OUT_DIR}"; fi
cmake -S /src -B "${BUILD_DIR}" -G Ninja \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DBUILD_ONLY=sts \

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.

why only STS? also why are we building the SDK as part of the run? imo we should be running the script against something that is built in another step. the script then would just reference the artifacts

std::shared_ptr<const Document> ReadDocument(const Schema& schema) override;

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

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.

does this get the last error? the idea with the API was that optional empty would be error, where the original comment was "we need to figure out how to show an error, either through a optional, or a outcome". do want chained outcomes on the api?

namespace schema {

// Container nesting limit, root included, shared by every serializer and deserializer.
constexpr int MAX_NESTING_DEPTH = 1000;

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.

why in this class and why exported? seems like this should be a implemetnation detail in a cpp file

virtual Aws::Crt::Optional<DeserializerError> GetError() const { return m_error; }

protected:
void Fail(const Aws::String& message) {

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.

a lot of this protected code should be hidden in the implementation using pimpl, or at least in the cpp of the translation unit

virtual Aws::Crt::Optional<DeserializerError> GetError() const { return m_error; }

protected:
void Fail(const Aws::String& message) {

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.

i think we're about to create a godclass here. you are making this protected so a child could use them. I think you either want a utils class (final class, nonstructible with static functions), or namespace functions to avoid this.

return value;
}

Aws::String StringUtils::FormatRoundTripDouble(double value)

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 function is weird to me. "RoundTrip" is a weird name. something more like FormatToPrecesion and passing in the precision as a argument would be better. additionally we use precesion 17 when 15 fails, when would that ever happn? and do we have a test for it?

@@ -0,0 +1 @@
oWV2YWx1ZV9CAQJBA/8=

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.

howd we come up with these test values?

@@ -0,0 +1,3 @@
choices: {18446744073709551615, 18446744073709551615, 18446744073709551615, 18446744073709551615, 18446744073709551615, 18446744073709551615, 18446744073709551615, 18446744073709551615, 18446744073709551615, 18446744073709551615, 14070713522838610127, 9223372036854775807, 18446462598732840959, 15394373617364385893, 18446744073709551598, 9223372036854775807, 9223372036854775807, 9223372036854775807, 18446744073709551613, 18446744073709551613, 18446744073709551615, 18446744073709551615, 9223372036854775807, 9223372036854775802, 10039637858738959069}

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.

sorry might be me not using google fuzz alot, ar we suppoed to be committing the findings files?

Comment thread tests/fuzz/README.md Outdated
`fuzz`, `campaign`, `repro` and `replay` pass `--stack_limit_kb=2304`; all but `repro` also pass
`--time_limit_per_input=10s`. Durations are `<number><s|m|h>`; anything else is a usage error. Arguments
reach the container as positional parameters, so spaces, globs and quotes in gtest filters survive. On
macOS these modes run under `caffeinate -i`, because host idle sleep freezes the container clock and

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.

can this only run on linux or it also runs on mac? or is clang the difference maker. additionally, i think this command doesnt come deafult in linux, worth checking the CI images.

ShapeDeserializer is now the interface plus a pure virtual GetError().
The first-error and nesting-depth state, and the Fail, Narrow*,
ParseLong, EpochSecondsToTimestamp, AdmitMember and ContainerScope
helpers, move to a final DeserializerState in
smithy/client/schema/internal/, which each codec's Impl owns.
MAX_NESTING_DEPTH and the serializers' NestingDepth move to
internal/NestingDepth.h. Behaviour is unchanged.
FormatLosslessDouble and FormatLosslessFloat are built on a private
FormatToPrecision(value, precision), trying numeric_limits digits10
and then max_digits10, as cJSON does for doubles.
A .tape encodes the generator's choice order, so a generator change
silently rebuilds a different value: the FLT_MAX tape no longer built
FLT_MAX. Each typed finding is now a CodecRegression TEST that calls
ExpectRoundTrip on the literal failing value, as FuzzTest's regression
test drafts do, and the tape loader is removed. Byte inputs stay in
corpus/.
clang18, compiler-rt18 and llvm18 come from the AL2023 repositories.
CMake 3.31.6 comes from pip, because AL2023 ships 3.22 and FuzzTest
needs 3.25. Build trees move to schema-fuzz-al2023-* so an existing
build volume's Ubuntu CMake cache is not reused.
run_fuzz.sh build <unit|fuzzing> configures and builds. unit, fuzz,
campaign, repro and replay only run: they fail when their binary has
not been built, and SCHEMA_FUZZ_BIN runs a binary built in an earlier
step instead. Explains why BUILD_ONLY=sts is set.
FuzzTest needs Linux and Clang, so macOS hosts run it in the container.
caffeinate is macOS-only and is skipped when it is not on the PATH.
Codec::DeserializeShape and ClientProtocol::DeserializeResponse return
Outcome<NoResult, DeserializerError> instead of
Optional<DeserializerError>, matching SerializerOutcome on the write
side. Inside the serde, reads still return Optional and the first
error stays in the deserializer behind GetError(); ToOutcome converts
it at the boundary. DeserializeShape moves out of line into Codec.cpp.
NoResult converted from AmazonWebServiceResult of XmlDocument, JsonValue
and CborValue through three constructors, so building or moving any
Outcome<NoResult, E> instantiated AmazonWebServiceResult for all three
and needed each payload type complete. One constructor template,
limited to the same three payloads, accepts the same conversions
without instantiating anything for other types. NoResult.h now compiles
on its own and Codec.h no longer includes CborValue.h.

On Windows DLL builds, binaries built against the old header that
import the three exported constructors need rebuilding.
@sbaluja
sbaluja changed the base branch from main to release/1.12 December 1, 2026 20:14
@sbaluja
sbaluja marked this pull request as ready for review December 1, 2026 20:20
SCHEMA_FUZZ_HOST=1 runs the build and run steps directly on a Linux
host that carries the image's toolchain, instead of in Docker. Build
trees and campaign logs go under build/schema-fuzz, or
SCHEMA_FUZZ_BUILD_ROOT. The nightly CI job runs this way.
FindChild found each child's closing tag by rescanning the child's
subtree, so reading XML nested n deep took O(n^2) time. The
well-formedness pass now records where every element's closing tag
starts, and navigation looks it up. A document nested 1000 deep reads
in 2 ms instead of 142 ms, and the fuzz campaign's deep XML seed no
longer exceeds the per-input time limit.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants