Skip to contentSkip to conversation

Implement byte-by-byte memory access facilitators

PATCH Dave Anderson 1 participant 1 message Apr 25, 2016

lore
Share & tools
lore
#0 lore raw

----- Original Message -----

Quoted 55 lines · Sam Ravnborg
Quoted 45 lines

Hi Dave.

Quoted 31 lines

Sparc64 requires type-aligned memory access. Since data buffers may not be properly aligned, implement a safe copy from memory per data type.

---
 defs.h |   41 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 41 insertions(+), 0 deletions(-)
defs.h +15 -0
index e3afc58..d9d4559 100644
@@ -2187,6 +2187,45 @@ struct builtin_debug_table {
  *  Facilitators for pulling correctly-sized data out of a buffer at a
  *  known address.
  */
+
+#ifdef NEED_ALIGNED_MEM_ACCESS
+
+#define DEF_LOADER(TYPE)			\
+static inline TYPE				\
+load_##TYPE (char *addr)			\
+{						\
+	TYPE ret;				\
+	size_t i = sizeof(TYPE);		\
+	while (i--)				\
+		((char *)&ret)[i] = addr[i];	\
+	return ret;				\
+}
+
+DEF_LOADER(int);

defs.h struct builtin_debug_table { · line 2214

Quoted 10 lines
defs.h +10 -0
+DEF_LOADER(uint);
+DEF_LOADER(long);
+DEF_LOADER(ulong);
+DEF_LOADER(ulonglong);
+DEF_LOADER(ushort);
+DEF_LOADER(short);
+typedef void *pointer_t;
+DEF_LOADER(pointer_t);
+
+#define LOADER(TYPE) load_##TYPE

A simpler model would be
#define LOADER(TYPE, addr) \

Quoted 5 lines
({                                                 \
	TYPE ret;                                  \
	memcpy(&ret, (void *)addr, sizeof(TYPE));  \
	ret;                                       \
})

This does simplify things a bit.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

I would expect the compiler to optimize this away for archs that do not require aligned access.

I hesitate to to replace the macros for every architecture with this. I'll let Dave A. make that call, but I think it can improve the NEED_ALIGNED_MEM_ACCESS case.

Do not modify the macros for the existing architectures.

Thanks,
Dave

Quoted 33 lines
Quoted 25 lines

Quick test:

#include <string.h>
#include <stdio.h>

#define LOADER(TYPE, addr)                       \
({                                               \
        TYPE ret;                                \
        memcpy(&ret, (void*)addr, sizeof(TYPE)); \
        ret;                                     \
})

#define INT(ADDR) LOADER(int, ADDR)

void main(void)

{
	int i = 37;
	int j = -17;

        printf("i=%d j=%d\n", INT(&i), INT(&j));
}
Quoted 2 lines

Sam

--
Crash-utility mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/crash-utility