icall.c 303 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934
  1. /**
  2. * \file
  3. *
  4. * Authors:
  5. * Dietmar Maurer (dietmar@ximian.com)
  6. * Paolo Molaro (lupus@ximian.com)
  7. * Patrik Torstensson (patrik.torstensson@labs2.com)
  8. * Marek Safar (marek.safar@gmail.com)
  9. * Aleksey Kliger (aleksey@xamarin.com)
  10. *
  11. * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
  12. * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  13. * Copyright 2011-2015 Xamarin Inc (http://www.xamarin.com).
  14. * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  15. */
  16. #include <config.h>
  17. #if defined(TARGET_WIN32) || defined(HOST_WIN32)
  18. /* Needed for _ecvt_s */
  19. #define MINGW_HAS_SECURE_API 1
  20. #include <stdio.h>
  21. #endif
  22. #include <glib.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #ifdef HAVE_ALLOCA_H
  27. #include <alloca.h>
  28. #endif
  29. #ifdef HAVE_SYS_TIME_H
  30. #include <sys/time.h>
  31. #endif
  32. #ifdef HAVE_UNISTD_H
  33. #include <unistd.h>
  34. #endif
  35. #if defined (HAVE_WCHAR_H)
  36. #include <wchar.h>
  37. #endif
  38. #include "mono/metadata/icall-internals.h"
  39. #include "mono/utils/mono-membar.h"
  40. #include <mono/metadata/object.h>
  41. #include <mono/metadata/threads.h>
  42. #include <mono/metadata/threads-types.h>
  43. #include <mono/metadata/threadpool.h>
  44. #include <mono/metadata/threadpool-io.h>
  45. #include <mono/metadata/monitor.h>
  46. #include <mono/metadata/reflection.h>
  47. #include <mono/metadata/image-internals.h>
  48. #include <mono/metadata/assembly.h>
  49. #include <mono/metadata/assembly-internals.h>
  50. #include <mono/metadata/tabledefs.h>
  51. #include <mono/metadata/exception.h>
  52. #include <mono/metadata/exception-internals.h>
  53. #include <mono/metadata/w32file.h>
  54. #include <mono/metadata/console-io.h>
  55. #include <mono/metadata/w32socket.h>
  56. #include <mono/metadata/mono-endian.h>
  57. #include <mono/metadata/tokentype.h>
  58. #include <mono/metadata/metadata-internals.h>
  59. #include <mono/metadata/class-internals.h>
  60. #include <mono/metadata/class-init.h>
  61. #include <mono/metadata/reflection-internals.h>
  62. #include <mono/metadata/marshal.h>
  63. #include <mono/metadata/gc-internals.h>
  64. #include <mono/metadata/mono-gc.h>
  65. #include <mono/metadata/rand.h>
  66. #include <mono/metadata/appdomain-icalls.h>
  67. #include <mono/metadata/string-icalls.h>
  68. #include <mono/metadata/debug-helpers.h>
  69. #include <mono/metadata/w32process.h>
  70. #include <mono/metadata/environment.h>
  71. #include <mono/metadata/profiler-private.h>
  72. #include <mono/metadata/locales.h>
  73. #include <mono/metadata/filewatcher.h>
  74. #include <mono/metadata/security.h>
  75. #include <mono/metadata/mono-config.h>
  76. #include <mono/metadata/cil-coff.h>
  77. #include <mono/metadata/number-formatter.h>
  78. #include <mono/metadata/security-manager.h>
  79. #include <mono/metadata/security-core-clr.h>
  80. #include <mono/metadata/mono-perfcounters.h>
  81. #include <mono/metadata/mono-debug.h>
  82. #include <mono/metadata/mono-ptr-array.h>
  83. #include <mono/metadata/verify-internals.h>
  84. #include <mono/metadata/runtime.h>
  85. #include <mono/metadata/file-mmap.h>
  86. #include <mono/metadata/seq-points-data.h>
  87. #include <mono/metadata/icall-table.h>
  88. #include <mono/metadata/handle.h>
  89. #include <mono/metadata/w32mutex.h>
  90. #include <mono/metadata/w32semaphore.h>
  91. #include <mono/metadata/w32event.h>
  92. #include <mono/metadata/abi-details.h>
  93. #include <mono/metadata/loader-internals.h>
  94. #include <mono/metadata/unity-utils.h>
  95. #include <mono/utils/monobitset.h>
  96. #include <mono/utils/mono-time.h>
  97. #include <mono/utils/mono-proclib.h>
  98. #include <mono/utils/mono-string.h>
  99. #include <mono/utils/mono-error-internals.h>
  100. #include <mono/utils/mono-mmap.h>
  101. #include <mono/utils/mono-io-portability.h>
  102. #include <mono/utils/mono-digest.h>
  103. #include <mono/utils/bsearch.h>
  104. #include <mono/utils/mono-os-mutex.h>
  105. #include <mono/utils/mono-threads.h>
  106. #include <mono/metadata/w32error.h>
  107. #include <mono/utils/w32api.h>
  108. #include <mono/utils/mono-merp.h>
  109. #include <mono/utils/mono-state.h>
  110. #include <mono/utils/mono-logger-internals.h>
  111. #include <mono/utils/mono-math.h>
  112. #if !defined(HOST_WIN32) && defined(HAVE_SYS_UTSNAME_H)
  113. #include <sys/utsname.h>
  114. #endif
  115. #include "icall-decl.h"
  116. #include "mono/utils/mono-threads-coop.h"
  117. #include "mono/metadata/icall-signatures.h"
  118. #include "mono/utils/mono-signal-handler.h"
  119. #if _MSC_VER
  120. #pragma warning(disable:4047) // FIXME differs in levels of indirection
  121. #endif
  122. //#define MONO_DEBUG_ICALLARRAY
  123. // Inline with CoreCLR heuristics, https://github.com/dotnet/runtime/blob/69e114c1abf91241a0eeecf1ecceab4711b8aa62/src/coreclr/vm/threads.cpp#L6408.
  124. // Minimum stack size should be sufficient to allow a typical non-recursive call chain to execute,
  125. // including potential exception handling and garbage collection. Used for probing for available
  126. // stack space through RuntimeHelpers.EnsureSufficientExecutionStack.
  127. #if TARGET_SIZEOF_VOID_P == 8
  128. #define MONO_MIN_EXECUTION_STACK_SIZE (128 * 1024)
  129. #else
  130. #define MONO_MIN_EXECUTION_STACK_SIZE (64 * 1024)
  131. #endif
  132. #ifdef MONO_DEBUG_ICALLARRAY
  133. static char debug_icallarray; // 0:uninitialized 1:true 2:false
  134. static gboolean
  135. icallarray_print_enabled (void)
  136. {
  137. if (!debug_icallarray)
  138. debug_icallarray = MONO_TRACE_IS_TRACED (G_LOG_LEVEL_DEBUG, MONO_TRACE_ICALLARRAY) ? 1 : 2;
  139. return debug_icallarray == 1;
  140. }
  141. static void
  142. icallarray_print (const char *format, ...)
  143. {
  144. if (!icallarray_print_enabled ())
  145. return;
  146. va_list args;
  147. va_start (args, format);
  148. g_printv (format, args);
  149. va_end (args);
  150. }
  151. #else
  152. #define icallarray_print_enabled() (FALSE)
  153. #define icallarray_print(...) /* nothing */
  154. #endif
  155. /* Lazy class loading functions */
  156. static GENERATE_GET_CLASS_WITH_CACHE (module, "System.Reflection", "Module")
  157. static void
  158. array_set_value_impl (MonoArrayHandle arr, MonoObjectHandle value, guint32 pos, gboolean strict_enums, gboolean strict_signs, MonoError *error);
  159. static MonoArrayHandle
  160. type_array_from_modifiers (MonoType *type, int optional, MonoError *error);
  161. static inline MonoBoolean
  162. is_generic_parameter (MonoType *type)
  163. {
  164. return !type->byref && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR);
  165. }
  166. #ifndef HOST_WIN32
  167. static inline void
  168. mono_icall_make_platform_path (gchar *path)
  169. {
  170. return;
  171. }
  172. static inline const gchar *
  173. mono_icall_get_file_path_prefix (const gchar *path)
  174. {
  175. return "file://";
  176. }
  177. #endif /* HOST_WIN32 */
  178. MonoJitICallInfos mono_jit_icall_info;
  179. MonoObjectHandle
  180. ves_icall_System_Array_GetValueImpl (MonoArrayHandle array, guint32 pos, MonoError *error)
  181. {
  182. MonoClass * const array_class = mono_handle_class (array);
  183. MonoClass * const element_class = m_class_get_element_class (array_class);
  184. #ifdef ENABLE_NETCORE
  185. if (m_class_is_native_pointer (element_class)) {
  186. mono_error_set_not_supported (error, NULL);
  187. return NULL_HANDLE;
  188. }
  189. #endif
  190. if (m_class_is_valuetype (element_class)) {
  191. gsize element_size = mono_array_element_size (array_class);
  192. gpointer element_address = mono_array_addr_with_size_fast (MONO_HANDLE_RAW (array), element_size, (gsize)pos);
  193. return mono_value_box_handle (MONO_HANDLE_DOMAIN (array), element_class, element_address, error);
  194. }
  195. MonoObjectHandle result = mono_new_null ();
  196. mono_handle_array_getref (result, array, pos);
  197. return result;
  198. }
  199. MonoObjectHandle
  200. ves_icall_System_Array_GetValue (MonoArrayHandle arr, MonoArrayHandle indices, MonoError *error)
  201. {
  202. MONO_CHECK_ARG_NULL_HANDLE (indices, NULL_HANDLE);
  203. MonoClass * const indices_class = mono_handle_class (indices);
  204. MonoClass * const array_class = mono_handle_class (arr);
  205. g_assert (m_class_get_rank (indices_class) == 1);
  206. if (MONO_HANDLE_GETVAL (indices, bounds) || MONO_HANDLE_GETVAL (indices, max_length) != m_class_get_rank (array_class)) {
  207. mono_error_set_argument (error, NULL, NULL);
  208. return NULL_HANDLE;
  209. }
  210. gint32 index = 0;
  211. if (!MONO_HANDLE_GETVAL (arr, bounds)) {
  212. MONO_HANDLE_ARRAY_GETVAL (index, indices, gint32, 0);
  213. if (index < 0 || index >= MONO_HANDLE_GETVAL (arr, max_length)) {
  214. mono_error_set_index_out_of_range (error);
  215. return NULL_HANDLE;
  216. }
  217. return ves_icall_System_Array_GetValueImpl (arr, index, error);
  218. }
  219. for (gint32 i = 0; i < m_class_get_rank (array_class); i++) {
  220. MONO_HANDLE_ARRAY_GETVAL (index, indices, gint32, i);
  221. if ((index < MONO_HANDLE_GETVAL (arr, bounds [i].lower_bound)) ||
  222. (index >= (mono_array_lower_bound_t)MONO_HANDLE_GETVAL (arr, bounds [i].length) + MONO_HANDLE_GETVAL (arr, bounds [i].lower_bound))) {
  223. mono_error_set_index_out_of_range (error);
  224. return NULL_HANDLE;
  225. }
  226. }
  227. MONO_HANDLE_ARRAY_GETVAL (index, indices, gint32, 0);
  228. gint32 pos = index - MONO_HANDLE_GETVAL (arr, bounds [0].lower_bound);
  229. for (gint32 i = 1; i < m_class_get_rank (array_class); i++) {
  230. MONO_HANDLE_ARRAY_GETVAL (index, indices, gint32, i);
  231. pos = pos * MONO_HANDLE_GETVAL (arr, bounds [i].length) + index -
  232. MONO_HANDLE_GETVAL (arr, bounds [i].lower_bound);
  233. }
  234. return ves_icall_System_Array_GetValueImpl (arr, pos, error);
  235. }
  236. void
  237. ves_icall_System_Array_SetValueImpl (MonoArrayHandle arr, MonoObjectHandle value, guint32 pos, MonoError *error)
  238. {
  239. array_set_value_impl (arr, value, pos, FALSE, TRUE, error);
  240. }
  241. static inline void
  242. set_invalid_cast (MonoError *error, MonoClass *src_class, MonoClass *dst_class)
  243. {
  244. mono_get_runtime_callbacks ()->set_cast_details (src_class, dst_class);
  245. mono_error_set_invalid_cast (error);
  246. }
  247. #if ENABLE_NETCORE
  248. void
  249. ves_icall_System_Array_SetValueRelaxedImpl (MonoArrayHandle arr, MonoObjectHandle value, guint32 pos, MonoError *error)
  250. {
  251. array_set_value_impl (arr, value, pos, FALSE, FALSE, error);
  252. }
  253. // Copied from CoreCLR: https://github.com/dotnet/coreclr/blob/d3e39bc2f81e3dbf9e4b96347f62b49d8700336c/src/vm/invokeutil.cpp#L33
  254. #define PT_Primitive 0x01000000
  255. static const guint32 primitive_conversions [] = {
  256. 0x00, // MONO_TYPE_END
  257. 0x00, // MONO_TYPE_VOID
  258. PT_Primitive | 0x0004, // MONO_TYPE_BOOLEAN
  259. PT_Primitive | 0x3F88, // MONO_TYPE_CHAR (W = U2, CHAR, I4, U4, I8, U8, R4, R8)
  260. PT_Primitive | 0x3550, // MONO_TYPE_I1 (W = I1, I2, I4, I8, R4, R8)
  261. PT_Primitive | 0x3FE8, // MONO_TYPE_U1 (W = CHAR, U1, I2, U2, I4, U4, I8, U8, R4, R8)
  262. PT_Primitive | 0x3540, // MONO_TYPE_I2 (W = I2, I4, I8, R4, R8)
  263. PT_Primitive | 0x3F88, // MONO_TYPE_U2 (W = U2, CHAR, I4, U4, I8, U8, R4, R8)
  264. PT_Primitive | 0x3500, // MONO_TYPE_I4 (W = I4, I8, R4, R8)
  265. PT_Primitive | 0x3E00, // MONO_TYPE_U4 (W = U4, I8, R4, R8)
  266. PT_Primitive | 0x3400, // MONO_TYPE_I8 (W = I8, R4, R8)
  267. PT_Primitive | 0x3800, // MONO_TYPE_U8 (W = U8, R4, R8)
  268. PT_Primitive | 0x3000, // MONO_TYPE_R4 (W = R4, R8)
  269. PT_Primitive | 0x2000, // MONO_TYPE_R8 (W = R8)
  270. };
  271. // Copied from CoreCLR: https://github.com/dotnet/coreclr/blob/030a3ea9b8dbeae89c90d34441d4d9a1cf4a7de6/src/vm/invokeutil.h#L176
  272. static
  273. gboolean can_primitive_widen (MonoTypeEnum src_type, MonoTypeEnum dest_type)
  274. {
  275. if (dest_type > MONO_TYPE_R8 || src_type > MONO_TYPE_R8) {
  276. return (MONO_TYPE_I == dest_type && MONO_TYPE_I == src_type) || (MONO_TYPE_U == dest_type && MONO_TYPE_U == src_type);
  277. }
  278. return ((1 << dest_type) & primitive_conversions [src_type]) != 0;
  279. }
  280. // Copied from CoreCLR: https://github.com/dotnet/coreclr/blob/eafa8648ebee92de1380278b15cd5c2b6ef11218/src/vm/array.cpp#L1406
  281. static MonoTypeEnum
  282. get_normalized_integral_array_element_type (MonoTypeEnum elementType)
  283. {
  284. // Array Primitive types such as E_T_I4 and E_T_U4 are interchangeable
  285. // Enums with interchangeable underlying types are interchangable
  286. // BOOL is NOT interchangeable with I1/U1, neither CHAR -- with I2/U2
  287. switch (elementType) {
  288. case MONO_TYPE_U1:
  289. case MONO_TYPE_U2:
  290. case MONO_TYPE_U4:
  291. case MONO_TYPE_U8:
  292. case MONO_TYPE_U:
  293. return (MonoTypeEnum) (elementType - 1); // normalize to signed type
  294. }
  295. return elementType;
  296. }
  297. MonoBoolean
  298. ves_icall_System_Array_CanChangePrimitive (MonoReflectionType *volatile* ref_src_type_handle, MonoReflectionType *volatile* ref_dst_type_handle, MonoBoolean reliable)
  299. {
  300. MonoReflectionType* const ref_src_type = *ref_src_type_handle;
  301. MonoReflectionType* const ref_dst_type = *ref_dst_type_handle;
  302. MonoType *src_type = ref_src_type->type;
  303. MonoType *dst_type = ref_dst_type->type;
  304. g_assert (mono_type_is_primitive (src_type));
  305. g_assert (mono_type_is_primitive (dst_type));
  306. MonoTypeEnum normalized_src_type = get_normalized_integral_array_element_type (src_type->type);
  307. MonoTypeEnum normalized_dst_type = get_normalized_integral_array_element_type (dst_type->type);
  308. // Allow conversions like int <-> uint
  309. if (normalized_src_type == normalized_dst_type) {
  310. return TRUE;
  311. }
  312. // Widening is not allowed if reliable is true.
  313. if (reliable) {
  314. return FALSE;
  315. }
  316. // NOTE we don't use normalized types here so int -> ulong will be false
  317. // see https://github.com/dotnet/coreclr/pull/25209#issuecomment-505952295
  318. return can_primitive_widen (src_type->type, dst_type->type);
  319. }
  320. #endif
  321. static void
  322. array_set_value_impl (MonoArrayHandle arr_handle, MonoObjectHandle value_handle, guint32 pos, gboolean strict_enums, gboolean strict_signs, MonoError *error)
  323. {
  324. MonoClass *ac, *vc, *ec;
  325. gint32 esize, vsize;
  326. gpointer *ea = NULL, *va = NULL;
  327. guint64 u64 = 0;
  328. gint64 i64 = 0;
  329. gdouble r64 = 0;
  330. gboolean castOk = FALSE;
  331. gboolean et_isenum = FALSE;
  332. gboolean vt_isenum = FALSE;
  333. error_init (error);
  334. if (!MONO_HANDLE_IS_NULL (value_handle))
  335. vc = mono_handle_class (value_handle);
  336. else
  337. vc = NULL;
  338. ac = mono_handle_class (arr_handle);
  339. ec = m_class_get_element_class (ac);
  340. esize = mono_array_element_size (ac);
  341. if (mono_class_is_nullable (ec)) {
  342. #ifdef ENABLE_NETCORE
  343. if (vc && m_class_is_primitive (vc) && vc != m_class_get_nullable_elem_class (ec)) {
  344. // T -> Nullable<T> T must be exact
  345. set_invalid_cast (error, vc, ec);
  346. goto leave;
  347. }
  348. #endif
  349. MONO_ENTER_NO_SAFEPOINTS;
  350. ea = (gpointer*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle), esize, pos);
  351. if (!MONO_HANDLE_IS_NULL (value_handle))
  352. va = (gpointer*) mono_object_unbox_internal (MONO_HANDLE_RAW (value_handle));
  353. mono_nullable_init_unboxed ((guint8*)ea, va, ec);
  354. MONO_EXIT_NO_SAFEPOINTS;
  355. goto leave;
  356. }
  357. if (MONO_HANDLE_IS_NULL (value_handle)) {
  358. MONO_ENTER_NO_SAFEPOINTS;
  359. ea = (gpointer*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle), esize, pos);
  360. mono_gc_bzero_atomic (ea, esize);
  361. MONO_EXIT_NO_SAFEPOINTS;
  362. goto leave;
  363. }
  364. #ifdef ENABLE_NETCORE
  365. #define WIDENING_MSG NULL
  366. #define WIDENING_ARG NULL
  367. #else
  368. #define WIDENING_MSG "not a widening conversion"
  369. #define WIDENING_ARG "value"
  370. #endif
  371. #define NO_WIDENING_CONVERSION G_STMT_START{ \
  372. mono_error_set_argument (error, WIDENING_ARG, WIDENING_MSG); \
  373. break; \
  374. }G_STMT_END
  375. #define CHECK_WIDENING_CONVERSION(extra) G_STMT_START{ \
  376. if (esize < vsize + (extra)) { \
  377. mono_error_set_argument (error, WIDENING_ARG, WIDENING_MSG); \
  378. break; \
  379. } \
  380. }G_STMT_END
  381. #define INVALID_CAST G_STMT_START{ \
  382. mono_get_runtime_callbacks ()->set_cast_details (vc, ec); \
  383. mono_error_set_invalid_cast (error); \
  384. break; \
  385. }G_STMT_END
  386. MonoTypeEnum et;
  387. et = m_class_get_byval_arg (ec)->type;
  388. MonoTypeEnum vt;
  389. vt = m_class_get_byval_arg (vc)->type;
  390. /* Check element (destination) type. */
  391. switch (et) {
  392. case MONO_TYPE_STRING:
  393. switch (vt) {
  394. case MONO_TYPE_STRING:
  395. break;
  396. default:
  397. INVALID_CAST;
  398. }
  399. break;
  400. case MONO_TYPE_BOOLEAN:
  401. switch (vt) {
  402. case MONO_TYPE_BOOLEAN:
  403. break;
  404. case MONO_TYPE_CHAR:
  405. case MONO_TYPE_U1:
  406. case MONO_TYPE_U2:
  407. case MONO_TYPE_U4:
  408. case MONO_TYPE_U8:
  409. case MONO_TYPE_I1:
  410. case MONO_TYPE_I2:
  411. case MONO_TYPE_I4:
  412. case MONO_TYPE_I8:
  413. case MONO_TYPE_R4:
  414. case MONO_TYPE_R8:
  415. NO_WIDENING_CONVERSION;
  416. break;
  417. default:
  418. INVALID_CAST;
  419. }
  420. break;
  421. default:
  422. break;
  423. }
  424. if (!is_ok (error))
  425. goto leave;
  426. castOk = mono_object_handle_isinst_mbyref_raw (value_handle, ec, error);
  427. if (!is_ok (error))
  428. goto leave;
  429. if (!m_class_is_valuetype (ec)) {
  430. if (!castOk)
  431. INVALID_CAST;
  432. if (is_ok (error))
  433. MONO_HANDLE_ARRAY_SETREF (arr_handle, pos, value_handle);
  434. goto leave;
  435. }
  436. if (castOk) {
  437. MONO_ENTER_NO_SAFEPOINTS;
  438. ea = (gpointer*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle), esize, pos);
  439. va = (gpointer*) mono_object_unbox_internal (MONO_HANDLE_RAW (value_handle));
  440. if (m_class_has_references (ec))
  441. mono_value_copy_internal (ea, va, ec);
  442. else
  443. mono_gc_memmove_atomic (ea, va, esize);
  444. MONO_EXIT_NO_SAFEPOINTS;
  445. goto leave;
  446. }
  447. if (!m_class_is_valuetype (vc))
  448. INVALID_CAST;
  449. if (!is_ok (error))
  450. goto leave;
  451. vsize = mono_class_value_size (vc, NULL);
  452. et_isenum = et == MONO_TYPE_VALUETYPE && m_class_is_enumtype (m_class_get_byval_arg (ec)->data.klass);
  453. vt_isenum = vt == MONO_TYPE_VALUETYPE && m_class_is_enumtype (m_class_get_byval_arg (vc)->data.klass);
  454. #if ENABLE_NETCORE
  455. if (strict_enums && et_isenum && !vt_isenum) {
  456. INVALID_CAST;
  457. goto leave;
  458. }
  459. #endif
  460. if (et_isenum)
  461. et = mono_class_enum_basetype_internal (m_class_get_byval_arg (ec)->data.klass)->type;
  462. if (vt_isenum)
  463. vt = mono_class_enum_basetype_internal (m_class_get_byval_arg (vc)->data.klass)->type;
  464. #if ENABLE_NETCORE
  465. // Treat MONO_TYPE_U/I as MONO_TYPE_U8/I8/U4/I4
  466. #if SIZEOF_VOID_P == 8
  467. vt = vt == MONO_TYPE_U ? MONO_TYPE_U8 : (vt == MONO_TYPE_I ? MONO_TYPE_I8 : vt);
  468. et = et == MONO_TYPE_U ? MONO_TYPE_U8 : (et == MONO_TYPE_I ? MONO_TYPE_I8 : et);
  469. #else
  470. vt = vt == MONO_TYPE_U ? MONO_TYPE_U4 : (vt == MONO_TYPE_I ? MONO_TYPE_I4 : vt);
  471. et = et == MONO_TYPE_U ? MONO_TYPE_U4 : (et == MONO_TYPE_I ? MONO_TYPE_I4 : et);
  472. #endif
  473. #endif
  474. #define ASSIGN_UNSIGNED(etype) G_STMT_START{\
  475. switch (vt) { \
  476. case MONO_TYPE_U1: \
  477. case MONO_TYPE_U2: \
  478. case MONO_TYPE_U4: \
  479. case MONO_TYPE_U8: \
  480. case MONO_TYPE_CHAR: \
  481. CHECK_WIDENING_CONVERSION(0); \
  482. *(etype *) ea = (etype) u64; \
  483. break; \
  484. /* You can't assign a signed value to an unsigned array. */ \
  485. case MONO_TYPE_I1: \
  486. case MONO_TYPE_I2: \
  487. case MONO_TYPE_I4: \
  488. case MONO_TYPE_I8: \
  489. if (!strict_signs) { \
  490. CHECK_WIDENING_CONVERSION(0); \
  491. *(etype *) ea = (etype) i64; \
  492. break; \
  493. } \
  494. /* You can't assign a floating point number to an integer array. */ \
  495. case MONO_TYPE_R4: \
  496. case MONO_TYPE_R8: \
  497. NO_WIDENING_CONVERSION; \
  498. break; \
  499. default: \
  500. INVALID_CAST; \
  501. break; \
  502. } \
  503. }G_STMT_END
  504. #define ASSIGN_SIGNED(etype) G_STMT_START{\
  505. switch (vt) { \
  506. case MONO_TYPE_I1: \
  507. case MONO_TYPE_I2: \
  508. case MONO_TYPE_I4: \
  509. case MONO_TYPE_I8: \
  510. CHECK_WIDENING_CONVERSION(0); \
  511. *(etype *) ea = (etype) i64; \
  512. break; \
  513. /* You can assign an unsigned value to a signed array if the array's */ \
  514. /* element size is larger than the value size. */ \
  515. case MONO_TYPE_U1: \
  516. case MONO_TYPE_U2: \
  517. case MONO_TYPE_U4: \
  518. case MONO_TYPE_U8: \
  519. case MONO_TYPE_CHAR: \
  520. CHECK_WIDENING_CONVERSION(strict_signs ? 1 : 0); \
  521. *(etype *) ea = (etype) u64; \
  522. break; \
  523. /* You can't assign a floating point number to an integer array. */ \
  524. case MONO_TYPE_R4: \
  525. case MONO_TYPE_R8: \
  526. NO_WIDENING_CONVERSION; \
  527. break; \
  528. default: \
  529. INVALID_CAST; \
  530. break; \
  531. } \
  532. }G_STMT_END
  533. #define ASSIGN_REAL(etype) G_STMT_START{\
  534. switch (vt) { \
  535. case MONO_TYPE_R4: \
  536. case MONO_TYPE_R8: \
  537. CHECK_WIDENING_CONVERSION(0); \
  538. *(etype *) ea = (etype) r64; \
  539. break; \
  540. /* All integer values fit into a floating point array, so we don't */ \
  541. /* need to CHECK_WIDENING_CONVERSION here. */ \
  542. case MONO_TYPE_I1: \
  543. case MONO_TYPE_I2: \
  544. case MONO_TYPE_I4: \
  545. case MONO_TYPE_I8: \
  546. *(etype *) ea = (etype) i64; \
  547. break; \
  548. case MONO_TYPE_U1: \
  549. case MONO_TYPE_U2: \
  550. case MONO_TYPE_U4: \
  551. case MONO_TYPE_U8: \
  552. case MONO_TYPE_CHAR: \
  553. *(etype *) ea = (etype) u64; \
  554. break; \
  555. default: \
  556. INVALID_CAST; \
  557. break; \
  558. } \
  559. }G_STMT_END
  560. MONO_ENTER_NO_SAFEPOINTS;
  561. g_assert (!MONO_HANDLE_IS_NULL (value_handle));
  562. g_assert (m_class_is_valuetype (vc));
  563. va = (gpointer*) mono_object_unbox_internal (MONO_HANDLE_RAW (value_handle));
  564. ea = (gpointer*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (arr_handle), esize, pos);
  565. switch (vt) {
  566. case MONO_TYPE_U1:
  567. u64 = *(guint8 *) va;
  568. break;
  569. case MONO_TYPE_U2:
  570. u64 = *(guint16 *) va;
  571. break;
  572. case MONO_TYPE_U4:
  573. u64 = *(guint32 *) va;
  574. break;
  575. case MONO_TYPE_U8:
  576. u64 = *(guint64 *) va;
  577. break;
  578. case MONO_TYPE_I1:
  579. i64 = *(gint8 *) va;
  580. break;
  581. case MONO_TYPE_I2:
  582. i64 = *(gint16 *) va;
  583. break;
  584. case MONO_TYPE_I4:
  585. i64 = *(gint32 *) va;
  586. break;
  587. case MONO_TYPE_I8:
  588. i64 = *(gint64 *) va;
  589. break;
  590. case MONO_TYPE_R4:
  591. r64 = *(gfloat *) va;
  592. break;
  593. case MONO_TYPE_R8:
  594. r64 = *(gdouble *) va;
  595. break;
  596. case MONO_TYPE_CHAR:
  597. u64 = *(guint16 *) va;
  598. break;
  599. case MONO_TYPE_BOOLEAN:
  600. /* Boolean is only compatible with itself. */
  601. switch (et) {
  602. case MONO_TYPE_CHAR:
  603. case MONO_TYPE_U1:
  604. case MONO_TYPE_U2:
  605. case MONO_TYPE_U4:
  606. case MONO_TYPE_U8:
  607. case MONO_TYPE_I1:
  608. case MONO_TYPE_I2:
  609. case MONO_TYPE_I4:
  610. case MONO_TYPE_I8:
  611. case MONO_TYPE_R4:
  612. case MONO_TYPE_R8:
  613. NO_WIDENING_CONVERSION;
  614. break;
  615. default:
  616. INVALID_CAST;
  617. }
  618. break;
  619. default:
  620. break;
  621. }
  622. /* If we can't do a direct copy, let's try a widening conversion. */
  623. if (is_ok (error)) {
  624. switch (et) {
  625. case MONO_TYPE_CHAR:
  626. ASSIGN_UNSIGNED (guint16);
  627. break;
  628. case MONO_TYPE_U1:
  629. ASSIGN_UNSIGNED (guint8);
  630. break;
  631. case MONO_TYPE_U2:
  632. ASSIGN_UNSIGNED (guint16);
  633. break;
  634. case MONO_TYPE_U4:
  635. ASSIGN_UNSIGNED (guint32);
  636. break;
  637. case MONO_TYPE_U8:
  638. ASSIGN_UNSIGNED (guint64);
  639. break;
  640. case MONO_TYPE_I1:
  641. ASSIGN_SIGNED (gint8);
  642. break;
  643. case MONO_TYPE_I2:
  644. ASSIGN_SIGNED (gint16);
  645. break;
  646. case MONO_TYPE_I4:
  647. ASSIGN_SIGNED (gint32);
  648. break;
  649. case MONO_TYPE_I8:
  650. ASSIGN_SIGNED (gint64);
  651. break;
  652. case MONO_TYPE_R4:
  653. ASSIGN_REAL (gfloat);
  654. break;
  655. case MONO_TYPE_R8:
  656. ASSIGN_REAL (gdouble);
  657. break;
  658. default:
  659. INVALID_CAST;
  660. }
  661. }
  662. MONO_EXIT_NO_SAFEPOINTS;
  663. #undef INVALID_CAST
  664. #undef NO_WIDENING_CONVERSION
  665. #undef CHECK_WIDENING_CONVERSION
  666. #undef ASSIGN_UNSIGNED
  667. #undef ASSIGN_SIGNED
  668. #undef ASSIGN_REAL
  669. leave:
  670. return;
  671. }
  672. void
  673. ves_icall_System_Array_SetValue (MonoArrayHandle arr, MonoObjectHandle value,
  674. MonoArrayHandle idxs, MonoError *error)
  675. {
  676. icallarray_print ("%s\n", __func__);
  677. MonoArrayBounds dim;
  678. MonoClass *ac, *ic;
  679. gint32 idx;
  680. gint32 i, pos;
  681. error_init (error);
  682. if (MONO_HANDLE_IS_NULL (idxs)) {
  683. #ifdef ENABLE_NETCORE
  684. mono_error_set_argument_null (error, "indices", "");
  685. #else
  686. mono_error_set_argument_null (error, "idxs", "");
  687. #endif
  688. return;
  689. }
  690. ic = mono_handle_class (idxs);
  691. ac = mono_handle_class (arr);
  692. g_assert (m_class_get_rank (ic) == 1);
  693. if (mono_handle_array_has_bounds (idxs) || MONO_HANDLE_GETVAL (idxs, max_length) != m_class_get_rank (ac)) {
  694. #ifdef ENABLE_NETCORE
  695. mono_error_set_argument (error, NULL, "");
  696. #else
  697. mono_error_set_argument (error, "idxs", "");
  698. #endif
  699. return;
  700. }
  701. if (!mono_handle_array_has_bounds (arr)) {
  702. MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, 0);
  703. if (idx < 0 || idx >= MONO_HANDLE_GETVAL (arr, max_length)) {
  704. mono_error_set_exception_instance (error, mono_get_exception_index_out_of_range ());
  705. return;
  706. }
  707. array_set_value_impl (arr, value, idx, TRUE, TRUE, error);
  708. return;
  709. }
  710. gint32 ac_rank = m_class_get_rank (ac);
  711. for (i = 0; i < ac_rank; i++) {
  712. mono_handle_array_get_bounds_dim (arr, i, &dim);
  713. MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, i);
  714. if ((idx < dim.lower_bound) ||
  715. (idx >= (mono_array_lower_bound_t)dim.length + dim.lower_bound)) {
  716. mono_error_set_exception_instance (error, mono_get_exception_index_out_of_range ());
  717. return;
  718. }
  719. }
  720. MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, 0);
  721. mono_handle_array_get_bounds_dim (arr, 0, &dim);
  722. pos = idx - dim.lower_bound;
  723. for (i = 1; i < ac_rank; i++) {
  724. mono_handle_array_get_bounds_dim (arr, i, &dim);
  725. MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, i);
  726. pos = pos * dim.length + idx - dim.lower_bound;
  727. }
  728. array_set_value_impl (arr, value, pos, TRUE, TRUE, error);
  729. }
  730. #ifdef ENABLE_NETCORE
  731. void
  732. ves_icall_System_Array_InternalCreate (MonoArray *volatile* result, MonoType* type, gint32 rank, gint32* pLengths, gint32* pLowerBounds)
  733. {
  734. ERROR_DECL (error);
  735. MonoClass* klass = mono_class_from_mono_type_internal (type);
  736. if (!mono_class_init_checked (klass, error))
  737. goto exit;
  738. if (m_class_get_byval_arg (m_class_get_element_class (klass))->type == MONO_TYPE_VOID) {
  739. mono_error_set_not_supported (error, "Arrays of System.Void are not supported.");
  740. goto exit;
  741. }
  742. if (type->byref || m_class_is_byreflike (klass)) {
  743. mono_error_set_not_supported (error, NULL);
  744. goto exit;
  745. }
  746. MonoGenericClass *gklass;
  747. gklass = mono_class_try_get_generic_class (klass);
  748. if (is_generic_parameter (type) || mono_class_is_gtd (klass) || (gklass && gklass->context.class_inst->is_open)) {
  749. mono_error_set_not_supported (error, NULL);
  750. goto exit;
  751. }
  752. /* vectors are not the same as one dimensional arrays with non-zero bounds */
  753. gboolean bounded;
  754. bounded = pLowerBounds != NULL && rank == 1 && pLowerBounds [0] != 0;
  755. MonoClass* aklass;
  756. aklass = mono_class_create_bounded_array (klass, rank, bounded);
  757. uintptr_t aklass_rank;
  758. aklass_rank = m_class_get_rank (aklass);
  759. uintptr_t* sizes;
  760. sizes = g_newa (uintptr_t, aklass_rank * 2);
  761. intptr_t* lower_bounds;
  762. lower_bounds = (intptr_t*)(sizes + aklass_rank);
  763. // Copy lengths and lower_bounds from gint32 to [u]intptr_t.
  764. for (uintptr_t i = 0; i < aklass_rank; ++i) {
  765. if (pLowerBounds != NULL) {
  766. lower_bounds [i] = pLowerBounds [i];
  767. if ((gint64) pLowerBounds [i] + (gint64) pLengths [i] > G_MAXINT32) {
  768. mono_error_set_argument_out_of_range (error, NULL, "Length + bound must not exceed Int32.MaxValue.");
  769. goto exit;
  770. }
  771. } else {
  772. lower_bounds [i] = 0;
  773. }
  774. sizes [i] = pLengths [i];
  775. }
  776. *result = mono_array_new_full_checked (mono_domain_get (), aklass, sizes, lower_bounds, error);
  777. exit:
  778. mono_error_set_pending_exception (error);
  779. }
  780. #endif
  781. #ifndef ENABLE_NETCORE
  782. MonoArrayHandle
  783. ves_icall_System_Array_CreateInstanceImpl (MonoReflectionTypeHandle type, MonoArrayHandle lengths, MonoArrayHandle bounds, MonoError *error)
  784. {
  785. // FIXME? fixed could be used for lengths, bounds.
  786. icallarray_print ("%s type:%p length:%p bounds:%p\n", __func__, type, lengths, bounds);
  787. MONO_CHECK_ARG_NULL_HANDLE (type, NULL_HANDLE_ARRAY);
  788. MONO_CHECK_ARG_NULL_HANDLE (lengths, NULL_HANDLE_ARRAY);
  789. MONO_CHECK_ARG (lengths, mono_array_handle_length (lengths) > 0, NULL_HANDLE_ARRAY);
  790. if (!MONO_HANDLE_IS_NULL (bounds))
  791. MONO_CHECK_ARG (bounds, mono_array_handle_length (lengths) == mono_array_handle_length (bounds), NULL_HANDLE_ARRAY);
  792. for (uintptr_t i = 0; i < mono_array_handle_length (lengths); ++i) {
  793. gint32 length = 0;
  794. MONO_HANDLE_ARRAY_GETVAL (length, lengths, gint32, i);
  795. if (length < 0) {
  796. mono_error_set_argument_out_of_range (error, NULL, "MonoArgumentException:NULL");
  797. return NULL_HANDLE_ARRAY;
  798. }
  799. }
  800. MonoClass *klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
  801. if (!mono_class_init_checked (klass, error))
  802. return NULL_HANDLE_ARRAY;
  803. if (m_class_get_byval_arg (m_class_get_element_class (klass))->type == MONO_TYPE_VOID) {
  804. mono_error_set_not_supported (error, "Arrays of System.Void are not supported.");
  805. return NULL_HANDLE_ARRAY;
  806. }
  807. /* vectors are not the same as one dimensional arrays with non-zero bounds */
  808. gboolean bounded = FALSE;
  809. if (!MONO_HANDLE_IS_NULL (bounds) && mono_array_handle_length (bounds) == 1) {
  810. gint32 bound0 = 0;
  811. MONO_HANDLE_ARRAY_GETVAL (bound0, bounds, gint32, 0);
  812. bounded = bound0 != 0;
  813. }
  814. MonoClass * const aklass = mono_class_create_bounded_array (klass, mono_array_handle_length (lengths), bounded);
  815. uintptr_t const aklass_rank = m_class_get_rank (aklass);
  816. uintptr_t * const sizes = g_newa (uintptr_t, aklass_rank);
  817. intptr_t * const lower_bounds = g_newa (intptr_t, aklass_rank);
  818. // Copy lengths and lower_bounds from gint32 to [u]intptr_t.
  819. for (uintptr_t i = 0; i < aklass_rank; ++i) {
  820. MONO_HANDLE_ARRAY_GETVAL (sizes [i], lengths, gint32, i);
  821. if (!MONO_HANDLE_IS_NULL (bounds))
  822. MONO_HANDLE_ARRAY_GETVAL (lower_bounds [i], bounds, gint32, i);
  823. else
  824. lower_bounds [i] = 0;
  825. }
  826. return mono_array_new_full_handle (MONO_HANDLE_DOMAIN (type), aklass, sizes, lower_bounds, error);
  827. }
  828. gint32
  829. ves_icall_System_Array_GetRank (MonoObjectHandle arr, MonoError *error)
  830. {
  831. gint32 const result = m_class_get_rank (mono_handle_class (arr));
  832. icallarray_print ("%s arr:%p res:%d\n", __func__, MONO_HANDLE_RAW (arr), result);
  833. return result;
  834. }
  835. #endif
  836. #ifdef ENABLE_NETCORE
  837. gint32
  838. ves_icall_System_Array_GetCorElementTypeOfElementType (MonoArrayHandle arr, MonoError *error)
  839. {
  840. MonoType *type = mono_type_get_underlying_type (m_class_get_byval_arg (m_class_get_element_class (mono_handle_class (arr))));
  841. return type->type;
  842. }
  843. gint32
  844. ves_icall_System_Array_IsValueOfElementType (MonoArrayHandle arr, MonoObjectHandle obj, MonoError *error)
  845. {
  846. return m_class_get_element_class (mono_handle_class (arr)) == mono_handle_class (obj);
  847. }
  848. #endif
  849. static mono_array_size_t
  850. mono_array_get_length (MonoArrayHandle arr, gint32 dimension, MonoError *error)
  851. {
  852. if (dimension < 0 || dimension >= m_class_get_rank (mono_handle_class (arr))) {
  853. mono_error_set_index_out_of_range (error);
  854. return 0;
  855. }
  856. return MONO_HANDLE_GETVAL (arr, bounds) ? MONO_HANDLE_GETVAL (arr, bounds [dimension].length)
  857. : MONO_HANDLE_GETVAL (arr, max_length);
  858. }
  859. gint32
  860. ves_icall_System_Array_GetLength (MonoArrayHandle arr, gint32 dimension, MonoError *error)
  861. {
  862. icallarray_print ("%s arr:%p dimension:%d\n", __func__, MONO_HANDLE_RAW (arr), (int)dimension);
  863. mono_array_size_t const length = mono_array_get_length (arr, dimension, error);
  864. if (length > G_MAXINT32) {
  865. mono_error_set_overflow (error);
  866. return 0;
  867. }
  868. return (gint32)length;
  869. }
  870. #ifndef ENABLE_NETCORE
  871. gint64
  872. ves_icall_System_Array_GetLongLength (MonoArrayHandle arr, gint32 dimension, MonoError *error)
  873. {
  874. icallarray_print ("%s arr:%p dimension:%d\n", __func__, MONO_HANDLE_RAW (arr), (int)dimension);
  875. return (gint64)mono_array_get_length (arr, dimension, error);
  876. }
  877. #endif
  878. gint32
  879. ves_icall_System_Array_GetLowerBound (MonoArrayHandle arr, gint32 dimension, MonoError *error)
  880. {
  881. icallarray_print ("%s arr:%p dimension:%d\n", __func__, MONO_HANDLE_RAW (arr), (int)dimension);
  882. if (dimension < 0 || dimension >= m_class_get_rank (mono_handle_class (arr))) {
  883. mono_error_set_index_out_of_range (error);
  884. return 0;
  885. }
  886. return MONO_HANDLE_GETVAL (arr, bounds) ? MONO_HANDLE_GETVAL (arr, bounds [dimension].lower_bound)
  887. : 0;
  888. }
  889. #ifndef ENABLE_NETCORE
  890. void
  891. ves_icall_System_Array_ClearInternal (MonoArrayHandle arr, int idx, int length, MonoError *error)
  892. {
  893. icallarray_print ("%s arr:%p idx:%d len:%d\n", __func__, MONO_HANDLE_RAW (arr), (int)idx, (int)length);
  894. int sz = mono_array_element_size (mono_handle_class (arr));
  895. mono_gc_bzero_atomic (mono_array_addr_with_size_fast (MONO_HANDLE_RAW (arr), sz, idx), length * sz);
  896. }
  897. #endif
  898. MonoBoolean
  899. ves_icall_System_Array_FastCopy (MonoArrayHandle source, int source_idx, MonoArrayHandle dest, int dest_idx, int length, MonoError *error)
  900. {
  901. MonoVTable * const src_vtable = MONO_HANDLE_GETVAL (source, obj.vtable);
  902. MonoVTable * const dest_vtable = MONO_HANDLE_GETVAL (dest, obj.vtable);
  903. if (src_vtable->rank != dest_vtable->rank)
  904. return FALSE;
  905. MonoArrayBounds *source_bounds = MONO_HANDLE_GETVAL (source, bounds);
  906. MonoArrayBounds *dest_bounds = MONO_HANDLE_GETVAL (dest, bounds);
  907. for (int i = 0; i < src_vtable->rank; i++) {
  908. if ((source_bounds && source_bounds [i].lower_bound > 0) ||
  909. (dest_bounds && dest_bounds [i].lower_bound > 0))
  910. return FALSE;
  911. }
  912. /* there's no integer overflow since mono_array_length_internal returns an unsigned integer */
  913. if ((dest_idx + length > mono_array_handle_length (dest)) ||
  914. (source_idx + length > mono_array_handle_length (source)))
  915. return FALSE;
  916. MonoClass * const src_class = m_class_get_element_class (src_vtable->klass);
  917. MonoClass * const dest_class = m_class_get_element_class (dest_vtable->klass);
  918. /*
  919. * Handle common cases.
  920. */
  921. /* Case1: object[] -> valuetype[] (ArrayList::ToArray)
  922. We fallback to managed here since we need to typecheck each boxed valuetype before storing them in the dest array.
  923. */
  924. if (src_class == mono_defaults.object_class && m_class_is_valuetype (dest_class))
  925. return FALSE;
  926. /* Check if we're copying a char[] <==> (u)short[] */
  927. if (src_class != dest_class) {
  928. if (m_class_is_valuetype (dest_class) || m_class_is_enumtype (dest_class) ||
  929. m_class_is_valuetype (src_class) || m_class_is_valuetype (src_class))
  930. return FALSE;
  931. /* It's only safe to copy between arrays if we can ensure the source will always have a subtype of the destination. We bail otherwise. */
  932. if (!mono_class_is_subclass_of_internal (src_class, dest_class, FALSE))
  933. return FALSE;
  934. if (m_class_is_native_pointer (src_class) || m_class_is_native_pointer (dest_class))
  935. return FALSE;
  936. }
  937. if (m_class_is_valuetype (dest_class)) {
  938. gsize const element_size = mono_array_element_size (MONO_HANDLE_GETVAL (source, obj.vtable->klass));
  939. MONO_ENTER_NO_SAFEPOINTS; // gchandle would also work here, is slow, breaks profiler tests.
  940. gconstpointer const source_addr =
  941. mono_array_addr_with_size_fast (MONO_HANDLE_RAW (source), element_size, source_idx);
  942. if (m_class_has_references (dest_class)) {
  943. mono_value_copy_array_handle (dest, dest_idx, source_addr, length);
  944. } else {
  945. gpointer const dest_addr =
  946. mono_array_addr_with_size_fast (MONO_HANDLE_RAW (dest), element_size, dest_idx);
  947. mono_gc_memmove_atomic (dest_addr, source_addr, element_size * length);
  948. }
  949. MONO_EXIT_NO_SAFEPOINTS;
  950. } else {
  951. mono_array_handle_memcpy_refs (dest, dest_idx, source, source_idx, length);
  952. }
  953. return TRUE;
  954. }
  955. void
  956. ves_icall_System_Array_GetGenericValue_icall (MonoArray **arr, guint32 pos, gpointer value)
  957. {
  958. icallarray_print ("%s arr:%p pos:%u value:%p\n", __func__, *arr, pos, value);
  959. MONO_REQ_GC_UNSAFE_MODE; // because of gpointer value
  960. MonoClass * const ac = mono_object_class (*arr);
  961. gsize const esize = mono_array_element_size (ac);
  962. gconstpointer * const ea = (gconstpointer*)((char*)(*arr)->vector + (pos * esize));
  963. mono_gc_memmove_atomic (value, ea, esize);
  964. }
  965. void
  966. ves_icall_System_Array_SetGenericValue_icall (MonoArray **arr, guint32 pos, gpointer value)
  967. {
  968. icallarray_print ("%s arr:%p pos:%u value:%p\n", __func__, *arr, pos, value);
  969. MONO_REQ_GC_UNSAFE_MODE; // because of gpointer value
  970. MonoClass * const ac = mono_object_class (*arr);
  971. MonoClass * const ec = m_class_get_element_class (ac);
  972. gsize const esize = mono_array_element_size (ac);
  973. gpointer * const ea = (gpointer*)((char*)(*arr)->vector + (pos * esize));
  974. if (MONO_TYPE_IS_REFERENCE (m_class_get_byval_arg (ec))) {
  975. g_assert (esize == sizeof (gpointer));
  976. mono_gc_wbarrier_generic_store_internal (ea, *(MonoObject **)value);
  977. } else {
  978. g_assert (m_class_is_inited (ec));
  979. g_assert (esize == mono_class_value_size (ec, NULL));
  980. if (m_class_has_references (ec))
  981. mono_gc_wbarrier_value_copy_internal (ea, value, 1, ec);
  982. else
  983. mono_gc_memmove_atomic (ea, value, esize);
  984. }
  985. }
  986. void
  987. #if ENABLE_NETCORE
  988. ves_icall_System_Runtime_RuntimeImports_Memmove (guint8 *destination, guint8 *source, size_t byte_count)
  989. #else
  990. ves_icall_System_Runtime_RuntimeImports_Memmove (guint8 *destination, guint8 *source, guint byte_count)
  991. #endif
  992. {
  993. mono_gc_memmove_atomic (destination, source, byte_count);
  994. }
  995. #if ENABLE_NETCORE
  996. void
  997. ves_icall_System_Buffer_BulkMoveWithWriteBarrier (guint8 *destination, guint8 *source, size_t len, MonoType *type)
  998. {
  999. if (MONO_TYPE_IS_REFERENCE (type))
  1000. mono_gc_wbarrier_arrayref_copy_internal (destination, source, (guint)len);
  1001. else
  1002. mono_gc_wbarrier_value_copy_internal (destination, source, (guint)len, mono_class_from_mono_type_internal (type));
  1003. }
  1004. #else
  1005. void
  1006. ves_icall_System_Runtime_RuntimeImports_Memmove_wbarrier (guint8 *destination, guint8 *source, guint len, MonoType *type)
  1007. {
  1008. if (MONO_TYPE_IS_REFERENCE (type))
  1009. mono_gc_wbarrier_arrayref_copy_internal (destination, source, len);
  1010. else
  1011. mono_gc_wbarrier_value_copy_internal (destination, source, len, mono_class_from_mono_type_internal (type));
  1012. }
  1013. #endif
  1014. void
  1015. #if ENABLE_NETCORE
  1016. ves_icall_System_Runtime_RuntimeImports_ZeroMemory (guint8 *p, size_t byte_length)
  1017. #else
  1018. ves_icall_System_Runtime_RuntimeImports_ZeroMemory (guint8 *p, guint byte_length)
  1019. #endif
  1020. {
  1021. memset (p, 0, byte_length);
  1022. }
  1023. void
  1024. ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray (MonoArrayHandle array, MonoClassField *field_handle, MonoError *error)
  1025. {
  1026. MonoClass *klass = mono_handle_class (array);
  1027. guint32 size = mono_array_element_size (klass);
  1028. MonoType *type = mono_type_get_underlying_type (m_class_get_byval_arg (m_class_get_element_class (klass)));
  1029. int align;
  1030. const char *field_data;
  1031. if (MONO_TYPE_IS_REFERENCE (type) || type->type == MONO_TYPE_VALUETYPE) {
  1032. mono_error_set_argument (error, "array", "Cannot initialize array of non-primitive type");
  1033. return;
  1034. }
  1035. MonoType *field_type = mono_field_get_type_checked (field_handle, error);
  1036. if (!field_type)
  1037. return;
  1038. if (!(field_type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA)) {
  1039. mono_error_set_argument_format (error, "field_handle", "Field '%s' doesn't have an RVA", mono_field_get_name (field_handle));
  1040. return;
  1041. }
  1042. size *= MONO_HANDLE_GETVAL(array, max_length);
  1043. field_data = mono_field_get_data (field_handle);
  1044. if (size > mono_type_size (field_handle->type, &align)) {
  1045. mono_error_set_argument (error, "field_handle", "Field not large enough to fill array");
  1046. return;
  1047. }
  1048. #if G_BYTE_ORDER != G_LITTLE_ENDIAN
  1049. #define SWAP(n) { \
  1050. guint ## n *data = (guint ## n *) mono_array_addr_internal (MONO_HANDLE_RAW(array), char, 0); \
  1051. guint ## n *src = (guint ## n *) field_data; \
  1052. int i, \
  1053. nEnt = (size / sizeof(guint ## n)); \
  1054. \
  1055. for (i = 0; i < nEnt; i++) { \
  1056. data[i] = read ## n (&src[i]); \
  1057. } \
  1058. }
  1059. /* printf ("Initialize array with elements of %s type\n", klass->element_class->name); */
  1060. switch (type->type) {
  1061. case MONO_TYPE_CHAR:
  1062. case MONO_TYPE_I2:
  1063. case MONO_TYPE_U2:
  1064. SWAP (16);
  1065. break;
  1066. case MONO_TYPE_I4:
  1067. case MONO_TYPE_U4:
  1068. case MONO_TYPE_R4:
  1069. SWAP (32);
  1070. break;
  1071. case MONO_TYPE_I8:
  1072. case MONO_TYPE_U8:
  1073. case MONO_TYPE_R8:
  1074. SWAP (64);
  1075. break;
  1076. default:
  1077. memcpy (mono_array_addr_internal (MONO_HANDLE_RAW(array), char, 0), field_data, size);
  1078. break;
  1079. }
  1080. #else
  1081. memcpy (mono_array_addr_internal (MONO_HANDLE_RAW(array), char, 0), field_data, size);
  1082. #endif
  1083. }
  1084. gint
  1085. ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetOffsetToStringData (void)
  1086. {
  1087. return offsetof (MonoString, chars);
  1088. }
  1089. MonoObjectHandle
  1090. ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetObjectValue (MonoObjectHandle obj, MonoError *error)
  1091. {
  1092. if (MONO_HANDLE_IS_NULL (obj) || !m_class_is_valuetype (mono_handle_class (obj)))
  1093. return obj;
  1094. return mono_object_clone_handle (obj, error);
  1095. }
  1096. void
  1097. ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor (MonoType *handle, MonoError *error)
  1098. {
  1099. MonoClass *klass;
  1100. MonoVTable *vtable;
  1101. MONO_CHECK_ARG_NULL (handle,);
  1102. klass = mono_class_from_mono_type_internal (handle);
  1103. MONO_CHECK_ARG (handle, klass,);
  1104. if (mono_class_is_gtd (klass))
  1105. return;
  1106. vtable = mono_class_vtable_checked (mono_domain_get (), klass, error);
  1107. return_if_nok (error);
  1108. /* This will call the type constructor */
  1109. mono_runtime_class_init_full (vtable, error);
  1110. }
  1111. void
  1112. ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunModuleConstructor (MonoImage *image, MonoError *error)
  1113. {
  1114. mono_image_check_for_module_cctor (image);
  1115. if (!image->has_module_cctor)
  1116. return;
  1117. MonoClass *module_klass = mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF | 1, error);
  1118. return_if_nok (error);
  1119. MonoVTable * vtable = mono_class_vtable_checked (mono_domain_get (), module_klass, error);
  1120. return_if_nok (error);
  1121. mono_runtime_class_init_full (vtable, error);
  1122. }
  1123. #ifdef ENABLE_NETCORE
  1124. MonoBoolean
  1125. ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_SufficientExecutionStack (void)
  1126. {
  1127. MonoThreadInfo *thread = mono_thread_info_current ();
  1128. void *current = &thread;
  1129. // Stack upper/lower bound should have been calculated and set as part of register_thread.
  1130. // If not, we are optimistic and assume there is enough room.
  1131. if (!thread->stack_start_limit || !thread->stack_end)
  1132. return TRUE;
  1133. // Stack start limit is stack lower bound. Make sure there is enough room left.
  1134. void *limit = ((uint8_t *)thread->stack_start_limit) + ALIGN_TO (MONO_STACK_OVERFLOW_GUARD_SIZE + MONO_MIN_EXECUTION_STACK_SIZE, ((gssize)mono_pagesize ()));
  1135. if (current < limit)
  1136. return FALSE;
  1137. return TRUE;
  1138. }
  1139. #else
  1140. MonoBoolean
  1141. ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_SufficientExecutionStack (void)
  1142. {
  1143. return mono_thread_has_sufficient_execution_stack ();
  1144. }
  1145. #endif
  1146. #ifdef ENABLE_NETCORE
  1147. MonoObjectHandle
  1148. ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetUninitializedObjectInternal (MonoType *handle, MonoError *error)
  1149. {
  1150. MonoClass *klass;
  1151. MonoVTable *vtable;
  1152. g_assert (handle);
  1153. klass = mono_class_from_mono_type_internal (handle);
  1154. if (m_class_is_string (klass)) {
  1155. mono_error_set_argument (error, NULL, NULL);
  1156. return NULL_HANDLE;
  1157. }
  1158. if (mono_class_is_array (klass) || mono_class_is_pointer (klass) || handle->byref) {
  1159. mono_error_set_argument (error, NULL, NULL);
  1160. return NULL_HANDLE;
  1161. }
  1162. if (MONO_TYPE_IS_VOID (handle)) {
  1163. mono_error_set_argument (error, NULL, NULL);
  1164. return NULL_HANDLE;
  1165. }
  1166. if (m_class_is_abstract (klass) || m_class_is_interface (klass) || m_class_is_gtd (klass)) {
  1167. mono_error_set_member_access (error, NULL, NULL);
  1168. return NULL_HANDLE;
  1169. }
  1170. if (m_class_is_byreflike (klass)) {
  1171. mono_error_set_not_supported (error, NULL, NULL);
  1172. return NULL_HANDLE;
  1173. }
  1174. if (!mono_class_is_before_field_init (klass)) {
  1175. vtable = mono_class_vtable_checked (mono_domain_get (), klass, error);
  1176. return_val_if_nok (error, NULL_HANDLE);
  1177. mono_runtime_class_init_full (vtable, error);
  1178. return_val_if_nok (error, NULL_HANDLE);
  1179. }
  1180. if (m_class_is_nullable (klass))
  1181. return mono_object_new_handle (mono_domain_get (), m_class_get_nullable_elem_class (klass), error);
  1182. else
  1183. return mono_object_new_handle (mono_domain_get (), klass, error);
  1184. }
  1185. void
  1186. ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_PrepareMethod (MonoMethod *method, gpointer inst_types, int n_inst_types, MonoError *error)
  1187. {
  1188. if (method->flags & METHOD_ATTRIBUTE_ABSTRACT) {
  1189. mono_error_set_argument (error, NULL, NULL);
  1190. return;
  1191. }
  1192. MonoGenericContainer *container = NULL;
  1193. if (method->is_generic)
  1194. container = mono_method_get_generic_container (method);
  1195. else if (m_class_is_gtd (method->klass))
  1196. container = mono_class_get_generic_container (method->klass);
  1197. if (container) {
  1198. int nparams = container->type_argc + (container->parent ? container->parent->type_argc : 0);
  1199. if (nparams != n_inst_types) {
  1200. mono_error_set_argument (error, NULL, NULL);
  1201. return;
  1202. }
  1203. }
  1204. // FIXME: Implement
  1205. }
  1206. #endif
  1207. MonoObjectHandle
  1208. ves_icall_System_Object_MemberwiseClone (MonoObjectHandle this_obj, MonoError *error)
  1209. {
  1210. return mono_object_clone_handle (this_obj, error);
  1211. }
  1212. gint32
  1213. ves_icall_System_ValueType_InternalGetHashCode (MonoObjectHandle this_obj, MonoArrayHandleOut fields, MonoError *error)
  1214. {
  1215. MonoClass *klass;
  1216. MonoClassField **unhandled = NULL;
  1217. int count = 0;
  1218. gint32 result = (int)(gsize)mono_defaults.int32_class;
  1219. MonoClassField* field;
  1220. gpointer iter;
  1221. klass = mono_handle_class (this_obj);
  1222. if (mono_class_num_fields (klass) == 0)
  1223. return result;
  1224. /*
  1225. * Compute the starting value of the hashcode for fields of primitive
  1226. * types, and return the remaining fields in an array to the managed side.
  1227. * This way, we can avoid costly reflection operations in managed code.
  1228. */
  1229. iter = NULL;
  1230. while ((field = mono_class_get_fields_internal (klass, &iter))) {
  1231. if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
  1232. continue;
  1233. if (mono_field_is_deleted (field))
  1234. continue;
  1235. gpointer addr = (guint8*)MONO_HANDLE_RAW (this_obj) + field->offset;
  1236. /* FIXME: Add more types */
  1237. switch (field->type->type) {
  1238. case MONO_TYPE_I4:
  1239. result ^= *(gint32*)addr;
  1240. break;
  1241. case MONO_TYPE_PTR:
  1242. result ^= mono_aligned_addr_hash (*(gpointer*)addr);
  1243. break;
  1244. case MONO_TYPE_STRING: {
  1245. MonoString *s;
  1246. s = *(MonoString**)addr;
  1247. if (s != NULL)
  1248. result ^= mono_string_hash_internal (s);
  1249. break;
  1250. }
  1251. default:
  1252. if (!unhandled)
  1253. unhandled = g_newa (MonoClassField*, mono_class_num_fields (klass));
  1254. unhandled [count ++] = field;
  1255. }
  1256. }
  1257. if (unhandled) {
  1258. MonoArrayHandle fields_arr = mono_array_new_handle (mono_domain_get (), mono_defaults.object_class, count, error);
  1259. return_val_if_nok (error, 0);
  1260. MONO_HANDLE_ASSIGN (fields, fields_arr);
  1261. MonoObjectHandle h = MONO_HANDLE_NEW (MonoObject, NULL);
  1262. for (int i = 0; i < count; ++i) {
  1263. MonoObject *o = mono_field_get_value_object_checked (mono_handle_domain (this_obj), unhandled [i], MONO_HANDLE_RAW (this_obj), error);
  1264. return_val_if_nok (error, 0);
  1265. MONO_HANDLE_ASSIGN_RAW (h, o);
  1266. mono_array_handle_setref (fields_arr, i, h);
  1267. }
  1268. } else {
  1269. MONO_HANDLE_ASSIGN (fields, NULL_HANDLE);
  1270. }
  1271. return result;
  1272. }
  1273. MonoBoolean
  1274. ves_icall_System_ValueType_Equals (MonoObjectHandle this_obj, MonoObjectHandle that, MonoArrayHandleOut fields, MonoError *error)
  1275. {
  1276. MonoClass *klass;
  1277. MonoClassField **unhandled = NULL;
  1278. MonoClassField* field;
  1279. gpointer iter;
  1280. int count = 0;
  1281. MONO_CHECK_ARG_NULL_HANDLE (that, FALSE);
  1282. MONO_HANDLE_ASSIGN (fields, NULL_HANDLE);
  1283. if (mono_handle_vtable (this_obj) != mono_handle_vtable (that))
  1284. return FALSE;
  1285. klass = mono_handle_class (this_obj);
  1286. if (m_class_is_enumtype (klass) && mono_class_enum_basetype_internal (klass) && mono_class_enum_basetype_internal (klass)->type == MONO_TYPE_I4)
  1287. return *(gint32*)mono_handle_get_data_unsafe (this_obj) == *(gint32*)mono_handle_get_data_unsafe (that);
  1288. /*
  1289. * Do the comparison for fields of primitive type and return a result if
  1290. * possible. Otherwise, return the remaining fields in an array to the
  1291. * managed side. This way, we can avoid costly reflection operations in
  1292. * managed code.
  1293. */
  1294. iter = NULL;
  1295. while ((field = mono_class_get_fields_internal (klass, &iter))) {
  1296. if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
  1297. continue;
  1298. if (mono_field_is_deleted (field))
  1299. continue;
  1300. guint8 *this_field = (guint8 *)MONO_HANDLE_RAW (this_obj) + field->offset;
  1301. guint8 *that_field = (guint8 *)MONO_HANDLE_RAW (that) + field->offset;
  1302. #define UNALIGNED_COMPARE(type) \
  1303. do { \
  1304. type left, right; \
  1305. memcpy (&left, this_field, sizeof (type)); \
  1306. memcpy (&right, that_field, sizeof (type)); \
  1307. if (left != right) \
  1308. return FALSE; \
  1309. } while (0)
  1310. /* FIXME: Add more types */
  1311. switch (field->type->type) {
  1312. case MONO_TYPE_U1:
  1313. case MONO_TYPE_I1:
  1314. case MONO_TYPE_BOOLEAN:
  1315. if (*this_field != *that_field)
  1316. return FALSE;
  1317. break;
  1318. case MONO_TYPE_U2:
  1319. case MONO_TYPE_I2:
  1320. case MONO_TYPE_CHAR:
  1321. #ifdef NO_UNALIGNED_ACCESS
  1322. if (G_UNLIKELY ((intptr_t) this_field & 1 || (intptr_t) that_field & 1))
  1323. UNALIGNED_COMPARE (gint16);
  1324. else
  1325. #endif
  1326. if (*(gint16 *) this_field != *(gint16 *) that_field)
  1327. return FALSE;
  1328. break;
  1329. case MONO_TYPE_U4:
  1330. case MONO_TYPE_I4:
  1331. #ifdef NO_UNALIGNED_ACCESS
  1332. if (G_UNLIKELY ((intptr_t) this_field & 3 || (intptr_t) that_field & 3))
  1333. UNALIGNED_COMPARE (gint32);
  1334. else
  1335. #endif
  1336. if (*(gint32 *) this_field != *(gint32 *) that_field)
  1337. return FALSE;
  1338. break;
  1339. case MONO_TYPE_U8:
  1340. case MONO_TYPE_I8:
  1341. #ifdef NO_UNALIGNED_ACCESS
  1342. if (G_UNLIKELY ((intptr_t) this_field & 7 || (intptr_t) that_field & 7))
  1343. UNALIGNED_COMPARE (gint64);
  1344. else
  1345. #endif
  1346. if (*(gint64 *) this_field != *(gint64 *) that_field)
  1347. return FALSE;
  1348. break;
  1349. case MONO_TYPE_R4: {
  1350. float d1, d2;
  1351. #ifdef NO_UNALIGNED_ACCESS
  1352. memcpy (&d1, this_field, sizeof (float));
  1353. memcpy (&d2, that_field, sizeof (float));
  1354. #else
  1355. d1 = *(float *) this_field;
  1356. d2 = *(float *) that_field;
  1357. #endif
  1358. if (d1 != d2 && !(mono_isnan (d1) && mono_isnan (d2)))
  1359. return FALSE;
  1360. break;
  1361. }
  1362. case MONO_TYPE_R8: {
  1363. double d1, d2;
  1364. #ifdef NO_UNALIGNED_ACCESS
  1365. memcpy (&d1, this_field, sizeof (double));
  1366. memcpy (&d2, that_field, sizeof (double));
  1367. #else
  1368. d1 = *(double *) this_field;
  1369. d2 = *(double *) that_field;
  1370. #endif
  1371. if (d1 != d2 && !(mono_isnan (d1) && mono_isnan (d2)))
  1372. return FALSE;
  1373. break;
  1374. }
  1375. case MONO_TYPE_PTR:
  1376. #ifdef NO_UNALIGNED_ACCESS
  1377. if (G_UNLIKELY ((intptr_t) this_field & 7 || (intptr_t) that_field & 7))
  1378. UNALIGNED_COMPARE (gpointer);
  1379. else
  1380. #endif
  1381. if (*(gpointer *) this_field != *(gpointer *) that_field)
  1382. return FALSE;
  1383. break;
  1384. case MONO_TYPE_STRING: {
  1385. MonoString *s1, *s2;
  1386. guint32 s1len, s2len;
  1387. s1 = *(MonoString**)this_field;
  1388. s2 = *(MonoString**)that_field;
  1389. if (s1 == s2)
  1390. break;
  1391. if ((s1 == NULL) || (s2 == NULL))
  1392. return FALSE;
  1393. s1len = mono_string_length_internal (s1);
  1394. s2len = mono_string_length_internal (s2);
  1395. if (s1len != s2len)
  1396. return FALSE;
  1397. if (memcmp (mono_string_chars_internal (s1), mono_string_chars_internal (s2), s1len * sizeof (gunichar2)) != 0)
  1398. return FALSE;
  1399. break;
  1400. }
  1401. default:
  1402. if (!unhandled)
  1403. unhandled = g_newa (MonoClassField*, mono_class_num_fields (klass));
  1404. unhandled [count ++] = field;
  1405. }
  1406. #undef UNALIGNED_COMPARE
  1407. if (m_class_is_enumtype (klass))
  1408. /* enums only have one non-static field */
  1409. break;
  1410. }
  1411. if (unhandled) {
  1412. MonoArrayHandle fields_arr = mono_array_new_handle (mono_domain_get (), mono_defaults.object_class, count * 2, error);
  1413. return_val_if_nok (error, 0);
  1414. MONO_HANDLE_ASSIGN (fields, fields_arr);
  1415. MonoObjectHandle h = MONO_HANDLE_NEW (MonoObject, NULL);
  1416. for (int i = 0; i < count; ++i) {
  1417. MonoObject *o = mono_field_get_value_object_checked (mono_handle_domain (this_obj), unhandled [i], MONO_HANDLE_RAW (this_obj), error);
  1418. return_val_if_nok (error, FALSE);
  1419. MONO_HANDLE_ASSIGN_RAW (h, o);
  1420. mono_array_handle_setref (fields_arr, i * 2, h);
  1421. o = mono_field_get_value_object_checked (mono_handle_domain (this_obj), unhandled [i], MONO_HANDLE_RAW (that), error);
  1422. return_val_if_nok (error, FALSE);
  1423. MONO_HANDLE_ASSIGN_RAW (h, o);
  1424. mono_array_handle_setref (fields_arr, (i * 2) + 1, h);
  1425. }
  1426. return FALSE;
  1427. } else {
  1428. return TRUE;
  1429. }
  1430. }
  1431. #ifndef ENABLE_NETCORE
  1432. MonoReflectionTypeHandle
  1433. ves_icall_System_Object_GetType (MonoObjectHandle obj, MonoError *error)
  1434. {
  1435. MonoDomain *domain = MONO_HANDLE_DOMAIN (obj);
  1436. MonoClass *klass = mono_handle_class (obj);
  1437. #ifndef DISABLE_REMOTING
  1438. if (mono_class_is_transparent_proxy (klass)) {
  1439. MonoTransparentProxyHandle proxy_obj = MONO_HANDLE_CAST (MonoTransparentProxy, obj);
  1440. MonoRemoteClass *remote_class = MONO_HANDLE_GETVAL (proxy_obj, remote_class);
  1441. /* If it's a transparent proxy for an interface, return the
  1442. * interface type, not the unhelpful proxy_class class (which
  1443. * is just MarshalByRefObject). */
  1444. MonoType *proxy_type =
  1445. mono_remote_class_is_interface_proxy (remote_class) ?
  1446. m_class_get_byval_arg (remote_class->interfaces[0]) :
  1447. m_class_get_byval_arg (remote_class->proxy_class);
  1448. return mono_type_get_object_handle (domain, proxy_type, error);
  1449. } else
  1450. #endif
  1451. return mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
  1452. }
  1453. #endif
  1454. static gboolean
  1455. get_executing (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
  1456. {
  1457. MonoMethod **dest = (MonoMethod **)data;
  1458. /* skip unmanaged frames */
  1459. if (!managed)
  1460. return FALSE;
  1461. if (!(*dest)) {
  1462. if (!strcmp (m_class_get_name_space (m->klass), "System.Reflection"))
  1463. return FALSE;
  1464. *dest = m;
  1465. return TRUE;
  1466. }
  1467. return FALSE;
  1468. }
  1469. static gboolean
  1470. in_corlib_name_space (MonoClass *klass, const char *name_space)
  1471. {
  1472. return m_class_get_image (klass) == mono_defaults.corlib &&
  1473. !strcmp (m_class_get_name_space (klass), name_space);
  1474. }
  1475. static gboolean
  1476. get_caller_no_reflection (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
  1477. {
  1478. MonoMethod **dest = (MonoMethod **)data;
  1479. /* skip unmanaged frames */
  1480. if (!managed)
  1481. return FALSE;
  1482. if (m->wrapper_type != MONO_WRAPPER_NONE)
  1483. return FALSE;
  1484. if (m == *dest) {
  1485. *dest = NULL;
  1486. return FALSE;
  1487. }
  1488. if (in_corlib_name_space (m->klass, "System.Reflection"))
  1489. return FALSE;
  1490. if (!(*dest)) {
  1491. *dest = m;
  1492. return TRUE;
  1493. }
  1494. return FALSE;
  1495. }
  1496. static gboolean
  1497. get_caller_no_system_or_reflection (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
  1498. {
  1499. MonoMethod **dest = (MonoMethod **)data;
  1500. /* skip unmanaged frames */
  1501. if (!managed)
  1502. return FALSE;
  1503. if (m->wrapper_type != MONO_WRAPPER_NONE)
  1504. return FALSE;
  1505. if (m == *dest) {
  1506. *dest = NULL;
  1507. return FALSE;
  1508. }
  1509. if (in_corlib_name_space (m->klass, "System.Reflection") || in_corlib_name_space (m->klass, "System"))
  1510. return FALSE;
  1511. if (!(*dest)) {
  1512. *dest = m;
  1513. return TRUE;
  1514. }
  1515. return FALSE;
  1516. }
  1517. /**
  1518. * mono_runtime_get_caller_no_system_or_reflection:
  1519. *
  1520. * Walk the stack of the current thread and find the first managed method that
  1521. * is not in the mscorlib System or System.Reflection namespace. This skips
  1522. * unmanaged callers and wrapper methods.
  1523. *
  1524. * \returns a pointer to the \c MonoMethod or NULL if we walked past all the
  1525. * callers.
  1526. */
  1527. MonoMethod*
  1528. mono_runtime_get_caller_no_system_or_reflection (void)
  1529. {
  1530. MonoMethod *dest = NULL;
  1531. mono_stack_walk_no_il (get_caller_no_system_or_reflection, &dest);
  1532. return dest;
  1533. }
  1534. /*
  1535. * mono_runtime_get_caller_from_stack_mark:
  1536. *
  1537. * Walk the stack and return the assembly of the method referenced
  1538. * by the stack mark STACK_MARK.
  1539. */
  1540. MonoAssembly*
  1541. mono_runtime_get_caller_from_stack_mark (MonoStackCrawlMark *stack_mark)
  1542. {
  1543. // FIXME: Use the stack mark
  1544. MonoMethod *dest = NULL;
  1545. mono_stack_walk_no_il (get_caller_no_system_or_reflection, &dest);
  1546. if (dest)
  1547. return m_class_get_image (dest->klass)->assembly;
  1548. else
  1549. return NULL;
  1550. }
  1551. static MonoReflectionTypeHandle
  1552. type_from_parsed_name (MonoTypeNameParse *info, MonoStackCrawlMark *stack_mark, MonoBoolean ignoreCase, MonoAssembly **caller_assembly, MonoError *error)
  1553. {
  1554. MonoMethod *m;
  1555. MonoType *type = NULL;
  1556. MonoAssembly *assembly = NULL;
  1557. gboolean type_resolve = FALSE;
  1558. MonoImage *rootimage = NULL;
  1559. MonoAssemblyLoadContext *alc = mono_domain_ambient_alc (mono_domain_get ());
  1560. error_init (error);
  1561. /*
  1562. * We must compute the calling assembly as type loading must happen under a metadata context.
  1563. * For example. The main assembly is a.exe and Type.GetType is called from dir/b.dll. Without
  1564. * the metadata context (basedir currently) set to dir/b.dll we won't be able to load a dir/c.dll.
  1565. */
  1566. m = mono_method_get_last_managed ();
  1567. if (m && m_class_get_image (m->klass) != mono_defaults.corlib) {
  1568. /* Happens with inlining */
  1569. assembly = m_class_get_image (m->klass)->assembly;
  1570. } else {
  1571. assembly = mono_runtime_get_caller_from_stack_mark (stack_mark);
  1572. }
  1573. if (assembly) {
  1574. type_resolve = TRUE;
  1575. rootimage = assembly->image;
  1576. } else {
  1577. // FIXME: once wasm can use stack marks, consider turning all this into an assert
  1578. g_warning (G_STRLOC);
  1579. }
  1580. *caller_assembly = assembly;
  1581. if (info->assembly.name) {
  1582. MonoAssemblyByNameRequest req;
  1583. mono_assembly_request_prepare_byname (&req, MONO_ASMCTX_DEFAULT, alc);
  1584. req.requesting_assembly = assembly;
  1585. req.basedir = assembly ? assembly->basedir : NULL;
  1586. assembly = mono_assembly_request_byname (&info->assembly, &req, NULL);
  1587. }
  1588. if (assembly) {
  1589. /* When loading from the current assembly, AppDomain.TypeResolve will not be called yet */
  1590. type = mono_reflection_get_type_checked (alc, rootimage, assembly->image, info, ignoreCase, TRUE, &type_resolve, error);
  1591. goto_if_nok (error, fail);
  1592. }
  1593. // XXXX - aleksey -
  1594. // Say we're looking for System.Generic.Dict<int, Local>
  1595. // we FAIL the get type above, because S.G.Dict isn't in assembly->image. So we drop down here.
  1596. // but then we FAIL AGAIN because now we pass null as the image and the rootimage and everything
  1597. // is messed up when we go to construct the Local as the type arg...
  1598. //
  1599. // By contrast, if we started with Mine<System.Generic.Dict<int, Local>> we'd go in with assembly->image
  1600. // as the root and then even the detour into generics would still not cause issues when we went to load Local.
  1601. if (!info->assembly.name && !type) {
  1602. /* try mscorlib */
  1603. type = mono_reflection_get_type_checked (alc, rootimage, NULL, info, ignoreCase, TRUE, &type_resolve, error);
  1604. goto_if_nok (error, fail);
  1605. }
  1606. if (assembly && !type && type_resolve) {
  1607. type_resolve = FALSE; /* This will invoke TypeResolve if not done in the first 'if' */
  1608. type = mono_reflection_get_type_checked (alc, rootimage, assembly->image, info, ignoreCase, TRUE, &type_resolve, error);
  1609. goto_if_nok (error, fail);
  1610. }
  1611. if (!type)
  1612. goto fail;
  1613. return mono_type_get_object_handle (mono_domain_get (), type, error);
  1614. fail:
  1615. return MONO_HANDLE_NEW (MonoReflectionType, NULL);
  1616. }
  1617. MonoReflectionTypeHandle
  1618. ves_icall_System_RuntimeTypeHandle_internal_from_name (MonoStringHandle name,
  1619. MonoStackCrawlMark *stack_mark,
  1620. MonoReflectionAssemblyHandle callerAssembly,
  1621. MonoBoolean throwOnError,
  1622. MonoBoolean ignoreCase,
  1623. MonoBoolean reflectionOnly,
  1624. MonoError *error)
  1625. {
  1626. MonoTypeNameParse info;
  1627. gboolean free_info = FALSE;
  1628. MonoAssembly *caller_assembly;
  1629. MonoReflectionTypeHandle type = MONO_HANDLE_NEW (MonoReflectionType, NULL);
  1630. /* The callerAssembly argument is unused for now */
  1631. char *str = mono_string_handle_to_utf8 (name, error);
  1632. goto_if_nok (error, leave);
  1633. free_info = TRUE;
  1634. if (!mono_reflection_parse_type_checked (str, &info, error))
  1635. goto leave;
  1636. /* mono_reflection_parse_type() mangles the string */
  1637. MONO_HANDLE_ASSIGN (type, type_from_parsed_name (&info, (MonoStackCrawlMark*)stack_mark, ignoreCase, &caller_assembly, error));
  1638. goto_if_nok (error, leave);
  1639. if (MONO_HANDLE_IS_NULL (type)) {
  1640. if (throwOnError) {
  1641. char *tname = info.name_space ? g_strdup_printf ("%s.%s", info.name_space, info.name) : g_strdup (info.name);
  1642. char *aname;
  1643. if (info.assembly.name)
  1644. aname = mono_stringify_assembly_name (&info.assembly);
  1645. else if (caller_assembly)
  1646. aname = mono_stringify_assembly_name (mono_assembly_get_name_internal (caller_assembly));
  1647. else
  1648. aname = g_strdup ("");
  1649. mono_error_set_type_load_name (error, tname, aname, "");
  1650. }
  1651. goto leave;
  1652. }
  1653. leave:
  1654. if (free_info)
  1655. mono_reflection_free_type_info (&info);
  1656. g_free (str);
  1657. if (!is_ok (error)) {
  1658. if (!throwOnError) {
  1659. mono_error_cleanup (error);
  1660. error_init (error);
  1661. }
  1662. return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  1663. } else
  1664. return type;
  1665. }
  1666. MonoReflectionTypeHandle
  1667. ves_icall_System_Type_internal_from_handle (MonoType *handle, MonoError *error)
  1668. {
  1669. MonoDomain *domain = mono_domain_get ();
  1670. return mono_type_get_object_handle (domain, handle, error);
  1671. }
  1672. MonoType*
  1673. ves_icall_Mono_RuntimeClassHandle_GetTypeFromClass (MonoClass *klass)
  1674. {
  1675. return m_class_get_byval_arg (klass);
  1676. }
  1677. void
  1678. ves_icall_Mono_RuntimeGPtrArrayHandle_GPtrArrayFree (GPtrArray *ptr_array)
  1679. {
  1680. g_ptr_array_free (ptr_array, TRUE);
  1681. }
  1682. void
  1683. ves_icall_Mono_SafeStringMarshal_GFree (void *c_str)
  1684. {
  1685. g_free (c_str);
  1686. }
  1687. char*
  1688. ves_icall_Mono_SafeStringMarshal_StringToUtf8 (MonoString *volatile* s)
  1689. {
  1690. ERROR_DECL (error);
  1691. char *result = mono_string_to_utf8_checked_internal (*s, error);
  1692. mono_error_set_pending_exception (error);
  1693. return result;
  1694. }
  1695. /* System.TypeCode */
  1696. typedef enum {
  1697. TYPECODE_EMPTY,
  1698. TYPECODE_OBJECT,
  1699. TYPECODE_DBNULL,
  1700. TYPECODE_BOOLEAN,
  1701. TYPECODE_CHAR,
  1702. TYPECODE_SBYTE,
  1703. TYPECODE_BYTE,
  1704. TYPECODE_INT16,
  1705. TYPECODE_UINT16,
  1706. TYPECODE_INT32,
  1707. TYPECODE_UINT32,
  1708. TYPECODE_INT64,
  1709. TYPECODE_UINT64,
  1710. TYPECODE_SINGLE,
  1711. TYPECODE_DOUBLE,
  1712. TYPECODE_DECIMAL,
  1713. TYPECODE_DATETIME,
  1714. TYPECODE_STRING = 18
  1715. } TypeCode;
  1716. #ifndef ENABLE_NETCORE
  1717. guint32
  1718. ves_icall_type_GetTypeCodeInternal (MonoReflectionTypeHandle ref_type, MonoError *error)
  1719. {
  1720. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  1721. if (type->byref)
  1722. return TYPECODE_OBJECT;
  1723. handle_enum:
  1724. switch (type->type) {
  1725. case MONO_TYPE_VOID:
  1726. return TYPECODE_OBJECT;
  1727. case MONO_TYPE_BOOLEAN:
  1728. return TYPECODE_BOOLEAN;
  1729. case MONO_TYPE_U1:
  1730. return TYPECODE_BYTE;
  1731. case MONO_TYPE_I1:
  1732. return TYPECODE_SBYTE;
  1733. case MONO_TYPE_U2:
  1734. return TYPECODE_UINT16;
  1735. case MONO_TYPE_I2:
  1736. return TYPECODE_INT16;
  1737. case MONO_TYPE_CHAR:
  1738. return TYPECODE_CHAR;
  1739. case MONO_TYPE_PTR:
  1740. case MONO_TYPE_U:
  1741. case MONO_TYPE_I:
  1742. return TYPECODE_OBJECT;
  1743. case MONO_TYPE_U4:
  1744. return TYPECODE_UINT32;
  1745. case MONO_TYPE_I4:
  1746. return TYPECODE_INT32;
  1747. case MONO_TYPE_U8:
  1748. return TYPECODE_UINT64;
  1749. case MONO_TYPE_I8:
  1750. return TYPECODE_INT64;
  1751. case MONO_TYPE_R4:
  1752. return TYPECODE_SINGLE;
  1753. case MONO_TYPE_R8:
  1754. return TYPECODE_DOUBLE;
  1755. case MONO_TYPE_VALUETYPE: {
  1756. MonoClass *klass = type->data.klass;
  1757. if (m_class_is_enumtype (klass)) {
  1758. type = mono_class_enum_basetype_internal (klass);
  1759. goto handle_enum;
  1760. } else if (mono_is_corlib_image (m_class_get_image (klass))) {
  1761. if (strcmp (m_class_get_name_space (klass), "System") == 0) {
  1762. if (strcmp (m_class_get_name (klass), "Decimal") == 0)
  1763. return TYPECODE_DECIMAL;
  1764. else if (strcmp (m_class_get_name (klass), "DateTime") == 0)
  1765. return TYPECODE_DATETIME;
  1766. }
  1767. }
  1768. return TYPECODE_OBJECT;
  1769. }
  1770. case MONO_TYPE_STRING:
  1771. return TYPECODE_STRING;
  1772. case MONO_TYPE_SZARRAY:
  1773. case MONO_TYPE_ARRAY:
  1774. case MONO_TYPE_OBJECT:
  1775. case MONO_TYPE_VAR:
  1776. case MONO_TYPE_MVAR:
  1777. case MONO_TYPE_TYPEDBYREF:
  1778. return TYPECODE_OBJECT;
  1779. case MONO_TYPE_CLASS:
  1780. {
  1781. MonoClass *klass = type->data.klass;
  1782. if (m_class_get_image (klass) == mono_defaults.corlib && strcmp (m_class_get_name_space (klass), "System") == 0) {
  1783. if (strcmp (m_class_get_name (klass), "DBNull") == 0)
  1784. return TYPECODE_DBNULL;
  1785. }
  1786. }
  1787. return TYPECODE_OBJECT;
  1788. case MONO_TYPE_GENERICINST:
  1789. if (m_class_is_enumtype (type->data.generic_class->container_class)) {
  1790. type = mono_class_enum_basetype_internal (type->data.generic_class->container_class);
  1791. goto handle_enum;
  1792. }
  1793. return TYPECODE_OBJECT;
  1794. default:
  1795. g_error ("type 0x%02x not handled in GetTypeCode()", type->type);
  1796. }
  1797. return 0;
  1798. }
  1799. #endif
  1800. guint32
  1801. ves_icall_RuntimeTypeHandle_type_is_assignable_from (MonoReflectionTypeHandle ref_type, MonoReflectionTypeHandle ref_c, MonoError *error)
  1802. {
  1803. g_assert (!MONO_HANDLE_IS_NULL (ref_type));
  1804. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  1805. MonoClass *klass = mono_class_from_mono_type_internal (type);
  1806. MonoType *ctype = MONO_HANDLE_GETVAL (ref_c, type);
  1807. MonoClass *klassc = mono_class_from_mono_type_internal (ctype);
  1808. if (type->byref ^ ctype->byref)
  1809. return FALSE;
  1810. if (type->byref) {
  1811. return mono_byref_type_is_assignable_from (type, ctype, FALSE);
  1812. }
  1813. gboolean result;
  1814. mono_class_is_assignable_from_checked (klass, klassc, &result, error);
  1815. return (guint32)result;
  1816. }
  1817. MonoBoolean
  1818. ves_icall_RuntimeTypeHandle_is_subclass_of (MonoType *childType, MonoType *baseType)
  1819. {
  1820. ERROR_DECL (error);
  1821. mono_bool result = FALSE;
  1822. MonoClass *childClass;
  1823. MonoClass *baseClass;
  1824. childClass = mono_class_from_mono_type_internal (childType);
  1825. baseClass = mono_class_from_mono_type_internal (baseType);
  1826. if (G_UNLIKELY (childType->byref)) {
  1827. result = !baseType->byref && baseClass == mono_defaults.object_class;
  1828. goto done;
  1829. }
  1830. if (G_UNLIKELY (baseType->byref)) {
  1831. result = FALSE;
  1832. goto done;
  1833. }
  1834. if (childType == baseType) {
  1835. /* .NET IsSubclassOf is not reflexive */
  1836. result = FALSE;
  1837. goto done;
  1838. }
  1839. if (G_UNLIKELY (is_generic_parameter (childType))) {
  1840. /* slow path: walk the type hierarchy looking at base types
  1841. * until we see baseType. If the current type is not a gparam,
  1842. * break out of the loop and use is_subclass_of.
  1843. */
  1844. MonoClass *c = mono_generic_param_get_base_type (childClass);
  1845. result = FALSE;
  1846. while (c != NULL) {
  1847. if (c == baseClass) {
  1848. result = TRUE;
  1849. break;
  1850. }
  1851. if (!is_generic_parameter (m_class_get_byval_arg (c))) {
  1852. result = mono_class_is_subclass_of_internal (c, baseClass, FALSE);
  1853. break;
  1854. } else
  1855. c = mono_generic_param_get_base_type (c);
  1856. }
  1857. } else {
  1858. result = mono_class_is_subclass_of_internal (childClass, baseClass, FALSE);
  1859. }
  1860. done:
  1861. mono_error_set_pending_exception (error);
  1862. return result;
  1863. }
  1864. guint32
  1865. ves_icall_RuntimeTypeHandle_IsInstanceOfType (MonoReflectionTypeHandle ref_type, MonoObjectHandle obj, MonoError *error)
  1866. {
  1867. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  1868. MonoClass *klass = mono_class_from_mono_type_internal (type);
  1869. mono_class_init_checked (klass, error);
  1870. return_val_if_nok (error, FALSE);
  1871. MonoObjectHandle inst = mono_object_handle_isinst (obj, klass, error);
  1872. return_val_if_nok (error, FALSE);
  1873. return !MONO_HANDLE_IS_NULL (inst);
  1874. }
  1875. guint32
  1876. ves_icall_RuntimeTypeHandle_GetAttributes (MonoReflectionTypeHandle ref_type, MonoError *error)
  1877. {
  1878. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  1879. #ifdef ENABLE_NETCORE
  1880. if (type->byref || type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR)
  1881. return TYPE_ATTRIBUTE_NOT_PUBLIC;
  1882. #endif
  1883. MonoClass *klass = mono_class_from_mono_type_internal (type);
  1884. return mono_class_get_flags (klass);
  1885. }
  1886. MonoReflectionMarshalAsAttributeHandle
  1887. ves_icall_System_Reflection_FieldInfo_get_marshal_info (MonoReflectionFieldHandle field_h, MonoError *error)
  1888. {
  1889. MonoDomain *domain = MONO_HANDLE_DOMAIN (field_h);
  1890. MonoClassField *field = MONO_HANDLE_GETVAL (field_h, field);
  1891. MonoClass *klass = field->parent;
  1892. MonoGenericClass *gklass = mono_class_try_get_generic_class (klass);
  1893. if (mono_class_is_gtd (klass) ||
  1894. (gklass && gklass->context.class_inst->is_open))
  1895. return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute, NULL_HANDLE);
  1896. MonoType *ftype = mono_field_get_type_internal (field);
  1897. if (ftype && !(ftype->attrs & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL))
  1898. return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute, NULL_HANDLE);
  1899. MonoMarshalType *info = mono_marshal_load_type_info (klass);
  1900. for (int i = 0; i < info->num_fields; ++i) {
  1901. if (info->fields [i].field == field) {
  1902. if (!info->fields [i].mspec)
  1903. return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute, NULL_HANDLE);
  1904. else {
  1905. return mono_reflection_marshal_as_attribute_from_marshal_spec (domain, klass, info->fields [i].mspec, error);
  1906. }
  1907. }
  1908. }
  1909. return MONO_HANDLE_CAST (MonoReflectionMarshalAsAttribute, NULL_HANDLE);
  1910. }
  1911. MonoReflectionFieldHandle
  1912. ves_icall_System_Reflection_FieldInfo_internal_from_handle_type (MonoClassField *handle, MonoType *type, MonoError *error)
  1913. {
  1914. MonoClass *klass;
  1915. g_assert (handle);
  1916. if (!type) {
  1917. klass = handle->parent;
  1918. } else {
  1919. klass = mono_class_from_mono_type_internal (type);
  1920. gboolean found = klass == handle->parent || mono_class_has_parent (klass, handle->parent);
  1921. if (!found)
  1922. /* The managed code will throw the exception */
  1923. return MONO_HANDLE_CAST (MonoReflectionField, NULL_HANDLE);
  1924. }
  1925. return mono_field_get_object_handle (mono_domain_get (), klass, handle, error);
  1926. }
  1927. MonoReflectionEventHandle
  1928. ves_icall_System_Reflection_EventInfo_internal_from_handle_type (MonoEvent *handle, MonoType *type, MonoError *error)
  1929. {
  1930. MonoClass *klass;
  1931. g_assert (handle);
  1932. if (!type) {
  1933. klass = handle->parent;
  1934. } else {
  1935. klass = mono_class_from_mono_type_internal (type);
  1936. gboolean found = klass == handle->parent || mono_class_has_parent (klass, handle->parent);
  1937. if (!found)
  1938. /* Managed code will throw an exception */
  1939. return MONO_HANDLE_CAST (MonoReflectionEvent, NULL_HANDLE);
  1940. }
  1941. return mono_event_get_object_handle (mono_domain_get (), klass, handle, error);
  1942. }
  1943. MonoReflectionPropertyHandle
  1944. ves_icall_System_Reflection_RuntimePropertyInfo_internal_from_handle_type (MonoProperty *handle, MonoType *type, MonoError *error)
  1945. {
  1946. MonoClass *klass;
  1947. g_assert (handle);
  1948. if (!type) {
  1949. klass = handle->parent;
  1950. } else {
  1951. klass = mono_class_from_mono_type_internal (type);
  1952. gboolean found = klass == handle->parent || mono_class_has_parent (klass, handle->parent);
  1953. if (!found)
  1954. /* Managed code will throw an exception */
  1955. return MONO_HANDLE_CAST (MonoReflectionProperty, NULL_HANDLE);
  1956. }
  1957. return mono_property_get_object_handle (mono_domain_get (), klass, handle, error);
  1958. }
  1959. MonoArrayHandle
  1960. ves_icall_System_Reflection_FieldInfo_GetTypeModifiers (MonoReflectionFieldHandle field_h, MonoBoolean optional, MonoError *error)
  1961. {
  1962. MonoClassField *field = MONO_HANDLE_GETVAL (field_h, field);
  1963. MonoType *type = mono_field_get_type_checked (field, error);
  1964. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  1965. return type_array_from_modifiers (type, optional, error);
  1966. }
  1967. int
  1968. ves_icall_get_method_attributes (MonoMethod *method)
  1969. {
  1970. return method->flags;
  1971. }
  1972. void
  1973. ves_icall_get_method_info (MonoMethod *method, MonoMethodInfo *info, MonoError *error)
  1974. {
  1975. MonoDomain *domain = mono_domain_get ();
  1976. MonoMethodSignature* sig = mono_method_signature_checked (method, error);
  1977. return_if_nok (error);
  1978. MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (method->klass), error);
  1979. return_if_nok (error);
  1980. MONO_STRUCT_SETREF_INTERNAL (info, parent, MONO_HANDLE_RAW (rt));
  1981. MONO_HANDLE_ASSIGN (rt, mono_type_get_object_handle (domain, sig->ret, error));
  1982. return_if_nok (error);
  1983. MONO_STRUCT_SETREF_INTERNAL (info, ret, MONO_HANDLE_RAW (rt));
  1984. info->attrs = method->flags;
  1985. info->implattrs = method->iflags;
  1986. guint32 callconv;
  1987. if (sig->call_convention == MONO_CALL_DEFAULT)
  1988. callconv = sig->sentinelpos >= 0 ? 2 : 1;
  1989. else {
  1990. if (sig->call_convention == MONO_CALL_VARARG || sig->sentinelpos >= 0)
  1991. callconv = 2;
  1992. else
  1993. callconv = 1;
  1994. }
  1995. callconv |= (sig->hasthis << 5) | (sig->explicit_this << 6);
  1996. info->callconv = callconv;
  1997. }
  1998. MonoArrayHandle
  1999. ves_icall_System_Reflection_MonoMethodInfo_get_parameter_info (MonoMethod *method, MonoReflectionMethodHandle member, MonoError *error)
  2000. {
  2001. MonoDomain *domain = mono_domain_get ();
  2002. MonoReflectionTypeHandle reftype = MONO_HANDLE_NEW (MonoReflectionType, NULL);
  2003. MONO_HANDLE_GET (reftype, member, reftype);
  2004. MonoClass *klass = NULL;
  2005. if (!MONO_HANDLE_IS_NULL (reftype))
  2006. klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (reftype, type));
  2007. return mono_param_get_objects_internal (domain, method, klass, error);
  2008. }
  2009. MonoReflectionMarshalAsAttributeHandle
  2010. ves_icall_System_MonoMethodInfo_get_retval_marshal (MonoMethod *method, MonoError *error)
  2011. {
  2012. MonoDomain *domain = mono_domain_get ();
  2013. MonoReflectionMarshalAsAttributeHandle res = MONO_HANDLE_NEW (MonoReflectionMarshalAsAttribute, NULL);
  2014. MonoMarshalSpec **mspecs = g_new (MonoMarshalSpec*, mono_method_signature_internal (method)->param_count + 1);
  2015. mono_method_get_marshal_info (method, mspecs);
  2016. if (mspecs [0]) {
  2017. MONO_HANDLE_ASSIGN (res, mono_reflection_marshal_as_attribute_from_marshal_spec (domain, method->klass, mspecs [0], error));
  2018. goto_if_nok (error, leave);
  2019. }
  2020. leave:
  2021. for (int i = mono_method_signature_internal (method)->param_count; i >= 0; i--)
  2022. if (mspecs [i])
  2023. mono_metadata_free_marshal_spec (mspecs [i]);
  2024. g_free (mspecs);
  2025. return res;
  2026. }
  2027. gint32
  2028. ves_icall_RuntimeFieldInfo_GetFieldOffset (MonoReflectionFieldHandle field, MonoError *error)
  2029. {
  2030. MonoClassField *class_field = MONO_HANDLE_GETVAL (field, field);
  2031. mono_class_setup_fields (class_field->parent);
  2032. return class_field->offset - MONO_ABI_SIZEOF (MonoObject);
  2033. }
  2034. MonoReflectionTypeHandle
  2035. ves_icall_RuntimeFieldInfo_GetParentType (MonoReflectionFieldHandle field, MonoBoolean declaring, MonoError *error)
  2036. {
  2037. MonoDomain *domain = MONO_HANDLE_DOMAIN (field);
  2038. MonoClass *parent;
  2039. if (declaring) {
  2040. MonoClassField *f = MONO_HANDLE_GETVAL (field, field);
  2041. parent = f->parent;
  2042. } else {
  2043. parent = MONO_HANDLE_GETVAL (field, klass);
  2044. }
  2045. return mono_type_get_object_handle (domain, m_class_get_byval_arg (parent), error);
  2046. }
  2047. MonoObjectHandle
  2048. ves_icall_RuntimeFieldInfo_GetValueInternal (MonoReflectionFieldHandle field_handle, MonoObjectHandle obj_handle, MonoError *error)
  2049. {
  2050. MonoReflectionField * const field = MONO_HANDLE_RAW (field_handle);
  2051. MonoClass *fklass = field->klass;
  2052. MonoClassField *cf = field->field;
  2053. if (mono_asmctx_get_kind (&m_class_get_image (fklass)->assembly->context) == MONO_ASMCTX_REFONLY) {
  2054. mono_error_set_invalid_operation (error,
  2055. "It is illegal to get the value on a field on a type loaded using the ReflectionOnly methods.");
  2056. return NULL_HANDLE;
  2057. }
  2058. if (mono_security_core_clr_enabled () &&
  2059. !mono_security_core_clr_ensure_reflection_access_field (cf, error)) {
  2060. return NULL_HANDLE;
  2061. }
  2062. MonoObject * const obj = MONO_HANDLE_RAW (obj_handle);
  2063. MonoObject *result;
  2064. #ifndef DISABLE_REMOTING
  2065. if (G_UNLIKELY (obj != NULL && mono_class_is_transparent_proxy (mono_object_class (obj)))) {
  2066. /* We get here if someone used a
  2067. * System.Reflection.FieldInfo:GetValue on a
  2068. * ContextBoundObject's or cross-domain MarshalByRefObject's
  2069. * transparent proxy. */
  2070. result = mono_load_remote_field_new_checked (obj, fklass, cf, error);
  2071. } else
  2072. #endif
  2073. result = mono_field_get_value_object_checked (mono_object_domain (field), cf, obj, error);
  2074. return MONO_HANDLE_NEW (MonoObject, result);
  2075. }
  2076. void
  2077. ves_icall_RuntimeFieldInfo_SetValueInternal (MonoReflectionFieldHandle field, MonoObjectHandle obj, MonoObjectHandle value, MonoError *error)
  2078. {
  2079. MonoClassField *cf = MONO_HANDLE_GETVAL (field, field);
  2080. MonoClass *field_klass = MONO_HANDLE_GETVAL (field, klass);
  2081. if (mono_asmctx_get_kind (&m_class_get_image (field_klass)->assembly->context) == MONO_ASMCTX_REFONLY) {
  2082. mono_error_set_invalid_operation (error, "It is illegal to set the value on a field on a type loaded using the ReflectionOnly methods.");
  2083. return;
  2084. }
  2085. if (mono_security_core_clr_enabled () &&
  2086. !mono_security_core_clr_ensure_reflection_access_field (cf, error)) {
  2087. return;
  2088. }
  2089. #ifndef DISABLE_REMOTING
  2090. if (G_UNLIKELY (!MONO_HANDLE_IS_NULL (obj) && mono_class_is_transparent_proxy (mono_handle_class (obj)))) {
  2091. /* We get here if someone used a
  2092. * System.Reflection.FieldInfo:SetValue on a
  2093. * ContextBoundObject's or cross-domain MarshalByRefObject's
  2094. * transparent proxy. */
  2095. /* FIXME: use handles for mono_store_remote_field_new_checked */
  2096. MonoObject *v = MONO_HANDLE_RAW (value);
  2097. MonoObject *o = MONO_HANDLE_RAW (obj);
  2098. mono_store_remote_field_new_checked (o, field_klass, cf, v, error);
  2099. return;
  2100. }
  2101. #endif
  2102. MonoType *type = mono_field_get_type_checked (cf, error);
  2103. return_if_nok (error);
  2104. gboolean isref = FALSE;
  2105. MonoGCHandle value_gchandle = 0;
  2106. gchar *v = NULL;
  2107. if (!type->byref) {
  2108. switch (type->type) {
  2109. case MONO_TYPE_U1:
  2110. case MONO_TYPE_I1:
  2111. case MONO_TYPE_BOOLEAN:
  2112. case MONO_TYPE_U2:
  2113. case MONO_TYPE_I2:
  2114. case MONO_TYPE_CHAR:
  2115. case MONO_TYPE_U:
  2116. case MONO_TYPE_I:
  2117. case MONO_TYPE_U4:
  2118. case MONO_TYPE_I4:
  2119. case MONO_TYPE_R4:
  2120. case MONO_TYPE_U8:
  2121. case MONO_TYPE_I8:
  2122. case MONO_TYPE_R8:
  2123. case MONO_TYPE_VALUETYPE:
  2124. case MONO_TYPE_PTR:
  2125. isref = FALSE;
  2126. if (!MONO_HANDLE_IS_NULL (value))
  2127. v = (char*)mono_object_handle_pin_unbox (value, &value_gchandle);
  2128. break;
  2129. case MONO_TYPE_STRING:
  2130. case MONO_TYPE_OBJECT:
  2131. case MONO_TYPE_CLASS:
  2132. case MONO_TYPE_ARRAY:
  2133. case MONO_TYPE_SZARRAY:
  2134. /* Do nothing */
  2135. isref = TRUE;
  2136. break;
  2137. case MONO_TYPE_GENERICINST: {
  2138. MonoGenericClass *gclass = type->data.generic_class;
  2139. g_assert (!gclass->context.class_inst->is_open);
  2140. if (mono_class_is_nullable (mono_class_from_mono_type_internal (type))) {
  2141. MonoClass *nklass = mono_class_from_mono_type_internal (type);
  2142. /*
  2143. * Convert the boxed vtype into a Nullable structure.
  2144. * This is complicated by the fact that Nullables have
  2145. * a variable structure.
  2146. */
  2147. MonoObjectHandle nullable = mono_object_new_handle (mono_domain_get (), nklass, error);
  2148. return_if_nok (error);
  2149. MonoGCHandle nullable_gchandle = 0;
  2150. guint8 *nval = (guint8*)mono_object_handle_pin_unbox (nullable, &nullable_gchandle);
  2151. mono_nullable_init_from_handle (nval, value, nklass);
  2152. isref = FALSE;
  2153. value_gchandle = nullable_gchandle;
  2154. v = (gchar*)nval;
  2155. }
  2156. else {
  2157. isref = !m_class_is_valuetype (gclass->container_class);
  2158. if (!isref && !MONO_HANDLE_IS_NULL (value)) {
  2159. v = (char*)mono_object_handle_pin_unbox (value, &value_gchandle);
  2160. };
  2161. }
  2162. break;
  2163. }
  2164. default:
  2165. g_error ("type 0x%x not handled in "
  2166. "ves_icall_FieldInfo_SetValueInternal", type->type);
  2167. return;
  2168. }
  2169. }
  2170. /* either value is a reference type, or it's a value type and we pinned
  2171. * it and v points to the payload. */
  2172. g_assert ((isref && v == NULL && value_gchandle == 0) ||
  2173. (!isref && v != NULL && value_gchandle != 0) ||
  2174. (!isref && v == NULL && value_gchandle == 0));
  2175. if (type->attrs & FIELD_ATTRIBUTE_STATIC) {
  2176. MonoVTable *vtable = mono_class_vtable_checked (MONO_HANDLE_DOMAIN (field), cf->parent, error);
  2177. goto_if_nok (error, leave);
  2178. if (!vtable->initialized) {
  2179. if (!mono_runtime_class_init_full (vtable, error))
  2180. goto leave;
  2181. }
  2182. if (isref)
  2183. mono_field_static_set_value_internal (vtable, cf, MONO_HANDLE_RAW (value)); /* FIXME make mono_field_static_set_value work with handles for value */
  2184. else
  2185. mono_field_static_set_value_internal (vtable, cf, v);
  2186. } else {
  2187. if (isref)
  2188. MONO_HANDLE_SET_FIELD_REF (obj, cf, value);
  2189. else
  2190. mono_field_set_value_internal (MONO_HANDLE_RAW (obj), cf, v); /* FIXME: make mono_field_set_value take a handle for obj */
  2191. }
  2192. leave:
  2193. if (value_gchandle)
  2194. mono_gchandle_free_internal (value_gchandle);
  2195. }
  2196. static MonoObjectHandle
  2197. typed_reference_to_object (MonoTypedRef *tref, MonoError *error)
  2198. {
  2199. HANDLE_FUNCTION_ENTER ();
  2200. MonoObjectHandle result;
  2201. if (MONO_TYPE_IS_REFERENCE (tref->type)) {
  2202. MonoObject** objp = (MonoObject **)tref->value;
  2203. result = MONO_HANDLE_NEW (MonoObject, *objp);
  2204. } else if (mono_type_is_pointer (tref->type)) {
  2205. /* Boxed as UIntPtr */
  2206. result = mono_value_box_handle (mono_domain_get (), mono_get_uintptr_class (), tref->value, error);
  2207. } else {
  2208. result = mono_value_box_handle (mono_domain_get (), tref->klass, tref->value, error);
  2209. }
  2210. HANDLE_FUNCTION_RETURN_REF (MonoObject, result);
  2211. }
  2212. MonoObjectHandle
  2213. ves_icall_System_RuntimeFieldHandle_GetValueDirect (MonoReflectionFieldHandle field_h, MonoReflectionTypeHandle field_type_h, MonoTypedRef *obj, MonoReflectionTypeHandle context_type_h, MonoError *error)
  2214. {
  2215. MonoClassField *field = MONO_HANDLE_GETVAL (field_h, field);
  2216. MonoClass *klass = mono_class_from_mono_type_internal (field->type);
  2217. if (!MONO_TYPE_ISSTRUCT (m_class_get_byval_arg (field->parent))) {
  2218. mono_error_set_not_implemented (error, "");
  2219. return MONO_HANDLE_NEW (MonoObject, NULL);
  2220. } else if (MONO_TYPE_IS_REFERENCE (field->type)) {
  2221. return MONO_HANDLE_NEW (MonoObject, *(MonoObject**)((guint8*)obj->value + field->offset - sizeof (MonoObject)));
  2222. } else {
  2223. return mono_value_box_handle (mono_domain_get (), klass, (guint8*)obj->value + field->offset - sizeof (MonoObject), error);
  2224. }
  2225. }
  2226. void
  2227. ves_icall_System_RuntimeFieldHandle_SetValueDirect (MonoReflectionFieldHandle field_h, MonoReflectionTypeHandle field_type_h, MonoTypedRef *obj, MonoObjectHandle value_h, MonoReflectionTypeHandle context_type_h, MonoError *error)
  2228. {
  2229. MonoClassField *f = MONO_HANDLE_GETVAL (field_h, field);
  2230. g_assert (obj);
  2231. mono_class_setup_fields (f->parent);
  2232. if (!MONO_TYPE_ISSTRUCT (m_class_get_byval_arg (f->parent))) {
  2233. MonoObjectHandle objHandle = typed_reference_to_object (obj, error);
  2234. return_if_nok (error);
  2235. ves_icall_RuntimeFieldInfo_SetValueInternal (field_h, objHandle, value_h, error);
  2236. } else if (MONO_TYPE_IS_REFERENCE (f->type)) {
  2237. mono_copy_value (f->type, (guint8*)obj->value + m_field_get_offset (f) - sizeof (MonoObject), MONO_HANDLE_RAW (value_h), FALSE);
  2238. } else {
  2239. MonoGCHandle gchandle = NULL;
  2240. g_assert (MONO_HANDLE_RAW (value_h));
  2241. mono_copy_value (f->type, (guint8*)obj->value + m_field_get_offset (f) - sizeof (MonoObject), mono_object_handle_pin_unbox (value_h, &gchandle), FALSE);
  2242. mono_gchandle_free_internal (gchandle);
  2243. }
  2244. }
  2245. MonoObjectHandle
  2246. ves_icall_RuntimeFieldInfo_GetRawConstantValue (MonoReflectionFieldHandle rfield, MonoError* error)
  2247. {
  2248. MonoObjectHandle o_handle = NULL_HANDLE_INIT;
  2249. MonoObject *o = NULL;
  2250. MonoClassField *field = MONO_HANDLE_GETVAL (rfield, field);
  2251. MonoClass *klass;
  2252. MonoDomain *domain = MONO_HANDLE_DOMAIN (rfield);
  2253. gchar *v;
  2254. MonoTypeEnum def_type;
  2255. const char *def_value;
  2256. MonoType *t;
  2257. MonoStringHandle string_handle = MONO_HANDLE_NEW (MonoString, NULL); // FIXME? Not always needed.
  2258. mono_class_init_internal (field->parent);
  2259. t = mono_field_get_type_checked (field, error);
  2260. goto_if_nok (error, return_null);
  2261. if (!(t->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT))
  2262. goto invalid_operation;
  2263. if (image_is_dynamic (m_class_get_image (field->parent))) {
  2264. MonoClass *klass = field->parent;
  2265. int fidx = field - m_class_get_fields (klass);
  2266. MonoFieldDefaultValue *def_values = mono_class_get_field_def_values (klass);
  2267. g_assert (def_values);
  2268. def_type = def_values [fidx].def_type;
  2269. def_value = def_values [fidx].data;
  2270. if (def_type == MONO_TYPE_END)
  2271. goto invalid_operation;
  2272. } else {
  2273. def_value = mono_class_get_field_default_value (field, &def_type);
  2274. /* FIXME, maybe we should try to raise TLE if field->parent is broken */
  2275. if (!def_value)
  2276. goto invalid_operation;
  2277. }
  2278. /*FIXME unify this with reflection.c:mono_get_object_from_blob*/
  2279. switch (def_type) {
  2280. case MONO_TYPE_U1:
  2281. case MONO_TYPE_I1:
  2282. case MONO_TYPE_BOOLEAN:
  2283. case MONO_TYPE_U2:
  2284. case MONO_TYPE_I2:
  2285. case MONO_TYPE_CHAR:
  2286. case MONO_TYPE_U:
  2287. case MONO_TYPE_I:
  2288. case MONO_TYPE_U4:
  2289. case MONO_TYPE_I4:
  2290. case MONO_TYPE_R4:
  2291. case MONO_TYPE_U8:
  2292. case MONO_TYPE_I8:
  2293. case MONO_TYPE_R8: {
  2294. MonoType *t;
  2295. /* boxed value type */
  2296. t = g_new0 (MonoType, 1);
  2297. t->type = def_type;
  2298. klass = mono_class_from_mono_type_internal (t);
  2299. g_free (t);
  2300. o = mono_object_new_checked (domain, klass, error);
  2301. goto_if_nok (error, return_null);
  2302. o_handle = MONO_HANDLE_NEW (MonoObject, o);
  2303. v = ((gchar *) o) + sizeof (MonoObject);
  2304. (void)mono_get_constant_value_from_blob (domain, def_type, def_value, v, string_handle, error);
  2305. goto_if_nok (error, return_null);
  2306. break;
  2307. }
  2308. case MONO_TYPE_STRING:
  2309. case MONO_TYPE_CLASS:
  2310. (void)mono_get_constant_value_from_blob (domain, def_type, def_value, &o, string_handle, error);
  2311. goto_if_nok (error, return_null);
  2312. o_handle = MONO_HANDLE_NEW (MonoObject, o);
  2313. break;
  2314. default:
  2315. g_assert_not_reached ();
  2316. }
  2317. goto exit;
  2318. invalid_operation:
  2319. mono_error_set_invalid_operation (error, NULL);
  2320. // fall through
  2321. return_null:
  2322. o_handle = NULL_HANDLE;
  2323. // fall through
  2324. exit:
  2325. return o_handle;
  2326. }
  2327. MonoReflectionTypeHandle
  2328. ves_icall_RuntimeFieldInfo_ResolveType (MonoReflectionFieldHandle ref_field, MonoError *error)
  2329. {
  2330. MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_field);
  2331. MonoClassField *field = MONO_HANDLE_GETVAL (ref_field, field);
  2332. MonoType *type = mono_field_get_type_checked (field, error);
  2333. return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
  2334. return mono_type_get_object_handle (domain, type, error);
  2335. }
  2336. void
  2337. ves_icall_RuntimePropertyInfo_get_property_info (MonoReflectionPropertyHandle property, MonoPropertyInfo *info, PInfo req_info, MonoError *error)
  2338. {
  2339. MonoDomain *domain = MONO_HANDLE_DOMAIN (property);
  2340. const MonoProperty *pproperty = MONO_HANDLE_GETVAL (property, property);
  2341. if ((req_info & PInfo_ReflectedType) != 0) {
  2342. MonoClass *klass = MONO_HANDLE_GETVAL (property, klass);
  2343. MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
  2344. return_if_nok (error);
  2345. MONO_STRUCT_SETREF_INTERNAL (info, parent, MONO_HANDLE_RAW (rt));
  2346. }
  2347. if ((req_info & PInfo_DeclaringType) != 0) {
  2348. MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (pproperty->parent), error);
  2349. return_if_nok (error);
  2350. MONO_STRUCT_SETREF_INTERNAL (info, declaring_type, MONO_HANDLE_RAW (rt));
  2351. }
  2352. if ((req_info & PInfo_Name) != 0) {
  2353. MonoStringHandle name = mono_string_new_handle (domain, pproperty->name, error);
  2354. return_if_nok (error);
  2355. MONO_STRUCT_SETREF_INTERNAL (info, name, MONO_HANDLE_RAW (name));
  2356. }
  2357. if ((req_info & PInfo_Attributes) != 0)
  2358. info->attrs = pproperty->attrs;
  2359. if ((req_info & PInfo_GetMethod) != 0) {
  2360. MonoClass *property_klass = MONO_HANDLE_GETVAL (property, klass);
  2361. MonoReflectionMethodHandle rm;
  2362. if (pproperty->get &&
  2363. (((pproperty->get->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) != METHOD_ATTRIBUTE_PRIVATE) ||
  2364. pproperty->get->klass == property_klass)) {
  2365. rm = mono_method_get_object_handle (domain, pproperty->get, property_klass, error);
  2366. return_if_nok (error);
  2367. } else {
  2368. rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
  2369. }
  2370. MONO_STRUCT_SETREF_INTERNAL (info, get, MONO_HANDLE_RAW (rm));
  2371. }
  2372. if ((req_info & PInfo_SetMethod) != 0) {
  2373. MonoClass *property_klass = MONO_HANDLE_GETVAL (property, klass);
  2374. MonoReflectionMethodHandle rm;
  2375. if (pproperty->set &&
  2376. (((pproperty->set->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) != METHOD_ATTRIBUTE_PRIVATE) ||
  2377. pproperty->set->klass == property_klass)) {
  2378. rm = mono_method_get_object_handle (domain, pproperty->set, property_klass, error);
  2379. return_if_nok (error);
  2380. } else {
  2381. rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
  2382. }
  2383. MONO_STRUCT_SETREF_INTERNAL (info, set, MONO_HANDLE_RAW (rm));
  2384. }
  2385. /*
  2386. * There may be other methods defined for properties, though, it seems they are not exposed
  2387. * in the reflection API
  2388. */
  2389. }
  2390. static gboolean
  2391. add_event_other_methods_to_array (MonoDomain *domain, MonoMethod *m, MonoArrayHandle dest, int i, MonoError *error)
  2392. {
  2393. HANDLE_FUNCTION_ENTER ();
  2394. error_init (error);
  2395. MonoReflectionMethodHandle rm = mono_method_get_object_handle (domain, m, NULL, error);
  2396. goto_if_nok (error, leave);
  2397. MONO_HANDLE_ARRAY_SETREF (dest, i, rm);
  2398. leave:
  2399. HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
  2400. }
  2401. void
  2402. ves_icall_RuntimeEventInfo_get_event_info (MonoReflectionMonoEventHandle ref_event, MonoEventInfo *info, MonoError *error)
  2403. {
  2404. MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_event);
  2405. MonoClass *klass = MONO_HANDLE_GETVAL (ref_event, klass);
  2406. MonoEvent *event = MONO_HANDLE_GETVAL (ref_event, event);
  2407. MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
  2408. return_if_nok (error);
  2409. MONO_STRUCT_SETREF_INTERNAL (info, reflected_type, MONO_HANDLE_RAW (rt));
  2410. rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (event->parent), error);
  2411. return_if_nok (error);
  2412. MONO_STRUCT_SETREF_INTERNAL (info, declaring_type, MONO_HANDLE_RAW (rt));
  2413. MonoStringHandle ev_name = mono_string_new_handle (domain, event->name, error);
  2414. return_if_nok (error);
  2415. MONO_STRUCT_SETREF_INTERNAL (info, name, MONO_HANDLE_RAW (ev_name));
  2416. info->attrs = event->attrs;
  2417. MonoReflectionMethodHandle rm;
  2418. if (event->add) {
  2419. rm = mono_method_get_object_handle (domain, event->add, klass, error);
  2420. return_if_nok (error);
  2421. } else {
  2422. rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
  2423. }
  2424. MONO_STRUCT_SETREF_INTERNAL (info, add_method, MONO_HANDLE_RAW (rm));
  2425. if (event->remove) {
  2426. rm = mono_method_get_object_handle (domain, event->remove, klass, error);
  2427. return_if_nok (error);
  2428. } else {
  2429. rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
  2430. }
  2431. MONO_STRUCT_SETREF_INTERNAL (info, remove_method, MONO_HANDLE_RAW (rm));
  2432. if (event->raise) {
  2433. rm = mono_method_get_object_handle (domain, event->raise, klass, error);
  2434. return_if_nok (error);
  2435. } else {
  2436. rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
  2437. }
  2438. MONO_STRUCT_SETREF_INTERNAL (info, raise_method, MONO_HANDLE_RAW (rm));
  2439. #ifndef MONO_SMALL_CONFIG
  2440. if (event->other) {
  2441. int i, n = 0;
  2442. while (event->other [n])
  2443. n++;
  2444. MonoArrayHandle info_arr = mono_array_new_handle (domain, mono_defaults.method_info_class, n, error);
  2445. return_if_nok (error);
  2446. MONO_STRUCT_SETREF_INTERNAL (info, other_methods, MONO_HANDLE_RAW (info_arr));
  2447. for (i = 0; i < n; i++)
  2448. if (!add_event_other_methods_to_array (domain, event->other [i], info_arr, i, error))
  2449. return;
  2450. }
  2451. #endif
  2452. }
  2453. static void
  2454. collect_interfaces (MonoClass *klass, GHashTable *ifaces, MonoError *error)
  2455. {
  2456. int i;
  2457. MonoClass *ic;
  2458. mono_class_setup_interfaces (klass, error);
  2459. return_if_nok (error);
  2460. int klass_interface_count = m_class_get_interface_count (klass);
  2461. MonoClass **klass_interfaces = m_class_get_interfaces (klass);
  2462. for (i = 0; i < klass_interface_count; i++) {
  2463. ic = klass_interfaces [i];
  2464. g_hash_table_insert (ifaces, ic, ic);
  2465. collect_interfaces (ic, ifaces, error);
  2466. return_if_nok (error);
  2467. }
  2468. }
  2469. typedef struct {
  2470. MonoArrayHandle iface_array;
  2471. MonoGenericContext *context;
  2472. MonoError *error;
  2473. MonoDomain *domain;
  2474. int next_idx;
  2475. } FillIfaceArrayData;
  2476. static void
  2477. fill_iface_array (gpointer key, gpointer value, gpointer user_data)
  2478. {
  2479. HANDLE_FUNCTION_ENTER ();
  2480. FillIfaceArrayData *data = (FillIfaceArrayData *)user_data;
  2481. MonoClass *ic = (MonoClass *)key;
  2482. MonoType *ret = m_class_get_byval_arg (ic), *inflated = NULL;
  2483. MonoError *error = data->error;
  2484. goto_if_nok (error, leave);
  2485. if (data->context && mono_class_is_ginst (ic) && mono_class_get_generic_class (ic)->context.class_inst->is_open) {
  2486. inflated = ret = mono_class_inflate_generic_type_checked (ret, data->context, error);
  2487. goto_if_nok (error, leave);
  2488. }
  2489. MonoReflectionTypeHandle rt;
  2490. rt = mono_type_get_object_handle (data->domain, ret, error);
  2491. goto_if_nok (error, leave);
  2492. MONO_HANDLE_ARRAY_SETREF (data->iface_array, data->next_idx, rt);
  2493. data->next_idx++;
  2494. if (inflated)
  2495. mono_metadata_free_type (inflated);
  2496. leave:
  2497. HANDLE_FUNCTION_RETURN ();
  2498. }
  2499. static guint
  2500. get_interfaces_hash (gconstpointer v1)
  2501. {
  2502. MonoClass *k = (MonoClass*)v1;
  2503. return m_class_get_type_token (k);
  2504. }
  2505. MonoArrayHandle
  2506. ves_icall_RuntimeType_GetInterfaces (MonoReflectionTypeHandle ref_type, MonoError *error)
  2507. {
  2508. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2509. MonoClass *klass = mono_class_from_mono_type_internal (type);
  2510. GHashTable *iface_hash = g_hash_table_new (get_interfaces_hash, NULL);
  2511. MonoGenericContext *context = NULL;
  2512. if (mono_class_is_ginst (klass) && mono_class_get_generic_class (klass)->context.class_inst->is_open) {
  2513. context = mono_class_get_context (klass);
  2514. klass = mono_class_get_generic_class (klass)->container_class;
  2515. }
  2516. for (MonoClass *parent = klass; parent; parent = m_class_get_parent (parent)) {
  2517. mono_class_setup_interfaces (parent, error);
  2518. goto_if_nok (error, fail);
  2519. collect_interfaces (parent, iface_hash, error);
  2520. goto_if_nok (error, fail);
  2521. }
  2522. MonoDomain *domain;
  2523. domain = MONO_HANDLE_DOMAIN (ref_type);
  2524. int len;
  2525. len = g_hash_table_size (iface_hash);
  2526. if (len == 0) {
  2527. g_hash_table_destroy (iface_hash);
  2528. if (!domain->empty_types) {
  2529. domain->empty_types = mono_array_new_cached (domain, mono_defaults.runtimetype_class, 0, error);
  2530. mono_gc_wbarrier_generic_nostore_internal (&domain->empty_types);
  2531. goto_if_nok (error, fail);
  2532. }
  2533. return MONO_HANDLE_NEW (MonoArray, domain->empty_types);
  2534. }
  2535. FillIfaceArrayData data;
  2536. data.iface_array = MONO_HANDLE_NEW (MonoArray, mono_array_new_cached (domain, mono_defaults.runtimetype_class, len, error));
  2537. goto_if_nok (error, fail);
  2538. data.context = context;
  2539. data.error = error;
  2540. data.domain = domain;
  2541. data.next_idx = 0;
  2542. g_hash_table_foreach (iface_hash, fill_iface_array, &data);
  2543. goto_if_nok (error, fail);
  2544. g_hash_table_destroy (iface_hash);
  2545. return data.iface_array;
  2546. fail:
  2547. g_hash_table_destroy (iface_hash);
  2548. return NULL_HANDLE_ARRAY;
  2549. }
  2550. static gboolean
  2551. method_is_reabstracted (MonoMethod *method)
  2552. {
  2553. /* only on interfaces */
  2554. /* method is marked "final abstract" */
  2555. /* FIXME: we need some other way to detect reabstracted methods. "final" is an incidental detail of the spec. */
  2556. return m_method_is_final (method) && m_method_is_abstract (method);
  2557. }
  2558. static gboolean
  2559. method_is_dim (MonoMethod *method)
  2560. {
  2561. /* only valid on interface methods*/
  2562. /* method is marked "virtual" but not "virtual abstract" */
  2563. return m_method_is_virtual (method) && !m_method_is_abstract (method);
  2564. }
  2565. static gboolean
  2566. set_interface_map_data_method_object (MonoDomain *domain, MonoMethod *method, MonoClass *iclass, int ioffset, MonoClass *klass, MonoArrayHandle targets, MonoArrayHandle methods, int i, MonoError *error)
  2567. {
  2568. HANDLE_FUNCTION_ENTER ();
  2569. error_init (error);
  2570. MonoReflectionMethodHandle member = mono_method_get_object_handle (domain, method, iclass, error);
  2571. goto_if_nok (error, leave);
  2572. MONO_HANDLE_ARRAY_SETREF (methods, i, member);
  2573. MonoMethod* foundMethod = m_class_get_vtable (klass) [i + ioffset];
  2574. if (mono_class_has_dim_conflicts (klass) && mono_class_is_interface (foundMethod->klass)) {
  2575. GSList* conflicts = mono_class_get_dim_conflicts (klass);
  2576. GSList* l;
  2577. MonoMethod* decl = method;
  2578. if (decl->is_inflated)
  2579. decl = ((MonoMethodInflated*)decl)->declaring;
  2580. gboolean in_conflict = FALSE;
  2581. for (l = conflicts; l; l = l->next) {
  2582. if (decl == l->data) {
  2583. in_conflict = TRUE;
  2584. break;
  2585. }
  2586. }
  2587. if (in_conflict) {
  2588. MONO_HANDLE_ARRAY_SETREF (targets, i, NULL_HANDLE);
  2589. goto leave;
  2590. }
  2591. }
  2592. /*
  2593. * if the iterface method is reabstracted, and either the found implementation method is abstract, or the found
  2594. * implementation method is from another DIM (meaning neither klass nor any of its ancestor classes implemented
  2595. * the method), then say the target method is null.
  2596. */
  2597. if (method_is_reabstracted (method) &&
  2598. (m_method_is_abstract (foundMethod) ||
  2599. (mono_class_is_interface (foundMethod->klass) && method_is_dim (foundMethod))))
  2600. MONO_HANDLE_ARRAY_SETREF (targets, i, NULL_HANDLE);
  2601. else if (mono_class_is_interface (foundMethod->klass) && method_is_reabstracted (foundMethod) && !m_class_is_abstract (klass)) {
  2602. /* if the method we found is a reabstracted DIM method, but the class isn't abstract, return NULL */
  2603. /*
  2604. * (C# doesn't seem to allow constructing such types, it requires the whole class to be abstract - in
  2605. * which case we are supposed to return the reabstracted interface method. But in IL we can make a
  2606. * non-abstract class with reabstracted interface methods - which is supposed to fail with an
  2607. * EntryPointNotFoundException at invoke time, but does not prevent the class from loading.)
  2608. */
  2609. MONO_HANDLE_ARRAY_SETREF (targets, i, NULL_HANDLE);
  2610. } else {
  2611. MONO_HANDLE_ASSIGN (member, mono_method_get_object_handle (domain, foundMethod, mono_class_is_interface (foundMethod->klass) ? foundMethod->klass : klass, error));
  2612. goto_if_nok (error, leave);
  2613. MONO_HANDLE_ARRAY_SETREF (targets, i, member);
  2614. }
  2615. leave:
  2616. HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
  2617. }
  2618. void
  2619. ves_icall_RuntimeType_GetInterfaceMapData (MonoReflectionTypeHandle ref_type, MonoReflectionTypeHandle ref_iface, MonoArrayHandleOut targets, MonoArrayHandleOut methods, MonoError *error)
  2620. {
  2621. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2622. MonoClass *klass = mono_class_from_mono_type_internal (type);
  2623. MonoType *iface = MONO_HANDLE_GETVAL (ref_iface, type);
  2624. MonoClass *iclass = mono_class_from_mono_type_internal (iface);
  2625. mono_class_init_checked (klass, error);
  2626. return_if_nok (error);
  2627. mono_class_init_checked (iclass, error);
  2628. return_if_nok (error);
  2629. mono_class_setup_vtable (klass);
  2630. gboolean variance_used;
  2631. int ioffset = mono_class_interface_offset_with_variance (klass, iclass, &variance_used);
  2632. if (ioffset == -1)
  2633. return;
  2634. MonoMethod* method;
  2635. int i = 0;
  2636. gpointer iter = NULL;
  2637. MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
  2638. while ((method = mono_class_get_methods(iclass, &iter))) {
  2639. if (method->flags & METHOD_ATTRIBUTE_VIRTUAL)
  2640. i++;
  2641. }
  2642. MonoArrayHandle targets_arr = mono_array_new_handle (domain, mono_defaults.method_info_class, i, error);
  2643. return_if_nok (error);
  2644. MONO_HANDLE_ASSIGN (targets, targets_arr);
  2645. MonoArrayHandle methods_arr = mono_array_new_handle (domain, mono_defaults.method_info_class, i, error);
  2646. return_if_nok (error);
  2647. MONO_HANDLE_ASSIGN (methods, methods_arr);
  2648. i = 0;
  2649. iter = NULL;
  2650. while ((method = mono_class_get_methods (iclass, &iter))) {
  2651. if (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL))
  2652. continue;
  2653. if (!set_interface_map_data_method_object (domain, method, iclass, ioffset, klass, targets, methods, i, error))
  2654. return;
  2655. i ++;
  2656. }
  2657. }
  2658. void
  2659. ves_icall_RuntimeType_GetPacking (MonoReflectionTypeHandle ref_type, guint32 *packing, guint32 *size, MonoError *error)
  2660. {
  2661. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2662. MonoClass *klass = mono_class_from_mono_type_internal (type);
  2663. mono_class_init_checked (klass, error);
  2664. return_if_nok (error);
  2665. if (image_is_dynamic (m_class_get_image (klass))) {
  2666. MonoReflectionTypeBuilderHandle tb = MONO_HANDLE_CAST (MonoReflectionTypeBuilder, ref_type);
  2667. *packing = MONO_HANDLE_GETVAL (tb, packing_size);
  2668. *size = MONO_HANDLE_GETVAL (tb, class_size);
  2669. } else {
  2670. mono_metadata_packing_from_typedef (m_class_get_image (klass), m_class_get_type_token (klass), packing, size);
  2671. }
  2672. }
  2673. MonoReflectionTypeHandle
  2674. ves_icall_RuntimeTypeHandle_GetElementType (MonoReflectionTypeHandle ref_type, MonoError *error)
  2675. {
  2676. MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
  2677. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2678. if (!type->byref && type->type == MONO_TYPE_SZARRAY) {
  2679. return mono_type_get_object_handle (domain, m_class_get_byval_arg (type->data.klass), error);
  2680. }
  2681. MonoClass *klass = mono_class_from_mono_type_internal (type);
  2682. mono_class_init_checked (klass, error);
  2683. return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
  2684. // GetElementType should only return a type for:
  2685. // Array Pointer PassedByRef
  2686. if (type->byref)
  2687. return mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
  2688. else if (m_class_get_element_class (klass) && MONO_CLASS_IS_ARRAY (klass))
  2689. return mono_type_get_object_handle (domain, m_class_get_byval_arg (m_class_get_element_class (klass)), error);
  2690. else if (m_class_get_element_class (klass) && type->type == MONO_TYPE_PTR)
  2691. return mono_type_get_object_handle (domain, m_class_get_byval_arg (m_class_get_element_class (klass)), error);
  2692. else
  2693. return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  2694. }
  2695. MonoReflectionTypeHandle
  2696. ves_icall_RuntimeTypeHandle_GetBaseType (MonoReflectionTypeHandle ref_type, MonoError *error)
  2697. {
  2698. MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
  2699. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2700. if (type->byref)
  2701. return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  2702. MonoClass *klass = mono_class_from_mono_type_internal (type);
  2703. if (!m_class_get_parent (klass))
  2704. return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  2705. return mono_type_get_object_handle (domain, m_class_get_byval_arg (m_class_get_parent (klass)), error);
  2706. }
  2707. guint32
  2708. ves_icall_RuntimeTypeHandle_GetCorElementType (MonoReflectionTypeHandle ref_type, MonoError *error)
  2709. {
  2710. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2711. if (type->byref)
  2712. return MONO_TYPE_BYREF;
  2713. else
  2714. return (guint32)type->type;
  2715. }
  2716. MonoBoolean
  2717. ves_icall_RuntimeTypeHandle_HasReferences (MonoReflectionTypeHandle ref_type, MonoError *error)
  2718. {
  2719. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2720. MonoClass *klass;
  2721. klass = mono_class_from_mono_type_internal (type);
  2722. mono_class_init_internal (klass);
  2723. return m_class_has_references (klass);
  2724. }
  2725. MonoBoolean
  2726. ves_icall_RuntimeTypeHandle_IsByRefLike (MonoReflectionTypeHandle ref_type, MonoError *error)
  2727. {
  2728. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2729. /* .NET Core says byref types are not IsByRefLike */
  2730. if (type->byref)
  2731. return FALSE;
  2732. MonoClass *klass = mono_class_from_mono_type_internal (type);
  2733. return m_class_is_byreflike (klass);
  2734. }
  2735. MonoBoolean
  2736. ves_icall_RuntimeTypeHandle_IsComObject (MonoReflectionTypeHandle ref_type, MonoError *error)
  2737. {
  2738. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2739. MonoClass *klass = mono_class_from_mono_type_internal (type);
  2740. mono_class_init_checked (klass, error);
  2741. return_val_if_nok (error, FALSE);
  2742. return mono_class_is_com_object (klass);
  2743. }
  2744. guint32
  2745. ves_icall_reflection_get_token (MonoObjectHandle obj, MonoError *error)
  2746. {
  2747. error_init (error);
  2748. return mono_reflection_get_token_checked (obj, error);
  2749. }
  2750. MonoReflectionModuleHandle
  2751. ves_icall_RuntimeTypeHandle_GetModule (MonoReflectionTypeHandle type, MonoError *error)
  2752. {
  2753. MonoDomain *domain = MONO_HANDLE_DOMAIN (type);
  2754. MonoType *t = MONO_HANDLE_GETVAL (type, type);
  2755. MonoClass *klass = mono_class_from_mono_type_internal (t);
  2756. return mono_module_get_object_handle (domain, m_class_get_image (klass), error);
  2757. }
  2758. MonoReflectionAssemblyHandle
  2759. ves_icall_RuntimeTypeHandle_GetAssembly (MonoReflectionTypeHandle type, MonoError *error)
  2760. {
  2761. MonoDomain *domain = mono_domain_get ();
  2762. MonoType *t = MONO_HANDLE_GETVAL (type, type);
  2763. MonoClass *klass = mono_class_from_mono_type_internal (t);
  2764. return mono_assembly_get_object_handle (domain, m_class_get_image (klass)->assembly, error);
  2765. }
  2766. MonoReflectionTypeHandle
  2767. ves_icall_RuntimeType_get_DeclaringType (MonoReflectionTypeHandle ref_type, MonoError *error)
  2768. {
  2769. MonoDomain *domain = mono_domain_get ();
  2770. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2771. MonoClass *klass;
  2772. if (type->byref)
  2773. return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  2774. if (type->type == MONO_TYPE_VAR) {
  2775. MonoGenericContainer *param = mono_type_get_generic_param_owner (type);
  2776. klass = param ? param->owner.klass : NULL;
  2777. } else if (type->type == MONO_TYPE_MVAR) {
  2778. MonoGenericContainer *param = mono_type_get_generic_param_owner (type);
  2779. klass = param ? param->owner.method->klass : NULL;
  2780. } else {
  2781. klass = m_class_get_nested_in (mono_class_from_mono_type_internal (type));
  2782. }
  2783. if (!klass)
  2784. return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  2785. return mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
  2786. }
  2787. MonoStringHandle
  2788. ves_icall_RuntimeType_get_Name (MonoReflectionTypeHandle reftype, MonoError *error)
  2789. {
  2790. MonoDomain *domain = mono_domain_get ();
  2791. MonoType *type = MONO_HANDLE_RAW(reftype)->type;
  2792. MonoClass *klass = mono_class_from_mono_type_internal (type);
  2793. // FIXME: this should be escaped in some scenarios with mono_identifier_escape_type_name_chars
  2794. // Determining exactly when to do so is fairly difficult, so for now we don't bother to avoid regressions
  2795. const char *klass_name = m_class_get_name (klass);
  2796. if (type->byref) {
  2797. char *n = g_strdup_printf ("%s&", klass_name);
  2798. MonoStringHandle res = mono_string_new_handle (domain, n, error);
  2799. g_free (n);
  2800. return res;
  2801. } else {
  2802. return mono_string_new_handle (domain, klass_name, error);
  2803. }
  2804. }
  2805. MonoStringHandle
  2806. ves_icall_RuntimeType_get_Namespace (MonoReflectionTypeHandle type, MonoError *error)
  2807. {
  2808. MonoDomain *domain = mono_domain_get ();
  2809. MonoClass *klass = mono_class_from_mono_type_handle (type);
  2810. MonoClass *klass_nested_in;
  2811. while ((klass_nested_in = m_class_get_nested_in (klass)))
  2812. klass = klass_nested_in;
  2813. if (m_class_get_name_space (klass) [0] == '\0')
  2814. return NULL_HANDLE_STRING;
  2815. char *escaped = mono_identifier_escape_type_name_chars (m_class_get_name_space (klass));
  2816. MonoStringHandle res = mono_string_new_handle (domain, escaped, error);
  2817. g_free (escaped);
  2818. return res;
  2819. }
  2820. gint32
  2821. ves_icall_RuntimeTypeHandle_GetArrayRank (MonoReflectionTypeHandle ref_type, MonoError *error)
  2822. {
  2823. error_init (error);
  2824. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2825. if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
  2826. mono_error_set_argument (error, "type", "Type must be an array type");
  2827. return 0;
  2828. }
  2829. MonoClass *klass = mono_class_from_mono_type_internal (type);
  2830. return m_class_get_rank (klass);
  2831. }
  2832. static MonoArrayHandle
  2833. create_type_array (MonoDomain *domain, MonoBoolean runtimeTypeArray, int count, MonoError *error)
  2834. {
  2835. return mono_array_new_handle (domain, runtimeTypeArray ? mono_defaults.runtimetype_class : mono_defaults.systemtype_class, count, error);
  2836. }
  2837. static gboolean
  2838. set_type_object_in_array (MonoDomain *domain, MonoType *type, MonoArrayHandle dest, int i, MonoError *error)
  2839. {
  2840. HANDLE_FUNCTION_ENTER();
  2841. error_init (error);
  2842. MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, type, error);
  2843. goto_if_nok (error, leave);
  2844. MONO_HANDLE_ARRAY_SETREF (dest, i, rt);
  2845. leave:
  2846. HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
  2847. }
  2848. #ifndef ENABLE_NETCORE
  2849. void
  2850. ves_icall_RuntimeType_GetGUID (MonoReflectionTypeHandle type_handle, MonoArrayHandle guid_handle, MonoError *error)
  2851. {
  2852. error_init (error);
  2853. g_assert (mono_array_handle_length (guid_handle) == 16);
  2854. if (MONO_HANDLE_IS_NULL (type_handle)) {
  2855. mono_error_set_argument_null (error, "type", "");
  2856. return;
  2857. }
  2858. MonoType *type = MONO_HANDLE_GETVAL (type_handle, type);
  2859. MonoClass *klass = mono_class_from_mono_type_internal (type);
  2860. if (!mono_class_init_checked (klass, error))
  2861. return;
  2862. guint8 *data = (guint8*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_handle), 1, 0);
  2863. mono_metadata_get_class_guid (klass, data, error);
  2864. }
  2865. #endif
  2866. MonoArrayHandle
  2867. ves_icall_RuntimeType_GetGenericArguments (MonoReflectionTypeHandle ref_type, MonoBoolean runtimeTypeArray, MonoError *error)
  2868. {
  2869. error_init (error);
  2870. MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
  2871. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2872. MonoClass *klass = mono_class_from_mono_type_internal (type);
  2873. MonoArrayHandle res = MONO_HANDLE_NEW (MonoArray, NULL);
  2874. if (mono_class_is_gtd (klass)) {
  2875. MonoGenericContainer *container = mono_class_get_generic_container (klass);
  2876. MONO_HANDLE_ASSIGN (res, create_type_array (domain, runtimeTypeArray, container->type_argc, error));
  2877. goto_if_nok (error, leave);
  2878. for (int i = 0; i < container->type_argc; ++i) {
  2879. MonoClass *pklass = mono_class_create_generic_parameter (mono_generic_container_get_param (container, i));
  2880. if (!set_type_object_in_array (domain, m_class_get_byval_arg (pklass), res, i, error))
  2881. goto leave;
  2882. }
  2883. } else if (mono_class_is_ginst (klass)) {
  2884. MonoGenericInst *inst = mono_class_get_generic_class (klass)->context.class_inst;
  2885. MONO_HANDLE_ASSIGN (res, create_type_array (domain, runtimeTypeArray, inst->type_argc, error));
  2886. goto_if_nok (error, leave);
  2887. for (int i = 0; i < inst->type_argc; ++i) {
  2888. if (!set_type_object_in_array (domain, inst->type_argv [i], res, i, error))
  2889. goto leave;
  2890. }
  2891. }
  2892. leave:
  2893. return res;
  2894. }
  2895. MonoBoolean
  2896. ves_icall_RuntimeTypeHandle_IsGenericTypeDefinition (MonoReflectionTypeHandle ref_type, MonoError *error)
  2897. {
  2898. error_init (error);
  2899. if (!IS_MONOTYPE (MONO_HANDLE_RAW(ref_type)))
  2900. return FALSE;
  2901. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2902. if (type->byref)
  2903. return FALSE;
  2904. MonoClass *klass = mono_class_from_mono_type_internal (type);
  2905. return mono_class_is_gtd (klass);
  2906. }
  2907. MonoReflectionTypeHandle
  2908. ves_icall_RuntimeTypeHandle_GetGenericTypeDefinition_impl (MonoReflectionTypeHandle ref_type, MonoError *error)
  2909. {
  2910. error_init (error);
  2911. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2912. MonoReflectionTypeHandle ret = MONO_HANDLE_NEW (MonoReflectionType, NULL);
  2913. if (type->byref)
  2914. goto leave;
  2915. MonoClass *klass;
  2916. klass = mono_class_from_mono_type_internal (type);
  2917. if (mono_class_is_gtd (klass)) {
  2918. /* check this one */
  2919. MONO_HANDLE_ASSIGN (ret, ref_type);
  2920. goto leave;
  2921. }
  2922. if (mono_class_is_ginst (klass)) {
  2923. MonoClass *generic_class = mono_class_get_generic_class (klass)->container_class;
  2924. MonoGCHandle ref_info_handle = mono_class_get_ref_info_handle (generic_class);
  2925. if (m_class_was_typebuilder (generic_class) && ref_info_handle) {
  2926. MonoObjectHandle tb = mono_gchandle_get_target_handle (ref_info_handle);
  2927. g_assert (!MONO_HANDLE_IS_NULL (tb));
  2928. MONO_HANDLE_ASSIGN (ret, tb);
  2929. } else {
  2930. MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
  2931. MONO_HANDLE_ASSIGN (ret, mono_type_get_object_handle (domain, m_class_get_byval_arg (generic_class), error));
  2932. }
  2933. }
  2934. leave:
  2935. return ret;
  2936. }
  2937. MonoReflectionTypeHandle
  2938. ves_icall_RuntimeType_MakeGenericType (MonoReflectionTypeHandle reftype, MonoArrayHandle type_array, MonoError *error)
  2939. {
  2940. error_init (error);
  2941. MonoDomain *domain = MONO_HANDLE_DOMAIN (reftype);
  2942. g_assert (IS_MONOTYPE_HANDLE (reftype));
  2943. MonoType *type = MONO_HANDLE_GETVAL (reftype, type);
  2944. mono_class_init_checked (mono_class_from_mono_type_internal (type), error);
  2945. return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
  2946. int count = mono_array_handle_length (type_array);
  2947. MonoType **types = g_new0 (MonoType *, count);
  2948. MonoReflectionTypeHandle t = MONO_HANDLE_NEW (MonoReflectionType, NULL);
  2949. for (int i = 0; i < count; i++) {
  2950. MONO_HANDLE_ARRAY_GETREF (t, type_array, i);
  2951. types [i] = MONO_HANDLE_GETVAL (t, type);
  2952. }
  2953. MonoType *geninst = mono_reflection_bind_generic_parameters (reftype, count, types, error);
  2954. g_free (types);
  2955. if (!geninst) {
  2956. return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  2957. }
  2958. MonoClass *klass = mono_class_from_mono_type_internal (geninst);
  2959. /*we might inflate to the GTD*/
  2960. if (mono_class_is_ginst (klass) && !mono_verifier_class_is_valid_generic_instantiation (klass)) {
  2961. mono_error_set_argument (error, "typeArguments", "Invalid generic arguments");
  2962. return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  2963. }
  2964. return mono_type_get_object_handle (domain, geninst, error);
  2965. }
  2966. MonoBoolean
  2967. ves_icall_RuntimeTypeHandle_HasInstantiation (MonoReflectionTypeHandle ref_type, MonoError *error)
  2968. {
  2969. error_init (error);
  2970. MonoClass *klass;
  2971. if (!IS_MONOTYPE (MONO_HANDLE_RAW (ref_type)))
  2972. return FALSE;
  2973. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2974. if (type->byref)
  2975. return FALSE;
  2976. klass = mono_class_from_mono_type_internal (type);
  2977. return mono_class_is_ginst (klass) || mono_class_is_gtd (klass);
  2978. }
  2979. gint32
  2980. ves_icall_RuntimeType_GetGenericParameterPosition (MonoReflectionTypeHandle ref_type, MonoError *error)
  2981. {
  2982. error_init (error);
  2983. if (!IS_MONOTYPE_HANDLE (ref_type))
  2984. return -1;
  2985. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2986. if (is_generic_parameter (type))
  2987. return mono_type_get_generic_param_num (type);
  2988. return -1;
  2989. }
  2990. MonoGenericParamInfo *
  2991. ves_icall_RuntimeTypeHandle_GetGenericParameterInfo (MonoReflectionTypeHandle ref_type, MonoError *error)
  2992. {
  2993. error_init (error);
  2994. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  2995. return mono_generic_param_info (type->data.generic_param);
  2996. }
  2997. MonoBoolean
  2998. ves_icall_RuntimeTypeHandle_IsGenericVariable (MonoReflectionTypeHandle ref_type, MonoError *error)
  2999. {
  3000. MonoType *type = MONO_HANDLE_GETVAL(ref_type, type);
  3001. return is_generic_parameter (type);
  3002. }
  3003. MonoReflectionMethodHandle
  3004. ves_icall_RuntimeType_GetCorrespondingInflatedMethod (MonoReflectionTypeHandle ref_type,
  3005. MonoReflectionMethodHandle generic,
  3006. MonoError *error)
  3007. {
  3008. error_init (error);
  3009. MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
  3010. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  3011. MonoClass *klass = mono_class_from_mono_type_internal (type);
  3012. mono_class_init_checked (klass, error);
  3013. return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
  3014. MonoMethod *generic_method = MONO_HANDLE_GETVAL (generic, method);
  3015. MonoReflectionMethodHandle ret = MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
  3016. MonoMethod *method;
  3017. gpointer iter = NULL;
  3018. while ((method = mono_class_get_methods (klass, &iter))) {
  3019. if (method->token == generic_method->token) {
  3020. ret = mono_method_get_object_handle (domain, method, klass, error);
  3021. return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
  3022. }
  3023. }
  3024. return ret;
  3025. }
  3026. MonoReflectionMethodHandle
  3027. ves_icall_RuntimeType_get_DeclaringMethod (MonoReflectionTypeHandle ref_type, MonoError *error)
  3028. {
  3029. error_init (error);
  3030. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  3031. MonoReflectionMethodHandle ret = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
  3032. if (type->byref || (type->type != MONO_TYPE_MVAR && type->type != MONO_TYPE_VAR)) {
  3033. mono_error_set_invalid_operation (error, "DeclaringMethod can only be used on generic arguments");
  3034. goto leave;
  3035. }
  3036. if (type->type == MONO_TYPE_VAR)
  3037. goto leave;
  3038. MonoMethod *method;
  3039. method = mono_type_get_generic_param_owner (type)->owner.method;
  3040. g_assert (method);
  3041. MonoDomain *domain;
  3042. domain = MONO_HANDLE_DOMAIN (ref_type);
  3043. MONO_HANDLE_ASSIGN (ret, mono_method_get_object_handle (domain, method, method->klass, error));
  3044. leave:
  3045. return ret;
  3046. }
  3047. #ifndef ENABLE_NETCORE
  3048. MonoBoolean
  3049. ves_icall_System_RuntimeType_IsTypeExportedToWindowsRuntime (MonoError *error)
  3050. {
  3051. error_init (error);
  3052. mono_error_set_not_implemented (error, "%s", "System.RuntimeType.IsTypeExportedToWindowsRuntime");
  3053. return FALSE;
  3054. }
  3055. MonoBoolean
  3056. ves_icall_System_RuntimeType_IsWindowsRuntimeObjectType (MonoError *error)
  3057. {
  3058. error_init (error);
  3059. mono_error_set_not_implemented (error, "%s", "System.RuntimeType.IsWindowsRuntimeObjectType");
  3060. return FALSE;
  3061. }
  3062. #endif /* ENABLE_NETCORE */
  3063. void
  3064. ves_icall_RuntimeMethodInfo_GetPInvoke (MonoReflectionMethodHandle ref_method, int* flags, MonoStringHandleOut entry_point, MonoStringHandleOut dll_name, MonoError *error)
  3065. {
  3066. MonoDomain *domain = mono_domain_get ();
  3067. MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
  3068. MonoImage *image = m_class_get_image (method->klass);
  3069. MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
  3070. MonoTableInfo *tables = image->tables;
  3071. MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
  3072. MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
  3073. guint32 im_cols [MONO_IMPLMAP_SIZE];
  3074. guint32 scope_token;
  3075. const char *import = NULL;
  3076. const char *scope = NULL;
  3077. error_init (error);
  3078. if (image_is_dynamic (image)) {
  3079. MonoReflectionMethodAux *method_aux =
  3080. (MonoReflectionMethodAux *)g_hash_table_lookup (((MonoDynamicImage*)image)->method_aux_hash, method);
  3081. if (method_aux) {
  3082. import = method_aux->dllentry;
  3083. scope = method_aux->dll;
  3084. }
  3085. if (!import || !scope) {
  3086. mono_error_set_argument (error, "method", "System.Refleciton.Emit method with invalid pinvoke information");
  3087. return;
  3088. }
  3089. }
  3090. else {
  3091. if (piinfo->implmap_idx) {
  3092. mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
  3093. piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
  3094. import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
  3095. scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
  3096. scope = mono_metadata_string_heap (image, scope_token);
  3097. }
  3098. }
  3099. *flags = piinfo->piflags;
  3100. MONO_HANDLE_ASSIGN (entry_point, mono_string_new_handle (domain, import, error));
  3101. return_if_nok (error);
  3102. MONO_HANDLE_ASSIGN (dll_name, mono_string_new_handle (domain, scope, error));
  3103. }
  3104. MonoReflectionMethodHandle
  3105. ves_icall_RuntimeMethodInfo_GetGenericMethodDefinition (MonoReflectionMethodHandle ref_method, MonoError *error)
  3106. {
  3107. error_init (error);
  3108. MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
  3109. if (method->is_generic)
  3110. return ref_method;
  3111. if (!method->is_inflated)
  3112. return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
  3113. MonoMethodInflated *imethod = (MonoMethodInflated *) method;
  3114. MonoMethod *result = imethod->declaring;
  3115. /* Not a generic method. */
  3116. if (!result->is_generic)
  3117. return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
  3118. if (image_is_dynamic (m_class_get_image (method->klass))) {
  3119. MonoDynamicImage *image = (MonoDynamicImage*)m_class_get_image (method->klass);
  3120. /*
  3121. * FIXME: Why is this stuff needed at all ? Why can't the code below work for
  3122. * the dynamic case as well ?
  3123. */
  3124. mono_image_lock ((MonoImage*)image);
  3125. MonoReflectionMethodHandle res = MONO_HANDLE_NEW (MonoReflectionMethod, (MonoReflectionMethod*)mono_g_hash_table_lookup (image->generic_def_objects, imethod));
  3126. mono_image_unlock ((MonoImage*)image);
  3127. if (!MONO_HANDLE_IS_NULL (res))
  3128. return res;
  3129. }
  3130. if (imethod->context.class_inst) {
  3131. MonoClass *klass = ((MonoMethod *) imethod)->klass;
  3132. /*Generic methods gets the context of the GTD.*/
  3133. if (mono_class_get_context (klass)) {
  3134. result = mono_class_inflate_generic_method_full_checked (result, klass, mono_class_get_context (klass), error);
  3135. return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
  3136. }
  3137. }
  3138. return mono_method_get_object_handle (MONO_HANDLE_DOMAIN (ref_method), result, NULL, error);
  3139. }
  3140. #ifdef ENABLE_NETCORE
  3141. static GENERATE_TRY_GET_CLASS_WITH_CACHE (stream, "System.IO", "Stream")
  3142. static int io_stream_begin_read_slot = -1;
  3143. static int io_stream_begin_write_slot = -1;
  3144. static int io_stream_end_read_slot = -1;
  3145. static int io_stream_end_write_slot = -1;
  3146. static gboolean io_stream_slots_set = FALSE;
  3147. static void
  3148. init_io_stream_slots (void)
  3149. {
  3150. MonoClass* klass = mono_class_try_get_stream_class ();
  3151. mono_class_setup_vtable (klass);
  3152. MonoMethod **klass_methods = m_class_get_methods (klass);
  3153. if (!klass_methods) {
  3154. mono_class_setup_methods (klass);
  3155. klass_methods = m_class_get_methods (klass);
  3156. }
  3157. int method_count = mono_class_get_method_count (klass);
  3158. int methods_found = 0;
  3159. for (int i = 0; i < method_count; i++) {
  3160. // find slots for Begin(End)Read and Begin(End)Write
  3161. MonoMethod* m = klass_methods [i];
  3162. if (m->slot == -1)
  3163. continue;
  3164. if (!strcmp (m->name, "BeginRead")) {
  3165. methods_found++;
  3166. io_stream_begin_read_slot = m->slot;
  3167. } else if (!strcmp (m->name, "BeginWrite")) {
  3168. methods_found++;
  3169. io_stream_begin_write_slot = m->slot;
  3170. } else if (!strcmp (m->name, "EndRead")) {
  3171. methods_found++;
  3172. io_stream_end_read_slot = m->slot;
  3173. } else if (!strcmp (m->name, "EndWrite")) {
  3174. methods_found++;
  3175. io_stream_end_write_slot = m->slot;
  3176. }
  3177. }
  3178. g_assert (methods_found <= 4); // some of them can be linked out
  3179. io_stream_slots_set = TRUE;
  3180. }
  3181. MonoBoolean
  3182. ves_icall_System_IO_Stream_HasOverriddenBeginEndRead (MonoObjectHandle stream, MonoError *error)
  3183. {
  3184. MonoClass* curr_klass = MONO_HANDLE_GET_CLASS (stream);
  3185. MonoClass* base_klass = mono_class_try_get_stream_class ();
  3186. if (!io_stream_slots_set)
  3187. init_io_stream_slots ();
  3188. // slots can still be -1 and it means Linker removed the methods from the base class (Stream)
  3189. // in this case we can safely assume the methods are not overridden
  3190. // otherwise - check vtable
  3191. MonoMethod **curr_klass_vtable = m_class_get_vtable (curr_klass);
  3192. gboolean begin_read_is_overriden = io_stream_begin_read_slot != -1 && curr_klass_vtable [io_stream_begin_read_slot]->klass != base_klass;
  3193. gboolean end_read_is_overriden = io_stream_end_read_slot != -1 && curr_klass_vtable [io_stream_end_read_slot]->klass != base_klass;
  3194. // return true if BeginRead or EndRead were overriden
  3195. return begin_read_is_overriden || end_read_is_overriden;
  3196. }
  3197. MonoBoolean
  3198. ves_icall_System_IO_Stream_HasOverriddenBeginEndWrite (MonoObjectHandle stream, MonoError *error)
  3199. {
  3200. MonoClass* curr_klass = MONO_HANDLE_GETVAL (stream, vtable)->klass;
  3201. MonoClass* base_klass = mono_class_try_get_stream_class ();
  3202. if (!io_stream_slots_set)
  3203. init_io_stream_slots ();
  3204. MonoMethod **curr_klass_vtable = m_class_get_vtable (curr_klass);
  3205. gboolean begin_write_is_overriden = curr_klass_vtable [io_stream_begin_write_slot]->klass != base_klass;
  3206. gboolean end_write_is_overriden = curr_klass_vtable [io_stream_end_write_slot]->klass != base_klass;
  3207. // return true if BeginWrite or EndWrite were overriden
  3208. return begin_write_is_overriden || end_write_is_overriden;
  3209. }
  3210. #endif
  3211. MonoBoolean
  3212. ves_icall_RuntimeMethodInfo_get_IsGenericMethod (MonoReflectionMethodHandle ref_method, MonoError *erro)
  3213. {
  3214. MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
  3215. return mono_method_signature_internal (method)->generic_param_count != 0;
  3216. }
  3217. MonoBoolean
  3218. ves_icall_RuntimeMethodInfo_get_IsGenericMethodDefinition (MonoReflectionMethodHandle ref_method, MonoError *Error)
  3219. {
  3220. MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
  3221. return method->is_generic;
  3222. }
  3223. static gboolean
  3224. set_array_generic_argument_handle_inflated (MonoDomain *domain, MonoGenericInst *inst, int i, MonoArrayHandle arr, MonoError *error)
  3225. {
  3226. HANDLE_FUNCTION_ENTER ();
  3227. error_init (error);
  3228. MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, inst->type_argv [i], error);
  3229. goto_if_nok (error, leave);
  3230. MONO_HANDLE_ARRAY_SETREF (arr, i, rt);
  3231. leave:
  3232. HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
  3233. }
  3234. static gboolean
  3235. set_array_generic_argument_handle_gparam (MonoDomain *domain, MonoGenericContainer *container, int i, MonoArrayHandle arr, MonoError *error)
  3236. {
  3237. HANDLE_FUNCTION_ENTER ();
  3238. error_init (error);
  3239. MonoGenericParam *param = mono_generic_container_get_param (container, i);
  3240. MonoClass *pklass = mono_class_create_generic_parameter (param);
  3241. MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (pklass), error);
  3242. goto_if_nok (error, leave);
  3243. MONO_HANDLE_ARRAY_SETREF (arr, i, rt);
  3244. leave:
  3245. HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
  3246. }
  3247. MonoArrayHandle
  3248. ves_icall_RuntimeMethodInfo_GetGenericArguments (MonoReflectionMethodHandle ref_method, MonoError *error)
  3249. {
  3250. error_init (error);
  3251. MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_method);
  3252. MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
  3253. if (method->is_inflated) {
  3254. MonoGenericInst *inst = mono_method_get_context (method)->method_inst;
  3255. if (inst) {
  3256. int count = inst->type_argc;
  3257. MonoArrayHandle res = mono_array_new_handle (domain, mono_defaults.systemtype_class, count, error);
  3258. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  3259. for (int i = 0; i < count; i++) {
  3260. if (!set_array_generic_argument_handle_inflated (domain, inst, i, res, error))
  3261. break;
  3262. }
  3263. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  3264. return res;
  3265. }
  3266. }
  3267. int count = mono_method_signature_internal (method)->generic_param_count;
  3268. MonoArrayHandle res = mono_array_new_handle (domain, mono_defaults.systemtype_class, count, error);
  3269. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  3270. MonoGenericContainer *container = mono_method_get_generic_container (method);
  3271. for (int i = 0; i < count; i++) {
  3272. if (!set_array_generic_argument_handle_gparam (domain, container, i, res, error))
  3273. break;
  3274. }
  3275. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  3276. return res;
  3277. }
  3278. MonoObjectHandle
  3279. ves_icall_InternalInvoke (MonoReflectionMethodHandle method_handle, MonoObjectHandle this_arg_handle,
  3280. MonoArrayHandle params_handle, MonoExceptionHandleOut exception_out, MonoError *error)
  3281. {
  3282. MonoReflectionMethod* const method = MONO_HANDLE_RAW (method_handle);
  3283. MonoObject* const this_arg = MONO_HANDLE_RAW (this_arg_handle);
  3284. MonoArray* const params = MONO_HANDLE_RAW (params_handle);
  3285. /*
  3286. * Invoke from reflection is supposed to always be a virtual call (the API
  3287. * is stupid), mono_runtime_invoke_*() calls the provided method, allowing
  3288. * greater flexibility.
  3289. */
  3290. MonoMethod *m = method->method;
  3291. MonoMethodSignature* const sig = mono_method_signature_internal (m);
  3292. MonoImage *image = NULL;
  3293. int pcount = 0;
  3294. void *obj = this_arg;
  3295. char *this_name = NULL;
  3296. char *target_name = NULL;
  3297. char *msg = NULL;
  3298. MonoObject *result = NULL;
  3299. MonoArray *arr = NULL;
  3300. MonoException *exception = NULL;
  3301. *MONO_HANDLE_REF (exception_out) = NULL;
  3302. if (mono_security_core_clr_enabled () &&
  3303. !mono_security_core_clr_ensure_reflection_access_method (m, error)) {
  3304. goto return_null;
  3305. }
  3306. if (!(m->flags & METHOD_ATTRIBUTE_STATIC)) {
  3307. if (!mono_class_vtable_checked (mono_object_domain (method), m->klass, error)) {
  3308. mono_error_cleanup (error); /* FIXME does this make sense? */
  3309. error_init_reuse (error);
  3310. exception = mono_class_get_exception_for_failure (m->klass);
  3311. goto return_null;
  3312. }
  3313. if (this_arg) {
  3314. if (!mono_object_isinst_checked (this_arg, m->klass, error)) {
  3315. if (!is_ok (error)) {
  3316. exception = mono_error_convert_to_exception (error);
  3317. goto return_null;
  3318. }
  3319. this_name = mono_type_get_full_name (mono_object_class (this_arg));
  3320. target_name = mono_type_get_full_name (m->klass);
  3321. msg = g_strdup_printf ("Object of type '%s' doesn't match target type '%s'", this_name, target_name);
  3322. exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Reflection", "TargetException", msg);
  3323. goto return_null;
  3324. }
  3325. m = mono_object_get_virtual_method_internal (this_arg, m);
  3326. /* must pass the pointer to the value for valuetype methods */
  3327. if (m_class_is_valuetype (m->klass)) {
  3328. obj = mono_object_unbox_internal (this_arg);
  3329. // FIXMEcoop? Does obj need to be put into a handle?
  3330. }
  3331. } else if (strcmp (m->name, ".ctor") && !m->wrapper_type) {
  3332. exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Reflection", "TargetException", "Non-static method requires a target.");
  3333. goto return_null;
  3334. }
  3335. }
  3336. if ((m->klass != NULL && m_class_is_byreflike (m->klass)) || m_class_is_byreflike (mono_class_from_mono_type_internal (sig->ret))) {
  3337. exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "NotSupportedException", "Cannot invoke method with stack pointers via reflection");
  3338. goto return_null;
  3339. }
  3340. #if ENABLE_NETCORE
  3341. if (sig->ret->byref) {
  3342. MonoType* ret_byval = m_class_get_byval_arg (mono_class_from_mono_type_internal (sig->ret));
  3343. if (ret_byval->type == MONO_TYPE_VOID) {
  3344. exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "NotSupportedException", "ByRef to void return values are not supported in reflection invocation");
  3345. goto return_null;
  3346. }
  3347. if (m_class_is_byreflike (mono_class_from_mono_type_internal (ret_byval))) {
  3348. exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "NotSupportedException", "Cannot invoke method returning ByRef to ByRefLike type via reflection");
  3349. goto return_null;
  3350. }
  3351. }
  3352. #else
  3353. if (sig->ret->byref) {
  3354. exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "NotSupportedException", "Cannot invoke method returning ByRef type via reflection");
  3355. goto return_null;
  3356. }
  3357. #endif
  3358. pcount = params? mono_array_length_internal (params): 0;
  3359. if (pcount != sig->param_count) {
  3360. exception = mono_exception_from_name (mono_defaults.corlib, "System.Reflection", "TargetParameterCountException");
  3361. goto return_null;
  3362. }
  3363. if (mono_class_is_abstract (m->klass) && !strcmp (m->name, ".ctor") && !this_arg) {
  3364. exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Reflection", "TargetException", "Cannot invoke constructor of an abstract class.");
  3365. goto return_null;
  3366. }
  3367. image = m_class_get_image (m->klass);
  3368. if (mono_asmctx_get_kind (&image->assembly->context) == MONO_ASMCTX_REFONLY) {
  3369. exception = mono_get_exception_invalid_operation ("It is illegal to invoke a method on a type loaded using the ReflectionOnly api.");
  3370. goto return_null;
  3371. }
  3372. if (image_is_dynamic (image) && !((MonoDynamicImage*)image)->run) {
  3373. exception = mono_get_exception_not_supported ("Cannot invoke a method in a dynamic assembly without run access.");
  3374. goto return_null;
  3375. }
  3376. if (m_class_get_rank (m->klass) && !strcmp (m->name, ".ctor")) {
  3377. int i;
  3378. pcount = mono_array_length_internal (params);
  3379. uintptr_t * const lengths = g_newa (uintptr_t, pcount);
  3380. /* Note: the synthetized array .ctors have int32 as argument type */
  3381. for (i = 0; i < pcount; ++i)
  3382. lengths [i] = *(int32_t*) ((char*)mono_array_get_internal (params, gpointer, i) + sizeof (MonoObject));
  3383. if (m_class_get_rank (m->klass) == 1 && sig->param_count == 2 && m_class_get_rank (m_class_get_element_class (m->klass))) {
  3384. /* This is a ctor for jagged arrays. MS creates an array of arrays. */
  3385. arr = mono_array_new_full_checked (mono_object_domain (params), m->klass, lengths, NULL, error);
  3386. goto_if_nok (error, return_null);
  3387. MonoArrayHandle subarray_handle = MONO_HANDLE_NEW (MonoArray, NULL);
  3388. for (i = 0; i < mono_array_length_internal (arr); ++i) {
  3389. MonoArray *subarray = mono_array_new_full_checked (mono_object_domain (params), m_class_get_element_class (m->klass), &lengths [1], NULL, error);
  3390. goto_if_nok (error, return_null);
  3391. MONO_HANDLE_ASSIGN_RAW (subarray_handle, subarray); // FIXME? Overkill?
  3392. mono_array_setref_fast (arr, i, subarray);
  3393. }
  3394. goto exit;
  3395. }
  3396. if (m_class_get_rank (m->klass) == pcount) {
  3397. /* Only lengths provided. */
  3398. arr = mono_array_new_full_checked (mono_object_domain (params), m->klass, lengths, NULL, error);
  3399. goto_if_nok (error, return_null);
  3400. goto exit;
  3401. } else {
  3402. g_assert (pcount == (m_class_get_rank (m->klass) * 2));
  3403. /* The arguments are lower-bound-length pairs */
  3404. intptr_t * const lower_bounds = (intptr_t *)g_alloca (sizeof (intptr_t) * pcount);
  3405. for (i = 0; i < pcount / 2; ++i) {
  3406. lower_bounds [i] = *(int32_t*) ((char*)mono_array_get_internal (params, gpointer, (i * 2)) + sizeof (MonoObject));
  3407. lengths [i] = *(int32_t*) ((char*)mono_array_get_internal (params, gpointer, (i * 2) + 1) + sizeof (MonoObject));
  3408. }
  3409. arr = mono_array_new_full_checked (mono_object_domain (params), m->klass, lengths, lower_bounds, error);
  3410. goto_if_nok (error, return_null);
  3411. goto exit;
  3412. }
  3413. }
  3414. result = mono_runtime_invoke_array_checked (m, obj, params, error);
  3415. goto exit;
  3416. return_null:
  3417. result = NULL;
  3418. arr = NULL;
  3419. exit:
  3420. if (exception) {
  3421. MONO_HANDLE_NEW (MonoException, exception); // FIXME? overkill?
  3422. mono_gc_wbarrier_generic_store_internal (MONO_HANDLE_REF (exception_out), (MonoObject*)exception);
  3423. }
  3424. g_free (target_name);
  3425. g_free (this_name);
  3426. g_free (msg);
  3427. g_assert (!result || !arr); // only one, or neither, should be set
  3428. return result ? MONO_HANDLE_NEW (MonoObject, result) : arr ? MONO_HANDLE_NEW (MonoObject, (MonoObject*)arr) : NULL_HANDLE;
  3429. }
  3430. #ifndef DISABLE_REMOTING
  3431. static void
  3432. internal_execute_field_getter (MonoDomain *domain, MonoObject *this_arg, MonoArray *params, MonoArrayHandleOut outArgs, MonoError *error)
  3433. {
  3434. MonoArray *out_args;
  3435. MonoClass *k = mono_object_class (this_arg);
  3436. MonoString *name;
  3437. char *str;
  3438. // FIXME Refactor/inline internal_execute_field_setter and internal_execute_field_getter.
  3439. /* If this is a proxy, then it must be a CBO */
  3440. if (mono_class_is_transparent_proxy (k)) {
  3441. MonoTransparentProxy *tp = (MonoTransparentProxy*) this_arg;
  3442. this_arg = tp->rp->unwrapped_server;
  3443. MONO_HANDLE_NEW (MonoObject, this_arg);
  3444. g_assert (this_arg);
  3445. k = mono_object_class (this_arg);
  3446. }
  3447. name = mono_array_get_internal (params, MonoString *, 1);
  3448. MONO_HANDLE_NEW (MonoString, name);
  3449. str = mono_string_to_utf8_checked_internal (name, error);
  3450. return_if_nok (error);
  3451. do {
  3452. MonoClassField* field = mono_class_get_field_from_name_full (k, str, NULL);
  3453. if (field) {
  3454. g_free (str);
  3455. MonoClass *field_klass = mono_class_from_mono_type_internal (field->type);
  3456. MonoObject *result;
  3457. if (m_class_is_valuetype (field_klass)) {
  3458. result = mono_value_box_checked (domain, field_klass, (char *)this_arg + field->offset, error);
  3459. return_if_nok (error);
  3460. } else
  3461. result = (MonoObject *)*((gpointer *)((char *)this_arg + field->offset));
  3462. MONO_HANDLE_NEW (MonoObject, result);
  3463. out_args = mono_array_new_checked (domain, mono_defaults.object_class, 1, error);
  3464. return_if_nok (error);
  3465. MONO_HANDLE_NEW (MonoArray, out_args); // FIXME? overkill?
  3466. mono_gc_wbarrier_generic_store_internal (MONO_HANDLE_REF (outArgs), (MonoObject*) out_args);
  3467. mono_array_setref_internal (out_args, 0, result);
  3468. return;
  3469. }
  3470. k = m_class_get_parent (k);
  3471. } while (k);
  3472. g_free (str);
  3473. g_assert_not_reached ();
  3474. }
  3475. static void
  3476. internal_execute_field_setter (MonoDomain *domain, MonoObject *this_arg, MonoArray *params, MonoArrayHandleOut outArgs, MonoError *error)
  3477. {
  3478. MonoArray *out_args;
  3479. MonoClass *k = mono_object_class (this_arg);
  3480. MonoString *name;
  3481. guint32 size;
  3482. gint32 align;
  3483. char *str;
  3484. /* If this is a proxy, then it must be a CBO */
  3485. if (mono_class_is_transparent_proxy (k)) {
  3486. MonoTransparentProxy *tp = (MonoTransparentProxy*) this_arg;
  3487. this_arg = tp->rp->unwrapped_server;
  3488. MONO_HANDLE_NEW (MonoObject, this_arg);
  3489. g_assert (this_arg);
  3490. k = mono_object_class (this_arg);
  3491. }
  3492. name = mono_array_get_internal (params, MonoString *, 1);
  3493. MONO_HANDLE_NEW (MonoString, name);
  3494. str = mono_string_to_utf8_checked_internal (name, error);
  3495. return_if_nok (error);
  3496. do {
  3497. MonoClassField* field = mono_class_get_field_from_name_full (k, str, NULL);
  3498. if (field) {
  3499. g_free (str);
  3500. MonoClass *field_klass = mono_class_from_mono_type_internal (field->type);
  3501. MonoObject *val = (MonoObject *)mono_array_get_internal (params, gpointer, 2);
  3502. MONO_HANDLE_NEW (MonoObject, val);
  3503. if (m_class_is_valuetype (field_klass)) {
  3504. size = mono_type_size (field->type, &align);
  3505. g_assert (size == mono_class_value_size (field_klass, NULL));
  3506. mono_gc_wbarrier_value_copy_internal ((char *)this_arg + field->offset, (char*)val + sizeof (MonoObject), 1, field_klass);
  3507. } else {
  3508. mono_gc_wbarrier_set_field_internal (this_arg, (char*)this_arg + field->offset, val);
  3509. }
  3510. out_args = mono_array_new_checked (domain, mono_defaults.object_class, 0, error);
  3511. return_if_nok (error);
  3512. MONO_HANDLE_NEW (MonoArray, out_args); // FIXME? overkill?
  3513. mono_gc_wbarrier_generic_store_internal (MONO_HANDLE_REF (outArgs), (MonoObject*) out_args);
  3514. return;
  3515. }
  3516. k = m_class_get_parent (k);
  3517. } while (k);
  3518. g_free (str);
  3519. g_assert_not_reached ();
  3520. }
  3521. MonoObjectHandle
  3522. ves_icall_InternalExecute (MonoReflectionMethodHandle method_handle, MonoObjectHandle this_arg_handle, MonoArrayHandle params_handle, MonoArrayHandleOut outArgs, MonoError* error)
  3523. {
  3524. MONO_HANDLE_ASSIGN_RAW (outArgs, NULL);
  3525. MonoReflectionMethod* const method = MONO_HANDLE_RAW (method_handle);
  3526. MonoObject* const this_arg = MONO_HANDLE_RAW (this_arg_handle);
  3527. MonoArray* const params = MONO_HANDLE_RAW (params_handle);
  3528. MonoObjectHandle null_handle = NULL_HANDLE_INIT;
  3529. MonoDomain *domain = mono_object_domain (method);
  3530. MonoMethod *m = method->method;
  3531. MonoMethodSignature *sig = mono_method_signature_internal (m);
  3532. MonoArray *out_args;
  3533. MonoObject *result;
  3534. int i, j, outarg_count = 0;
  3535. if (m->klass == mono_defaults.object_class) {
  3536. if (!strcmp (m->name, "FieldGetter")) {
  3537. internal_execute_field_getter (domain, this_arg, params, outArgs, error);
  3538. return null_handle;
  3539. } else if (!strcmp (m->name, "FieldSetter")) {
  3540. internal_execute_field_setter (domain, this_arg, params, outArgs, error);
  3541. return null_handle;
  3542. }
  3543. }
  3544. for (i = 0; i < mono_array_length_internal (params); i++) {
  3545. if (sig->params [i]->byref)
  3546. outarg_count++;
  3547. }
  3548. out_args = mono_array_new_checked (domain, mono_defaults.object_class, outarg_count, error);
  3549. return_val_if_nok (error, null_handle);
  3550. MONO_HANDLE_NEW (MonoArray, out_args);
  3551. /* handle constructors only for objects already allocated */
  3552. if (!strcmp (method->method->name, ".ctor"))
  3553. g_assert (this_arg);
  3554. /* This can be called only on MBR objects, so no need to unbox for valuetypes. */
  3555. g_assert (!m_class_is_valuetype (method->method->klass));
  3556. result = mono_runtime_invoke_array_checked (method->method, this_arg, params, error);
  3557. return_val_if_nok (error, null_handle);
  3558. MonoObjectHandle result_handle = MONO_HANDLE_NEW (MonoObject, result);
  3559. MonoObjectHandle arg_handle = MONO_HANDLE_NEW (MonoObject, NULL);
  3560. for (i = 0, j = 0; i < mono_array_length_internal (params); i++) {
  3561. if (sig->params [i]->byref) {
  3562. gpointer arg;
  3563. arg = mono_array_get_internal (params, gpointer, i);
  3564. MONO_HANDLE_ASSIGN_RAW (arg_handle, arg); // FIXME? overkill?
  3565. mono_array_setref_internal (out_args, j, arg);
  3566. j++;
  3567. }
  3568. }
  3569. mono_gc_wbarrier_generic_store_internal (MONO_HANDLE_REF (outArgs), (MonoObject*)out_args);
  3570. return result_handle;
  3571. }
  3572. #endif
  3573. static guint64
  3574. read_enum_value (const char *mem, int type)
  3575. {
  3576. switch (type) {
  3577. case MONO_TYPE_BOOLEAN:
  3578. case MONO_TYPE_U1:
  3579. return *(guint8*)mem;
  3580. case MONO_TYPE_I1:
  3581. return *(gint8*)mem;
  3582. case MONO_TYPE_CHAR:
  3583. case MONO_TYPE_U2:
  3584. return read16 (mem);
  3585. case MONO_TYPE_I2:
  3586. return (gint16) read16 (mem);
  3587. case MONO_TYPE_U4:
  3588. case MONO_TYPE_R4:
  3589. return read32 (mem);
  3590. case MONO_TYPE_I4:
  3591. return (gint32) read32 (mem);
  3592. case MONO_TYPE_U8:
  3593. case MONO_TYPE_I8:
  3594. case MONO_TYPE_R8:
  3595. return read64 (mem);
  3596. case MONO_TYPE_U:
  3597. case MONO_TYPE_I:
  3598. #if SIZEOF_REGISTER == 8
  3599. return read64 (mem);
  3600. #else
  3601. return read32 (mem);
  3602. #endif
  3603. default:
  3604. g_assert_not_reached ();
  3605. }
  3606. return 0;
  3607. }
  3608. static void
  3609. write_enum_value (void *mem, int type, guint64 value)
  3610. {
  3611. switch (type) {
  3612. case MONO_TYPE_U1:
  3613. case MONO_TYPE_I1:
  3614. case MONO_TYPE_BOOLEAN: {
  3615. guint8 *p = (guint8*)mem;
  3616. *p = value;
  3617. break;
  3618. }
  3619. case MONO_TYPE_U2:
  3620. case MONO_TYPE_I2:
  3621. case MONO_TYPE_CHAR: {
  3622. guint16 *p = (guint16 *)mem;
  3623. *p = value;
  3624. break;
  3625. }
  3626. case MONO_TYPE_U4:
  3627. case MONO_TYPE_I4:
  3628. case MONO_TYPE_R4: {
  3629. guint32 *p = (guint32 *)mem;
  3630. *p = value;
  3631. break;
  3632. }
  3633. case MONO_TYPE_U8:
  3634. case MONO_TYPE_I8:
  3635. case MONO_TYPE_R8: {
  3636. guint64 *p = (guint64 *)mem;
  3637. *p = value;
  3638. break;
  3639. }
  3640. case MONO_TYPE_U:
  3641. case MONO_TYPE_I: {
  3642. #if SIZEOF_REGISTER == 8
  3643. guint64 *p = (guint64 *)mem;
  3644. *p = value;
  3645. #else
  3646. guint32 *p = (guint32 *)mem;
  3647. *p = value;
  3648. break;
  3649. #endif
  3650. break;
  3651. }
  3652. default:
  3653. g_assert_not_reached ();
  3654. }
  3655. return;
  3656. }
  3657. MonoObjectHandle
  3658. ves_icall_System_Enum_ToObject (MonoReflectionTypeHandle enumType, guint64 value, MonoError *error)
  3659. {
  3660. MonoDomain *domain;
  3661. MonoClass *enumc;
  3662. MonoObjectHandle resultHandle;
  3663. MonoType *etype;
  3664. domain = MONO_HANDLE_DOMAIN (enumType);
  3665. enumc = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (enumType, type));
  3666. mono_class_init_checked (enumc, error);
  3667. goto_if_nok (error, return_null);
  3668. etype = mono_class_enum_basetype_internal (enumc);
  3669. resultHandle = mono_object_new_handle (domain, enumc, error);
  3670. goto_if_nok (error, return_null);
  3671. write_enum_value (mono_handle_unbox_unsafe (resultHandle), etype->type, value);
  3672. return resultHandle;
  3673. return_null:
  3674. return MONO_HANDLE_NEW (MonoObject, NULL);
  3675. }
  3676. MonoBoolean
  3677. ves_icall_System_Enum_InternalHasFlag (MonoObjectHandle a, MonoObjectHandle b, MonoError *error)
  3678. {
  3679. int size = mono_class_value_size (mono_handle_class (a), NULL);
  3680. guint64 a_val = 0, b_val = 0;
  3681. memcpy (&a_val, mono_handle_unbox_unsafe (a), size);
  3682. memcpy (&b_val, mono_handle_unbox_unsafe (b), size);
  3683. return (a_val & b_val) == b_val;
  3684. }
  3685. #ifndef ENABLE_NETCORE
  3686. MonoObjectHandle
  3687. ves_icall_System_Enum_get_value (MonoObjectHandle ehandle, MonoError *error)
  3688. {
  3689. MonoObjectHandle resultHandle;
  3690. MonoClass *enumc;
  3691. int size;
  3692. goto_if (MONO_HANDLE_IS_NULL (ehandle), return_null);
  3693. g_assert (m_class_is_enumtype (mono_handle_class (ehandle)));
  3694. enumc = mono_class_from_mono_type_internal (mono_class_enum_basetype_internal (mono_handle_class (ehandle)));
  3695. resultHandle = mono_object_new_handle (MONO_HANDLE_DOMAIN (ehandle), enumc, error);
  3696. goto_if_nok (error, return_null);
  3697. size = mono_class_value_size (enumc, NULL);
  3698. memcpy (mono_handle_unbox_unsafe (resultHandle), mono_handle_unbox_unsafe (ehandle), size);
  3699. return resultHandle;
  3700. return_null:
  3701. return MONO_HANDLE_NEW (MonoObject, NULL);
  3702. }
  3703. #endif
  3704. MonoReflectionTypeHandle
  3705. ves_icall_System_Enum_get_underlying_type (MonoReflectionTypeHandle type, MonoError *error)
  3706. {
  3707. MonoType *etype;
  3708. MonoClass *klass;
  3709. klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
  3710. mono_class_init_checked (klass, error);
  3711. goto_if_nok (error, return_null);
  3712. etype = mono_class_enum_basetype_internal (klass);
  3713. if (!etype) {
  3714. mono_error_set_argument (error, "enumType", "Type provided must be an Enum.");
  3715. goto return_null;
  3716. }
  3717. return mono_type_get_object_handle (MONO_HANDLE_DOMAIN (type), etype, error);
  3718. return_null:
  3719. return MONO_HANDLE_NEW (MonoReflectionType, NULL);
  3720. }
  3721. int
  3722. ves_icall_System_Enum_InternalGetCorElementType (MonoObjectHandle this_handle, MonoError *error)
  3723. {
  3724. MonoClass *klass = MONO_HANDLE_GETVAL (this_handle, vtable)->klass;
  3725. return (int)m_class_get_byval_arg (m_class_get_element_class (klass))->type;
  3726. }
  3727. #ifndef ENABLE_NETCORE
  3728. int
  3729. ves_icall_System_Enum_compare_value_to (MonoObjectHandle enumHandle, MonoObjectHandle otherHandle, MonoError *error)
  3730. {
  3731. if (MONO_HANDLE_IS_NULL (otherHandle))
  3732. return 1;
  3733. if (MONO_HANDLE_GETVAL (enumHandle, vtable)->klass != MONO_HANDLE_GETVAL (otherHandle, vtable)->klass)
  3734. return 2;
  3735. gpointer tdata = mono_handle_unbox_unsafe (enumHandle);
  3736. gpointer odata = mono_handle_unbox_unsafe (otherHandle);
  3737. MonoType *basetype = mono_class_enum_basetype_internal (MONO_HANDLE_GETVAL (enumHandle, vtable)->klass);
  3738. g_assert (basetype);
  3739. #define COMPARE_ENUM_VALUES(ENUM_TYPE) do { \
  3740. ENUM_TYPE me = *((ENUM_TYPE*)tdata); \
  3741. ENUM_TYPE other = *((ENUM_TYPE*)odata); \
  3742. if (me == other) \
  3743. return 0; \
  3744. return me > other ? 1 : -1; \
  3745. } while (0)
  3746. switch (basetype->type) {
  3747. case MONO_TYPE_BOOLEAN:
  3748. case MONO_TYPE_U1:
  3749. COMPARE_ENUM_VALUES (guint8);
  3750. case MONO_TYPE_I1:
  3751. COMPARE_ENUM_VALUES (gint8);
  3752. case MONO_TYPE_CHAR:
  3753. case MONO_TYPE_U2:
  3754. COMPARE_ENUM_VALUES (guint16);
  3755. case MONO_TYPE_I2:
  3756. COMPARE_ENUM_VALUES (gint16);
  3757. case MONO_TYPE_U4:
  3758. COMPARE_ENUM_VALUES (guint32);
  3759. case MONO_TYPE_I4:
  3760. COMPARE_ENUM_VALUES (gint32);
  3761. case MONO_TYPE_R4:
  3762. COMPARE_ENUM_VALUES (gfloat);
  3763. case MONO_TYPE_U8:
  3764. COMPARE_ENUM_VALUES (guint64);
  3765. case MONO_TYPE_I8:
  3766. COMPARE_ENUM_VALUES (gint64);
  3767. case MONO_TYPE_R8:
  3768. COMPARE_ENUM_VALUES (gdouble);
  3769. case MONO_TYPE_U:
  3770. #if SIZEOF_REGISTER == 8
  3771. COMPARE_ENUM_VALUES (guint64);
  3772. #else
  3773. COMPARE_ENUM_VALUES (guint32);
  3774. #endif
  3775. case MONO_TYPE_I:
  3776. #if SIZEOF_REGISTER == 8
  3777. COMPARE_ENUM_VALUES (gint64);
  3778. #else
  3779. COMPARE_ENUM_VALUES (gint32);
  3780. #endif
  3781. default:
  3782. break;
  3783. }
  3784. #undef COMPARE_ENUM_VALUES
  3785. /* indicates that the enum was of an unsupported underlying type */
  3786. return 3;
  3787. }
  3788. #endif
  3789. #ifndef ENABLE_NETCORE
  3790. int
  3791. ves_icall_System_Enum_get_hashcode (MonoObjectHandle enumHandle, MonoError *error)
  3792. {
  3793. gpointer data = mono_handle_unbox_unsafe (enumHandle);
  3794. MonoType *basetype = mono_class_enum_basetype_internal (MONO_HANDLE_GETVAL (enumHandle, vtable)->klass);
  3795. g_assert (basetype);
  3796. switch (basetype->type) {
  3797. case MONO_TYPE_I1: {
  3798. gint8 value = *((gint8*)data);
  3799. return ((int)value ^ (int)value << 8);
  3800. }
  3801. case MONO_TYPE_U1:
  3802. return *((guint8*)data);
  3803. case MONO_TYPE_CHAR:
  3804. case MONO_TYPE_U2:
  3805. return *((guint16*)data);
  3806. case MONO_TYPE_I2: {
  3807. gint16 value = *((gint16*)data);
  3808. return ((int)(guint16)value | (((int)value) << 16));
  3809. }
  3810. case MONO_TYPE_U4:
  3811. case MONO_TYPE_R4:
  3812. return *((guint32*)data);
  3813. case MONO_TYPE_I4:
  3814. return *((gint32*)data);
  3815. case MONO_TYPE_U8:
  3816. case MONO_TYPE_I8:
  3817. case MONO_TYPE_R8: {
  3818. gint64 value = *((gint64*)data);
  3819. return (gint)(value & 0xffffffff) ^ (int)(value >> 32);
  3820. }
  3821. case MONO_TYPE_I:
  3822. case MONO_TYPE_U: {
  3823. #if SIZEOF_REGISTER == 8
  3824. gint64 value = *((gint64*)data);
  3825. return (gint)(value & 0xffffffff) ^ (int)(value >> 32);
  3826. #else
  3827. return *((guint32*)data);
  3828. #endif
  3829. }
  3830. default:
  3831. g_error ("Implement type 0x%02x in get_hashcode", basetype->type);
  3832. }
  3833. return 0;
  3834. }
  3835. #endif
  3836. static void
  3837. get_enum_field (MonoDomain *domain, MonoArrayHandle names, MonoArrayHandle values, int base_type, MonoClassField *field, guint* j, guint64 *previous_value, gboolean *sorted, MonoError *error)
  3838. {
  3839. error_init (error);
  3840. HANDLE_FUNCTION_ENTER();
  3841. guint64 field_value;
  3842. const char *p;
  3843. MonoTypeEnum def_type;
  3844. if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
  3845. goto leave;
  3846. if (strcmp ("value__", mono_field_get_name (field)) == 0)
  3847. goto leave;
  3848. if (mono_field_is_deleted (field))
  3849. goto leave;
  3850. MonoStringHandle name;
  3851. name = mono_string_new_handle (domain, mono_field_get_name (field), error);
  3852. goto_if_nok (error, leave);
  3853. MONO_HANDLE_ARRAY_SETREF (names, *j, name);
  3854. p = mono_class_get_field_default_value (field, &def_type);
  3855. /* len = */ mono_metadata_decode_blob_size (p, &p);
  3856. field_value = read_enum_value (p, base_type);
  3857. MONO_HANDLE_ARRAY_SETVAL (values, guint64, *j, field_value);
  3858. if (*previous_value > field_value)
  3859. *sorted = FALSE;
  3860. *previous_value = field_value;
  3861. (*j)++;
  3862. leave:
  3863. HANDLE_FUNCTION_RETURN();
  3864. }
  3865. MonoBoolean
  3866. ves_icall_System_Enum_GetEnumValuesAndNames (MonoReflectionTypeHandle type, MonoArrayHandleOut values, MonoArrayHandleOut names, MonoError *error)
  3867. {
  3868. MonoDomain *domain = MONO_HANDLE_DOMAIN (type);
  3869. MonoClass *enumc = mono_class_from_mono_type_internal (MONO_HANDLE_RAW(type)->type);
  3870. guint j = 0, nvalues;
  3871. gpointer iter;
  3872. MonoClassField *field;
  3873. int base_type;
  3874. guint64 previous_value = 0;
  3875. gboolean sorted = TRUE;
  3876. error_init (error);
  3877. mono_class_init_checked (enumc, error);
  3878. return_val_if_nok (error, FALSE);
  3879. if (!m_class_is_enumtype (enumc)) {
  3880. #if ENABLE_NETCORE
  3881. mono_error_set_argument (error, NULL, "Type provided must be an Enum.");
  3882. #else
  3883. mono_error_set_argument (error, "enumType", "Type provided must be an Enum.");
  3884. #endif
  3885. return TRUE;
  3886. }
  3887. base_type = mono_class_enum_basetype_internal (enumc)->type;
  3888. nvalues = mono_class_num_fields (enumc) > 0 ? mono_class_num_fields (enumc) - 1 : 0;
  3889. MONO_HANDLE_ASSIGN(names, mono_array_new_handle (domain, mono_defaults.string_class, nvalues, error));
  3890. return_val_if_nok (error, FALSE);
  3891. MONO_HANDLE_ASSIGN(values, mono_array_new_handle (domain, mono_defaults.uint64_class, nvalues, error));
  3892. return_val_if_nok (error, FALSE);
  3893. iter = NULL;
  3894. while ((field = mono_class_get_fields_internal (enumc, &iter))) {
  3895. get_enum_field(domain, names, values, base_type, field, &j, &previous_value, &sorted, error);
  3896. if (!is_ok (error))
  3897. break;
  3898. }
  3899. return_val_if_nok (error, FALSE);
  3900. return sorted || base_type == MONO_TYPE_R4 || base_type == MONO_TYPE_R8;
  3901. }
  3902. enum {
  3903. BFLAGS_IgnoreCase = 1,
  3904. BFLAGS_DeclaredOnly = 2,
  3905. BFLAGS_Instance = 4,
  3906. BFLAGS_Static = 8,
  3907. BFLAGS_Public = 0x10,
  3908. BFLAGS_NonPublic = 0x20,
  3909. BFLAGS_FlattenHierarchy = 0x40,
  3910. BFLAGS_InvokeMethod = 0x100,
  3911. BFLAGS_CreateInstance = 0x200,
  3912. BFLAGS_GetField = 0x400,
  3913. BFLAGS_SetField = 0x800,
  3914. BFLAGS_GetProperty = 0x1000,
  3915. BFLAGS_SetProperty = 0x2000,
  3916. BFLAGS_ExactBinding = 0x10000,
  3917. BFLAGS_SuppressChangeType = 0x20000,
  3918. BFLAGS_OptionalParamBinding = 0x40000
  3919. };
  3920. enum {
  3921. MLISTTYPE_All = 0,
  3922. MLISTTYPE_CaseSensitive = 1,
  3923. MLISTTYPE_CaseInsensitive = 2,
  3924. MLISTTYPE_HandleToInfo = 3
  3925. };
  3926. GPtrArray*
  3927. ves_icall_RuntimeType_GetFields_native (MonoReflectionTypeHandle ref_type, char *utf8_name, guint32 bflags, guint32 mlisttype, MonoError *error)
  3928. {
  3929. error_init (error);
  3930. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  3931. if (type->byref) {
  3932. return g_ptr_array_new ();
  3933. }
  3934. int (*compare_func) (const char *s1, const char *s2) = NULL;
  3935. compare_func = ((bflags & BFLAGS_IgnoreCase) || (mlisttype == MLISTTYPE_CaseInsensitive)) ? mono_utf8_strcasecmp : strcmp;
  3936. MonoClass *startklass, *klass;
  3937. klass = startklass = mono_class_from_mono_type_internal (type);
  3938. GPtrArray *ptr_array = g_ptr_array_sized_new (16);
  3939. handle_parent:
  3940. if (mono_class_has_failure (klass)) {
  3941. mono_error_set_for_class_failure (error, klass);
  3942. goto fail;
  3943. }
  3944. MonoClassField *field;
  3945. gpointer iter;
  3946. iter = NULL;
  3947. while ((field = mono_class_get_fields_lazy (klass, &iter))) {
  3948. guint32 flags = mono_field_get_flags (field);
  3949. int match = 0;
  3950. if (mono_field_is_deleted_with_flags (field, flags))
  3951. continue;
  3952. if ((flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
  3953. if (bflags & BFLAGS_Public)
  3954. match++;
  3955. } else if ((klass == startklass) || (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) != FIELD_ATTRIBUTE_PRIVATE) {
  3956. if (bflags & BFLAGS_NonPublic) {
  3957. match++;
  3958. }
  3959. }
  3960. if (!match)
  3961. continue;
  3962. match = 0;
  3963. if (flags & FIELD_ATTRIBUTE_STATIC) {
  3964. if (bflags & BFLAGS_Static)
  3965. if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
  3966. match++;
  3967. } else {
  3968. if (bflags & BFLAGS_Instance)
  3969. match++;
  3970. }
  3971. if (!match)
  3972. continue;
  3973. if (((mlisttype != MLISTTYPE_All) && (utf8_name != NULL)) && compare_func (mono_field_get_name (field), utf8_name))
  3974. continue;
  3975. g_ptr_array_add (ptr_array, field);
  3976. }
  3977. if (!(bflags & BFLAGS_DeclaredOnly) && (klass = m_class_get_parent (klass)))
  3978. goto handle_parent;
  3979. return ptr_array;
  3980. fail:
  3981. g_ptr_array_free (ptr_array, TRUE);
  3982. return NULL;
  3983. }
  3984. static gboolean
  3985. method_nonpublic (MonoMethod* method, gboolean start_klass)
  3986. {
  3987. switch (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) {
  3988. case METHOD_ATTRIBUTE_ASSEM:
  3989. return TRUE;
  3990. case METHOD_ATTRIBUTE_PRIVATE:
  3991. return start_klass;
  3992. case METHOD_ATTRIBUTE_PUBLIC:
  3993. return FALSE;
  3994. default:
  3995. return TRUE;
  3996. }
  3997. }
  3998. GPtrArray*
  3999. mono_class_get_methods_by_name (MonoClass *klass, const char *name, guint32 bflags, guint32 mlisttype, gboolean allow_ctors, MonoError *error)
  4000. {
  4001. GPtrArray *array;
  4002. MonoClass *startklass;
  4003. MonoMethod *method;
  4004. gpointer iter;
  4005. int match, nslots;
  4006. /*FIXME, use MonoBitSet*/
  4007. guint32 method_slots_default [8];
  4008. guint32 *method_slots = NULL;
  4009. int (*compare_func) (const char *s1, const char *s2) = NULL;
  4010. array = g_ptr_array_new ();
  4011. startklass = klass;
  4012. error_init (error);
  4013. compare_func = ((bflags & BFLAGS_IgnoreCase) || (mlisttype == MLISTTYPE_CaseInsensitive)) ? mono_utf8_strcasecmp : strcmp;
  4014. /* An optimization for calls made from Delegate:CreateDelegate () */
  4015. if (m_class_is_delegate (klass) && klass != mono_defaults.delegate_class && klass != mono_defaults.multicastdelegate_class && name && !strcmp (name, "Invoke") && (bflags == (BFLAGS_Public | BFLAGS_Static | BFLAGS_Instance))) {
  4016. method = mono_get_delegate_invoke_internal (klass);
  4017. g_assert (method);
  4018. g_ptr_array_add (array, method);
  4019. return array;
  4020. }
  4021. mono_class_setup_methods (klass);
  4022. mono_class_setup_vtable (klass);
  4023. if (mono_class_has_failure (klass))
  4024. goto loader_error;
  4025. if (is_generic_parameter (m_class_get_byval_arg (klass)))
  4026. nslots = mono_class_get_vtable_size (m_class_get_parent (klass));
  4027. else
  4028. nslots = MONO_CLASS_IS_INTERFACE_INTERNAL (klass) ? mono_class_num_methods (klass) : mono_class_get_vtable_size (klass);
  4029. if (nslots >= sizeof (method_slots_default) * 8) {
  4030. method_slots = g_new0 (guint32, nslots / 32 + 1);
  4031. } else {
  4032. method_slots = method_slots_default;
  4033. memset (method_slots, 0, sizeof (method_slots_default));
  4034. }
  4035. handle_parent:
  4036. mono_class_setup_methods (klass);
  4037. mono_class_setup_vtable (klass);
  4038. if (mono_class_has_failure (klass))
  4039. goto loader_error;
  4040. iter = NULL;
  4041. while ((method = mono_class_get_methods (klass, &iter))) {
  4042. match = 0;
  4043. if (method->slot != -1) {
  4044. g_assert (method->slot < nslots);
  4045. if (method_slots [method->slot >> 5] & (1 << (method->slot & 0x1f)))
  4046. continue;
  4047. if (!(method->flags & METHOD_ATTRIBUTE_NEW_SLOT))
  4048. method_slots [method->slot >> 5] |= 1 << (method->slot & 0x1f);
  4049. }
  4050. if (!allow_ctors && method->name [0] == '.' && (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0))
  4051. continue;
  4052. if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
  4053. if (bflags & BFLAGS_Public)
  4054. match++;
  4055. } else if ((bflags & BFLAGS_NonPublic) && method_nonpublic (method, (klass == startklass))) {
  4056. match++;
  4057. }
  4058. if (!match)
  4059. continue;
  4060. match = 0;
  4061. if (method->flags & METHOD_ATTRIBUTE_STATIC) {
  4062. if (bflags & BFLAGS_Static)
  4063. if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
  4064. match++;
  4065. } else {
  4066. if (bflags & BFLAGS_Instance)
  4067. match++;
  4068. }
  4069. if (!match)
  4070. continue;
  4071. if ((mlisttype != MLISTTYPE_All) && (name != NULL)) {
  4072. if (compare_func (name, method->name))
  4073. continue;
  4074. }
  4075. match = 0;
  4076. g_ptr_array_add (array, method);
  4077. }
  4078. if (!(bflags & BFLAGS_DeclaredOnly) && (klass = m_class_get_parent (klass)))
  4079. goto handle_parent;
  4080. if (method_slots != method_slots_default)
  4081. g_free (method_slots);
  4082. return array;
  4083. loader_error:
  4084. if (method_slots != method_slots_default)
  4085. g_free (method_slots);
  4086. g_ptr_array_free (array, TRUE);
  4087. g_assert (mono_class_has_failure (klass));
  4088. mono_error_set_for_class_failure (error, klass);
  4089. return NULL;
  4090. }
  4091. GPtrArray*
  4092. ves_icall_RuntimeType_GetMethodsByName_native (MonoReflectionTypeHandle ref_type, const char *mname, guint32 bflags, guint32 mlisttype, MonoError *error)
  4093. {
  4094. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  4095. MonoClass *klass = mono_class_from_mono_type_internal (type);
  4096. if (type->byref) {
  4097. return g_ptr_array_new ();
  4098. }
  4099. return mono_class_get_methods_by_name (klass, mname, bflags, mlisttype, FALSE, error);
  4100. }
  4101. GPtrArray*
  4102. ves_icall_RuntimeType_GetConstructors_native (MonoReflectionTypeHandle ref_type, guint32 bflags, MonoError *error)
  4103. {
  4104. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  4105. if (type->byref) {
  4106. return g_ptr_array_new ();
  4107. }
  4108. MonoClass *startklass, *klass;
  4109. klass = startklass = mono_class_from_mono_type_internal (type);
  4110. mono_class_setup_methods (klass);
  4111. if (mono_class_has_failure (klass)) {
  4112. mono_error_set_for_class_failure (error, klass);
  4113. return NULL;
  4114. }
  4115. GPtrArray *res_array = g_ptr_array_sized_new (4); /* FIXME, guestimating */
  4116. MonoMethod *method;
  4117. gpointer iter = NULL;
  4118. while ((method = mono_class_get_methods (klass, &iter))) {
  4119. int match = 0;
  4120. if (strcmp (method->name, ".ctor") && strcmp (method->name, ".cctor"))
  4121. continue;
  4122. if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
  4123. if (bflags & BFLAGS_Public)
  4124. match++;
  4125. } else {
  4126. if (bflags & BFLAGS_NonPublic)
  4127. match++;
  4128. }
  4129. if (!match)
  4130. continue;
  4131. match = 0;
  4132. if (method->flags & METHOD_ATTRIBUTE_STATIC) {
  4133. if (bflags & BFLAGS_Static)
  4134. if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
  4135. match++;
  4136. } else {
  4137. if (bflags & BFLAGS_Instance)
  4138. match++;
  4139. }
  4140. if (!match)
  4141. continue;
  4142. g_ptr_array_add (res_array, method);
  4143. }
  4144. return res_array;
  4145. }
  4146. static guint
  4147. property_hash (gconstpointer data)
  4148. {
  4149. MonoProperty *prop = (MonoProperty*)data;
  4150. return g_str_hash (prop->name);
  4151. }
  4152. static gboolean
  4153. property_accessor_override (MonoMethod *method1, MonoMethod *method2)
  4154. {
  4155. if (method1->slot != -1 && method1->slot == method2->slot)
  4156. return TRUE;
  4157. if (mono_class_get_generic_type_definition (method1->klass) == mono_class_get_generic_type_definition (method2->klass)) {
  4158. if (method1->is_inflated)
  4159. method1 = ((MonoMethodInflated*) method1)->declaring;
  4160. if (method2->is_inflated)
  4161. method2 = ((MonoMethodInflated*) method2)->declaring;
  4162. }
  4163. return mono_metadata_signature_equal (mono_method_signature_internal (method1), mono_method_signature_internal (method2));
  4164. }
  4165. static gboolean
  4166. property_equal (MonoProperty *prop1, MonoProperty *prop2)
  4167. {
  4168. // Properties are hide-by-name-and-signature
  4169. if (!g_str_equal (prop1->name, prop2->name))
  4170. return FALSE;
  4171. /* If we see a property in a generic method, we want to
  4172. compare the generic signatures, not the inflated signatures
  4173. because we might conflate two properties that were
  4174. distinct:
  4175. class Foo<T,U> {
  4176. public T this[T t] { getter { return t; } } // method 1
  4177. public U this[U u] { getter { return u; } } // method 2
  4178. }
  4179. If we see int Foo<int,int>::Item[int] we need to know if
  4180. the indexer came from method 1 or from method 2, and we
  4181. shouldn't conflate them. (Bugzilla 36283)
  4182. */
  4183. if (prop1->get && prop2->get && !property_accessor_override (prop1->get, prop2->get))
  4184. return FALSE;
  4185. if (prop1->set && prop2->set && !property_accessor_override (prop1->set, prop2->set))
  4186. return FALSE;
  4187. return TRUE;
  4188. }
  4189. static gboolean
  4190. property_accessor_nonpublic (MonoMethod* accessor, gboolean start_klass)
  4191. {
  4192. if (!accessor)
  4193. return FALSE;
  4194. return method_nonpublic (accessor, start_klass);
  4195. }
  4196. GPtrArray*
  4197. ves_icall_RuntimeType_GetPropertiesByName_native (MonoReflectionTypeHandle ref_type, gchar *propname, guint32 bflags, guint32 mlisttype, MonoError *error)
  4198. {
  4199. #if ENABLE_NETCORE
  4200. // Fetch non-public properties as well because they can hide public properties with the same name in base classes
  4201. bflags |= BFLAGS_NonPublic;
  4202. #endif
  4203. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  4204. if (type->byref) {
  4205. return g_ptr_array_new ();
  4206. }
  4207. MonoClass *startklass, *klass;
  4208. klass = startklass = mono_class_from_mono_type_internal (type);
  4209. int (*compare_func) (const char *s1, const char *s2) = (mlisttype == MLISTTYPE_CaseInsensitive) ? mono_utf8_strcasecmp : strcmp;
  4210. GPtrArray *res_array = g_ptr_array_sized_new (8); /*This the average for ASP.NET types*/
  4211. GHashTable *properties = g_hash_table_new (property_hash, (GEqualFunc)property_equal);
  4212. handle_parent:
  4213. mono_class_setup_methods (klass);
  4214. mono_class_setup_vtable (klass);
  4215. if (mono_class_has_failure (klass)) {
  4216. mono_error_set_for_class_failure (error, klass);
  4217. goto loader_error;
  4218. }
  4219. MonoProperty *prop;
  4220. gpointer iter;
  4221. iter = NULL;
  4222. while ((prop = mono_class_get_properties (klass, &iter))) {
  4223. int match = 0;
  4224. MonoMethod *method = prop->get;
  4225. if (!method)
  4226. method = prop->set;
  4227. guint32 flags = 0;
  4228. if (method)
  4229. flags = method->flags;
  4230. if ((prop->get && ((prop->get->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)) ||
  4231. (prop->set && ((prop->set->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC))) {
  4232. if (bflags & BFLAGS_Public)
  4233. match++;
  4234. } else if (bflags & BFLAGS_NonPublic) {
  4235. if (property_accessor_nonpublic(prop->get, startklass == klass) ||
  4236. property_accessor_nonpublic(prop->set, startklass == klass)) {
  4237. match++;
  4238. }
  4239. }
  4240. if (!match)
  4241. continue;
  4242. match = 0;
  4243. if (flags & METHOD_ATTRIBUTE_STATIC) {
  4244. if (bflags & BFLAGS_Static)
  4245. if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
  4246. match++;
  4247. } else {
  4248. if (bflags & BFLAGS_Instance)
  4249. match++;
  4250. }
  4251. if (!match)
  4252. continue;
  4253. match = 0;
  4254. if ((mlisttype != MLISTTYPE_All) && (propname != NULL) && compare_func (propname, prop->name))
  4255. continue;
  4256. if (g_hash_table_lookup (properties, prop))
  4257. continue;
  4258. g_ptr_array_add (res_array, prop);
  4259. g_hash_table_insert (properties, prop, prop);
  4260. }
  4261. if (!(bflags & BFLAGS_DeclaredOnly) && (klass = m_class_get_parent (klass))) {
  4262. #if ENABLE_NETCORE
  4263. // BFLAGS_NonPublic should be excluded for base classes
  4264. bflags &= ~BFLAGS_NonPublic;
  4265. #endif
  4266. goto handle_parent;
  4267. }
  4268. g_hash_table_destroy (properties);
  4269. return res_array;
  4270. loader_error:
  4271. if (properties)
  4272. g_hash_table_destroy (properties);
  4273. g_ptr_array_free (res_array, TRUE);
  4274. return NULL;
  4275. }
  4276. static guint
  4277. event_hash (gconstpointer data)
  4278. {
  4279. MonoEvent *event = (MonoEvent*)data;
  4280. return g_str_hash (event->name);
  4281. }
  4282. static gboolean
  4283. event_equal (MonoEvent *event1, MonoEvent *event2)
  4284. {
  4285. // Events are hide-by-name
  4286. return g_str_equal (event1->name, event2->name);
  4287. }
  4288. GPtrArray*
  4289. ves_icall_RuntimeType_GetEvents_native (MonoReflectionTypeHandle ref_type, char *utf8_name, guint32 mlisttype, MonoError *error)
  4290. {
  4291. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  4292. if (type->byref) {
  4293. return g_ptr_array_new ();
  4294. }
  4295. int (*compare_func) (const char *s1, const char *s2) = (mlisttype == MLISTTYPE_CaseInsensitive) ? mono_utf8_strcasecmp : strcmp;
  4296. GPtrArray *res_array = g_ptr_array_sized_new (4);
  4297. MonoClass *startklass, *klass;
  4298. klass = startklass = mono_class_from_mono_type_internal (type);
  4299. GHashTable *events = g_hash_table_new (event_hash, (GEqualFunc)event_equal);
  4300. handle_parent:
  4301. mono_class_setup_methods (klass);
  4302. mono_class_setup_vtable (klass);
  4303. if (mono_class_has_failure (klass)) {
  4304. mono_error_set_for_class_failure (error, klass);
  4305. goto failure;
  4306. }
  4307. MonoEvent *event;
  4308. gpointer iter;
  4309. iter = NULL;
  4310. while ((event = mono_class_get_events (klass, &iter))) {
  4311. // Remove inherited privates and inherited
  4312. // without add/remove/raise methods
  4313. if (klass != startklass)
  4314. {
  4315. MonoMethod *method = event->add;
  4316. if (!method)
  4317. method = event->remove;
  4318. if (!method)
  4319. method = event->raise;
  4320. if (!method)
  4321. continue;
  4322. if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PRIVATE)
  4323. continue;
  4324. }
  4325. if ((mlisttype != MLISTTYPE_All) && (utf8_name != NULL) && compare_func (event->name, utf8_name))
  4326. continue;
  4327. if (g_hash_table_lookup (events, event))
  4328. continue;
  4329. g_ptr_array_add (res_array, event);
  4330. g_hash_table_insert (events, event, event);
  4331. }
  4332. if ((klass = m_class_get_parent (klass)))
  4333. goto handle_parent;
  4334. g_hash_table_destroy (events);
  4335. return res_array;
  4336. failure:
  4337. if (events != NULL)
  4338. g_hash_table_destroy (events);
  4339. g_ptr_array_free (res_array, TRUE);
  4340. return NULL;
  4341. }
  4342. GPtrArray *
  4343. ves_icall_RuntimeType_GetNestedTypes_native (MonoReflectionTypeHandle ref_type, char *str, guint32 bflags, guint32 mlisttype, MonoError *error)
  4344. {
  4345. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  4346. if (type->byref) {
  4347. return g_ptr_array_new ();
  4348. }
  4349. int (*compare_func) (const char *s1, const char *s2) = ((bflags & BFLAGS_IgnoreCase) || (mlisttype == MLISTTYPE_CaseInsensitive)) ? mono_utf8_strcasecmp : strcmp;
  4350. MonoClass *klass = mono_class_from_mono_type_internal (type);
  4351. /*
  4352. * If a nested type is generic, return its generic type definition.
  4353. * Note that this means that the return value is essentially the set
  4354. * of nested types of the generic type definition of @klass.
  4355. *
  4356. * A note in MSDN claims that a generic type definition can have
  4357. * nested types that aren't generic. In any case, the container of that
  4358. * nested type would be the generic type definition.
  4359. */
  4360. if (mono_class_is_ginst (klass))
  4361. klass = mono_class_get_generic_class (klass)->container_class;
  4362. GPtrArray *res_array = g_ptr_array_new ();
  4363. MonoClass *nested;
  4364. gpointer iter = NULL;
  4365. while ((nested = mono_class_get_nested_types (klass, &iter))) {
  4366. int match = 0;
  4367. if ((mono_class_get_flags (nested) & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
  4368. if (bflags & BFLAGS_Public)
  4369. match++;
  4370. } else {
  4371. if (bflags & BFLAGS_NonPublic)
  4372. match++;
  4373. }
  4374. if (!match)
  4375. continue;
  4376. if ((mlisttype != MLISTTYPE_All) && (str != NULL) && compare_func (m_class_get_name (nested), str))
  4377. continue;
  4378. g_ptr_array_add (res_array, m_class_get_byval_arg (nested));
  4379. }
  4380. return res_array;
  4381. }
  4382. static MonoType*
  4383. get_type_from_module_builder_module (MonoAssemblyLoadContext *alc, MonoArrayHandle modules, int i, MonoTypeNameParse *info, MonoBoolean ignoreCase, gboolean *type_resolve, MonoError *error)
  4384. {
  4385. HANDLE_FUNCTION_ENTER ();
  4386. MonoType *type = NULL;
  4387. MonoReflectionModuleBuilderHandle mb = MONO_HANDLE_NEW (MonoReflectionModuleBuilder, NULL);
  4388. MONO_HANDLE_ARRAY_GETREF (mb, modules, i);
  4389. MonoDynamicImage *dynamic_image = MONO_HANDLE_GETVAL (mb, dynamic_image);
  4390. type = mono_reflection_get_type_checked (alc, &dynamic_image->image, &dynamic_image->image, info, ignoreCase, FALSE, type_resolve, error);
  4391. HANDLE_FUNCTION_RETURN_VAL (type);
  4392. }
  4393. static MonoType*
  4394. get_type_from_module_builder_loaded_modules (MonoAssemblyLoadContext *alc, MonoArrayHandle loaded_modules, int i, MonoTypeNameParse *info, MonoBoolean ignoreCase, gboolean *type_resolve, MonoError *error)
  4395. {
  4396. HANDLE_FUNCTION_ENTER ();
  4397. MonoType *type = NULL;
  4398. MonoReflectionModuleHandle mod = MONO_HANDLE_NEW (MonoReflectionModule, NULL);
  4399. MONO_HANDLE_ARRAY_GETREF (mod, loaded_modules, i);
  4400. MonoImage *image = MONO_HANDLE_GETVAL (mod, image);
  4401. type = mono_reflection_get_type_checked (alc, image, image, info, ignoreCase, FALSE, type_resolve, error);
  4402. HANDLE_FUNCTION_RETURN_VAL (type);
  4403. }
  4404. MonoReflectionTypeHandle
  4405. ves_icall_System_Reflection_Assembly_InternalGetType (MonoReflectionAssemblyHandle assembly_h, MonoReflectionModuleHandle module, MonoStringHandle name, MonoBoolean throwOnError, MonoBoolean ignoreCase, MonoError *error)
  4406. {
  4407. ERROR_DECL (parse_error);
  4408. MonoTypeNameParse info;
  4409. gboolean type_resolve;
  4410. MonoAssemblyLoadContext *alc = mono_domain_ambient_alc (mono_domain_get ());
  4411. /* On MS.NET, this does not fire a TypeResolve event */
  4412. type_resolve = TRUE;
  4413. char *str = mono_string_handle_to_utf8 (name, error);
  4414. goto_if_nok (error, fail);
  4415. /*g_print ("requested type %s in %s\n", str, assembly->assembly->aname.name);*/
  4416. if (!mono_reflection_parse_type_checked (str, &info, parse_error)) {
  4417. g_free (str);
  4418. mono_reflection_free_type_info (&info);
  4419. mono_error_cleanup (parse_error);
  4420. if (throwOnError) {
  4421. #if ENABLE_NETCORE
  4422. mono_error_set_argument (error, "typeName@0", "failed to parse the type");
  4423. #else
  4424. mono_error_set_argument (error, "typeName", "failed to parse the type");
  4425. #endif
  4426. goto fail;
  4427. }
  4428. /*g_print ("failed parse\n");*/
  4429. return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  4430. }
  4431. if (info.assembly.name) {
  4432. g_free (str);
  4433. mono_reflection_free_type_info (&info);
  4434. if (throwOnError) {
  4435. mono_error_set_argument (error, NULL, "Type names passed to Assembly.GetType() must not specify an assembly.");
  4436. goto fail;
  4437. }
  4438. return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  4439. }
  4440. MonoType *type;
  4441. type = NULL;
  4442. if (!MONO_HANDLE_IS_NULL (module)) {
  4443. MonoImage *image = MONO_HANDLE_GETVAL (module, image);
  4444. if (image) {
  4445. type = mono_reflection_get_type_checked (alc, image, image, &info, ignoreCase, FALSE, &type_resolve, error);
  4446. if (!is_ok (error)) {
  4447. g_free (str);
  4448. mono_reflection_free_type_info (&info);
  4449. goto fail;
  4450. }
  4451. }
  4452. }
  4453. else {
  4454. MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
  4455. if (assembly_is_dynamic (assembly)) {
  4456. /* Enumerate all modules */
  4457. MonoReflectionAssemblyBuilderHandle abuilder = MONO_HANDLE_NEW (MonoReflectionAssemblyBuilder, NULL);
  4458. MONO_HANDLE_ASSIGN (abuilder, assembly_h);
  4459. int i;
  4460. MonoArrayHandle modules = MONO_HANDLE_NEW (MonoArray, NULL);
  4461. MONO_HANDLE_GET (modules, abuilder, modules);
  4462. if (!MONO_HANDLE_IS_NULL (modules)) {
  4463. int n = mono_array_handle_length (modules);
  4464. for (i = 0; i < n; ++i) {
  4465. type = get_type_from_module_builder_module (alc, modules, i, &info, ignoreCase, &type_resolve, error);
  4466. if (!is_ok (error)) {
  4467. g_free (str);
  4468. mono_reflection_free_type_info (&info);
  4469. goto fail;
  4470. }
  4471. if (type)
  4472. break;
  4473. }
  4474. }
  4475. MonoArrayHandle loaded_modules = MONO_HANDLE_NEW (MonoArray, NULL);
  4476. MONO_HANDLE_GET (loaded_modules, abuilder, loaded_modules);
  4477. if (!type && !MONO_HANDLE_IS_NULL (loaded_modules)) {
  4478. int n = mono_array_handle_length (loaded_modules);
  4479. for (i = 0; i < n; ++i) {
  4480. type = get_type_from_module_builder_loaded_modules (alc, loaded_modules, i, &info, ignoreCase, &type_resolve, error);
  4481. if (!is_ok (error)) {
  4482. g_free (str);
  4483. mono_reflection_free_type_info (&info);
  4484. goto fail;
  4485. }
  4486. if (type)
  4487. break;
  4488. }
  4489. }
  4490. }
  4491. else {
  4492. type = mono_reflection_get_type_checked (alc, assembly->image, assembly->image, &info, ignoreCase, FALSE, &type_resolve, error);
  4493. if (!is_ok (error)) {
  4494. g_free (str);
  4495. mono_reflection_free_type_info (&info);
  4496. goto fail;
  4497. }
  4498. }
  4499. }
  4500. g_free (str);
  4501. mono_reflection_free_type_info (&info);
  4502. if (!type) {
  4503. if (throwOnError) {
  4504. ERROR_DECL (inner_error);
  4505. char *type_name = mono_string_handle_to_utf8 (name, inner_error);
  4506. mono_error_assert_ok (inner_error);
  4507. MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
  4508. char *assmname = mono_stringify_assembly_name (&assembly->aname);
  4509. mono_error_set_type_load_name (error, type_name, assmname, "%s", "");
  4510. goto fail;
  4511. }
  4512. return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  4513. }
  4514. if (type->type == MONO_TYPE_CLASS) {
  4515. MonoClass *klass = mono_type_get_class_internal (type);
  4516. /* need to report exceptions ? */
  4517. if (throwOnError && mono_class_has_failure (klass)) {
  4518. /* report SecurityException (or others) that occured when loading the assembly */
  4519. mono_error_set_for_class_failure (error, klass);
  4520. goto fail;
  4521. }
  4522. }
  4523. /* g_print ("got it\n"); */
  4524. return mono_type_get_object_handle (MONO_HANDLE_DOMAIN (assembly_h), type, error);
  4525. fail:
  4526. g_assert (!is_ok (error));
  4527. return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  4528. }
  4529. static gboolean
  4530. replace_shadow_path (MonoDomain *domain, gchar *dirname, gchar **filename)
  4531. {
  4532. gchar *content;
  4533. gchar *shadow_ini_file;
  4534. gsize len;
  4535. /* Check for shadow-copied assembly */
  4536. if (mono_is_shadow_copy_enabled (domain, dirname)) {
  4537. shadow_ini_file = g_build_filename (dirname, "__AssemblyInfo__.ini", (const char*)NULL);
  4538. content = NULL;
  4539. if (!g_file_get_contents (shadow_ini_file, &content, &len, NULL) ||
  4540. !g_file_test (content, G_FILE_TEST_IS_REGULAR)) {
  4541. g_free (content);
  4542. content = NULL;
  4543. }
  4544. g_free (shadow_ini_file);
  4545. if (content != NULL) {
  4546. g_free (*filename);
  4547. *filename = content;
  4548. return TRUE;
  4549. }
  4550. }
  4551. return FALSE;
  4552. }
  4553. MonoStringHandle
  4554. ves_icall_System_Reflection_RuntimeAssembly_get_code_base (MonoReflectionAssemblyHandle assembly, MonoBoolean escaped, MonoError *error)
  4555. {
  4556. MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
  4557. MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly, assembly);
  4558. gchar *absolute;
  4559. gchar *dirname;
  4560. if (g_path_is_absolute (mass->image->name)) {
  4561. absolute = g_strdup (mass->image->name);
  4562. dirname = g_path_get_dirname (absolute);
  4563. } else {
  4564. absolute = g_build_filename (mass->basedir, mass->image->name, (const char*)NULL);
  4565. dirname = g_strdup (mass->basedir);
  4566. }
  4567. replace_shadow_path (domain, dirname, &absolute);
  4568. g_free (dirname);
  4569. mono_icall_make_platform_path (absolute);
  4570. gchar *uri;
  4571. if (escaped) {
  4572. uri = g_filename_to_uri (absolute, NULL, NULL);
  4573. } else {
  4574. const gchar *prepend = mono_icall_get_file_path_prefix (absolute);
  4575. uri = g_strconcat (prepend, absolute, (const char*)NULL);
  4576. }
  4577. g_free (absolute);
  4578. MonoStringHandle res;
  4579. if (uri) {
  4580. res = mono_string_new_handle (domain, uri, error);
  4581. g_free (uri);
  4582. } else {
  4583. res = MONO_HANDLE_NEW (MonoString, NULL);
  4584. }
  4585. return res;
  4586. }
  4587. #ifndef ENABLE_NETCORE
  4588. MonoBoolean
  4589. ves_icall_System_Reflection_RuntimeAssembly_get_global_assembly_cache (MonoReflectionAssemblyHandle assembly, MonoError *error)
  4590. {
  4591. MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly,assembly);
  4592. return mass->in_gac;
  4593. }
  4594. MonoReflectionAssemblyHandle
  4595. ves_icall_System_Reflection_Assembly_load_with_partial_name (MonoStringHandle mname, MonoObjectHandle evidence, MonoError *error)
  4596. {
  4597. gchar *name;
  4598. MonoImageOpenStatus status;
  4599. MonoReflectionAssemblyHandle result = MONO_HANDLE_CAST (MonoReflectionAssembly, NULL_HANDLE);
  4600. name = mono_string_handle_to_utf8 (mname, error);
  4601. goto_if_nok (error, leave);
  4602. MonoAssembly *res;
  4603. res = mono_assembly_load_with_partial_name_internal (name, mono_domain_default_alc (mono_domain_get ()), &status);
  4604. g_free (name);
  4605. if (res == NULL)
  4606. goto leave;
  4607. result = mono_assembly_get_object_handle (mono_domain_get (), res, error);
  4608. leave:
  4609. return result;
  4610. }
  4611. #endif
  4612. MonoStringHandle
  4613. ves_icall_System_Reflection_RuntimeAssembly_get_location (MonoReflectionAssemblyHandle refassembly, MonoError *error)
  4614. {
  4615. MonoDomain *domain = MONO_HANDLE_DOMAIN (refassembly);
  4616. MonoAssembly *assembly = MONO_HANDLE_GETVAL (refassembly, assembly);
  4617. const char *image_name = m_image_get_filename (assembly->image);
  4618. return mono_string_new_handle (domain, image_name != NULL ? image_name : "", error);
  4619. }
  4620. #ifndef ENABLE_NETCORE
  4621. MonoBoolean
  4622. ves_icall_System_Reflection_RuntimeAssembly_get_ReflectionOnly (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
  4623. {
  4624. MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
  4625. return mono_asmctx_get_kind (&assembly->context) == MONO_ASMCTX_REFONLY;
  4626. }
  4627. #endif
  4628. MonoStringHandle
  4629. ves_icall_System_Reflection_RuntimeAssembly_InternalImageRuntimeVersion (MonoReflectionAssemblyHandle refassembly, MonoError *error)
  4630. {
  4631. MonoDomain *domain = MONO_HANDLE_DOMAIN (refassembly);
  4632. MonoAssembly *assembly = MONO_HANDLE_GETVAL (refassembly, assembly);
  4633. return mono_string_new_handle (domain, assembly->image->version, error);
  4634. }
  4635. MonoReflectionMethodHandle
  4636. ves_icall_System_Reflection_RuntimeAssembly_get_EntryPoint (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
  4637. {
  4638. MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
  4639. MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
  4640. MonoMethod *method;
  4641. MonoReflectionMethodHandle res = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
  4642. guint32 token = mono_image_get_entry_point (assembly->image);
  4643. if (!token)
  4644. goto leave;
  4645. method = mono_get_method_checked (assembly->image, token, NULL, NULL, error);
  4646. goto_if_nok (error, leave);
  4647. MONO_HANDLE_ASSIGN (res, mono_method_get_object_handle (domain, method, NULL, error));
  4648. leave:
  4649. return res;
  4650. }
  4651. MonoReflectionModuleHandle
  4652. ves_icall_System_Reflection_Assembly_GetManifestModuleInternal (MonoReflectionAssemblyHandle assembly, MonoError *error)
  4653. {
  4654. MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
  4655. MonoAssembly *a = MONO_HANDLE_GETVAL (assembly, assembly);
  4656. return mono_module_get_object_handle (domain, a->image, error);
  4657. }
  4658. static gboolean
  4659. add_manifest_resource_name_to_array (MonoDomain *domain, MonoImage *image, MonoTableInfo *table, int i, MonoArrayHandle dest, MonoError *error)
  4660. {
  4661. HANDLE_FUNCTION_ENTER ();
  4662. const char *val = mono_metadata_string_heap (image, mono_metadata_decode_row_col (table, i, MONO_MANIFEST_NAME));
  4663. MonoStringHandle str = mono_string_new_handle (domain, val, error);
  4664. goto_if_nok (error, leave);
  4665. MONO_HANDLE_ARRAY_SETREF (dest, i, str);
  4666. leave:
  4667. HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
  4668. }
  4669. MonoArrayHandle
  4670. ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceNames (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
  4671. {
  4672. MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
  4673. MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
  4674. MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
  4675. MonoArrayHandle result = mono_array_new_handle (domain, mono_defaults.string_class, table->rows, error);
  4676. goto_if_nok (error, fail);
  4677. int i;
  4678. for (i = 0; i < table->rows; ++i) {
  4679. if (!add_manifest_resource_name_to_array (domain, assembly->image, table, i, result, error))
  4680. goto fail;
  4681. }
  4682. return result;
  4683. fail:
  4684. return NULL_HANDLE_ARRAY;
  4685. }
  4686. #ifndef ENABLE_NETCORE
  4687. MonoBoolean
  4688. ves_icall_System_Reflection_RuntimeAssembly_GetAotIdInternal (MonoArrayHandle guid_h, MonoError *error)
  4689. {
  4690. g_assert (mono_array_handle_length (guid_h) == 16);
  4691. guint8 *aotid = mono_runtime_get_aotid_arr ();
  4692. if (!aotid) {
  4693. return FALSE;
  4694. } else {
  4695. MONO_ENTER_NO_SAFEPOINTS;
  4696. guint8 *data = (guint8*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_h), 1, 0);
  4697. memcpy (data, aotid, 16);
  4698. MONO_EXIT_NO_SAFEPOINTS;
  4699. return TRUE;
  4700. }
  4701. }
  4702. #endif
  4703. static MonoAssemblyName*
  4704. create_referenced_assembly_name (MonoDomain *domain, MonoImage *image, int i, MonoError *error)
  4705. {
  4706. MonoAssemblyName *aname = g_new0 (MonoAssemblyName, 1);
  4707. mono_assembly_get_assemblyref_checked (image, i, aname, error);
  4708. return_val_if_nok (error, NULL);
  4709. aname->hash_alg = ASSEMBLY_HASH_SHA1 /* SHA1 (default) */;
  4710. /* name and culture are pointers into the image tables, but we need
  4711. * real malloc'd strings (so that we can g_free() them later from
  4712. * Mono.RuntimeMarshal.FreeAssemblyName) */
  4713. aname->name = g_strdup (aname->name);
  4714. aname->culture = g_strdup (aname->culture);
  4715. /* Don't need the hash value in managed */
  4716. aname->hash_value = NULL;
  4717. aname->hash_len = 0;
  4718. g_assert (aname->public_key == NULL);
  4719. /* note: this function doesn't return the codebase on purpose (i.e. it can
  4720. be used under partial trust as path information isn't present). */
  4721. return aname;
  4722. }
  4723. GPtrArray*
  4724. ves_icall_System_Reflection_Assembly_InternalGetReferencedAssemblies (MonoReflectionAssemblyHandle assembly, MonoError *error)
  4725. {
  4726. error_init (error);
  4727. MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
  4728. MonoAssembly *ass = MONO_HANDLE_GETVAL(assembly, assembly);
  4729. MonoImage *image = ass->image;
  4730. int count;
  4731. if (image_is_dynamic (ass->image)) {
  4732. MonoDynamicTable *t = &(((MonoDynamicImage*) image)->tables [MONO_TABLE_ASSEMBLYREF]);
  4733. count = t->rows;
  4734. }
  4735. else {
  4736. MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
  4737. count = t->rows;
  4738. }
  4739. GPtrArray *result = g_ptr_array_sized_new (count);
  4740. for (int i = 0; i < count; i++) {
  4741. MonoAssemblyName *aname = create_referenced_assembly_name (domain, image, i, error);
  4742. if (!is_ok (error))
  4743. break;
  4744. g_ptr_array_add (result, aname);
  4745. }
  4746. return result;
  4747. }
  4748. /* move this in some file in mono/util/ */
  4749. static char *
  4750. g_concat_dir_and_file (const char *dir, const char *file)
  4751. {
  4752. g_return_val_if_fail (dir != NULL, NULL);
  4753. g_return_val_if_fail (file != NULL, NULL);
  4754. /*
  4755. * If the directory name doesn't have a / on the end, we need
  4756. * to add one so we get a proper path to the file
  4757. */
  4758. if (dir [strlen(dir) - 1] != G_DIR_SEPARATOR)
  4759. return g_strconcat (dir, G_DIR_SEPARATOR_S, file, (const char*)NULL);
  4760. else
  4761. return g_strconcat (dir, file, (const char*)NULL);
  4762. }
  4763. #ifdef ENABLE_NETCORE
  4764. static MonoReflectionAssemblyHandle
  4765. try_resource_resolve_name (MonoReflectionAssemblyHandle assembly_handle, MonoStringHandle name_handle)
  4766. {
  4767. MonoObjectHandle ret;
  4768. ERROR_DECL (error);
  4769. HANDLE_FUNCTION_ENTER ();
  4770. if (mono_runtime_get_no_exec ())
  4771. goto return_null;
  4772. MONO_STATIC_POINTER_INIT (MonoMethod, resolve_method)
  4773. static gboolean inited;
  4774. if (!inited) {
  4775. MonoClass *alc_class = mono_class_get_assembly_load_context_class ();
  4776. g_assert (alc_class);
  4777. resolve_method = mono_class_get_method_from_name_checked (alc_class, "OnResourceResolve", -1, 0, error);
  4778. inited = TRUE;
  4779. }
  4780. mono_error_cleanup (error);
  4781. error_init_reuse (error);
  4782. MONO_STATIC_POINTER_INIT_END (MonoMethod, resolve_method)
  4783. if (!resolve_method)
  4784. goto return_null;
  4785. gpointer args [2];
  4786. args [0] = MONO_HANDLE_RAW (assembly_handle);
  4787. args [1] = MONO_HANDLE_RAW (name_handle);
  4788. ret = mono_runtime_try_invoke_handle (resolve_method, NULL_HANDLE, args, error);
  4789. goto_if_nok (error, return_null);
  4790. goto exit;
  4791. return_null:
  4792. ret = NULL_HANDLE;
  4793. exit:
  4794. HANDLE_FUNCTION_RETURN_REF (MonoReflectionAssembly, MONO_HANDLE_CAST (MonoReflectionAssembly, ret));
  4795. }
  4796. #endif
  4797. static void *
  4798. get_manifest_resource_internal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, gint32 *size, MonoReflectionModuleHandleOut ref_module, MonoError *error)
  4799. {
  4800. MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
  4801. MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
  4802. MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
  4803. guint32 i;
  4804. guint32 cols [MONO_MANIFEST_SIZE];
  4805. guint32 impl, file_idx;
  4806. const char *val;
  4807. MonoImage *module;
  4808. char *n = mono_string_handle_to_utf8 (name, error);
  4809. return_val_if_nok (error, NULL);
  4810. for (i = 0; i < table->rows; ++i) {
  4811. mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
  4812. val = mono_metadata_string_heap (assembly->image, cols [MONO_MANIFEST_NAME]);
  4813. if (strcmp (val, n) == 0)
  4814. break;
  4815. }
  4816. g_free (n);
  4817. if (i == table->rows)
  4818. return NULL;
  4819. /* FIXME */
  4820. impl = cols [MONO_MANIFEST_IMPLEMENTATION];
  4821. if (impl) {
  4822. /*
  4823. * this code should only be called after obtaining the
  4824. * ResourceInfo and handling the other cases.
  4825. */
  4826. g_assert ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE);
  4827. file_idx = impl >> MONO_IMPLEMENTATION_BITS;
  4828. module = mono_image_load_file_for_image_checked (assembly->image, file_idx, error);
  4829. if (!is_ok (error) || !module)
  4830. return NULL;
  4831. }
  4832. else
  4833. module = assembly->image;
  4834. MonoReflectionModuleHandle rm = mono_module_get_object_handle (domain, module, error);
  4835. return_val_if_nok (error, NULL);
  4836. MONO_HANDLE_ASSIGN (ref_module, rm);
  4837. return (void*)mono_image_get_resource (module, cols [MONO_MANIFEST_OFFSET], (guint32*)size);
  4838. }
  4839. void *
  4840. ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, gint32 *size, MonoReflectionModuleHandleOut ref_module, MonoError *error)
  4841. {
  4842. gpointer ret = get_manifest_resource_internal (assembly_h, name, size, ref_module, error);
  4843. #ifdef ENABLE_NETCORE
  4844. if (!ret) {
  4845. MonoReflectionAssemblyHandle event_assembly_h = try_resource_resolve_name (assembly_h, name);
  4846. if (MONO_HANDLE_BOOL (event_assembly_h))
  4847. ret = get_manifest_resource_internal (event_assembly_h, name, size, ref_module, error);
  4848. }
  4849. #endif
  4850. return ret;
  4851. }
  4852. static gboolean
  4853. get_manifest_resource_info_internal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, MonoManifestResourceInfoHandle info, MonoError *error)
  4854. {
  4855. HANDLE_FUNCTION_ENTER ();
  4856. MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
  4857. MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
  4858. MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
  4859. int i;
  4860. guint32 cols [MONO_MANIFEST_SIZE];
  4861. guint32 file_cols [MONO_FILE_SIZE];
  4862. const char *val;
  4863. char *n;
  4864. gboolean result = FALSE;
  4865. n = mono_string_handle_to_utf8 (name, error);
  4866. goto_if_nok (error, leave);
  4867. for (i = 0; i < table->rows; ++i) {
  4868. mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
  4869. val = mono_metadata_string_heap (assembly->image, cols [MONO_MANIFEST_NAME]);
  4870. if (strcmp (val, n) == 0)
  4871. break;
  4872. }
  4873. g_free (n);
  4874. if (i == table->rows)
  4875. goto leave;
  4876. if (!cols [MONO_MANIFEST_IMPLEMENTATION]) {
  4877. MONO_HANDLE_SETVAL (info, location, guint32, RESOURCE_LOCATION_EMBEDDED | RESOURCE_LOCATION_IN_MANIFEST);
  4878. }
  4879. else {
  4880. switch (cols [MONO_MANIFEST_IMPLEMENTATION] & MONO_IMPLEMENTATION_MASK) {
  4881. case MONO_IMPLEMENTATION_FILE:
  4882. i = cols [MONO_MANIFEST_IMPLEMENTATION] >> MONO_IMPLEMENTATION_BITS;
  4883. table = &assembly->image->tables [MONO_TABLE_FILE];
  4884. mono_metadata_decode_row (table, i - 1, file_cols, MONO_FILE_SIZE);
  4885. val = mono_metadata_string_heap (assembly->image, file_cols [MONO_FILE_NAME]);
  4886. MONO_HANDLE_SET (info, filename, mono_string_new_handle (domain, val, error));
  4887. if (file_cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA)
  4888. MONO_HANDLE_SETVAL (info, location, guint32, 0);
  4889. else
  4890. MONO_HANDLE_SETVAL (info, location, guint32, RESOURCE_LOCATION_EMBEDDED);
  4891. break;
  4892. case MONO_IMPLEMENTATION_ASSEMBLYREF:
  4893. i = cols [MONO_MANIFEST_IMPLEMENTATION] >> MONO_IMPLEMENTATION_BITS;
  4894. mono_assembly_load_reference (assembly->image, i - 1);
  4895. if (assembly->image->references [i - 1] == REFERENCE_MISSING) {
  4896. mono_error_set_file_not_found (error, NULL, "Assembly %d referenced from assembly %s not found ", i - 1, assembly->image->name);
  4897. goto leave;
  4898. }
  4899. MonoReflectionAssemblyHandle assm_obj;
  4900. assm_obj = mono_assembly_get_object_handle (mono_domain_get (), assembly->image->references [i - 1], error);
  4901. goto_if_nok (error, leave);
  4902. MONO_HANDLE_SET (info, assembly, assm_obj);
  4903. /* Obtain info recursively */
  4904. get_manifest_resource_info_internal (assm_obj, name, info, error);
  4905. goto_if_nok (error, leave);
  4906. guint32 location;
  4907. location = MONO_HANDLE_GETVAL (info, location);
  4908. location |= RESOURCE_LOCATION_ANOTHER_ASSEMBLY;
  4909. MONO_HANDLE_SETVAL (info, location, guint32, location);
  4910. break;
  4911. case MONO_IMPLEMENTATION_EXP_TYPE:
  4912. g_assert_not_reached ();
  4913. break;
  4914. }
  4915. }
  4916. result = TRUE;
  4917. leave:
  4918. HANDLE_FUNCTION_RETURN_VAL (result);
  4919. }
  4920. MonoBoolean
  4921. ves_icall_System_Reflection_RuntimeAssembly_GetManifestResourceInfoInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, MonoManifestResourceInfoHandle info_h, MonoError *error)
  4922. {
  4923. return get_manifest_resource_info_internal (assembly_h, name, info_h, error);
  4924. }
  4925. static gboolean
  4926. add_filename_to_files_array (MonoDomain *domain, MonoAssembly * assembly, MonoTableInfo *table, int i, MonoArrayHandle dest, int dest_idx, MonoError *error)
  4927. {
  4928. HANDLE_FUNCTION_ENTER();
  4929. const char *val = mono_metadata_string_heap (assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
  4930. char *n = g_concat_dir_and_file (assembly->basedir, val);
  4931. MonoStringHandle str = mono_string_new_handle (domain, n, error);
  4932. g_free (n);
  4933. goto_if_nok (error, leave);
  4934. MONO_HANDLE_ARRAY_SETREF (dest, dest_idx, str);
  4935. leave:
  4936. HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
  4937. }
  4938. MonoObjectHandle
  4939. ves_icall_System_Reflection_RuntimeAssembly_GetFilesInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, MonoBoolean resource_modules, MonoError *error)
  4940. {
  4941. MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
  4942. MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
  4943. MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_FILE];
  4944. int i, count;
  4945. /* check hash if needed */
  4946. if (!MONO_HANDLE_IS_NULL(name)) {
  4947. char *n = mono_string_handle_to_utf8 (name, error);
  4948. goto_if_nok (error, fail);
  4949. for (i = 0; i < table->rows; ++i) {
  4950. const char *val = mono_metadata_string_heap (assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
  4951. if (strcmp (val, n) == 0) {
  4952. g_free (n);
  4953. n = g_concat_dir_and_file (assembly->basedir, val);
  4954. MonoStringHandle fn = mono_string_new_handle (domain, n, error);
  4955. g_free (n);
  4956. goto_if_nok (error, fail);
  4957. return MONO_HANDLE_CAST (MonoObject, fn);
  4958. }
  4959. }
  4960. g_free (n);
  4961. return NULL_HANDLE;
  4962. }
  4963. count = 0;
  4964. for (i = 0; i < table->rows; ++i) {
  4965. if (resource_modules || !(mono_metadata_decode_row_col (table, i, MONO_FILE_FLAGS) & FILE_CONTAINS_NO_METADATA))
  4966. count ++;
  4967. }
  4968. MonoArrayHandle result;
  4969. result = mono_array_new_handle (domain, mono_defaults.string_class, count, error);
  4970. goto_if_nok (error, fail);
  4971. count = 0;
  4972. for (i = 0; i < table->rows; ++i) {
  4973. if (resource_modules || !(mono_metadata_decode_row_col (table, i, MONO_FILE_FLAGS) & FILE_CONTAINS_NO_METADATA)) {
  4974. if (!add_filename_to_files_array (domain, assembly, table, i, result, count, error))
  4975. goto fail;
  4976. count++;
  4977. }
  4978. }
  4979. return MONO_HANDLE_CAST (MonoObject, result);
  4980. fail:
  4981. return NULL_HANDLE;
  4982. }
  4983. static gboolean
  4984. add_module_to_modules_array (MonoDomain *domain, MonoArrayHandle dest, int *dest_idx, MonoImage* module, MonoError *error)
  4985. {
  4986. HANDLE_FUNCTION_ENTER ();
  4987. error_init (error);
  4988. if (module) {
  4989. MonoReflectionModuleHandle rm = mono_module_get_object_handle (domain, module, error);
  4990. goto_if_nok (error, leave);
  4991. MONO_HANDLE_ARRAY_SETREF (dest, *dest_idx, rm);
  4992. ++(*dest_idx);
  4993. }
  4994. leave:
  4995. HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
  4996. }
  4997. static gboolean
  4998. add_file_to_modules_array (MonoDomain *domain, MonoArrayHandle dest, int dest_idx, MonoImage *image, MonoTableInfo *table, int table_idx, MonoError *error)
  4999. {
  5000. HANDLE_FUNCTION_ENTER ();
  5001. guint32 cols [MONO_FILE_SIZE];
  5002. mono_metadata_decode_row (table, table_idx, cols, MONO_FILE_SIZE);
  5003. if (cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA) {
  5004. MonoReflectionModuleHandle rm = mono_module_file_get_object_handle (domain, image, table_idx, error);
  5005. goto_if_nok (error, leave);
  5006. MONO_HANDLE_ARRAY_SETREF (dest, dest_idx, rm);
  5007. } else {
  5008. MonoImage *m = mono_image_load_file_for_image_checked (image, table_idx + 1, error);
  5009. goto_if_nok (error, leave);
  5010. if (!m) {
  5011. const char *filename = mono_metadata_string_heap (image, cols [MONO_FILE_NAME]);
  5012. gboolean refonly = FALSE;
  5013. if (image->assembly)
  5014. refonly = mono_asmctx_get_kind (&image->assembly->context) == MONO_ASMCTX_REFONLY;
  5015. mono_error_set_simple_file_not_found (error, filename, refonly);
  5016. goto leave;
  5017. }
  5018. MonoReflectionModuleHandle rm = mono_module_get_object_handle (domain, m, error);
  5019. goto_if_nok (error, leave);
  5020. MONO_HANDLE_ARRAY_SETREF (dest, dest_idx, rm);
  5021. }
  5022. leave:
  5023. HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
  5024. }
  5025. MonoArrayHandle
  5026. ves_icall_System_Reflection_RuntimeAssembly_GetModulesInternal (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
  5027. {
  5028. error_init (error);
  5029. MonoDomain *domain = mono_domain_get();
  5030. MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
  5031. MonoClass *klass;
  5032. int i, j, file_count = 0;
  5033. MonoImage **modules;
  5034. guint32 module_count, real_module_count;
  5035. MonoTableInfo *table;
  5036. MonoImage *image = assembly->image;
  5037. g_assert (image != NULL);
  5038. g_assert (!assembly_is_dynamic (assembly));
  5039. table = &image->tables [MONO_TABLE_FILE];
  5040. file_count = table->rows;
  5041. modules = image->modules;
  5042. module_count = image->module_count;
  5043. real_module_count = 0;
  5044. for (i = 0; i < module_count; ++i)
  5045. if (modules [i])
  5046. real_module_count ++;
  5047. klass = mono_class_get_module_class ();
  5048. MonoArrayHandle res = mono_array_new_handle (domain, klass, 1 + real_module_count + file_count, error);
  5049. goto_if_nok (error, fail);
  5050. MonoReflectionModuleHandle image_obj;
  5051. image_obj = mono_module_get_object_handle (domain, image, error);
  5052. goto_if_nok (error, fail);
  5053. MONO_HANDLE_ARRAY_SETREF (res, 0, image_obj);
  5054. j = 1;
  5055. for (i = 0; i < module_count; ++i)
  5056. if (!add_module_to_modules_array (domain, res, &j, modules[i], error))
  5057. goto fail;
  5058. for (i = 0; i < file_count; ++i, ++j) {
  5059. if (!add_file_to_modules_array (domain, res, j, image, table, i, error))
  5060. goto fail;
  5061. }
  5062. return res;
  5063. fail:
  5064. return NULL_HANDLE_ARRAY;
  5065. }
  5066. MonoReflectionMethodHandle
  5067. ves_icall_GetCurrentMethod (MonoError *error)
  5068. {
  5069. MonoMethod *m = mono_method_get_last_managed ();
  5070. if (!m) {
  5071. mono_error_set_not_supported (error, "Stack walks are not supported on this platform.");
  5072. return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
  5073. }
  5074. while (m->is_inflated)
  5075. m = ((MonoMethodInflated*)m)->declaring;
  5076. return mono_method_get_object_handle (mono_domain_get (), m, NULL, error);
  5077. }
  5078. static MonoMethod*
  5079. mono_method_get_equivalent_method (MonoMethod *method, MonoClass *klass)
  5080. {
  5081. int offset = -1, i;
  5082. if (method->is_inflated && ((MonoMethodInflated*)method)->context.method_inst) {
  5083. ERROR_DECL (error);
  5084. MonoMethod *result;
  5085. MonoMethodInflated *inflated = (MonoMethodInflated*)method;
  5086. //method is inflated, we should inflate it on the other class
  5087. MonoGenericContext ctx;
  5088. ctx.method_inst = inflated->context.method_inst;
  5089. ctx.class_inst = inflated->context.class_inst;
  5090. if (mono_class_is_ginst (klass))
  5091. ctx.class_inst = mono_class_get_generic_class (klass)->context.class_inst;
  5092. else if (mono_class_is_gtd (klass))
  5093. ctx.class_inst = mono_class_get_generic_container (klass)->context.class_inst;
  5094. result = mono_class_inflate_generic_method_full_checked (inflated->declaring, klass, &ctx, error);
  5095. g_assert (is_ok (error)); /* FIXME don't swallow the error */
  5096. return result;
  5097. }
  5098. mono_class_setup_methods (method->klass);
  5099. if (mono_class_has_failure (method->klass))
  5100. return NULL;
  5101. int mcount = mono_class_get_method_count (method->klass);
  5102. MonoMethod **method_klass_methods = m_class_get_methods (method->klass);
  5103. for (i = 0; i < mcount; ++i) {
  5104. if (method_klass_methods [i] == method) {
  5105. offset = i;
  5106. break;
  5107. }
  5108. }
  5109. mono_class_setup_methods (klass);
  5110. if (mono_class_has_failure (klass))
  5111. return NULL;
  5112. g_assert (offset >= 0 && offset < mono_class_get_method_count (klass));
  5113. return m_class_get_methods (klass) [offset];
  5114. }
  5115. MonoReflectionMethodHandle
  5116. ves_icall_System_Reflection_RuntimeMethodInfo_GetMethodFromHandleInternalType_native (MonoMethod *method, MonoType *type, MonoBoolean generic_check, MonoError *error)
  5117. {
  5118. MonoClass *klass;
  5119. if (type && generic_check) {
  5120. klass = mono_class_from_mono_type_internal (type);
  5121. if (mono_class_get_generic_type_definition (method->klass) != mono_class_get_generic_type_definition (klass))
  5122. return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
  5123. if (method->klass != klass) {
  5124. method = mono_method_get_equivalent_method (method, klass);
  5125. if (!method)
  5126. return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
  5127. }
  5128. } else if (type)
  5129. klass = mono_class_from_mono_type_internal (type);
  5130. else
  5131. klass = method->klass;
  5132. return mono_method_get_object_handle (mono_domain_get (), method, klass, error);
  5133. }
  5134. MonoReflectionMethodBodyHandle
  5135. ves_icall_System_Reflection_RuntimeMethodInfo_GetMethodBodyInternal (MonoMethod *method, MonoError *error)
  5136. {
  5137. return mono_method_body_get_object_handle (mono_domain_get (), method, error);
  5138. }
  5139. #if ENABLE_NETCORE
  5140. MonoReflectionAssemblyHandle
  5141. ves_icall_System_Reflection_Assembly_GetExecutingAssembly (MonoStackCrawlMark *stack_mark, MonoError *error)
  5142. {
  5143. MonoAssembly *assembly;
  5144. assembly = mono_runtime_get_caller_from_stack_mark (stack_mark);
  5145. g_assert (assembly);
  5146. return mono_assembly_get_object_handle (mono_domain_get (), assembly, error);
  5147. }
  5148. #else
  5149. MonoReflectionAssemblyHandle
  5150. ves_icall_System_Reflection_Assembly_GetExecutingAssembly (MonoError *error)
  5151. {
  5152. MonoMethod *dest = NULL;
  5153. mono_stack_walk_no_il (get_executing, &dest);
  5154. g_assert (dest);
  5155. return mono_assembly_get_object_handle (mono_domain_get (), m_class_get_image (dest->klass)->assembly, error);
  5156. }
  5157. #endif
  5158. MonoReflectionAssemblyHandle
  5159. ves_icall_System_Reflection_Assembly_GetEntryAssembly (MonoError *error)
  5160. {
  5161. MonoDomain* domain = mono_domain_get ();
  5162. if (!domain->entry_assembly)
  5163. return MONO_HANDLE_CAST (MonoReflectionAssembly, NULL_HANDLE);
  5164. return mono_assembly_get_object_handle (domain, domain->entry_assembly, error);
  5165. }
  5166. MonoReflectionAssemblyHandle
  5167. ves_icall_System_Reflection_Assembly_GetCallingAssembly (MonoError *error)
  5168. {
  5169. error_init (error);
  5170. MonoMethod *m;
  5171. MonoMethod *dest;
  5172. dest = NULL;
  5173. mono_stack_walk_no_il (get_executing, &dest);
  5174. m = dest;
  5175. mono_stack_walk_no_il (get_caller_no_reflection, &dest);
  5176. if (!dest)
  5177. dest = m;
  5178. if (!m) {
  5179. mono_error_set_not_supported (error, "Stack walks are not supported on this platform.");
  5180. return MONO_HANDLE_CAST (MonoReflectionAssembly, NULL_HANDLE);
  5181. }
  5182. return mono_assembly_get_object_handle (mono_domain_get (), m_class_get_image (dest->klass)->assembly, error);
  5183. }
  5184. MonoStringHandle
  5185. ves_icall_System_RuntimeType_getFullName (MonoReflectionTypeHandle object, MonoBoolean full_name,
  5186. MonoBoolean assembly_qualified, MonoError *error)
  5187. {
  5188. MonoDomain *domain = mono_object_domain (MONO_HANDLE_RAW (object));
  5189. MonoType *type = MONO_HANDLE_RAW (object)->type;
  5190. MonoTypeNameFormat format;
  5191. MonoStringHandle res;
  5192. gchar *name;
  5193. if (full_name)
  5194. format = assembly_qualified ?
  5195. MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED :
  5196. MONO_TYPE_NAME_FORMAT_FULL_NAME;
  5197. else
  5198. format = MONO_TYPE_NAME_FORMAT_REFLECTION;
  5199. name = mono_type_get_name_full (type, format);
  5200. if (!name)
  5201. return NULL_HANDLE_STRING;
  5202. if (full_name && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)) {
  5203. g_free (name);
  5204. return NULL_HANDLE_STRING;
  5205. }
  5206. res = mono_string_new_handle (domain, name, error);
  5207. g_free (name);
  5208. return res;
  5209. }
  5210. #ifndef ENABLE_NETCORE
  5211. int
  5212. ves_icall_RuntimeType_get_core_clr_security_level (MonoReflectionTypeHandle rfield, MonoError *error)
  5213. {
  5214. MonoType *type = MONO_HANDLE_GETVAL (rfield, type);
  5215. MonoClass *klass = mono_class_from_mono_type_internal (type);
  5216. mono_class_init_checked (klass, error);
  5217. return_val_if_nok (error, -1);
  5218. return mono_security_core_clr_class_level (klass);
  5219. }
  5220. int
  5221. ves_icall_RuntimeFieldInfo_get_core_clr_security_level (MonoReflectionFieldHandle rfield, MonoError *error)
  5222. {
  5223. MonoClassField *field = MONO_HANDLE_GETVAL (rfield, field);
  5224. return mono_security_core_clr_field_level (field, TRUE);
  5225. }
  5226. int
  5227. ves_icall_RuntimeMethodInfo_get_core_clr_security_level (MonoReflectionMethodHandle rfield, MonoError *error)
  5228. {
  5229. MonoMethod *method = MONO_HANDLE_GETVAL (rfield, method);
  5230. return mono_security_core_clr_method_level (method, TRUE);
  5231. }
  5232. #endif
  5233. MonoStringHandle
  5234. ves_icall_System_Reflection_RuntimeAssembly_get_fullname (MonoReflectionAssemblyHandle assembly, MonoError *error)
  5235. {
  5236. MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
  5237. MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly, assembly);
  5238. gchar *name;
  5239. name = mono_stringify_assembly_name (&mass->aname);
  5240. MonoStringHandle res = mono_string_new_handle (domain, name, error);
  5241. g_free (name);
  5242. return res;
  5243. }
  5244. MonoAssemblyName *
  5245. ves_icall_System_Reflection_AssemblyName_GetNativeName (MonoAssembly *mass)
  5246. {
  5247. return &mass->aname;
  5248. }
  5249. void
  5250. ves_icall_System_Reflection_Assembly_InternalGetAssemblyName (MonoStringHandle fname, MonoAssemblyName *name, MonoStringHandleOut normalized_codebase, MonoError *error)
  5251. {
  5252. char *filename;
  5253. MonoImageOpenStatus status = MONO_IMAGE_OK;
  5254. char *codebase = NULL;
  5255. gboolean res;
  5256. MonoImage *image;
  5257. char *dirname;
  5258. error_init (error);
  5259. MonoDomain *domain = MONO_HANDLE_DOMAIN (fname);
  5260. filename = mono_string_handle_to_utf8 (fname, error);
  5261. return_if_nok (error);
  5262. dirname = g_path_get_dirname (filename);
  5263. replace_shadow_path (mono_domain_get (), dirname, &filename);
  5264. g_free (dirname);
  5265. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_ASSEMBLY, "InternalGetAssemblyName (\"%s\")", filename);
  5266. MonoAssemblyLoadContext *alc = mono_domain_default_alc (domain);
  5267. image = mono_image_open_a_lot (alc, filename, &status, TRUE, FALSE);
  5268. if (!image){
  5269. if (status == MONO_IMAGE_IMAGE_INVALID)
  5270. mono_error_set_bad_image_by_name (error, filename, "Invalid Image: %s", filename);
  5271. else
  5272. mono_error_set_simple_file_not_found (error, filename, FALSE);
  5273. g_free (filename);
  5274. return;
  5275. }
  5276. res = mono_assembly_fill_assembly_name_full (image, name, TRUE);
  5277. if (!res) {
  5278. mono_image_close (image);
  5279. g_free (filename);
  5280. mono_error_set_argument (error, "assemblyFile", "The file does not contain a manifest");
  5281. return;
  5282. }
  5283. if (filename != NULL && *filename != '\0') {
  5284. gchar *result;
  5285. codebase = g_strdup (filename);
  5286. mono_icall_make_platform_path (codebase);
  5287. const gchar *prepend = mono_icall_get_file_path_prefix (codebase);
  5288. result = g_strconcat (prepend, codebase, (const char*)NULL);
  5289. g_free (codebase);
  5290. codebase = result;
  5291. }
  5292. MONO_HANDLE_ASSIGN (normalized_codebase, mono_string_new_handle (mono_domain_get (), codebase, error));
  5293. g_free (codebase);
  5294. mono_image_close (image);
  5295. g_free (filename);
  5296. }
  5297. #ifndef ENABLE_NETCORE
  5298. MonoBoolean
  5299. ves_icall_System_Reflection_RuntimeAssembly_LoadPermissions (MonoReflectionAssemblyHandle assembly_h,
  5300. char **minimum, guint32 *minLength, char **optional, guint32 *optLength, char **refused, guint32 *refLength, MonoError *error)
  5301. {
  5302. MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
  5303. MonoBoolean result = FALSE;
  5304. MonoDeclSecurityEntry entry;
  5305. /* SecurityAction.RequestMinimum */
  5306. if (mono_declsec_get_assembly_action (assembly, SECURITY_ACTION_REQMIN, &entry)) {
  5307. *minimum = entry.blob;
  5308. *minLength = entry.size;
  5309. result = TRUE;
  5310. }
  5311. /* SecurityAction.RequestOptional */
  5312. if (mono_declsec_get_assembly_action (assembly, SECURITY_ACTION_REQOPT, &entry)) {
  5313. *optional = entry.blob;
  5314. *optLength = entry.size;
  5315. result = TRUE;
  5316. }
  5317. /* SecurityAction.RequestRefuse */
  5318. if (mono_declsec_get_assembly_action (assembly, SECURITY_ACTION_REQREFUSE, &entry)) {
  5319. *refused = entry.blob;
  5320. *refLength = entry.size;
  5321. result = TRUE;
  5322. }
  5323. return result;
  5324. }
  5325. #endif
  5326. static gboolean
  5327. mono_module_type_is_visible (MonoTableInfo *tdef, MonoImage *image, int type)
  5328. {
  5329. guint32 attrs, visibility;
  5330. do {
  5331. attrs = mono_metadata_decode_row_col (tdef, type - 1, MONO_TYPEDEF_FLAGS);
  5332. visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
  5333. if (visibility != TYPE_ATTRIBUTE_PUBLIC && visibility != TYPE_ATTRIBUTE_NESTED_PUBLIC)
  5334. return FALSE;
  5335. } while ((type = mono_metadata_token_index (mono_metadata_nested_in_typedef (image, type))));
  5336. return TRUE;
  5337. }
  5338. static void
  5339. image_get_type (MonoDomain *domain, MonoImage *image, MonoTableInfo *tdef, int table_idx, int count, MonoArrayHandle res, MonoArrayHandle exceptions, MonoBoolean exportedOnly, MonoError *error)
  5340. {
  5341. error_init (error);
  5342. HANDLE_FUNCTION_ENTER ();
  5343. ERROR_DECL (klass_error);
  5344. MonoClass *klass = mono_class_get_checked (image, table_idx | MONO_TOKEN_TYPE_DEF, klass_error);
  5345. if (klass) {
  5346. MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
  5347. return_if_nok (error);
  5348. MONO_HANDLE_ARRAY_SETREF (res, count, rt);
  5349. } else {
  5350. MonoExceptionHandle ex = mono_error_convert_to_exception_handle (klass_error);
  5351. MONO_HANDLE_ARRAY_SETREF (exceptions, count, ex);
  5352. }
  5353. HANDLE_FUNCTION_RETURN ();
  5354. }
  5355. static MonoArrayHandle
  5356. mono_module_get_types (MonoDomain *domain, MonoImage *image, MonoArrayHandleOut exceptions, MonoBoolean exportedOnly, MonoError *error)
  5357. {
  5358. MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
  5359. int i, count;
  5360. error_init (error);
  5361. /* we start the count from 1 because we skip the special type <Module> */
  5362. if (exportedOnly) {
  5363. count = 0;
  5364. for (i = 1; i < tdef->rows; ++i) {
  5365. if (mono_module_type_is_visible (tdef, image, i + 1))
  5366. count++;
  5367. }
  5368. } else {
  5369. count = tdef->rows - 1;
  5370. }
  5371. MonoArrayHandle res = mono_array_new_handle (domain, mono_defaults.runtimetype_class, count, error);
  5372. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  5373. MONO_HANDLE_ASSIGN (exceptions, mono_array_new_handle (domain, mono_defaults.exception_class, count, error));
  5374. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  5375. count = 0;
  5376. for (i = 1; i < tdef->rows; ++i) {
  5377. if (!exportedOnly || mono_module_type_is_visible (tdef, image, i+1)) {
  5378. image_get_type (domain, image, tdef, i + 1, count, res, exceptions, exportedOnly, error);
  5379. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  5380. count++;
  5381. }
  5382. }
  5383. return res;
  5384. }
  5385. static void
  5386. append_module_types (MonoDomain *domain, MonoArrayHandleOut res, MonoArrayHandleOut exceptions, MonoImage *image, MonoBoolean exportedOnly, MonoError *error)
  5387. {
  5388. HANDLE_FUNCTION_ENTER ();
  5389. error_init (error);
  5390. MonoArrayHandle ex2 = MONO_HANDLE_NEW (MonoArray, NULL);
  5391. MonoArrayHandle res2 = mono_module_get_types (domain, image, ex2, exportedOnly, error);
  5392. goto_if_nok (error, leave);
  5393. /* Append the new types to the end of the array */
  5394. if (mono_array_handle_length (res2) > 0) {
  5395. guint32 len1, len2;
  5396. len1 = mono_array_handle_length (res);
  5397. len2 = mono_array_handle_length (res2);
  5398. MonoArrayHandle res3 = mono_array_new_handle (domain, mono_defaults.runtimetype_class, len1 + len2, error);
  5399. goto_if_nok (error, leave);
  5400. mono_array_handle_memcpy_refs (res3, 0, res, 0, len1);
  5401. mono_array_handle_memcpy_refs (res3, len1, res2, 0, len2);
  5402. MONO_HANDLE_ASSIGN (res, res3);
  5403. MonoArrayHandle ex3 = mono_array_new_handle (domain, mono_defaults.runtimetype_class, len1 + len2, error);
  5404. goto_if_nok (error, leave);
  5405. mono_array_handle_memcpy_refs (ex3, 0, exceptions, 0, len1);
  5406. mono_array_handle_memcpy_refs (ex3, len1, ex2, 0, len2);
  5407. MONO_HANDLE_ASSIGN (exceptions, ex3);
  5408. }
  5409. leave:
  5410. HANDLE_FUNCTION_RETURN ();
  5411. }
  5412. static void
  5413. set_class_failure_in_array (MonoArrayHandle exl, int i, MonoClass *klass)
  5414. {
  5415. HANDLE_FUNCTION_ENTER ();
  5416. ERROR_DECL (unboxed_error);
  5417. mono_error_set_for_class_failure (unboxed_error, klass);
  5418. MonoExceptionHandle exc = MONO_HANDLE_NEW (MonoException, mono_error_convert_to_exception (unboxed_error));
  5419. MONO_HANDLE_ARRAY_SETREF (exl, i, exc);
  5420. HANDLE_FUNCTION_RETURN ();
  5421. }
  5422. static MonoArrayHandle
  5423. assembly_get_types (MonoReflectionAssemblyHandle assembly_handle, MonoBoolean exportedOnly, MonoError *error)
  5424. {
  5425. MonoArrayHandle exceptions = MONO_HANDLE_NEW(MonoArray, NULL);
  5426. int i;
  5427. MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_handle);
  5428. MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_handle, assembly);
  5429. g_assert (!assembly_is_dynamic (assembly));
  5430. MonoImage *image = assembly->image;
  5431. MonoTableInfo *table = &image->tables [MONO_TABLE_FILE];
  5432. MonoArrayHandle res = mono_module_get_types (domain, image, exceptions, exportedOnly, error);
  5433. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  5434. /* Append data from all modules in the assembly */
  5435. for (i = 0; i < table->rows; ++i) {
  5436. if (!(mono_metadata_decode_row_col (table, i, MONO_FILE_FLAGS) & FILE_CONTAINS_NO_METADATA)) {
  5437. MonoImage *loaded_image = mono_assembly_load_module_checked (image->assembly, i + 1, error);
  5438. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  5439. if (loaded_image) {
  5440. append_module_types (domain, res, exceptions, loaded_image, exportedOnly, error);
  5441. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  5442. }
  5443. }
  5444. }
  5445. /* the ReflectionTypeLoadException must have all the types (Types property),
  5446. * NULL replacing types which throws an exception. The LoaderException must
  5447. * contain all exceptions for NULL items.
  5448. */
  5449. int len = mono_array_handle_length (res);
  5450. int ex_count = 0;
  5451. GList *list = NULL;
  5452. MonoReflectionTypeHandle t = MONO_HANDLE_NEW (MonoReflectionType, NULL);
  5453. for (i = 0; i < len; i++) {
  5454. MONO_HANDLE_ARRAY_GETREF (t, res, i);
  5455. if (!MONO_HANDLE_IS_NULL (t)) {
  5456. MonoClass *klass = mono_type_get_class_internal (MONO_HANDLE_GETVAL (t, type));
  5457. if ((klass != NULL) && mono_class_has_failure (klass)) {
  5458. /* keep the class in the list */
  5459. list = g_list_append (list, klass);
  5460. /* and replace Type with NULL */
  5461. MONO_HANDLE_ARRAY_SETREF (res, i, NULL_HANDLE);
  5462. }
  5463. } else {
  5464. ex_count ++;
  5465. }
  5466. }
  5467. if (list || ex_count) {
  5468. GList *tmp = NULL;
  5469. int j, length = g_list_length (list) + ex_count;
  5470. MonoArrayHandle exl = mono_array_new_handle (domain, mono_defaults.exception_class, length, error);
  5471. if (!is_ok (error)) {
  5472. g_list_free (list);
  5473. return NULL_HANDLE_ARRAY;
  5474. }
  5475. /* Types for which mono_class_get_checked () succeeded */
  5476. MonoExceptionHandle exc = MONO_HANDLE_NEW (MonoException, NULL);
  5477. for (i = 0, tmp = list; tmp; i++, tmp = tmp->next) {
  5478. set_class_failure_in_array (exl, i, (MonoClass*)tmp->data);
  5479. }
  5480. /* Types for which it don't */
  5481. for (j = 0; j < mono_array_handle_length (exceptions); ++j) {
  5482. MONO_HANDLE_ARRAY_GETREF (exc, exceptions, j);
  5483. if (!MONO_HANDLE_IS_NULL (exc)) {
  5484. g_assert (i < length);
  5485. MONO_HANDLE_ARRAY_SETREF (exl, i, exc);
  5486. i ++;
  5487. }
  5488. }
  5489. g_list_free (list);
  5490. list = NULL;
  5491. MONO_HANDLE_ASSIGN (exc, mono_get_exception_reflection_type_load_checked (res, exl, error));
  5492. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  5493. mono_error_set_exception_handle (error, exc);
  5494. return NULL_HANDLE_ARRAY;
  5495. }
  5496. return res;
  5497. }
  5498. #ifndef ENABLE_NETCORE
  5499. MonoArrayHandle
  5500. ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssemblyHandle assembly_handle, MonoBoolean exportedOnly, MonoError *error)
  5501. {
  5502. return assembly_get_types (assembly_handle, exportedOnly, error);
  5503. }
  5504. #endif
  5505. #if ENABLE_NETCORE
  5506. MonoArrayHandle
  5507. ves_icall_System_Reflection_RuntimeAssembly_GetExportedTypes (MonoReflectionAssemblyHandle assembly_handle, MonoError *error)
  5508. {
  5509. return assembly_get_types (assembly_handle, TRUE, error);
  5510. }
  5511. static void
  5512. get_top_level_forwarded_type (MonoImage *image, MonoTableInfo *table, int i, MonoArrayHandle types, MonoArrayHandle exceptions, int *aindex, int *exception_count)
  5513. {
  5514. ERROR_DECL (local_error);
  5515. guint32 cols [MONO_EXP_TYPE_SIZE];
  5516. MonoClass *klass;
  5517. MonoReflectionTypeHandle rt;
  5518. mono_metadata_decode_row (table, i, cols, MONO_EXP_TYPE_SIZE);
  5519. if (!(cols [MONO_EXP_TYPE_FLAGS] & TYPE_ATTRIBUTE_FORWARDER))
  5520. return;
  5521. guint32 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
  5522. const char *name = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAME]);
  5523. const char *nspace = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAMESPACE]);
  5524. g_assert ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_ASSEMBLYREF);
  5525. guint32 assembly_idx = impl >> MONO_IMPLEMENTATION_BITS;
  5526. mono_assembly_load_reference (image, assembly_idx - 1);
  5527. g_assert (image->references [assembly_idx - 1]);
  5528. HANDLE_FUNCTION_ENTER ();
  5529. if (image->references [assembly_idx - 1] == REFERENCE_MISSING) {
  5530. MonoExceptionHandle ex = MONO_HANDLE_NEW (MonoException, mono_get_exception_bad_image_format ("Invalid image"));
  5531. MONO_HANDLE_ARRAY_SETREF (types, *aindex, NULL_HANDLE);
  5532. MONO_HANDLE_ARRAY_SETREF (exceptions, *aindex, ex);
  5533. (*exception_count)++; (*aindex)++;
  5534. goto exit;
  5535. }
  5536. klass = mono_class_from_name_checked (image->references [assembly_idx - 1]->image, nspace, name, local_error);
  5537. if (!is_ok (local_error)) {
  5538. MonoExceptionHandle ex = mono_error_convert_to_exception_handle (local_error);
  5539. MONO_HANDLE_ARRAY_SETREF (types, *aindex, NULL_HANDLE);
  5540. MONO_HANDLE_ARRAY_SETREF (exceptions, *aindex, ex);
  5541. mono_error_cleanup (local_error);
  5542. (*exception_count)++; (*aindex)++;
  5543. goto exit;
  5544. }
  5545. rt = mono_type_get_object_handle (mono_domain_get (), m_class_get_byval_arg (klass), local_error);
  5546. if (!is_ok (local_error)) {
  5547. MonoExceptionHandle ex = mono_error_convert_to_exception_handle (local_error);
  5548. MONO_HANDLE_ARRAY_SETREF (types, *aindex, NULL_HANDLE);
  5549. MONO_HANDLE_ARRAY_SETREF (exceptions, *aindex, ex);
  5550. mono_error_cleanup (local_error);
  5551. (*exception_count)++; (*aindex)++;
  5552. goto exit;
  5553. }
  5554. MONO_HANDLE_ARRAY_SETREF (types, *aindex, rt);
  5555. MONO_HANDLE_ARRAY_SETREF (exceptions, *aindex, NULL_HANDLE);
  5556. (*aindex)++;
  5557. exit:
  5558. HANDLE_FUNCTION_RETURN ();
  5559. }
  5560. MonoArrayHandle
  5561. ves_icall_System_Reflection_RuntimeAssembly_GetTopLevelForwardedTypes (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
  5562. {
  5563. MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
  5564. MonoImage *image = assembly->image;
  5565. int count = 0;
  5566. g_assert (!assembly_is_dynamic (assembly));
  5567. MonoTableInfo *table = &image->tables [MONO_TABLE_EXPORTEDTYPE];
  5568. for (int i = 0; i < table->rows; ++i) {
  5569. if (mono_metadata_decode_row_col (table, i, MONO_EXP_TYPE_FLAGS) & TYPE_ATTRIBUTE_FORWARDER)
  5570. count ++;
  5571. }
  5572. MonoArrayHandle types = mono_array_new_handle (mono_domain_get (), mono_defaults.runtimetype_class, count, error);
  5573. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  5574. MonoArrayHandle exceptions = mono_array_new_handle (mono_domain_get (), mono_defaults.exception_class, count, error);
  5575. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  5576. int aindex = 0;
  5577. int exception_count = 0;
  5578. for (int i = 0; i < table->rows; ++i) {
  5579. get_top_level_forwarded_type (image, table, i, types, exceptions, &aindex, &exception_count);
  5580. }
  5581. if (exception_count > 0) {
  5582. MonoExceptionHandle exc = MONO_HANDLE_NEW (MonoException, NULL);
  5583. MONO_HANDLE_ASSIGN (exc, mono_get_exception_reflection_type_load_checked (types, exceptions, error));
  5584. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  5585. mono_error_set_exception_handle (error, exc);
  5586. return NULL_HANDLE_ARRAY;
  5587. }
  5588. return types;
  5589. }
  5590. #endif
  5591. void
  5592. ves_icall_Mono_RuntimeMarshal_FreeAssemblyName (MonoAssemblyName *aname, MonoBoolean free_struct)
  5593. {
  5594. mono_assembly_name_free_internal (aname);
  5595. if (free_struct)
  5596. g_free (aname);
  5597. }
  5598. void
  5599. ves_icall_Mono_Runtime_DisableMicrosoftTelemetry (void)
  5600. {
  5601. #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
  5602. mono_merp_disable ();
  5603. #else
  5604. // Icall has platform check in managed too.
  5605. g_assert_not_reached ();
  5606. #endif
  5607. }
  5608. void
  5609. ves_icall_Mono_Runtime_AnnotateMicrosoftTelemetry (const char *key, const char *value)
  5610. {
  5611. #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
  5612. if (!mono_merp_enabled ())
  5613. g_error ("Cannot add attributes to telemetry without enabling subsystem");
  5614. mono_merp_add_annotation (key, value);
  5615. #else
  5616. // Icall has platform check in managed too.
  5617. g_assert_not_reached ();
  5618. #endif
  5619. }
  5620. void
  5621. ves_icall_Mono_Runtime_EnableMicrosoftTelemetry (const char *appBundleID, const char *appSignature, const char *appVersion, const char *merpGUIPath, const char *appPath, const char *configDir, MonoError *error)
  5622. {
  5623. #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
  5624. mono_merp_enable (appBundleID, appSignature, appVersion, merpGUIPath, appPath, configDir);
  5625. mono_get_runtime_callbacks ()->install_state_summarizer ();
  5626. #else
  5627. // Icall has platform check in managed too.
  5628. g_assert_not_reached ();
  5629. #endif
  5630. }
  5631. // Number derived from trials on relevant hardware.
  5632. // If it seems large, please confirm it's safe to shrink
  5633. // before doing so.
  5634. #define MONO_MAX_SUMMARY_LEN_ICALL 500000
  5635. MonoStringHandle
  5636. ves_icall_Mono_Runtime_ExceptionToState (MonoExceptionHandle exc_handle, guint64 *portable_hash_out, guint64 *unportable_hash_out, MonoError *error)
  5637. {
  5638. MonoStringHandle result;
  5639. #ifndef DISABLE_CRASH_REPORTING
  5640. if (mono_get_eh_callbacks ()->mono_summarize_exception) {
  5641. // FIXME: Push handles down into mini/mini-exceptions.c
  5642. MonoException *exc = MONO_HANDLE_RAW (exc_handle);
  5643. MonoThreadSummary out;
  5644. mono_summarize_timeline_start ("ExceptionToState");
  5645. mono_summarize_timeline_phase_log (MonoSummarySuspendHandshake);
  5646. mono_summarize_timeline_phase_log (MonoSummaryUnmanagedStacks);
  5647. mono_get_eh_callbacks ()->mono_summarize_exception (exc, &out);
  5648. mono_summarize_timeline_phase_log (MonoSummaryManagedStacks);
  5649. *portable_hash_out = (guint64) out.hashes.offset_free_hash;
  5650. *unportable_hash_out = (guint64) out.hashes.offset_rich_hash;
  5651. MonoStateWriter writer;
  5652. char *scratch = g_new0 (gchar, MONO_MAX_SUMMARY_LEN_ICALL);
  5653. mono_state_writer_init (&writer, scratch, MONO_MAX_SUMMARY_LEN_ICALL);
  5654. mono_native_state_init (&writer);
  5655. mono_summarize_timeline_phase_log (MonoSummaryStateWriter);
  5656. gboolean first_thread_added = TRUE;
  5657. mono_native_state_add_thread (&writer, &out, NULL, first_thread_added, TRUE);
  5658. char *output = mono_native_state_free (&writer, FALSE);
  5659. mono_summarize_timeline_phase_log (MonoSummaryStateWriterDone);
  5660. result = mono_string_new_handle (mono_domain_get (), output, error);
  5661. g_free (output);
  5662. g_free (scratch);
  5663. return result;
  5664. }
  5665. #endif
  5666. *portable_hash_out = 0;
  5667. *unportable_hash_out = 0;
  5668. result = mono_string_new_handle (mono_domain_get (), "", error);
  5669. return result;
  5670. }
  5671. void
  5672. ves_icall_Mono_Runtime_SendMicrosoftTelemetry (const char *payload, guint64 portable_hash, guint64 unportable_hash, MonoError *error)
  5673. {
  5674. #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
  5675. if (!mono_merp_enabled ())
  5676. g_error ("Cannot send telemetry without registering parameters first");
  5677. pid_t crashed_pid = getpid ();
  5678. MonoStackHash hashes;
  5679. memset (&hashes, 0, sizeof (MonoStackHash));
  5680. hashes.offset_free_hash = portable_hash;
  5681. hashes.offset_rich_hash = unportable_hash;
  5682. const char *signal = "MANAGED_EXCEPTION";
  5683. gboolean success = mono_merp_invoke (crashed_pid, signal, payload, &hashes);
  5684. if (!success) {
  5685. //g_assert_not_reached ();
  5686. mono_error_set_generic_error (error, "System", "Exception", "We were unable to start the Microsoft Error Reporting client.");
  5687. }
  5688. #else
  5689. // Icall has platform check in managed too.
  5690. g_assert_not_reached ();
  5691. #endif
  5692. }
  5693. void
  5694. ves_icall_Mono_Runtime_DumpTelemetry (const char *payload, guint64 portable_hash, guint64 unportable_hash, MonoError *error)
  5695. {
  5696. #ifndef DISABLE_CRASH_REPORTING
  5697. MonoStackHash hashes;
  5698. memset (&hashes, 0, sizeof (MonoStackHash));
  5699. hashes.offset_free_hash = portable_hash;
  5700. hashes.offset_rich_hash = unportable_hash;
  5701. mono_crash_dump (payload, &hashes);
  5702. #else
  5703. return;
  5704. #endif
  5705. }
  5706. MonoStringHandle
  5707. ves_icall_Mono_Runtime_DumpStateSingle (guint64 *portable_hash, guint64 *unportable_hash, MonoError *error)
  5708. {
  5709. MonoStringHandle result;
  5710. #ifndef DISABLE_CRASH_REPORTING
  5711. MonoStackHash hashes;
  5712. memset (&hashes, 0, sizeof (MonoStackHash));
  5713. MonoContext *ctx = NULL;
  5714. MonoThreadSummary this_thread;
  5715. if (!mono_threads_summarize_one (&this_thread, ctx))
  5716. return mono_string_new_handle (mono_domain_get (), "", error);
  5717. *portable_hash = (guint64) this_thread.hashes.offset_free_hash;
  5718. *unportable_hash = (guint64) this_thread.hashes.offset_rich_hash;
  5719. MonoStateWriter writer;
  5720. char *scratch = g_new0 (gchar, MONO_MAX_SUMMARY_LEN_ICALL);
  5721. mono_state_writer_init (&writer, scratch, MONO_MAX_SUMMARY_LEN_ICALL);
  5722. mono_native_state_init (&writer);
  5723. gboolean first_thread_added = TRUE;
  5724. mono_native_state_add_thread (&writer, &this_thread, NULL, first_thread_added, TRUE);
  5725. char *output = mono_native_state_free (&writer, FALSE);
  5726. result = mono_string_new_handle (mono_domain_get (), output, error);
  5727. g_free (output);
  5728. g_free (scratch);
  5729. #else
  5730. *portable_hash = 0;
  5731. *unportable_hash = 0;
  5732. result = mono_string_new_handle (mono_domain_get (), "", error);
  5733. #endif
  5734. return result;
  5735. }
  5736. void
  5737. ves_icall_Mono_Runtime_RegisterReportingForNativeLib (const char *path_suffix, const char *module_name)
  5738. {
  5739. #ifndef DISABLE_CRASH_REPORTING
  5740. if (mono_get_eh_callbacks ()->mono_register_native_library)
  5741. mono_get_eh_callbacks ()->mono_register_native_library (path_suffix, module_name);
  5742. #endif
  5743. }
  5744. void
  5745. ves_icall_Mono_Runtime_RegisterReportingForAllNativeLibs ()
  5746. {
  5747. #ifndef DISABLE_CRASH_REPORTING
  5748. if (mono_get_eh_callbacks ()->mono_allow_all_native_libraries)
  5749. mono_get_eh_callbacks ()->mono_allow_all_native_libraries ();
  5750. #endif
  5751. }
  5752. void
  5753. ves_icall_Mono_Runtime_EnableCrashReportingLog (const char *directory)
  5754. {
  5755. #ifndef DISABLE_CRASH_REPORTING
  5756. mono_summarize_set_timeline_dir (directory);
  5757. #endif
  5758. }
  5759. int
  5760. ves_icall_Mono_Runtime_CheckCrashReportingLog (const char *directory, MonoBoolean clear)
  5761. {
  5762. int ret = 0;
  5763. #ifndef DISABLE_CRASH_REPORTING
  5764. ret = (int) mono_summarize_timeline_read_level (directory, clear != 0);
  5765. #endif
  5766. return ret;
  5767. }
  5768. MonoStringHandle
  5769. ves_icall_Mono_Runtime_DumpStateTotal (guint64 *portable_hash, guint64 *unportable_hash, MonoError *error)
  5770. {
  5771. MonoStringHandle result;
  5772. #ifndef DISABLE_CRASH_REPORTING
  5773. char *scratch = g_new0 (gchar, MONO_MAX_SUMMARY_LEN_ICALL);
  5774. char *out;
  5775. MonoStackHash hashes;
  5776. memset (&hashes, 0, sizeof (MonoStackHash));
  5777. MonoContext *ctx = NULL;
  5778. while (!mono_dump_start ())
  5779. g_usleep (1000); // wait around for other dump to finish
  5780. mono_get_runtime_callbacks ()->install_state_summarizer ();
  5781. mono_summarize_timeline_start ("DumpStateTotal");
  5782. gboolean success = mono_threads_summarize (ctx, &out, &hashes, TRUE, FALSE, scratch, MONO_MAX_SUMMARY_LEN_ICALL);
  5783. mono_summarize_timeline_phase_log (MonoSummaryCleanup);
  5784. if (!success)
  5785. return mono_string_new_handle (mono_domain_get (), "", error);
  5786. *portable_hash = (guint64) hashes.offset_free_hash;
  5787. *unportable_hash = (guint64) hashes.offset_rich_hash;
  5788. result = mono_string_new_handle (mono_domain_get (), out, error);
  5789. // out is now a pointer into garbage memory
  5790. g_free (scratch);
  5791. mono_summarize_timeline_phase_log (MonoSummaryDone);
  5792. mono_dump_complete ();
  5793. #else
  5794. *portable_hash = 0;
  5795. *unportable_hash = 0;
  5796. result = mono_string_new_handle (mono_domain_get (), "", error);
  5797. #endif
  5798. return result;
  5799. }
  5800. #if defined (ENABLE_NETCORE) && defined (ENABLE_METADATA_UPDATE)
  5801. void
  5802. ves_icall_Mono_Runtime_LoadMetadataUpdate (MonoAssembly *assm,
  5803. gconstpointer dmeta_bytes, int32_t dmeta_len,
  5804. gconstpointer dil_bytes, int32_t dil_len)
  5805. {
  5806. ERROR_DECL (error);
  5807. g_assert (assm);
  5808. g_assert (dmeta_len >= 0);
  5809. MonoImage *image_base = assm->image;
  5810. g_assert (image_base);
  5811. MonoDomain *domain = mono_domain_get ();
  5812. mono_image_load_enc_delta (domain, image_base, dmeta_bytes, dmeta_len, dil_bytes, dil_len, error);
  5813. mono_error_set_pending_exception (error);
  5814. }
  5815. #endif
  5816. MonoBoolean
  5817. ves_icall_System_Reflection_AssemblyName_ParseAssemblyName (const char *name, MonoAssemblyName *aname, MonoBoolean *is_version_defined_arg, MonoBoolean *is_token_defined_arg)
  5818. {
  5819. gboolean is_version_defined = FALSE;
  5820. gboolean is_token_defined = FALSE;
  5821. gboolean result = FALSE;
  5822. result = mono_assembly_name_parse_full (name, aname, TRUE, &is_version_defined, &is_token_defined);
  5823. *is_version_defined_arg = (MonoBoolean)is_version_defined;
  5824. *is_token_defined_arg = (MonoBoolean)is_token_defined;
  5825. return result;
  5826. }
  5827. MonoReflectionTypeHandle
  5828. ves_icall_System_Reflection_RuntimeModule_GetGlobalType (MonoImage *image, MonoError *error)
  5829. {
  5830. MonoDomain *domain = mono_domain_get ();
  5831. MonoClass *klass;
  5832. g_assert (image);
  5833. MonoReflectionTypeHandle ret = MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  5834. if (image_is_dynamic (image) && ((MonoDynamicImage*)image)->initial_image)
  5835. /* These images do not have a global type */
  5836. goto leave;
  5837. klass = mono_class_get_checked (image, 1 | MONO_TOKEN_TYPE_DEF, error);
  5838. goto_if_nok (error, leave);
  5839. ret = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
  5840. leave:
  5841. return ret;
  5842. }
  5843. void
  5844. ves_icall_System_Reflection_RuntimeModule_GetGuidInternal (MonoImage *image, MonoArrayHandle guid_h, MonoError *error)
  5845. {
  5846. g_assert (mono_array_handle_length (guid_h) == 16);
  5847. if (!image->metadata_only) {
  5848. g_assert (image->heap_guid.data);
  5849. g_assert (image->heap_guid.size >= 16);
  5850. MONO_ENTER_NO_SAFEPOINTS;
  5851. guint8 *data = (guint8*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_h), 1, 0);
  5852. memcpy (data, (guint8*)image->heap_guid.data, 16);
  5853. MONO_EXIT_NO_SAFEPOINTS;
  5854. } else {
  5855. MONO_ENTER_NO_SAFEPOINTS;
  5856. guint8 *data = (guint8*) mono_array_addr_with_size_internal (MONO_HANDLE_RAW (guid_h), 1, 0);
  5857. memset (data, 0, 16);
  5858. MONO_EXIT_NO_SAFEPOINTS;
  5859. }
  5860. }
  5861. #ifndef HOST_WIN32
  5862. static inline gpointer
  5863. mono_icall_module_get_hinstance (MonoImage *image)
  5864. {
  5865. return (gpointer) (-1);
  5866. }
  5867. #endif /* HOST_WIN32 */
  5868. #ifndef ENABLE_NETCORE
  5869. gpointer
  5870. ves_icall_System_Reflection_RuntimeModule_GetHINSTANCE (MonoImage *image, MonoError *error)
  5871. {
  5872. return mono_icall_module_get_hinstance (image);
  5873. }
  5874. #endif
  5875. void
  5876. ves_icall_System_Reflection_RuntimeModule_GetPEKind (MonoImage *image, gint32 *pe_kind, gint32 *machine, MonoError *error)
  5877. {
  5878. if (image_is_dynamic (image)) {
  5879. MonoDynamicImage *dyn = (MonoDynamicImage*)image;
  5880. *pe_kind = dyn->pe_kind;
  5881. *machine = dyn->machine;
  5882. }
  5883. else {
  5884. *pe_kind = (image->image_info->cli_cli_header.ch_flags & 0x3);
  5885. *machine = image->image_info->cli_header.coff.coff_machine;
  5886. }
  5887. }
  5888. gint32
  5889. ves_icall_System_Reflection_RuntimeModule_GetMDStreamVersion (MonoImage *image, MonoError *error)
  5890. {
  5891. return (image->md_version_major << 16) | (image->md_version_minor);
  5892. }
  5893. MonoArrayHandle
  5894. ves_icall_System_Reflection_RuntimeModule_InternalGetTypes (MonoImage *image, MonoError *error)
  5895. {
  5896. MonoDomain *domain = mono_domain_get ();
  5897. if (!image) {
  5898. MonoArrayHandle arr = mono_array_new_handle (domain, mono_defaults.runtimetype_class, 0, error);
  5899. return arr;
  5900. } else {
  5901. MonoArrayHandle exceptions = MONO_HANDLE_NEW (MonoArray, NULL);
  5902. MonoArrayHandle res = mono_module_get_types (domain, image, exceptions, FALSE, error);
  5903. return_val_if_nok (error, MONO_HANDLE_CAST(MonoArray, NULL_HANDLE));
  5904. int n = mono_array_handle_length (exceptions);
  5905. MonoExceptionHandle ex = MONO_HANDLE_NEW (MonoException, NULL);
  5906. for (int i = 0; i < n; ++i) {
  5907. MONO_HANDLE_ARRAY_GETREF(ex, exceptions, i);
  5908. if (!MONO_HANDLE_IS_NULL (ex)) {
  5909. mono_error_set_exception_handle (error, ex);
  5910. return MONO_HANDLE_CAST(MonoArray, NULL_HANDLE);
  5911. }
  5912. }
  5913. return res;
  5914. }
  5915. }
  5916. static gboolean
  5917. mono_memberref_is_method (MonoImage *image, guint32 token)
  5918. {
  5919. if (!image_is_dynamic (image)) {
  5920. guint32 cols [MONO_MEMBERREF_SIZE];
  5921. const char *sig;
  5922. const MonoTableInfo *table = &image->tables [MONO_TABLE_MEMBERREF];
  5923. int idx = mono_metadata_token_index (token) - 1;
  5924. if (idx < 0 || table->rows <= idx) {
  5925. return FALSE;
  5926. }
  5927. mono_metadata_decode_row (table, idx, cols, MONO_MEMBERREF_SIZE);
  5928. sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
  5929. mono_metadata_decode_blob_size (sig, &sig);
  5930. return (*sig != 0x6);
  5931. } else {
  5932. ERROR_DECL (error);
  5933. MonoClass *handle_class;
  5934. if (!mono_lookup_dynamic_token_class (image, token, FALSE, &handle_class, NULL, error)) {
  5935. mono_error_cleanup (error); /* just probing, ignore error */
  5936. return FALSE;
  5937. }
  5938. return mono_defaults.methodhandle_class == handle_class;
  5939. }
  5940. }
  5941. static MonoGenericInst *
  5942. get_generic_inst_from_array_handle (MonoArrayHandle type_args)
  5943. {
  5944. int type_argc = mono_array_handle_length (type_args);
  5945. int size = MONO_SIZEOF_GENERIC_INST + type_argc * sizeof (MonoType *);
  5946. MonoGenericInst *ginst = (MonoGenericInst *)g_alloca (size);
  5947. memset (ginst, 0, MONO_SIZEOF_GENERIC_INST);
  5948. ginst->type_argc = type_argc;
  5949. for (int i = 0; i < type_argc; i++) {
  5950. MONO_HANDLE_ARRAY_GETVAL (ginst->type_argv[i], type_args, MonoType*, i);
  5951. }
  5952. ginst->is_open = FALSE;
  5953. for (int i = 0; i < type_argc; i++) {
  5954. if (mono_class_is_open_constructed_type (ginst->type_argv[i])) {
  5955. ginst->is_open = TRUE;
  5956. break;
  5957. }
  5958. }
  5959. return mono_metadata_get_canonical_generic_inst (ginst);
  5960. }
  5961. static void
  5962. init_generic_context_from_args_handles (MonoGenericContext *context, MonoArrayHandle type_args, MonoArrayHandle method_args)
  5963. {
  5964. if (!MONO_HANDLE_IS_NULL (type_args)) {
  5965. context->class_inst = get_generic_inst_from_array_handle (type_args);
  5966. } else {
  5967. context->class_inst = NULL;
  5968. }
  5969. if (!MONO_HANDLE_IS_NULL (method_args)) {
  5970. context->method_inst = get_generic_inst_from_array_handle (method_args);
  5971. } else {
  5972. context->method_inst = NULL;
  5973. }
  5974. }
  5975. static MonoType*
  5976. module_resolve_type_token (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
  5977. {
  5978. HANDLE_FUNCTION_ENTER ();
  5979. error_init (error);
  5980. MonoType *result = NULL;
  5981. MonoClass *klass;
  5982. int table = mono_metadata_token_table (token);
  5983. int index = mono_metadata_token_index (token);
  5984. MonoGenericContext context;
  5985. *resolve_error = ResolveTokenError_Other;
  5986. /* Validate token */
  5987. if ((table != MONO_TABLE_TYPEDEF) && (table != MONO_TABLE_TYPEREF) &&
  5988. (table != MONO_TABLE_TYPESPEC)) {
  5989. *resolve_error = ResolveTokenError_BadTable;
  5990. goto leave;
  5991. }
  5992. if (image_is_dynamic (image)) {
  5993. if ((table == MONO_TABLE_TYPEDEF) || (table == MONO_TABLE_TYPEREF)) {
  5994. ERROR_DECL (inner_error);
  5995. klass = (MonoClass *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL, inner_error);
  5996. mono_error_cleanup (inner_error);
  5997. result = klass ? m_class_get_byval_arg (klass) : NULL;
  5998. goto leave;
  5999. }
  6000. init_generic_context_from_args_handles (&context, type_args, method_args);
  6001. ERROR_DECL (inner_error);
  6002. klass = (MonoClass *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, &context, inner_error);
  6003. mono_error_cleanup (inner_error);
  6004. result = klass ? m_class_get_byval_arg (klass) : NULL;
  6005. goto leave;
  6006. }
  6007. if ((index <= 0) || (index > image->tables [table].rows)) {
  6008. *resolve_error = ResolveTokenError_OutOfRange;
  6009. goto leave;
  6010. }
  6011. init_generic_context_from_args_handles (&context, type_args, method_args);
  6012. klass = mono_class_get_checked (image, token, error);
  6013. if (klass)
  6014. klass = mono_class_inflate_generic_class_checked (klass, &context, error);
  6015. goto_if_nok (error, leave);
  6016. if (klass)
  6017. result = m_class_get_byval_arg (klass);
  6018. leave:
  6019. HANDLE_FUNCTION_RETURN_VAL (result);
  6020. }
  6021. MonoType*
  6022. ves_icall_System_Reflection_RuntimeModule_ResolveTypeToken (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
  6023. {
  6024. return module_resolve_type_token (image, token, type_args, method_args, resolve_error, error);
  6025. }
  6026. static MonoMethod*
  6027. module_resolve_method_token (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
  6028. {
  6029. HANDLE_FUNCTION_ENTER ();
  6030. error_init (error);
  6031. MonoMethod *method = NULL;
  6032. int table = mono_metadata_token_table (token);
  6033. int index = mono_metadata_token_index (token);
  6034. MonoGenericContext context;
  6035. *resolve_error = ResolveTokenError_Other;
  6036. /* Validate token */
  6037. if ((table != MONO_TABLE_METHOD) && (table != MONO_TABLE_METHODSPEC) &&
  6038. (table != MONO_TABLE_MEMBERREF)) {
  6039. *resolve_error = ResolveTokenError_BadTable;
  6040. goto leave;
  6041. }
  6042. if (image_is_dynamic (image)) {
  6043. if (table == MONO_TABLE_METHOD) {
  6044. ERROR_DECL (inner_error);
  6045. method = (MonoMethod *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL, inner_error);
  6046. mono_error_cleanup (inner_error);
  6047. goto leave;
  6048. }
  6049. if ((table == MONO_TABLE_MEMBERREF) && !(mono_memberref_is_method (image, token))) {
  6050. *resolve_error = ResolveTokenError_BadTable;
  6051. goto leave;
  6052. }
  6053. init_generic_context_from_args_handles (&context, type_args, method_args);
  6054. ERROR_DECL (inner_error);
  6055. method = (MonoMethod *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, &context, inner_error);
  6056. mono_error_cleanup (inner_error);
  6057. goto leave;
  6058. }
  6059. if ((index <= 0) || (index > image->tables [table].rows)) {
  6060. *resolve_error = ResolveTokenError_OutOfRange;
  6061. goto leave;
  6062. }
  6063. if ((table == MONO_TABLE_MEMBERREF) && (!mono_memberref_is_method (image, token))) {
  6064. *resolve_error = ResolveTokenError_BadTable;
  6065. goto leave;
  6066. }
  6067. init_generic_context_from_args_handles (&context, type_args, method_args);
  6068. method = mono_get_method_checked (image, token, NULL, &context, error);
  6069. leave:
  6070. HANDLE_FUNCTION_RETURN_VAL (method);
  6071. }
  6072. MonoMethod*
  6073. ves_icall_System_Reflection_RuntimeModule_ResolveMethodToken (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
  6074. {
  6075. return module_resolve_method_token (image, token, type_args, method_args, resolve_error, error);
  6076. }
  6077. MonoStringHandle
  6078. ves_icall_System_Reflection_RuntimeModule_ResolveStringToken (MonoImage *image, guint32 token, MonoResolveTokenError *resolve_error, MonoError *error)
  6079. {
  6080. int index = mono_metadata_token_index (token);
  6081. *resolve_error = ResolveTokenError_Other;
  6082. /* Validate token */
  6083. if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
  6084. *resolve_error = ResolveTokenError_BadTable;
  6085. return NULL_HANDLE_STRING;
  6086. }
  6087. if (image_is_dynamic (image)) {
  6088. ERROR_DECL (ignore_inner_error);
  6089. // FIXME ignoring error
  6090. // FIXME Push MONO_HANDLE_NEW to lower layers.
  6091. MonoStringHandle result = MONO_HANDLE_NEW (MonoString, (MonoString*)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL, ignore_inner_error));
  6092. mono_error_cleanup (ignore_inner_error);
  6093. return result;
  6094. }
  6095. if ((index <= 0) || (index >= image->heap_us.size)) {
  6096. *resolve_error = ResolveTokenError_OutOfRange;
  6097. return NULL_HANDLE_STRING;
  6098. }
  6099. /* FIXME: What to do if the index points into the middle of a string ? */
  6100. return mono_ldstr_handle (mono_domain_get (), image, index, error);
  6101. }
  6102. static MonoClassField*
  6103. module_resolve_field_token (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
  6104. {
  6105. HANDLE_FUNCTION_ENTER ();
  6106. MonoClass *klass;
  6107. int table = mono_metadata_token_table (token);
  6108. int index = mono_metadata_token_index (token);
  6109. MonoGenericContext context;
  6110. MonoClassField *field = NULL;
  6111. error_init (error);
  6112. *resolve_error = ResolveTokenError_Other;
  6113. /* Validate token */
  6114. if ((table != MONO_TABLE_FIELD) && (table != MONO_TABLE_MEMBERREF)) {
  6115. *resolve_error = ResolveTokenError_BadTable;
  6116. goto leave;
  6117. }
  6118. if (image_is_dynamic (image)) {
  6119. if (table == MONO_TABLE_FIELD) {
  6120. ERROR_DECL (inner_error);
  6121. field = (MonoClassField *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL, inner_error);
  6122. mono_error_cleanup (inner_error);
  6123. goto leave;
  6124. }
  6125. if (mono_memberref_is_method (image, token)) {
  6126. *resolve_error = ResolveTokenError_BadTable;
  6127. goto leave;
  6128. }
  6129. init_generic_context_from_args_handles (&context, type_args, method_args);
  6130. ERROR_DECL (inner_error);
  6131. field = (MonoClassField *)mono_lookup_dynamic_token_class (image, token, FALSE, NULL, &context, inner_error);
  6132. mono_error_cleanup (inner_error);
  6133. goto leave;
  6134. }
  6135. if ((index <= 0) || (index > image->tables [table].rows)) {
  6136. *resolve_error = ResolveTokenError_OutOfRange;
  6137. goto leave;
  6138. }
  6139. if ((table == MONO_TABLE_MEMBERREF) && (mono_memberref_is_method (image, token))) {
  6140. *resolve_error = ResolveTokenError_BadTable;
  6141. goto leave;
  6142. }
  6143. init_generic_context_from_args_handles (&context, type_args, method_args);
  6144. field = mono_field_from_token_checked (image, token, &klass, &context, error);
  6145. leave:
  6146. HANDLE_FUNCTION_RETURN_VAL (field);
  6147. }
  6148. MonoClassField*
  6149. ves_icall_System_Reflection_RuntimeModule_ResolveFieldToken (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
  6150. {
  6151. return module_resolve_field_token (image, token, type_args, method_args, resolve_error, error);
  6152. }
  6153. MonoObjectHandle
  6154. ves_icall_System_Reflection_RuntimeModule_ResolveMemberToken (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *error, MonoError *merror)
  6155. {
  6156. int table = mono_metadata_token_table (token);
  6157. *error = ResolveTokenError_Other;
  6158. switch (table) {
  6159. case MONO_TABLE_TYPEDEF:
  6160. case MONO_TABLE_TYPEREF:
  6161. case MONO_TABLE_TYPESPEC: {
  6162. MonoType *t = module_resolve_type_token (image, token, type_args, method_args, error, merror);
  6163. if (t) {
  6164. return MONO_HANDLE_CAST (MonoObject, mono_type_get_object_handle (mono_domain_get (), t, merror));
  6165. }
  6166. else
  6167. return NULL_HANDLE;
  6168. }
  6169. case MONO_TABLE_METHOD:
  6170. case MONO_TABLE_METHODSPEC: {
  6171. MonoMethod *m = module_resolve_method_token (image, token, type_args, method_args, error, merror);
  6172. if (m) {
  6173. return MONO_HANDLE_CAST (MonoObject, mono_method_get_object_handle (mono_domain_get (), m, m->klass, merror));
  6174. } else
  6175. return NULL_HANDLE;
  6176. }
  6177. case MONO_TABLE_FIELD: {
  6178. MonoClassField *f = module_resolve_field_token (image, token, type_args, method_args, error, merror);
  6179. if (f) {
  6180. return MONO_HANDLE_CAST (MonoObject, mono_field_get_object_handle (mono_domain_get (), f->parent, f, merror));
  6181. }
  6182. else
  6183. return NULL_HANDLE;
  6184. }
  6185. case MONO_TABLE_MEMBERREF:
  6186. if (mono_memberref_is_method (image, token)) {
  6187. MonoMethod *m = module_resolve_method_token (image, token, type_args, method_args, error, merror);
  6188. if (m) {
  6189. return MONO_HANDLE_CAST (MonoObject, mono_method_get_object_handle (mono_domain_get (), m, m->klass, merror));
  6190. } else
  6191. return NULL_HANDLE;
  6192. }
  6193. else {
  6194. MonoClassField *f = module_resolve_field_token (image, token, type_args, method_args, error, merror);
  6195. if (f) {
  6196. return MONO_HANDLE_CAST (MonoObject, mono_field_get_object_handle (mono_domain_get (), f->parent, f, merror));
  6197. }
  6198. else
  6199. return NULL_HANDLE;
  6200. }
  6201. break;
  6202. default:
  6203. *error = ResolveTokenError_BadTable;
  6204. }
  6205. return NULL_HANDLE;
  6206. }
  6207. MonoArrayHandle
  6208. ves_icall_System_Reflection_RuntimeModule_ResolveSignature (MonoImage *image, guint32 token, MonoResolveTokenError *resolve_error, MonoError *error)
  6209. {
  6210. int table = mono_metadata_token_table (token);
  6211. int idx = mono_metadata_token_index (token);
  6212. MonoTableInfo *tables = image->tables;
  6213. guint32 sig, len;
  6214. const char *ptr;
  6215. *resolve_error = ResolveTokenError_OutOfRange;
  6216. /* FIXME: Support other tables ? */
  6217. if (table != MONO_TABLE_STANDALONESIG)
  6218. return NULL_HANDLE_ARRAY;
  6219. if (image_is_dynamic (image))
  6220. return NULL_HANDLE_ARRAY;
  6221. if ((idx == 0) || (idx > tables [MONO_TABLE_STANDALONESIG].rows))
  6222. return NULL_HANDLE_ARRAY;
  6223. sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
  6224. ptr = mono_metadata_blob_heap (image, sig);
  6225. len = mono_metadata_decode_blob_size (ptr, &ptr);
  6226. MonoArrayHandle res = mono_array_new_handle (mono_domain_get (), mono_defaults.byte_class, len, error);
  6227. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  6228. // FIXME MONO_ENTER_NO_SAFEPOINTS instead of pin/gchandle.
  6229. MonoGCHandle h;
  6230. gpointer array_base = MONO_ARRAY_HANDLE_PIN (res, guint8, 0, &h);
  6231. memcpy (array_base, ptr, len);
  6232. mono_gchandle_free_internal (h);
  6233. return res;
  6234. }
  6235. static void
  6236. check_for_invalid_array_type (MonoClass *klass, MonoError *error)
  6237. {
  6238. char *name;
  6239. error_init (error);
  6240. if (m_class_get_byval_arg (klass)->type != MONO_TYPE_TYPEDBYREF)
  6241. return;
  6242. name = mono_type_get_full_name (klass);
  6243. mono_error_set_type_load_name (error, name, g_strdup (""), "");
  6244. }
  6245. static void
  6246. check_for_invalid_byref_or_pointer_type (MonoClass *klass, MonoError *error)
  6247. {
  6248. #ifdef ENABLE_NETCORE
  6249. return;
  6250. #else
  6251. check_for_invalid_array_type (klass, error);
  6252. #endif
  6253. }
  6254. MonoReflectionTypeHandle
  6255. ves_icall_RuntimeType_make_array_type (MonoReflectionTypeHandle ref_type, int rank, MonoError *error)
  6256. {
  6257. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  6258. MonoClass *klass = mono_class_from_mono_type_internal (type);
  6259. check_for_invalid_array_type (klass, error);
  6260. return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
  6261. MonoClass *aklass;
  6262. if (rank == 0) //single dimension array
  6263. aklass = mono_class_create_array (klass, 1);
  6264. else
  6265. aklass = mono_class_create_bounded_array (klass, rank, TRUE);
  6266. if (mono_class_has_failure (aklass)) {
  6267. mono_error_set_for_class_failure (error, aklass);
  6268. return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
  6269. }
  6270. MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
  6271. return mono_type_get_object_handle (domain, m_class_get_byval_arg (aklass), error);
  6272. }
  6273. MonoReflectionTypeHandle
  6274. ves_icall_RuntimeType_make_byref_type (MonoReflectionTypeHandle ref_type, MonoError *error)
  6275. {
  6276. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  6277. MonoClass *klass = mono_class_from_mono_type_internal (type);
  6278. mono_class_init_checked (klass, error);
  6279. return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
  6280. check_for_invalid_byref_or_pointer_type (klass, error);
  6281. return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
  6282. MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
  6283. return mono_type_get_object_handle (domain, m_class_get_this_arg (klass), error);
  6284. }
  6285. MonoReflectionTypeHandle
  6286. ves_icall_RuntimeType_MakePointerType (MonoReflectionTypeHandle ref_type, MonoError *error)
  6287. {
  6288. MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
  6289. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  6290. MonoClass *klass = mono_class_from_mono_type_internal (type);
  6291. mono_class_init_checked (klass, error);
  6292. return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
  6293. check_for_invalid_byref_or_pointer_type (klass, error);
  6294. return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE));
  6295. MonoClass *pklass = mono_class_create_ptr (type);
  6296. return mono_type_get_object_handle (domain, m_class_get_byval_arg (pklass), error);
  6297. }
  6298. MonoObjectHandle
  6299. ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionTypeHandle ref_type, MonoObjectHandle target,
  6300. MonoReflectionMethodHandle info, MonoBoolean throwOnBindFailure, MonoError *error)
  6301. {
  6302. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  6303. MonoClass *delegate_class = mono_class_from_mono_type_internal (type);
  6304. MonoMethod *method = MONO_HANDLE_GETVAL (info, method);
  6305. MonoMethodSignature *sig = mono_method_signature_internal (method);
  6306. mono_class_init_checked (delegate_class, error);
  6307. return_val_if_nok (error, NULL_HANDLE);
  6308. if (!(m_class_get_parent (delegate_class) == mono_defaults.multicastdelegate_class)) {
  6309. /* FIXME improve this exception message */
  6310. mono_error_set_execution_engine (error, "file %s: line %d (%s): assertion failed: (%s)", __FILE__, __LINE__,
  6311. __func__,
  6312. "delegate_class->parent == mono_defaults.multicastdelegate_class");
  6313. return NULL_HANDLE;
  6314. }
  6315. if (mono_security_core_clr_enabled ()) {
  6316. ERROR_DECL (security_error);
  6317. if (!mono_security_core_clr_ensure_delegate_creation (method, security_error)) {
  6318. if (throwOnBindFailure)
  6319. mono_error_move (error, security_error);
  6320. else
  6321. mono_error_cleanup (security_error);
  6322. return NULL_HANDLE;
  6323. }
  6324. }
  6325. if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
  6326. if (!method->is_inflated) {
  6327. mono_error_set_argument (error, "method", " Cannot bind to the target method because its signature differs from that of the delegate type");
  6328. return NULL_HANDLE;
  6329. }
  6330. }
  6331. MonoObjectHandle delegate = mono_object_new_handle (MONO_HANDLE_DOMAIN (ref_type), delegate_class, error);
  6332. return_val_if_nok (error, NULL_HANDLE);
  6333. if (!method_is_dynamic (method) && (!MONO_HANDLE_IS_NULL (target) && method->flags & METHOD_ATTRIBUTE_VIRTUAL && method->klass != mono_handle_class (target))) {
  6334. method = mono_object_handle_get_virtual_method (target, method, error);
  6335. return_val_if_nok (error, NULL_HANDLE);
  6336. }
  6337. mono_delegate_ctor (delegate, target, NULL, method, error);
  6338. return_val_if_nok (error, NULL_HANDLE);
  6339. return delegate;
  6340. }
  6341. MonoMulticastDelegateHandle
  6342. ves_icall_System_Delegate_AllocDelegateLike_internal (MonoDelegateHandle delegate, MonoError *error)
  6343. {
  6344. MonoClass *klass = mono_handle_class (delegate);
  6345. g_assert (mono_class_has_parent (klass, mono_defaults.multicastdelegate_class));
  6346. MonoMulticastDelegateHandle ret = MONO_HANDLE_CAST (MonoMulticastDelegate, mono_object_new_handle (MONO_HANDLE_DOMAIN (delegate), klass, error));
  6347. return_val_if_nok (error, MONO_HANDLE_CAST (MonoMulticastDelegate, NULL_HANDLE));
  6348. MONO_HANDLE_SETVAL (MONO_HANDLE_CAST (MonoDelegate, ret), invoke_impl, gpointer, mono_runtime_create_delegate_trampoline (klass));
  6349. return ret;
  6350. }
  6351. MonoReflectionMethodHandle
  6352. ves_icall_System_Delegate_GetVirtualMethod_internal (MonoDelegateHandle delegate, MonoError *error)
  6353. {
  6354. MonoObjectHandle delegate_target = MONO_HANDLE_NEW_GET (MonoObject, delegate, target);
  6355. MonoMethod *m = mono_object_handle_get_virtual_method (delegate_target, MONO_HANDLE_GETVAL (delegate, method), error);
  6356. return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
  6357. return mono_method_get_object_handle (mono_domain_get (), m, m->klass, error);
  6358. }
  6359. /* System.Buffer */
  6360. static gint32
  6361. mono_array_get_byte_length (MonoArrayHandle array)
  6362. {
  6363. int length;
  6364. MonoClass * const klass = mono_handle_class (array);
  6365. // This resembles mono_array_get_length, but adds the loop.
  6366. if (mono_handle_array_has_bounds (array)) {
  6367. length = 1;
  6368. const int klass_rank = m_class_get_rank (klass);
  6369. for (int i = 0; i < klass_rank; ++ i)
  6370. length *= MONO_HANDLE_GETVAL (array, bounds [i].length);
  6371. } else {
  6372. length = mono_array_handle_length (array);
  6373. }
  6374. switch (m_class_get_byval_arg (m_class_get_element_class (klass))->type) {
  6375. case MONO_TYPE_I1:
  6376. case MONO_TYPE_U1:
  6377. case MONO_TYPE_BOOLEAN:
  6378. return length;
  6379. case MONO_TYPE_I2:
  6380. case MONO_TYPE_U2:
  6381. case MONO_TYPE_CHAR:
  6382. return length << 1;
  6383. case MONO_TYPE_I4:
  6384. case MONO_TYPE_U4:
  6385. case MONO_TYPE_R4:
  6386. return length << 2;
  6387. case MONO_TYPE_I:
  6388. case MONO_TYPE_U:
  6389. return length * sizeof (gpointer);
  6390. case MONO_TYPE_I8:
  6391. case MONO_TYPE_U8:
  6392. case MONO_TYPE_R8:
  6393. return length << 3;
  6394. default:
  6395. return -1;
  6396. }
  6397. }
  6398. #ifndef ENABLE_NETCORE
  6399. gint32
  6400. ves_icall_System_Buffer_ByteLengthInternal (MonoArrayHandle array, MonoError* error)
  6401. {
  6402. return mono_array_get_byte_length (array);
  6403. }
  6404. void
  6405. ves_icall_System_Buffer_MemcpyInternal (gpointer dest, gconstpointer src, gint32 count)
  6406. {
  6407. memcpy (dest, src, count);
  6408. }
  6409. MonoBoolean
  6410. ves_icall_System_Buffer_BlockCopyInternal (MonoArrayHandle src, gint32 src_offset, MonoArrayHandle dest, gint32 dest_offset, gint32 count, MonoError* error)
  6411. {
  6412. if (count < 0) {
  6413. mono_error_set_argument (error, "count", "is negative");
  6414. return FALSE;
  6415. }
  6416. /* This is called directly from the class libraries without going through the managed wrapper */
  6417. MONO_CHECK_ARG_NULL_HANDLE (src, FALSE);
  6418. MONO_CHECK_ARG_NULL_HANDLE (dest, FALSE);
  6419. /* watch out for integer overflow */
  6420. if ((src_offset > mono_array_get_byte_length (src) - count) || (dest_offset > mono_array_get_byte_length (dest) - count))
  6421. return FALSE;
  6422. MONO_ENTER_NO_SAFEPOINTS;
  6423. guint8 const * const src_buf = (guint8*)MONO_HANDLE_RAW (src)->vector + src_offset;
  6424. guint8* const dest_buf = (guint8*)MONO_HANDLE_RAW (dest)->vector + dest_offset;
  6425. #if !HOST_WIN32
  6426. // Windows memcpy is memmove and checks for overlap anyway, so skip
  6427. // the check here that would not help.
  6428. if (MONO_HANDLE_RAW (src) != MONO_HANDLE_RAW (dest))
  6429. memcpy (dest_buf, src_buf, count);
  6430. else
  6431. #endif
  6432. memmove (dest_buf, src_buf, count); /* Source and dest are the same array */
  6433. MONO_EXIT_NO_SAFEPOINTS;
  6434. return TRUE;
  6435. }
  6436. #endif
  6437. #ifndef DISABLE_REMOTING
  6438. MonoObjectHandle
  6439. ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObjectHandle this_obj, MonoStringHandle class_name, MonoError *error)
  6440. {
  6441. MonoDomain *domain = MONO_HANDLE_DOMAIN (this_obj);
  6442. MonoRealProxyHandle rp = MONO_HANDLE_CAST (MonoRealProxy, this_obj);
  6443. MonoObjectHandle res = mono_object_new_handle (domain, mono_defaults.transparent_proxy_class, error);
  6444. return_val_if_nok (error, NULL_HANDLE);
  6445. MonoTransparentProxyHandle tp = MONO_HANDLE_CAST (MonoTransparentProxy, res);
  6446. MONO_HANDLE_SET (tp, rp, rp);
  6447. MonoReflectionTypeHandle reftype = MONO_HANDLE_NEW (MonoReflectionType, NULL);
  6448. MONO_HANDLE_GET (reftype, rp, class_to_proxy);
  6449. MonoType *type = MONO_HANDLE_GETVAL (reftype, type);
  6450. MonoClass *klass = mono_class_from_mono_type_internal (type);
  6451. // mono_remote_class_vtable cannot handle errors well, so force any loading error to occur early
  6452. mono_class_setup_vtable (klass);
  6453. if (mono_class_has_failure (klass)) {
  6454. mono_error_set_for_class_failure (error, klass);
  6455. return NULL_HANDLE;
  6456. }
  6457. MonoObjectHandle remoting_obj = mono_object_handle_isinst (this_obj, mono_defaults.iremotingtypeinfo_class, error);
  6458. return_val_if_nok (error, NULL_HANDLE);
  6459. MONO_HANDLE_SETVAL (tp, custom_type_info, MonoBoolean, !MONO_HANDLE_IS_NULL (remoting_obj));
  6460. MonoRemoteClass *remote_class = (MonoRemoteClass*)mono_remote_class (domain, class_name, klass, error);
  6461. return_val_if_nok (error, NULL_HANDLE);
  6462. MONO_HANDLE_SETVAL (tp, remote_class, MonoRemoteClass*, remote_class);
  6463. MONO_HANDLE_SETVAL (res, vtable, MonoVTable*, (MonoVTable*)mono_remote_class_vtable (domain, remote_class, rp, error));
  6464. return_val_if_nok (error, NULL_HANDLE);
  6465. return res;
  6466. }
  6467. MonoReflectionTypeHandle
  6468. ves_icall_Remoting_RealProxy_InternalGetProxyType (MonoTransparentProxyHandle tp, MonoError *error)
  6469. {
  6470. MonoRemoteClass *remote_class;
  6471. g_assert (mono_handle_class (tp) == mono_defaults.transparent_proxy_class);
  6472. remote_class = MONO_HANDLE_RAW (tp)->remote_class;
  6473. g_assert (remote_class != NULL && remote_class->proxy_class != NULL);
  6474. return mono_type_get_object_handle (mono_handle_domain (tp), m_class_get_byval_arg (remote_class->proxy_class), error);
  6475. }
  6476. #endif
  6477. /* System.Environment */
  6478. #ifndef ENABLE_NETCORE
  6479. MonoStringHandle
  6480. ves_icall_System_Environment_get_UserName (MonoError *error)
  6481. {
  6482. /* using glib is more portable */
  6483. const gchar *user_name = g_get_user_name ();
  6484. if (user_name != NULL)
  6485. return mono_string_new_handle (mono_domain_get (), user_name, error);
  6486. else
  6487. return NULL_HANDLE_STRING;
  6488. }
  6489. #ifndef HOST_WIN32
  6490. static MonoStringHandle
  6491. mono_icall_get_machine_name (MonoError *error)
  6492. {
  6493. #if !defined(DISABLE_SOCKETS)
  6494. MonoStringHandle result;
  6495. char *buf;
  6496. int n;
  6497. #if defined _SC_HOST_NAME_MAX
  6498. n = sysconf (_SC_HOST_NAME_MAX);
  6499. if (n == -1)
  6500. #endif
  6501. n = 512;
  6502. buf = (char*)g_malloc (n + 1);
  6503. #if defined(HAVE_GETHOSTNAME)
  6504. if (gethostname (buf, n) == 0){
  6505. buf [n] = 0;
  6506. int i;
  6507. // try truncating the string at the first dot
  6508. for (i = 0; i < n; i++) {
  6509. if (buf [i] == '.') {
  6510. buf [i] = 0;
  6511. break;
  6512. }
  6513. }
  6514. result = mono_string_new_handle (mono_domain_get (), buf, error);
  6515. } else
  6516. #endif
  6517. result = MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
  6518. g_free (buf);
  6519. return result;
  6520. #else
  6521. return mono_string_new_handle (mono_domain_get (), "mono", error);
  6522. #endif
  6523. }
  6524. #endif /* !HOST_WIN32 */
  6525. MonoStringHandle
  6526. ves_icall_System_Environment_get_MachineName (MonoError *error)
  6527. {
  6528. return mono_icall_get_machine_name (error);
  6529. }
  6530. #ifndef HOST_WIN32
  6531. static int
  6532. mono_icall_get_platform (void)
  6533. {
  6534. #if defined(__MACH__)
  6535. /* OSX */
  6536. //
  6537. // Notice that the value is hidden from user code, and only exposed
  6538. // to mscorlib. This is due to Mono's Unix/MacOS code predating the
  6539. // define and making assumptions based on Unix/128/4 values before there
  6540. // was a MacOS define. Lots of code would assume that not-Unix meant
  6541. // Windows, but in this case, it would be OSX.
  6542. //
  6543. return 6;
  6544. #else
  6545. /* Unix */
  6546. return 4;
  6547. #endif
  6548. }
  6549. #endif /* !HOST_WIN32 */
  6550. int
  6551. ves_icall_System_Environment_get_Platform (void)
  6552. {
  6553. return mono_icall_get_platform ();
  6554. }
  6555. #endif /* !ENABLE_NETCORE */
  6556. #ifndef HOST_WIN32
  6557. static MonoStringHandle
  6558. mono_icall_get_new_line (MonoError *error)
  6559. {
  6560. return mono_string_new_handle (mono_domain_get (), "\n", error);
  6561. }
  6562. #endif /* !HOST_WIN32 */
  6563. #ifndef ENABLE_NETCORE
  6564. MonoStringHandle
  6565. ves_icall_System_Environment_get_NewLine (MonoError *error)
  6566. {
  6567. return mono_icall_get_new_line (error);
  6568. }
  6569. #endif
  6570. #ifndef HOST_WIN32
  6571. static inline MonoBoolean
  6572. mono_icall_is_64bit_os (void)
  6573. {
  6574. #if SIZEOF_VOID_P == 8
  6575. return TRUE;
  6576. #else
  6577. #if defined(HAVE_SYS_UTSNAME_H)
  6578. struct utsname name;
  6579. if (uname (&name) >= 0) {
  6580. return strcmp (name.machine, "x86_64") == 0 || strncmp (name.machine, "aarch64", 7) == 0 || strncmp (name.machine, "ppc64", 5) == 0 || strncmp (name.machine, "riscv64", 7) == 0;
  6581. }
  6582. #endif
  6583. return FALSE;
  6584. #endif
  6585. }
  6586. #endif /* !HOST_WIN32 */
  6587. MonoBoolean
  6588. ves_icall_System_Environment_GetIs64BitOperatingSystem (void)
  6589. {
  6590. return mono_icall_is_64bit_os ();
  6591. }
  6592. MonoStringHandle
  6593. ves_icall_System_Environment_GetEnvironmentVariable_native (const gchar *utf8_name, MonoError *error)
  6594. {
  6595. gchar *value;
  6596. if (utf8_name == NULL)
  6597. return NULL_HANDLE_STRING;
  6598. value = g_getenv (utf8_name);
  6599. if (value == 0)
  6600. return NULL_HANDLE_STRING;
  6601. MonoStringHandle res = mono_string_new_handle (mono_domain_get (), value, error);
  6602. g_free (value);
  6603. return res;
  6604. }
  6605. /*
  6606. * There is no standard way to get at environ.
  6607. */
  6608. #ifndef _MSC_VER
  6609. #ifndef __MINGW32_VERSION
  6610. #if defined(__APPLE__)
  6611. #if defined (TARGET_OSX)
  6612. /* Apple defines this in crt_externs.h but doesn't provide that header for
  6613. * arm-apple-darwin9. We'll manually define the symbol on Apple as it does
  6614. * in fact exist on all implementations (so far)
  6615. */
  6616. G_BEGIN_DECLS
  6617. gchar ***_NSGetEnviron(void);
  6618. G_END_DECLS
  6619. #define environ (*_NSGetEnviron())
  6620. #else
  6621. static char *mono_environ[1] = { NULL };
  6622. #define environ mono_environ
  6623. #endif /* defined (TARGET_OSX) */
  6624. #else
  6625. G_BEGIN_DECLS
  6626. extern
  6627. char **environ;
  6628. G_END_DECLS
  6629. #endif
  6630. #endif
  6631. #endif
  6632. MonoArrayHandle
  6633. ves_icall_System_Environment_GetCommandLineArgs (MonoError *error)
  6634. {
  6635. MonoArrayHandle result = mono_runtime_get_main_args_handle (error);
  6636. return result;
  6637. }
  6638. #ifndef HOST_WIN32
  6639. static MonoArrayHandle
  6640. mono_icall_get_environment_variable_names (MonoError *error)
  6641. {
  6642. MonoArrayHandle names;
  6643. MonoDomain *domain;
  6644. MonoStringHandle str;
  6645. gchar **e, **parts;
  6646. int n;
  6647. n = 0;
  6648. for (e = environ; *e != 0; ++ e)
  6649. ++ n;
  6650. domain = mono_domain_get ();
  6651. names = mono_array_new_handle (domain, mono_defaults.string_class, n, error);
  6652. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  6653. str = MONO_HANDLE_NEW (MonoString, NULL);
  6654. n = 0;
  6655. for (e = environ; *e != 0; ++ e) {
  6656. parts = g_strsplit (*e, "=", 2);
  6657. if (*parts != 0) {
  6658. MonoString *s = mono_string_new_checked (domain, *parts, error);
  6659. MONO_HANDLE_ASSIGN_RAW (str, s);
  6660. if (!is_ok (error)) {
  6661. g_strfreev (parts);
  6662. return NULL_HANDLE_ARRAY;
  6663. }
  6664. mono_array_handle_setref (names, n, str);
  6665. }
  6666. g_strfreev (parts);
  6667. ++ n;
  6668. }
  6669. return names;
  6670. }
  6671. #endif /* !HOST_WIN32 */
  6672. MonoArrayHandle
  6673. ves_icall_System_Environment_GetEnvironmentVariableNames (MonoError *error)
  6674. {
  6675. return mono_icall_get_environment_variable_names (error);
  6676. }
  6677. #ifndef ENABLE_NETCORE
  6678. void
  6679. ves_icall_System_Environment_InternalSetEnvironmentVariable (const gunichar2 *name, gint32 name_length,
  6680. const gunichar2 *value, gint32 value_length, MonoError *error)
  6681. {
  6682. #ifdef HOST_WIN32
  6683. if (!value || !value_length || !value [0])
  6684. value = NULL;
  6685. SetEnvironmentVariableW (name, value);
  6686. #else
  6687. char *utf8_name = NULL;
  6688. char *utf8_value = NULL;
  6689. utf8_name = mono_utf16_to_utf8 (name, name_length, error); // FIXME: this should be ascii
  6690. goto_if_nok (error, exit);
  6691. if (!value || !value_length || !value [0]) {
  6692. g_unsetenv (utf8_name);
  6693. goto exit;
  6694. }
  6695. utf8_value = mono_utf16_to_utf8 (value, value_length, error);
  6696. goto_if_nok (error, exit);
  6697. g_setenv (utf8_name, utf8_value, TRUE);
  6698. exit:
  6699. g_free (utf8_name);
  6700. g_free (utf8_value);
  6701. #endif
  6702. }
  6703. #endif
  6704. void
  6705. ves_icall_System_Environment_Exit (int result)
  6706. {
  6707. mono_environment_exitcode_set (result);
  6708. if (!mono_runtime_try_shutdown ())
  6709. mono_thread_exit ();
  6710. #ifndef ENABLE_NETCORE
  6711. /* Suspend all managed threads since the runtime is going away */
  6712. mono_thread_suspend_all_other_threads ();
  6713. #endif
  6714. mono_runtime_quit_internal ();
  6715. /* we may need to do some cleanup here... */
  6716. exit (result);
  6717. }
  6718. void
  6719. ves_icall_System_Environment_FailFast (MonoStringHandle message, MonoExceptionHandle exception, MonoStringHandle errorSource, MonoError *error)
  6720. {
  6721. if (MONO_HANDLE_IS_NULL (message)) {
  6722. g_warning ("CLR: Managed code called FailFast without specifying a reason.");
  6723. } else {
  6724. char *msg = mono_string_handle_to_utf8 (message, error);
  6725. g_warning ("CLR: Managed code called FailFast, saying \"%s\"", msg);
  6726. #ifndef DISABLE_CRASH_REPORTING
  6727. char *old_msg = mono_crash_save_failfast_msg (msg);
  6728. g_free (old_msg);
  6729. #else
  6730. g_free (msg);
  6731. #endif
  6732. }
  6733. if (!MONO_HANDLE_IS_NULL (exception)) {
  6734. mono_print_unhandled_exception_internal ((MonoObject *) MONO_HANDLE_RAW (exception));
  6735. }
  6736. // NOTE: While this does trigger WER on Windows it doesn't quite provide all the
  6737. // information in the error dump that CoreCLR would. On Windows 7+ we should call
  6738. // RaiseFailFastException directly instead of relying on the C runtime doing it
  6739. // for us and pass it as much information as possible. On Windows 8+ we can also
  6740. // use the __fastfail intrinsic.
  6741. abort ();
  6742. }
  6743. #ifndef ENABLE_NETCORE
  6744. MonoStringHandle
  6745. ves_icall_System_Environment_GetGacPath (MonoError *error)
  6746. {
  6747. return mono_string_new_handle (mono_domain_get (), mono_assembly_getrootdir (), error);
  6748. }
  6749. #endif
  6750. #ifndef HOST_WIN32
  6751. static inline MonoStringHandle
  6752. mono_icall_get_windows_folder_path (int folder, MonoError *error)
  6753. {
  6754. error_init (error);
  6755. g_warning ("ves_icall_System_Environment_GetWindowsFolderPath should only be called on Windows!");
  6756. return mono_string_new_handle (mono_domain_get (), "", error);
  6757. }
  6758. #endif /* !HOST_WIN32 */
  6759. #ifndef ENABLE_NETCORE
  6760. MonoStringHandle
  6761. ves_icall_System_Environment_GetWindowsFolderPath (int folder, MonoError *error)
  6762. {
  6763. return mono_icall_get_windows_folder_path (folder, error);
  6764. }
  6765. #endif
  6766. static MonoArrayHandle
  6767. mono_icall_get_logical_drives (MonoError *error)
  6768. {
  6769. gunichar2 buf [256], *ptr, *dname;
  6770. gunichar2 *u16;
  6771. guint initial_size = 127, size = 128;
  6772. gint ndrives;
  6773. MonoArrayHandle result = NULL_HANDLE_ARRAY;
  6774. MonoStringHandle drivestr;
  6775. MonoDomain *domain = mono_domain_get ();
  6776. gint len;
  6777. buf [0] = '\0';
  6778. ptr = buf;
  6779. while (size > initial_size) {
  6780. size = (guint) mono_w32file_get_logical_drive (initial_size, ptr, error);
  6781. if (!is_ok (error))
  6782. goto leave;
  6783. if (size > initial_size) {
  6784. if (ptr != buf)
  6785. g_free (ptr);
  6786. ptr = (gunichar2 *)g_malloc0 ((size + 1) * sizeof (gunichar2));
  6787. initial_size = size;
  6788. size++;
  6789. }
  6790. }
  6791. /* Count strings */
  6792. dname = ptr;
  6793. ndrives = 0;
  6794. do {
  6795. while (*dname++);
  6796. ndrives++;
  6797. } while (*dname);
  6798. dname = ptr;
  6799. result = mono_array_new_handle (domain, mono_defaults.string_class, ndrives, error);
  6800. goto_if_nok (error, leave);
  6801. drivestr = MONO_HANDLE_NEW (MonoString, NULL);
  6802. ndrives = 0;
  6803. do {
  6804. len = 0;
  6805. u16 = dname;
  6806. while (*u16) {
  6807. u16++; len ++;
  6808. }
  6809. MonoString *s = mono_string_new_utf16_checked (domain, dname, len, error);
  6810. goto_if_nok (error, leave);
  6811. MONO_HANDLE_ASSIGN_RAW (drivestr, s);
  6812. mono_array_handle_setref (result, ndrives, drivestr);
  6813. ndrives ++;
  6814. while (*dname++);
  6815. } while (*dname);
  6816. leave:
  6817. if (ptr != buf)
  6818. g_free (ptr);
  6819. return result;
  6820. }
  6821. #ifndef ENABLE_NETCORE
  6822. MonoArrayHandle
  6823. ves_icall_System_Environment_GetLogicalDrivesInternal (MonoError *error)
  6824. {
  6825. return mono_icall_get_logical_drives (error);
  6826. }
  6827. guint32
  6828. ves_icall_System_IO_DriveInfo_GetDriveType (const gunichar2 *root_path_name, gint32 root_path_name_length, MonoError *error)
  6829. {
  6830. return mono_w32file_get_drive_type (root_path_name, root_path_name_length, error);
  6831. }
  6832. MonoStringHandle
  6833. ves_icall_System_IO_DriveInfo_GetDriveFormat (const gunichar2 *path, gint32 path_length, MonoError *error)
  6834. {
  6835. gunichar2 volume_name [MAX_PATH + 1];
  6836. if (mono_w32file_get_file_system_type (path, volume_name, MAX_PATH + 1) == FALSE)
  6837. return NULL_HANDLE_STRING;
  6838. return mono_string_new_utf16_handle (mono_domain_get (), volume_name, g_utf16_len (volume_name), error);
  6839. }
  6840. MonoStringHandle
  6841. ves_icall_System_Environment_InternalGetHome (MonoError *error)
  6842. {
  6843. const gchar *home_dir = g_get_home_dir ();
  6844. if (home_dir != NULL)
  6845. return mono_string_new_handle (mono_domain_get (), home_dir, error);
  6846. else
  6847. return NULL_HANDLE_STRING;
  6848. }
  6849. static const char * const encodings [] = {
  6850. (char *) 1,
  6851. "ascii", "us_ascii", "us", "ansi_x3.4_1968",
  6852. "ansi_x3.4_1986", "cp367", "csascii", "ibm367",
  6853. "iso_ir_6", "iso646_us", "iso_646.irv:1991",
  6854. (char *) 2,
  6855. "utf_7", "csunicode11utf7", "unicode_1_1_utf_7",
  6856. "unicode_2_0_utf_7", "x_unicode_1_1_utf_7",
  6857. "x_unicode_2_0_utf_7",
  6858. (char *) 3,
  6859. "utf_8", "unicode_1_1_utf_8", "unicode_2_0_utf_8",
  6860. "x_unicode_1_1_utf_8", "x_unicode_2_0_utf_8",
  6861. (char *) 4,
  6862. "utf_16", "UTF_16LE", "ucs_2", "unicode",
  6863. "iso_10646_ucs2",
  6864. (char *) 5,
  6865. "unicodefffe", "utf_16be",
  6866. (char *) 6,
  6867. "iso_8859_1",
  6868. (char *) 0
  6869. };
  6870. /*
  6871. * Returns the internal codepage, if the value of "int_code_page" is
  6872. * 1 at entry, and we can not compute a suitable code page number,
  6873. * returns the code page as a string
  6874. */
  6875. MonoStringHandle
  6876. ves_icall_System_Text_EncodingHelper_InternalCodePage (gint32 *int_code_page, MonoError *error)
  6877. {
  6878. error_init (error);
  6879. const char *cset;
  6880. const char *p;
  6881. char *c;
  6882. char *codepage = NULL;
  6883. int code;
  6884. int want_name = *int_code_page;
  6885. int i;
  6886. *int_code_page = -1;
  6887. g_get_charset (&cset);
  6888. c = codepage = g_strdup (cset);
  6889. for (c = codepage; *c; c++){
  6890. if (isascii (*c) && isalpha (*c))
  6891. *c = tolower (*c);
  6892. if (*c == '-')
  6893. *c = '_';
  6894. }
  6895. /* g_print ("charset: %s\n", cset); */
  6896. /* handle some common aliases */
  6897. p = encodings [0];
  6898. code = 0;
  6899. for (i = 0; p != 0; ){
  6900. if ((gsize) p < 7){
  6901. code = (gssize) p;
  6902. p = encodings [++i];
  6903. continue;
  6904. }
  6905. if (strcmp (p, codepage) == 0){
  6906. *int_code_page = code;
  6907. break;
  6908. }
  6909. p = encodings [++i];
  6910. }
  6911. if (strstr (codepage, "utf_8") != NULL)
  6912. *int_code_page |= 0x10000000;
  6913. g_free (codepage);
  6914. if (want_name && *int_code_page == -1)
  6915. return mono_string_new_handle (mono_domain_get (), cset, error);
  6916. return NULL_HANDLE_STRING;
  6917. }
  6918. #endif
  6919. MonoBoolean
  6920. ves_icall_System_Environment_get_HasShutdownStarted (void)
  6921. {
  6922. return mono_runtime_is_shutting_down () || mono_domain_is_unloading (mono_domain_get ());
  6923. }
  6924. #ifndef HOST_WIN32
  6925. #ifndef ENABLE_NETCORE
  6926. void
  6927. ves_icall_System_Environment_BroadcastSettingChange (MonoError *error)
  6928. {
  6929. }
  6930. #endif
  6931. #endif
  6932. gint32
  6933. ves_icall_System_Environment_get_TickCount (void)
  6934. {
  6935. /* this will overflow after ~24 days */
  6936. return (gint32) (mono_msec_boottime () & 0xffffffff);
  6937. }
  6938. #if ENABLE_NETCORE
  6939. gint64
  6940. ves_icall_System_Environment_get_TickCount64 (void)
  6941. {
  6942. return mono_msec_boottime ();
  6943. }
  6944. #endif
  6945. #ifndef ENABLE_NETCORE
  6946. gint32
  6947. ves_icall_System_Runtime_Versioning_VersioningHelper_GetRuntimeId (MonoError *error)
  6948. {
  6949. return 9;
  6950. }
  6951. #endif
  6952. #ifndef DISABLE_REMOTING
  6953. MonoBoolean
  6954. ves_icall_IsTransparentProxy (MonoObjectHandle proxy, MonoError *error)
  6955. {
  6956. if (MONO_HANDLE_IS_NULL (proxy))
  6957. return 0;
  6958. if (mono_class_is_transparent_proxy (mono_handle_class (proxy)))
  6959. return 1;
  6960. return 0;
  6961. }
  6962. MonoReflectionMethodHandle
  6963. ves_icall_Remoting_RemotingServices_GetVirtualMethod (
  6964. MonoReflectionTypeHandle rtype, MonoReflectionMethodHandle rmethod, MonoError *error)
  6965. {
  6966. MonoReflectionMethodHandle ret = MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
  6967. if (MONO_HANDLE_IS_NULL (rtype)) {
  6968. mono_error_set_argument_null (error, "type", "");
  6969. return ret;
  6970. }
  6971. if (MONO_HANDLE_IS_NULL (rmethod)) {
  6972. mono_error_set_argument_null (error, "method", "");
  6973. return ret;
  6974. }
  6975. MonoMethod *method = MONO_HANDLE_GETVAL (rmethod, method);
  6976. MonoType *type = MONO_HANDLE_GETVAL (rtype, type);
  6977. MonoClass *klass = mono_class_from_mono_type_internal (type);
  6978. mono_class_init_checked (klass, error);
  6979. return_val_if_nok (error, ret);
  6980. if (MONO_CLASS_IS_INTERFACE_INTERNAL (klass))
  6981. return ret;
  6982. if (method->flags & METHOD_ATTRIBUTE_STATIC)
  6983. return ret;
  6984. if ((method->flags & METHOD_ATTRIBUTE_FINAL) || !(method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
  6985. if (klass == method->klass || mono_class_is_subclass_of_internal (klass, method->klass, FALSE))
  6986. ret = rmethod;
  6987. return ret;
  6988. }
  6989. mono_class_setup_vtable (klass);
  6990. MonoMethod **vtable = m_class_get_vtable (klass);
  6991. MonoMethod *res = NULL;
  6992. if (mono_class_is_interface (method->klass)) {
  6993. gboolean variance_used = FALSE;
  6994. /*MS fails with variant interfaces but it's the right thing to do anyway.*/
  6995. int offs = mono_class_interface_offset_with_variance (klass, method->klass, &variance_used);
  6996. if (offs >= 0)
  6997. res = vtable [offs + method->slot];
  6998. } else {
  6999. if (!(klass == method->klass || mono_class_is_subclass_of_internal (klass, method->klass, FALSE)))
  7000. return ret;
  7001. if (method->slot != -1)
  7002. res = vtable [method->slot];
  7003. }
  7004. if (!res)
  7005. return ret;
  7006. ret = mono_method_get_object_handle (mono_domain_get (), res, NULL, error);
  7007. return ret;
  7008. }
  7009. void
  7010. ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionTypeHandle type, MonoBoolean enable, MonoError *error)
  7011. {
  7012. MonoClass *klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
  7013. MonoVTable *vtable = mono_class_vtable_checked (mono_domain_get (), klass, error);
  7014. return_if_nok (error);
  7015. mono_vtable_set_is_remote (vtable, enable);
  7016. }
  7017. #else /* DISABLE_REMOTING */
  7018. ICALL_EXPORT MonoBoolean
  7019. ves_icall_IsTransparentProxy (MonoObject *proxy)
  7020. {
  7021. return 0;
  7022. }
  7023. #ifndef ENABLE_NETCORE
  7024. void
  7025. ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionTypeHandle type, MonoBoolean enable, MonoError *error)
  7026. {
  7027. g_assert_not_reached ();
  7028. }
  7029. #endif
  7030. #endif
  7031. #ifndef ENABLE_NETCORE
  7032. MonoObjectHandle
  7033. ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance (MonoReflectionTypeHandle type, MonoError *error)
  7034. {
  7035. MonoDomain *domain = MONO_HANDLE_DOMAIN (type);
  7036. MonoClass *klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
  7037. mono_class_init_checked (klass, error);
  7038. return_val_if_nok (error, NULL_HANDLE);
  7039. if (MONO_CLASS_IS_INTERFACE_INTERNAL (klass) || mono_class_is_abstract (klass)) {
  7040. mono_error_set_argument (error, "type", "Type cannot be instantiated");
  7041. return NULL_HANDLE;
  7042. }
  7043. if (m_class_get_rank (klass) >= 1) {
  7044. g_assert (m_class_get_rank (klass) == 1);
  7045. return MONO_HANDLE_CAST (MonoObject, mono_array_new_handle (domain, m_class_get_element_class (klass), 0, error));
  7046. } else {
  7047. MonoVTable *vtable = mono_class_vtable_checked (domain, klass, error);
  7048. return_val_if_nok (error, NULL_HANDLE);
  7049. /* Bypass remoting object creation check */
  7050. return MONO_HANDLE_NEW (MonoObject, mono_object_new_alloc_specific_checked (vtable, error));
  7051. }
  7052. }
  7053. MonoStringHandle
  7054. ves_icall_System_IO_get_temp_path (MonoError *error)
  7055. {
  7056. return mono_string_new_handle (mono_domain_get (), g_get_tmp_dir (), error);
  7057. }
  7058. #if defined(ENABLE_MONODROID) || defined(ENABLE_MONOTOUCH) || defined(TARGET_WASM)
  7059. // FIXME? Names should start "mono"?
  7060. G_EXTERN_C gpointer CreateZStream (gint32 compress, MonoBoolean gzip, gpointer feeder, gpointer data);
  7061. G_EXTERN_C gint32 CloseZStream (gpointer stream);
  7062. G_EXTERN_C gint32 Flush (gpointer stream);
  7063. G_EXTERN_C gint32 ReadZStream (gpointer stream, gpointer buffer, gint32 length);
  7064. G_EXTERN_C gint32 WriteZStream (gpointer stream, gpointer buffer, gint32 length);
  7065. gpointer
  7066. ves_icall_System_IO_Compression_DeflateStreamNative_CreateZStream (gint32 compress, MonoBoolean gzip, gpointer feeder, gpointer data)
  7067. {
  7068. #ifdef MONO_CROSS_COMPILE
  7069. return NULL;
  7070. #else
  7071. return CreateZStream (compress, gzip, feeder, data);
  7072. #endif
  7073. }
  7074. gint32
  7075. ves_icall_System_IO_Compression_DeflateStreamNative_CloseZStream (gpointer stream)
  7076. {
  7077. #ifdef MONO_CROSS_COMPILE
  7078. return 0;
  7079. #else
  7080. return CloseZStream (stream);
  7081. #endif
  7082. }
  7083. gint32
  7084. ves_icall_System_IO_Compression_DeflateStreamNative_Flush (gpointer stream)
  7085. {
  7086. #ifdef MONO_CROSS_COMPILE
  7087. return 0;
  7088. #else
  7089. return Flush (stream);
  7090. #endif
  7091. }
  7092. gint32
  7093. ves_icall_System_IO_Compression_DeflateStreamNative_ReadZStream (gpointer stream, gpointer buffer, gint32 length)
  7094. {
  7095. #ifdef MONO_CROSS_COMPILE
  7096. return 0;
  7097. #else
  7098. return ReadZStream (stream, buffer, length);
  7099. #endif
  7100. }
  7101. gint32
  7102. ves_icall_System_IO_Compression_DeflateStreamNative_WriteZStream (gpointer stream, gpointer buffer, gint32 length)
  7103. {
  7104. #ifdef MONO_CROSS_COMPILE
  7105. return 0;
  7106. #else
  7107. return WriteZStream (stream, buffer, length);
  7108. #endif
  7109. }
  7110. #endif
  7111. #if defined(TARGET_WASM)
  7112. G_EXTERN_C void mono_timezone_get_local_name (MonoString **result);
  7113. void
  7114. ves_icall_System_TimeZoneInfo_mono_timezone_get_local_name (MonoString **result)
  7115. {
  7116. // MONO_CROSS_COMPILE returns undefined symbol "_mono_timezone_get_local_name"
  7117. // The icall offsets will be generated and linked at build time
  7118. // This is defined outside the runtime within the webassembly sdk
  7119. #ifndef MONO_CROSS_COMPILE
  7120. return mono_timezone_get_local_name (result);
  7121. #endif
  7122. }
  7123. #endif
  7124. #endif /* ENABLE_NETCORE */
  7125. #ifndef PLATFORM_NO_DRIVEINFO
  7126. MonoBoolean
  7127. ves_icall_System_IO_DriveInfo_GetDiskFreeSpace (const gunichar2 *path_name, gint32 path_name_length, guint64 *free_bytes_avail,
  7128. guint64 *total_number_of_bytes, guint64 *total_number_of_free_bytes,
  7129. gint32 *error)
  7130. {
  7131. g_assert (error);
  7132. g_assert (free_bytes_avail);
  7133. g_assert (total_number_of_bytes);
  7134. g_assert (total_number_of_free_bytes);
  7135. // FIXME check for embedded nuls here or managed
  7136. *error = ERROR_SUCCESS;
  7137. *free_bytes_avail = (guint64)-1;
  7138. *total_number_of_bytes = (guint64)-1;
  7139. *total_number_of_free_bytes = (guint64)-1;
  7140. gboolean result = mono_w32file_get_disk_free_space (path_name, free_bytes_avail, total_number_of_bytes, total_number_of_free_bytes);
  7141. if (!result)
  7142. *error = mono_w32error_get_last ();
  7143. return result;
  7144. }
  7145. #endif /* PLATFORM_NO_DRIVEINFO */
  7146. gpointer
  7147. ves_icall_RuntimeMethodHandle_GetFunctionPointer (MonoMethod *method, MonoError *error)
  7148. {
  7149. return mono_compile_method_checked (method, error);
  7150. }
  7151. #ifndef ENABLE_NETCORE
  7152. MonoStringHandle
  7153. ves_icall_System_Configuration_DefaultConfig_get_machine_config_path (MonoError *error)
  7154. {
  7155. gchar *path;
  7156. const char *mono_cfg_dir = mono_get_config_dir ();
  7157. if (!mono_cfg_dir)
  7158. return mono_string_new_handle (mono_domain_get (), "", error);
  7159. path = g_build_path (G_DIR_SEPARATOR_S, mono_cfg_dir, "mono", mono_get_runtime_info ()->framework_version, "machine.config", (const char*)NULL);
  7160. mono_icall_make_platform_path (path);
  7161. MonoStringHandle mcpath = mono_string_new_handle (mono_domain_get (), path, error);
  7162. g_free (path);
  7163. mono_error_assert_ok (error);
  7164. return mcpath;
  7165. }
  7166. MonoStringHandle
  7167. ves_icall_System_Configuration_InternalConfigurationHost_get_bundled_app_config (MonoError *error)
  7168. {
  7169. const gchar *app_config;
  7170. MonoDomain *domain;
  7171. gchar *config_file_name, *config_file_path;
  7172. gsize len, config_file_path_length, config_ext_length;
  7173. gchar *module;
  7174. domain = mono_domain_get ();
  7175. MonoStringHandle file = MONO_HANDLE_NEW (MonoString, domain->setup->configuration_file);
  7176. if (MONO_HANDLE_IS_NULL (file) || MONO_HANDLE_GETVAL (file, length) == 0)
  7177. return MONO_HANDLE_CAST (MonoString, mono_new_null ());
  7178. // Retrieve config file and remove the extension
  7179. config_file_name = mono_string_handle_to_utf8 (file, error);
  7180. return_val_if_nok (error, MONO_HANDLE_CAST (MonoString, NULL_HANDLE));
  7181. config_file_path = mono_portability_find_file (config_file_name, TRUE);
  7182. if (!config_file_path)
  7183. config_file_path = config_file_name;
  7184. config_file_path_length = strlen (config_file_path);
  7185. config_ext_length = strlen (".config");
  7186. if (config_file_path_length <= config_ext_length) {
  7187. if (config_file_name != config_file_path)
  7188. g_free (config_file_name);
  7189. return MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
  7190. }
  7191. len = config_file_path_length - config_ext_length;
  7192. module = (gchar *)g_malloc0 (len + 1);
  7193. memcpy (module, config_file_path, len);
  7194. // Get the config file from the module name
  7195. app_config = mono_config_string_for_assembly_file (module);
  7196. // Clean-up
  7197. g_free (module);
  7198. if (config_file_name != config_file_path)
  7199. g_free (config_file_name);
  7200. g_free (config_file_path);
  7201. if (!app_config)
  7202. return MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
  7203. return mono_string_new_handle (mono_domain_get (), app_config, error);
  7204. }
  7205. static MonoStringHandle
  7206. get_bundled_machine_config (MonoError *error)
  7207. {
  7208. const gchar *machine_config;
  7209. machine_config = mono_get_machine_config ();
  7210. if (!machine_config)
  7211. return NULL_HANDLE_STRING;
  7212. return mono_string_new_handle (mono_domain_get (), machine_config, error);
  7213. }
  7214. MonoStringHandle
  7215. ves_icall_System_Environment_get_bundled_machine_config (MonoError *error)
  7216. {
  7217. return get_bundled_machine_config (error);
  7218. }
  7219. MonoStringHandle
  7220. ves_icall_System_Configuration_DefaultConfig_get_bundled_machine_config (MonoError *error)
  7221. {
  7222. return get_bundled_machine_config (error);
  7223. }
  7224. MonoStringHandle
  7225. ves_icall_System_Configuration_InternalConfigurationHost_get_bundled_machine_config (MonoError *error)
  7226. {
  7227. return get_bundled_machine_config (error);
  7228. }
  7229. MonoStringHandle
  7230. ves_icall_System_Web_Util_ICalls_get_machine_install_dir (MonoError *error)
  7231. {
  7232. const char *mono_cfg_dir = mono_get_config_dir ();
  7233. if (!mono_cfg_dir)
  7234. return mono_string_new_handle (mono_domain_get (), "", error);
  7235. char *path = g_path_get_dirname (mono_cfg_dir);
  7236. mono_icall_make_platform_path (path);
  7237. MonoStringHandle ipath = mono_string_new_handle (mono_domain_get (), path, error);
  7238. g_free (path);
  7239. return ipath;
  7240. }
  7241. MonoBoolean
  7242. ves_icall_get_resources_ptr (MonoReflectionAssemblyHandle assembly, gpointer *result, gint32 *size, MonoError *error)
  7243. {
  7244. MonoPEResourceDataEntry *entry;
  7245. MonoImage *image;
  7246. if (MONO_HANDLE_IS_NULL (assembly) || !result || !size)
  7247. return FALSE;
  7248. *result = NULL;
  7249. *size = 0;
  7250. MonoAssembly *assm = MONO_HANDLE_GETVAL (assembly, assembly);
  7251. image = assm->image;
  7252. entry = (MonoPEResourceDataEntry *)mono_image_lookup_resource (image, MONO_PE_RESOURCE_ID_ASPNET_STRING, 0, NULL);
  7253. if (!entry)
  7254. return FALSE;
  7255. *result = mono_image_rva_map (image, entry->rde_data_offset);
  7256. if (!(*result)) {
  7257. g_free (entry);
  7258. return FALSE;
  7259. }
  7260. *size = entry->rde_size;
  7261. g_free (entry);
  7262. return TRUE;
  7263. }
  7264. #endif /* ENABLE_NETCORE */
  7265. MonoBoolean
  7266. ves_icall_System_Diagnostics_Debugger_IsAttached_internal (void)
  7267. {
  7268. return mono_is_debugger_attached ();
  7269. }
  7270. MonoBoolean
  7271. ves_icall_System_Diagnostics_Debugger_IsLogging (void)
  7272. {
  7273. return mono_get_runtime_callbacks ()->debug_log_is_enabled
  7274. && mono_get_runtime_callbacks ()->debug_log_is_enabled ();
  7275. }
  7276. void
  7277. ves_icall_System_Diagnostics_Debugger_Log (int level, MonoString *volatile* category, MonoString *volatile* message)
  7278. {
  7279. if (mono_get_runtime_callbacks ()->debug_log)
  7280. mono_get_runtime_callbacks ()->debug_log (level, *category, *message);
  7281. }
  7282. #ifndef HOST_WIN32
  7283. static inline void
  7284. mono_icall_write_windows_debug_string (const gunichar2 *message)
  7285. {
  7286. g_warning ("WriteWindowsDebugString called and HOST_WIN32 not defined!\n");
  7287. }
  7288. #endif /* !HOST_WIN32 */
  7289. #ifndef ENABLE_NETCORE
  7290. void
  7291. ves_icall_System_Diagnostics_DefaultTraceListener_WriteWindowsDebugString (const gunichar2 *message, MonoError *error)
  7292. {
  7293. mono_icall_write_windows_debug_string (message);
  7294. }
  7295. #endif
  7296. /* Only used for value types */
  7297. MonoObjectHandle
  7298. ves_icall_System_Activator_CreateInstanceInternal (MonoReflectionTypeHandle ref_type, MonoError *error)
  7299. {
  7300. MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
  7301. MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
  7302. MonoClass *klass = mono_class_from_mono_type_internal (type);
  7303. (void)klass;
  7304. mono_class_init_checked (klass, error);
  7305. return_val_if_nok (error, NULL_HANDLE);
  7306. if (mono_class_is_nullable (klass))
  7307. /* No arguments -> null */
  7308. return NULL_HANDLE;
  7309. return mono_object_new_handle (domain, klass, error);
  7310. }
  7311. MonoReflectionMethodHandle
  7312. ves_icall_RuntimeMethodInfo_get_base_method (MonoReflectionMethodHandle m, MonoBoolean definition, MonoError *error)
  7313. {
  7314. MonoMethod *method = MONO_HANDLE_GETVAL (m, method);
  7315. MonoMethod *base = mono_method_get_base_method (method, definition, error);
  7316. return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
  7317. if (base == method) {
  7318. /* we want to short-circuit and return 'm' here. But we should
  7319. return the same method object that
  7320. mono_method_get_object_handle, below would return. Since
  7321. that call takes NULL for the reftype argument, it will take
  7322. base->klass as the reflected type for the MonoMethod. So we
  7323. need to check that m also has base->klass as the reflected
  7324. type. */
  7325. MonoReflectionTypeHandle orig_reftype = MONO_HANDLE_NEW_GET (MonoReflectionType, m, reftype);
  7326. MonoClass *orig_klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (orig_reftype, type));
  7327. if (base->klass == orig_klass)
  7328. return m;
  7329. }
  7330. return mono_method_get_object_handle (mono_domain_get (), base, NULL, error);
  7331. }
  7332. MonoStringHandle
  7333. ves_icall_RuntimeMethodInfo_get_name (MonoReflectionMethodHandle m, MonoError *error)
  7334. {
  7335. MonoMethod *method = MONO_HANDLE_GETVAL (m, method);
  7336. MonoStringHandle s = mono_string_new_handle (MONO_HANDLE_DOMAIN (m), method->name, error);
  7337. return_val_if_nok (error, NULL_HANDLE_STRING);
  7338. MONO_HANDLE_SET (m, name, s);
  7339. return s;
  7340. }
  7341. void
  7342. ves_icall_System_ArgIterator_Setup (MonoArgIterator *iter, char* argsp, char* start)
  7343. {
  7344. iter->sig = *(MonoMethodSignature**)argsp;
  7345. g_assert (iter->sig->sentinelpos <= iter->sig->param_count);
  7346. g_assert (iter->sig->call_convention == MONO_CALL_VARARG);
  7347. iter->next_arg = 0;
  7348. /* FIXME: it's not documented what start is exactly... */
  7349. if (start) {
  7350. iter->args = start;
  7351. } else {
  7352. iter->args = argsp + sizeof (gpointer);
  7353. }
  7354. iter->num_args = iter->sig->param_count - iter->sig->sentinelpos;
  7355. /* g_print ("sig %p, param_count: %d, sent: %d\n", iter->sig, iter->sig->param_count, iter->sig->sentinelpos); */
  7356. }
  7357. void
  7358. ves_icall_System_ArgIterator_IntGetNextArg (MonoArgIterator *iter, MonoTypedRef *res)
  7359. {
  7360. guint32 i, arg_size;
  7361. gint32 align;
  7362. i = iter->sig->sentinelpos + iter->next_arg;
  7363. g_assert (i < iter->sig->param_count);
  7364. res->type = iter->sig->params [i];
  7365. res->klass = mono_class_from_mono_type_internal (res->type);
  7366. arg_size = mono_type_stack_size (res->type, &align);
  7367. #if defined(__arm__) || defined(__mips__)
  7368. iter->args = (guint8*)(((gsize)iter->args + (align) - 1) & ~(align - 1));
  7369. #endif
  7370. res->value = iter->args;
  7371. #if G_BYTE_ORDER != G_LITTLE_ENDIAN
  7372. if (arg_size <= sizeof (gpointer)) {
  7373. int dummy;
  7374. int padding = arg_size - mono_type_size (res->type, &dummy);
  7375. res->value = (guint8*)res->value + padding;
  7376. }
  7377. #endif
  7378. iter->args = (char*)iter->args + arg_size;
  7379. iter->next_arg++;
  7380. /* g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res->type->type, arg_size, res->value); */
  7381. }
  7382. void
  7383. ves_icall_System_ArgIterator_IntGetNextArgWithType (MonoArgIterator *iter, MonoTypedRef *res, MonoType *type)
  7384. {
  7385. guint32 i, arg_size;
  7386. gint32 align;
  7387. i = iter->sig->sentinelpos + iter->next_arg;
  7388. g_assert (i < iter->sig->param_count);
  7389. while (i < iter->sig->param_count) {
  7390. if (!mono_metadata_type_equal (type, iter->sig->params [i]))
  7391. continue;
  7392. res->type = iter->sig->params [i];
  7393. res->klass = mono_class_from_mono_type_internal (res->type);
  7394. /* FIXME: endianess issue... */
  7395. arg_size = mono_type_stack_size (res->type, &align);
  7396. #if defined(__arm__) || defined(__mips__)
  7397. iter->args = (guint8*)(((gsize)iter->args + (align) - 1) & ~(align - 1));
  7398. #endif
  7399. res->value = iter->args;
  7400. iter->args = (char*)iter->args + arg_size;
  7401. iter->next_arg++;
  7402. /* g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res.type->type, arg_size, res.value); */
  7403. return;
  7404. }
  7405. /* g_print ("arg type 0x%02x not found\n", res.type->type); */
  7406. memset (res, 0, sizeof (MonoTypedRef));
  7407. }
  7408. MonoType*
  7409. ves_icall_System_ArgIterator_IntGetNextArgType (MonoArgIterator *iter)
  7410. {
  7411. gint i;
  7412. i = iter->sig->sentinelpos + iter->next_arg;
  7413. g_assert (i < iter->sig->param_count);
  7414. return iter->sig->params [i];
  7415. }
  7416. MonoObjectHandle
  7417. ves_icall_System_TypedReference_ToObject (MonoTypedRef* tref, MonoError *error)
  7418. {
  7419. return typed_reference_to_object (tref, error);
  7420. }
  7421. void
  7422. ves_icall_System_TypedReference_InternalMakeTypedReference (MonoTypedRef *res, MonoObjectHandle target, MonoArrayHandle fields, MonoReflectionTypeHandle last_field, MonoError *error)
  7423. {
  7424. MonoType *ftype = NULL;
  7425. int i;
  7426. memset (res, 0, sizeof (MonoTypedRef));
  7427. g_assert (mono_array_handle_length (fields) > 0);
  7428. (void)mono_handle_class (target);
  7429. int offset = 0;
  7430. for (i = 0; i < mono_array_handle_length (fields); ++i) {
  7431. MonoClassField *f;
  7432. MONO_HANDLE_ARRAY_GETVAL (f, fields, MonoClassField*, i);
  7433. g_assert (f);
  7434. if (i == 0)
  7435. offset = f->offset;
  7436. else
  7437. offset += f->offset - sizeof (MonoObject);
  7438. (void)mono_class_from_mono_type_internal (f->type);
  7439. ftype = f->type;
  7440. }
  7441. res->type = ftype;
  7442. res->klass = mono_class_from_mono_type_internal (ftype);
  7443. res->value = (guint8*)MONO_HANDLE_RAW (target) + offset;
  7444. }
  7445. static void
  7446. prelink_method (MonoMethod *method, MonoError *error)
  7447. {
  7448. error_init (error);
  7449. if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
  7450. return;
  7451. mono_lookup_pinvoke_call_internal (method, error);
  7452. /* create the wrapper, too? */
  7453. }
  7454. void
  7455. ves_icall_System_Runtime_InteropServices_Marshal_Prelink (MonoReflectionMethodHandle method, MonoError *error)
  7456. {
  7457. error_init (error);
  7458. prelink_method (MONO_HANDLE_GETVAL (method, method), error);
  7459. }
  7460. void
  7461. ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionTypeHandle type, MonoError *error);
  7462. void
  7463. ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionTypeHandle type, MonoError *error)
  7464. {
  7465. g_assert_not_netcore ();
  7466. error_init (error);
  7467. MonoClass *klass = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (type, type));
  7468. MonoMethod* m;
  7469. gpointer iter = NULL;
  7470. mono_class_init_checked (klass, error);
  7471. return_if_nok (error);
  7472. while ((m = mono_class_get_methods (klass, &iter))) {
  7473. prelink_method (m, error);
  7474. return_if_nok (error);
  7475. }
  7476. }
  7477. #ifndef ENABLE_NETCORE
  7478. /*
  7479. * used by System.Runtime.InteropServices.RuntimeInformation.(OS|Process)Architecture;
  7480. * which use them in different ways for filling in an enum
  7481. */
  7482. MonoStringHandle
  7483. ves_icall_System_Runtime_InteropServices_RuntimeInformation_GetRuntimeArchitecture (MonoError *error)
  7484. {
  7485. return mono_string_new_handle (mono_domain_get (), mono_config_get_cpu (), error);
  7486. }
  7487. /*
  7488. * used by System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform
  7489. */
  7490. MonoStringHandle
  7491. ves_icall_System_Runtime_InteropServices_RuntimeInformation_GetOSName (MonoError *error)
  7492. {
  7493. return mono_string_new_handle (mono_domain_get (), mono_config_get_os (), error);
  7494. }
  7495. #endif /* ENABLE_NETCORE */
  7496. int
  7497. ves_icall_Interop_Sys_DoubleToString(double value, char *format, char *buffer, int bufferLength)
  7498. {
  7499. #if defined(TARGET_ARM)
  7500. /* workaround for faulty vcmp.f64 implementation on some 32bit ARM CPUs */
  7501. guint64 bits = *(guint64 *) &value;
  7502. if (bits == 0x1) { /* 4.9406564584124654E-324 */
  7503. g_assert (!strcmp (format, "%.40e"));
  7504. return snprintf (buffer, bufferLength, "%s", "4.9406564584124654417656879286822137236506e-324");
  7505. } else if (bits == 0x4) { /* 2E-323 */
  7506. g_assert (!strcmp (format, "%.40e"));
  7507. return snprintf (buffer, bufferLength, "%s", "1.9762625833649861767062751714728854894602e-323");
  7508. }
  7509. #endif
  7510. return snprintf(buffer, bufferLength, format, value);
  7511. }
  7512. void
  7513. ves_icall_System_Runtime_RuntimeImports_ecvt_s(char *buffer, size_t sizeInBytes, double value, int count, int* dec, int* sign)
  7514. {
  7515. #if defined(TARGET_WIN32) || defined(HOST_WIN32)
  7516. _ecvt_s(buffer, sizeInBytes, value, count, dec, sign);
  7517. #endif
  7518. }
  7519. /* These parameters are "readonly" in corlib/System/NumberFormatter.cs */
  7520. void
  7521. ves_icall_System_NumberFormatter_GetFormatterTables (guint64 const **mantissas,
  7522. gint32 const **exponents,
  7523. gunichar2 const **digitLowerTable,
  7524. gunichar2 const **digitUpperTable,
  7525. gint64 const **tenPowersList,
  7526. gint32 const **decHexDigits)
  7527. {
  7528. *mantissas = Formatter_MantissaBitsTable;
  7529. *exponents = Formatter_TensExponentTable;
  7530. *digitLowerTable = Formatter_DigitLowerTable;
  7531. *digitUpperTable = Formatter_DigitUpperTable;
  7532. *tenPowersList = Formatter_TenPowersList;
  7533. *decHexDigits = Formatter_DecHexDigits;
  7534. }
  7535. static gboolean
  7536. add_modifier_to_array (MonoDomain *domain, MonoType *type, MonoArrayHandle dest, int dest_idx, MonoError *error)
  7537. {
  7538. HANDLE_FUNCTION_ENTER ();
  7539. error_init (error);
  7540. MonoClass *klass = mono_class_from_mono_type_internal (type);
  7541. MonoReflectionTypeHandle rt;
  7542. rt = mono_type_get_object_handle (domain, m_class_get_byval_arg (klass), error);
  7543. goto_if_nok (error, leave);
  7544. MONO_HANDLE_ARRAY_SETREF (dest, dest_idx, rt);
  7545. leave:
  7546. HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
  7547. }
  7548. /*
  7549. * We return NULL for no modifiers so the corlib code can return Type.EmptyTypes
  7550. * and avoid useless allocations.
  7551. */
  7552. static MonoArrayHandle
  7553. type_array_from_modifiers (MonoType *type, int optional, MonoError *error)
  7554. {
  7555. int i, count = 0;
  7556. MonoDomain *domain = mono_domain_get ();
  7557. int cmod_count = mono_type_custom_modifier_count (type);
  7558. if (cmod_count == 0)
  7559. goto fail;
  7560. error_init (error);
  7561. for (i = 0; i < cmod_count; ++i) {
  7562. gboolean required;
  7563. (void) mono_type_get_custom_modifier (type, i, &required, error);
  7564. goto_if_nok (error, fail);
  7565. if ((optional && !required) || (!optional && required))
  7566. count++;
  7567. }
  7568. if (!count)
  7569. goto fail;
  7570. MonoArrayHandle res;
  7571. res = mono_array_new_handle (domain, mono_defaults.systemtype_class, count, error);
  7572. goto_if_nok (error, fail);
  7573. count = 0;
  7574. for (i = 0; i < cmod_count; ++i) {
  7575. gboolean required;
  7576. MonoType *cmod_type = mono_type_get_custom_modifier (type, i, &required, error);
  7577. goto_if_nok (error, fail);
  7578. if ((optional && !required) || (!optional && required)) {
  7579. if (!add_modifier_to_array (domain, cmod_type, res, count, error))
  7580. goto fail;
  7581. count++;
  7582. }
  7583. }
  7584. return res;
  7585. fail:
  7586. return MONO_HANDLE_NEW (MonoArray, NULL);
  7587. }
  7588. MonoArrayHandle
  7589. ves_icall_RuntimeParameterInfo_GetTypeModifiers (MonoReflectionTypeHandle rt, MonoObjectHandle member, int pos, MonoBoolean optional, MonoError *error)
  7590. {
  7591. error_init (error);
  7592. MonoType *type = MONO_HANDLE_GETVAL (rt, type);
  7593. MonoClass *member_class = mono_handle_class (member);
  7594. MonoMethod *method = NULL;
  7595. MonoImage *image;
  7596. MonoMethodSignature *sig;
  7597. if (mono_class_is_reflection_method_or_constructor (member_class)) {
  7598. method = MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoReflectionMethod, member), method);
  7599. } else if (m_class_get_image (member_class) == mono_defaults.corlib && !strcmp ("RuntimePropertyInfo", m_class_get_name (member_class))) {
  7600. MonoProperty *prop = MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoReflectionProperty, member), property);
  7601. if (!(method = prop->get))
  7602. method = prop->set;
  7603. g_assert (method);
  7604. } else if (strcmp (m_class_get_name (member_class), "DynamicMethod") == 0 && strcmp (m_class_get_name_space (member_class), "System.Reflection.Emit") == 0) {
  7605. MonoArrayHandle params = MONO_HANDLE_NEW_GET (MonoArray, MONO_HANDLE_CAST (MonoReflectionDynamicMethod, member), parameters);
  7606. MonoReflectionTypeHandle t = MONO_HANDLE_NEW (MonoReflectionType, NULL);
  7607. MONO_HANDLE_ARRAY_GETREF (t, params, pos);
  7608. type = mono_reflection_type_handle_mono_type (t, error);
  7609. return type_array_from_modifiers (type, optional, error);
  7610. } else {
  7611. char *type_name = mono_type_get_full_name (member_class);
  7612. mono_error_set_not_supported (error, "Custom modifiers on a ParamInfo with member %s are not supported", type_name);
  7613. g_free (type_name);
  7614. return NULL_HANDLE_ARRAY;
  7615. }
  7616. image = m_class_get_image (method->klass);
  7617. sig = mono_method_signature_internal (method);
  7618. if (pos == -1)
  7619. type = sig->ret;
  7620. else
  7621. type = sig->params [pos];
  7622. return type_array_from_modifiers (type, optional, error);
  7623. }
  7624. static MonoType*
  7625. get_property_type (MonoProperty *prop)
  7626. {
  7627. MonoMethodSignature *sig;
  7628. if (prop->get) {
  7629. sig = mono_method_signature_internal (prop->get);
  7630. return sig->ret;
  7631. } else if (prop->set) {
  7632. sig = mono_method_signature_internal (prop->set);
  7633. return sig->params [sig->param_count - 1];
  7634. }
  7635. return NULL;
  7636. }
  7637. MonoArrayHandle
  7638. ves_icall_RuntimePropertyInfo_GetTypeModifiers (MonoReflectionPropertyHandle property, MonoBoolean optional, MonoError *error)
  7639. {
  7640. error_init (error);
  7641. MonoProperty *prop = MONO_HANDLE_GETVAL (property, property);
  7642. MonoType *type = get_property_type (prop);
  7643. if (!type)
  7644. return NULL_HANDLE_ARRAY;
  7645. return type_array_from_modifiers (type, optional, error);
  7646. }
  7647. /*
  7648. *Construct a MonoType suited to be used to decode a constant blob object.
  7649. *
  7650. * @type is the target type which will be constructed
  7651. * @blob_type is the blob type, for example, that comes from the constant table
  7652. * @real_type is the expected constructed type.
  7653. */
  7654. static void
  7655. mono_type_from_blob_type (MonoType *type, MonoTypeEnum blob_type, MonoType *real_type)
  7656. {
  7657. type->type = blob_type;
  7658. type->data.klass = NULL;
  7659. if (blob_type == MONO_TYPE_CLASS)
  7660. type->data.klass = mono_defaults.object_class;
  7661. else if (real_type->type == MONO_TYPE_VALUETYPE && m_class_is_enumtype (real_type->data.klass)) {
  7662. /* For enums, we need to use the base type */
  7663. type->type = MONO_TYPE_VALUETYPE;
  7664. type->data.klass = mono_class_from_mono_type_internal (real_type);
  7665. } else
  7666. type->data.klass = mono_class_from_mono_type_internal (real_type);
  7667. }
  7668. MonoObjectHandle
  7669. ves_icall_property_info_get_default_value (MonoReflectionPropertyHandle property_handle, MonoError* error)
  7670. {
  7671. MonoReflectionProperty* property = MONO_HANDLE_RAW (property_handle);
  7672. MonoType blob_type;
  7673. MonoProperty *prop = property->property;
  7674. MonoType *type = get_property_type (prop);
  7675. MonoDomain *domain = mono_object_domain (property);
  7676. MonoTypeEnum def_type;
  7677. const char *def_value;
  7678. mono_class_init_internal (prop->parent);
  7679. if (!(prop->attrs & PROPERTY_ATTRIBUTE_HAS_DEFAULT)) {
  7680. mono_error_set_invalid_operation (error, NULL);
  7681. return NULL_HANDLE;
  7682. }
  7683. def_value = mono_class_get_property_default_value (prop, &def_type);
  7684. mono_type_from_blob_type (&blob_type, def_type, type);
  7685. return mono_get_object_from_blob (domain, &blob_type, def_value, MONO_HANDLE_NEW (MonoString, NULL), error);
  7686. }
  7687. MonoBoolean
  7688. ves_icall_MonoCustomAttrs_IsDefinedInternal (MonoObjectHandle obj, MonoReflectionTypeHandle attr_type, MonoError *error)
  7689. {
  7690. MonoClass *attr_class = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (attr_type, type));
  7691. mono_class_init_checked (attr_class, error);
  7692. return_val_if_nok (error, FALSE);
  7693. MonoCustomAttrInfo *cinfo = mono_reflection_get_custom_attrs_info_checked (obj, error);
  7694. return_val_if_nok (error, FALSE);
  7695. if (!cinfo)
  7696. return FALSE;
  7697. gboolean found = mono_custom_attrs_has_attr (cinfo, attr_class);
  7698. if (!cinfo->cached)
  7699. mono_custom_attrs_free (cinfo);
  7700. return found;
  7701. }
  7702. MonoArrayHandle
  7703. ves_icall_MonoCustomAttrs_GetCustomAttributesInternal (MonoObjectHandle obj, MonoReflectionTypeHandle attr_type, MonoBoolean pseudoattrs, MonoError *error)
  7704. {
  7705. MonoClass *attr_class;
  7706. if (MONO_HANDLE_IS_NULL (attr_type))
  7707. attr_class = NULL;
  7708. else
  7709. attr_class = mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (attr_type, type));
  7710. if (attr_class) {
  7711. mono_class_init_checked (attr_class, error);
  7712. return_val_if_nok (error, NULL_HANDLE_ARRAY);
  7713. }
  7714. return mono_reflection_get_custom_attrs_by_type_handle (obj, attr_class, error);
  7715. }
  7716. MonoArrayHandle
  7717. ves_icall_MonoCustomAttrs_GetCustomAttributesDataInternal (MonoObjectHandle obj, MonoError *error)
  7718. {
  7719. return mono_reflection_get_custom_attrs_data_checked (obj, error);
  7720. }
  7721. MonoStringHandle
  7722. ves_icall_Mono_Runtime_GetDisplayName (MonoError *error)
  7723. {
  7724. char *info;
  7725. MonoStringHandle display_name;
  7726. error_init (error);
  7727. info = mono_get_runtime_callbacks ()->get_runtime_build_info ();
  7728. display_name = mono_string_new_handle (mono_domain_get (), info, error);
  7729. g_free (info);
  7730. return display_name;
  7731. }
  7732. #ifndef HOST_WIN32
  7733. static gint32
  7734. mono_icall_wait_for_input_idle (gpointer handle, gint32 milliseconds)
  7735. {
  7736. return WAIT_TIMEOUT;
  7737. }
  7738. #endif /* !HOST_WIN32 */
  7739. #ifndef ENABLE_NETCORE
  7740. gint32
  7741. ves_icall_Microsoft_Win32_NativeMethods_WaitForInputIdle (gpointer handle, gint32 milliseconds)
  7742. {
  7743. return mono_icall_wait_for_input_idle (handle, milliseconds);
  7744. }
  7745. gint32
  7746. ves_icall_Microsoft_Win32_NativeMethods_GetCurrentProcessId (void)
  7747. {
  7748. return mono_process_current_pid ();
  7749. }
  7750. MonoBoolean
  7751. ves_icall_Mono_TlsProviderFactory_IsBtlsSupported (void)
  7752. {
  7753. #if HAVE_BTLS
  7754. return TRUE;
  7755. #else
  7756. return FALSE;
  7757. #endif
  7758. }
  7759. #endif /* ENABLE_NETCORE */
  7760. #ifndef DISABLE_COM
  7761. int
  7762. ves_icall_System_Runtime_InteropServices_Marshal_GetHRForException_WinRT(MonoExceptionHandle ex, MonoError *error)
  7763. {
  7764. mono_error_set_not_implemented (error, "System.Runtime.InteropServices.Marshal.GetHRForException_WinRT internal call is not implemented.");
  7765. return 0;
  7766. }
  7767. MonoObjectHandle
  7768. ves_icall_System_Runtime_InteropServices_Marshal_GetNativeActivationFactory(MonoObjectHandle type, MonoError *error)
  7769. {
  7770. mono_error_set_not_implemented (error, "System.Runtime.InteropServices.Marshal.GetNativeActivationFactory internal call is not implemented.");
  7771. return NULL_HANDLE;
  7772. }
  7773. void*
  7774. ves_icall_System_Runtime_InteropServices_Marshal_GetRawIUnknownForComObjectNoAddRef(MonoObjectHandle obj, MonoError *error)
  7775. {
  7776. mono_error_set_not_implemented (error, "System.Runtime.InteropServices.Marshal.GetRawIUnknownForComObjectNoAddRef internal call is not implemented.");
  7777. return NULL;
  7778. }
  7779. MonoObjectHandle
  7780. ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_GetRestrictedErrorInfo(MonoError *error)
  7781. {
  7782. mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.GetRestrictedErrorInfo internal call is not implemented.");
  7783. return NULL_HANDLE;
  7784. }
  7785. MonoBoolean
  7786. ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_RoOriginateLanguageException (int ierr, MonoStringHandle message, void* languageException, MonoError *error)
  7787. {
  7788. mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.RoOriginateLanguageException internal call is not implemented.");
  7789. return FALSE;
  7790. }
  7791. void
  7792. ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_RoReportUnhandledError (MonoObjectHandle oerr, MonoError *error)
  7793. {
  7794. mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.RoReportUnhandledError internal call is not implemented.");
  7795. }
  7796. int
  7797. ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_WindowsCreateString(MonoStringHandle sourceString, int length, void** hstring, MonoError *error)
  7798. {
  7799. mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.WindowsCreateString internal call is not implemented.");
  7800. return 0;
  7801. }
  7802. int
  7803. ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_WindowsDeleteString(void* hstring, MonoError *error)
  7804. {
  7805. mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.WindowsDeleteString internal call is not implemented.");
  7806. return 0;
  7807. }
  7808. mono_unichar2*
  7809. ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_WindowsGetStringRawBuffer(void* hstring, unsigned* length, MonoError *error)
  7810. {
  7811. mono_error_set_not_implemented (error, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.WindowsGetStringRawBuffer internal call is not implemented.");
  7812. return NULL;
  7813. }
  7814. #endif
  7815. #if !ENABLE_NETCORE
  7816. void
  7817. ves_icall_System_IO_LogcatTextWriter_Log (const char *appname, gint32 level, const char *message)
  7818. {
  7819. g_log (appname, (GLogLevelFlags)level, "%s", message);
  7820. }
  7821. #endif
  7822. static const MonoIcallTableCallbacks *icall_table;
  7823. static mono_mutex_t icall_mutex;
  7824. static GHashTable *icall_hash = NULL;
  7825. typedef struct _MonoIcallHashTableValue {
  7826. gconstpointer method;
  7827. guint32 flags;
  7828. } MonoIcallHashTableValue;
  7829. void
  7830. mono_install_icall_table_callbacks (const MonoIcallTableCallbacks *cb)
  7831. {
  7832. g_assert (cb->version == MONO_ICALL_TABLE_CALLBACKS_VERSION);
  7833. icall_table = cb;
  7834. }
  7835. void
  7836. mono_icall_init (void)
  7837. {
  7838. #ifndef DISABLE_ICALL_TABLES
  7839. mono_icall_table_init ();
  7840. #endif
  7841. icall_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
  7842. mono_os_mutex_init (&icall_mutex);
  7843. }
  7844. static void
  7845. mono_icall_lock (void)
  7846. {
  7847. mono_locks_os_acquire (&icall_mutex, IcallLock);
  7848. }
  7849. static void
  7850. mono_icall_unlock (void)
  7851. {
  7852. mono_locks_os_release (&icall_mutex, IcallLock);
  7853. }
  7854. void
  7855. mono_icall_cleanup (void)
  7856. {
  7857. g_hash_table_destroy (icall_hash);
  7858. mono_os_mutex_destroy (&icall_mutex);
  7859. }
  7860. static void
  7861. add_internal_call_with_flags (const char *name, gconstpointer method, guint32 flags)
  7862. {
  7863. char *key = g_strdup (name);
  7864. MonoIcallHashTableValue *value = g_new (MonoIcallHashTableValue, 1);
  7865. if (key && value) {
  7866. value->method = method;
  7867. value->flags = flags;
  7868. mono_icall_lock ();
  7869. g_hash_table_insert (icall_hash, key, (gpointer)value);
  7870. mono_icall_unlock ();
  7871. }
  7872. }
  7873. /**
  7874. * mono_dangerous_add_internal_call_coop:
  7875. * \param name method specification to surface to the managed world
  7876. * \param method pointer to a C method to invoke when the method is called
  7877. *
  7878. * Similar to \c mono_dangerous_add_raw_internal_call.
  7879. *
  7880. */
  7881. void
  7882. mono_dangerous_add_internal_call_coop (const char *name, gconstpointer method)
  7883. {
  7884. add_internal_call_with_flags (name, method, MONO_ICALL_FLAGS_COOPERATIVE);
  7885. }
  7886. /**
  7887. * mono_dangerous_add_internal_call_no_wrapper:
  7888. * \param name method specification to surface to the managed world
  7889. * \param method pointer to a C method to invoke when the method is called
  7890. *
  7891. * Similar to \c mono_dangerous_add_raw_internal_call but with more requirements for correct
  7892. * operation.
  7893. *
  7894. * The \p method must NOT:
  7895. *
  7896. * Run for an unbounded amount of time without calling the mono runtime.
  7897. * Additionally, the method must switch to GC Safe mode to perform all blocking
  7898. * operations: performing blocking I/O, taking locks, etc. The method can't throw or raise
  7899. * exceptions or call other methods that will throw or raise exceptions since the runtime won't
  7900. * be able to detect exeptions and unwinder won't be able to correctly find last managed frame in callstack.
  7901. * This registration method is for icalls that needs very low overhead and follow all rules in their implementation.
  7902. *
  7903. */
  7904. void
  7905. mono_dangerous_add_internal_call_no_wrapper (const char *name, gconstpointer method)
  7906. {
  7907. add_internal_call_with_flags (name, method, MONO_ICALL_FLAGS_NO_WRAPPER);
  7908. }
  7909. /**
  7910. * mono_add_internal_call:
  7911. * \param name method specification to surface to the managed world
  7912. * \param method pointer to a C method to invoke when the method is called
  7913. *
  7914. * This method surfaces the C function pointed by \p method as a method
  7915. * that has been surfaced in managed code with the method specified in
  7916. * \p name as an internal call.
  7917. *
  7918. * Internal calls are surfaced to all app domains loaded and they are
  7919. * accessibly by a type with the specified name.
  7920. *
  7921. * You must provide a fully qualified type name, that is namespaces
  7922. * and type name, followed by a colon and the method name, with an
  7923. * optional signature to bind.
  7924. *
  7925. * For example, the following are all valid declarations:
  7926. *
  7927. * \c MyApp.Services.ScriptService:Accelerate
  7928. *
  7929. * \c MyApp.Services.ScriptService:Slowdown(int,bool)
  7930. *
  7931. * You use method parameters in cases where there might be more than
  7932. * one surface method to managed code. That way you can register different
  7933. * internal calls for different method overloads.
  7934. *
  7935. * The internal calls are invoked with no marshalling. This means that .NET
  7936. * types like \c System.String are exposed as \c MonoString* parameters. This is
  7937. * different than the way that strings are surfaced in P/Invoke.
  7938. *
  7939. * For more information on how the parameters are marshalled, see the
  7940. * <a href="http://www.mono-project.com/docs/advanced/embedding/">Mono Embedding</a>
  7941. * page.
  7942. *
  7943. * See the <a href="mono-api-methods.html#method-desc">Method Description</a>
  7944. * reference for more information on the format of method descriptions.
  7945. */
  7946. void
  7947. mono_add_internal_call (const char *name, gconstpointer method)
  7948. {
  7949. add_internal_call_with_flags (name, method, MONO_ICALL_FLAGS_FOREIGN);
  7950. }
  7951. /**
  7952. * mono_dangerous_add_raw_internal_call:
  7953. * \param name method specification to surface to the managed world
  7954. * \param method pointer to a C method to invoke when the method is called
  7955. *
  7956. * Similar to \c mono_add_internal_call but with more requirements for correct
  7957. * operation.
  7958. *
  7959. * A thread running a dangerous raw internal call will avoid a thread state
  7960. * transition on entry and exit, but it must take responsiblity for cooperating
  7961. * with the Mono runtime.
  7962. *
  7963. * The \p method must NOT:
  7964. *
  7965. * Run for an unbounded amount of time without calling the mono runtime.
  7966. * Additionally, the method must switch to GC Safe mode to perform all blocking
  7967. * operations: performing blocking I/O, taking locks, etc.
  7968. *
  7969. */
  7970. void
  7971. mono_dangerous_add_raw_internal_call (const char *name, gconstpointer method)
  7972. {
  7973. add_internal_call_with_flags (name, method, MONO_ICALL_FLAGS_COOPERATIVE);
  7974. }
  7975. /**
  7976. * mono_add_internal_call_with_flags:
  7977. * \param name method specification to surface to the managed world
  7978. * \param method pointer to a C method to invoke when the method is called
  7979. * \param cooperative if \c TRUE, run icall in GC Unsafe (cooperatively suspended) mode,
  7980. * otherwise GC Safe (blocking)
  7981. *
  7982. * Like \c mono_add_internal_call, but if \p cooperative is \c TRUE the added
  7983. * icall promises that it will use the coopertive API to inform the runtime
  7984. * when it is running blocking operations, that it will not run for unbounded
  7985. * amounts of time without safepointing, and that it will not hold managed
  7986. * object references across suspend safepoints.
  7987. *
  7988. * If \p cooperative is \c FALSE, run the icall in GC Safe mode - the icall may
  7989. * block. The icall must obey the GC Safe rules, e.g. it must not touch
  7990. * unpinned managed memory.
  7991. *
  7992. */
  7993. void
  7994. mono_add_internal_call_with_flags (const char *name, gconstpointer method, gboolean cooperative)
  7995. {
  7996. add_internal_call_with_flags (name, method, cooperative ? MONO_ICALL_FLAGS_COOPERATIVE : MONO_ICALL_FLAGS_FOREIGN);
  7997. }
  7998. void
  7999. mono_add_internal_call_internal (const char *name, gconstpointer method)
  8000. {
  8001. add_internal_call_with_flags (name, method, MONO_ICALL_FLAGS_COOPERATIVE);
  8002. }
  8003. /*
  8004. * we should probably export this as an helper (handle nested types).
  8005. * Returns the number of chars written in buf.
  8006. */
  8007. static int
  8008. concat_class_name (char *buf, int bufsize, MonoClass *klass)
  8009. {
  8010. int nspacelen, cnamelen;
  8011. nspacelen = strlen (m_class_get_name_space (klass));
  8012. cnamelen = strlen (m_class_get_name (klass));
  8013. if (nspacelen + cnamelen + 2 > bufsize)
  8014. return 0;
  8015. if (nspacelen) {
  8016. memcpy (buf, m_class_get_name_space (klass), nspacelen);
  8017. buf [nspacelen ++] = '.';
  8018. }
  8019. memcpy (buf + nspacelen, m_class_get_name (klass), cnamelen);
  8020. buf [nspacelen + cnamelen] = 0;
  8021. return nspacelen + cnamelen;
  8022. }
  8023. static void
  8024. no_icall_table (void)
  8025. {
  8026. g_assert_not_reached ();
  8027. }
  8028. gboolean
  8029. mono_is_missing_icall_addr (gconstpointer addr)
  8030. {
  8031. return addr == NULL || addr == no_icall_table;
  8032. }
  8033. /*
  8034. * Returns either NULL or no_icall_table for missing icalls.
  8035. */
  8036. gconstpointer
  8037. mono_lookup_internal_call_full_with_flags (MonoMethod *method, gboolean warn_on_missing, guint32 *flags)
  8038. {
  8039. char *sigstart = NULL;
  8040. char *tmpsig = NULL;
  8041. char mname [2048];
  8042. char *classname = NULL;
  8043. int typelen = 0, mlen, siglen;
  8044. gconstpointer res = NULL;
  8045. gboolean locked = FALSE;
  8046. g_assert (method != NULL);
  8047. if (method->is_inflated)
  8048. method = ((MonoMethodInflated *) method)->declaring;
  8049. if (m_class_get_nested_in (method->klass)) {
  8050. int pos = concat_class_name (mname, sizeof (mname)-2, m_class_get_nested_in (method->klass));
  8051. if (!pos)
  8052. goto exit;
  8053. mname [pos++] = '/';
  8054. mname [pos] = 0;
  8055. typelen = concat_class_name (mname+pos, sizeof (mname)-pos-1, method->klass);
  8056. if (!typelen)
  8057. goto exit;
  8058. typelen += pos;
  8059. } else {
  8060. typelen = concat_class_name (mname, sizeof (mname), method->klass);
  8061. if (!typelen)
  8062. goto exit;
  8063. }
  8064. classname = g_strdup (mname);
  8065. mname [typelen] = ':';
  8066. mname [typelen + 1] = ':';
  8067. mlen = strlen (method->name);
  8068. memcpy (mname + typelen + 2, method->name, mlen);
  8069. sigstart = mname + typelen + 2 + mlen;
  8070. *sigstart = 0;
  8071. tmpsig = mono_signature_get_desc (mono_method_signature_internal (method), TRUE);
  8072. siglen = strlen (tmpsig);
  8073. if (typelen + mlen + siglen + 6 > sizeof (mname))
  8074. goto exit;
  8075. sigstart [0] = '(';
  8076. memcpy (sigstart + 1, tmpsig, siglen);
  8077. sigstart [siglen + 1] = ')';
  8078. sigstart [siglen + 2] = 0;
  8079. /* mono_marshal_get_native_wrapper () depends on this */
  8080. if (method->klass == mono_defaults.string_class && !strcmp (method->name, ".ctor")) {
  8081. res = (gconstpointer)ves_icall_System_String_ctor_RedirectToCreateString;
  8082. goto exit;
  8083. }
  8084. mono_icall_lock ();
  8085. locked = TRUE;
  8086. res = g_hash_table_lookup (icall_hash, mname);
  8087. if (res) {
  8088. MonoIcallHashTableValue *value = (MonoIcallHashTableValue *)res;
  8089. if (flags)
  8090. *flags = value->flags;
  8091. res = value->method;
  8092. goto exit;
  8093. }
  8094. /* try without signature */
  8095. *sigstart = 0;
  8096. res = g_hash_table_lookup (icall_hash, mname);
  8097. if (res) {
  8098. MonoIcallHashTableValue *value = (MonoIcallHashTableValue *)res;
  8099. if (flags)
  8100. *flags = value->flags;
  8101. res = value->method;
  8102. goto exit;
  8103. }
  8104. if (!icall_table) {
  8105. /* Fail only when the result is actually used */
  8106. res = (gconstpointer)no_icall_table;
  8107. goto exit;
  8108. } else {
  8109. gboolean uses_handles = FALSE;
  8110. g_assert (icall_table->lookup);
  8111. res = icall_table->lookup (method, classname, sigstart - mlen, sigstart, &uses_handles);
  8112. if (res && flags && uses_handles)
  8113. *flags = *flags | MONO_ICALL_FLAGS_USES_HANDLES;
  8114. mono_icall_unlock ();
  8115. locked = FALSE;
  8116. if (res)
  8117. goto exit;
  8118. if (warn_on_missing) {
  8119. g_warning ("cant resolve internal call to \"%s\" (tested without signature also)", mname);
  8120. g_print ("\nYour mono runtime and class libraries are out of sync.\n");
  8121. g_print ("The out of sync library is: %s\n", m_class_get_image (method->klass)->name);
  8122. g_print ("\nWhen you update one from git you need to update, compile and install\nthe other too.\n");
  8123. g_print ("Do not report this as a bug unless you're sure you have updated correctly:\nyou probably have a broken mono install.\n");
  8124. g_print ("If you see other errors or faults after this message they are probably related\n");
  8125. g_print ("and you need to fix your mono install first.\n");
  8126. }
  8127. res = NULL;
  8128. }
  8129. exit:
  8130. if (locked)
  8131. mono_icall_unlock ();
  8132. g_free (classname);
  8133. g_free (tmpsig);
  8134. return res;
  8135. }
  8136. /**
  8137. * mono_lookup_internal_call_full:
  8138. * \param method the method to look up
  8139. * \param uses_handles out argument if method needs handles around managed objects.
  8140. * \returns a pointer to the icall code for the given method. If
  8141. * \p uses_handles is not NULL, it will be set to TRUE if the method
  8142. * needs managed objects wrapped using the infrastructure in handle.h
  8143. *
  8144. * If the method is not found, warns and returns NULL.
  8145. */
  8146. gconstpointer
  8147. mono_lookup_internal_call_full (MonoMethod *method, gboolean warn_on_missing, mono_bool *uses_handles, mono_bool *foreign)
  8148. {
  8149. if (uses_handles)
  8150. *uses_handles = FALSE;
  8151. if (foreign)
  8152. *foreign = FALSE;
  8153. guint32 flags = MONO_ICALL_FLAGS_NONE;
  8154. gconstpointer addr = mono_lookup_internal_call_full_with_flags (method, warn_on_missing, &flags);
  8155. if (uses_handles && (flags & MONO_ICALL_FLAGS_USES_HANDLES))
  8156. *uses_handles = TRUE;
  8157. if (foreign && (flags & MONO_ICALL_FLAGS_FOREIGN))
  8158. *foreign = TRUE;
  8159. return addr;
  8160. }
  8161. /**
  8162. * mono_lookup_internal_call:
  8163. */
  8164. gpointer
  8165. mono_lookup_internal_call (MonoMethod *method)
  8166. {
  8167. return (gpointer)mono_lookup_internal_call_full (method, TRUE, NULL, NULL);
  8168. }
  8169. /*
  8170. * mono_lookup_icall_symbol:
  8171. *
  8172. * Given the icall METHOD, returns its C symbol.
  8173. */
  8174. const char*
  8175. mono_lookup_icall_symbol (MonoMethod *m)
  8176. {
  8177. if (!icall_table)
  8178. return NULL;
  8179. g_assert (icall_table->lookup_icall_symbol);
  8180. gpointer func;
  8181. func = (gpointer)mono_lookup_internal_call_full (m, FALSE, NULL, NULL);
  8182. if (!func)
  8183. return NULL;
  8184. return icall_table->lookup_icall_symbol (func);
  8185. }
  8186. #if defined(TARGET_WIN32) && defined(TARGET_X86)
  8187. /*
  8188. * Under windows, the default pinvoke calling convention is STDCALL but
  8189. * we need CDECL.
  8190. */
  8191. #define MONO_ICALL_SIGNATURE_CALL_CONVENTION MONO_CALL_C
  8192. #else
  8193. #define MONO_ICALL_SIGNATURE_CALL_CONVENTION 0
  8194. #endif
  8195. // Storage for these enums is pointer-sized as it gets replaced with MonoType*.
  8196. //
  8197. // mono_create_icall_signatures depends on this order. Handle with care.
  8198. typedef enum ICallSigType {
  8199. ICALL_SIG_TYPE_bool = 0x00,
  8200. ICALL_SIG_TYPE_boolean = ICALL_SIG_TYPE_bool,
  8201. ICALL_SIG_TYPE_double = 0x01,
  8202. ICALL_SIG_TYPE_float = 0x02,
  8203. ICALL_SIG_TYPE_int = 0x03,
  8204. ICALL_SIG_TYPE_int16 = 0x04,
  8205. ICALL_SIG_TYPE_int32 = ICALL_SIG_TYPE_int,
  8206. ICALL_SIG_TYPE_int8 = 0x05,
  8207. ICALL_SIG_TYPE_long = 0x06,
  8208. ICALL_SIG_TYPE_obj = 0x07,
  8209. ICALL_SIG_TYPE_object = ICALL_SIG_TYPE_obj,
  8210. ICALL_SIG_TYPE_ptr = 0x08,
  8211. ICALL_SIG_TYPE_ptrref = 0x09,
  8212. ICALL_SIG_TYPE_string = 0x0A,
  8213. ICALL_SIG_TYPE_uint16 = 0x0B,
  8214. ICALL_SIG_TYPE_uint32 = 0x0C,
  8215. ICALL_SIG_TYPE_uint8 = 0x0D,
  8216. ICALL_SIG_TYPE_ulong = 0x0E,
  8217. ICALL_SIG_TYPE_void = 0x0F,
  8218. ICALL_SIG_TYPE_sizet = 0x10
  8219. } ICallSigType;
  8220. #define ICALL_SIG_TYPES_1(a) ICALL_SIG_TYPE_ ## a,
  8221. #define ICALL_SIG_TYPES_2(a, b) ICALL_SIG_TYPES_1 (a ) ICALL_SIG_TYPES_1 (b)
  8222. #define ICALL_SIG_TYPES_3(a, b, c) ICALL_SIG_TYPES_2 (a, b ) ICALL_SIG_TYPES_1 (c)
  8223. #define ICALL_SIG_TYPES_4(a, b, c, d) ICALL_SIG_TYPES_3 (a, b, c ) ICALL_SIG_TYPES_1 (d)
  8224. #define ICALL_SIG_TYPES_5(a, b, c, d, e) ICALL_SIG_TYPES_4 (a, b, c, d ) ICALL_SIG_TYPES_1 (e)
  8225. #define ICALL_SIG_TYPES_6(a, b, c, d, e, f) ICALL_SIG_TYPES_5 (a, b, c, d, e) ICALL_SIG_TYPES_1 (f)
  8226. #define ICALL_SIG_TYPES_7(a, b, c, d, e, f, g) ICALL_SIG_TYPES_6 (a, b, c, d, e, f) ICALL_SIG_TYPES_1 (g)
  8227. #define ICALL_SIG_TYPES_8(a, b, c, d, e, f, g, h) ICALL_SIG_TYPES_7 (a, b, c, d, e, f, g) ICALL_SIG_TYPES_1 (h)
  8228. #define ICALL_SIG_TYPES(n, types) ICALL_SIG_TYPES_ ## n types
  8229. // A scheme to make these const would be nice.
  8230. static struct {
  8231. #define ICALL_SIG(n, xtypes) \
  8232. struct { \
  8233. MonoMethodSignature sig; \
  8234. gsize types [n]; \
  8235. } ICALL_SIG_NAME (n, xtypes);
  8236. ICALL_SIGS
  8237. MonoMethodSignature end; // terminal zeroed element
  8238. } mono_icall_signatures = {
  8239. #undef ICALL_SIG
  8240. #define ICALL_SIG(n, types) { { \
  8241. 0, /* ret */ \
  8242. n, /* param_count */ \
  8243. -1, /* sentinelpos */ \
  8244. 0, /* generic_param_count */ \
  8245. MONO_ICALL_SIGNATURE_CALL_CONVENTION, \
  8246. 0, /* hasthis */ \
  8247. 0, /* explicit_this */ \
  8248. 1, /* pinvoke */ \
  8249. 0, /* is_inflated */ \
  8250. 0, /* has_type_parameters */ \
  8251. }, /* possible gap here, depending on MONO_ZERO_LEN_ARRAY */ \
  8252. { ICALL_SIG_TYPES (n, types) } }, /* params and ret */
  8253. ICALL_SIGS
  8254. };
  8255. #undef ICALL_SIG
  8256. #define ICALL_SIG(n, types) MonoMethodSignature * const ICALL_SIG_NAME (n, types) = &mono_icall_signatures.ICALL_SIG_NAME (n, types).sig;
  8257. ICALL_SIGS
  8258. #undef ICALL_SIG
  8259. void
  8260. mono_create_icall_signatures (void)
  8261. {
  8262. // Fixup the mostly statically initialized icall signatures.
  8263. // x = m_class_get_byval_arg (x)
  8264. // Initialize ret with params [0] and params [i] with params [i + 1].
  8265. // ptrref is special
  8266. //
  8267. // FIXME This is a bit obscure.
  8268. typedef MonoMethodSignature G_MAY_ALIAS MonoMethodSignature_a;
  8269. typedef gsize G_MAY_ALIAS gsize_a;
  8270. MonoType * const lookup [ ] = {
  8271. m_class_get_byval_arg (mono_defaults.boolean_class), // ICALL_SIG_TYPE_bool
  8272. m_class_get_byval_arg (mono_defaults.double_class), // ICALL_SIG_TYPE_double
  8273. m_class_get_byval_arg (mono_defaults.single_class), // ICALL_SIG_TYPE_float
  8274. m_class_get_byval_arg (mono_defaults.int32_class), // ICALL_SIG_TYPE_int
  8275. m_class_get_byval_arg (mono_defaults.int16_class), // ICALL_SIG_TYPE_int16
  8276. m_class_get_byval_arg (mono_defaults.sbyte_class), // ICALL_SIG_TYPE_int8
  8277. m_class_get_byval_arg (mono_defaults.int64_class), // ICALL_SIG_TYPE_long
  8278. m_class_get_byval_arg (mono_defaults.object_class), // ICALL_SIG_TYPE_obj
  8279. m_class_get_byval_arg (mono_defaults.int_class), // ICALL_SIG_TYPE_ptr
  8280. mono_class_get_byref_type (mono_defaults.int_class), // ICALL_SIG_TYPE_ptrref
  8281. m_class_get_byval_arg (mono_defaults.string_class), // ICALL_SIG_TYPE_string
  8282. m_class_get_byval_arg (mono_defaults.uint16_class), // ICALL_SIG_TYPE_uint16
  8283. m_class_get_byval_arg (mono_defaults.uint32_class), // ICALL_SIG_TYPE_uint32
  8284. m_class_get_byval_arg (mono_defaults.byte_class), // ICALL_SIG_TYPE_uint8
  8285. m_class_get_byval_arg (mono_defaults.uint64_class), // ICALL_SIG_TYPE_ulong
  8286. m_class_get_byval_arg (mono_defaults.void_class), // ICALL_SIG_TYPE_void
  8287. m_class_get_byval_arg (mono_defaults.int_class), // ICALL_SIG_TYPE_sizet
  8288. };
  8289. MonoMethodSignature_a *sig = (MonoMethodSignature*)&mono_icall_signatures;
  8290. int n;
  8291. while ((n = sig->param_count)) {
  8292. --sig->param_count; // remove ret
  8293. gsize_a *types = (gsize_a*)(sig + 1);
  8294. for (int i = 0; i < n; ++i) {
  8295. gsize index = *types++;
  8296. g_assert (index < G_N_ELEMENTS (lookup));
  8297. // Casts on next line are attempt to follow strict aliasing rules,
  8298. // to ensure reading from *types precedes writing
  8299. // to params [].
  8300. *(gsize*)(i ? &sig->params [i - 1] : &sig->ret) = (gsize)lookup [index];
  8301. }
  8302. sig = (MonoMethodSignature*)types;
  8303. }
  8304. }
  8305. void
  8306. mono_register_jit_icall_info (MonoJitICallInfo *info, gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean avoid_wrapper, const char *c_symbol)
  8307. {
  8308. // Duplicate initialization is allowed and racy, assuming it is equivalent.
  8309. info->name = name;
  8310. info->func = func;
  8311. info->sig = sig;
  8312. info->c_symbol = c_symbol;
  8313. // Fill in wrapper ahead of time, to just be func, to avoid
  8314. // later initializing it to anything else. So therefore, no wrapper.
  8315. if (avoid_wrapper) {
  8316. info->wrapper = func;
  8317. } else {
  8318. // Leave it alone in case of a race.
  8319. }
  8320. }
  8321. int
  8322. ves_icall_System_GC_GetCollectionCount (int generation)
  8323. {
  8324. return mono_gc_collection_count (generation);
  8325. }
  8326. int
  8327. ves_icall_System_GC_GetGeneration (MonoObjectHandle object, MonoError *error)
  8328. {
  8329. return mono_gc_get_generation (MONO_HANDLE_RAW (object));
  8330. }
  8331. int
  8332. ves_icall_System_GC_GetMaxGeneration (void)
  8333. {
  8334. return mono_gc_max_generation ();
  8335. }
  8336. gint64
  8337. ves_icall_System_GC_GetAllocatedBytesForCurrentThread (void)
  8338. {
  8339. return mono_gc_get_allocated_bytes_for_current_thread ();
  8340. }
  8341. #ifdef ENABLE_NETCORE
  8342. guint64
  8343. ves_icall_System_GC_GetTotalAllocatedBytes (MonoBoolean precise, MonoError* error)
  8344. {
  8345. return mono_gc_get_total_allocated_bytes (precise);
  8346. }
  8347. #endif
  8348. void
  8349. ves_icall_System_GC_RecordPressure (gint64 value)
  8350. {
  8351. mono_gc_add_memory_pressure (value);
  8352. }
  8353. gint64
  8354. ves_icall_System_Diagnostics_Stopwatch_GetTimestamp (void)
  8355. {
  8356. return mono_100ns_ticks ();
  8357. }
  8358. gint64
  8359. ves_icall_System_Threading_Timer_GetTimeMonotonic (void)
  8360. {
  8361. return mono_100ns_ticks ();
  8362. }
  8363. gint64
  8364. ves_icall_System_DateTime_GetSystemTimeAsFileTime (void)
  8365. {
  8366. return mono_100ns_datetime ();
  8367. }
  8368. int
  8369. ves_icall_System_Threading_Thread_SystemMaxStackSize (void)
  8370. {
  8371. return mono_thread_info_get_system_max_stack_size ();
  8372. }
  8373. MonoBoolean
  8374. ves_icall_System_Threading_Thread_YieldInternal (void)
  8375. {
  8376. mono_threads_platform_yield ();
  8377. return TRUE;
  8378. }
  8379. gint32
  8380. ves_icall_System_Environment_get_ProcessorCount (void)
  8381. {
  8382. return mono_cpu_count ();
  8383. }
  8384. #if !defined(ENABLE_NETCORE)
  8385. #if defined(ENABLE_MONODROID)
  8386. G_EXTERN_C gpointer CreateNLSocket (void);
  8387. G_EXTERN_C gint32 ReadEvents (gpointer sock, gpointer buffer, gint32 count, gint32 size);
  8388. G_EXTERN_C gpointer CloseNLSocket (gpointer sock);
  8389. gpointer
  8390. ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_CreateNLSocket (void)
  8391. {
  8392. return CreateNLSocket ();
  8393. }
  8394. gint32
  8395. ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_ReadEvents (gpointer sock, gpointer buffer, gint32 count, gint32 size)
  8396. {
  8397. return ReadEvents (sock, buffer, count, size);
  8398. }
  8399. gpointer
  8400. ves_icall_System_Net_NetworkInformation_LinuxNetworkChange_CloseNLSocket (gpointer sock)
  8401. {
  8402. return CloseNLSocket (sock);
  8403. }
  8404. #endif
  8405. #endif
  8406. // Generate wrappers.
  8407. #define ICALL_TYPE(id,name,first) /* nothing */
  8408. #define ICALL(id,name,func) /* nothing */
  8409. #define NOHANDLES(inner) /* nothing */
  8410. #define MONO_HANDLE_REGISTER_ICALL(func, ret, nargs, argtypes) MONO_HANDLE_REGISTER_ICALL_IMPLEMENT (func, ret, nargs, argtypes)
  8411. // Some native functions are exposed via multiple managed names.
  8412. // Producing a wrapper for these results in duplicate wrappers with the same names,
  8413. // which fails to compile. Do not produce such duplicate wrappers. Alternatively,
  8414. // a one line native function with a different name that calls the main one could be used.
  8415. // i.e. the wrapper would also have a different name.
  8416. #define HANDLES_REUSE_WRAPPER(...) /* nothing */
  8417. #define HANDLES(id, name, func, ret, nargs, argtypes) \
  8418. MONO_HANDLE_DECLARE (id, name, func, ret, nargs, argtypes); \
  8419. MONO_HANDLE_IMPLEMENT (id, name, func, ret, nargs, argtypes)
  8420. #include "metadata/icall-def.h"
  8421. #undef HANDLES
  8422. #undef HANDLES_REUSE_WRAPPER
  8423. #undef ICALL_TYPE
  8424. #undef ICALL
  8425. #undef NOHANDLES
  8426. #undef MONO_HANDLE_REGISTER_ICALL
  8427. MonoObjectHandle
  8428. ves_icall_System_Threading_OSSpecificSynchronizationContext_GetOSContext ()
  8429. {
  8430. return NULL_HANDLE;
  8431. }
  8432. void
  8433. ves_icall_System_Threading_OSSpecificSynchronizationContext_PostInternal (gpointer callback, gpointer arg)
  8434. {
  8435. /* This isn't actually reachable since ves_icall_System_Threading_OSSpecificSynchronizationContext_GetOSContext always returns NULL */
  8436. mono_set_pending_exception (mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", "System.Threading.InteropServices.OSSpecificSynchronizationContext.PostInternal internal call is not implemented."));
  8437. }