Skip to content

Commit a2332b1

Browse files
yonghuahwenlingz
authored andcommitted
dm: refine 'assert' usage in timer.c and rtc.c
- 'assert' cleanup - fix memory leakage in vrtc_init() Tracked-On: #3252 Signed-off-by: Yonghua Huang <yonghua.huang@intel.com> Reviewed-by: Shuo A Liu <shuo.a.liu@intel.com>
1 parent ec62648 commit a2332b1

File tree

2 files changed

+60
-46
lines changed

2 files changed

+60
-46
lines changed

devicemodel/core/timer.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <stdbool.h>
88
#include <unistd.h>
99
#include <errno.h>
10-
#include <assert.h>
1110
#include <sys/timerfd.h>
1211

1312
#include "vmmapi.h"
@@ -51,7 +50,9 @@ timer_handler(int fd __attribute__((unused)),
5150
return;
5251
}
5352

54-
assert(size > 0 && nexp > 0);
53+
/* check the validity of timer expiration. */
54+
if ((size == 0) || (nexp == 0))
55+
return;
5556

5657
if ((cb = timer->callback) != NULL) {
5758
(*cb)(timer->callback_param, nexp);

devicemodel/hw/platform/rtc.c

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626

2727
#include <pthread.h>
2828
#include <string.h>
29-
#include <assert.h>
3029
#include <stddef.h>
3130
#include <stdbool.h>
3231
#include <stdio.h>
3332
#include <stdlib.h>
3433
#include <time.h>
34+
#include <errno.h>
3535

3636
#include "vmmapi.h"
3737
#include "inout.h"
@@ -231,8 +231,6 @@ vrtc_curtime(struct vrtc *vrtc, time_t *basetime)
231231
if (update_enabled(vrtc)) {
232232
now = time(NULL);
233233
delta = now - vrtc->base_uptime;
234-
assert(delta >= 0);
235-
236234
secs = delta;
237235
t += secs;
238236
*basetime += secs;
@@ -308,21 +306,11 @@ clk_ts_to_ct(struct timespec *ts, struct clktime *ct)
308306
rsec = rsec % 60;
309307
ct->sec = rsec;
310308
ct->nsec = ts->tv_nsec;
311-
312-
assert(ct->year >= 0 && ct->year < 10000);
313-
assert(ct->mon >= 1 && ct->mon <= 12);
314-
assert(ct->day >= 1 && ct->day <= 31);
315-
assert(ct->hour >= 0 && ct->hour <= 23);
316-
assert(ct->min >= 0 && ct->min <= 59);
317-
/* Not sure if this interface needs to handle leapseconds or not. */
318-
assert(ct->sec >= 0 && ct->sec <= 60);
319309
}
320310

321311
static inline uint8_t
322312
rtcset(struct rtcdev *rtc, int val)
323313
{
324-
assert(val >= 0 && val < 100);
325-
326314
return ((rtc->reg_b & RTCSB_BIN) ? val : bin2bcd_data[val]);
327315
}
328316

@@ -396,10 +384,9 @@ secs_to_rtc(time_t rtctime, struct vrtc *vrtc, int force_update)
396384
struct rtcdev *rtc;
397385
int hour;
398386

399-
if (rtctime < 0) {
400-
assert(rtctime == VRTC_BROKEN_TIME);
387+
if (rtctime < 0)
388+
/*VRTC_BROKEN_TIME case*/
401389
return;
402-
}
403390

404391
/*
405392
* If the RTC is halted then the guest has "ownership" of the
@@ -413,13 +400,11 @@ secs_to_rtc(time_t rtctime, struct vrtc *vrtc, int force_update)
413400
ts.tv_nsec = 0;
414401
clk_ts_to_ct(&ts, &ct);
415402

416-
assert(ct.sec >= 0 && ct.sec <= 59);
417-
assert(ct.min >= 0 && ct.min <= 59);
418-
assert(ct.hour >= 0 && ct.hour <= 23);
419-
assert(ct.dow >= 0 && ct.dow <= 6);
420-
assert(ct.day >= 1 && ct.day <= 31);
421-
assert(ct.mon >= 1 && ct.mon <= 12);
422-
assert(ct.year >= POSIX_BASE_YEAR);
403+
if ((ct.sec < 0 || ct.sec > 59) || (ct.min < 0 || ct.min > 59)
404+
|| (ct.hour < 0 || ct.hour > 23) || (ct.dow < 0 || ct.dow > 6)
405+
|| (ct.day < 1 || ct.day > 31) || (ct.mon < 1 || ct.mon > 12)
406+
|| (ct.year < POSIX_BASE_YEAR))
407+
return;
423408

424409
rtc = &vrtc->rtcdev;
425410
rtc->sec = rtcset(rtc, ct.sec);
@@ -582,7 +567,7 @@ vrtc_start_timer(struct acrn_timer *timer, time_t sec, time_t nsec)
582567
/*set the delay time it will be started when timer_setting*/
583568
ts.it_value.tv_sec = sec;
584569
ts.it_value.tv_nsec = nsec;
585-
assert(acrn_timer_settime(timer, &ts) == 0);
570+
acrn_timer_settime(timer, &ts);
586571
}
587572

588573
static int
@@ -735,7 +720,6 @@ vrtc_set_reg_b(struct vrtc *vrtc, uint8_t newval)
735720
struct rtcdev *rtc;
736721
time_t oldfreq, newfreq, basetime;
737722
time_t curtime, rtctime;
738-
int error;
739723
uint8_t oldval, changed;
740724

741725
rtc = &vrtc->rtcdev;
@@ -759,7 +743,8 @@ vrtc_set_reg_b(struct vrtc *vrtc, uint8_t newval)
759743
}
760744
} else {
761745
curtime = vrtc_curtime(vrtc, &basetime);
762-
assert(curtime == vrtc->base_rtctime);
746+
if (curtime != vrtc->base_rtctime)
747+
return -1;
763748

764749
/*
765750
* Force a refresh of the RTC date/time fields so
@@ -775,8 +760,8 @@ vrtc_set_reg_b(struct vrtc *vrtc, uint8_t newval)
775760
rtctime = VRTC_BROKEN_TIME;
776761
rtc->reg_b &= ~RTCSB_UINTR;
777762
}
778-
error = vrtc_time_update(vrtc, rtctime, basetime);
779-
assert(error == 0);
763+
if (vrtc_time_update(vrtc, rtctime, basetime) != 0)
764+
return -1;
780765
}
781766

782767
/*
@@ -981,8 +966,7 @@ vrtc_data_handler(struct vmctx *ctx, int vcpu, int in, int port,
981966
if (offset == RTC_CENTURY && !rtc_halted(vrtc)) {
982967
curtime = rtc_to_secs(vrtc);
983968
error = vrtc_time_update(vrtc, curtime, time(NULL));
984-
assert(!error);
985-
if (curtime == VRTC_BROKEN_TIME && rtc_flag_broken_time)
969+
if ((error != 0) || (curtime == VRTC_BROKEN_TIME && rtc_flag_broken_time))
986970
error = -1;
987971
}
988972
}
@@ -1020,7 +1004,9 @@ vrtc_init(struct vmctx *ctx)
10201004
struct inout_port rtc_addr, rtc_data;
10211005

10221006
vrtc = calloc(1, sizeof(struct vrtc));
1023-
assert(vrtc != NULL);
1007+
if (vrtc == NULL)
1008+
return -ENOMEM;
1009+
10241010
vrtc->vm = ctx;
10251011
ctx->vrtc = vrtc;
10261012

@@ -1033,20 +1019,37 @@ vrtc_init(struct vmctx *ctx)
10331019
* 0x5b/0x5c/0x5d - 64KB chunks above 4GB
10341020
*/
10351021
lomem = vm_get_lowmem_size(ctx);
1036-
assert(lomem >= 16 * MB);
1022+
if (lomem < 16 * MB) {
1023+
err = -EINVAL;
1024+
goto fail;
1025+
}
1026+
10371027
lomem = (lomem - 16 * MB) / (64 * KB);
1038-
err = vrtc_nvram_write(vrtc, RTC_LMEM_LSB, lomem);
1039-
assert(err == 0);
1040-
err = vrtc_nvram_write(vrtc, RTC_LMEM_MSB, lomem >> 8);
1041-
assert(err == 0);
1028+
if (vrtc_nvram_write(vrtc, RTC_LMEM_LSB, lomem) != 0) {
1029+
err = -EIO;
1030+
goto fail;
1031+
}
1032+
1033+
if (vrtc_nvram_write(vrtc, RTC_LMEM_MSB, lomem >> 8) != 0) {
1034+
err = -EIO;
1035+
goto fail;
1036+
}
10421037

10431038
himem = vm_get_highmem_size(ctx) / (64 * KB);
1044-
err = vrtc_nvram_write(vrtc, RTC_HMEM_LSB, himem);
1045-
assert(err == 0);
1046-
err = vrtc_nvram_write(vrtc, RTC_HMEM_SB, himem >> 8);
1047-
assert(err == 0);
1048-
err = vrtc_nvram_write(vrtc, RTC_HMEM_MSB, himem >> 16);
1049-
assert(err == 0);
1039+
if (vrtc_nvram_write(vrtc, RTC_HMEM_LSB, himem) != 0) {
1040+
err = -EIO;
1041+
goto fail;
1042+
}
1043+
1044+
if (vrtc_nvram_write(vrtc, RTC_HMEM_SB, himem >> 8) != 0) {
1045+
err = -EIO;
1046+
goto fail;
1047+
}
1048+
1049+
if (vrtc_nvram_write(vrtc, RTC_HMEM_MSB, himem >> 16) != 0) {
1050+
err = -EIO;
1051+
goto fail;
1052+
}
10501053

10511054
memset(&rtc_addr, 0, sizeof(struct inout_port));
10521055
memset(&rtc_data, 0, sizeof(struct inout_port));
@@ -1057,7 +1060,10 @@ vrtc_init(struct vmctx *ctx)
10571060
rtc_addr.flags = IOPORT_F_INOUT;
10581061
rtc_addr.handler = vrtc_addr_handler;
10591062
rtc_addr.arg = vrtc;
1060-
assert(register_inout(&rtc_addr) == 0);
1063+
if (register_inout(&rtc_addr) != 0) {
1064+
err = -EINVAL;
1065+
goto fail;
1066+
}
10611067

10621068
/*register io port handler for rtc data*/
10631069
rtc_data.name = "rtc";
@@ -1066,7 +1072,10 @@ vrtc_init(struct vmctx *ctx)
10661072
rtc_data.flags = IOPORT_F_INOUT;
10671073
rtc_data.handler = vrtc_data_handler;
10681074
rtc_data.arg = vrtc;
1069-
assert(register_inout(&rtc_data) == 0);
1075+
if (register_inout(&rtc_data) != 0) {
1076+
err = -EINVAL;
1077+
goto fail;
1078+
}
10701079

10711080
/* Allow dividers o keep time but disable everything else */
10721081
rtc = &vrtc->rtcdev;
@@ -1100,6 +1109,10 @@ vrtc_init(struct vmctx *ctx)
11001109
vrtc_start_timer(&vrtc->update_timer, 1, 0);
11011110

11021111
return 0;
1112+
1113+
fail:
1114+
free(vrtc);
1115+
return err;
11031116
}
11041117

11051118
void

0 commit comments

Comments
 (0)