[Tizen] Enable ASan annotation of passing to native code buffers
[platform/upstream/coreclr.git] / src / pal / src / memory / local.cpp
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 /*++
6
7
8
9 Module Name:
10
11     local.c
12
13 Abstract:
14
15     Implementation of local memory management functions.
16
17 Revision History:
18
19
20
21 --*/
22
23 #include "pal/palinternal.h"
24 #include "pal/dbgmsg.h"
25
26
27 SET_DEFAULT_DEBUG_CHANNEL(MEM);
28
29 #ifdef TIZEN_ASAN_ENVIRONMENT
30 extern "C" {
31 extern void __sanitizer_disable_interceptors() __attribute__ ((weak));
32 extern void __sanitizer_enable_interceptors() __attribute__ ((weak));
33 extern bool __sanitizer_interceptors_are_enabled() __attribute__ ((weak));
34 }
35 #endif
36
37 static
38 int
39 AllocFlagsToHeapAllocFlags (IN  UINT  AllocFlags,
40                        OUT PUINT pHeapallocFlags)
41 {
42     int success = 1;
43     UINT newFlags = 0, flags = AllocFlags;
44     if (flags & LMEM_ZEROINIT) {
45         newFlags |= HEAP_ZERO_MEMORY;
46         flags &= ~LMEM_ZEROINIT;
47     }
48     if (flags != 0) {
49         ASSERT("Invalid parameter AllocFlags=0x%x\n", AllocFlags);
50         SetLastError(ERROR_INVALID_PARAMETER);
51         success = 0;
52     }
53     if (success) {
54         *pHeapallocFlags = newFlags;
55     }
56     return success;
57 }
58         
59     
60
61 /*++
62 Function:
63   LocalAlloc
64
65 See MSDN doc.
66 --*/
67 HLOCAL
68 PALAPI
69 LocalAlloc(
70            IN UINT uFlags,
71            IN SIZE_T uBytes)
72 {
73     LPVOID lpRetVal = NULL;
74     PERF_ENTRY(LocalAlloc);
75     ENTRY("LocalAlloc (uFlags=%#x, uBytes=%u)\n", uFlags, uBytes);
76
77     if (!AllocFlagsToHeapAllocFlags (uFlags, &uFlags)) {
78         goto done;
79     }
80
81 #ifdef TIZEN_ASAN_ENVIRONMENT
82     if (__sanitizer_interceptors_are_enabled != NULL)
83     {
84         bool san_enabled;
85         san_enabled = __sanitizer_interceptors_are_enabled();
86         if (!san_enabled) {
87             __sanitizer_enable_interceptors();
88         }
89         lpRetVal = HeapAlloc( GetProcessHeap(), uFlags, uBytes );
90         if (!san_enabled) {
91             __sanitizer_disable_interceptors();
92         }
93     }
94     else
95 #endif
96     {
97         lpRetVal = HeapAlloc( GetProcessHeap(), uFlags, uBytes );
98     }
99
100 done:
101     LOGEXIT( "LocalAlloc returning %p.\n", lpRetVal );
102     PERF_EXIT(LocalAlloc);
103     return (HLOCAL) lpRetVal;
104 }
105
106 /*++
107 Function:
108 LocalReAlloc
109
110 See MSDN doc.
111 --*/
112 HLOCAL
113 PALAPI
114 LocalReAlloc(
115        IN HLOCAL hMem,
116        IN SIZE_T uBytes,
117        IN UINT   uFlags)
118 {
119     LPVOID lpRetVal = NULL;
120     PERF_ENTRY(LocalReAlloc);
121     ENTRY("LocalReAlloc (hMem=%p, uBytes=%u, uFlags=%#x)\n", hMem, uBytes, uFlags);
122
123     if (uFlags != LMEM_MOVEABLE) {
124         // Currently valid iff uFlags is LMEM_MOVEABLE
125         ASSERT("Invalid parameter uFlags=0x%x\n", uFlags);
126         SetLastError(ERROR_INVALID_PARAMETER);
127         goto done;
128     }
129     uFlags = 0;
130
131     lpRetVal = HeapReAlloc(GetProcessHeap(), uFlags, hMem, uBytes);
132
133 done:
134     LOGEXIT("LocalReAlloc returning %p.\n", lpRetVal);
135     PERF_EXIT(LocalReAlloc);
136     return (HLOCAL)lpRetVal;
137 }
138
139 /*++
140 Function:
141   LocalFree
142
143 See MSDN doc.
144 --*/
145 HLOCAL
146 PALAPI
147 LocalFree(
148           IN HLOCAL hMem)
149 {
150     BOOL bRetVal = FALSE;
151     PERF_ENTRY(LocalFree);
152     ENTRY("LocalFree (hmem=%p)\n", hMem);
153
154     if ( hMem )
155     {
156 #ifdef TIZEN_ASAN_ENVIRONMENT
157         if (__sanitizer_interceptors_are_enabled != NULL)
158         {
159             bool san_enabled;
160             san_enabled = __sanitizer_interceptors_are_enabled();
161             if (!san_enabled) {
162                 __sanitizer_enable_interceptors();
163             }
164             bRetVal = HeapFree( GetProcessHeap(), 0, hMem );
165             if (!san_enabled) {
166                 __sanitizer_disable_interceptors();
167             }
168         }
169         else
170 #endif
171         {
172             bRetVal = HeapFree( GetProcessHeap(), 0, hMem );
173         }
174
175     }
176     else
177     {
178         bRetVal = TRUE;
179     }
180     
181     LOGEXIT( "LocalFree returning %p.\n", bRetVal == TRUE ? (HLOCAL)NULL : hMem );
182     PERF_EXIT(LocalFree);
183     return bRetVal == TRUE ? (HLOCAL)NULL : hMem;
184 }