[CRIU] [PATCH 7/8] cr: introduced the functions decode_pointer() and encode_pointer()

Alexander Kartashov alekskartashov at parallels.com
Fri Jan 18 02:08:38 EST 2013


These functions are designated to convert a native pointer
to uint64_t used to store a virtual address in protobuf messages
and vice versa in a machine-independent way.

Signed-off-by: Alexander Kartashov <alekskartashov at parallels.com>
---
 arch/x86/include/asm/types.h |    3 +++
 cr-dump.c                    |    2 +-
 cr-restore.c                 |   10 +++++-----
 parasite-syscall.c           |    6 +++---
 pie/parasite.c               |    6 +++---
 pie/restorer.c               |   10 +++++-----
 6 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/arch/x86/include/asm/types.h b/arch/x86/include/asm/types.h
index 84dbabe..d75761b 100644
--- a/arch/x86/include/asm/types.h
+++ b/arch/x86/include/asm/types.h
@@ -282,4 +282,7 @@ typedef UserX86RegsEntry UserRegsEntry;
 
 #define BITS_PER_ULONG 64
 
+static inline uint64_t encode_pointer(void *p) { return (uint64_t)p; }
+static inline void *decode_pointer(uint64_t v) { return (void*)v; }
+
 #endif /* __CR_ASM_TYPES_H__ */
diff --git a/cr-dump.c b/cr-dump.c
index 77ba15f..712cae4 100644
--- a/cr-dump.c
+++ b/cr-dump.c
@@ -788,7 +788,7 @@ static int get_task_futex_robust_list(pid_t pid, ThreadCoreEntry *info)
 		return -1;
 	}
 
-	info->futex_rla		= (u64)head;
+	info->futex_rla		= encode_pointer(head);
 	info->futex_rla_len	= (u32)len;
 
 	return 0;
diff --git a/cr-restore.c b/cr-restore.c
index fb4aace..55399e0 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -218,7 +218,7 @@ static int map_private_vma(pid_t pid, struct vma_area *vma, void *tgt_addr,
 		    p->vma.start == vma->vma.start) {
 			pr_info("COW 0x%016"PRIx64"-0x%016"PRIx64" 0x%016"PRIx64" vma\n",
 				vma->vma.start, vma->vma.end, vma->vma.pgoff);
-			paddr = (void *) vma_premmaped_start(&p->vma);
+			paddr = decode_pointer(vma_premmaped_start(&p->vma));
 			break;
 		}
 
@@ -310,7 +310,7 @@ static int restore_priv_vma_content(pid_t pid)
 			return -1;
 		}
 
-		p = (void *) (va - vma->vma.start +
+		p = decode_pointer(va - vma->vma.start +
 					vma_premmaped_start(&vma->vma));
 		if (memcmp(p, buf, PAGE_SIZE) == 0) {
 			nr_shared++;
@@ -325,7 +325,7 @@ static int restore_priv_vma_content(pid_t pid)
 	/* Remove pages, which were not shared with a child */
 	list_for_each_entry(vma, &rst_vma_list, list) {
 		unsigned long size, i = 0;
-		void *addr = (void *) vma_premmaped_start(&vma->vma);
+		void *addr = decode_pointer(vma_premmaped_start(&vma->vma));
 
 		if (vma->ppage_bitmap == NULL)
 			continue;
@@ -530,9 +530,9 @@ static int prepare_sigactions(int pid)
 		if (ret < 0)
 			break;
 
-		ASSIGN_TYPED(act.rt_sa_handler, e->sigaction);
+		ASSIGN_TYPED(act.rt_sa_handler, decode_pointer(e->sigaction));
 		ASSIGN_TYPED(act.rt_sa_flags, e->flags);
-		ASSIGN_TYPED(act.rt_sa_restorer, e->restorer);
+		ASSIGN_TYPED(act.rt_sa_restorer, decode_pointer(e->restorer));
 		ASSIGN_TYPED(act.rt_sa_mask.sig[0], e->mask);
 
 		sa_entry__free_unpacked(e, NULL);
diff --git a/parasite-syscall.c b/parasite-syscall.c
index ec45090..bbbda15 100644
--- a/parasite-syscall.c
+++ b/parasite-syscall.c
@@ -362,7 +362,7 @@ int parasite_dump_thread_seized(struct parasite_ctl *ctl, struct pid *tid,
 	ret = parasite_execute_by_pid(PARASITE_CMD_DUMP_THREAD, ctl, tid->real);
 
 	memcpy(&core->thread_core->blk_sigset, &args->blocked, sizeof(args->blocked));
-	CORE_THREAD_ARCH_INFO(core)->clear_tid_addr = (u64)args->tid_addr;
+	CORE_THREAD_ARCH_INFO(core)->clear_tid_addr = encode_pointer(args->tid_addr);
 	tid->virt = args->tid;
 	core_put_tls(core, args->tls);
 
@@ -389,9 +389,9 @@ int parasite_dump_sigacts_seized(struct parasite_ctl *ctl, struct cr_fdset *cr_f
 		if (sig == SIGSTOP || sig == SIGKILL)
 			continue;
 
-		ASSIGN_TYPED(se.sigaction, args->sas[i].rt_sa_handler);
+		ASSIGN_TYPED(se.sigaction, encode_pointer(args->sas[i].rt_sa_handler));
 		ASSIGN_TYPED(se.flags, args->sas[i].rt_sa_flags);
-		ASSIGN_TYPED(se.restorer, args->sas[i].rt_sa_restorer);
+		ASSIGN_TYPED(se.restorer, encode_pointer(args->sas[i].rt_sa_restorer));
 		ASSIGN_TYPED(se.mask, args->sas[i].rt_sa_mask.sig[0]);
 
 		if (pb_write_one(fd, &se, PB_SIGACT) < 0)
diff --git a/pie/parasite.c b/pie/parasite.c
index c344779..4f02934 100644
--- a/pie/parasite.c
+++ b/pie/parasite.c
@@ -179,7 +179,7 @@ static int dump_pages(struct parasite_dump_pages_args *args)
 	if (!(args->vma_entry.prot & PROT_READ)) {
 		prot_old = (unsigned long)args->vma_entry.prot;
 		prot_new = prot_old | PROT_READ;
-		ret = sys_mprotect((void *)args->vma_entry.start,
+		ret = sys_mprotect(decode_pointer(args->vma_entry.start),
 				   (unsigned long)vma_entry_len(&args->vma_entry),
 				   prot_new);
 		if (ret) {
@@ -202,7 +202,7 @@ static int dump_pages(struct parasite_dump_pages_args *args)
 			ret = sys_write_safe(fd_pages, &vaddr, sizeof(vaddr));
 			if (ret)
 				return ret;
-			ret = sys_write_safe(fd_pages, (void *)vaddr, PAGE_SIZE);
+			ret = sys_write_safe(fd_pages, decode_pointer(vaddr), PAGE_SIZE);
 			if (ret)
 				return ret;
 
@@ -215,7 +215,7 @@ static int dump_pages(struct parasite_dump_pages_args *args)
 	 * Don't left pages readable if they were not.
 	 */
 	if (prot_old != prot_new) {
-		ret = sys_mprotect((void *)args->vma_entry.start,
+		ret = sys_mprotect(decode_pointer(args->vma_entry.start),
 				   (unsigned long)vma_entry_len(&args->vma_entry),
 				   prot_old);
 		if (ret) {
diff --git a/pie/restorer.c b/pie/restorer.c
index b4f1ba5..0e5f940 100644
--- a/pie/restorer.c
+++ b/pie/restorer.c
@@ -159,10 +159,10 @@ static void restore_rlims(struct task_restore_core_args *ta)
 static int restore_thread_common(struct rt_sigframe *sigframe,
 		struct thread_restore_args *args)
 {
-	sys_set_tid_address((int *)args->clear_tid_addr);
+	sys_set_tid_address((int *)decode_pointer(args->clear_tid_addr));
 
 	if (args->has_futex) {
-		if (sys_set_robust_list((void *)args->futex_rla, args->futex_rla_len)) {
+		if (sys_set_robust_list(decode_pointer(args->futex_rla), args->futex_rla_len)) {
 			pr_err("Robust list err\n");
 			return -1;
 		}
@@ -243,7 +243,7 @@ static u64 restore_mapping(const VmaEntry *vma_entry)
 	u64 addr;
 
 	if (vma_entry_is(vma_entry, VMA_AREA_SYSVIPC))
-		return sys_shmat(vma_entry->fd, (void *)vma_entry->start,
+		return sys_shmat(vma_entry->fd, decode_pointer(vma_entry->start),
 				 (vma_entry->prot & PROT_WRITE) ? 0 : SHM_RDONLY);
 
 	/*
@@ -267,7 +267,7 @@ static u64 restore_mapping(const VmaEntry *vma_entry)
 	 * writable since we're going to restore page
 	 * contents.
 	 */
-	addr = sys_mmap((void *)vma_entry->start,
+	addr = sys_mmap(decode_pointer(vma_entry->start),
 			vma_entry_len(vma_entry),
 			prot, flags,
 			vma_entry->fd,
@@ -523,7 +523,7 @@ long __export_restore_task(struct task_restore_core_args *args)
 		if (vma_entry->prot & PROT_WRITE)
 			continue;
 
-		sys_mprotect((void *)vma_entry->start,
+		sys_mprotect(decode_pointer(vma_entry->start),
 			     vma_entry_len(vma_entry),
 			     vma_entry->prot);
 	}
-- 
1.7.10.4



More information about the CRIU mailing list