[Tizen] Enable ASan annotation of passing to native code buffers
authorAndrey Kazmin <a.kazmin@partner.samsung.com>
Thu, 14 May 2020 13:57:25 +0000 (16:57 +0300)
committerAlexander Soldatov/AI Compiler Lab /SRR/Staff Engineer/Samsung Electronics <soldatov.a@samsung.com>
Thu, 4 Jun 2020 12:04:39 +0000 (15:04 +0300)
Turn on ASan inteceptors while marshaling managed buffers to native code.
We could not properly annotate already allocated on heap buffers, so
we have to disable pinning of such objects.
Current patch affects only pinning of native arrays.

src/pal/src/memory/local.cpp
src/vm/ilmarshalers.cpp

index 3a0f40f..44d9feb 100644 (file)
@@ -26,6 +26,14 @@ Revision History:
 
 SET_DEFAULT_DEBUG_CHANNEL(MEM);
 
+#ifdef TIZEN_ASAN_ENVIRONMENT
+extern "C" {
+extern void __sanitizer_disable_interceptors() __attribute__ ((weak));
+extern void __sanitizer_enable_interceptors() __attribute__ ((weak));
+extern bool __sanitizer_interceptors_are_enabled() __attribute__ ((weak));
+}
+#endif
+
 static
 int
 AllocFlagsToHeapAllocFlags (IN  UINT  AllocFlags,
@@ -70,7 +78,24 @@ LocalAlloc(
         goto done;
     }
 
-    lpRetVal = HeapAlloc( GetProcessHeap(), uFlags, uBytes );
+#ifdef TIZEN_ASAN_ENVIRONMENT
+    if (__sanitizer_interceptors_are_enabled != NULL)
+    {
+        bool san_enabled;
+        san_enabled = __sanitizer_interceptors_are_enabled();
+        if (!san_enabled) {
+            __sanitizer_enable_interceptors();
+        }
+        lpRetVal = HeapAlloc( GetProcessHeap(), uFlags, uBytes );
+        if (!san_enabled) {
+            __sanitizer_disable_interceptors();
+        }
+    }
+    else
+#endif
+    {
+        lpRetVal = HeapAlloc( GetProcessHeap(), uFlags, uBytes );
+    }
 
 done:
     LOGEXIT( "LocalAlloc returning %p.\n", lpRetVal );
@@ -128,7 +153,25 @@ LocalFree(
 
     if ( hMem )
     {
-        bRetVal = HeapFree( GetProcessHeap(), 0, hMem );
+#ifdef TIZEN_ASAN_ENVIRONMENT
+        if (__sanitizer_interceptors_are_enabled != NULL)
+        {
+            bool san_enabled;
+            san_enabled = __sanitizer_interceptors_are_enabled();
+            if (!san_enabled) {
+                __sanitizer_enable_interceptors();
+            }
+            bRetVal = HeapFree( GetProcessHeap(), 0, hMem );
+            if (!san_enabled) {
+                __sanitizer_disable_interceptors();
+            }
+        }
+        else
+#endif
+        {
+            bRetVal = HeapFree( GetProcessHeap(), 0, hMem );
+        }
+
     }
     else
     {
index a89aeaf..9e7ee17 100644 (file)
@@ -3808,11 +3808,12 @@ void ILMngdMarshaler::EmitCallMngdMarshalerMethod(ILCodeStream* pslILEmit, Metho
 
 bool ILNativeArrayMarshaler::UsePinnedArraySpecialCase()
 {
+#ifndef TIZEN_ASAN_ENVIRONMENT
     if (IsCLRToNative(m_dwMarshalFlags) && !IsByref(m_dwMarshalFlags) && (NULL != m_pargs->na.m_pArrayMT) && (NULL == OleVariant::GetMarshalerForVarType(m_pargs->na.m_vt, TRUE)))
     {
         return true;
     }
-
+#endif
     return false;
 }