修复ppoll接口"[ERR]OsMemFree check error!"报错

【背景】
内核中释放用户空间指针报错:"[ERR]OsMemFree check error!"

【修改方案】
修改SysPpoll函数。

【影响】
对现有的产品编译不会有影响。

re #I47YWZ

Change-Id: Id7f86036870d4f32be8fc438b9aad85cdda59546
Signed-off-by: pef <cyd1997@126.com>
This commit is contained in:
pef 2021-10-28 11:47:10 +00:00
parent a55f68f957
commit 78a297fd4e
6 changed files with 183 additions and 91 deletions

View File

@ -228,7 +228,7 @@ int OsSigprocMask(int how, const sigset_t_l *setl, sigset_t_l *oldset)
int retVal;
if (setl != NULL) {
retVal = LOS_ArchCopyFromUser(&set, &(setl->sig[0]), sizeof(sigset_t));
retVal = LOS_CopyToKernel(&set, sizeof(sigset_t), &(setl->sig[0]), sizeof(sigset_t));
if (retVal != 0) {
return -EFAULT;
}
@ -268,7 +268,7 @@ int OsSigprocMask(int how, const sigset_t_l *setl, sigset_t_l *oldset)
SCHEDULER_UNLOCK(intSave);
if (oldset != NULL) {
retVal = LOS_ArchCopyToUser(&(oldset->sig[0]), &oldSigprocmask, sizeof(sigset_t));
retVal = LOS_CopyFromKernel(&(oldset->sig[0]), sizeof(sigset_t), &oldSigprocmask, sizeof(sigset_t));
if (retVal != 0) {
return -EFAULT;
}

View File

@ -2518,39 +2518,42 @@ int SysPpoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmo_p, cons
{
int timeout;
int ret;
int retVal;
sigset_t_l origMask;
sigset_t_l setl;
CHECK_ASPACE(tmo_p, sizeof(struct timespec));
CHECK_ASPACE(sigMask, sizeof(sigset_t));
DUP_FROM_USER(tmo_p, sizeof(struct timespec));
DUP_FROM_USER(sigMask, sizeof(sigset_t), FREE_DUP(tmo_p));
CPY_FROM_USER(tmo_p);
CPY_FROM_USER(sigMask);
if (tmo_p != NULL) {
timeout = tmo_p->tv_sec * OS_SYS_US_PER_MS + tmo_p->tv_nsec / OS_SYS_NS_PER_MS;
if (timeout & 0x80000000) {
ret = -EINVAL;
printf("ret[1] = %d\n", ret);
return ret;
if (timeout < 0) {
return -EINVAL;
}
} else {
timeout = -1;
}
if (sigMask != NULL) {
memcpy(&setl.sig[0], sigMask, sizeof(sigset_t));
memcpy_s(&setl.sig[0], sizeof(sigset_t), sigMask, sizeof(sigset_t));
}
OsSigprocMask(SIG_SETMASK, sigMask?(&setl):NULL, &origMask);
ret = OsSigprocMask(SIG_SETMASK, sigMask ? &setl : NULL, &origMask);
if (ret != 0) {
return -EINVAL;
}
ret = SysPoll(fds, nfds, timeout);
if (ret < 0) {
ret = -get_errno();
retVal = -get_errno();
}
ret = OsSigprocMask(SIG_SETMASK, &origMask, NULL);
if (ret != 0) {
return -EINVAL;
}
OsSigprocMask(SIG_SETMASK, &origMask, NULL);
FREE_DUP(tmo_p);
FREE_DUP(sigMask);
return (ret == -1) ? -get_errno() : ret;
return retVal;
}
int SysPselect6(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,

View File

@ -110,7 +110,7 @@ Please deal with the "char *" by function:UserPathCopy.
#define CPY_FROM_USER(ptr) \
__typeof(*ptr) ptr##cpy = {0}, *ptr##bak = ptr; \
if (ptr != NULL) { \
if (LOS_ArchCopyFromUser((void*)&ptr##cpy, ptr, sizeof(*ptr)) != 0) { \
if (LOS_ArchCopyFromUser((void*)&ptr##cpy, ptr##bak, sizeof(*ptr##bak)) != 0) { \
set_errno(EFAULT); \
return -get_errno(); \
} \

View File

@ -1,36 +1,66 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_test_IO.h"
#include "pthread.h"
#define BUF_SIZE 128
#define DELAY_TIME 200
const int BUF_SIZE = 128;
const int DELAY_TIME = 200;
STATIC INT32 pipeFdPpoll[2];
STATIC INT32 g_step = 1;
STATIC CHAR strBuf[] = "hello world.";
STATIC struct pollfd pfd;
static INT32 pipeFdPpoll[2];
static INT32 g_step = 1;
static CHAR strBuf[] = "hello world.";
static struct pollfd pfd;
STATIC VOID *pthread_01(VOID *arg)
static void *pthread_01(void *arg)
{
INT32 retVal;
CHAR buf[BUF_SIZE];
const struct timespec timeout = {1000, 0};
/* 执行ppoll监视文件描述符 */
while(g_step < 3) {
while (g_step < 3) { /* 3, 3rd step */
usleep(DELAY_TIME);
}
g_step++;
retVal = ppoll(&pfd, 1, NULL, NULL);
ICUNIT_ASSERT_NOT_EQUAL_NULL(retVal, -1, retVal);
while(g_step < 5) {
while (g_step < 5) { /* 5, 5th step */
usleep(DELAY_TIME);
}
g_step++;
/* 判断revents */
if (pfd.revents & POLLIN) {
memset(buf, 0, sizeof(buf));
memset_s(buf, sizeof(buf), 0, sizeof(buf));
retVal = read(pfd.fd, buf, BUF_SIZE);
ICUNIT_ASSERT_NOT_EQUAL_NULL(retVal, -1, retVal);
@ -38,7 +68,7 @@ STATIC VOID *pthread_01(VOID *arg)
ICUNIT_ASSERT_EQUAL_NULL(retVal, 0, retVal);
}
while(g_step < 6) {
while (g_step < 6) { /* 6, 6th step */
usleep(DELAY_TIME);
}
pthread_exit(NULL);
@ -50,7 +80,7 @@ STATIC UINT32 testcase(VOID)
pthread_t tid;
/* 建立管道 */
while(g_step < 1) {
while (g_step < 1) { /* 1, 1st step */
usleep(DELAY_TIME);
}
retVal = pipe(pipeFdPpoll);
@ -62,7 +92,7 @@ STATIC UINT32 testcase(VOID)
pfd.events = POLLIN;
/* 开辟线程执行 ppoll */
while(g_step < 2) {
while (g_step < 2) { /* 2, 2nd step */
usleep(DELAY_TIME);
}
retVal = pthread_create(&tid, NULL, pthread_01, NULL);
@ -70,7 +100,7 @@ STATIC UINT32 testcase(VOID)
g_step++;
/* 向管道写入数据 */
while(g_step < 4) {
while (g_step < 4) { /* 4, 4th step */
usleep(DELAY_TIME);
}
sleep(1);

View File

@ -1,9 +1,40 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_test_IO.h"
#include "signal.h"
#include "pthread.h"
#define BUF_SIZE 128
#define DELAY_TIME 200
const int BUF_SIZE = 128;
const int DELAY_TIME = 200;
static int pipeFdPpoll[2];
static int g_step = 1;
@ -17,7 +48,7 @@ static void *pthread_01(void *arg)
const struct timespec timeout = {10000, 0};
/* 执行ppoll监视文件描述符 */
while(g_step < 4) {
while (g_step < 4) { /* 4, 4th step */
usleep(DELAY_TIME);
}
g_step++;
@ -26,14 +57,14 @@ static void *pthread_01(void *arg)
/* 判断revents */
if (pfd.revents & POLLIN) {
memset(buf, 0, sizeof(buf));
memset_s(buf, sizeof(buf), 0, sizeof(buf));
retVal = read(pfd.fd, buf, BUF_SIZE);
ICUNIT_ASSERT_NOT_EQUAL_NULL(retVal, -1, retVal);
retVal = strcmp(strBuf, buf);
ICUNIT_ASSERT_EQUAL_NULL(retVal, 0, retVal);
}
while(g_step < 5) {
while (g_step < 5) { /* 5, 5th step */
usleep(DELAY_TIME);
}
pthread_exit(NULL);
@ -57,7 +88,7 @@ static UINT32 testcase(VOID)
pfd.events = POLLIN;
/* 向管道写入数据 */
while(g_step < 2) {
while (g_step < 2) { /* 2, 2nd step */
usleep(DELAY_TIME);
}
sleep(1);
@ -67,7 +98,7 @@ static UINT32 testcase(VOID)
g_step++;
/* 开辟线程执行 ppoll */
while(g_step < 3) {
while (g_step < 3) { /* 3, 3rd step */
usleep(DELAY_TIME);
}
retVal = pthread_create(&tid, NULL, pthread_01, NULL);

View File

@ -1,35 +1,64 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_test_IO.h"
#include "pthread.h"
#include "signal.h"
#define BUF_SIZE 128
#define DELAY_TIME 200
const int BUF_SIZE = 128;
const int DELAY_TIME = 200;
STATIC INT32 pipeFdPpoll[2];
STATIC INT32 g_step = 1;
STATIC CHAR strBuf[] = "hello world.";
STATIC struct pollfd pfd;
sigset_t sigMask;
STATIC UINT32 count = 0;
static INT32 pipeFdPpoll[2];
static INT32 g_step = 1;
static CHAR strBuf[] = "hello world.";
static struct pollfd pfd;
static sigset_t sigMask;
static UINT32 count = 0;
STATIC VOID signalHandle(INT32 sigNum)
static void signalHandle(INT32 sigNum)
{
//printf("Capture %d\n", sigNum);
g_step++;
return;
}
STATIC VOID *pthread_01(VOID *arg)
static void *pthread_01(void *arg)
{
INT32 retVal;
CHAR buf[BUF_SIZE];
signal(SIGUSR1, signalHandle);
(void)signal(SIGUSR1, signalHandle);
while (1) {
/* 执行ppoll监视文件描述符 */
while (g_step < 2) {
while (g_step < 2) { /* 2, 2nd step */
usleep(DELAY_TIME);
}
g_step++;
@ -39,7 +68,7 @@ STATIC VOID *pthread_01(VOID *arg)
/* 判断revents */
if (pfd.revents & POLLIN) {
memset(buf, 0, sizeof(buf));
memset_s(buf, sizeof(buf), 0, sizeof(buf));
retVal = read(pfd.fd, buf, BUF_SIZE);
ICUNIT_ASSERT_NOT_EQUAL_NULL(retVal, -1, retVal);
@ -52,17 +81,16 @@ STATIC VOID *pthread_01(VOID *arg)
}
g_step++;
if (g_step >= 7) {
ICUNIT_ASSERT_EQUAL_NULL(count, 2, count);
if (g_step >= 7) { /* 7, 7th step */
ICUNIT_ASSERT_EQUAL_NULL(count, 2, count); /* 2, 2nd step */
pthread_exit(NULL);
}
}
return LOS_OK;
}
STATIC UINT32 testcase(VOID)
static UINT32 testcase(VOID)
{
INT32 retVal;
pthread_t tid;
@ -88,7 +116,7 @@ STATIC UINT32 testcase(VOID)
g_step++;
/* 向管道写入数据 */
while (g_step < 3) {
while (g_step < 3) { /* 3, 3ed step */
usleep(DELAY_TIME);
}
sleep(1); /* 保证先挂起再写入数据 */
@ -96,7 +124,7 @@ STATIC UINT32 testcase(VOID)
ICUNIT_ASSERT_NOT_EQUAL(retVal, -1, retVal);
/* 向线程发送信号 */
while (g_step < 5) {
while (g_step < 5) { /* 5, 5th step */
usleep(DELAY_TIME);
}
sleep(1); /* 保证先挂起再发送信号 */
@ -104,14 +132,14 @@ STATIC UINT32 testcase(VOID)
ICUNIT_ASSERT_EQUAL(retVal, 0, retVal);
/* 继续向管道写入数据 */
ICUNIT_ASSERT_EQUAL(g_step, 5, g_step); /*判断挂起解除之前信号没有被处理*/
ICUNIT_ASSERT_EQUAL(g_step, 5, g_step); /* 5, sth。判断挂起解除之前信号没有被处理 */
retVal = write(pipeFdPpoll[1], "hello world.", sizeof(strBuf));
ICUNIT_ASSERT_NOT_EQUAL(retVal, -1, retVal);
while (g_step < 7) {
while (g_step < 7) { /* 7, 7th step */
usleep(DELAY_TIME);
}
ICUNIT_ASSERT_EQUAL(count, 2, count);
ICUNIT_ASSERT_EQUAL(count, 2, count); /* 2, 2nd step */
/* 等待退出 */
pthread_join(tid, NULL);