Skip to content

Commit

Permalink
(#8578) add 'libprotobuf-mutator' recipe
Browse files Browse the repository at this point in the history
* init libprotobuf-mutator

* validate cppstd

* allow only 'clang'

* check libcxx

* allow only 'libstdc++11'

* patch before build

* little improvements

* Update recipes/libprotobuf-mutator/all/test_package/CMakeLists.txt

Co-authored-by: Uilian Ries <uilianries@gmail.com>

Co-authored-by: Uilian Ries <uilianries@gmail.com>
  • Loading branch information
n-bes and uilianries authored Mar 12, 2022
1 parent 299aa5c commit 2341f93
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 0 deletions.
13 changes: 13 additions & 0 deletions recipes/libprotobuf-mutator/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 2.8.11)
project(cmake_wrapper)

include(conanbuildinfo.cmake)
conan_basic_setup()

add_subdirectory(source_subfolder)

if(MSVC)
# Should be added because of
# https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-initonceexecuteonce
add_definitions(-D_WIN32_WINNT=0x0600)
endif()
4 changes: 4 additions & 0 deletions recipes/libprotobuf-mutator/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sources:
"cci.20210831":
url: "https://github.com/google/libprotobuf-mutator/archive/ffd86a32874e5c08a143019aad1aaf0907294c9f.zip"
sha256: "14f595863452808483776ce1964209db00cdac79e649b31eb4d4b06ca72912d8"
88 changes: 88 additions & 0 deletions recipes/libprotobuf-mutator/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
import os

required_conan_version = ">=1.33.0"


class LibProtobufMutatorConan(ConanFile):
name = "libprotobuf-mutator"
license = "Apache-2.0"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/google/libprotobuf-mutator"
description = "A library to randomly mutate protobuffers."
topics = ("test", "fuzzing", "protobuf")
settings = "os", "compiler", "build_type", "arch"
generators = "cmake"
exports_sources = ["CMakeLists.txt"]

_cmake = None

@property
def _source_subfolder(self):
return "source_subfolder"

@property
def _build_subfolder(self):
return "build_subfolder"

def requirements(self):
self.requires("protobuf/3.17.1")

def source(self):
tools.get(**self.conan_data["sources"][self.version],
destination=self._source_subfolder, strip_root=True)

def validate(self):
if self.settings.compiler != "clang":
raise ConanInvalidConfiguration("Only clang allowed")
if self.settings.compiler.libcxx != "libstdc++11":
raise ConanInvalidConfiguration("Requires either compiler.libcxx=libstdc++11")
if self.settings.compiler.cppstd:
tools.check_min_cppstd(self, 11)

def _patch_sources(self):
tools.replace_in_file(
os.path.join(self._source_subfolder, 'CMakeLists.txt'),
"""include_directories(${PROTOBUF_INCLUDE_DIRS})""",
"""include_directories(${protobuf_INCLUDE_DIRS})""")
tools.replace_in_file(
os.path.join(self._source_subfolder, 'CMakeLists.txt'),
"""set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/external)""",
"""# (disabled by conan) set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/external)""")
tools.replace_in_file(
os.path.join(self._source_subfolder, 'CMakeLists.txt'),
"""add_subdirectory(examples EXCLUDE_FROM_ALL)""",
"""# (disabled by conan) add_subdirectory(examples EXCLUDE_FROM_ALL)""")

def _configure_cmake(self):
if self._cmake:
return self._cmake

self._cmake = CMake(self)
self._cmake.definitions["LIB_PROTO_MUTATOR_TESTING"] = "OFF"
self._cmake.definitions["LIB_PROTO_MUTATOR_DOWNLOAD_PROTOBUF"] = "OFF"
self._cmake.definitions["LIB_PROTO_MUTATOR_WITH_ASAN"] = "OFF"
self._cmake.definitions["LIB_PROTO_MUTATOR_FUZZER_LIBRARIES"] = ""
# todo: check option(LIB_PROTO_MUTATOR_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON)
self._cmake.configure(build_folder=self._build_subfolder)
return self._cmake

def build(self):
self._patch_sources()
cmake = self._configure_cmake()
cmake.build()

def package(self):
self.copy(pattern="LICENSE", dst="licenses", src=self._source_subfolder)
cmake = self._configure_cmake()
cmake.install()
tools.rmdir(os.path.join(self.package_folder, "OFF"))
tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))

def package_info(self):
self.cpp_info.names["cmake_find_package"] = "libprotobuf-mutator"
self.cpp_info.names["cmake_find_package_multi"] = "libprotobuf-mutator"

self.cpp_info.libs = ['protobuf-mutator-libfuzzer', 'protobuf-mutator']
self.cpp_info.includedirs.append(os.path.join("include", "libprotobuf-mutator"))
19 changes: 19 additions & 0 deletions recipes/libprotobuf-mutator/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.1)
project(test_package CXX)


include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
find_package(libprotobuf-mutator REQUIRED CONFIG)


protobuf_generate_cpp(
MSG_PROTO_SRCS
MSG_PROTO_HDRS
msg.proto
)


add_executable(${PROJECT_NAME} ${MSG_PROTO_SRCS} test_package.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(${PROJECT_NAME} libprotobuf-mutator::libprotobuf-mutator)
17 changes: 17 additions & 0 deletions recipes/libprotobuf-mutator/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from conans import ConanFile, CMake, tools
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
11 changes: 11 additions & 0 deletions recipes/libprotobuf-mutator/all/test_package/msg.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto2";
package libfuzzer_example;

import "google/protobuf/any.proto";

message Msg {
optional float optional_float = 1;
optional uint64 optional_uint64 = 2;
optional string optional_string = 3;
optional google.protobuf.Any any = 4;
}
23 changes: 23 additions & 0 deletions recipes/libprotobuf-mutator/all/test_package/test_package.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <cmath>
#include <iostream>

#include "msg.pb.h"
#include <libprotobuf-mutator/src/libfuzzer/libfuzzer_macro.h>

DEFINE_PROTO_FUZZER(const libfuzzer_example::Msg& message) {
protobuf_mutator::protobuf::FileDescriptorProto file;

// Emulate a bug.
if (message.optional_uint64() == std::hash<std::string>{}(message.optional_string()) &&
message.optional_string() == "abcdefghijklmnopqrstuvwxyz" &&
!std::isnan(message.optional_float()) &&
std::fabs(message.optional_float()) > 1000 &&
message.any().UnpackTo(&file) && !file.name().empty())
{
std::cerr << message.DebugString() << "\n";
}
}

int main() {
return 0;
}
3 changes: 3 additions & 0 deletions recipes/libprotobuf-mutator/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
versions:
"cci.20210831":
folder: all

0 comments on commit 2341f93

Please sign in to comment.