FCIMPL0(Object*, SystemNative::GetCommandLineArgs) { FCALL_CONTRACT; PTRARRAYREF strArray = NULL; HELPER_METHOD_FRAME_BEGIN_RET_1(strArray); LPWSTR commandLine; commandLine = WszGetCommandLine(); if (commandLine==NULL) COMPlusThrowOM(); DWORD numArgs = 0; LPWSTR* argv = SegmentCommandLine(commandLine, &numArgs); if (!argv) COMPlusThrowOM(); _ASSERTE(numArgs > 0); strArray = (PTRARRAYREF) AllocateObjectArray(numArgs, g_pStringClass); // Copy each argument into new Strings. for(unsigned int i=0; i<numArgs; i++) { STRINGREF str = StringObject::NewString(argv[i]); STRINGREF * destData = ((STRINGREF*)(strArray->GetDataPtr())) + i; SetObjectReference((OBJECTREF*)destData, (OBJECTREF)str, strArray->GetAppDomain()); } delete [] argv; HELPER_METHOD_FRAME_END(); return OBJECTREFToObject(strArray); }
VOID ComCallWrapper::InvokeByNameCallback(LPVOID ptr) { InvokeByNameArgs* args = (InvokeByNameArgs*)ptr; INT32 NumByrefArgs = 0; INT32 *aByrefArgMngVariantIndex = NULL; INT32 iArg; struct __gc { OBJECTREF Target; STRINGREF MemberName; PTRARRAYREF ParamArray; OBJECTREF TmpObj; OBJECTREF RetVal; } gc; ZeroMemory(&gc, sizeof(gc)); GCPROTECT_BEGIN(gc); gc.MemberName = COMString::NewString(args->MemberName); gc.ParamArray = (PTRARRAYREF)AllocateObjectArray(args->ArgCount, g_pObjectClass); // // Fill in the arguments. // for (iArg = 0; iArg < args->ArgCount; iArg++) { // Convert the variant. VARIANT *pSrcOleVariant = &args->ArgList[iArg]; OleVariant::MarshalObjectForOleVariant(pSrcOleVariant, &gc.TmpObj); gc.ParamArray->SetAt(iArg, gc.TmpObj); // If the argument is byref then add it to the array of byref arguments. if (V_VT(pSrcOleVariant) & VT_BYREF) { if (aByrefArgMngVariantIndex == NULL) { aByrefArgMngVariantIndex = (INT32 *)_alloca(sizeof(INT32) * args->ArgCount); } aByrefArgMngVariantIndex[NumByrefArgs] = iArg; NumByrefArgs++; } } gc.Target = ObjectFromHandle(args->pThis->m_hThis); // // Invoke using IReflect::InvokeMember // EEClass *pClass = gc.Target->GetClass(); // Retrieve the method descriptor that will be called on. MethodDesc *pMD = GetInvokeMemberMD(); // Prepare the arguments that will be passed to Invoke. ARG_SLOT Args[] = { ObjToArgSlot(GetReflectionObject(pClass)), // IReflect ObjToArgSlot(gc.MemberName), // name (ARG_SLOT) args->BindingFlags, // invokeAttr ObjToArgSlot(GetOleAutBinder()),// binder ObjToArgSlot(gc.Target), // target ObjToArgSlot(gc.ParamArray), // args ObjToArgSlot(NULL), // modifiers ObjToArgSlot(NULL), // culture ObjToArgSlot(NULL) // namedParameters }; // Do the actual method invocation. MetaSig metaSig(pMD->GetSig(),pMD->GetModule()); gc.RetVal = ArgSlotToObj(pMD->Call(Args, &metaSig)); // // Convert the return value and the byref arguments. // // Convert all the ByRef arguments back. for (iArg = 0; iArg < NumByrefArgs; iArg++) { INT32 i = aByrefArgMngVariantIndex[iArg]; gc.TmpObj = gc.ParamArray->GetAt(i); OleVariant::MarshalOleRefVariantForObject(&gc.TmpObj, &args->ArgList[i]); } // Convert the return COM+ object to an OLE variant. if (args->pRetVal) OleVariant::MarshalOleVariantForObject(&gc.RetVal, args->pRetVal); GCPROTECT_END(); }
//+---------------------------------------------------------------------------- // // Function: CallDescrWithObjectArray, private // // Synopsis: Builds the stack from a object array and call the object // // Note this function triggers GC and assumes that pServer, pArguments, pVarRet, and ppVarOutParams are // all already protected!! //+---------------------------------------------------------------------------- void CallDescrWithObjectArray(OBJECTREF& pServer, ReflectMethod *pRM, const BYTE *pTarget, MetaSig* sig, VASigCookie *pCookie, BOOL fIsStatic, PTRARRAYREF& pArgArray, OBJECTREF *pVarRet, PTRARRAYREF *ppVarOutParams) { THROWSCOMPLUSEXCEPTION(); TRIGGERSGC(); // the debugger, profiler code triggers a GC LOG((LF_REMOTING, LL_INFO10, "CallDescrWithObjectArray IN\n")); ByRefInfo *pByRefs = NULL; INT64 retval = 0; UINT nActualStackBytes = 0; LPBYTE pAlloc = 0; LPBYTE pFrameBase = 0; UINT32 numByRef = 0; DWORD attr = pRM->dwFlags; #ifdef _DEBUG MethodDesc *pMD = pRM->pMethod; #endif // check the calling convention BYTE callingconvention = sig->GetCallingConvention(); if (!isCallConv(callingconvention, IMAGE_CEE_CS_CALLCONV_DEFAULT)) { _ASSERTE(!"This calling convention is not supported."); COMPlusThrow(kInvalidProgramException); } #ifdef DEBUGGING_SUPPORTED // debugger goo What does this do? can someone put a comment here? if (CORDebuggerTraceCall()) g_pDebugInterface->TraceCall(pTarget); #endif // DEBUGGING_SUPPORTED #ifdef PROFILING_SUPPORTED // If we're profiling, notify the profiler that we're about to invoke the remoting target if (CORProfilerTrackRemoting()) g_profControlBlock.pProfInterface->RemotingServerInvocationStarted( reinterpret_cast<ThreadID>(GetThread())); #endif // PROFILING_SUPPORTED // Create a fake FramedMethodFrame on the stack. nActualStackBytes = sig->SizeOfActualFixedArgStack(fIsStatic); pAlloc = (LPBYTE)_alloca(FramedMethodFrame::GetNegSpaceSize() + sizeof(FramedMethodFrame) + nActualStackBytes); pFrameBase = pAlloc + FramedMethodFrame::GetNegSpaceSize(); // cycle through the parameters and see if there are byrefs BYTE typ = 0; BOOL fHasByRefs = FALSE; if (attr & RM_ATTR_BYREF_FLAG_SET) fHasByRefs = attr & RM_ATTR_HAS_BYREF_ARG; else { sig->Reset(); while ((typ = sig->NextArg()) != ELEMENT_TYPE_END) { if (typ == ELEMENT_TYPE_BYREF) { fHasByRefs = TRUE; attr |= RM_ATTR_HAS_BYREF_ARG; break; } } attr |= RM_ATTR_BYREF_FLAG_SET; pRM->dwFlags = attr; sig->Reset(); } int nFixedArgs = sig->NumFixedArgs(); // if there are byrefs allocate and array for the out parameters if (fHasByRefs) { *ppVarOutParams = PTRARRAYREF(AllocateObjectArray(sig->NumFixedArgs(), g_pObjectClass)); // Null out the array memset(&(*ppVarOutParams)->m_Array, 0, sizeof(OBJECTREF) * sig->NumFixedArgs()); } ArgIterator argit(pFrameBase, sig, fIsStatic); // set the this pointer OBJECTREF *ppThis = (OBJECTREF*)argit.GetThisAddr(); *ppThis = NULL; // if there is a return buffer, allocate it if (sig->HasRetBuffArg()) { EEClass *pEECValue = sig->GetRetEEClass(); _ASSERTE(pEECValue->IsValueClass()); MethodTable * mt = pEECValue->GetMethodTable(); *pVarRet = AllocateObject(mt); *(argit.GetRetBuffArgAddr()) = (*pVarRet)->UnBox(); #if defined(_PPC_) || defined(_SPARC_) // retbuf // the CallDescrWorker callsite for methods with return buffer is // different for RISC CPUs - we pass this information along by setting // the lowest bit in pTarget pTarget = (const BYTE *)((UINT_PTR)pTarget | 0x1); #endif } // gather data about the parameters by iterating over the sig: UINT32 arg = 0; UINT32 structSize = 0; int ofs = 0; // REVIEW: need to use actual arg count if VarArgs are supported ArgInfo* pArgInfo = (ArgInfo*) _alloca(nFixedArgs*sizeof(ArgInfo)); #ifdef _DEBUG // We expect to write useful data over every part of this so need // not do this in retail! memset((void *)pArgInfo, 0, sizeof(ArgInfo)*nFixedArgs); #endif for( ; 0 != (ofs = argit.GetNextOffset(&typ, &structSize)); arg++, pArgInfo++ ) { if (typ == ELEMENT_TYPE_BYREF) { EEClass *pClass = NULL; CorElementType brType = sig->GetByRefType(&pClass); if (CorIsPrimitiveType(brType)) { pArgInfo->dataSize = gElementTypeInfo[brType].m_cbSize; } else if (pClass->IsValueClass()) { pArgInfo->dataSize = pClass->GetAlignedNumInstanceFieldBytes(); numByRef ++; } else { pArgInfo->dataSize = sizeof(Object *); numByRef ++; } ByRefInfo *brInfo = (ByRefInfo *) _alloca(offsetof(ByRefInfo,data) + pArgInfo->dataSize); brInfo->argIndex = arg; brInfo->typ = brType; brInfo->pClass = pClass; brInfo->pNext = pByRefs; pByRefs = brInfo; pArgInfo->dataLocation = (BYTE*)brInfo->data; *((void**)(pFrameBase + ofs)) = (void*)pArgInfo->dataLocation; pArgInfo->dataClass = pClass; pArgInfo->dataType = brType; pArgInfo->byref = TRUE; } else { pArgInfo->dataLocation = pFrameBase + ofs; pArgInfo->dataSize = StackElemSize(structSize); pArgInfo->dataClass = sig->GetTypeHandle().GetClass(); // this may cause GC! pArgInfo->dataType = typ; pArgInfo->byref = FALSE; } } if (!fIsStatic) { // If this isn't a value class, verify the objectref #ifdef _DEBUG if (pMD->GetClass()->IsValueClass() == FALSE) { VALIDATEOBJECTREF(pServer); } #endif //_DEBUG *ppThis = pServer; } // There should be no GC when we fill up the stack with parameters, as we don't protect them // Assignment of "*ppThis" above triggers the point where we become unprotected. BEGINFORBIDGC(); // reset pArgInfo to point to the start of the block we _alloca-ed pArgInfo = pArgInfo-nFixedArgs; PBYTE dataLocation; INT32 dataSize; EEClass *dataClass; BYTE dataType; OBJECTREF* pArguments = pArgArray->m_Array; UINT32 i, j = arg; for (i=0; i<j; i++) { dataSize = pArgInfo->dataSize; dataLocation = pArgInfo->dataLocation; dataClass = pArgInfo->dataClass; dataType = pArgInfo->dataType; switch (dataSize) { case 1: // This "if" statement is necessary to make the assignement big-endian aware if (pArgInfo->byref) *((INT8*)dataLocation) = *((INT8*)pArguments[i]->GetData()); else *(StackElemType*)dataLocation = (StackElemType)*((INT8*)pArguments[i]->GetData()); break; case 2: // This "if" statement is necessary to make the assignement big-endian aware if (pArgInfo->byref) *((INT16*)dataLocation) = *((INT16*)pArguments[i]->GetData()); else *(StackElemType*)dataLocation = (StackElemType)*((INT16*)pArguments[i]->GetData()); break; case 4: if ((dataType == ELEMENT_TYPE_STRING) || (dataType == ELEMENT_TYPE_OBJECT) || (dataType == ELEMENT_TYPE_CLASS) || (dataType == ELEMENT_TYPE_SZARRAY) || (dataType == ELEMENT_TYPE_ARRAY)) { *(OBJECTREF *)dataLocation = pArguments[i]; } else { *(StackElemType*)dataLocation = (StackElemType)*((INT32*)pArguments[i]->GetData()); } break; case 8: *((INT64*)dataLocation) = *((INT64*)pArguments[i]->GetData()); break; default: { memcpy(dataLocation, pArguments[i]->UnBox(), dataSize); } } pArgInfo++; } #ifdef _DEBUG // Should not be using this any more pArgInfo = pArgInfo - nFixedArgs; memset((void *)pArgInfo, 0, sizeof(ArgInfo)*nFixedArgs); #endif // if there were byrefs, push a protection frame ProtectByRefsFrame *pProtectionFrame = NULL; if (pByRefs && numByRef > 0) { char *pBuffer = (char*)_alloca (sizeof (ProtectByRefsFrame)); pProtectionFrame = new (pBuffer) ProtectByRefsFrame(GetThread(), pByRefs); } // call the correct worker function depending of if the method // is varargs or not ENDFORBIDGC(); #ifdef _PPC_ FramedMethodFrame::Enregister(pFrameBase, sig, fIsStatic, nActualStackBytes); #endif INSTALL_COMPLUS_EXCEPTION_HANDLER(); retval = CallDescrWorker( pFrameBase + sizeof(FramedMethodFrame) + nActualStackBytes, nActualStackBytes / STACK_ELEM_SIZE, #if defined(_X86_) || defined(_PPC_) // argregs (ArgumentRegisters*)(pFrameBase + FramedMethodFrame::GetOffsetOfArgumentRegisters()), #endif (LPVOID)pTarget); UNINSTALL_COMPLUS_EXCEPTION_HANDLER(); // set floating point return values getFPReturn(sig->GetFPReturnSize(), &retval); // need to build a object based on the return type. if (!sig->HasRetBuffArg()) { BYTE *pRetVal = (BYTE*)&retval; #ifdef BIGENDIAN switch (sig->GetReturnTypeSize()) { case 1: pRetVal += sizeof(void*)-1; break; case 2: pRetVal += sizeof(void*)-2; break; default: // nothing to do break; } #endif GetObjectFromStack(pVarRet, pRetVal, sig->GetReturnType(), sig->GetRetEEClass()); } // extract the out args from the byrefs if (pByRefs) { do { // Always extract the data ptr every time we enter this loop because // calls to GetObjectFromStack below can cause a GC. // Even this is not enough, because that we are passing a pointer to GC heap // to GetObjectFromStack . If GC happens, nobody is protecting the passed in pointer. OBJECTREF pTmp = NULL; GetObjectFromStack(&pTmp, pByRefs->data, pByRefs->typ, pByRefs->pClass); (*ppVarOutParams)->SetAt(pByRefs->argIndex, pTmp); pByRefs = pByRefs->pNext; } while (pByRefs); if (pProtectionFrame) pProtectionFrame->Pop(); } #ifdef PROFILING_SUPPORTED // If we're profiling, notify the profiler that we're about to invoke the remoting target if (CORProfilerTrackRemoting()) g_profControlBlock.pProfInterface->RemotingServerInvocationReturned( reinterpret_cast<ThreadID>(GetThread())); #endif // PROFILING_SUPPORTED LOG((LF_REMOTING, LL_INFO10, "CallDescrWithObjectArray OUT\n")); }