testing/fs: add testcase to test the stability of memfd.

Supplement ostest test cases.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
This commit is contained in:
wangzhi16 2025-05-07 21:03:08 +08:00 committed by Donny(董九柱)
parent 094b0f235a
commit 5c6d646d76
5 changed files with 173 additions and 0 deletions

View file

@ -0,0 +1,23 @@
# ##############################################################################
# apps/testing/fs/memfd/CMakeLists.txt
#
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. The ASF licenses this
# file to you under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
# ##############################################################################
if(CONFIG_TESTING_MEMFD)
nuttx_add_application(NAME memfd SRCS memfd_test.c)
endif()

11
testing/fs/memfd/Kconfig Normal file
View file

@ -0,0 +1,11 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config TESTING_MEMFD
bool "Testing the stability of memfd"
depends on !LIBC_MEMFD_ERROR && MM_KASAN
default n
---help---
Enable to test the stability of memfd

View file

@ -0,0 +1,23 @@
############################################################################
# apps/testing/fs/memfd/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
ifneq ($(CONFIG_TESTING_MEMFD),)
CONFIGURED_APPS += $(APPDIR)/testing/fs/memfd
endif

29
testing/fs/memfd/Makefile Normal file
View file

@ -0,0 +1,29 @@
############################################################################
# apps/testing/fs/memfd/Makefile
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
include $(APPDIR)/Make.defs
PROGNAME = memfd_test
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = $(CONFIG_DEFAULT_TASK_STACKSIZE)
MAINSRC = memfd_test.c
include $(APPDIR)/Application.mk

View file

@ -0,0 +1,87 @@
/****************************************************************************
* apps/testing/fs/memfd/memfd_test.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define BUFFER_SIZE 8
#define THREAD_NUM 50
#define LOOP_NUM 1000
/****************************************************************************
* Private Functions
****************************************************************************/
static void *func(FAR void *arg)
{
void *buf;
int size = BUFFER_SIZE;
int i;
int memfd;
for (i = 0; i < LOOP_NUM; i++)
{
memfd = memfd_create("optee", O_CREAT | O_CLOEXEC);
ftruncate(memfd, size);
buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, 0);
close(memfd);
*((int *)buf) = 0xdeadbeef;
usleep(10);
*((int *)buf) = 0xdeadbeef;
munmap(buf, size);
}
printf("thread %d test pass!\n", pthread_self());
return NULL;
}
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, FAR char *argv[])
{
pid_t pid[THREAD_NUM];
int i;
for (i = 0; i < THREAD_NUM; i++)
{
pthread_create(&pid[i], NULL, func, NULL);
}
for (i = 0; i < THREAD_NUM; i++)
{
pthread_join(pid[i], NULL);
}
return 0;
}