This is an automated email from the ASF dual-hosted git repository.

jiuzhudong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 5c6d646d7656b6d3cd4192771e69b22fdbd25c0f
Author: wangzhi16 <[email protected]>
AuthorDate: Wed May 7 21:03:08 2025 +0800

    testing/fs: add testcase to test the stability of memfd.
    
    Supplement ostest test cases.
    
    Signed-off-by: wangzhi16 <[email protected]>
---
 testing/fs/memfd/CMakeLists.txt | 23 +++++++++++
 testing/fs/memfd/Kconfig        | 11 ++++++
 testing/fs/memfd/Make.defs      | 23 +++++++++++
 testing/fs/memfd/Makefile       | 29 ++++++++++++++
 testing/fs/memfd/memfd_test.c   | 87 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 173 insertions(+)

diff --git a/testing/fs/memfd/CMakeLists.txt b/testing/fs/memfd/CMakeLists.txt
new file mode 100644
index 000000000..bde183129
--- /dev/null
+++ b/testing/fs/memfd/CMakeLists.txt
@@ -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()
diff --git a/testing/fs/memfd/Kconfig b/testing/fs/memfd/Kconfig
new file mode 100644
index 000000000..464537bbf
--- /dev/null
+++ b/testing/fs/memfd/Kconfig
@@ -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
diff --git a/testing/fs/memfd/Make.defs b/testing/fs/memfd/Make.defs
new file mode 100644
index 000000000..286699d9e
--- /dev/null
+++ b/testing/fs/memfd/Make.defs
@@ -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
diff --git a/testing/fs/memfd/Makefile b/testing/fs/memfd/Makefile
new file mode 100644
index 000000000..0080d0f48
--- /dev/null
+++ b/testing/fs/memfd/Makefile
@@ -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
diff --git a/testing/fs/memfd/memfd_test.c b/testing/fs/memfd/memfd_test.c
new file mode 100644
index 000000000..1e72915bf
--- /dev/null
+++ b/testing/fs/memfd/memfd_test.c
@@ -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;
+}

Reply via email to