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

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


The following commit(s) were added to refs/heads/master by this push:
     new ab3dde861 testing/libc/memfd_create: Add a test of memfd_create().
ab3dde861 is described below

commit ab3dde86164ab5bbf0b372f2513d97175404eec0
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Nov 10 21:46:46 2026 +0100

    testing/libc/memfd_create: Add a test of memfd_create().
    
    memfd_create() must make a new file with the flags 0, MFD_CLOEXEC and
    MFD_ALLOW_SEALING, and with a name longer than NAME_MAX: the name is only
    for debug, and Linux accepts names of up to 249 characters.  The test
    makes these files and checks FD_CLOEXEC, the size after ftruncate(),
    write() and read(), and that two files with the same name are two files.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/libc/memfd_create/CMakeLists.txt      |  35 +++++++
 testing/libc/memfd_create/Kconfig             |  31 ++++++
 testing/libc/memfd_create/Make.defs           |  25 +++++
 testing/libc/memfd_create/Makefile            |  36 +++++++
 testing/libc/memfd_create/memfd_create_main.c | 142 ++++++++++++++++++++++++++
 5 files changed, 269 insertions(+)

diff --git a/testing/libc/memfd_create/CMakeLists.txt 
b/testing/libc/memfd_create/CMakeLists.txt
new file mode 100644
index 000000000..f6afbf609
--- /dev/null
+++ b/testing/libc/memfd_create/CMakeLists.txt
@@ -0,0 +1,35 @@
+# 
##############################################################################
+# apps/testing/libc/memfd_create/CMakeLists.txt
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# 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_CREATE)
+  nuttx_add_application(
+    NAME
+    ${CONFIG_TESTING_MEMFD_CREATE_PROGNAME}
+    PRIORITY
+    ${CONFIG_TESTING_MEMFD_CREATE_PRIORITY}
+    STACKSIZE
+    ${CONFIG_TESTING_MEMFD_CREATE_STACKSIZE}
+    MODULE
+    ${CONFIG_TESTING_MEMFD_CREATE}
+    SRCS
+    memfd_create_main.c)
+endif()
diff --git a/testing/libc/memfd_create/Kconfig 
b/testing/libc/memfd_create/Kconfig
new file mode 100644
index 000000000..0d1f26bf8
--- /dev/null
+++ b/testing/libc/memfd_create/Kconfig
@@ -0,0 +1,31 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config TESTING_MEMFD_CREATE
+       tristate "memfd_create() test"
+       default n
+       depends on !LIBC_MEMFD_ERROR
+       ---help---
+               Check that memfd_create() makes a file with the flags 0,
+               MFD_CLOEXEC and MFD_ALLOW_SEALING, and with a long name.
+
+if TESTING_MEMFD_CREATE
+
+config TESTING_MEMFD_CREATE_PROGNAME
+       string "Program name"
+       default "memfd_create_test"
+       ---help---
+               This is the name of the program that will be used when the NSH 
ELF
+               program is installed.
+
+config TESTING_MEMFD_CREATE_PRIORITY
+       int "memfd_create_test task priority"
+       default 100
+
+config TESTING_MEMFD_CREATE_STACKSIZE
+       int "memfd_create_test stack size"
+       default DEFAULT_TASK_STACKSIZE
+
+endif
diff --git a/testing/libc/memfd_create/Make.defs 
b/testing/libc/memfd_create/Make.defs
new file mode 100644
index 000000000..da1db73a0
--- /dev/null
+++ b/testing/libc/memfd_create/Make.defs
@@ -0,0 +1,25 @@
+############################################################################
+# apps/testing/libc/memfd_create/Make.defs
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# 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_CREATE),)
+CONFIGURED_APPS += $(APPDIR)/testing/libc/memfd_create
+endif
diff --git a/testing/libc/memfd_create/Makefile 
b/testing/libc/memfd_create/Makefile
new file mode 100644
index 000000000..aa336422c
--- /dev/null
+++ b/testing/libc/memfd_create/Makefile
@@ -0,0 +1,36 @@
+############################################################################
+# apps/testing/libc/memfd_create/Makefile
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# 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
+
+# memfd_create() test built-in application info
+
+PROGNAME  = $(CONFIG_TESTING_MEMFD_CREATE_PROGNAME)
+PRIORITY  = $(CONFIG_TESTING_MEMFD_CREATE_PRIORITY)
+STACKSIZE = $(CONFIG_TESTING_MEMFD_CREATE_STACKSIZE)
+MODULE    = $(CONFIG_TESTING_MEMFD_CREATE)
+
+# memfd_create() test files
+
+MAINSRC = memfd_create_main.c
+
+include $(APPDIR)/Application.mk
diff --git a/testing/libc/memfd_create/memfd_create_main.c 
b/testing/libc/memfd_create/memfd_create_main.c
new file mode 100644
index 000000000..f5a30ce13
--- /dev/null
+++ b/testing/libc/memfd_create/memfd_create_main.c
@@ -0,0 +1,142 @@
+/****************************************************************************
+ * apps/testing/libc/memfd_create/memfd_create_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 <sys/mman.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Linux accepts names of up to 249 characters */
+
+#define MEMFD_CREATE_TEST_NAMELEN 200
+#define MEMFD_CREATE_TEST_SIZE    64
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* Print the result of one check.  Return the number of errors. */
+
+static int check(FAR const char *name, bool ok, int errcode)
+{
+  if (!ok)
+    {
+      printf("ERROR: %s: errno %d\n", name, errcode);
+      return 1;
+    }
+
+  printf("%s: OK\n", name);
+  return 0;
+}
+
+/* Make a file with memfd_create() and check that it is there */
+
+static int create(FAR const char *test, FAR const char *name,
+                  unsigned int flags)
+{
+  int fd;
+
+  errno = 0;
+  fd = memfd_create(name, flags);
+  if (fd >= 0)
+    {
+      close(fd);
+    }
+
+  return check(test, fd >= 0, errno);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  char longname[MEMFD_CREATE_TEST_NAMELEN + 1];
+  char buf[6];
+  struct stat st;
+  int nerrors = 0;
+  bool ok;
+  int fd;
+  int fd2;
+
+  printf("memfd_create test\n");
+
+  nerrors += create("flags 0", "memfd_test", 0);
+  nerrors += create("MFD_ALLOW_SEALING", "memfd_test",
+                    MFD_CLOEXEC | MFD_ALLOW_SEALING);
+
+  memset(longname, 'n', MEMFD_CREATE_TEST_NAMELEN);
+  longname[MEMFD_CREATE_TEST_NAMELEN] = '\0';
+  nerrors += create("long name", longname, MFD_CLOEXEC);
+
+  errno = 0;
+  fd = memfd_create("memfd_test", MFD_CLOEXEC);
+  ok = fd >= 0 && (fcntl(fd, F_GETFD) & FD_CLOEXEC) != 0;
+  nerrors += check("MFD_CLOEXEC", ok, errno);
+  if (fd < 0)
+    {
+      goto out;
+    }
+
+  /* The size first: a shmfs file has no memory before ftruncate() */
+
+  errno = 0;
+  ok = ftruncate(fd, MEMFD_CREATE_TEST_SIZE) == 0 && fstat(fd, &st) == 0 &&
+       st.st_size == MEMFD_CREATE_TEST_SIZE;
+  nerrors += check("ftruncate", ok, errno);
+
+  errno = 0;
+  ok = write(fd, "hello", 5) == 5 && lseek(fd, 0, SEEK_SET) == 0 &&
+       read(fd, buf, 5) == 5 && memcmp(buf, "hello", 5) == 0;
+  nerrors += check("write and read", ok, errno);
+
+  /* A second file with the same name is a new, empty file */
+
+  errno = 0;
+  fd2 = memfd_create("memfd_test", MFD_CLOEXEC);
+  ok = fd2 >= 0 && fstat(fd2, &st) == 0 && st.st_size == 0;
+  nerrors += check("same name", ok, errno);
+  if (fd2 >= 0)
+    {
+      close(fd2);
+    }
+
+  close(fd);
+
+out:
+  printf("memfd_create test: %d errors\n", nerrors);
+  return nerrors > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}

Reply via email to