diff options
Diffstat (limited to 'fuzz')
-rw-r--r-- | fuzz/build.mk | 2 | ||||
-rw-r--r-- | fuzz/fuzz_config.h | 14 | ||||
-rw-r--r-- | fuzz/host_command_fuzz.c | 17 | ||||
-rw-r--r-- | fuzz/host_command_fuzz.mocklist | 2 | ||||
-rw-r--r-- | fuzz/host_command_fuzz.tasklist | 2 | ||||
-rw-r--r-- | fuzz/pchg_fuzz.c | 16 | ||||
-rw-r--r-- | fuzz/pchg_fuzz.tasklist | 2 | ||||
-rw-r--r-- | fuzz/span.h | 94 | ||||
-rw-r--r-- | fuzz/usb_pd_fuzz.c | 102 | ||||
-rw-r--r-- | fuzz/usb_pd_fuzz.tasklist | 2 | ||||
-rw-r--r-- | fuzz/usb_tcpm_v2_rev20_fuzz.c | 15 | ||||
-rw-r--r-- | fuzz/usb_tcpm_v2_rev20_fuzz.mocklist | 2 | ||||
-rw-r--r-- | fuzz/usb_tcpm_v2_rev20_fuzz.tasklist | 2 |
13 files changed, 166 insertions, 106 deletions
diff --git a/fuzz/build.mk b/fuzz/build.mk index ebb5a3a839..bc79df71e6 100644 --- a/fuzz/build.mk +++ b/fuzz/build.mk @@ -1,5 +1,5 @@ # -*- makefile -*- -# Copyright 2018 The Chromium OS Authors. All rights reserved. +# Copyright 2018 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. # diff --git a/fuzz/fuzz_config.h b/fuzz/fuzz_config.h index fb974ea727..006919d314 100644 --- a/fuzz/fuzz_config.h +++ b/fuzz/fuzz_config.h @@ -1,4 +1,4 @@ -/* Copyright 2018 The Chromium OS Authors. All rights reserved. +/* Copyright 2018 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -90,11 +90,11 @@ #define CONFIG_MKBP_EVENT #define CONFIG_MKBP_USE_GPIO #define CONFIG_PERIPHERAL_CHARGER -#define I2C_PORT_WLC 0 -#define GPIO_WLC_IRQ_CONN 1 -#define GPIO_WLC_NRST_CONN 2 +#define I2C_PORT_WLC 0 +#define GPIO_WLC_IRQ_CONN 1 +#define GPIO_WLC_NRST_CONN 2 #define GPIO_PCHG_P0 GPIO_WLC_IRQ_CONN -#endif /* TEST_PCHG_FUZZ */ +#endif /* TEST_PCHG_FUZZ */ -#endif /* TEST_FUZZ */ -#endif /* __FUZZ_FUZZ_CONFIG_H */ +#endif /* TEST_FUZZ */ +#endif /* __FUZZ_FUZZ_CONFIG_H */ diff --git a/fuzz/host_command_fuzz.c b/fuzz/host_command_fuzz.c index 4e30a5e8c1..0a648f602c 100644 --- a/fuzz/host_command_fuzz.c +++ b/fuzz/host_command_fuzz.c @@ -1,4 +1,4 @@ -/* Copyright 2018 The Chromium OS Authors. All rights reserved. +/* Copyright 2018 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. * @@ -8,10 +8,12 @@ #include <pthread.h> #include <sys/time.h> +#include "builtin/assert.h" #include "common.h" #include "console.h" #include "host_command.h" #include "host_test.h" +#include "printf.h" #include "task.h" #include "test_util.h" #include "timer.h" @@ -75,7 +77,7 @@ static int hostcmd_fill(const uint8_t *data, size_t size) chunks[2].start = chunks[1].start + chunks[1].size + data_len_size; chunks[2].size = sizeof(req_buf) - chunks[2].start; #else - struct chunk chunks[1] = { {0, sizeof(req_buf)} }; + struct chunk chunks[1] = { { 0, sizeof(req_buf) } }; #endif /* @@ -89,7 +91,7 @@ static int hostcmd_fill(const uint8_t *data, size_t size) * over checksum and data_len. */ for (i = 0; i < ARRAY_SIZE(chunks) && ipos < size; i++) { - int cp_size = MIN(chunks[i].size, size-ipos); + int cp_size = MIN(chunks[i].size, size - ipos); memcpy(req_buf + chunks[i].start, data + ipos, cp_size); @@ -112,8 +114,11 @@ static int hostcmd_fill(const uint8_t *data, size_t size) * issues. */ if (first) { - ccprintf("Request: cmd=%04x data=%ph\n", - req->command, HEX_BUF(req_buf, req_size)); + char str_buf[hex_str_buf_size(req_size)]; + + snprintf_hex_buffer(str_buf, sizeof(str_buf), + HEX_BUF(req_buf, req_size)); + ccprintf("Request: cmd=%04x data=%s\n", req->command, str_buf); first = 0; } @@ -130,7 +135,7 @@ static int hostcmd_fill(const uint8_t *data, size_t size) static pthread_cond_t done_cond; static pthread_mutex_t lock; -void run_test(int argc, char **argv) +void run_test(int argc, const char **argv) { ccprints("Fuzzing task started"); wait_for_task_started(); diff --git a/fuzz/host_command_fuzz.mocklist b/fuzz/host_command_fuzz.mocklist index 4ffc786b32..3b7d03ee0e 100644 --- a/fuzz/host_command_fuzz.mocklist +++ b/fuzz/host_command_fuzz.mocklist @@ -1,4 +1,4 @@ -/* Copyright 2019 The Chromium OS Authors. All rights reserved. +/* Copyright 2019 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ diff --git a/fuzz/host_command_fuzz.tasklist b/fuzz/host_command_fuzz.tasklist index 2ff8a94d98..1edc0ce0cb 100644 --- a/fuzz/host_command_fuzz.tasklist +++ b/fuzz/host_command_fuzz.tasklist @@ -1,4 +1,4 @@ -/* Copyright 2018 The Chromium OS Authors. All rights reserved. +/* Copyright 2018 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ diff --git a/fuzz/pchg_fuzz.c b/fuzz/pchg_fuzz.c index 97dbca74c4..4ef5f28b74 100644 --- a/fuzz/pchg_fuzz.c +++ b/fuzz/pchg_fuzz.c @@ -1,11 +1,10 @@ -/* Copyright 2021 The Chromium OS Authors. All rights reserved. +/* Copyright 2021 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. * * Test peripheral device charger module. */ -#define HIDE_EC_STDLIB #include "common.h" #include "compile_time_macros.h" #include "driver/nfc/ctn730.h" @@ -40,15 +39,15 @@ static pthread_cond_t done_cond; static pthread_mutex_t lock; #define MAX_MESSAGES 8 -#define MAX_MESSAGE_SIZE (sizeof(struct ctn730_msg) \ - + member_size(struct ctn730_msg, length) * 256) +#define MAX_MESSAGE_SIZE \ + (sizeof(struct ctn730_msg) + \ + member_size(struct ctn730_msg, length) * 256) static uint8_t input[MAX_MESSAGE_SIZE * MAX_MESSAGES]; static uint8_t *head, *tail; static bool data_available; -int pchg_i2c_xfer(int port, uint16_t addr_flags, - const uint8_t *out, int out_size, - uint8_t *in, int in_size, int flags) +int pchg_i2c_xfer(int port, uint16_t addr_flags, const uint8_t *out, + int out_size, uint8_t *in, int in_size, int flags) { if (port != I2C_PORT_WLC || addr_flags != CTN730_I2C_ADDR) return EC_ERROR_INVAL; @@ -92,10 +91,9 @@ void irq_task(int argc, char **argv) pthread_cond_signal(&done_cond); pthread_mutex_unlock(&lock); } - } -void run_test(int argc, char **argv) +void run_test(int argc, const char **argv) { ccprints("Fuzzing task started"); task_wait_event(-1); diff --git a/fuzz/pchg_fuzz.tasklist b/fuzz/pchg_fuzz.tasklist index 5b30e09245..7c57f19854 100644 --- a/fuzz/pchg_fuzz.tasklist +++ b/fuzz/pchg_fuzz.tasklist @@ -1,4 +1,4 @@ -/* Copyright 2021 The Chromium OS Authors. All rights reserved. +/* Copyright 2021 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ diff --git a/fuzz/span.h b/fuzz/span.h index 531df832a3..9cb08f9461 100644 --- a/fuzz/span.h +++ b/fuzz/span.h @@ -1,4 +1,4 @@ -// Copyright 2018 The Chromium OS Authors. All rights reserved. +// Copyright 2018 The ChromiumOS Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -9,48 +9,76 @@ #include <algorithm> -namespace fuzz { +namespace fuzz +{ -template <typename T> -class span { - public: - typedef T value_type; +template <typename T> class span { + public: + typedef T value_type; - constexpr span() : span<T>(nullptr, nullptr) {} - constexpr span(T* begin, size_t size) : begin_(begin), end_(begin + size) {} - constexpr span(T* begin, T* end) : begin_(begin), end_(end) {} + constexpr span() + : span<T>(nullptr, nullptr) + { + } + constexpr span(T *begin, size_t size) + : begin_(begin) + , end_(begin + size) + { + } + constexpr span(T *begin, T *end) + : begin_(begin) + , end_(end) + { + } - template <class Container> - constexpr span(Container& container) - : begin_(container.begin()), end_(container.end()){}; + template <class Container> + constexpr span(Container &container) + : begin_(container.begin()) + , end_(container.end()){}; - constexpr T* begin() const { return begin_; } - constexpr T* end() const { return end_; } + constexpr T *begin() const + { + return begin_; + } + constexpr T *end() const + { + return end_; + } - constexpr T* data() const { return begin_; } + constexpr T *data() const + { + return begin_; + } - constexpr bool empty() const { return begin_ == end_; } - constexpr size_t size() const { return end_ - begin_; } + constexpr bool empty() const + { + return begin_ == end_; + } + constexpr size_t size() const + { + return end_ - begin_; + } - private: - T* begin_; - T* end_; + private: + T *begin_; + T *end_; }; template <typename Source, typename Destination> -size_t CopyWithPadding(Source source, - Destination destination, - typename Destination::value_type fill_value) { - if (source.size() >= destination.size()) { - std::copy(source.begin(), source.begin() + destination.size(), - destination.begin()); - return destination.size(); - } - std::copy(source.begin(), source.end(), destination.begin()); - std::fill(destination.begin() + source.size(), destination.end(), fill_value); - return source.size(); +size_t CopyWithPadding(Source source, Destination destination, + typename Destination::value_type fill_value) +{ + if (source.size() >= destination.size()) { + std::copy(source.begin(), source.begin() + destination.size(), + destination.begin()); + return destination.size(); + } + std::copy(source.begin(), source.end(), destination.begin()); + std::fill(destination.begin() + source.size(), destination.end(), + fill_value); + return source.size(); } -} // namespace fuzz +} // namespace fuzz -#endif // __FUZZ_SPAN_H +#endif // __FUZZ_SPAN_H diff --git a/fuzz/usb_pd_fuzz.c b/fuzz/usb_pd_fuzz.c index 64eb0913a6..bb462b9e61 100644 --- a/fuzz/usb_pd_fuzz.c +++ b/fuzz/usb_pd_fuzz.c @@ -1,10 +1,9 @@ -/* Copyright 2018 The Chromium OS Authors. All rights reserved. +/* Copyright 2018 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. * * Test USB PD module. */ -#define HIDE_EC_STDLIB #include "common.h" #include "task.h" #include "tcpm/tcpm.h" @@ -20,17 +19,26 @@ #define TASK_EVENT_FUZZ TASK_EVENT_CUSTOM_BIT(0) -#define PORT0 0 +#define PORT0 0 -static int mock_tcpm_init(int port) { return EC_SUCCESS; } -static int mock_tcpm_release(int port) { return EC_SUCCESS; } +static int mock_tcpm_init(int port) +{ + return EC_SUCCESS; +} +static int mock_tcpm_release(int port) +{ + return EC_SUCCESS; +} static int mock_tcpm_select_rp_value(int port, int rp) { return EC_SUCCESS; } -static int mock_tcpm_set_cc(int port, int pull) { return EC_SUCCESS; } +static int mock_tcpm_set_cc(int port, int pull) +{ + return EC_SUCCESS; +} static int mock_tcpm_set_polarity(int port, enum tcpc_cc_polarity polarity) { return EC_SUCCESS; @@ -41,16 +49,28 @@ static __maybe_unused int mock_tcpm_sop_prime_enable(int port, bool enable) return EC_SUCCESS; } -static int mock_tcpm_set_vconn(int port, int enable) { return EC_SUCCESS; } -static int mock_tcpm_set_msg_header(int port, - int power_role, int data_role) { return EC_SUCCESS; } -static int mock_tcpm_set_rx_enable(int port, int enable) { return EC_SUCCESS; } +static int mock_tcpm_set_vconn(int port, int enable) +{ + return EC_SUCCESS; +} +static int mock_tcpm_set_msg_header(int port, int power_role, int data_role) +{ + return EC_SUCCESS; +} +static int mock_tcpm_set_rx_enable(int port, int enable) +{ + return EC_SUCCESS; +} static int mock_tcpm_transmit(int port, enum tcpci_msg_type type, - uint16_t header, const uint32_t *data) -{ return EC_SUCCESS; } -static void mock_tcpc_alert(int port) {} + uint16_t header, const uint32_t *data) +{ + return EC_SUCCESS; +} +static void mock_tcpc_alert(int port) +{ +} static int mock_tcpci_get_chip_info(int port, int live, - struct ec_response_pd_chip_info_v1 *info) + struct ec_response_pd_chip_info_v1 *info) { return EC_ERROR_UNIMPLEMENTED; } @@ -76,7 +96,7 @@ struct tcpc_state { static struct tcpc_state mock_tcpc_state[CONFIG_USB_PD_PORT_MAX_COUNT]; static int mock_tcpm_get_cc(int port, enum tcpc_cc_voltage_status *cc1, - enum tcpc_cc_voltage_status *cc2) + enum tcpc_cc_voltage_status *cc2) { *cc1 = mock_tcpc_state[port].cc1; *cc2 = mock_tcpc_state[port].cc2; @@ -125,31 +145,33 @@ int tcpm_enqueue_message(const int port) return EC_SUCCESS; } -void tcpm_clear_pending_messages(int port) {} +void tcpm_clear_pending_messages(int port) +{ +} static const struct tcpm_drv mock_tcpm_drv = { - .init = &mock_tcpm_init, - .release = &mock_tcpm_release, - .get_cc = &mock_tcpm_get_cc, + .init = &mock_tcpm_init, + .release = &mock_tcpm_release, + .get_cc = &mock_tcpm_get_cc, #ifdef CONFIG_USB_PD_VBUS_DETECT_TCPC - .check_vbus_level = &mock_tcpm_check_vbus_level, + .check_vbus_level = &mock_tcpm_check_vbus_level, #endif - .select_rp_value = &mock_tcpm_select_rp_value, - .set_cc = &mock_tcpm_set_cc, - .set_polarity = &mock_tcpm_set_polarity, + .select_rp_value = &mock_tcpm_select_rp_value, + .set_cc = &mock_tcpm_set_cc, + .set_polarity = &mock_tcpm_set_polarity, #ifdef CONFIG_USB_PD_DECODE_SOP - .sop_prime_enable = &mock_tcpm_sop_prime_enable, + .sop_prime_enable = &mock_tcpm_sop_prime_enable, #endif - .set_vconn = &mock_tcpm_set_vconn, - .set_msg_header = &mock_tcpm_set_msg_header, - .set_rx_enable = &mock_tcpm_set_rx_enable, + .set_vconn = &mock_tcpm_set_vconn, + .set_msg_header = &mock_tcpm_set_msg_header, + .set_rx_enable = &mock_tcpm_set_rx_enable, /* The core calls tcpm_dequeue_message. */ - .get_message_raw = NULL, - .transmit = &mock_tcpm_transmit, - .tcpc_alert = &mock_tcpc_alert, - .get_chip_info = &mock_tcpci_get_chip_info, + .get_message_raw = NULL, + .transmit = &mock_tcpm_transmit, + .tcpc_alert = &mock_tcpc_alert, + .get_chip_info = &mock_tcpci_get_chip_info, #ifdef CONFIG_USB_PD_TCPC_LOW_POWER - .enter_low_power_mode = &mock_enter_low_power_mode, + .enter_low_power_mode = &mock_enter_low_power_mode, #endif }; @@ -170,7 +192,7 @@ enum tcpc_cc_voltage_status next_cc1, next_cc2; #define MAX_MESSAGES 8 static struct message messages[MAX_MESSAGES]; -void run_test(int argc, char **argv) +void run_test(int argc, const char **argv) { uint8_t port = PORT0; int i; @@ -181,8 +203,8 @@ void run_test(int argc, char **argv) while (1) { task_wait_event_mask(TASK_EVENT_FUZZ, -1); - memset(&mock_tcpc_state[port], - 0, sizeof(mock_tcpc_state[port])); + memset(&mock_tcpc_state[port], 0, + sizeof(mock_tcpc_state[port])); task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_TCPC_RESET); task_wait_event(250 * MSEC); @@ -196,7 +218,7 @@ void run_test(int argc, char **argv) /* Fake RX messages, one by one. */ for (i = 0; i < MAX_MESSAGES && messages[i].cnt; i++) { memcpy(&mock_tcpc_state[port].message, &messages[i], - sizeof(messages[i])); + sizeof(messages[i])); tcpm_enqueue_message(port); task_wait_event(50 * MSEC); @@ -220,21 +242,23 @@ int test_fuzz_one_input(const uint8_t *data, unsigned int size) next_cc1 = data[0] & 0x0f; next_cc2 = (data[0] & 0xf0) >> 4; - data++; size--; + data++; + size--; memset(messages, 0, sizeof(messages)); for (i = 0; i < MAX_MESSAGES && size > 0; i++) { int cnt = data[0]; - if (cnt < 3 || cnt > MAX_TCPC_PAYLOAD+3 || cnt > size) { + if (cnt < 3 || cnt > MAX_TCPC_PAYLOAD + 3 || cnt > size) { /* Invalid count, or out of bounds. */ return 0; } memcpy(&messages[i], data, cnt); - data += cnt; size -= cnt; + data += cnt; + size -= cnt; } if (size != 0) { diff --git a/fuzz/usb_pd_fuzz.tasklist b/fuzz/usb_pd_fuzz.tasklist index 6edeac2f98..c006f00784 100644 --- a/fuzz/usb_pd_fuzz.tasklist +++ b/fuzz/usb_pd_fuzz.tasklist @@ -1,4 +1,4 @@ -/* Copyright 2018 The Chromium OS Authors. All rights reserved. +/* Copyright 2018 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ diff --git a/fuzz/usb_tcpm_v2_rev20_fuzz.c b/fuzz/usb_tcpm_v2_rev20_fuzz.c index 2f0129464a..c4f57432ad 100644 --- a/fuzz/usb_tcpm_v2_rev20_fuzz.c +++ b/fuzz/usb_tcpm_v2_rev20_fuzz.c @@ -1,11 +1,10 @@ -/* Copyright 2020 The Chromium OS Authors. All rights reserved. +/* Copyright 2020 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. * * Stubs needed for fuzz testing the USB TCPMv2 state machines. */ -#define HIDE_EC_STDLIB #include "charge_manager.h" #include "mock/usb_mux_mock.h" #include "usb_pd.h" @@ -17,12 +16,18 @@ const struct svdm_response svdm_rsp = { }; /* USB mux configuration */ -const struct usb_mux usb_muxes[CONFIG_USB_PD_PORT_MAX_COUNT] = { +const struct usb_mux_chain usb_muxes[CONFIG_USB_PD_PORT_MAX_COUNT] = { { - .driver = &mock_usb_mux_driver, + .mux = + &(const struct usb_mux){ + .driver = &mock_usb_mux_driver, + }, }, { - .driver = &mock_usb_mux_driver, + .mux = + &(const struct usb_mux){ + .driver = &mock_usb_mux_driver, + }, } }; diff --git a/fuzz/usb_tcpm_v2_rev20_fuzz.mocklist b/fuzz/usb_tcpm_v2_rev20_fuzz.mocklist index 1b2c615371..fe2a3f8f38 100644 --- a/fuzz/usb_tcpm_v2_rev20_fuzz.mocklist +++ b/fuzz/usb_tcpm_v2_rev20_fuzz.mocklist @@ -1,4 +1,4 @@ -/* Copyright 2020 The Chromium OS Authors. All rights reserved. +/* Copyright 2020 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ diff --git a/fuzz/usb_tcpm_v2_rev20_fuzz.tasklist b/fuzz/usb_tcpm_v2_rev20_fuzz.tasklist index e3ad19e719..d20bba1365 100644 --- a/fuzz/usb_tcpm_v2_rev20_fuzz.tasklist +++ b/fuzz/usb_tcpm_v2_rev20_fuzz.tasklist @@ -1,4 +1,4 @@ -/* Copyright 2019 The Chromium OS Authors. All rights reserved. +/* Copyright 2019 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ |