| 1 |
/* Regression test for state explosion seen whilst implementing |
| 2 |
PR analyzer/110426, involving an explosion in the number of |
| 3 |
conjured_svalues whilst handling a long chain of external |
| 4 |
function calls. */ |
| 5 |
|
| 6 |
/* { dg-additional-options "-Wno-implicit-function-declaration -Wno-int-conversion -Wno-analyzer-too-complex -Wno-analyzer-symbol-too-complex" } */ |
| 7 |
|
| 8 |
#define NULL ((void *)0) |
| 9 |
typedef unsigned char uint8_t; |
| 10 |
typedef unsigned short int uint16_t; |
| 11 |
typedef unsigned int uint32_t; |
| 12 |
typedef struct Error Error; |
| 13 |
typedef struct QEMUTimer QEMUTimer; |
| 14 |
enum libusb_class_code { |
| 15 |
LIBUSB_CLASS_PER_INTERFACE = 0x00, |
| 16 |
}; |
| 17 |
struct libusb_device_descriptor {}; |
| 18 |
struct libusb_endpoint_descriptor { |
| 19 |
uint8_t bEndpointAddress; |
| 20 |
uint8_t bmAttributes; |
| 21 |
}; |
| 22 |
struct libusb_interface_descriptor { |
| 23 |
uint8_t bNumEndpoints; |
| 24 |
const struct libusb_endpoint_descriptor *endpoint; |
| 25 |
}; |
| 26 |
struct libusb_interface { |
| 27 |
const struct libusb_interface_descriptor *altsetting; |
| 28 |
}; |
| 29 |
struct libusb_config_descriptor { |
| 30 |
uint8_t bNumInterfaces; |
| 31 |
const struct libusb_interface *interface; |
| 32 |
}; |
| 33 |
typedef struct libusb_context libusb_context; |
| 34 |
typedef struct libusb_device libusb_device; |
| 35 |
typedef struct libusb_device_handle libusb_device_handle; |
| 36 |
enum libusb_speed { |
| 37 |
LIBUSB_SUCCESS = 0, |
| 38 |
LIBUSB_TRANSFER_TYPE_BULK = 2U, |
| 39 |
}; |
| 40 |
typedef struct USBDevice USBDevice; |
| 41 |
typedef struct USBHostDevice USBHostDevice; |
| 42 |
struct USBAutoFilter { |
| 43 |
uint32_t bus_num; |
| 44 |
}; |
| 45 |
struct USBHostDevice { |
| 46 |
struct USBAutoFilter match; |
| 47 |
struct USBHostDevice *tqe_next; |
| 48 |
int bus_num; |
| 49 |
int addr; |
| 50 |
char port[16]; |
| 51 |
libusb_device *dev; |
| 52 |
libusb_device_handle *dh; |
| 53 |
struct libusb_device_descriptor ddesc; |
| 54 |
}; |
| 55 |
static union { struct USBHostDevice *tqh_first; } hostdevs = {}; |
| 56 |
static libusb_context *ctx; |
| 57 |
static void usb_host_ep_update(USBHostDevice *s) { |
| 58 |
static const char *tname[] = {}; |
| 59 |
USBDevice *udev = USB_DEVICE(s); |
| 60 |
struct libusb_config_descriptor *conf; |
| 61 |
const struct libusb_interface_descriptor *intf; |
| 62 |
const struct libusb_endpoint_descriptor *endp; |
| 63 |
struct libusb_ss_endpoint_companion_descriptor *endp_ss_comp; |
| 64 |
uint8_t devep, type; |
| 65 |
int ep; |
| 66 |
int rc, i, e; |
| 67 |
usb_ep_reset(udev); |
| 68 |
rc = libusb_get_active_config_descriptor(s->dev, &conf); |
| 69 |
for (i = 0; i < conf->bNumInterfaces; i++) { |
| 70 |
intf = &conf->interface[i].altsetting[0]; |
| 71 |
trace_usb_host_parse_interface(); |
| 72 |
for (e = 0; e < intf->bNumEndpoints; e++) { |
| 73 |
endp = &intf->endpoint[e]; |
| 74 |
devep = endp->bEndpointAddress; |
| 75 |
ep = devep & 0xf; |
| 76 |
type = endp->bmAttributes & 0x3; |
| 77 |
if (usb_ep_get_type(udev) != 255) { |
| 78 |
trace_usb_host_parse_error(); |
| 79 |
} |
| 80 |
trace_usb_host_parse_endpoint(s->bus_num, s->addr); |
| 81 |
usb_ep_set_max_packet_size(udev); |
| 82 |
usb_ep_set_type(udev); |
| 83 |
usb_ep_set_ifnum(udev); |
| 84 |
usb_ep_set_halted(udev); |
| 85 |
if (type == LIBUSB_TRANSFER_TYPE_BULK && |
| 86 |
libusb_get_ss_endpoint_companion_descriptor( |
| 87 |
ctx, endp, &endp_ss_comp) == LIBUSB_SUCCESS) { |
| 88 |
usb_ep_set_max_streams(udev); |
| 89 |
libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
static int usb_host_open(USBHostDevice *s, libusb_device *dev, int hostfd) { |
| 95 |
USBDevice *udev = USB_DEVICE(s); |
| 96 |
int bus_num = 0; |
| 97 |
int addr = 0; |
| 98 |
Error *local_err = NULL; |
| 99 |
if (dev) { |
| 100 |
bus_num = libusb_get_bus_number(dev); |
| 101 |
addr = libusb_get_device_address(dev); |
| 102 |
trace_usb_host_open_started(bus_num, addr); |
| 103 |
libusb_open(dev, &s->dh); |
| 104 |
trace_usb_host_open_hostfd(hostfd); |
| 105 |
libusb_wrap_sys_device(ctx, &s->dh); |
| 106 |
dev = libusb_get_device(s->dh); |
| 107 |
bus_num = libusb_get_bus_number(dev); |
| 108 |
addr = libusb_get_device_address(dev); |
| 109 |
} |
| 110 |
usb_host_detach_kernel(s); |
| 111 |
libusb_get_device_descriptor(dev, &s->ddesc); |
| 112 |
usb_host_get_port(s->dev, s->port, sizeof(s->port)); |
| 113 |
usb_ep_init(udev); |
| 114 |
usb_host_ep_update(s); |
| 115 |
libusb_get_device_speed(dev); |
| 116 |
usb_device_attach(udev, &local_err); |
| 117 |
if (local_err) { |
| 118 |
error_report_err(local_err); |
| 119 |
goto fail; |
| 120 |
} |
| 121 |
return 0; |
| 122 |
fail: |
| 123 |
trace_usb_host_open_failure(); |
| 124 |
if (s->dh != NULL) { |
| 125 |
usb_host_release_interfaces(s); |
| 126 |
libusb_reset_device(s->dh); |
| 127 |
usb_host_attach_kernel(s); |
| 128 |
} |
| 129 |
} |
| 130 |
static QEMUTimer *usb_auto_timer; |
| 131 |
static void usb_host_vm_state(void *unused, _Bool running) {} |
| 132 |
static void usb_host_auto_check(void *unused) { |
| 133 |
struct USBHostDevice *s; |
| 134 |
struct USBAutoFilter *f; |
| 135 |
libusb_device **devs = NULL; |
| 136 |
struct libusb_device_descriptor ddesc; |
| 137 |
int i, n; |
| 138 |
if (usb_host_init() != 0) { |
| 139 |
n = libusb_get_device_list(ctx, &devs); |
| 140 |
for (i = 0; i < n; i++) { |
| 141 |
if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) { |
| 142 |
} |
| 143 |
for ((s) = ((&hostdevs)->tqh_first); (s); (s) = ((s)->tqe_next)) { |
| 144 |
f = &s->match; |
| 145 |
if (f->bus_num > 0 && f->bus_num != libusb_get_bus_number(devs[i])) { |
| 146 |
} |
| 147 |
if (usb_host_open(s, devs[i], 0) < 0) { |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
libusb_free_device_list(devs, 1); |
| 152 |
qemu_add_vm_change_state_handler(usb_host_vm_state, NULL); |
| 153 |
timer_new_ms(usb_host_auto_check, NULL); |
| 154 |
trace_usb_host_auto_scan_enabled(); |
| 155 |
} |
| 156 |
timer_mod(usb_auto_timer, qemu_clock_get_ms() + 2000); |
| 157 |
} |