db-core.c:buffer_secure_zero:
   48|     26|static inline void buffer_secure_zero(void *buf, size_t size) {
   49|     26|#if defined FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
   50|     26|    memset(buf, 0, size);
   51|       |#elif defined _WIN32
   52|       |    SecureZeroMemory(buf, size);
   53|       |#else
   54|       |    explicit_bzero(buf, size);
   55|       |#endif
   56|     26|}

handle_vchan_data:
  683|     26|int handle_vchan_data(struct db_daemon_data *d) {
  684|     26|    struct qdb_hdr untrusted_hdr;
  685|     26|    struct qdb_hdr hdr;
  686|       |
  687|     26|    if (d->vchan_pending_hdr.type == QDB_INVALID_CMD) {
  ------------------
  |  |   96|     26|#define QDB_INVALID_CMD 0xff
  ------------------
  |  Branch (687:9): [True: 0, False: 26]
  ------------------
  688|       |        /* no previous header, retrieve it */
  689|      0|        if (libvchan_data_ready(d->vchan) < sizeof(untrusted_hdr)) {
  ------------------
  |  Branch (689:13): [True: 0, False: 0]
  ------------------
  690|       |            /* not enough data in vchan, wait for more */
  691|      0|            return 2;
  692|      0|        }
  693|      0|        if (libvchan_recv(d->vchan, &untrusted_hdr, sizeof(untrusted_hdr)) < 0) {
  ------------------
  |  Branch (693:13): [True: 0, False: 0]
  ------------------
  694|      0|            perror("vchan read");
  695|      0|            return 0;
  696|      0|        }
  697|      0|        if (!verify_hdr(&untrusted_hdr, 1)) {
  ------------------
  |  Branch (697:13): [True: 0, False: 0]
  ------------------
  698|      0|            fprintf(stderr, "invalid message received from peer\n");
  699|      0|            return 0;
  700|      0|        }
  701|      0|        hdr = untrusted_hdr;
  702|     26|    } else {
  703|     26|        hdr = d->vchan_pending_hdr;
  704|     26|        d->vchan_pending_hdr.type = QDB_INVALID_CMD;
  ------------------
  |  |   96|     26|#define QDB_INVALID_CMD 0xff
  ------------------
  705|     26|    }
  706|       |
  707|       |    /* This check is correct only because the whole message (up to
  708|       |     * QDB_MAX_DATA) can fit into a vchan buffer. Otherwise it could cause a
  709|       |     * deadlock. */
  710|     26|    if (libvchan_data_ready(d->vchan) < hdr.data_len) {
  ------------------
  |  Branch (710:9): [True: 0, False: 26]
  ------------------
  711|       |        /* save the header, but process the message when the rest is available */
  712|      0|        d->vchan_pending_hdr = hdr;
  713|      0|        return 2;
  714|      0|    }
  715|       |
  716|     26|    switch (hdr.type) {
  717|      0|        case QDB_CMD_WRITE:
  ------------------
  |  Branch (717:9): [True: 0, False: 26]
  ------------------
  718|      0|            if (!handle_write(d, NULL, &hdr))
  ------------------
  |  Branch (718:17): [True: 0, False: 0]
  ------------------
  719|      0|                return 0;
  720|      0|            break;
  721|      0|        case QDB_CMD_MULTIREAD:
  ------------------
  |  Branch (721:9): [True: 0, False: 26]
  ------------------
  722|      0|            if (d->remote_connected) {
  ------------------
  |  Branch (722:17): [True: 0, False: 0]
  ------------------
  723|      0|                fprintf(stderr, "received spurious QDB_CMD_MULTIREAD on vchan: "
  724|      0|                                "only one after connection allowed\n");
  725|      0|                discard_data_and_send_error(d, NULL, &hdr);
  726|      0|                return 0;
  727|      0|            }
  728|      0|            if (!handle_multiread(d, NULL, &hdr))
  ------------------
  |  Branch (728:17): [True: 0, False: 0]
  ------------------
  729|      0|                return 0;
  730|       |            /* remote have synchronized database, send furher updates */
  731|      0|            d->remote_connected = 1;
  732|      0|            break;
  733|       |
  734|      0|        case QDB_CMD_RM:
  ------------------
  |  Branch (734:9): [True: 0, False: 26]
  ------------------
  735|       |            /* if there is no space for response, drop the command - remote
  736|       |             * side seems unresponsive */
  737|      0|            if (libvchan_buffer_space(d->vchan) < sizeof(hdr)) {
  ------------------
  |  Branch (737:17): [True: 0, False: 0]
  ------------------
  738|      0|                fprintf(stderr, "got QDB_CMD_RM from remote domain, "
  739|      0|                                "but there is no space in vchan for the reponse; dropping\n");
  740|      0|                return 0;
  741|      0|            }
  742|      0|            if (!handle_rm(d, NULL, &hdr))
  ------------------
  |  Branch (742:17): [True: 0, False: 0]
  ------------------
  743|      0|                return 0;
  744|      0|            break;
  745|       |
  746|      0|        case QDB_RESP_OK:
  ------------------
  |  Branch (746:9): [True: 0, False: 26]
  ------------------
  747|      0|            break;
  748|      0|        case QDB_RESP_ERROR:
  ------------------
  |  Branch (748:9): [True: 0, False: 26]
  ------------------
  749|      0|            fprintf(stderr, "received error from peer\n");
  750|      0|            if (hdr.data_len) {
  ------------------
  |  Branch (750:17): [True: 0, False: 0]
  ------------------
  751|      0|                fprintf(stderr, "FATAL: error packet contains some unexpected data\n");
  752|      0|                return 0;
  753|      0|            }
  754|      0|            break;
  755|      0|        case QDB_RESP_MULTIREAD:
  ------------------
  |  Branch (755:9): [True: 0, False: 26]
  ------------------
  756|      0|            if (d->multiread_requested) {
  ------------------
  |  Branch (756:17): [True: 0, False: 0]
  ------------------
  757|      0|                if (!handle_vchan_multiread_resp(d, &hdr))
  ------------------
  |  Branch (757:21): [True: 0, False: 0]
  ------------------
  758|      0|                    return 0;
  759|      0|            } else {
  760|      0|                fprintf(stderr, "spurious MULTIREAD response\n");
  761|      0|                return 0;
  762|      0|            }
  763|      0|            break;
  764|     26|        default:
  ------------------
  |  Branch (764:9): [True: 26, False: 0]
  ------------------
  765|     26|            fprintf(stderr, "unexpected command from peer: %d\n", hdr.type);
  766|     26|            return 0;
  767|     26|    }
  768|      0|    return 1;
  769|     26|}

qubesdb_init:
   16|     26|struct qubesdb *qubesdb_init(send_watch_notify_t send_notify_func) {
   17|     26|    struct qubesdb *db;
   18|       |
   19|     26|    db = malloc(sizeof (*db));
   20|     26|    if (!db) {
  ------------------
  |  Branch (20:9): [True: 0, False: 26]
  ------------------
   21|      0|        return NULL;
   22|      0|    }
   23|       |
   24|     26|    db->entries = malloc(sizeof(*db->entries));
   25|     26|    if (!db->entries) {
  ------------------
  |  Branch (25:9): [True: 0, False: 26]
  ------------------
   26|      0|        free(db);
   27|      0|        return NULL;
   28|      0|    }
   29|       |
   30|     26|    db->entries->prev = db->entries;
   31|     26|    db->entries->next = db->entries;
   32|     26|    db->entries->path[0] = '/';
   33|     26|    db->entries->path[1] = '\0';
   34|     26|    db->entries->value = strdup("");
   35|     26|    db->entries->value_len = 0;
   36|       |
   37|     26|    db->watches = NULL;
   38|       |
   39|     26|    db->send_watch_notify = send_notify_func;
   40|       |
   41|     26|    return db;
   42|     26|}
qubesdb_destroy:
   44|     26|void qubesdb_destroy(struct qubesdb *db) {
   45|     26|    struct qubesdb_entry *entry;
   46|     26|    struct qubesdb_entry *tmp_entry;
   47|       |
   48|     26|    entry = db->entries->next;
   49|       |
   50|     26|    while (entry != db->entries) {
  ------------------
  |  Branch (50:12): [True: 0, False: 26]
  ------------------
   51|      0|        tmp_entry = entry;
   52|      0|        entry = entry->next;
   53|       |
   54|      0|        tmp_entry->prev->next = tmp_entry->next;
   55|      0|        tmp_entry->next->prev = tmp_entry->prev;
   56|      0|        buffer_secure_zero(tmp_entry->value, tmp_entry->value_len);
   57|      0|        free(tmp_entry->value);
   58|      0|        free(tmp_entry);
   59|      0|    }
   60|       |
   61|     26|    buffer_secure_zero(db->entries->value, db->entries->value_len);
   62|     26|    free(db->entries->value);
   63|     26|    free(db->entries);
   64|       |    // TODO: free watches
   65|     26|    free(db);
   66|     26|}

libvchan_data_ready:
   97|     26|int libvchan_data_ready(libvchan_t *ctrl) {
   98|     26|	return fuzz_size;
   99|     26|};
LLVMFuzzerTestOneInput:
  127|     26|int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  128|     26|	fuzz_data = Data;
  129|     26|	fuzz_size = Size;
  130|     26|	fuzz_offset = 0;
  131|       |
  132|     26|	struct db_daemon_data d;
  133|     26|	memset(&d, 0, sizeof(d));
  134|     26|	d.remote_name = "test";
  135|     26|	d.db = qubesdb_init(write_client_buffered);
  136|     26|	d.vchan = (libvchan_t *) 1; // any not-null pointer is needed
  137|       |
  138|     26|	while (fuzz_size && handle_vchan_data(&d) ) ;
  ------------------
  |  Branch (138:9): [True: 26, False: 0]
  |  Branch (138:22): [True: 0, False: 26]
  ------------------
  139|       |
  140|     26|	qubesdb_destroy(d.db);
  141|       |
  142|     26|	return 0;
  143|     26|};

