Index: trunk/src/VBox/Runtime/Makefile.kmk
===================================================================
--- trunk/src/VBox/Runtime/Makefile.kmk	(revision 28270)
+++ trunk/src/VBox/Runtime/Makefile.kmk	(revision 28271)
@@ -186,4 +186,5 @@
 RuntimeR3_SDKS.win      = WINPSDK W2K3DDK
 RuntimeR3_DEFS          = IN_RT_R3 IN_SUP_R3 LDR_WITH_NATIVE LDR_WITH_ELF32 LDR_WITH_PE RT_WITH_VBOX RT_NO_GIP
+#RuntimeR3_DEFS         += RTMEM_WRAP_TO_EF_APIS
 ifdef IPRT_WITH_KSTUFF
  RuntimeR3_DEFS        += LDR_WITH_KLDR
@@ -1740,5 +1741,5 @@
 # Static library for new & delete for the electric fence.
 #
-RuntimeEFCPP_TEMPLATE   = $(RuntimeR3_TEMPLATE)
+RuntimeEFCPP_TEMPLATE   = $(VBoxRT_TEMPLATE)
 RuntimeEFCPP_SDKS       = $(RuntimeR3_SDKS)
 RuntimeEFCPP_SDKS.$(KBUILD_TARGET) = $(RuntimeR3_SDKS.$(KBUILD_TARGET))
Index: trunk/src/VBox/Runtime/common/alloc/alloc.cpp
===================================================================
--- trunk/src/VBox/Runtime/common/alloc/alloc.cpp	(revision 28270)
+++ trunk/src/VBox/Runtime/common/alloc/alloc.cpp	(revision 28271)
@@ -38,4 +38,48 @@
 #include <iprt/assert.h>
 #include <iprt/string.h>
+
+#ifdef RTMEM_WRAP_TO_EF_APIS
+# undef RTMemAllocVar
+# undef RTMemAllocZVar
+# undef RTMemDup
+# undef RTMemDupEx
+#endif
+
+
+
+/**
+ * Wrapper around RTMemAlloc for automatically aligning variable sized
+ * allocations so that the various electric fence heaps works correctly.
+ *
+ * @returns See RTMemAlloc.
+ * @param   cbUnaligned         The unaligned size.
+ */
+RTDECL(void *) RTMemAllocVar(size_t cbUnaligned)
+{
+    size_t cbAligned;
+    if (cbUnaligned >= 16)
+        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
+    else
+        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
+    return RTMemAlloc(cbAligned);
+}
+
+
+/**
+ * Wrapper around RTMemAllocZ for automatically aligning variable sized
+ * allocations so that the various electric fence heaps works correctly.
+ *
+ * @returns See RTMemAllocZ.
+ * @param   cbUnaligned         The unaligned size.
+ */
+RTDECL(void *) RTMemAllocZVar(size_t cbUnaligned)
+{
+    size_t cbAligned;
+    if (cbUnaligned >= 16)
+        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
+    else
+        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
+    return RTMemAllocZ(cbAligned);
+}
 
 
Index: trunk/src/VBox/Runtime/common/log/log.cpp
===================================================================
--- trunk/src/VBox/Runtime/common/log/log.cpp	(revision 28270)
+++ trunk/src/VBox/Runtime/common/log/log.cpp	(revision 28271)
@@ -276,5 +276,5 @@
      */
     cb = RT_OFFSETOF(RTLOGGER, afGroups[cGroups + 1]) + RTPATH_MAX;
-    pLogger = (PRTLOGGER)RTMemAllocZ(cb);
+    pLogger = (PRTLOGGER)RTMemAllocZVar(cb);
     if (pLogger)
     {
Index: trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp
===================================================================
--- trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp	(revision 28270)
+++ trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp	(revision 28271)
@@ -1072,5 +1072,5 @@
     size_t const       cbFile   = pSrcPos->pszFile ? strlen(pSrcPos->pszFile) + 1 : 0;
     size_t const     cbFunction = pSrcPos->pszFile ? strlen(pSrcPos->pszFunction) + 1 : 0;
-    RTLOCKVALCLASSINT *pThis    = (RTLOCKVALCLASSINT *)RTMemAlloc(sizeof(*pThis) + cbFile + cbFunction + cbName);
+    RTLOCKVALCLASSINT *pThis    = (RTLOCKVALCLASSINT *)RTMemAllocVar(sizeof(*pThis) + cbFile + cbFunction + cbName);
     if (!pThis)
         return VERR_NO_MEMORY;
Index: trunk/src/VBox/Runtime/r3/alloc-ef-cpp.cpp
===================================================================
--- trunk/src/VBox/Runtime/r3/alloc-ef-cpp.cpp	(revision 28270)
+++ trunk/src/VBox/Runtime/r3/alloc-ef-cpp.cpp	(revision 28271)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2006-2007 Sun Microsystems, Inc.
+ * Copyright (C) 2006-2010 Sun Microsystems, Inc.
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -34,5 +34,6 @@
 #include "alloc-ef.h"
 
-#ifdef RTALLOC_EFENCE_CPP /* rest of the file */
+
+#if defined(RTALLOC_EFENCE_CPP) || defined(RTMEM_WRAP_TO_EF_APIS) /* rest of the file */
 
 #include <new>
@@ -122,3 +123,3 @@
 }
 
-#endif /* RTALLOC_EFENCE_CPP */
+#endif /* RTALLOC_EFENCE_CPP || RTMEM_WRAP_TO_EF_APIS */
Index: trunk/src/VBox/Runtime/r3/alloc-ef.cpp
===================================================================
--- trunk/src/VBox/Runtime/r3/alloc-ef.cpp	(revision 28270)
+++ trunk/src/VBox/Runtime/r3/alloc-ef.cpp	(revision 28271)
@@ -34,4 +34,5 @@
 *******************************************************************************/
 #include "alloc-ef.h"
+#include <iprt/mem.h>
 #include <iprt/log.h>
 #include <iprt/asm.h>
@@ -272,5 +273,5 @@
  * Internal allocator.
  */
-void *rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
+RTDECL(void *) rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
 {
     /*
@@ -372,5 +373,5 @@
  * Internal free.
  */
-void rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
+RTDECL(void) rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
 {
     /*
@@ -498,8 +499,9 @@
 }
 
+
 /**
  * Internal realloc.
  */
-void *rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
+RTDECL(void *) rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction)
 {
     /*
@@ -609,4 +611,40 @@
 
 /**
+ * Same as RTMemAllocVar() except that it's fenced.
+ *
+ * @returns Pointer to the allocated memory. Free with RTMemEfFree().
+ * @returns NULL on failure.
+ * @param   cbUnaligned Size in bytes of the memory block to allocate.
+ */
+RTDECL(void *)  RTMemEfAllocVar(size_t cbUnaligned) RT_NO_THROW
+{
+    size_t cbAligned;
+    if (cbUnaligned >= 16)
+        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
+    else
+        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
+    return rtMemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cbAligned, ((void **)&cbUnaligned)[-1], 0, NULL, NULL);
+}
+
+
+/**
+ * Same as RTMemAllocZVar() except that it's fenced.
+ *
+ * @returns Pointer to the allocated memory.
+ * @returns NULL on failure.
+ * @param   cbUnaligned Size in bytes of the memory block to allocate.
+ */
+RTDECL(void *)  RTMemEfAllocZVar(size_t cbUnaligned) RT_NO_THROW
+{
+    size_t cbAligned;
+    if (cbUnaligned >= 16)
+        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
+    else
+        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
+    return rtMemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cbAligned, ((void **)&cbUnaligned)[-1], 0, NULL, NULL);
+}
+
+
+/**
  * Same as RTMemRealloc() except that it's fenced.
  *
Index: trunk/src/VBox/Runtime/r3/alloc-ef.h
===================================================================
--- trunk/src/VBox/Runtime/r3/alloc-ef.h	(revision 28270)
+++ trunk/src/VBox/Runtime/r3/alloc-ef.h	(revision 28271)
@@ -45,5 +45,5 @@
  * RTMemAllocZ(), RTMemRealloc(), RTMemTmpAlloc() and RTMemTmpAllocZ().
  */
-#if 0// defined(DEBUG_bird)
+#if defined(DEBUG_bird)
 # define RTALLOC_USE_EFENCE
 #endif
@@ -57,10 +57,9 @@
  * The allocation alignment, power of two of course.
  *
- * Normally, 1 would be the perfect choice here, except that there is code, like
- * the atomic operations in iprt/asm.h, that makes assumptions about naturally
- * aligned data members.  If the code you're working against doesn't have strict
- * alignment requirements, changing this to 1 is highly desirable.
+ * Use this for working around misaligned sizes, usually stemming from
+ * allocating a string or something after the main structure.  When you
+ * encounter this, please fix the allocation to RTMemAllocVar or RTMemAllocZVar.
  */
-#if 1
+#if 0
 # define RTALLOC_EFENCE_ALIGNMENT       (ARCH_BITS / 8)
 #else
@@ -186,7 +185,7 @@
 ******************************************************************************/
 RT_C_DECLS_BEGIN
-void *  rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
-void *  rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
-void    rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
+RTDECL(void *)  rtMemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cb, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
+RTDECL(void *)  rtMemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
+RTDECL(void)    rtMemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, unsigned iLine, const char *pszFile, const char *pszFunction);
 RT_C_DECLS_END
 
Index: trunk/src/VBox/Runtime/r3/alloc.cpp
===================================================================
--- trunk/src/VBox/Runtime/r3/alloc.cpp	(revision 28270)
+++ trunk/src/VBox/Runtime/r3/alloc.cpp	(revision 28271)
@@ -28,4 +28,13 @@
  * additional information or have any questions.
  */
+
+
+/*******************************************************************************
+*   Defined Constants And Macros                                               *
+*******************************************************************************/
+#ifdef RTMEM_WRAP_TO_EF_APIS
+# undef RTMEM_WRAP_TO_EF_APIS
+# define RTALLOC_USE_EFENCE 1
+#endif
 
 
@@ -103,11 +112,8 @@
     void *pv = malloc(cb);
     AssertMsg(pv, ("malloc(%#zx) failed!!!\n", cb));
-#ifdef RT_OS_OS2 /* temporary workaround until libc062. */
-    AssertMsg(   cb < 32
-              || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
-#else
     AssertMsg(   cb < RTMEM_ALIGNMENT
-              || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
-#endif
+              || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
+              || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
+              , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
 #endif /* !RTALLOC_USE_EFENCE */
     return pv;
@@ -137,11 +143,8 @@
     void *pv = calloc(1, cb);
     AssertMsg(pv, ("calloc(1,%#zx) failed!!!\n", cb));
-#ifdef RT_OS_OS2 /* temporary workaround until libc062. */
-    AssertMsg(   cb < 32
-              || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
-#else
     AssertMsg(   cb < RTMEM_ALIGNMENT
-              || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
-#endif
+              || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
+              || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
+              , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
 #endif /* !RTALLOC_USE_EFENCE */
     return pv;
@@ -166,11 +169,8 @@
     void *pv = realloc(pvOld, cbNew);
     AssertMsg(pv && cbNew, ("realloc(%p, %#zx) failed!!!\n", pvOld, cbNew));
-#ifdef RT_OS_OS2 /* temporary workaround until libc062. */
-    AssertMsg(   cbNew < 32
-              || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
-#else
     AssertMsg(   cbNew < RTMEM_ALIGNMENT
-              || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
-#endif
+              || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
+              || ( (cbNew & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
+              , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
 #endif  /* !RTALLOC_USE_EFENCE */
     return pv;
