Skip to content

Commit d0e0871

Browse files
conghuic23wenlingz
authored andcommitted
dm: virtio-block: clean up assert
This patch is to clean up assert from virtio-block Tracked-On: #3252 Signed-off-by: Conghui Chen <conghui.chen@intel.com> Reviewed-by: Shuo A Liu <shuo.a.liu@intel.com>
1 parent 3ef385d commit d0e0871

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

devicemodel/hw/pci/virtio/virtio_block.c

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <stdio.h>
3232
#include <stdlib.h>
3333
#include <string.h>
34-
#include <assert.h>
3534
#include <pthread.h>
3635
#include <openssl/md5.h>
3736

@@ -219,6 +218,15 @@ virtio_blk_done(struct blockif_req *br, int err)
219218
pthread_mutex_unlock(&blk->mtx);
220219
}
221220

221+
static void
222+
virtio_blk_abort(struct virtio_vq_info *vq, uint16_t idx)
223+
{
224+
if (idx < vq->qsize) {
225+
vq_relchain(vq, idx, 1);
226+
vq_endchains(vq, 0);
227+
}
228+
}
229+
222230
static void
223231
virtio_blk_proc(struct virtio_blk *blk, struct virtio_vq_info *vq)
224232
{
@@ -231,6 +239,7 @@ virtio_blk_proc(struct virtio_blk *blk, struct virtio_vq_info *vq)
231239
struct iovec iov[BLOCKIF_IOV_MAX + 2];
232240
uint16_t idx, flags[BLOCKIF_IOV_MAX + 2];
233241

242+
idx = vq->qsize;
234243
n = vq_getchain(vq, &idx, iov, BLOCKIF_IOV_MAX + 2, flags);
235244

236245
/*
@@ -241,18 +250,36 @@ virtio_blk_proc(struct virtio_blk *blk, struct virtio_vq_info *vq)
241250
* XXX - note - this fails on crash dump, which does a
242251
* VIRTIO_BLK_T_FLUSH with a zero transfer length
243252
*/
244-
assert(n >= 2 && n <= BLOCKIF_IOV_MAX + 2);
253+
if (n < 2 || n > BLOCKIF_IOV_MAX + 2) {
254+
WPRINTF(("%s: vq_getchain failed\n", __func__));
255+
virtio_blk_abort(vq, idx);
256+
return;
257+
}
245258

246259
io = &blk->ios[idx];
247-
assert((flags[0] & VRING_DESC_F_WRITE) == 0);
248-
assert(iov[0].iov_len == sizeof(struct virtio_blk_hdr));
260+
if ((flags[0] & VRING_DESC_F_WRITE) != 0) {
261+
WPRINTF(("%s: the type for hdr should not be VRING_DESC_F_WRITE\n", __func__));
262+
virtio_blk_abort(vq, idx);
263+
return;
264+
}
265+
if (iov[0].iov_len != sizeof(struct virtio_blk_hdr)) {
266+
WPRINTF(("%s: the size for hdr %ld should be %ld \n",
267+
__func__,
268+
iov[0].iov_len,
269+
sizeof(struct virtio_blk_hdr)));
270+
virtio_blk_abort(vq, idx);
271+
return;
272+
}
249273
vbh = iov[0].iov_base;
250274
memcpy(&io->req.iov, &iov[1], sizeof(struct iovec) * (n - 2));
251275
io->req.iovcnt = n - 2;
252276
io->req.offset = vbh->sector * DEV_BSIZE;
253277
io->status = iov[--n].iov_base;
254-
assert(iov[n].iov_len == 1);
255-
assert(flags[n] & VRING_DESC_F_WRITE);
278+
if (iov[n].iov_len != 1 || ((flags[n] & VRING_DESC_F_WRITE) == 0)) {
279+
WPRINTF(("%s: status iov is invalid!\n", __func__));
280+
virtio_blk_abort(vq, idx);
281+
return;
282+
}
256283

257284
/*
258285
* XXX
@@ -283,7 +310,11 @@ virtio_blk_proc(struct virtio_blk *blk, struct virtio_vq_info *vq)
283310
* therefore test the inverse of the descriptor bit
284311
* to the op.
285312
*/
286-
assert(((flags[i] & VRING_DESC_F_WRITE) == 0) == writeop);
313+
if (((flags[i] & VRING_DESC_F_WRITE) == 0) != writeop) {
314+
WPRINTF(("%s: flag is confict with operation\n", __func__));
315+
virtio_blk_done(&io->req, EINVAL);
316+
return;
317+
}
287318
iolen += iov[i].iov_len;
288319
}
289320
io->req.resid = iolen;
@@ -337,7 +368,8 @@ virtio_blk_proc(struct virtio_blk *blk, struct virtio_vq_info *vq)
337368
virtio_blk_done(&io->req, EOPNOTSUPP);
338369
return;
339370
}
340-
assert(err == 0);
371+
if (err)
372+
WPRINTF(("%s: request process failed\n", __func__));
341373
}
342374

343375
static void

0 commit comments

Comments
 (0)