Skip to content

Commit

Permalink
syscalls/clock_settime: Add support for time64 tests
Browse files Browse the repository at this point in the history
This adds support for time64 tests to the existing clock_settime()
syscall tests.

Signed-off-by: Viresh Kumar <[email protected]>
Signed-off-by: Cyril Hrubis <[email protected]>
  • Loading branch information
vireshk authored and metan-ucw committed Apr 23, 2020
1 parent a143f48 commit bcf3733
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 53 deletions.
15 changes: 15 additions & 0 deletions include/tst_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ static inline int sys_clock_gettime64(clockid_t clk_id, void *ts)
return tst_syscall(__NR_clock_gettime64, clk_id, ts);
}

static inline int libc_clock_settime(clockid_t clk_id, void *ts)
{
return clock_settime(clk_id, ts);
}

static inline int sys_clock_settime(clockid_t clk_id, void *ts)
{
return tst_syscall(__NR_clock_settime, clk_id, ts);
}

static inline int sys_clock_settime64(clockid_t clk_id, void *ts)
{
return tst_syscall(__NR_clock_settime64, clk_id, ts);
}

/*
* Returns tst_ts seconds.
*/
Expand Down
78 changes: 61 additions & 17 deletions testcases/kernel/syscalls/clock_settime/clock_settime01.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,91 @@
#include "config.h"
#include "tst_timer.h"
#include "tst_safe_clocks.h"
#include "tst_test.h"
#include "lapi/syscalls.h"
#include "lapi/abisize.h"

#define DELTA_SEC 10
#define DELTA_US (long long) (DELTA_SEC * 1000000)
#define DELTA_EPS (long long) (DELTA_US * 0.1)

static struct timespec *begin, *change, *end;
static struct tst_ts *begin, *change, *end;

static struct test_variants {
int (*gettime)(clockid_t clk_id, void *ts);
int (*settime)(clockid_t clk_id, void *ts);
enum tst_ts_type type;
char *desc;
} variants[] = {
#if defined(TST_ABI32)
{ .gettime = libc_clock_gettime, .settime = libc_clock_settime, .type = TST_LIBC_TIMESPEC, .desc = "vDSO or syscall with libc spec"},
{ .gettime = sys_clock_gettime, .settime = sys_clock_settime, .type = TST_LIBC_TIMESPEC, .desc = "syscall with libc spec"},
{ .gettime = sys_clock_gettime, .settime = sys_clock_settime, .type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with kernel spec32"},
#endif

#if defined(TST_ABI64)
{ .gettime = sys_clock_gettime, .settime = sys_clock_settime, .type = TST_KERN_TIMESPEC, .desc = "syscall with kernel spec64"},
#endif

#if (__NR_clock_settime64 != __LTP__NR_INVALID_SYSCALL)
{ .gettime = sys_clock_gettime64, .settime = sys_clock_settime64, .type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec64"},
#endif
};

static void setup(void)
{
begin->type = change->type = end->type = variants[tst_variant].type;
tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
}

static void do_clock_gettime(struct test_variants *tv, struct tst_ts *ts)
{
int ret;

ret = tv->gettime(CLOCK_REALTIME, tst_ts_get(ts));
if (ret == -1)
tst_brk(TBROK | TERRNO, "clock_settime(CLOCK_REALTIME) failed");
}

static void verify_clock_settime(void)
{
struct test_variants *tv = &variants[tst_variant];
long long elapsed;

/* test 01: move forward */
do_clock_gettime(tv, begin);

SAFE_CLOCK_GETTIME(CLOCK_REALTIME, begin);

*change = tst_timespec_add_us(*begin, DELTA_US);
*change = tst_ts_add_us(*begin, DELTA_US);

if (clock_settime(CLOCK_REALTIME, change) != 0)
tst_brk(TBROK | TTERRNO, "could not set realtime change");
TEST(tv->settime(CLOCK_REALTIME, tst_ts_get(change)));
if (TST_RET == -1) {
tst_res(TFAIL | TTERRNO, "clock_settime(2) failed for clock %s",
tst_clock_name(CLOCK_REALTIME));
return;
}

SAFE_CLOCK_GETTIME(CLOCK_REALTIME, end);
do_clock_gettime(tv, end);

elapsed = tst_timespec_diff_us(*end, *begin);
elapsed = tst_ts_diff_us(*end, *begin);

if (elapsed >= DELTA_US && elapsed < (DELTA_US + DELTA_EPS))
tst_res(TPASS, "clock_settime(2): was able to advance time");
else
tst_res(TFAIL, "clock_settime(2): could not advance time");

/* test 02: move backward */
do_clock_gettime(tv, begin);

SAFE_CLOCK_GETTIME(CLOCK_REALTIME, begin);

*change = tst_timespec_sub_us(*begin, DELTA_US);
*change = tst_ts_sub_us(*begin, DELTA_US);

if (clock_settime(CLOCK_REALTIME, change) != 0)
tst_brk(TBROK | TTERRNO, "could not set realtime change");
TEST(tv->settime(CLOCK_REALTIME, tst_ts_get(change)));
if (TST_RET == -1) {
tst_res(TFAIL | TTERRNO, "clock_settime(2) failed for clock %s",
tst_clock_name(CLOCK_REALTIME));
return;
}

SAFE_CLOCK_GETTIME(CLOCK_REALTIME, end);
do_clock_gettime(tv, end);

elapsed = tst_timespec_diff_us(*end, *begin);
elapsed = tst_ts_diff_us(*end, *begin);

if (~(elapsed) <= DELTA_US && ~(elapsed) > (DELTA_US - DELTA_EPS))
tst_res(TPASS, "clock_settime(2): was able to recede time");
Expand All @@ -68,6 +110,8 @@ static void verify_clock_settime(void)

static struct tst_test test = {
.test_all = verify_clock_settime,
.test_variants = ARRAY_SIZE(variants),
.setup = setup,
.needs_root = 1,
.restore_wallclock = 1,
.bufs = (struct tst_buffers []) {
Expand Down
102 changes: 66 additions & 36 deletions testcases/kernel/syscalls/clock_settime/clock_settime02.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,51 @@
*/

#include "config.h"
#include "tst_test.h"
#include "lapi/syscalls.h"
#include "tst_timer.h"
#include "tst_safe_clocks.h"
#include "lapi/abisize.h"

#define DELTA_SEC 10
#define NSEC_PER_SEC (1000000000L)

static void *bad_addr;

struct test_case {
clockid_t type;
struct timespec newtime;
int exp_err;
int replace;
long tv_sec;
long tv_nsec;
};

struct test_case tc[] = {
{ /* case 01: REALTIME: timespec NULL */
.type = CLOCK_REALTIME,
.exp_err = EFAULT,
.replace = 1,
.tv_sec = 0,
.tv_nsec = 0,
},
{ /* case 02: REALTIME: tv_sec = -1 */
.type = CLOCK_REALTIME,
.newtime.tv_sec = -1,
.exp_err = EINVAL,
.replace = 1,
.tv_sec = -1,
.tv_nsec = 0,
},
{ /* case 03: REALTIME: tv_nsec = -1 */
.type = CLOCK_REALTIME,
.newtime.tv_nsec = -1,
.exp_err = EINVAL,
.replace = 1,
.tv_sec = 0,
.tv_nsec = -1,
},
{ /* case 04: REALTIME: tv_nsec = 1s+1 */
.type = CLOCK_REALTIME,
.newtime.tv_nsec = NSEC_PER_SEC + 1,
.exp_err = EINVAL,
.replace = 1,
.tv_sec = 0,
.tv_nsec = NSEC_PER_SEC + 1,
},
{ /* case 05: MONOTONIC */
.type = CLOCK_MONOTONIC,
Expand Down Expand Up @@ -83,64 +90,87 @@ struct test_case tc[] = {
},
};

/*
* Some tests may cause libc to segfault when passing bad arguments.
*/
static int sys_clock_settime(clockid_t clk_id, struct timespec *tp)
static struct tst_ts spec;

static struct test_variants {
int (*gettime)(clockid_t clk_id, void *ts);
int (*settime)(clockid_t clk_id, void *ts);
enum tst_ts_type type;
char *desc;
} variants[] = {
#if defined(TST_ABI32)
{ .gettime = sys_clock_gettime, .settime = sys_clock_settime, .type = TST_LIBC_TIMESPEC, .desc = "syscall with libc spec"},
{ .gettime = sys_clock_gettime, .settime = sys_clock_settime, .type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with kernel spec32"},
#endif

#if defined(TST_ABI64)
{ .gettime = sys_clock_gettime, .settime = sys_clock_settime, .type = TST_KERN_TIMESPEC, .desc = "syscall with kernel spec64"},
#endif

#if (__NR_clock_settime64 != __LTP__NR_INVALID_SYSCALL)
{ .gettime = sys_clock_gettime64, .settime = sys_clock_settime64, .type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec64"},
#endif
};

static void setup(void)
{
return tst_syscall(__NR_clock_settime, clk_id, tp);
tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);

bad_addr = tst_get_bad_addr(NULL);
}

static void verify_clock_settime(unsigned int i)
{
struct timespec spec, *specptr;
struct test_variants *tv = &variants[tst_variant];
void *ts;

specptr = &spec;
spec.type = tv->type;

if (tc[i].replace == 0) {

SAFE_CLOCK_GETTIME(CLOCK_REALTIME, specptr);
TEST(tv->gettime(CLOCK_REALTIME, tst_ts_get(&spec)));
if (TST_RET == -1) {
tst_res(TFAIL | TTERRNO, "clock_gettime(2) failed for clock %s",
tst_clock_name(CLOCK_REALTIME));
return;
}

/* add 1 sec to wall clock */
specptr->tv_sec += 1;

spec = tst_ts_add_us(spec, 1000000);
} else {

/* use given time spec */
*specptr = tc[i].newtime;
tst_ts_set_sec(&spec, tc[i].tv_sec);
tst_ts_set_nsec(&spec, tc[i].tv_nsec);
}

/* bad pointer case */
if (tc[i].exp_err == EFAULT)
specptr = tst_get_bad_addr(NULL);

TEST(sys_clock_settime(tc[i].type, specptr));

if (TST_RET == -1) {
ts = bad_addr;
else
ts = tst_ts_get(&spec);

if (tc[i].exp_err == TST_ERR) {
tst_res(TPASS | TTERRNO,
"clock_settime(%s): failed as expected",
tst_clock_name(tc[i].type));
return;
}
TEST(tv->settime(tc[i].type, ts));

tst_res(TFAIL | TTERRNO, "clock_settime(2): clock %s "
"expected to fail with %s",
if (TST_RET != -1) {
tst_res(TFAIL | TTERRNO, "clock_settime(2): clock %s passed unexpectedly, expected %s",
tst_clock_name(tc[i].type),
tst_strerrno(tc[i].exp_err));
return;
}

if (tc[i].exp_err == TST_ERR) {
tst_res(TPASS | TTERRNO, "clock_settime(%s): failed as expected",
tst_clock_name(tc[i].type));
return;
}

tst_res(TFAIL | TTERRNO, "clock_settime(2): clock %s passed "
"unexpectedly, expected %s",
tst_clock_name(tc[i].type),
tst_strerrno(tc[i].exp_err));
tst_res(TFAIL | TTERRNO, "clock_settime(2): clock %s " "expected to fail with %s",
tst_clock_name(tc[i].type), tst_strerrno(tc[i].exp_err));
}

static struct tst_test test = {
.test = verify_clock_settime,
.test_variants = ARRAY_SIZE(variants),
.setup = setup,
.tcnt = ARRAY_SIZE(tc),
.needs_root = 1,
.restore_wallclock = 1,
Expand Down

0 comments on commit bcf3733

Please sign in to comment.