From a53ad1290752b8d7076f2a8fe52a1e25fa209f91 Mon Sep 17 00:00:00 2001 From: teamol Date: Thu, 29 Apr 2021 15:41:33 +0800 Subject: [PATCH 1/2] fix time mould by ChinaSoft Team Change-Id: I391eccfa8591feb95dcb5b74fba3788f63b981bf --- compat/posix/src/time.c | 117 +++++++++++++++++- .../unittest/time/clock/time_clock_test.cpp | 24 ++++ .../unittest/time/timer/time_timer_test.cpp | 27 +++- 3 files changed, 162 insertions(+), 6 deletions(-) diff --git a/compat/posix/src/time.c b/compat/posix/src/time.c index 97a82ae0..010a68a9 100644 --- a/compat/posix/src/time.c +++ b/compat/posix/src/time.c @@ -50,6 +50,8 @@ #include "los_swtmr_pri.h" #include "los_sys_pri.h" +#define CPUCLOCK_PERTHREAD_MASK 4 +#define CPUCLOCK_ID_OFFSET 3 /* * Do a time package defined return. This requires the error code @@ -450,13 +452,85 @@ int clock_settime(clockid_t clockID, const struct timespec *tp) return settimeofday(&tv, NULL); } +static int PthreadGetCputime(clockid_t clockID, struct timespec *ats) +{ + uint64_t runtime; + UINT32 intSave; + UINT32 tid = ((UINT32) ~((clockID) >> CPUCLOCK_ID_OFFSET)); + + if (OS_TID_CHECK_INVALID(tid)) { + return -ESRCH; + } + LosTaskCB *task = OsGetTaskCB(tid); + + if (OsCurrTaskGet()->processID != task->processID) { + return -EINVAL; + } + +#ifdef LOSCFG_KERNEL_CPUP + SCHEDULER_LOCK(intSave); + runtime = task->taskCpup.allTime; + SCHEDULER_UNLOCK(intSave); +#endif + + ats->tv_sec = runtime / OS_SYS_NS_PER_SECOND; + ats->tv_nsec = runtime % OS_SYS_NS_PER_SECOND; + + return 0; +} + +static int ProcessGetCputime(clockid_t clockID, struct timespec *ats) +{ + UINT64 runtime; + UINT32 intSave; + const pid_t pid = ((pid_t) ~((clockID) >> CPUCLOCK_ID_OFFSET)); + LosProcessCB *spcb = NULL; + + if (OsProcessIDUserCheckInvalid(pid) || pid < 0) { + return -EINVAL; + } + + spcb = OS_PCB_FROM_PID(pid); + if (OsProcessIsUnused(spcb)) { + return -EINVAL; + } + +#ifdef LOSCFG_KERNEL_CPUP + SCHEDULER_LOCK(intSave); + runtime = spcb->processCpup.allTime; + SCHEDULER_UNLOCK(intSave); +#endif + + ats->tv_sec = runtime / OS_SYS_NS_PER_SECOND; + ats->tv_nsec = runtime % OS_SYS_NS_PER_SECOND; + + return 0; +} + +static int GetCputime(clockid_t clockID, struct timespec *tp) +{ + int ret; + + if (clockID >= 0) { + return -EINVAL; + } + + if ((UINT32)clockID & CPUCLOCK_PERTHREAD_MASK) { + ret = PthreadGetCputime(clockID, tp); + } else { + ret = ProcessGetCputime(clockID, tp); + } + + return ret; +} + int clock_gettime(clockid_t clockID, struct timespec *tp) { UINT32 intSave; struct timespec64 tmp = {0}; struct timespec64 hwTime = {0}; - if ((clockID > MAX_CLOCKS) || (clockID < CLOCK_REALTIME)) { + if (clockID > MAX_CLOCKS) { goto ERROUT; } @@ -497,7 +571,10 @@ int clock_gettime(clockid_t clockID, struct timespec *tp) case CLOCK_TAI: TIME_RETURN(ENOTSUP); default: - goto ERROUT; + { + int ret = GetCputime(clockID, tp); + TIME_RETURN(-ret); + } } return 0; @@ -506,6 +583,37 @@ ERROUT: TIME_RETURN(EINVAL); } +static int CheckClock(const clockid_t clockID) +{ + int error = 0; + const pid_t pid = ((pid_t) ~((clockID) >> CPUCLOCK_ID_OFFSET)); + + if (!((UINT32)clockID & CPUCLOCK_PERTHREAD_MASK)) { + LosProcessCB *spcb = NULL; + if (OsProcessIDUserCheckInvalid(pid) || pid < 0) { + return -EINVAL; + } + spcb = OS_PCB_FROM_PID(pid); + if (OsProcessIsUnused(spcb)) { + error = -EINVAL; + } + } else { + error = -EINVAL; + } + + return error; +} + +static int CpuClockGetres(const clockid_t clockID, struct timespec *tp) +{ + int error = CheckClock(clockID); + if (!error) { + error = ProcessGetCputime(clockID, tp); + } + + return error; +} + int clock_getres(clockid_t clockID, struct timespec *tp) { if (tp == NULL) { @@ -536,7 +644,10 @@ int clock_getres(clockid_t clockID, struct timespec *tp) case CLOCK_TAI: TIME_RETURN(ENOTSUP); default: - TIME_RETURN(EINVAL); + { + int ret = CpuClockGetres(clockID, tp); + TIME_RETURN(-ret); + } } TIME_RETURN(0); diff --git a/testsuites/unittest/time/clock/time_clock_test.cpp b/testsuites/unittest/time/clock/time_clock_test.cpp index 0d73d34b..43655d17 100644 --- a/testsuites/unittest/time/clock/time_clock_test.cpp +++ b/testsuites/unittest/time/clock/time_clock_test.cpp @@ -133,7 +133,9 @@ HWTEST_F(TimeClockTest, ClockTest009, TestSize.Level0) { ClockTest009(); } +#endif +#if defined(LOSCFG_USER_TEST_FULL) /* * * @tc.name: ClockTest010 * @tc.desc: function for TimeClockTest @@ -145,5 +147,27 @@ HWTEST_F(TimeClockTest, ClockTest010, TestSize.Level0) ClockTest010(); } +/* * + * @tc.name: ClockTest011 + * @tc.desc: test pthread_getcpuclockid:get pthread time + * @tc.type: FUNC + * @tc.require: AR000E0QAB + */ +HWTEST_F(TimeClockTest, ClockTest011, TestSize.Level0) +{ + ClockTest011(); +} + +/* * + * @tc.name: ClockTest012 + * @tc.desc: test clock_getcpuclockid:get process time + * @tc.type: FUNC + * @tc.require: AR000E0QAB + */ +HWTEST_F(TimeClockTest, ClockTest012, TestSize.Level0) +{ + ClockTest012(); +} + #endif } // namespace OHOS diff --git a/testsuites/unittest/time/timer/time_timer_test.cpp b/testsuites/unittest/time/timer/time_timer_test.cpp index d25aaa3b..187ea911 100644 --- a/testsuites/unittest/time/timer/time_timer_test.cpp +++ b/testsuites/unittest/time/timer/time_timer_test.cpp @@ -82,10 +82,31 @@ HWTEST_F(TimeTimerTest, TimerTest003, TestSize.Level0) * @tc.type: FUNC * @tc.require: AR000EEMQ9 */ -/* HWTEST_F(TimeTimerTest, TimerTest004, TestSize.Level0) +/*HWTEST_F(TimeTimerTest, TimerTest004, TestSize.Level0) { - TimerTest004(); -} */ + TimerTest004(); // TODO: musl sigaction handler have only one param. +}*/ +/* * + * @tc.name: TIME_TEST_TZSET_001 + * @tc.desc: function for TIME_TEST_TZSET_001 + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(TimeTimerTest, TIME_TEST_TZSET_001, TestSize.Level0) +{ + TIME_TEST_TZSET_001(); +} + +/* * + * @tc.name: TIME_TEST_TZSET_002 + * @tc.desc: function for TimeTimerTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(TimeTimerTest, TIME_TEST_TZSET_002, TestSize.Level0) +{ + TIME_TEST_TZSET_002(); +} #endif } // namespace OHOS From e8427a45c50cd1e503b9d05773d87c434feb7494 Mon Sep 17 00:00:00 2001 From: teamol Date: Fri, 14 May 2021 15:10:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix=20time=20mould-kernel=EF=BC=9Afix=202?= =?UTF-8?q?=20api=20clock=5Fgettime=E3=80=81clock=5Fgetres?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I260e47c895ced01e033ef8a824744e574887c9f5 --- compat/posix/src/time.c | 23 +++++++++---- .../posix/pthread/posix_pthread_test.cpp | 11 +++++++ .../process/mutex/process_mutex_test.cpp | 32 +++++++++++++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/compat/posix/src/time.c b/compat/posix/src/time.c index 010a68a9..6c262960 100644 --- a/compat/posix/src/time.c +++ b/compat/posix/src/time.c @@ -467,11 +467,9 @@ static int PthreadGetCputime(clockid_t clockID, struct timespec *ats) return -EINVAL; } -#ifdef LOSCFG_KERNEL_CPUP SCHEDULER_LOCK(intSave); runtime = task->taskCpup.allTime; SCHEDULER_UNLOCK(intSave); -#endif ats->tv_sec = runtime / OS_SYS_NS_PER_SECOND; ats->tv_nsec = runtime % OS_SYS_NS_PER_SECOND; @@ -495,11 +493,9 @@ static int ProcessGetCputime(clockid_t clockID, struct timespec *ats) return -EINVAL; } -#ifdef LOSCFG_KERNEL_CPUP SCHEDULER_LOCK(intSave); runtime = spcb->processCpup.allTime; SCHEDULER_UNLOCK(intSave); -#endif ats->tv_sec = runtime / OS_SYS_NS_PER_SECOND; ats->tv_nsec = runtime % OS_SYS_NS_PER_SECOND; @@ -529,6 +525,7 @@ int clock_gettime(clockid_t clockID, struct timespec *tp) UINT32 intSave; struct timespec64 tmp = {0}; struct timespec64 hwTime = {0}; + int ret; if (clockID > MAX_CLOCKS) { goto ERROUT; @@ -572,8 +569,12 @@ int clock_gettime(clockid_t clockID, struct timespec *tp) TIME_RETURN(ENOTSUP); default: { - int ret = GetCputime(clockID, tp); +#ifdef LOSCFG_KERNEL_CPUP + ret = GetCputime(clockID, tp); TIME_RETURN(-ret); +#else + TIME_RETURN(EINVAL); +#endif } } @@ -606,6 +607,10 @@ static int CheckClock(const clockid_t clockID) static int CpuClockGetres(const clockid_t clockID, struct timespec *tp) { + if (clockID > 0) { + return -EINVAL; + } + int error = CheckClock(clockID); if (!error) { error = ProcessGetCputime(clockID, tp); @@ -616,6 +621,8 @@ static int CpuClockGetres(const clockid_t clockID, struct timespec *tp) int clock_getres(clockid_t clockID, struct timespec *tp) { + int ret; + if (tp == NULL) { TIME_RETURN(EINVAL); } @@ -644,10 +651,14 @@ int clock_getres(clockid_t clockID, struct timespec *tp) case CLOCK_TAI: TIME_RETURN(ENOTSUP); default: +#ifdef LOSCFG_KERNEL_CPUP { - int ret = CpuClockGetres(clockID, tp); + ret = CpuClockGetres(clockID, tp); TIME_RETURN(-ret); } +#else + TIME_RETURN(EINVAL); +#endif } TIME_RETURN(0); diff --git a/testsuites/unittest/posix/pthread/posix_pthread_test.cpp b/testsuites/unittest/posix/pthread/posix_pthread_test.cpp index 99821340..5b28bf5f 100644 --- a/testsuites/unittest/posix/pthread/posix_pthread_test.cpp +++ b/testsuites/unittest/posix/pthread/posix_pthread_test.cpp @@ -327,6 +327,17 @@ HWTEST_F(PosixPthreadTest, ItPosixPthread022, TestSize.Level0) ItPosixPthread022(); // pthread_cancel } +/* * + * @tc.name: IT_POSIX_PTHREAD_203 + * @tc.desc: function for pthread concurrency + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(PosixPthreadTest, ItPosixPthread203, TestSize.Level0) +{ + ItPosixPthread203(); +} + #endif #if defined(LOSCFG_USER_TEST_FULL) diff --git a/testsuites/unittest/process/mutex/process_mutex_test.cpp b/testsuites/unittest/process/mutex/process_mutex_test.cpp index 1b938cff..a2fdea35 100644 --- a/testsuites/unittest/process/mutex/process_mutex_test.cpp +++ b/testsuites/unittest/process/mutex/process_mutex_test.cpp @@ -293,4 +293,36 @@ HWTEST_F(ProcessMutexTest, ItTestPthreadMutex022, TestSize.Level0) ItTestPthreadMutex022(); } +/* * + * @tc.name: it_test_pthread_mutex_023 + * @tc.desc: function for test mutexattr robust + * @tc.type: FUNC + * @tc.require: AR000E0QAB + */ +HWTEST_F(ProcessMutexTest, ItTestPthreadMutex023, TestSize.Level0) +{ + ItTestPthreadMutex023(); +} + +/* * + * @tc.name: it_test_pthread_mutex_024 + * @tc.desc: function for test mutexattr robust:error return value + * @tc.type: FUNC + * @tc.require: AR000E0QAB + */ +HWTEST_F(ProcessMutexTest, ItTestPthreadMutex024, TestSize.Level0) +{ + ItTestPthreadMutex024(); +} + +/* * + * @tc.name: it_test_pthread_mutex_025 + * @tc.desc: test mutexattr robust:robustness product deadlock is not set + * @tc.type: FUNC + * @tc.require: AR000E0QAB + */ +HWTEST_F(ProcessMutexTest, ItTestPthreadMutex025, TestSize.Level0) +{ + ItTestPthreadMutex025(); +} } // namespace OHOS