From c06528180b3893199212d17a640f0037f6f40088 Mon Sep 17 00:00:00 2001 From: kabeer27 <32016558+kabeer27@users.noreply.github.com> Date: Mon, 15 Jun 2020 03:20:25 +0000 Subject: [PATCH] [abseil-cpp] Initial Integration (#3958) Co-authored-by: Kabeer Seth --- projects/abseil-cpp/BUILD | 30 ++++++++ projects/abseil-cpp/Dockerfile | 30 ++++++++ projects/abseil-cpp/WORKSPACE | 20 ++++++ projects/abseil-cpp/build.sh | 68 +++++++++++++++++++ projects/abseil-cpp/project.yaml | 3 + projects/abseil-cpp/string_escape_fuzzer.cc | 62 +++++++++++++++++ .../abseil-cpp/string_utilities_fuzzer.cc | 56 +++++++++++++++ 7 files changed, 269 insertions(+) create mode 100644 projects/abseil-cpp/BUILD create mode 100644 projects/abseil-cpp/Dockerfile create mode 100644 projects/abseil-cpp/WORKSPACE create mode 100644 projects/abseil-cpp/build.sh create mode 100644 projects/abseil-cpp/project.yaml create mode 100644 projects/abseil-cpp/string_escape_fuzzer.cc create mode 100644 projects/abseil-cpp/string_utilities_fuzzer.cc diff --git a/projects/abseil-cpp/BUILD b/projects/abseil-cpp/BUILD new file mode 100644 index 000000000..d40194ed5 --- /dev/null +++ b/projects/abseil-cpp/BUILD @@ -0,0 +1,30 @@ +# Copyright 2020 Google Inc. +# +# 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. +# +################################################################################ + +cc_binary( + name = "string_escape_fuzzer", + deps = ["@com_google_absl//absl/strings"], + srcs = ["string_escape_fuzzer.cc"], +) + +cc_binary( + name = "string_utilities_fuzzer", + deps = [ + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:cord" + ], + srcs = ["string_utilities_fuzzer.cc"], +) \ No newline at end of file diff --git a/projects/abseil-cpp/Dockerfile b/projects/abseil-cpp/Dockerfile new file mode 100644 index 000000000..09f66844f --- /dev/null +++ b/projects/abseil-cpp/Dockerfile @@ -0,0 +1,30 @@ +# Copyright 2020 Google Inc. +# +# 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. +# +################################################################################ + +FROM gcr.io/oss-fuzz-base/base-builder +RUN apt-get update && apt-get --no-install-recommends install -y build-essential make curl wget rsync + +RUN echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list +RUN curl https://bazel.build/bazel-release.pub.gpg | apt-key add - +RUN apt-get update && apt-get install -y bazel +RUN curl -Lo /usr/bin/bazel \ + https://github.com/bazelbuild/bazelisk/releases/download/v1.1.0/bazelisk-linux-amd64 \ + && \ + chmod +x /usr/bin/bazel + +RUN git clone --depth 1 https://github.com/abseil/abseil-cpp.git + +COPY BUILD WORKSPACE build.sh string_escape_fuzzer.cc string_utilities_fuzzer.cc $SRC/ \ No newline at end of file diff --git a/projects/abseil-cpp/WORKSPACE b/projects/abseil-cpp/WORKSPACE new file mode 100644 index 000000000..e155690d8 --- /dev/null +++ b/projects/abseil-cpp/WORKSPACE @@ -0,0 +1,20 @@ +# Copyright 2020 Google Inc. +# +# 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. +# +################################################################################ + +local_repository( + name = "com_google_absl", + path = "abseil-cpp/", +) diff --git a/projects/abseil-cpp/build.sh b/projects/abseil-cpp/build.sh new file mode 100644 index 000000000..ec3acd4a5 --- /dev/null +++ b/projects/abseil-cpp/build.sh @@ -0,0 +1,68 @@ +# Copyright 2020 Google Inc. +# +# 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. +# +################################################################################ + +readonly EXTRA_BAZEL_FLAGS="$( +for f in ${CFLAGS}; do + echo "--conlyopt=${f}" "--linkopt=${f}" +done +for f in ${CXXFLAGS}; do + echo "--cxxopt=${f}" "--linkopt=${f}" +done + +if [ "$SANITIZER" = "undefined" ] +then + # Bazel uses clang to link binary, which does not link clang_rt ubsan library for C++ automatically. + # See issue: https://github.com/bazelbuild/bazel/issues/8777 + echo "--linkopt=\"$(find $(llvm-config --libdir) -name libclang_rt.ubsan_standalone_cxx-x86_64.a | head -1)\"" +fi +)" + +declare FUZZ_TARGETS=("string_escape_fuzzer" "string_utilities_fuzzer") + +bazel build \ + --verbose_failures \ + --dynamic_mode=off \ + --spawn_strategy=standalone \ + --genrule_strategy=standalone \ + --strip=never \ + --linkopt=-pthread \ + --copt=${LIB_FUZZING_ENGINE} \ + --linkopt=${LIB_FUZZING_ENGINE} \ + --linkopt=-lc++ \ + ${EXTRA_BAZEL_FLAGS} \ + ${FUZZ_TARGETS[*]} + + +if [ "$SANITIZER" = "coverage" ] +then + # The build invoker looks for sources in $SRC, but it turns out that we need + # to not be buried under src/, paths are expected at out/proc/self/cwd by + # the profiler. + declare -r REMAP_PATH="${OUT}/proc/self/cwd" + mkdir -p "${REMAP_PATH}" + mkdir -p "${REMAP_PATH}/external/com_google_absl" + rsync -av "${SRC}"/abseil-cpp/absl "${REMAP_PATH}/external/com_google_absl" + + declare -r RSYNC_FILTER_ARGS=("--include" "*.h" "--include" "*.cc" "--include" \ + "*.hpp" "--include" "*.cpp" "--include" "*.c" "--include" "*/" "--exclude" "*") + rsync -avLk "${RSYNC_FILTER_ARGS[@]}" "${SRC}"/bazel-out "${REMAP_PATH}" + rsync -avLkR "${RSYNC_FILTER_ARGS[@]}" "${HOME}" "${OUT}" + rsync -avLkR "${RSYNC_FILTER_ARGS[@]}" /tmp "${OUT}" + + cp *fuzzer.cc "${OUT}/proc/self/cwd" +fi + +cp "./bazel-bin/"*fuzzer "${OUT}/" diff --git a/projects/abseil-cpp/project.yaml b/projects/abseil-cpp/project.yaml new file mode 100644 index 000000000..241f9c8db --- /dev/null +++ b/projects/abseil-cpp/project.yaml @@ -0,0 +1,3 @@ +homepage: "abseil.io" +language: c++ +primary_contact: "abseil-io@googlegroups.com" diff --git a/projects/abseil-cpp/string_escape_fuzzer.cc b/projects/abseil-cpp/string_escape_fuzzer.cc new file mode 100644 index 000000000..581afab53 --- /dev/null +++ b/projects/abseil-cpp/string_escape_fuzzer.cc @@ -0,0 +1,62 @@ +// Copyright 2020 Google Inc. +// +// 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 "absl/strings/escaping.h" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + std::string str (reinterpret_cast(data), size); + std::string escaped, unescaped; + escaped = absl::CHexEscape(str); + absl::CUnescape(escaped, &unescaped); + if (str != unescaped) + abort(); + + escaped = absl::CEscape(str); + absl::CUnescape(escaped, &unescaped); + if (str != unescaped) + abort(); + + escaped = absl::Utf8SafeCEscape(str); + absl::CUnescape(escaped, &unescaped); + if (str != unescaped) + abort(); + + escaped = absl::Utf8SafeCHexEscape(str); + absl::CUnescape(escaped, &unescaped); + if (str != unescaped) + abort(); + + std::string encoded, decoded; + absl::Base64Escape(str, &encoded); + absl::Base64Unescape(encoded, &decoded); + if (str != unescaped) + abort(); + + absl::WebSafeBase64Escape(str, &encoded); + absl::WebSafeBase64Unescape(encoded, &decoded); + if (str != decoded) + abort(); + + std::string hex_result, bytes_result; + hex_result = absl::BytesToHexString(str); + bytes_result = absl::HexStringToBytes(hex_result); + if (str != decoded) + abort(); + + return 0; +} \ No newline at end of file diff --git a/projects/abseil-cpp/string_utilities_fuzzer.cc b/projects/abseil-cpp/string_utilities_fuzzer.cc new file mode 100644 index 000000000..6735b7ba6 --- /dev/null +++ b/projects/abseil-cpp/string_utilities_fuzzer.cc @@ -0,0 +1,56 @@ +// Copyright 2020 Google Inc. +// +// 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 "absl/strings/str_format.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_split.h" +#include "absl/strings/numbers.h" +#include "absl/strings/str_cat.h" + + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + FuzzedDataProvider fuzzed_data(data, size); + float float_value = fuzzed_data.ConsumeFloatingPoint(); + double double_value = fuzzed_data.ConsumeFloatingPoint(); + int int_value = fuzzed_data.ConsumeIntegral(); + bool bool_value = fuzzed_data.ConsumeBool(); + std::string str1 = fuzzed_data.ConsumeRandomLengthString(); + std::string str2 = fuzzed_data.ConsumeRemainingBytesAsString(); + + std::string float_str = absl::StrFormat("%g", float_value); + std::string double_str = absl::StrFormat("%g", double_value); + std::string int_str = absl::StrFormat("%d", int_value); + std::string bool_str = absl::StrFormat("%d", bool_value); + + if (!absl::SimpleAtof(float_str, &float_value)) + return 0; + if (!absl::SimpleAtod(double_str, &double_value)) + return 0; + if (!absl::SimpleAtoi(int_str, &int_value)) + return 0; + if (!absl::SimpleAtob(bool_str, &bool_value)) + return 0; + + absl::StrAppend(&str1, str2); + std::string str_result = absl::StrCat(str1, float_value, double_value, int_value, bool_value); + std::vector v = absl::StrSplit(str_result, "."); + absl::StrJoin(v, "."); + return 0; +} \ No newline at end of file