diff options
author | mdempsky <mdempsky@chromium.org> | 2016-02-08 21:41:47 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-09 05:42:41 +0000 |
commit | 8a5190449d48e06efa581390426dfa3bb6750f4c (patch) | |
tree | 4d0eedc7f5de614a881cd0f2db73721c76398c28 /tools/ipc_fuzzer | |
parent | 022b60ae0d50b45a8252cbdd7c244162e8013380 (diff) | |
download | chromium_src-8a5190449d48e06efa581390426dfa3bb6750f4c.zip chromium_src-8a5190449d48e06efa581390426dfa3bb6750f4c.tar.gz chromium_src-8a5190449d48e06efa581390426dfa3bb6750f4c.tar.bz2 |
use variadic macros/templates in IPC message implementation
TBR=jam@chromium.org, mseaborn@chromium.org
Review URL: https://codereview.chromium.org/1532053002
Cr-Commit-Position: refs/heads/master@{#374316}
Diffstat (limited to 'tools/ipc_fuzzer')
-rw-r--r-- | tools/ipc_fuzzer/fuzzer/fuzzer.cc | 203 | ||||
-rw-r--r-- | tools/ipc_fuzzer/message_lib/message_names.cc | 2 | ||||
-rw-r--r-- | tools/ipc_fuzzer/message_tools/message_list.cc | 13 |
3 files changed, 92 insertions, 126 deletions
diff --git a/tools/ipc_fuzzer/fuzzer/fuzzer.cc b/tools/ipc_fuzzer/fuzzer/fuzzer.cc index 22bbb5c..f4c8856 100644 --- a/tools/ipc_fuzzer/fuzzer/fuzzer.cc +++ b/tools/ipc_fuzzer/fuzzer/fuzzer.cc @@ -2001,135 +2001,110 @@ struct FuzzTraits<webrtc::MouseCursor> { #include "tools/ipc_fuzzer/message_lib/all_messages.h" #include "tools/ipc_fuzzer/message_lib/all_message_null_macros.h" -// Redefine macros to generate generating funtions -#undef IPC_MESSAGE_DECL -#define IPC_MESSAGE_DECL(kind, type, name, in, out, ilist, olist) \ - IPC_##kind##_##type##_FUZZ(name, in, out, ilist, olist) - -#define IPC_EMPTY_CONTROL_FUZZ(name, in, out, ilist, olist) \ - IPC::Message* fuzzer_for_##name(IPC::Message* msg, Fuzzer* fuzzer) { \ - if (msg) { \ - return NULL; \ - } \ - return new name(); \ - } - -#define IPC_EMPTY_ROUTED_FUZZ(name, in, out, ilist, olist) \ - IPC::Message* fuzzer_for_##name(IPC::Message* msg, Fuzzer* fuzzer) { \ - if (msg) { \ - return NULL; \ - } \ - return new name(RandInRange(MAX_FAKE_ROUTING_ID)); \ - } - -#define IPC_ASYNC_CONTROL_FUZZ(name, in, out, ilist, olist) \ - IPC::Message* fuzzer_for_##name(IPC::Message* msg, Fuzzer* fuzzer) { \ - IPC_TUPLE_IN_##in ilist p; \ - if (msg) { \ - name::Read(static_cast<name*>(msg), &p); \ - } \ - if (FuzzParam(&p, fuzzer)) { \ - return new name(IPC_MEMBERS_IN_##in(p)); \ - } \ - std::cerr << "Don't know how to handle " << #name << "\n"; \ - return 0; \ - } - -#define IPC_ASYNC_ROUTED_FUZZ(name, in, out, ilist, olist) \ - IPC::Message* fuzzer_for_##name(IPC::Message* msg, Fuzzer* fuzzer) { \ - IPC_TUPLE_IN_##in ilist p; \ - if (msg) { \ - name::Read(static_cast<name*>(msg), &p); \ - } \ - if (FuzzParam(&p, fuzzer)) { \ - return new name(RandInRange(MAX_FAKE_ROUTING_ID) \ - IPC_COMMA_##in \ - IPC_MEMBERS_IN_##in(p)); \ - } \ - std::cerr << "Don't know how to handle " << #name << "\n"; \ - return 0; \ - } - -#define IPC_SYNC_CONTROL_FUZZ(name, in, out, ilist, olist) \ - IPC::Message* fuzzer_for_##name(IPC::Message* msg, Fuzzer* fuzzer) { \ - IPC_TUPLE_IN_##in ilist p; \ - name* real_msg = static_cast<name*>(msg); \ - name* new_msg = NULL; \ - if (real_msg) { \ - name::ReadSendParam(real_msg, &p); \ - } \ - if (FuzzParam(&p, fuzzer)) { \ - new_msg = new name(IPC_MEMBERS_IN_##in(p) \ - IPC_COMMA_AND_##out(IPC_COMMA_##in) \ - IPC_MEMBERS_OUT_##out()); \ - } \ - if (real_msg && new_msg) { \ - MessageCracker::CopyMessageID(new_msg, real_msg); \ - } \ - else if (!new_msg) { \ - std::cerr << "Don't know how to handle " << #name << "\n"; \ - } \ - return new_msg; \ - } - -#define IPC_SYNC_ROUTED_FUZZ(name, in, out, ilist, olist) \ - IPC::Message* fuzzer_for_##name(IPC::Message* msg, Fuzzer* fuzzer) { \ - IPC_TUPLE_IN_##in ilist p; \ - name* real_msg = static_cast<name*>(msg); \ - name* new_msg = NULL; \ - if (real_msg) { \ - name::ReadSendParam(real_msg, &p); \ - } \ - if (FuzzParam(&p, fuzzer)) { \ - new_msg = new name(RandInRange(MAX_FAKE_ROUTING_ID) \ - IPC_COMMA_OR_##out(IPC_COMMA_##in) \ - IPC_MEMBERS_IN_##in(p) \ - IPC_COMMA_AND_##out(IPC_COMMA_##in) \ - IPC_MEMBERS_OUT_##out()); \ - } \ - if (real_msg && new_msg) { \ - MessageCracker::CopyMessageID(new_msg, real_msg); \ - } \ - else if (!new_msg) { \ - std::cerr << "Don't know how to handle " << #name << "\n"; \ - } \ - return new_msg; \ +#define MAX_FAKE_ROUTING_ID 15 + +// MessageFactory abstracts away constructing control/routed messages by +// providing an additional random routing ID argument when necessary. +template <typename Message, IPC::MessageKind> +class MessageFactory; + +template <typename Message> +class MessageFactory<Message, IPC::MessageKind::CONTROL> { + public: + template <typename... Args> + static Message* New(const Args&... args) { + return new Message(args...); } +}; -#define MAX_FAKE_ROUTING_ID 15 +template <typename Message> +class MessageFactory<Message, IPC::MessageKind::ROUTED> { + public: + template <typename... Args> + static Message* New(const Args&... args) { + return new Message(RandInRange(MAX_FAKE_ROUTING_ID), args...); + } +}; -#define IPC_MEMBERS_IN_0(p) -#define IPC_MEMBERS_IN_1(p) base::get<0>(p) -#define IPC_MEMBERS_IN_2(p) base::get<0>(p), base::get<1>(p) -#define IPC_MEMBERS_IN_3(p) base::get<0>(p), base::get<1>(p), base::get<2>(p) -#define IPC_MEMBERS_IN_4(p) base::get<0>(p), base::get<1>(p), base::get<2>(p), \ - base::get<3>(p) -#define IPC_MEMBERS_IN_5(p) base::get<0>(p), base::get<1>(p), base::get<2>(p), \ - base::get<3>(p), base::get<4>(p) - -#define IPC_MEMBERS_OUT_0() -#define IPC_MEMBERS_OUT_1() NULL -#define IPC_MEMBERS_OUT_2() NULL, NULL -#define IPC_MEMBERS_OUT_3() NULL, NULL, NULL -#define IPC_MEMBERS_OUT_4() NULL, NULL, NULL, NULL -#define IPC_MEMBERS_OUT_5() NULL, NULL, NULL, NULL, NULL +template <typename Message> +class FuzzerHelper; + +template <typename Meta, typename... Ins> +class FuzzerHelper<IPC::MessageT<Meta, base::Tuple<Ins...>, void>> { + public: + using Message = IPC::MessageT<Meta, base::Tuple<Ins...>, void>; + + static IPC::Message* Fuzz(IPC::Message* msg, Fuzzer* fuzzer) { + return FuzzImpl(msg, fuzzer, base::MakeIndexSequence<sizeof...(Ins)>()); + } + + private: + template <size_t... Ns> + static IPC::Message* FuzzImpl(IPC::Message* msg, + Fuzzer* fuzzer, + base::IndexSequence<Ns...>) { + typename Message::Param p; + if (msg) { + Message::Read(static_cast<Message*>(msg), &p); + } + if (FuzzParam(&p, fuzzer)) { + return MessageFactory<Message, Meta::kKind>::New(base::get<Ns>(p)...); + } + std::cerr << "Don't know how to handle " << Meta::kName << "\n"; + return nullptr; + } +}; + +template <typename Meta, typename... Ins, typename... Outs> +class FuzzerHelper< + IPC::MessageT<Meta, base::Tuple<Ins...>, base::Tuple<Outs...>>> { + public: + using Message = + IPC::MessageT<Meta, base::Tuple<Ins...>, base::Tuple<Outs...>>; + + static IPC::Message* Fuzz(IPC::Message* msg, Fuzzer* fuzzer) { + return FuzzImpl(msg, fuzzer, base::MakeIndexSequence<sizeof...(Ins)>()); + } + + private: + template <size_t... Ns> + static IPC::Message* FuzzImpl(IPC::Message* msg, + Fuzzer* fuzzer, + base::IndexSequence<Ns...>) { + typename Message::SendParam p; + Message* real_msg = static_cast<Message*>(msg); + Message* new_msg = nullptr; + if (real_msg) { + Message::ReadSendParam(real_msg, &p); + } + if (FuzzParam(&p, fuzzer)) { + new_msg = MessageFactory<Message, Meta::kKind>::New( + base::get<Ns>(p)..., static_cast<Outs*>(nullptr)...); + } + if (real_msg && new_msg) { + MessageCracker::CopyMessageID(new_msg, real_msg); + } else if (!new_msg) { + std::cerr << "Don't know how to handle " << Meta::kName << "\n"; + } + return new_msg; + } +}; -#include "tools/ipc_fuzzer/message_lib/all_messages.h" #include "tools/ipc_fuzzer/message_lib/all_message_null_macros.h" void PopulateFuzzerFunctionVector( FuzzerFunctionVector* function_vector) { #undef IPC_MESSAGE_DECL -#define IPC_MESSAGE_DECL(kind, type, name, in, out, ilist, olist) \ - function_vector->push_back(fuzzer_for_##name); +#define IPC_MESSAGE_DECL(name, ...) \ + function_vector->push_back(FuzzerHelper<name>::Fuzz); #include "tools/ipc_fuzzer/message_lib/all_messages.h" } // Redefine macros to register fuzzing functions into map. #include "tools/ipc_fuzzer/message_lib/all_message_null_macros.h" #undef IPC_MESSAGE_DECL -#define IPC_MESSAGE_DECL(kind, type, name, in, out, ilist, olist) \ - (*map)[static_cast<uint32_t>(name::ID)] = fuzzer_for_##name; +#define IPC_MESSAGE_DECL(name, ...) \ + (*map)[static_cast<uint32_t>(name::ID)] = FuzzerHelper<name>::Fuzz; void PopulateFuzzerFunctionMap(FuzzerFunctionMap* map) { #include "tools/ipc_fuzzer/message_lib/all_messages.h" diff --git a/tools/ipc_fuzzer/message_lib/message_names.cc b/tools/ipc_fuzzer/message_lib/message_names.cc index 2702328..da11f23 100644 --- a/tools/ipc_fuzzer/message_lib/message_names.cc +++ b/tools/ipc_fuzzer/message_lib/message_names.cc @@ -7,7 +7,7 @@ #include "tools/ipc_fuzzer/message_lib/all_message_null_macros.h" #undef IPC_MESSAGE_DECL -#define IPC_MESSAGE_DECL(kind, type, name, in, out, ilist, olist) \ +#define IPC_MESSAGE_DECL(name, ...) \ names.Add(static_cast<uint32_t>(name::ID), #name); void PopulateIpcMessageNames(ipc_fuzzer::MessageNames& names) { diff --git a/tools/ipc_fuzzer/message_tools/message_list.cc b/tools/ipc_fuzzer/message_tools/message_list.cc index ac3a598..69e4699 100644 --- a/tools/ipc_fuzzer/message_tools/message_list.cc +++ b/tools/ipc_fuzzer/message_tools/message_list.cc @@ -18,8 +18,6 @@ struct msginfo { const char* name; const char* file; int id; - int in_count; - int out_count; bool operator< (const msginfo& other) const { return id < other.id; @@ -29,8 +27,7 @@ struct msginfo { // Redefine macros to generate table #include "tools/ipc_fuzzer/message_lib/all_message_null_macros.h" #undef IPC_MESSAGE_DECL -#define IPC_MESSAGE_DECL(kind, type, name, in, out, ilist, olist) \ - { #name, __FILE__, IPC_MESSAGE_ID(), in, out }, +#define IPC_MESSAGE_DECL(name, ...) {#name, __FILE__, IPC_MESSAGE_ID()}, static msginfo msgtable[] = { #include "tools/ipc_fuzzer/message_lib/all_messages.h" @@ -143,12 +140,7 @@ static void dump_msgtable(bool show_args, bool show_ids, std::cout << msgtable[i].id << " " << IPC_MESSAGE_ID_CLASS(msgtable[i].id) << "," << IPC_MESSAGE_ID_LINE(msgtable[i].id) << " "; - std::cout << msgtable[i].name; - if (show_args) { - std::cout << "(" << msgtable[i].in_count << " IN, " << - msgtable[i].out_count << " OUT)"; - } - std::cout << "\n"; + std::cout << msgtable[i].name << "\n"; } } } @@ -192,4 +184,3 @@ int main(int argc, char **argv) { dump_msgtable(show_args, show_ids, show_comma, filter); return 0; } - |