diff --git a/tensorflow/core/framework/BUILD b/tensorflow/core/framework/BUILD index eec4af36e7e..83f254b7f32 100644 --- a/tensorflow/core/framework/BUILD +++ b/tensorflow/core/framework/BUILD @@ -37,6 +37,7 @@ load( package( default_visibility = [ "//tensorflow/core:__subpackages__", + "//tensorflow/security/fuzzing:__subpackages__", ], licenses = ["notice"], # Apache 2.0 ) diff --git a/tensorflow/security/fuzzing/AreAttrValuesEqual_fuzz.cc b/tensorflow/security/fuzzing/AreAttrValuesEqual_fuzz.cc new file mode 100644 index 00000000000..8a7f9d9ffa7 --- /dev/null +++ b/tensorflow/security/fuzzing/AreAttrValuesEqual_fuzz.cc @@ -0,0 +1,76 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ +#include + +#include +#include + +#include "absl/strings/match.h" +#include "tensorflow/core/framework/attr_value.pb.h" +#include "tensorflow/core/framework/attr_value_util.h" + +// This is a fuzzer for AreAttrValuesEqual and FastAreAttrValuesEqual. + +namespace { + +// A few helpers to construct AttrValue protos. +template +tensorflow::AttrValue createAttrValue(T value) { + tensorflow::AttrValue ret; + SetAttrValue(value, &ret); + return ret; +} + +// A helper to do the comparison asserts. +template +void compareValues(T value, T value_2) { + const tensorflow::AttrValue proto = createAttrValue(value); + const tensorflow::AttrValue proto_same = createAttrValue(value); + const tensorflow::AttrValue proto2 = createAttrValue(value_2); + + // Assert that the Fast and Regular are true. + assert(tensorflow::AreAttrValuesEqual(proto, proto_same)); + assert(tensorflow::FastAreAttrValuesEqual(proto, proto_same)); + // Assert that Fast and Regular for the random values. + assert(tensorflow::AreAttrValuesEqual(proto, proto2) == + tensorflow::FastAreAttrValuesEqual(proto, proto2)); +} + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + FuzzedDataProvider fuzzed_data(data, size); + + // Choose random integers. + const int random_int = fuzzed_data.ConsumeIntegralInRange(1, 100); + const int random_int2 = fuzzed_data.ConsumeIntegralInRange(1, 1000); + compareValues(random_int, random_int2); + + // Choose random floats. + const float random_float = + fuzzed_data.ConsumeFloatingPointInRange(1.0f, 1000.0f); + const float random_float2 = + fuzzed_data.ConsumeFloatingPointInRange(1.0f, 1000.0f); + compareValues(random_float, random_float2); + + // Choose random strings. + const int content_size = fuzzed_data.ConsumeIntegralInRange(10, 300); + const std::string test_string = + fuzzed_data.ConsumeRandomLengthString(content_size); + const std::string test_string2 = fuzzed_data.ConsumeRemainingBytesAsString(); + compareValues(test_string, test_string2); + + return 0; +} + +} // namespace diff --git a/tensorflow/security/fuzzing/BUILD b/tensorflow/security/fuzzing/BUILD index 94c6134b643..71df37b7253 100644 --- a/tensorflow/security/fuzzing/BUILD +++ b/tensorflow/security/fuzzing/BUILD @@ -27,6 +27,16 @@ tf_fuzz_target( ], ) +tf_fuzz_target( + name = "AreAttrValuesEqual_fuzz", + srcs = ["AreAttrValuesEqual_fuzz.cc"], + deps = [ + "//tensorflow/core/framework:attr_value_proto_cc", + "//tensorflow/core/framework:attr_value_util", + "@com_google_absl//absl/strings", + ], +) + tf_fuzz_target( name = "joinpath_fuzz", srcs = ["joinpath_fuzz.cc"],