[Tizen] Enable ASan annotation of passing to native code buffers
authorAndrey Kazmin <a.kazmin@partner.samsung.com>
Wed, 16 Jun 2021 13:49:18 +0000 (16:49 +0300)
committerGleb Balykov <g.balykov@samsung.com>
Tue, 27 Sep 2022 12:50:22 +0000 (15:50 +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.

Signed-off-by: Slava Barinov <v.barinov@samsung.com>
src/coreclr/palrt/comem.cpp
src/coreclr/vm/ilmarshalers.cpp

index e56e720..a683ec5 100644 (file)
 
 #include "common.h"
 
+#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
+
 STDAPI_(LPVOID) CoTaskMemAlloc(SIZE_T cb)
 {
-    return malloc(cb);
+    LPVOID lpRetVal = NULL;
+#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 = malloc(cb);
+        if (!san_enabled) {
+            __sanitizer_disable_interceptors();
+        }
+    }
+    else
+#endif
+    {
+        lpRetVal = malloc(cb);
+    }
+    return lpRetVal;
 }
 
 STDAPI_(void) CoTaskMemFree(LPVOID pv)
 {
-    free(pv);
+#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();
+      }
+      free(pv);
+      if (!san_enabled) {
+       __sanitizer_disable_interceptors();
+      }
+    }
+    else
+#endif
+    {
+        free(pv);
+    }
 }
index 8f34846..14fa3cb 100644 (file)
@@ -3934,7 +3934,11 @@ bool ILNativeArrayMarshaler::CanMarshalViaPinning()
 {
     // We can't pin an array if we have a marshaler for the var type
     // or if we can't get a method-table representing the array (how we determine the offset to pin).
+#ifndef TIZEN_ASAN_ENVIRONMENT
     return IsCLRToNative(m_dwMarshalFlags) && !IsByref(m_dwMarshalFlags) && (NULL != m_pargs->na.m_pArrayMT) && (NULL == OleVariant::GetMarshalerForVarType(m_pargs->na.m_vt, TRUE));
+#else
+    return false;
+#endif
 }
 
 void ILNativeArrayMarshaler::EmitMarshalViaPinning(ILCodeStream* pslILEmit)