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 //*****************************************************************************
9 // Dac function implementations.
11 //*****************************************************************************
18 #endif // FEATURE_PREJIT
19 #include <virtualcallstub.h>
20 #include "peimagelayout.inl"
22 #include "gcinterface.h"
23 #include "gcinterface.dac.h"
26 DacTableInfo g_dacTableInfo;
27 DacGlobals g_dacGlobals;
31 #define VPTR_CLASS(name) PVOID name;
32 #define VPTR_MULTI_CLASS(name, keyBase) PVOID name##__##keyBase;
33 #include <vptr_list.h>
35 #undef VPTR_MULTI_CLASS
39 const WCHAR *g_dacVtStrings[] =
41 #define VPTR_CLASS(name) W(#name),
42 #define VPTR_MULTI_CLASS(name, keyBase) W(#name),
43 #include <vptr_list.h>
45 #undef VPTR_MULTI_CLASS
48 DacHostVtPtrs g_dacHostVtPtrs;
51 DacGetHostVtPtrs(void)
53 #define VPTR_CLASS(name) \
54 g_dacHostVtPtrs.name = name::VPtrHostVTable();
55 #define VPTR_MULTI_CLASS(name, keyBase) \
56 g_dacHostVtPtrs.name##__##keyBase = name::VPtrHostVTable();
57 #include <vptr_list.h>
59 #undef VPTR_MULTI_CLASS
65 DacExceptionFilter(Exception* ex, ClrDataAccess* access,
68 SUPPORTS_DAC_HOST_ONLY;
70 // The DAC support functions throw HRExceptions and
71 // the underlying code can throw the normal set of
72 // CLR exceptions. Handle any exception
73 // other than an unexpected SEH exception.
74 // If we're not debugging, handle SEH exceptions also
75 // so that dac absorbs all exceptions by default.
76 if ((access && access->m_debugMode) &&
77 ex->IsType(SEHException::GetType()))
79 // Indicate this exception should be rethrown.
83 // Indicate this exception is handled.
84 // XXX Microsoft - The C++-based EH has broken the ability
85 // to get proper SEH results. Make sure that the
86 // error returned is actually an error code as
87 // often it's just zero.
88 *status = ex->GetHR();
97 DacWarning(__in char* format, ...)
102 va_start(args, format);
103 _vsnprintf_s(text, sizeof(text), _TRUNCATE, format, args);
104 text[sizeof(text) - 1] = 0;
106 OutputDebugStringA(text);
112 EX_THROW(HRException, (E_NOTIMPL));
116 DacError(HRESULT err)
118 EX_THROW(HRException, (err));
121 // Ideally DacNoImpl and DacError would be marked no-return, but that will require changing a bunch of existing
122 // code to avoid "unreachable code" warnings.
123 void DECLSPEC_NORETURN
124 DacError_NoRet(HRESULT err)
126 EX_THROW(HRException, (err));
134 DacError(E_UNEXPECTED);
138 return g_dacImpl->m_globalBase;
142 DacReadAll(TADDR addr, PVOID buffer, ULONG32 size, bool throwEx)
146 DacError(E_UNEXPECTED);
150 ClrSafeInt<TADDR> end = ClrSafeInt<TADDR>(addr) + ClrSafeInt<TADDR>(size);
151 if( end.IsOverflow() )
153 // Overflow - corrupt data
154 DacError(CORDBG_E_TARGET_INCONSISTENT);
160 #if defined(DAC_MEASURE_PERF)
161 unsigned __int64 nStart, nEnd;
162 nStart = GetCycleCount();
163 #endif // #if defined(DAC_MEASURE_PERF)
165 status = g_dacImpl->m_pTarget->
166 ReadVirtual(addr, (PBYTE)buffer, size, &returned);
168 #if defined(DAC_MEASURE_PERF)
169 nEnd = GetCycleCount();
170 g_nReadVirtualTotalTime += nEnd - nStart;
171 #endif // #if defined(DAC_MEASURE_PERF)
175 // Regardless of what status is, it's very important for dump debugging to
176 // always return CORDBG_E_READVIRTUAL_FAILURE.
179 DacError(CORDBG_E_READVIRTUAL_FAILURE);
181 return CORDBG_E_READVIRTUAL_FAILURE;
183 if (returned != size)
187 DacError(HRESULT_FROM_WIN32(ERROR_PARTIAL_COPY));
189 return HRESULT_FROM_WIN32(ERROR_PARTIAL_COPY);
196 DacWriteAll(TADDR addr, PVOID buffer, ULONG32 size, bool throwEx)
200 DacError(E_UNEXPECTED);
206 status = g_dacImpl->m_pMutableTarget->WriteVirtual(addr, (PBYTE)buffer, size);
221 static BOOL DacReadAllAdapter(PVOID address, PVOID buffer, SIZE_T size)
223 HRESULT hr = DacReadAll((TADDR)address, (PVOID)buffer, size, false);
224 return SUCCEEDED(hr);
228 DacVirtualUnwind(DWORD threadId, PT_CONTEXT context, PT_KNONVOLATILE_CONTEXT_POINTERS contextPointers)
232 DacError(E_UNEXPECTED);
236 // The DAC code doesn't use these context pointers but zero them out to be safe.
237 if (contextPointers != NULL)
239 memset(contextPointers, 0, sizeof(T_KNONVOLATILE_CONTEXT_POINTERS));
244 #ifdef FEATURE_DATATARGET4
245 ReleaseHolder<ICorDebugDataTarget4> dt;
246 hr = g_dacImpl->m_pTarget->QueryInterface(IID_ICorDebugDataTarget4, (void **)&dt);
249 hr = dt->VirtualUnwind(threadId, sizeof(CONTEXT), (BYTE*)context);
254 SIZE_T baseAddress = DacGlobalBase();
255 if (baseAddress == 0 || !PAL_VirtualUnwindOutOfProc(context, contextPointers, baseAddress, DacReadAllAdapter))
264 #endif // FEATURE_PAL
266 // DacAllocVirtual - Allocate memory from the target process
267 // Note: this is only available to clients supporting the legacy
268 // ICLRDataTarget2 interface. It's currently used by SOS for notification tables.
270 DacAllocVirtual(TADDR addr, ULONG32 size,
271 ULONG32 typeFlags, ULONG32 protectFlags,
272 bool throwEx, TADDR* mem)
276 DacError(E_UNEXPECTED);
280 ICLRDataTarget2 * pTarget2 = g_dacImpl->GetLegacyTarget2();
281 if (pTarget2 == NULL)
287 CLRDATA_ADDRESS cdaMem;
288 HRESULT status = pTarget2->AllocVirtual(
289 TO_CDADDR(addr), size, typeFlags, protectFlags, &cdaMem);
301 *mem = CLRDATA_ADDRESS_TO_TADDR(cdaMem);
305 // DacFreeVirtual - Free memory from the target process
306 // Note: this is only available to clients supporting the legacy
307 // ICLRDataTarget2 interface. This is not currently used.
309 DacFreeVirtual(TADDR mem, ULONG32 size, ULONG32 typeFlags,
314 DacError(E_UNEXPECTED);
318 ICLRDataTarget2 * pTarget2 = g_dacImpl->GetLegacyTarget2();
319 if (pTarget2 == NULL)
325 HRESULT status = pTarget2->FreeVirtual(
326 TO_CDADDR(mem), size, typeFlags);
328 if (status != S_OK && throwEx)
338 DacInstantiateTypeByAddressHelper(TADDR addr, ULONG32 size, bool throwEx, bool fReport)
342 // Dac accesses are not interesting for PREfix and cause alot of PREfix noise
343 // so we just return the unmodified pointer for our PREFIX builds
350 DacError(E_UNEXPECTED);
354 // Preserve special pointer values.
355 if (!addr || addr == (TADDR)-1)
360 // DacInstanceManager::Alloc will assert (with a non-obvious message) on 0-size instances.
361 // Fail sooner and more obviously here.
362 _ASSERTE_MSG( size > 0, "DAC coding error: instance size cannot be 0" );
364 // Do not attempt to allocate more than 64megs for one object instance. While we should
365 // never even come close to this size, in cases of heap corruption or bogus data passed
366 // into the dac, we can allocate huge amounts of data if we are unlucky. This santiy
367 // checks the size to ensure we don't allocate gigs of data.
368 if (size > 0x4000000)
372 DacError(E_OUTOFMEMORY);
378 // Check the cache for an existing DPTR instance.
379 // It's possible that a previous access may have been
380 // smaller than the current access, so we have to
381 // allow an existing instance to be superseded.
384 DAC_INSTANCE* inst = g_dacImpl->m_instances.Find(addr);
385 DAC_INSTANCE* oldInst = NULL;
388 // If the existing instance is large enough we
389 // can reuse it, otherwise we need to promote.
390 // We cannot promote a VPTR as the VPTR data
391 // has been updated with a host vtable and we
392 // don't want to lose that. This shouldn't
394 if (inst->size >= size)
400 // Existing instance is too small and must
402 if (inst->usage == DAC_VPTR)
404 // The same address has already been marshalled as a VPTR, now we're trying to marshal as a
405 // DPTR. This is not allowed.
406 _ASSERTE_MSG(false, "DAC coding error: DPTR/VPTR usage conflict");
407 DacError(E_INVALIDARG);
411 // Promote the larger instance into the hash
412 // in place of the smaller, but keep the
413 // smaller instance around in case code still
414 // has a pointer to it. But ensure that we can
415 // create the larger instance and add it to the
416 // hash table before removing the old one.
421 inst = g_dacImpl->m_instances.Alloc(addr, size, DAC_DPTR);
424 DacError(E_OUTOFMEMORY);
428 if (fReport == false)
430 // mark the bit if necessary
438 HRESULT status = DacReadAll(addr, inst + 1, size, false);
441 g_dacImpl->m_instances.ReturnAlloc(inst);
449 if (!g_dacImpl->m_instances.Add(inst))
451 g_dacImpl->m_instances.ReturnAlloc(inst);
452 DacError(E_OUTOFMEMORY);
458 g_dacImpl->m_instances.Supersede(oldInst);
466 PVOID DacInstantiateTypeByAddress(TADDR addr, ULONG32 size, bool throwEx)
468 return DacInstantiateTypeByAddressHelper(addr, size, throwEx, true);
471 PVOID DacInstantiateTypeByAddressNoReport(TADDR addr, ULONG32 size, bool throwEx)
473 return DacInstantiateTypeByAddressHelper(addr, size, throwEx, false);
478 DacInstantiateClassByVTable(TADDR addr, ULONG32 minSize, bool throwEx)
482 // Dac accesses are not interesting for PREfix and cause alot of PREfix noise
483 // so we just return the unmodified pointer for our PREFIX builds
490 DacError(E_UNEXPECTED);
494 // Preserve special pointer values.
495 if (!addr || addr == (TADDR)-1)
500 // Do not attempt to allocate more than 64megs for one object instance. While we should
501 // never even come close to this size, in cases of heap corruption or bogus data passed
502 // into the dac, we can allocate huge amounts of data if we are unlucky. This santiy
503 // checks the size to ensure we don't allocate gigs of data.
504 if (minSize > 0x4000000)
508 DacError(E_OUTOFMEMORY);
514 // Check the cache for an existing VPTR instance.
515 // If there is an instance we assume that it's
519 DAC_INSTANCE* inst = g_dacImpl->m_instances.Find(addr);
520 DAC_INSTANCE* oldInst = NULL;
523 // If the existing instance is a VPTR we can
524 // reuse it, otherwise we need to promote.
525 if (inst->usage == DAC_VPTR)
527 // Sanity check that the object we're returning is big enough to fill the PTR type it's being
528 // accessed with. For more information, see the similar check below for the case when the
529 // object isn't already cached
530 _ASSERTE_MSG(inst->size >= minSize, "DAC coding error: Attempt to instantiate a VPTR from an object that is too small");
536 // Existing instance is not a match and must
538 // Promote the new instance into the hash
539 // in place of the old, but keep the
540 // old instance around in case code still
541 // has a pointer to it. But ensure that we can
542 // create the larger instance and add it to the
543 // hash table before removing the old one.
553 // Read the vtable pointer to get the actual
554 // implementation class identity.
555 if ((status = DacReadAll(addr, &vtAddr, sizeof(vtAddr), throwEx)) != S_OK)
561 // Instantiate the right class, using the vtable as
565 #define VPTR_CLASS(name) \
566 if (vtAddr == g_dacImpl->m_globalBase + \
567 g_dacGlobals.name##__vtAddr) \
569 size = sizeof(name); \
570 hostVtPtr = g_dacHostVtPtrs.name; \
573 #define VPTR_MULTI_CLASS(name, keyBase) \
574 if (vtAddr == g_dacImpl->m_globalBase + \
575 g_dacGlobals.name##__##keyBase##__mvtAddr) \
577 size = sizeof(name); \
578 hostVtPtr = g_dacHostVtPtrs.name##__##keyBase; \
581 #include <vptr_list.h>
583 #undef VPTR_MULTI_CLASS
586 // Can't identify the vtable pointer.
589 _ASSERTE_MSG(false,"DAC coding error: Unrecognized vtable pointer in VPTR marshalling code");
590 DacError(E_INVALIDARG);
595 // Sanity check that the object we're returning is big enough to fill the PTR type it's being
597 // If this is not true, it means the type being marshalled isn't a sub-type (or the same type)
598 // as the PTR type it's being used as. For example, trying to marshal an instance of a SystemDomain
599 // object into a PTR_AppDomain will cause this ASSERT to fire (because both SystemDomain and AppDomain
600 // derived from BaseDomain, and SystemDomain is smaller than AppDomain).
601 _ASSERTE_MSG(size >= minSize, "DAC coding error: Attempt to instantiate a VPTR from an object that is too small");
603 inst = g_dacImpl->m_instances.Alloc(addr, size, DAC_VPTR);
606 DacError(E_OUTOFMEMORY);
610 // Copy the object contents into the host instance. Note that this assumes the host and target
611 // have the same exact layout. Specifically, it assumes the host and target vtable pointers are
613 if ((status = DacReadAll(addr, inst + 1, size, false)) != S_OK)
615 g_dacImpl->m_instances.ReturnAlloc(inst);
623 // We now have a proper target object with a target
624 // vtable. We need to patch the vtable to the appropriate
625 // host vtable so that the virtual functions can be
626 // called in the host process.
627 *(PVOID*)(inst + 1) = hostVtPtr;
629 if (!g_dacImpl->m_instances.Add(inst))
631 g_dacImpl->m_instances.ReturnAlloc(inst);
632 DacError(E_OUTOFMEMORY);
638 g_dacImpl->m_instances.Supersede(oldInst);
645 #define LOCAL_STR_BUF 256
648 DacInstantiateStringA(TADDR addr, ULONG32 maxChars, bool throwEx)
652 // Dac accesses are not interesting for PREfix and cause alot of PREfix noise
653 // so we just return the unmodified pointer for our PREFIX builds
662 DacError(E_UNEXPECTED);
666 // Preserve special pointer values.
667 if (!addr || addr == (TADDR)-1)
673 // Do not attempt to allocate more than 64megs for a string. While we should
674 // never even come close to this size, in cases of heap corruption or bogus data passed
675 // into the dac, we can allocate huge amounts of data if we are unlucky. This santiy
676 // checks the size to ensure we don't allocate gigs of data.
677 if (maxChars > 0x4000000)
681 DacError(E_OUTOFMEMORY);
687 // Look for an existing string instance.
690 DAC_INSTANCE* inst = g_dacImpl->m_instances.Find(addr);
691 if (inst && inst->usage == DAC_STRA)
693 return (PSTR)(inst + 1);
697 // Determine the length of the string
698 // by iteratively reading blocks and scanning them
702 char buf[LOCAL_STR_BUF];
703 TADDR scanAddr = addr;
704 ULONG32 curBytes = 0;
709 status = g_dacImpl->m_pTarget->
710 ReadVirtual(scanAddr, (PBYTE)buf, sizeof(buf),
714 // We hit invalid memory before finding a terminator.
717 DacError(CORDBG_E_READVIRTUAL_FAILURE);
722 PSTR scan = (PSTR)buf;
723 PSTR scanEnd = scan + (returned / sizeof(*scan));
724 while (scan < scanEnd)
736 // Found a terminator.
737 scanAddr += ((scan + 1) - buf) * sizeof(*scan);
741 // Ignore any partial character reads. The character
742 // will be reread on the next loop if necessary.
743 returned &= ~(sizeof(buf[0]) - 1);
745 // The assumption is that a memory read cannot wrap
746 // around the address space, thus if we have read to
747 // the top of memory scanAddr cannot wrap farther
749 curBytes += returned;
750 scanAddr += returned;
753 (curBytes + sizeof(buf[0]) - 1) / sizeof(buf[0]) >= maxChars)
755 // Wrapped around the top of memory or
756 // we didn't find a terminator within the given bound.
759 DacError(E_INVALIDARG);
765 // Now that we know the length we can create a
766 // host copy of the string.
768 DacInstantiateTypeByAddress(addr, (ULONG32)(scanAddr - addr), throwEx);
770 (inst = g_dacImpl->m_instances.Find(addr)))
772 inst->usage = DAC_STRA;
780 DacInstantiateStringW(TADDR addr, ULONG32 maxChars, bool throwEx)
784 // Dac accesses are not interesting for PREfix and cause alot of PREfix noise
785 // so we just return the unmodified pointer for our PREFIX builds
794 DacError(E_UNEXPECTED);
798 // Preserve special pointer values.
799 if (!addr || addr == (TADDR)-1)
804 // Do not attempt to allocate more than 64megs for a string. While we should
805 // never even come close to this size, in cases of heap corruption or bogus data passed
806 // into the dac, we can allocate huge amounts of data if we are unlucky. This santiy
807 // checks the size to ensure we don't allocate gigs of data.
808 if (maxChars > 0x4000000)
812 DacError(E_OUTOFMEMORY);
819 // Look for an existing string instance.
822 DAC_INSTANCE* inst = g_dacImpl->m_instances.Find(addr);
823 if (inst && inst->usage == DAC_STRW)
825 return (PWSTR)(inst + 1);
829 // Determine the length of the string
830 // by iteratively reading blocks and scanning them
834 WCHAR buf[LOCAL_STR_BUF];
835 TADDR scanAddr = addr;
836 ULONG32 curBytes = 0;
841 status = g_dacImpl->m_pTarget->
842 ReadVirtual(scanAddr, (PBYTE)buf, sizeof(buf),
846 // We hit invalid memory before finding a terminator.
849 DacError(CORDBG_E_READVIRTUAL_FAILURE);
854 PWSTR scan = (PWSTR)buf;
855 PWSTR scanEnd = scan + (returned / sizeof(*scan));
856 while (scan < scanEnd)
868 // Found a terminator.
869 scanAddr += ((scan + 1) - buf) * sizeof(*scan);
873 // Ignore any partial character reads. The character
874 // will be reread on the next loop if necessary.
875 returned &= ~(sizeof(buf[0]) - 1);
877 // The assumption is that a memory read cannot wrap
878 // around the address space, thus if we have read to
879 // the top of memory scanAddr cannot wrap farther
881 curBytes += returned;
882 scanAddr += returned;
885 (curBytes + sizeof(buf[0]) - 1) / sizeof(buf[0]) >= maxChars)
887 // Wrapped around the top of memory or
888 // we didn't find a terminator within the given bound.
891 DacError(E_INVALIDARG);
897 // Now that we know the length we can create a
898 // host copy of the string.
899 PWSTR retVal = (PWSTR)
900 DacInstantiateTypeByAddress(addr, (ULONG32)(scanAddr - addr), throwEx);
902 (inst = g_dacImpl->m_instances.Find(addr)))
904 inst->usage = DAC_STRW;
912 DacGetTargetAddrForHostAddr(LPCVOID ptr, bool throwEx)
916 // Dac accesses are not interesting for PREfix and cause alot of PREfix noise
917 // so we just return the unmodified pointer for our PREFIX builds
922 // Preserve special pointer values.
923 if (ptr == NULL || ((TADDR) ptr == (TADDR)-1))
930 HRESULT status = E_FAIL;
934 DAC_INSTANCE* inst = (DAC_INSTANCE*)ptr - 1;
935 if (inst->sig == DAC_INSTANCE_SIG)
942 status = E_INVALIDARG;
947 status = E_INVALIDARG;
949 EX_END_CATCH(SwallowAllExceptions)
953 if (g_dacImpl && g_dacImpl->m_debugMode)
960 // This means a pointer was supplied which doesn't actually point to the beginning of
961 // a marshalled DAC instance.
962 _ASSERTE_MSG(false, "DAC coding error: Attempt to get target address from a host pointer "
963 "which is not an instance marshalled by DAC!");
974 // Similar to DacGetTargetAddrForHostAddr above except that ptr can represent any pointer within a host data
975 // structure marshalled from the target (rather than just a pointer to the first field).
977 DacGetTargetAddrForHostInteriorAddr(LPCVOID ptr, bool throwEx)
979 // Our algorithm for locating the containing DAC instance will search backwards through memory in
980 // DAC_INSTANCE_ALIGN increments looking for a valid header. The following constant determines how many of
981 // these iterations we'll perform before deciding the caller made a mistake and didn't marshal the
982 // containing instance from the target to the host properly. Lower values will determine the maximum
983 // offset from the start of a marshalled structure at which an interior pointer can appear. Higher values
984 // will bound the amount of time it takes to report an error in the case where code has been incorrectly
986 const DWORD kMaxSearchIterations = 100;
990 // Dac accesses are not interesting for PREfix and cause alot of PREfix noise
991 // so we just return the unmodified pointer for our PREFIX builds
996 // Preserve special pointer values.
997 if (ptr == NULL || ((TADDR) ptr == (TADDR)-1))
1004 HRESULT status = E_FAIL;
1008 // We're going to search backwards through memory from the pointer looking for a valid DAC
1009 // instance header. Initialize this search pointer to the first legal value it could hold.
1010 // Intuitively this would be ptr - sizeof(DAC_INSTANCE), but DAC_INSTANCE headers are further
1011 // constrained to lie on DAC_INSTANCE_ALIGN boundaries. DAC_INSTANCE_ALIGN is large (16 bytes) due
1012 // to the need to keep the marshalled structure also aligned for any possible need, so we gain
1013 // considerable performance from only needing to test for DAC_INSTANCE headers at
1014 // DAC_INSTANCE_ALIGN aligned addresses.
1015 DAC_INSTANCE * inst = (DAC_INSTANCE*)(((ULONG_PTR)ptr - sizeof(DAC_INSTANCE)) & ~(DAC_INSTANCE_ALIGN - 1));
1017 // When code is DAC'ized correctly then our search algorithm is guaranteed to terminate safely
1018 // before reading memory that doesn't belong to the containing DAC instance. Since people do make
1019 // mistakes we want to limit how long and far we search however. The counter below will let us
1020 // assert if we've likely tried to locate an interior host pointer in a non-marshalled structure.
1021 DWORD cIterations = 0;
1023 bool tryAgain = false;
1025 // Scan backwards in memory looking for a DAC_INSTANCE header.
1028 // Step back DAC_INSTANCE_ALIGN bytes at a time (the initialization of inst above guarantees
1029 // we start with an aligned pointer value. Stop every time our potential DAC_INSTANCE header
1030 // has a correct signature value.
1031 while (tryAgain || inst->sig != DAC_INSTANCE_SIG)
1034 inst = (DAC_INSTANCE*)((BYTE*)inst - DAC_INSTANCE_ALIGN);
1036 // If we've searched a lot of memory (currently 100 * 16 == 1600 bytes) without success,
1037 // then assume this is due to an issue DAC-izing code (if you really do have a field within a
1038 // DAC marshalled structure whose offset is >1600 bytes then feel free to update the
1039 // constant at the start of this method).
1040 if (++cIterations > kMaxSearchIterations)
1042 status = E_INVALIDARG;
1047 // Fall through to a DAC error if we searched too long without finding a header candidate.
1048 if (status == E_INVALIDARG)
1051 // Validate our candidate header by looking up the target address it claims to map in the
1052 // instance hash. The entry should both exist and correspond exactly to our candidate instance
1054 // TODO: but what if the same memory was marshalled more than once (eg. once as a DPTR, once as a VPTR)?
1055 if (inst == g_dacImpl->m_instances.Find(inst->addr))
1057 // We've found a valid DAC instance. Now validate that the marshalled structure it
1058 // represents really does enclose the pointer we're asking about. If not, someone hasn't
1059 // marshalled a containing structure before trying to map a pointer within that structure
1060 // (we've just gone and found the previous, unrelated marshalled structure in host memory).
1061 BYTE * parent = (BYTE*)(inst + 1);
1062 if (((BYTE*)ptr + sizeof(LPCVOID)) <= (parent + inst->size))
1064 // Everything checks out: we've found a DAC instance header and its address range
1065 // encompasses the pointer we're interested in. Compute the corresponding target
1066 // address by taking into account the offset of the interior pointer into its
1067 // enclosing structure.
1068 addr = inst->addr + ((BYTE*)ptr - parent);
1073 // We found a valid DAC instance but it doesn't cover the address range containing our
1074 // input pointer. Fall though to report an erroring DAC-izing code.
1075 status = E_INVALIDARG;
1081 // This must not really be a match, perhaps a coincidence?
1089 status = E_INVALIDARG;
1091 EX_END_CATCH(SwallowAllExceptions)
1095 if (g_dacImpl && g_dacImpl->m_debugMode)
1102 // This means a pointer was supplied which doesn't actually point to somewhere in a marshalled
1104 _ASSERTE_MSG(false, "DAC coding error: Attempt to get target address from a host interior "
1105 "pointer which is not an instance marshalled by DAC!");
1115 PWSTR DacGetVtNameW(TADDR targetVtable)
1117 PWSTR pszRet = NULL;
1119 ULONG *targ = &g_dacGlobals.Thread__vtAddr;
1120 ULONG *targStart = targ;
1121 for (ULONG i = 0; i < sizeof(g_dacHostVtPtrs) / sizeof(PVOID); i++)
1123 if (targetVtable == (*targ + DacGlobalBase()))
1125 pszRet = (PWSTR) *(g_dacVtStrings + (targ - targStart));
1135 DacGetTargetVtForHostVt(LPCVOID vtHost, bool throwEx)
1141 // The host vtable table exactly parallels the
1142 // target vtable table, so just iterate to a match
1143 // return the matching entry.
1144 host = &g_dacHostVtPtrs.Thread;
1145 targ = &g_dacGlobals.Thread__vtAddr;
1146 for (i = 0; i < sizeof(g_dacHostVtPtrs) / sizeof(PVOID); i++)
1148 if (*host == vtHost)
1150 return *targ + DacGlobalBase();
1159 DacError(E_INVALIDARG);
1165 // DacEnumMemoryRegion - report a region of memory to the dump generation code
1168 // addr - target address of the beginning of the memory region
1169 // size - number of bytes to report
1170 // fExpectSuccess - whether or not ASSERTs should be raised if some memory in this region
1171 // is found to be unreadable. Generally we should only report readable
1172 // memory (unless the target is corrupt, in which case we expect asserts
1173 // if target consistency checking is enabled). Reporting memory that
1174 // isn't fully readable often indicates an issue that could cause much worse
1175 // problems (loss of dump data, long/infinite loops in dump generation),
1176 // so we want to try and catch any such usage. Ocassionally we can't say
1177 // for sure how much of the reported region will be readable (eg. for the
1178 // LoaderHeap, we only know the length of the allocated address space, not
1179 // the size of the commit region for every block). In these special cases,
1180 // we pass false to indicate that we're happy reporting up to the first
1181 // unreadable byte. This should be avoided if at all possible.
1183 bool DacEnumMemoryRegion(TADDR addr, TSIZE_T size, bool fExpectSuccess /*=true*/)
1187 DacError(E_UNEXPECTED);
1191 return g_dacImpl->ReportMem(addr, size, fExpectSuccess);
1195 // DacUpdateMemoryRegion - updates/poisons a region of memory of generated dump
1198 // addr - target address of the beginning of the memory region
1199 // bufferSize - number of bytes to update/poison
1200 // buffer - data to be written at given target address
1202 bool DacUpdateMemoryRegion(TADDR addr, TSIZE_T bufferSize, BYTE* buffer)
1206 DacError(E_UNEXPECTED);
1210 return g_dacImpl->DacUpdateMemoryRegion(addr, bufferSize, buffer);
1214 DacWriteHostInstance(PVOID host, bool throwEx)
1218 DacError(E_UNEXPECTED);
1222 TADDR addr = DacGetTargetAddrForHostAddr(host, throwEx);
1228 DAC_INSTANCE* inst = (DAC_INSTANCE*)host - 1;
1229 return g_dacImpl->m_instances.Write(inst, throwEx);
1233 DacHostPtrHasEnumMark(LPCVOID host)
1235 if (!DacGetTargetAddrForHostAddr(host, false))
1237 // Make it easy to ignore invalid pointers when enumerating.
1241 DAC_INSTANCE* inst = ((DAC_INSTANCE*)host) - 1;
1242 bool marked = inst->enumMem ? true : false;
1243 inst->enumMem = true;
1248 DacHasMethodDescBeenEnumerated(LPCVOID pMD)
1250 if (!DacGetTargetAddrForHostAddr(pMD, false))
1252 // Make it easy to ignore invalid pointers when enumerating.
1256 DAC_INSTANCE* inst = ((DAC_INSTANCE*) pMD) - 1;
1257 bool MDEnumed = inst->MDEnumed ? true : false;
1262 DacSetMethodDescEnumerated(LPCVOID pMD)
1264 if (!DacGetTargetAddrForHostAddr(pMD, false))
1266 // Make it easy to ignore invalid pointers when enumerating.
1270 DAC_INSTANCE* inst = ((DAC_INSTANCE*) pMD) - 1;
1271 bool MDEnumed = inst->MDEnumed ? true : false;
1272 inst->MDEnumed = true;
1276 // This gets called from DAC-ized code in the VM.
1278 DacGetMDImport(const PEFile* peFile, bool throwEx)
1282 DacError(E_UNEXPECTED);
1286 return g_dacImpl->GetMDImport(peFile, throwEx);
1290 DacGetMDImport(const ReflectionModule* reflectionModule, bool throwEx)
1294 DacError(E_UNEXPECTED);
1298 return g_dacImpl->GetMDImport(reflectionModule, throwEx);
1302 DacGetIlMethod(TADDR methAddr)
1304 ULONG32 methodSize = static_cast<ULONG32>(PEDecoder::ComputeILMethodSize(methAddr));
1306 // Sometimes when reading from dumps and inspect NGEN images, but we end up reading metadata from IL image
1307 // the method RVA could not match and we could read from a random address that will translate in inconsistent
1308 // IL code header. If we see the size of the code bigger than 64 Megs we are probably reading a bad IL code header.
1309 // For details see issue DevDiv 273199.
1310 if (methodSize > 0x4000000)
1312 DacError(CORDBG_E_TARGET_INCONSISTENT);
1315 return (COR_ILMETHOD*)
1316 DacInstantiateTypeByAddress(methAddr, methodSize,
1320 #ifdef FEATURE_MINIMETADATA_IN_TRIAGEDUMPS
1322 DacMdCacheAddEEName(TADDR taEE, const SString& ssEEName)
1326 DacError(E_UNEXPECTED);
1330 g_dacImpl->MdCacheAddEEName(taEE, ssEEName);
1333 DacMdCacheGetEEName(TADDR taEE, SString & eeName)
1337 DacError(E_UNEXPECTED);
1341 return g_dacImpl->MdCacheGetEEName(taEE, eeName);
1344 #endif // FEATURE_MINIMETADATA_IN_TRIAGEDUMPS
1347 DacAllocHostOnlyInstance(ULONG32 size, bool throwEx)
1349 SUPPORTS_DAC_HOST_ONLY;
1352 DacError(E_UNEXPECTED);
1356 DAC_INSTANCE* inst = g_dacImpl->m_instances.Alloc(0, size, DAC_DPTR);
1359 DacError(E_OUTOFMEMORY);
1363 g_dacImpl->m_instances.AddSuperseded(inst);
1369 // Queries whether ASSERTs should be raised when inconsistencies in the target are detected
1372 // true if ASSERTs should be raised in DACized code.
1373 // false if ASSERTs should be ignored.
1376 // See code:ClrDataAccess::TargetConsistencyAssertsEnabled for details.
1377 bool DacTargetConsistencyAssertsEnabled()
1381 // No ClrDataAccess instance available (maybe we're still initializing). Any asserts when this is
1382 // the case should only be host-asserts (i.e. always bugs), and so we should just return true.
1386 return g_dacImpl->TargetConsistencyAssertsEnabled();
1390 // DacEnumCodeForStackwalk
1391 // This is a helper function to enumerate the instructions around a call site to aid heuristics
1392 // used by debugger stack walkers.
1395 // taCallEnd - target address of the instruction just after the call instruction for the stack
1396 // frame we want to examine(i.e. the return address for the next frame).
1398 // Note that this is shared by our two stackwalks during minidump generation,
1399 // code:Thread::EnumMemoryRegionsWorker and code:ClrDataAccess::EnumMemWalkStackHelper. Ideally
1400 // we'd only have one stackwalk, but we currently have two different APIs for stackwalking
1401 // (CLR StackFrameIterator and IXCLRDataStackWalk), and we must ensure that the memory needed
1402 // for either is captured in a minidump. Eventually, all clients should get moved over to the
1403 // arrowhead debugging architecture, at which time we can rip out all the IXCLRData APIs, and
1404 // so this logic could just be private to the EnumMem code for Thread.
1406 void DacEnumCodeForStackwalk(TADDR taCallEnd)
1411 // x86 stack walkers often end up having to guess
1412 // about what's a return address on the stack.
1413 // Doing so involves looking at the code at the
1414 // possible call site and seeing if it could
1415 // reach the callee. Save enough code and around
1416 // the call site to allow this with a dump.
1418 // For whatever reason 64-bit platforms require us to save
1419 // the instructions around the call sites on the stack as well.
1420 // Otherwise we cannnot show the stack in a minidump.
1422 // Note that everything we do here is a heuristic that won't always work in general.
1423 // Eg., part of the 2xMAX_INSTRUCTION_LENGTH range might not be mapped (we could be
1424 // right on a page boundary). More seriously, X86 is not necessarily parsable in reverse
1425 // (eg. there could be a segment-override prefix in front of the call instruction that
1426 // we miss). So we'll dump what we can and ignore any failures. Ideally we'd better
1427 // quantify exactly what debuggers need and why, and try and avoid these ugly heuristics.
1428 // It seems like these heuristics are too tightly coupled to the implementation details
1429 // of some specific debugger stackwalking algorithm.
1431 DacEnumMemoryRegion(taCallEnd - MAX_INSTRUCTION_LENGTH, MAX_INSTRUCTION_LENGTH * 2, false);
1433 #if defined(_TARGET_X86_)
1434 // If it was an indirect call we also need to save the data indirected through.
1435 // Note that this only handles absolute indirect calls (ModR/M byte of 0x15), all the other forms of
1436 // indirect calls are register-relative, and so we'd have to do a much more complicated decoding based
1437 // on the register context. Regardless, it seems like this is fundamentally error-prone because it's
1438 // aways possible that the call instruction was not 6 bytes long, and we could have some other instructions
1439 // that happen to match the pattern we're looking for.
1440 PTR_BYTE callCode = PTR_BYTE(taCallEnd - 6);
1441 PTR_BYTE callMrm = PTR_BYTE(taCallEnd - 5);
1442 PTR_TADDR callInd = PTR_TADDR(taCallEnd - 4);
1443 if (callCode.IsValid() &&
1444 (*callCode == 0xff) &&
1445 callMrm.IsValid() &&
1446 (*callMrm == 0x15) &&
1449 DacEnumMemoryRegion(*callInd, sizeof(TADDR), false);
1451 #endif // #ifdef _TARGET_X86_
1454 // ----------------------------------------------------------------------------
1455 // DacReplacePatches
1458 // Given the address and the size of a memory range which is stored in the buffer, replace all the patches
1459 // in the buffer with the real opcodes. This is especially important on X64 where the unwinder needs to
1460 // disassemble the native instructions.
1463 // * range - the address and the size of the memory range
1464 // * pBuffer - the buffer containting the memory range
1467 // Return S_OK if everything succeeds.
1470 // * The debuggee has to be stopped.
1473 // * @dbgtodo ICDProcess - When we DACize code:CordbProcess::ReadMemory,
1474 // we should change it to use this function.
1477 HRESULT DacReplacePatchesInHostMemory(MemoryRange range, PVOID pBuffer)
1481 // If the patch table is invalid, then there is no patch to replace.
1482 if (!DebuggerController::GetPatchTableValid())
1489 DebuggerPatchTable * pTable = DebuggerController::GetPatchTable();
1490 DebuggerControllerPatch * pPatch = pTable->GetFirstPatch(&info);
1493 // The unwinder needs to read the stack very often to restore pushed registers, retrieve the
1494 // return addres, etc. However, stack addresses should never be patched.
1495 // One way to optimize this code is to pass the stack base and the stack limit of the thread to this
1496 // function and use those two values to filter out stack addresses.
1498 // Another thing we can do is instead of enumerating the patches, we could enumerate the address.
1499 // This is more efficient when we have a large number of patches and a small memory range. Perhaps
1500 // we could do a hybrid approach, i.e. use the size of the range and the number of patches to dynamically
1501 // determine which enumeration is more efficient.
1503 while (pPatch != NULL)
1505 CORDB_ADDRESS patchAddress = (CORDB_ADDRESS)dac_cast<TADDR>(pPatch->address);
1507 if (patchAddress != NULL)
1509 PRD_TYPE opcode = pPatch->opcode;
1511 CORDB_ADDRESS address = (CORDB_ADDRESS)(dac_cast<TADDR>(range.StartAddress()));
1512 SIZE_T cbSize = range.Size();
1514 // Check if the address of the patch is in the specified memory range.
1515 if (IsPatchInRequestedRange(address, cbSize, patchAddress))
1517 // Replace the patch in the buffer with the original opcode.
1518 CORDbgSetInstructionEx(reinterpret_cast<PBYTE>(pBuffer), address, patchAddress, opcode, cbSize);
1522 pPatch = pTable->GetNextPatch(&info);