[LTP] [PATCH] [PATCH] Migrating the libhugetlbfs/testcases/ptrace-write-hugepage.c
Pavithra
pavrampu@linux.ibm.com
Fri Nov 28 05:47:28 CET 2025
This test uses ptrace() to poke and peek data in a child process that has mapped a hugepage
This test creates a child process, has it map a hugepage, and
then the parent process attaches to the child using ptrace() to modify and verify the data in the hugepage
Signed-off-by: Pavithra <pavrampu@linux.ibm.com>
---
.../kernel/mem/hugetlb/hugemmap/hugemem41.c | 178 ++++++++++++++++++
1 file changed, 178 insertions(+)
create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemem41.c
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemem41.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemem41.c
new file mode 100644
index 000000000..a6bf9963b
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemem41.c
@@ -0,0 +1,178 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * libhugetlbfs - Easy use of Linux hugepages
+ * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/ptrace.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include "hugetlb.h"
+
+#define CONST 0xdeadbeefL
+#define MNTPOINT "hugetlbfs/"
+
+static long hpage_size;
+static volatile int ready_to_trace;
+static int fd;
+
+static void sigchld_handler()
+{
+ int status;
+
+ wait(&status);
+ if (WIFEXITED(status))
+ exit(WEXITSTATUS(status));
+ else if (WIFSIGNALED(status))
+ exit(status);
+
+ ready_to_trace = 1;
+}
+
+static void child(int hugefd, int pipefd)
+{
+ void *p;
+ int err;
+
+ p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
+ hugefd, 0);
+
+ memset(p, 0, hpage_size);
+
+ tst_res(TINFO, "Child mapped data at %p\n", p);
+
+ err = write(pipefd, &p, sizeof(p));
+ if (err == -1)
+ tst_res(TFAIL, "Writing to pipe: %s", strerror(errno));
+ if (err != sizeof(p))
+ tst_res(TFAIL, "Short write to pipe");
+
+ pause();
+}
+
+static void do_poke(pid_t pid, void *p)
+{
+ long err;
+
+ tst_res(TINFO, "Poking...");
+ err = ptrace(PTRACE_POKEDATA, pid, p, (void *)CONST);
+ if (err)
+ tst_res(TFAIL, "ptrace(POKEDATA): %s", strerror(errno));
+ tst_res(TINFO, "done");
+
+ tst_res(TINFO, "Peeking...");
+ err = ptrace(PTRACE_PEEKDATA, pid, p, NULL);
+ if (err == -1)
+ tst_res(TFAIL, "ptrace(PEEKDATA): %s", strerror(errno));
+
+ if (err != CONST)
+ tst_res(TFAIL, "mismatch (%lx instead of %lx)", err, CONST);
+ tst_res(TINFO, "done");
+}
+
+static void run_test(void)
+{
+ int pipefd[2];
+ long err;
+ pid_t cpid;
+ void *p;
+ int signal;
+
+ struct sigaction sa = {
+ .sa_sigaction = sigchld_handler,
+ .sa_flags = SA_SIGINFO,
+ };
+ struct sigaction old_sa;
+
+ hpage_size = tst_get_hugepage_size();
+ fd = tst_creat_unlinked(MNTPOINT, 0, 0600);
+
+ if (fd < 0)
+ tst_res(TFAIL, "hugetlbfs_unlinked_fd()");
+
+ err = sigaction(SIGCHLD, &sa, &old_sa);
+ if (err)
+ tst_res(TFAIL, "Can't install SIGCHLD handler: %s", strerror(errno));
+
+ err = pipe(pipefd);
+ if (err)
+ tst_res(TFAIL, "pipe(): %s", strerror(errno));
+
+ cpid = fork();
+ if (cpid < 0)
+ tst_res(TFAIL, "fork(): %s", strerror(errno));
+
+
+ if (cpid == 0) {
+ child(fd, pipefd[1]);
+ exit(0);
+ }
+
+ /* Parent */
+ err = read(pipefd[0], &p, sizeof(p));
+ if (err == -1)
+ tst_res(TFAIL, "Reading pipe: %s\n", strerror(errno));
+ if (err != sizeof(p))
+ tst_res(TFAIL, "Short read over pipe");
+
+ tst_res(TINFO, "Parent received address %p\n", p);
+
+ err = ptrace(PTRACE_ATTACH, cpid, NULL, NULL);
+ if (err)
+ tst_res(TFAIL, "ptrace(ATTACH): %s", strerror(errno));
+
+ while (!ready_to_trace)
+ ;
+
+ do_poke(cpid, p);
+ do_poke(cpid, p + getpagesize());
+
+ err = sigaction(SIGCHLD, &old_sa, NULL);
+ if (err)
+ tst_res(TFAIL, "Clearing SIGCHLD handler: %s", strerror(errno));
+
+ ptrace(PTRACE_KILL, cpid, NULL, NULL);
+ waitpid(cpid, &signal, 0);
+
+ tst_res(TPASS, "Test passed!");
+}
+
+static void cleanup(void)
+{
+ if (fd >= 0)
+ SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+ .tags = (struct tst_tag[]) {
+ {"linux-git", "ebed4bfc8da8"},
+ {}
+ },
+ .needs_root = 1,
+ .mntpoint = MNTPOINT,
+ .needs_hugetlbfs = 1,
+ .hugepages = {4, TST_NEEDS},
+ .cleanup = cleanup,
+ .test_all = run_test,
+};
--
2.43.5
More information about the ltp
mailing list