mmaptest.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. mmaptest.c
  9. Abstract:
  10. This module implements the tests used to verify that memory map operations
  11. are working.
  12. Author:
  13. Chris Stevens 10-Mar-2014
  14. Environment:
  15. User Mode
  16. --*/
  17. //
  18. // ------------------------------------------------------------------- Includes
  19. //
  20. #include <minoca/lib/types.h>
  21. #include <assert.h>
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <getopt.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/stat.h>
  29. #include <sys/wait.h>
  30. #include <sys/time.h>
  31. #include <time.h>
  32. #include <unistd.h>
  33. #include <signal.h>
  34. #include <sys/mman.h>
  35. //
  36. // --------------------------------------------------------------------- Macros
  37. //
  38. #define DEBUG_PRINT(...) \
  39. if (MemoryMapTestVerbosity >= TestVerbosityDebug) { \
  40. printf(__VA_ARGS__); \
  41. }
  42. #define PRINT(...) \
  43. if (MemoryMapTestVerbosity >= TestVerbosityNormal) { \
  44. printf(__VA_ARGS__); \
  45. }
  46. #define PRINT_ERROR(...) fprintf(stderr, "\nmmaptest: " __VA_ARGS__)
  47. //
  48. // ---------------------------------------------------------------- Definitions
  49. //
  50. #define MEMORY_MAP_TEST_VERSION_MAJOR 1
  51. #define MEMORY_MAP_TEST_VERSION_MINOR 0
  52. #define MEMORY_MAP_TEST_USAGE \
  53. "Usage: mmaptest [options] \n" \
  54. "This utility test memory map functionality. Options are:\n" \
  55. " -c, --file-count <count> -- Set the number of files to create.\n" \
  56. " -s, --file-size <size> -- Set the size of each file in bytes.\n" \
  57. " -i, --iterations <count> -- Set the number of operations to perform.\n" \
  58. " -p, --threads <count> -- Set the number of threads to spin up.\n" \
  59. " -t, --test -- Set the test to perform. Valid values are all, \n" \
  60. " basic, private, shared, shmprivate, and shmshared.\n" \
  61. " --debug -- Print lots of information about what's happening.\n" \
  62. " --quiet -- Print only errors.\n" \
  63. " --no-cleanup -- Leave test files around for debugging.\n" \
  64. " --help -- Print this help text and exit.\n" \
  65. " --version -- Print the test version and exit.\n" \
  66. #define MEMORY_MAP_TEST_OPTIONS_STRING "c:s:i:t:p:ndqhV"
  67. #define MEMORY_MAP_TEST_CREATE_PERMISSIONS (S_IRUSR | S_IWUSR)
  68. #define DEFAULT_FILE_COUNT 20
  69. #define DEFAULT_FILE_SIZE (1024 * 17)
  70. #define DEFAULT_OPERATION_COUNT (DEFAULT_FILE_COUNT * 50)
  71. #define DEFAULT_THREAD_COUNT 1
  72. //
  73. // ------------------------------------------------------ Data Type Definitions
  74. //
  75. typedef enum _MEMORY_MAP_TEST_ACTION {
  76. MemoryMapTestActionMap,
  77. MemoryMapTestActionUnmap,
  78. MemoryMapTestActionMappedWrite,
  79. MemoryMapTestActionMappedRead,
  80. MemoryMapTestActionSync,
  81. MemoryMapTestActionFileWrite,
  82. MemoryMapTestActionFileRead,
  83. MemoryMapTestActionCount
  84. } MEMORY_MAP_TEST_ACTION, *PMEMORY_MAP_TEST_ACTION;
  85. typedef enum _TEST_VERBOSITY {
  86. TestVerbosityQuiet,
  87. TestVerbosityNormal,
  88. TestVerbosityDebug
  89. } TEST_VERBOSITY, *PTEST_VERBOSITY;
  90. typedef enum _MEMORY_MAP_TEST_TYPE {
  91. MemoryMapTestAll,
  92. MemoryMapTestBasic,
  93. MemoryMapTestPrivate,
  94. MemoryMapTestShared,
  95. MemoryMapTestShmPrivate,
  96. MemoryMapTestShmShared
  97. } MEMORY_MAP_TEST_TYPE, *PMEMORY_MAP_TEST_TYPE;
  98. typedef
  99. ULONG
  100. (*PMEMORY_MAP_BASIC_TEST_ROUTINE) (
  101. INT FileSize
  102. );
  103. /*++
  104. Routine Description:
  105. This routine prototype represents a function that gets called to perform
  106. a basic memory map test.
  107. Arguments:
  108. FileSize - Supplies the size of the file to create, if needed.
  109. Return Value:
  110. Returns the number of failures encountered during the test.
  111. --*/
  112. //
  113. // ----------------------------------------------- Internal Function Prototypes
  114. //
  115. ULONG
  116. MemoryMapTestCreateFiles (
  117. MEMORY_MAP_TEST_TYPE Test,
  118. INT FileCount
  119. );
  120. VOID
  121. MemoryMapTestDestroyFiles (
  122. MEMORY_MAP_TEST_TYPE Test,
  123. INT FileCount
  124. );
  125. ULONG
  126. RunMemoryMapBasicTests (
  127. INT FileCount,
  128. INT FileSize,
  129. INT Iterations
  130. );
  131. ULONG
  132. RunMemoryMapPrivateTest (
  133. INT FileCount,
  134. INT FileSize,
  135. INT Iterations
  136. );
  137. ULONG
  138. RunMemoryMapSharedTest (
  139. INT FileCount,
  140. INT FileSize,
  141. INT Iterations
  142. );
  143. ULONG
  144. RunMemoryMapShmPrivateTest (
  145. INT FileCount,
  146. INT FileSize,
  147. INT Iterations
  148. );
  149. ULONG
  150. RunMemoryMapShmSharedTest (
  151. INT FileCount,
  152. INT FileSize,
  153. INT Iterations
  154. );
  155. static
  156. VOID
  157. MemoryMapTestExpectedSignalHandler (
  158. INT SignalNumber,
  159. siginfo_t *SignalInformation,
  160. PVOID Context
  161. );
  162. static
  163. VOID
  164. MemoryMapTestUnexpectedSignalHandler (
  165. INT SignalNumber,
  166. siginfo_t *SignalInformation,
  167. PVOID Context
  168. );
  169. ULONG
  170. PrintTestTime (
  171. struct timeval *StartTime
  172. );
  173. ULONG
  174. MemoryMapEmptyTest (
  175. INT FileSize
  176. );
  177. ULONG
  178. MemoryMapTruncateTest (
  179. INT FileSize
  180. );
  181. ULONG
  182. MemoryMapReadOnlyTest (
  183. INT FileSize
  184. );
  185. ULONG
  186. MemoryMapNoAccessTest (
  187. INT FileSize
  188. );
  189. ULONG
  190. MemoryMapAnonymousTest (
  191. INT FileSize
  192. );
  193. ULONG
  194. MemoryMapSharedAnonymousTest (
  195. INT FileSize
  196. );
  197. //
  198. // -------------------------------------------------------------------- Globals
  199. //
  200. //
  201. // Higher levels here print out more stuff.
  202. //
  203. TEST_VERBOSITY MemoryMapTestVerbosity = TestVerbosityNormal;
  204. //
  205. // Set this boolean to skip cleaning up files.
  206. //
  207. BOOL MemoryMapTestNoCleanup = FALSE;
  208. struct option MemoryMapTestLongOptions[] = {
  209. {"file-count", required_argument, 0, 'c'},
  210. {"file-size", required_argument, 0, 's'},
  211. {"iterations", required_argument, 0, 'i'},
  212. {"threads", required_argument, 0, 'p'},
  213. {"test", required_argument, 0, 't'},
  214. {"no-cleanup", no_argument, 0, 'n'},
  215. {"debug", no_argument, 0, 'd'},
  216. {"quiet", no_argument, 0, 'q'},
  217. {"help", no_argument, 0, 'h'},
  218. {"version", no_argument, 0, 'V'},
  219. {NULL, 0, 0, 0},
  220. };
  221. //
  222. // Define an array of basic memory map tests.
  223. //
  224. PMEMORY_MAP_BASIC_TEST_ROUTINE MemoryMapBasicTests[] = {
  225. MemoryMapEmptyTest,
  226. MemoryMapTruncateTest,
  227. MemoryMapReadOnlyTest,
  228. MemoryMapNoAccessTest,
  229. MemoryMapAnonymousTest,
  230. MemoryMapSharedAnonymousTest
  231. };
  232. //
  233. // ------------------------------------------------------------------ Functions
  234. //
  235. int
  236. main (
  237. int ArgumentCount,
  238. char **Arguments
  239. )
  240. /*++
  241. Routine Description:
  242. This routine implements the memory map test program.
  243. Arguments:
  244. ArgumentCount - Supplies the number of elements in the arguments array.
  245. Arguments - Supplies an array of strings. The array count is bounded by the
  246. previous parameter, and the strings are null-terminated.
  247. Return Value:
  248. 0 on success.
  249. Non-zero on failure.
  250. --*/
  251. {
  252. PSTR AfterScan;
  253. pid_t Child;
  254. INT ChildIndex;
  255. pid_t *Children;
  256. BOOL DestroyFiles;
  257. INT Failures;
  258. INT FileCount;
  259. INT FileSize;
  260. BOOL IsParent;
  261. INT Iterations;
  262. INT Option;
  263. INT Status;
  264. MEMORY_MAP_TEST_TYPE Test;
  265. INT Threads;
  266. Children = NULL;
  267. DestroyFiles = FALSE;
  268. Failures = 0;
  269. FileCount = DEFAULT_FILE_COUNT;
  270. FileSize = DEFAULT_FILE_SIZE;
  271. Iterations = DEFAULT_OPERATION_COUNT;
  272. Test = MemoryMapTestAll;
  273. Threads = DEFAULT_THREAD_COUNT;
  274. Status = 0;
  275. setvbuf(stdout, NULL, _IONBF, 0);
  276. setvbuf(stderr, NULL, _IONBF, 0);
  277. srand(time(NULL));
  278. //
  279. // Process the control arguments.
  280. //
  281. while (TRUE) {
  282. Option = getopt_long(ArgumentCount,
  283. Arguments,
  284. MEMORY_MAP_TEST_OPTIONS_STRING,
  285. MemoryMapTestLongOptions,
  286. NULL);
  287. if (Option == -1) {
  288. break;
  289. }
  290. if ((Option == '?') || (Option == ':')) {
  291. Status = 1;
  292. goto MainEnd;
  293. }
  294. switch (Option) {
  295. case 'c':
  296. FileCount = strtol(optarg, &AfterScan, 0);
  297. if ((FileCount <= 0) || (AfterScan == optarg)) {
  298. PRINT_ERROR("Invalid file count %s.\n", optarg);
  299. Status = 1;
  300. goto MainEnd;
  301. }
  302. break;
  303. case 's':
  304. FileSize = strtol(optarg, &AfterScan, 0);
  305. if ((FileSize < 0) || (AfterScan == optarg)) {
  306. PRINT_ERROR("Invalid file size %s.\n", optarg);
  307. Status = 1;
  308. goto MainEnd;
  309. }
  310. break;
  311. case 'i':
  312. Iterations = strtol(optarg, &AfterScan, 0);
  313. if ((Iterations < 0) || (AfterScan == optarg)) {
  314. PRINT_ERROR("Invalid iteration count %s.\n", optarg);
  315. Status = 1;
  316. goto MainEnd;
  317. }
  318. break;
  319. case 'n':
  320. MemoryMapTestNoCleanup = TRUE;
  321. break;
  322. case 'p':
  323. Threads = strtol(optarg, &AfterScan, 0);
  324. if ((Threads <= 0) || (AfterScan == optarg)) {
  325. PRINT_ERROR("Invalid thread count %s.\n", optarg);
  326. Status = 1;
  327. goto MainEnd;
  328. }
  329. break;
  330. case 't':
  331. if (strcasecmp(optarg, "all") == 0) {
  332. Test = MemoryMapTestAll;
  333. } else if (strcasecmp(optarg, "basic") == 0) {
  334. Test = MemoryMapTestBasic;
  335. } else if (strcasecmp(optarg, "private") == 0) {
  336. Test = MemoryMapTestPrivate;
  337. } else if (strcasecmp(optarg, "shared") == 0) {
  338. Test = MemoryMapTestShared;
  339. } else if (strcasecmp(optarg, "shmprivate") == 0) {
  340. Test = MemoryMapTestShmPrivate;
  341. } else if (strcasecmp(optarg, "shmshared") == 0) {
  342. Test = MemoryMapTestShmShared;
  343. } else {
  344. PRINT_ERROR("Invalid test: %s.\n", optarg);
  345. Status = 1;
  346. goto MainEnd;
  347. }
  348. break;
  349. case 'd':
  350. MemoryMapTestVerbosity = TestVerbosityDebug;
  351. break;
  352. case 'q':
  353. MemoryMapTestVerbosity = TestVerbosityQuiet;
  354. break;
  355. case 'V':
  356. printf("Minoca mmaptest version %d.%d\n",
  357. MEMORY_MAP_TEST_VERSION_MAJOR,
  358. MEMORY_MAP_TEST_VERSION_MINOR);
  359. return 1;
  360. case 'h':
  361. printf(MEMORY_MAP_TEST_USAGE);
  362. return 1;
  363. default:
  364. assert(FALSE);
  365. Status = 1;
  366. goto MainEnd;
  367. }
  368. }
  369. //
  370. // Create any files that are necessary for the test.
  371. //
  372. DestroyFiles = TRUE;
  373. Failures += MemoryMapTestCreateFiles(Test, FileCount);
  374. if (Failures != 0) {
  375. goto MainEnd;
  376. }
  377. IsParent = TRUE;
  378. if (Threads > 1) {
  379. Children = malloc(sizeof(pid_t) * (Threads - 1));
  380. if (Children == NULL) {
  381. Status = ENOMEM;
  382. goto MainEnd;
  383. }
  384. memset(Children, 0, sizeof(pid_t) * (Threads - 1));
  385. for (ChildIndex = 0; ChildIndex < Threads - 1; ChildIndex += 1) {
  386. Child = fork();
  387. //
  388. // If this is the child, break out and run the tests.
  389. //
  390. if (Child == 0) {
  391. srand(time(NULL) + ChildIndex);
  392. IsParent = FALSE;
  393. break;
  394. }
  395. Children[ChildIndex] = Child;
  396. }
  397. }
  398. //
  399. // Run the tests.
  400. //
  401. if ((Test == MemoryMapTestAll) || (Test == MemoryMapTestBasic)) {
  402. Failures += RunMemoryMapBasicTests(FileCount, FileSize, Iterations);
  403. }
  404. if ((Test == MemoryMapTestAll) || (Test == MemoryMapTestPrivate)) {
  405. Failures += RunMemoryMapPrivateTest(FileCount, FileSize, Iterations);
  406. }
  407. if ((Test == MemoryMapTestAll) || (Test == MemoryMapTestShared)) {
  408. Failures += RunMemoryMapSharedTest(FileCount, FileSize, Iterations);
  409. }
  410. if ((Test == MemoryMapTestAll) || (Test == MemoryMapTestShmPrivate)) {
  411. Failures += RunMemoryMapShmPrivateTest(FileCount, FileSize, Iterations);
  412. }
  413. if ((Test == MemoryMapTestAll) || (Test == MemoryMapTestShmShared)) {
  414. Failures += RunMemoryMapShmSharedTest(FileCount, FileSize, Iterations);
  415. }
  416. //
  417. // Wait for any children.
  418. //
  419. if (IsParent != FALSE) {
  420. if (Threads > 1) {
  421. for (ChildIndex = 0; ChildIndex < Threads - 1; ChildIndex += 1) {
  422. Child = waitpid(Children[ChildIndex], &Status, 0);
  423. if (Child == -1) {
  424. PRINT_ERROR("Failed to wait for child %d: %s.\n",
  425. Children[ChildIndex],
  426. strerror(errno));
  427. Status = errno;
  428. } else {
  429. assert(Child == Children[ChildIndex]);
  430. if (!WIFEXITED(Status)) {
  431. PRINT_ERROR("Child %d returned with status %x\n",
  432. Status);
  433. Failures += 1;
  434. }
  435. Failures += WEXITSTATUS(Status);
  436. Status = 0;
  437. }
  438. }
  439. }
  440. //
  441. // If this is a child, just report back the number of failures to the
  442. // parent.
  443. //
  444. } else {
  445. if (Failures > 100) {
  446. exit(100);
  447. } else {
  448. exit(Failures);
  449. }
  450. }
  451. MainEnd:
  452. //
  453. // Destroy any files that were needed for the test, if necessary.
  454. //
  455. if ((MemoryMapTestNoCleanup == FALSE) && (DestroyFiles != FALSE)) {
  456. MemoryMapTestDestroyFiles(Test, FileCount);
  457. }
  458. if (Children != NULL) {
  459. free(Children);
  460. }
  461. if (Status != 0) {
  462. PRINT_ERROR("Error: %d.\n", Status);
  463. }
  464. if (Failures != 0) {
  465. PRINT_ERROR("\n *** %d failures in mmaptest ***\n", Failures);
  466. return Failures;
  467. }
  468. return 0;
  469. }
  470. //
  471. // --------------------------------------------------------- Internal Functions
  472. ULONG
  473. MemoryMapTestCreateFiles (
  474. MEMORY_MAP_TEST_TYPE Test,
  475. INT FileCount
  476. )
  477. /*++
  478. Routine Description:
  479. This routine creates any files that are necessary for the given test.
  480. Arguments:
  481. Test - Supplies the type of test being run.
  482. FileCount - Supplies the number of files to be used during the test.
  483. Return Value:
  484. Returns the number of failures encountered when creating files.
  485. --*/
  486. {
  487. ULONG Failures;
  488. INT File;
  489. INT FileIndex;
  490. CHAR FileName[16];
  491. INT OpenFlags;
  492. Failures = 0;
  493. if ((Test == MemoryMapTestAll) || (Test == MemoryMapTestPrivate)) {
  494. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  495. snprintf(FileName,
  496. sizeof(FileName),
  497. "mmpt-%06x",
  498. FileIndex);
  499. OpenFlags = O_RDWR | O_CREAT;
  500. File = open(FileName,
  501. OpenFlags,
  502. MEMORY_MAP_TEST_CREATE_PERMISSIONS);
  503. if (File < 0) {
  504. PRINT_ERROR("Failed to open file %s (flags %x): %s.\n",
  505. FileName,
  506. OpenFlags,
  507. strerror(errno));
  508. Failures += 1;
  509. goto MemoryMapTestCreateFilesEnd;
  510. }
  511. if (close(File) != 0) {
  512. Failures += 1;
  513. goto MemoryMapTestCreateFilesEnd;
  514. }
  515. }
  516. }
  517. if ((Test == MemoryMapTestAll) || (Test == MemoryMapTestShared)) {
  518. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  519. snprintf(FileName,
  520. sizeof(FileName),
  521. "mmst-%06x",
  522. FileIndex);
  523. OpenFlags = O_RDWR | O_CREAT;
  524. File = open(FileName,
  525. OpenFlags,
  526. MEMORY_MAP_TEST_CREATE_PERMISSIONS);
  527. if (File < 0) {
  528. PRINT_ERROR("Failed to open file %s (flags %x): %s.\n",
  529. FileName,
  530. OpenFlags,
  531. strerror(errno));
  532. Failures += 1;
  533. goto MemoryMapTestCreateFilesEnd;
  534. }
  535. if (close(File) != 0) {
  536. Failures += 1;
  537. goto MemoryMapTestCreateFilesEnd;
  538. }
  539. }
  540. }
  541. if ((Test == MemoryMapTestAll) || (Test == MemoryMapTestShmPrivate)) {
  542. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  543. snprintf(FileName,
  544. sizeof(FileName),
  545. "shmpt-%06x",
  546. FileIndex);
  547. OpenFlags = O_RDWR | O_CREAT;
  548. File = shm_open(FileName,
  549. OpenFlags,
  550. MEMORY_MAP_TEST_CREATE_PERMISSIONS);
  551. if (File < 0) {
  552. PRINT_ERROR("Failed to open shared memory object %s "
  553. "(flags %x): %s.\n",
  554. FileName,
  555. OpenFlags,
  556. strerror(errno));
  557. Failures += 1;
  558. goto MemoryMapTestCreateFilesEnd;
  559. }
  560. if (close(File) != 0) {
  561. Failures += 1;
  562. goto MemoryMapTestCreateFilesEnd;
  563. }
  564. }
  565. }
  566. if ((Test == MemoryMapTestAll) || (Test == MemoryMapTestShmShared)) {
  567. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  568. snprintf(FileName,
  569. sizeof(FileName),
  570. "shmst-%06x",
  571. FileIndex);
  572. OpenFlags = O_RDWR | O_CREAT;
  573. File = shm_open(FileName,
  574. OpenFlags,
  575. MEMORY_MAP_TEST_CREATE_PERMISSIONS);
  576. if (File < 0) {
  577. PRINT_ERROR("Failed to open shared memory object %s "
  578. "(flags %x): %s.\n",
  579. FileName,
  580. OpenFlags,
  581. strerror(errno));
  582. Failures += 1;
  583. goto MemoryMapTestCreateFilesEnd;
  584. }
  585. if (close(File) != 0) {
  586. Failures += 1;
  587. goto MemoryMapTestCreateFilesEnd;
  588. }
  589. }
  590. }
  591. MemoryMapTestCreateFilesEnd:
  592. return Failures;
  593. }
  594. VOID
  595. MemoryMapTestDestroyFiles (
  596. MEMORY_MAP_TEST_TYPE Test,
  597. INT FileCount
  598. )
  599. /*++
  600. Routine Description:
  601. This routine destroys any files that were necessary for the given test.
  602. Arguments:
  603. Test - Supplies the type of test being run.
  604. FileCount - Supplies the number of files used during the test.
  605. Return Value:
  606. None.
  607. --*/
  608. {
  609. INT FileIndex;
  610. CHAR FileName[16];
  611. INT Result;
  612. if ((Test == MemoryMapTestAll) || (Test == MemoryMapTestPrivate)) {
  613. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  614. snprintf(FileName,
  615. sizeof(FileName),
  616. "mmpt-%06x",
  617. FileIndex);
  618. Result = unlink(FileName);
  619. if ((Result != 0) && (errno != ENOENT)) {
  620. PRINT_ERROR("Failed to unlink %s: %s.\n",
  621. FileName,
  622. strerror(errno));
  623. }
  624. }
  625. }
  626. if ((Test == MemoryMapTestAll) || (Test == MemoryMapTestShared)) {
  627. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  628. snprintf(FileName,
  629. sizeof(FileName),
  630. "mmst-%06x",
  631. FileIndex);
  632. Result = unlink(FileName);
  633. if ((Result != 0) && (errno != ENOENT)) {
  634. PRINT_ERROR("Failed to unlink %s: %s.\n",
  635. FileName,
  636. strerror(errno));
  637. }
  638. }
  639. }
  640. if ((Test == MemoryMapTestAll) || (Test == MemoryMapTestShmPrivate)) {
  641. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  642. snprintf(FileName,
  643. sizeof(FileName),
  644. "shmpt-%06x",
  645. FileIndex);
  646. Result = shm_unlink(FileName);
  647. if ((Result != 0) && (errno != ENOENT)) {
  648. PRINT_ERROR("Failed to unlink %s: %s.\n",
  649. FileName,
  650. strerror(errno));
  651. }
  652. }
  653. }
  654. if ((Test == MemoryMapTestAll) || (Test == MemoryMapTestShmShared)) {
  655. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  656. snprintf(FileName,
  657. sizeof(FileName),
  658. "shmst-%06x",
  659. FileIndex);
  660. Result = shm_unlink(FileName);
  661. if ((Result != 0) && (errno != ENOENT)) {
  662. PRINT_ERROR("Failed to unlink %s: %s.\n",
  663. FileName,
  664. strerror(errno));
  665. }
  666. }
  667. }
  668. return;
  669. }
  670. ULONG
  671. RunMemoryMapBasicTests (
  672. INT FileCount,
  673. INT FileSize,
  674. INT Iterations
  675. )
  676. /*++
  677. Routine Description:
  678. This routine executes the memory map basic test.
  679. Arguments:
  680. FileCount - Supplies the number of files to work with.
  681. FileSize - Supplies the size of each file.
  682. Iterations - Supplies the number of iterations to perform.
  683. Return Value:
  684. Returns the number of failures in the test suite.
  685. --*/
  686. {
  687. ULONG Failures;
  688. INT Iteration;
  689. INT Percent;
  690. pid_t Process;
  691. INT Test;
  692. INT TestCount;
  693. PMEMORY_MAP_BASIC_TEST_ROUTINE TestRoutine;
  694. Failures = 0;
  695. //
  696. // Announce the test.
  697. //
  698. Process = getpid();
  699. PRINT("Process %d Running memory map basic tests with %d iterations.\n",
  700. Process,
  701. Iterations);
  702. Percent = Iterations / 100;
  703. if (Percent == 0) {
  704. Percent = 1;
  705. }
  706. //
  707. // Determine how many basic tests there are.
  708. //
  709. TestCount = sizeof(MemoryMapBasicTests) / sizeof(MemoryMapBasicTests[0]);
  710. //
  711. // For each iteration, pick a random test and execute it.
  712. //
  713. for (Iteration = 0; Iteration < Iterations; Iteration += 1) {
  714. Test = rand() % TestCount;
  715. TestRoutine = MemoryMapBasicTests[Test];
  716. Failures += TestRoutine(FileSize);
  717. if ((Iteration % Percent) == 0) {
  718. PRINT("b");
  719. }
  720. }
  721. PRINT("\n");
  722. return Failures;
  723. }
  724. ULONG
  725. RunMemoryMapPrivateTest (
  726. INT FileCount,
  727. INT FileSize,
  728. INT Iterations
  729. )
  730. /*++
  731. Routine Description:
  732. This routine executes the memory map private consistency test.
  733. Arguments:
  734. FileCount - Supplies the number of files to work with.
  735. FileSize - Supplies the size of each file.
  736. Iterations - Supplies the number of iterations to perform.
  737. Return Value:
  738. Returns the number of failures in the test suite.
  739. --*/
  740. {
  741. MEMORY_MAP_TEST_ACTION Action;
  742. unsigned ActionSeed;
  743. size_t BytesComplete;
  744. INT ExpectedValue;
  745. ULONG Failures;
  746. INT File;
  747. PINT FileBuffer;
  748. INT FileIndex;
  749. CHAR FileName[16];
  750. PINT FileOffset;
  751. INT FillIndex;
  752. INT Iteration;
  753. PINT *MapBuffer;
  754. INT MaxSimultaneousFiles;
  755. INT OpenFlags;
  756. INT Percent;
  757. pid_t Process;
  758. INT Result;
  759. struct sigaction SignalAction;
  760. INT SimultaneousFiles;
  761. struct timeval StartTime;
  762. size_t TotalBytesComplete;
  763. Failures = 0;
  764. FileBuffer = NULL;
  765. FileOffset = NULL;
  766. MapBuffer = NULL;
  767. //
  768. // Set up the signal handler.
  769. //
  770. SignalAction.sa_sigaction = MemoryMapTestUnexpectedSignalHandler;
  771. sigemptyset(&(SignalAction.sa_mask));
  772. SignalAction.sa_flags = SA_SIGINFO;
  773. sigaction(SIGSEGV, &SignalAction, NULL);
  774. sigaction(SIGBUS, &SignalAction, NULL);
  775. //
  776. // Record the test start time.
  777. //
  778. Result = gettimeofday(&StartTime, NULL);
  779. if (Result != 0) {
  780. PRINT_ERROR("Failed to get time of day: %s.\n", strerror(errno));
  781. Failures += 1;
  782. goto RunMemoryMapPrivateTestEnd;
  783. }
  784. //
  785. // Announce the test.
  786. //
  787. Process = getpid();
  788. PRINT("Process %d Running memory map private test with %d files of %d "
  789. "bytes each. %d iterations.\n",
  790. Process,
  791. FileCount,
  792. FileSize,
  793. Iterations);
  794. Percent = Iterations / 100;
  795. if (Percent == 0) {
  796. Percent = 1;
  797. }
  798. MaxSimultaneousFiles = 0;
  799. SimultaneousFiles = 0;
  800. FileBuffer = malloc(FileSize);
  801. if (FileBuffer == NULL) {
  802. Failures += 1;
  803. goto RunMemoryMapPrivateTestEnd;
  804. }
  805. FileOffset = malloc(FileCount * sizeof(INT));
  806. if (FileOffset == NULL) {
  807. Failures += 1;
  808. goto RunMemoryMapPrivateTestEnd;
  809. }
  810. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  811. FileOffset[FileIndex] = -1;
  812. }
  813. MapBuffer = malloc(FileCount * sizeof(PINT));
  814. if (MapBuffer == NULL) {
  815. Failures += 1;
  816. goto RunMemoryMapPrivateTestEnd;
  817. }
  818. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  819. MapBuffer[FileIndex] = MAP_FAILED;
  820. }
  821. FileSize = ALIGN_RANGE_UP(FileSize, sizeof(INT));
  822. //
  823. // Get a separate seed for the random actions.
  824. //
  825. ActionSeed = time(NULL);
  826. //
  827. // Perform the memory map operations. This test writes an entire private
  828. // memory mapped file with incremental values and then tests that any file
  829. // reads return the same values.
  830. //
  831. for (Iteration = 0; Iteration < Iterations; Iteration += 1) {
  832. //
  833. // Pick a random file and a random action.
  834. //
  835. FileIndex = rand() % FileCount;
  836. snprintf(FileName,
  837. sizeof(FileName),
  838. "mmpt-%06x",
  839. FileIndex);
  840. Action = rand_r(&ActionSeed) % MemoryMapTestActionCount;
  841. //
  842. // If the file has yet to be created, then the action must be map.
  843. //
  844. if (MapBuffer[FileIndex] == MAP_FAILED) {
  845. Action = MemoryMapTestActionMap;
  846. //
  847. // Otherwise if the file is set to be mapped again, go unmap it first.
  848. //
  849. } else if (Action == MemoryMapTestActionMap) {
  850. Action = MemoryMapTestActionUnmap;
  851. }
  852. switch (Action) {
  853. case MemoryMapTestActionMap:
  854. assert(MapBuffer[FileIndex] == MAP_FAILED);
  855. SimultaneousFiles += 1;
  856. if (SimultaneousFiles > MaxSimultaneousFiles) {
  857. MaxSimultaneousFiles = SimultaneousFiles;
  858. }
  859. //
  860. // Open the file read/write.
  861. //
  862. OpenFlags = O_RDWR;
  863. File = open(FileName, OpenFlags);
  864. if (File < 0) {
  865. PRINT_ERROR("Failed to open file %s (flags %x): %s.\n",
  866. FileName,
  867. OpenFlags,
  868. strerror(errno));
  869. Failures += 1;
  870. continue;
  871. }
  872. //
  873. // Use ftruncate to make the file bigger so that access to the
  874. // mmapped region succeeds.
  875. //
  876. Result = ftruncate(File, FileSize);
  877. if (Result != 0) {
  878. PRINT_ERROR("Failed to ftruncate file %s: %s.\n",
  879. FileName,
  880. strerror(errno));
  881. Failures += 1;
  882. }
  883. //
  884. // Mmap the file privately with read/write permissions.
  885. //
  886. MapBuffer[FileIndex] = mmap(0,
  887. FileSize,
  888. PROT_READ | PROT_WRITE,
  889. MAP_PRIVATE,
  890. File,
  891. 0);
  892. //
  893. // Close the file.
  894. //
  895. if (close(File) != 0) {
  896. PRINT_ERROR("Failed to close: %s.\n", strerror(errno));
  897. Failures += 1;
  898. }
  899. //
  900. // Increment the failure count if the mapping failed.
  901. //
  902. if (MapBuffer[FileIndex] == MAP_FAILED) {
  903. PRINT_ERROR("Failed to map file %s rw + private: %s.\n",
  904. FileName,
  905. strerror(errno));
  906. Failures += 1;
  907. continue;
  908. }
  909. break;
  910. case MemoryMapTestActionUnmap:
  911. assert(MapBuffer[FileIndex] != MAP_FAILED);
  912. //
  913. // Unmap the file.
  914. //
  915. Result = munmap(MapBuffer[FileIndex], FileSize);
  916. if (Result != 0) {
  917. PRINT_ERROR("Failed to unmap %s.\n", strerror(errno));
  918. Failures += 1;
  919. continue;
  920. }
  921. MapBuffer[FileIndex] = MAP_FAILED;
  922. FileOffset[FileIndex] = -1;
  923. SimultaneousFiles -= 1;
  924. break;
  925. case MemoryMapTestActionMappedWrite:
  926. assert(MapBuffer[FileIndex] != MAP_FAILED);
  927. FileOffset[FileIndex] = rand();
  928. DEBUG_PRINT("Writing file %s, Value %x.\n",
  929. FileName,
  930. FileOffset[FileIndex]);
  931. for (FillIndex = 0;
  932. FillIndex < FileSize / sizeof(INT);
  933. FillIndex += 1) {
  934. MapBuffer[FileIndex][FillIndex] = FileOffset[FileIndex] +
  935. FillIndex;
  936. }
  937. break;
  938. case MemoryMapTestActionMappedRead:
  939. assert(MapBuffer[FileIndex] != MAP_FAILED);
  940. DEBUG_PRINT("Reading file %s, Value should be %x.\n",
  941. FileName,
  942. FileOffset[FileIndex]);
  943. //
  944. // If the mapped section has been written to, then the buffer
  945. // should always read the incrementing random values. Otherwise the
  946. // file should be all zero's.
  947. //
  948. for (FillIndex = 0;
  949. FillIndex < FileSize / sizeof(INT);
  950. FillIndex += 1) {
  951. if (FileOffset[FileIndex] == -1) {
  952. ExpectedValue = 0;
  953. } else {
  954. ExpectedValue = FileOffset[FileIndex] + FillIndex;
  955. }
  956. if (MapBuffer[FileIndex][FillIndex] != ExpectedValue) {
  957. PRINT_ERROR("Mapped read file %s index %x came back %x, "
  958. "should have been %x.\n",
  959. FileName,
  960. FillIndex,
  961. MapBuffer[FileIndex][FillIndex],
  962. ExpectedValue);
  963. Failures += 1;
  964. }
  965. }
  966. break;
  967. case MemoryMapTestActionSync:
  968. assert(MapBuffer[FileIndex] != MAP_FAILED);
  969. DEBUG_PRINT("Syncing file %s, Value should be %x.\n",
  970. FileName,
  971. FileOffset[FileIndex]);
  972. Result = msync(MapBuffer[FileIndex], FileSize, MS_SYNC);
  973. if (Result != 0) {
  974. PRINT_ERROR("Failed to msync file %s: %s.\n",
  975. FileName,
  976. strerror(errno));
  977. Failures += 1;
  978. continue;
  979. }
  980. break;
  981. case MemoryMapTestActionFileWrite:
  982. //
  983. // Open the file and write to it using the file I/O APIs.
  984. //
  985. OpenFlags = O_WRONLY;
  986. File = open(FileName, OpenFlags);
  987. if (File < 0) {
  988. if (errno != ENOENT) {
  989. PRINT_ERROR("Failed to open file %s (flags %x): %s.\n",
  990. FileName,
  991. OpenFlags,
  992. strerror(errno));
  993. Failures += 1;
  994. }
  995. continue;
  996. }
  997. //
  998. // Fill the file buffer with 0's and write it out to the file. The
  999. // memory mappings are marked private, so none of their prior
  1000. // writes should get erased by this action.
  1001. //
  1002. for (FillIndex = 0;
  1003. FillIndex < FileSize / sizeof(INT);
  1004. FillIndex += 1) {
  1005. FileBuffer[FillIndex] = 0;
  1006. }
  1007. DEBUG_PRINT("Writing to file %s.\n", FileName);
  1008. do {
  1009. BytesComplete = write(File, FileBuffer, FileSize);
  1010. } while ((BytesComplete < 0) && (errno == EINTR));
  1011. if (BytesComplete != FileSize) {
  1012. PRINT_ERROR("Write failed. Wrote %d of %d bytes: %s.\n",
  1013. BytesComplete,
  1014. FileSize,
  1015. strerror(errno));
  1016. Failures += 1;
  1017. }
  1018. if (close(File) != 0) {
  1019. PRINT_ERROR("Failed to close: %s.\n", strerror(errno));
  1020. Failures += 1;
  1021. }
  1022. break;
  1023. case MemoryMapTestActionFileRead:
  1024. //
  1025. // Open the file and read from it using the file I/O APIs.
  1026. //
  1027. OpenFlags = O_RDONLY;
  1028. File = open(FileName, OpenFlags);
  1029. if (File < 0) {
  1030. if (errno != ENOENT) {
  1031. PRINT_ERROR("Failed to open file %s (flags %x): %s.\n",
  1032. FileName,
  1033. OpenFlags,
  1034. strerror(errno));
  1035. Failures += 1;
  1036. }
  1037. continue;
  1038. }
  1039. DEBUG_PRINT("Reading from file %s.\n", FileName);
  1040. //
  1041. // Fill the file buffer with junk and read it in.
  1042. //
  1043. for (FillIndex = 0;
  1044. FillIndex < FileSize / sizeof(INT);
  1045. FillIndex += 1) {
  1046. FileBuffer[FillIndex] = 0xFEEDF00D;
  1047. }
  1048. TotalBytesComplete = 0;
  1049. while (TotalBytesComplete < FileSize) {
  1050. do {
  1051. BytesComplete = read(File,
  1052. FileBuffer + TotalBytesComplete,
  1053. FileSize - TotalBytesComplete);
  1054. } while ((BytesComplete < 0) && (errno == EINTR));
  1055. if (BytesComplete <= 0) {
  1056. PRINT_ERROR("Read failed. Read %d (%d total) of "
  1057. "%d bytes: %s.\n",
  1058. BytesComplete,
  1059. TotalBytesComplete,
  1060. FileSize,
  1061. strerror(errno));
  1062. Failures += 1;
  1063. break;
  1064. }
  1065. TotalBytesComplete += BytesComplete;
  1066. }
  1067. //
  1068. // The result should either be -2 or 0. The mappings were done
  1069. // privately, so any mapped writes should not have had an effect.
  1070. //
  1071. for (FillIndex = 0;
  1072. FillIndex < FileSize / sizeof(INT);
  1073. FillIndex += 1) {
  1074. if (FileBuffer[FillIndex] != 0) {
  1075. PRINT_ERROR("Read data file %s index %x came back %x, "
  1076. "should have been 0.\n",
  1077. FileName,
  1078. FillIndex,
  1079. FileBuffer[FillIndex]);
  1080. Failures += 1;
  1081. }
  1082. }
  1083. if (close(File) != 0) {
  1084. PRINT_ERROR("Failed to close: %s.\n", strerror(errno));
  1085. Failures += 1;
  1086. }
  1087. break;
  1088. default:
  1089. assert(FALSE);
  1090. break;
  1091. }
  1092. if ((Iteration % Percent) == 0) {
  1093. PRINT("p");
  1094. }
  1095. }
  1096. //
  1097. // Unmap all the files that remain mapped.
  1098. //
  1099. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  1100. if (MapBuffer[FileIndex] != MAP_FAILED) {
  1101. Result = munmap(MapBuffer[FileIndex], FileSize);
  1102. if (Result != 0) {
  1103. PRINT_ERROR("Failed to unmap %p: %s.\n",
  1104. MapBuffer[FileIndex],
  1105. strerror(errno));
  1106. Failures += 1;
  1107. }
  1108. }
  1109. }
  1110. PRINT("\nMax usage: %d files, %I64d bytes.\n",
  1111. MaxSimultaneousFiles,
  1112. (ULONGLONG)MaxSimultaneousFiles * (ULONGLONG)FileSize);
  1113. Failures += PrintTestTime(&StartTime);
  1114. RunMemoryMapPrivateTestEnd:
  1115. if (FileBuffer != NULL) {
  1116. free(FileBuffer);
  1117. }
  1118. if (FileOffset != NULL) {
  1119. free(FileOffset);
  1120. }
  1121. if (MapBuffer != NULL) {
  1122. free(MapBuffer);
  1123. }
  1124. return Failures;
  1125. }
  1126. ULONG
  1127. RunMemoryMapSharedTest (
  1128. INT FileCount,
  1129. INT FileSize,
  1130. INT Iterations
  1131. )
  1132. /*++
  1133. Routine Description:
  1134. This routine executes the memory map shared consistency test.
  1135. Arguments:
  1136. FileCount - Supplies the number of files to work with.
  1137. FileSize - Supplies the size of each file.
  1138. Iterations - Supplies the number of iterations to perform.
  1139. Return Value:
  1140. Returns the number of failures in the test suite.
  1141. --*/
  1142. {
  1143. MEMORY_MAP_TEST_ACTION Action;
  1144. unsigned ActionSeed;
  1145. ssize_t BytesComplete;
  1146. ULONG Failures;
  1147. INT File;
  1148. INT FileIndex;
  1149. CHAR FileName[16];
  1150. INT Iteration;
  1151. PBYTE *MapBuffer;
  1152. INT MaxSimultaneousFiles;
  1153. INT Offset;
  1154. INT OpenFlags;
  1155. INT Percent;
  1156. pid_t Process;
  1157. INT Result;
  1158. struct sigaction SignalAction;
  1159. INT SimultaneousFiles;
  1160. struct timeval StartTime;
  1161. BYTE Value;
  1162. Failures = 0;
  1163. MapBuffer = NULL;
  1164. //
  1165. // Set up the signal handler.
  1166. //
  1167. SignalAction.sa_sigaction = MemoryMapTestUnexpectedSignalHandler;
  1168. sigemptyset(&(SignalAction.sa_mask));
  1169. SignalAction.sa_flags = SA_SIGINFO;
  1170. sigaction(SIGSEGV, &SignalAction, NULL);
  1171. sigaction(SIGBUS, &SignalAction, NULL);
  1172. //
  1173. // Record the test start time.
  1174. //
  1175. Result = gettimeofday(&StartTime, NULL);
  1176. if (Result != 0) {
  1177. PRINT_ERROR("Failed to get time of day: %s.\n", strerror(errno));
  1178. Failures += 1;
  1179. goto RunMemoryMapSharedTestEnd;
  1180. }
  1181. //
  1182. // Announce the test.
  1183. //
  1184. Process = getpid();
  1185. PRINT("Process %d Running memory map shared test with %d files of %d "
  1186. "bytes each. %d iterations.\n",
  1187. Process,
  1188. FileCount,
  1189. FileSize,
  1190. Iterations);
  1191. Percent = Iterations / 100;
  1192. if (Percent == 0) {
  1193. Percent = 1;
  1194. }
  1195. MaxSimultaneousFiles = 0;
  1196. SimultaneousFiles = 0;
  1197. MapBuffer = malloc(FileCount * sizeof(PINT));
  1198. if (MapBuffer == NULL) {
  1199. Failures += 1;
  1200. goto RunMemoryMapSharedTestEnd;
  1201. }
  1202. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  1203. MapBuffer[FileIndex] = MAP_FAILED;
  1204. }
  1205. FileSize = ALIGN_RANGE_UP(FileSize, sizeof(INT));
  1206. //
  1207. // Get a separate seed for the random actions.
  1208. //
  1209. ActionSeed = time(NULL);
  1210. //
  1211. // Perform the memory map operations. This test picks a random offset and
  1212. // writes the low byte of the offset to the offet's location in a file.
  1213. // Memory mapped regions of the file that are mapped shared are then tested
  1214. // at random offsets to determine if the correct value, or 0, is stored
  1215. // there.
  1216. //
  1217. for (Iteration = 0; Iteration < Iterations; Iteration += 1) {
  1218. //
  1219. // Pick a random file and a random action.
  1220. //
  1221. FileIndex = rand() % FileCount;
  1222. snprintf(FileName,
  1223. sizeof(FileName),
  1224. "mmst-%06x",
  1225. FileIndex);
  1226. Action = rand_r(&ActionSeed) % MemoryMapTestActionCount;
  1227. //
  1228. // If the file has yet to be created, then the action must be map.
  1229. //
  1230. if (MapBuffer[FileIndex] == MAP_FAILED) {
  1231. Action = MemoryMapTestActionMap;
  1232. //
  1233. // Otherwise if the file is set to be mapped again, go unmap it first.
  1234. //
  1235. } else if (Action == MemoryMapTestActionMap) {
  1236. Action = MemoryMapTestActionUnmap;
  1237. }
  1238. switch (Action) {
  1239. case MemoryMapTestActionMap:
  1240. assert(MapBuffer[FileIndex] == MAP_FAILED);
  1241. SimultaneousFiles += 1;
  1242. if (SimultaneousFiles > MaxSimultaneousFiles) {
  1243. MaxSimultaneousFiles = SimultaneousFiles;
  1244. }
  1245. //
  1246. // Open the file read/write.
  1247. //
  1248. OpenFlags = O_RDWR;
  1249. File = open(FileName, OpenFlags);
  1250. if (File < 0) {
  1251. PRINT_ERROR("Failed to open file %s (flags %x): %s.\n",
  1252. FileName,
  1253. OpenFlags,
  1254. strerror(errno));
  1255. Failures += 1;
  1256. continue;
  1257. }
  1258. //
  1259. // Use ftruncate to make the file bigger so that access to the
  1260. // mmapped region succeeds.
  1261. //
  1262. Result = ftruncate(File, FileSize);
  1263. if (Result != 0) {
  1264. PRINT_ERROR("Failed to ftruncate file %s: %s.\n",
  1265. FileName,
  1266. strerror(errno));
  1267. Failures += 1;
  1268. }
  1269. //
  1270. // Mmap the file privately with read/write permissions.
  1271. //
  1272. MapBuffer[FileIndex] = mmap(0,
  1273. FileSize,
  1274. PROT_READ | PROT_WRITE,
  1275. MAP_SHARED,
  1276. File,
  1277. 0);
  1278. //
  1279. // Close the file.
  1280. //
  1281. if (close(File) != 0) {
  1282. PRINT_ERROR("Failed to close: %s.\n", strerror(errno));
  1283. Failures += 1;
  1284. }
  1285. //
  1286. // Increment the failure count if the mapping failed.
  1287. //
  1288. if (MapBuffer[FileIndex] == MAP_FAILED) {
  1289. PRINT_ERROR("Failed to map file %s rw + private: %s.\n",
  1290. FileName,
  1291. strerror(errno));
  1292. Failures += 1;
  1293. continue;
  1294. }
  1295. break;
  1296. case MemoryMapTestActionUnmap:
  1297. assert(MapBuffer[FileIndex] != MAP_FAILED);
  1298. //
  1299. // Unmap the file.
  1300. //
  1301. Result = munmap(MapBuffer[FileIndex], FileSize);
  1302. if (Result != 0) {
  1303. PRINT_ERROR("Failed to unmap %s.\n", strerror(errno));
  1304. Failures += 1;
  1305. continue;
  1306. }
  1307. MapBuffer[FileIndex] = MAP_FAILED;
  1308. SimultaneousFiles -= 1;
  1309. break;
  1310. case MemoryMapTestActionMappedWrite:
  1311. assert(MapBuffer[FileIndex] != MAP_FAILED);
  1312. Offset = rand() % FileSize;
  1313. DEBUG_PRINT("Writing file %s at offset %x.\n", FileName, Offset);
  1314. MapBuffer[FileIndex][Offset] = (BYTE)Offset;
  1315. break;
  1316. case MemoryMapTestActionMappedRead:
  1317. assert(MapBuffer[FileIndex] != MAP_FAILED);
  1318. Offset = rand() % FileSize;
  1319. DEBUG_PRINT("Reading file %s at offset %x.\n", FileName, Offset);
  1320. Value = MapBuffer[FileIndex][Offset];
  1321. if ((Value != 0) && (Value != (BYTE)Offset)) {
  1322. PRINT_ERROR("Read data file %s at offset %x came back %x, "
  1323. "should have been %x or 0.\n",
  1324. FileName,
  1325. Offset,
  1326. Value,
  1327. (BYTE)Offset);
  1328. Failures += 1;
  1329. }
  1330. break;
  1331. case MemoryMapTestActionSync:
  1332. assert(MapBuffer[FileIndex] != MAP_FAILED);
  1333. DEBUG_PRINT("Syncing file %s.\n", FileName);
  1334. Result = msync(MapBuffer[FileIndex], FileSize, MS_SYNC);
  1335. if (Result != 0) {
  1336. PRINT_ERROR("Failed to msync file %s: %s.\n",
  1337. FileName,
  1338. strerror(errno));
  1339. Failures += 1;
  1340. continue;
  1341. }
  1342. break;
  1343. case MemoryMapTestActionFileWrite:
  1344. assert(MapBuffer[FileIndex] != MAP_FAILED);
  1345. //
  1346. // Open the file and write to it using the file I/O APIs.
  1347. //
  1348. OpenFlags = O_WRONLY;
  1349. File = open(FileName, OpenFlags);
  1350. if (File < 0) {
  1351. if (errno != ENOENT) {
  1352. PRINT_ERROR("Failed to open file %s (flags %x): %s.\n",
  1353. FileName,
  1354. OpenFlags,
  1355. strerror(errno));
  1356. Failures += 1;
  1357. }
  1358. continue;
  1359. }
  1360. Offset = rand() % FileSize;
  1361. Result = lseek(File, Offset, SEEK_SET);
  1362. if (Result < 0) {
  1363. PRINT_ERROR("Seek on file %s offset %d failed.\n",
  1364. FileName,
  1365. Offset);
  1366. Failures += 1;
  1367. }
  1368. DEBUG_PRINT("Writing to file %s at offset %x.\n", FileName, Offset);
  1369. do {
  1370. BytesComplete = write(File, &Offset, 1);
  1371. } while ((BytesComplete < 0) && (errno == EINTR));
  1372. if (BytesComplete != 1) {
  1373. PRINT_ERROR("Write failed. Wrote %d of 1 byte: %s.\n",
  1374. BytesComplete,
  1375. strerror(errno));
  1376. Failures += 1;
  1377. }
  1378. if (close(File) != 0) {
  1379. PRINT_ERROR("Failed to close: %s.\n", strerror(errno));
  1380. Failures += 1;
  1381. }
  1382. //
  1383. // If the write succeeded and the mapped buffer at this offset
  1384. // does not match the low byte of the offset, then something is
  1385. // wrong.
  1386. //
  1387. if ((BytesComplete == 1) &&
  1388. (MapBuffer[FileIndex][Offset] != (BYTE)Offset)) {
  1389. PRINT_ERROR("Wrote to %s at offset %x with value %x, but "
  1390. "mapped buffer read %x.\n",
  1391. FileName,
  1392. Offset,
  1393. (BYTE)Offset,
  1394. MapBuffer[FileIndex][Offset]);
  1395. Failures += 1;
  1396. }
  1397. break;
  1398. case MemoryMapTestActionFileRead:
  1399. assert(MapBuffer[FileIndex] != MAP_FAILED);
  1400. //
  1401. // Open the file and read from it using the file I/O APIs.
  1402. //
  1403. OpenFlags = O_RDONLY;
  1404. File = open(FileName, OpenFlags);
  1405. if (File < 0) {
  1406. if (errno != ENOENT) {
  1407. PRINT_ERROR("Failed to open file %s (flags %x): %s.\n",
  1408. FileName,
  1409. OpenFlags,
  1410. strerror(errno));
  1411. Failures += 1;
  1412. }
  1413. continue;
  1414. }
  1415. Offset = rand() % FileSize;
  1416. Result = lseek(File, Offset, SEEK_SET);
  1417. if (Result < 0) {
  1418. PRINT_ERROR("Seek on file %s offset %d failed.\n",
  1419. FileName,
  1420. Offset);
  1421. Failures += 1;
  1422. }
  1423. DEBUG_PRINT("Reading from file %s at offset %x.\n",
  1424. FileName,
  1425. Offset);
  1426. Value = 0;
  1427. do {
  1428. BytesComplete = read(File, &Value, 1);
  1429. } while ((BytesComplete < 0) && (errno == EINTR));
  1430. if (close(File) != 0) {
  1431. PRINT_ERROR("Failed to close: %s.\n", strerror(errno));
  1432. Failures += 1;
  1433. }
  1434. if (BytesComplete < 0) {
  1435. PRINT_ERROR("Read failed. Read %d of 1 bytes: %s.\n",
  1436. BytesComplete,
  1437. strerror(errno));
  1438. Failures += 1;
  1439. break;
  1440. }
  1441. //
  1442. // The value should be 0 or the low byte of the offset.
  1443. //
  1444. if ((BytesComplete == 1) &&
  1445. ((Value != 0) && (Value != (BYTE)Offset))) {
  1446. PRINT_ERROR("Read file %s at offset %x. Read value %x but "
  1447. "expected 0 or %x.\n",
  1448. FileName,
  1449. Offset,
  1450. Value,
  1451. (BYTE)Offset);
  1452. Failures += 1;
  1453. }
  1454. //
  1455. // The value should match what the mapped buffer sees unless the
  1456. // value is 0 and the mapped buffer is now equal to the low by
  1457. // of the offset.
  1458. //
  1459. if ((BytesComplete == 1) &&
  1460. (Value != MapBuffer[FileIndex][Offset]) &&
  1461. ((Value != 0) ||
  1462. (MapBuffer[FileIndex][Offset] != (BYTE)Offset))) {
  1463. PRINT_ERROR("Read file %s at offset %x. Read value %x but "
  1464. "expected %x.\n",
  1465. FileName,
  1466. Offset,
  1467. Value,
  1468. MapBuffer[FileIndex][Offset]);
  1469. Failures += 1;
  1470. }
  1471. break;
  1472. default:
  1473. assert(FALSE);
  1474. break;
  1475. }
  1476. if ((Iteration % Percent) == 0) {
  1477. PRINT("s");
  1478. }
  1479. }
  1480. //
  1481. // Unmap all the files that remain mapped.
  1482. //
  1483. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  1484. if (MapBuffer[FileIndex] != MAP_FAILED) {
  1485. Result = munmap(MapBuffer[FileIndex], FileSize);
  1486. if (Result != 0) {
  1487. PRINT_ERROR("Failed to unmap %p: %s.\n",
  1488. MapBuffer[FileIndex],
  1489. strerror(errno));
  1490. Failures += 1;
  1491. }
  1492. }
  1493. }
  1494. PRINT("\nMax usage: %d files, %I64d bytes.\n",
  1495. MaxSimultaneousFiles,
  1496. (ULONGLONG)MaxSimultaneousFiles * (ULONGLONG)FileSize);
  1497. Failures += PrintTestTime(&StartTime);
  1498. RunMemoryMapSharedTestEnd:
  1499. if (MapBuffer != NULL) {
  1500. free(MapBuffer);
  1501. }
  1502. return Failures;
  1503. }
  1504. ULONG
  1505. RunMemoryMapShmPrivateTest (
  1506. INT FileCount,
  1507. INT FileSize,
  1508. INT Iterations
  1509. )
  1510. /*++
  1511. Routine Description:
  1512. This routine executes the memory map shared memory object private test.
  1513. Arguments:
  1514. FileCount - Supplies the number of files to work with.
  1515. FileSize - Supplies the size of each file.
  1516. Iterations - Supplies the number of iterations to perform.
  1517. Return Value:
  1518. Returns the number of failures in the test suite.
  1519. --*/
  1520. {
  1521. MEMORY_MAP_TEST_ACTION Action;
  1522. unsigned ActionSeed;
  1523. size_t BytesComplete;
  1524. INT ExpectedValue;
  1525. ULONG Failures;
  1526. INT File;
  1527. PINT FileBuffer;
  1528. INT FileIndex;
  1529. CHAR FileName[16];
  1530. PINT FileOffset;
  1531. INT FillIndex;
  1532. INT Iteration;
  1533. PINT *MapBuffer;
  1534. INT MaxSimultaneousObjects;
  1535. INT OpenFlags;
  1536. INT Percent;
  1537. pid_t Process;
  1538. INT Result;
  1539. struct sigaction SignalAction;
  1540. INT SimultaneousObjects;
  1541. struct timeval StartTime;
  1542. size_t TotalBytesComplete;
  1543. Failures = 0;
  1544. FileBuffer = NULL;
  1545. FileOffset = NULL;
  1546. MapBuffer = NULL;
  1547. //
  1548. // Set up the signal handler.
  1549. //
  1550. SignalAction.sa_sigaction = MemoryMapTestUnexpectedSignalHandler;
  1551. sigemptyset(&(SignalAction.sa_mask));
  1552. SignalAction.sa_flags = SA_SIGINFO;
  1553. sigaction(SIGSEGV, &SignalAction, NULL);
  1554. sigaction(SIGBUS, &SignalAction, NULL);
  1555. //
  1556. // Record the test start time.
  1557. //
  1558. Result = gettimeofday(&StartTime, NULL);
  1559. if (Result != 0) {
  1560. PRINT_ERROR("Failed to get time of day: %s.\n", strerror(errno));
  1561. Failures += 1;
  1562. goto RunMemoryMapShmPrivateTestEnd;
  1563. }
  1564. //
  1565. // Announce the test.
  1566. //
  1567. Process = getpid();
  1568. PRINT("Process %d Running shared memory object private test with %d files "
  1569. "of %d bytes each. %d iterations.\n",
  1570. Process,
  1571. FileCount,
  1572. FileSize,
  1573. Iterations);
  1574. Percent = Iterations / 100;
  1575. if (Percent == 0) {
  1576. Percent = 1;
  1577. }
  1578. MaxSimultaneousObjects = 0;
  1579. SimultaneousObjects = 0;
  1580. FileBuffer = malloc(FileSize);
  1581. if (FileBuffer == NULL) {
  1582. Failures += 1;
  1583. goto RunMemoryMapShmPrivateTestEnd;
  1584. }
  1585. FileOffset = malloc(FileCount * sizeof(INT));
  1586. if (FileOffset == NULL) {
  1587. Failures += 1;
  1588. goto RunMemoryMapShmPrivateTestEnd;
  1589. }
  1590. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  1591. FileOffset[FileIndex] = -1;
  1592. }
  1593. MapBuffer = malloc(FileCount * sizeof(PINT));
  1594. if (MapBuffer == NULL) {
  1595. Failures += 1;
  1596. goto RunMemoryMapShmPrivateTestEnd;
  1597. }
  1598. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  1599. MapBuffer[FileIndex] = MAP_FAILED;
  1600. }
  1601. FileSize = ALIGN_RANGE_UP(FileSize, sizeof(INT));
  1602. //
  1603. // Get a separate seed for the random actions.
  1604. //
  1605. ActionSeed = time(NULL);
  1606. //
  1607. // Perform the memory map operations. This test writes an entire shared
  1608. // memory object with incremental values and then tests that any file
  1609. // reads return the same values.
  1610. //
  1611. for (Iteration = 0; Iteration < Iterations; Iteration += 1) {
  1612. //
  1613. // Pick a random shared memory object and a random action.
  1614. //
  1615. FileIndex = rand() % FileCount;
  1616. snprintf(FileName,
  1617. sizeof(FileName),
  1618. "shmpt-%06x",
  1619. FileIndex);
  1620. Action = rand_r(&ActionSeed) % MemoryMapTestActionCount;
  1621. //
  1622. // If the file has yet to be created, then the action must be map.
  1623. //
  1624. if (MapBuffer[FileIndex] == MAP_FAILED) {
  1625. Action = MemoryMapTestActionMap;
  1626. //
  1627. // Otherwise if the file is set to be mapped again, go unmap it first.
  1628. //
  1629. } else if (Action == MemoryMapTestActionMap) {
  1630. Action = MemoryMapTestActionUnmap;
  1631. }
  1632. switch (Action) {
  1633. case MemoryMapTestActionMap:
  1634. assert(MapBuffer[FileIndex] == MAP_FAILED);
  1635. SimultaneousObjects += 1;
  1636. if (SimultaneousObjects > MaxSimultaneousObjects) {
  1637. MaxSimultaneousObjects = SimultaneousObjects;
  1638. }
  1639. //
  1640. // Open the shared memory object read/write.
  1641. //
  1642. OpenFlags = O_RDWR;
  1643. File = shm_open(FileName, OpenFlags, 0);
  1644. if (File < 0) {
  1645. PRINT_ERROR("Failed to open shm object %s (flags %x): %s.\n",
  1646. FileName,
  1647. OpenFlags,
  1648. strerror(errno));
  1649. Failures += 1;
  1650. continue;
  1651. }
  1652. //
  1653. // Use ftruncate to make the shared memory object bigger so that
  1654. // access to the mmapped region succeeds.
  1655. //
  1656. Result = ftruncate(File, FileSize);
  1657. if (Result != 0) {
  1658. PRINT_ERROR("Failed to ftruncate shm object %s: %s.\n",
  1659. FileName,
  1660. strerror(errno));
  1661. Failures += 1;
  1662. }
  1663. //
  1664. // Mmap the shared memory object privately with read/write
  1665. // permissions.
  1666. //
  1667. MapBuffer[FileIndex] = mmap(0,
  1668. FileSize,
  1669. PROT_READ | PROT_WRITE,
  1670. MAP_PRIVATE,
  1671. File,
  1672. 0);
  1673. //
  1674. // Close the file.
  1675. //
  1676. if (close(File) != 0) {
  1677. PRINT_ERROR("Failed to close: %s.\n", strerror(errno));
  1678. Failures += 1;
  1679. }
  1680. //
  1681. // Increment the failure count if the mapping failed.
  1682. //
  1683. if (MapBuffer[FileIndex] == MAP_FAILED) {
  1684. PRINT_ERROR("Failed to map shm object %s rw + private: %s.\n",
  1685. FileName,
  1686. strerror(errno));
  1687. Failures += 1;
  1688. continue;
  1689. }
  1690. break;
  1691. case MemoryMapTestActionUnmap:
  1692. assert(MapBuffer[FileIndex] != MAP_FAILED);
  1693. //
  1694. // Unmap the file.
  1695. //
  1696. Result = munmap(MapBuffer[FileIndex], FileSize);
  1697. if (Result != 0) {
  1698. PRINT_ERROR("Failed to unmap %s.\n", strerror(errno));
  1699. Failures += 1;
  1700. continue;
  1701. }
  1702. MapBuffer[FileIndex] = MAP_FAILED;
  1703. FileOffset[FileIndex] = -1;
  1704. SimultaneousObjects -= 1;
  1705. break;
  1706. case MemoryMapTestActionMappedWrite:
  1707. assert(MapBuffer[FileIndex] != MAP_FAILED);
  1708. FileOffset[FileIndex] = rand();
  1709. DEBUG_PRINT("Writing shm object %s, Value %x.\n",
  1710. FileName,
  1711. FileOffset[FileIndex]);
  1712. for (FillIndex = 0;
  1713. FillIndex < FileSize / sizeof(INT);
  1714. FillIndex += 1) {
  1715. MapBuffer[FileIndex][FillIndex] = FileOffset[FileIndex] +
  1716. FillIndex;
  1717. }
  1718. break;
  1719. case MemoryMapTestActionMappedRead:
  1720. assert(MapBuffer[FileIndex] != MAP_FAILED);
  1721. DEBUG_PRINT("Reading shm object %s, Value should be %x.\n",
  1722. FileName,
  1723. FileOffset[FileIndex]);
  1724. //
  1725. // If the mapped section has been written to, then the buffer
  1726. // should always read the incrementing random values. Otherwise the
  1727. // file should be all zero's.
  1728. //
  1729. for (FillIndex = 0;
  1730. FillIndex < FileSize / sizeof(INT);
  1731. FillIndex += 1) {
  1732. if (FileOffset[FileIndex] == -1) {
  1733. ExpectedValue = 0;
  1734. } else {
  1735. ExpectedValue = FileOffset[FileIndex] + FillIndex;
  1736. }
  1737. if (MapBuffer[FileIndex][FillIndex] != ExpectedValue) {
  1738. PRINT_ERROR("Mapped read shm object %s index %x came back"
  1739. "%x, should have been %x.\n",
  1740. FileName,
  1741. FillIndex,
  1742. MapBuffer[FileIndex][FillIndex],
  1743. ExpectedValue);
  1744. Failures += 1;
  1745. }
  1746. }
  1747. break;
  1748. case MemoryMapTestActionSync:
  1749. assert(MapBuffer[FileIndex] != MAP_FAILED);
  1750. DEBUG_PRINT("Syncing shm object %s, Value should be %x.\n",
  1751. FileName,
  1752. FileOffset[FileIndex]);
  1753. Result = msync(MapBuffer[FileIndex], FileSize, MS_SYNC);
  1754. if (Result != 0) {
  1755. PRINT_ERROR("Failed to msync shm object %s: %s.\n",
  1756. FileName,
  1757. strerror(errno));
  1758. Failures += 1;
  1759. continue;
  1760. }
  1761. break;
  1762. case MemoryMapTestActionFileWrite:
  1763. //
  1764. // Open the shared memory object and write to it using the file
  1765. // I/O APIs.
  1766. //
  1767. OpenFlags = O_RDWR;
  1768. File = shm_open(FileName, OpenFlags, 0);
  1769. if (File < 0) {
  1770. if (errno != ENOENT) {
  1771. PRINT_ERROR("Failed to open shm object %s (flags %x): "
  1772. "%s.\n",
  1773. FileName,
  1774. OpenFlags,
  1775. strerror(errno));
  1776. Failures += 1;
  1777. }
  1778. continue;
  1779. }
  1780. //
  1781. // Fill the file buffer with 0's and write it out to the file. The
  1782. // memory mappings are marked private, so none of their prior
  1783. // writes should get erased by this action.
  1784. //
  1785. for (FillIndex = 0;
  1786. FillIndex < FileSize / sizeof(INT);
  1787. FillIndex += 1) {
  1788. FileBuffer[FillIndex] = 0;
  1789. }
  1790. DEBUG_PRINT("Writing to shm object %s.\n", FileName);
  1791. do {
  1792. BytesComplete = write(File, FileBuffer, FileSize);
  1793. } while ((BytesComplete < 0) && (errno == EINTR));
  1794. if (BytesComplete != FileSize) {
  1795. PRINT_ERROR("Write failed. Wrote %d of %d bytes: %s.\n",
  1796. BytesComplete,
  1797. FileSize,
  1798. strerror(errno));
  1799. Failures += 1;
  1800. }
  1801. if (close(File) != 0) {
  1802. PRINT_ERROR("Failed to close: %s.\n", strerror(errno));
  1803. Failures += 1;
  1804. }
  1805. break;
  1806. case MemoryMapTestActionFileRead:
  1807. //
  1808. // Open the shared memory object and read from it using the file
  1809. // I/O APIs.
  1810. //
  1811. OpenFlags = O_RDONLY;
  1812. File = shm_open(FileName, OpenFlags, 0);
  1813. if (File < 0) {
  1814. if (errno != ENOENT) {
  1815. PRINT_ERROR("Failed to open shm object %s (flags %x): "
  1816. "%s.\n",
  1817. FileName,
  1818. OpenFlags,
  1819. strerror(errno));
  1820. Failures += 1;
  1821. }
  1822. continue;
  1823. }
  1824. DEBUG_PRINT("Reading from shm object %s.\n", FileName);
  1825. //
  1826. // Fill the file buffer with junk and read it in.
  1827. //
  1828. for (FillIndex = 0;
  1829. FillIndex < FileSize / sizeof(INT);
  1830. FillIndex += 1) {
  1831. FileBuffer[FillIndex] = 0xFEEDF00D;
  1832. }
  1833. TotalBytesComplete = 0;
  1834. while (TotalBytesComplete < FileSize) {
  1835. do {
  1836. BytesComplete = read(File,
  1837. FileBuffer + TotalBytesComplete,
  1838. FileSize - TotalBytesComplete);
  1839. } while ((BytesComplete < 0) && (errno == EINTR));
  1840. if (BytesComplete <= 0) {
  1841. PRINT_ERROR("Read failed. Read %d (%d total) of "
  1842. "%d bytes: %s.\n",
  1843. BytesComplete,
  1844. TotalBytesComplete,
  1845. FileSize,
  1846. strerror(errno));
  1847. Failures += 1;
  1848. break;
  1849. }
  1850. TotalBytesComplete += BytesComplete;
  1851. }
  1852. //
  1853. // The result should either be -2 or 0. The mappings were done
  1854. // privately, so any mapped writes should not have had an effect.
  1855. //
  1856. for (FillIndex = 0;
  1857. FillIndex < FileSize / sizeof(INT);
  1858. FillIndex += 1) {
  1859. if (FileBuffer[FillIndex] != 0) {
  1860. PRINT_ERROR("Read data shm object %s index %x came back "
  1861. "%x, should have been 0.\n",
  1862. FileName,
  1863. FillIndex,
  1864. FileBuffer[FillIndex]);
  1865. Failures += 1;
  1866. }
  1867. }
  1868. if (close(File) != 0) {
  1869. PRINT_ERROR("Failed to close: %s.\n", strerror(errno));
  1870. Failures += 1;
  1871. }
  1872. break;
  1873. default:
  1874. assert(FALSE);
  1875. break;
  1876. }
  1877. if ((Iteration % Percent) == 0) {
  1878. PRINT("p");
  1879. }
  1880. }
  1881. //
  1882. // Unmap all the files that remain mapped.
  1883. //
  1884. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  1885. if (MapBuffer[FileIndex] != MAP_FAILED) {
  1886. Result = munmap(MapBuffer[FileIndex], FileSize);
  1887. if (Result != 0) {
  1888. PRINT_ERROR("Failed to unmap %p: %s.\n",
  1889. MapBuffer[FileIndex],
  1890. strerror(errno));
  1891. Failures += 1;
  1892. }
  1893. }
  1894. }
  1895. PRINT("\nMax usage: %d shm objects, %I64d bytes.\n",
  1896. MaxSimultaneousObjects,
  1897. (ULONGLONG)MaxSimultaneousObjects * (ULONGLONG)FileSize);
  1898. Failures += PrintTestTime(&StartTime);
  1899. RunMemoryMapShmPrivateTestEnd:
  1900. if (FileBuffer != NULL) {
  1901. free(FileBuffer);
  1902. }
  1903. if (FileOffset != NULL) {
  1904. free(FileOffset);
  1905. }
  1906. if (MapBuffer != NULL) {
  1907. free(MapBuffer);
  1908. }
  1909. return Failures;
  1910. }
  1911. ULONG
  1912. RunMemoryMapShmSharedTest (
  1913. INT FileCount,
  1914. INT FileSize,
  1915. INT Iterations
  1916. )
  1917. /*++
  1918. Routine Description:
  1919. This routine executes the memory map shared consistency test.
  1920. Arguments:
  1921. FileCount - Supplies the number of files to work with.
  1922. FileSize - Supplies the size of each file.
  1923. Iterations - Supplies the number of iterations to perform.
  1924. Return Value:
  1925. Returns the number of failures in the test suite.
  1926. --*/
  1927. {
  1928. MEMORY_MAP_TEST_ACTION Action;
  1929. unsigned ActionSeed;
  1930. size_t BytesComplete;
  1931. ULONG Failures;
  1932. INT File;
  1933. INT FileIndex;
  1934. CHAR FileName[16];
  1935. INT Iteration;
  1936. PBYTE *MapBuffer;
  1937. INT MaxSimultaneousObjects;
  1938. INT Offset;
  1939. INT OpenFlags;
  1940. INT Percent;
  1941. pid_t Process;
  1942. INT Result;
  1943. struct sigaction SignalAction;
  1944. INT SimultaneousObjects;
  1945. struct timeval StartTime;
  1946. BYTE Value;
  1947. Failures = 0;
  1948. MapBuffer = NULL;
  1949. //
  1950. // Set up the signal handler.
  1951. //
  1952. SignalAction.sa_sigaction = MemoryMapTestUnexpectedSignalHandler;
  1953. sigemptyset(&(SignalAction.sa_mask));
  1954. SignalAction.sa_flags = SA_SIGINFO;
  1955. sigaction(SIGSEGV, &SignalAction, NULL);
  1956. sigaction(SIGBUS, &SignalAction, NULL);
  1957. //
  1958. // Record the test start time.
  1959. //
  1960. Result = gettimeofday(&StartTime, NULL);
  1961. if (Result != 0) {
  1962. PRINT_ERROR("Failed to get time of day: %s.\n", strerror(errno));
  1963. Failures += 1;
  1964. goto RunMemoryMapShmSharedTestEnd;
  1965. }
  1966. //
  1967. // Announce the test.
  1968. //
  1969. Process = getpid();
  1970. PRINT("Process %d Running shared memroy object shared test with %d files "
  1971. "of %d bytes each. %d iterations.\n",
  1972. Process,
  1973. FileCount,
  1974. FileSize,
  1975. Iterations);
  1976. Percent = Iterations / 100;
  1977. if (Percent == 0) {
  1978. Percent = 1;
  1979. }
  1980. MaxSimultaneousObjects = 0;
  1981. SimultaneousObjects = 0;
  1982. MapBuffer = malloc(FileCount * sizeof(PINT));
  1983. if (MapBuffer == NULL) {
  1984. Failures += 1;
  1985. goto RunMemoryMapShmSharedTestEnd;
  1986. }
  1987. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  1988. MapBuffer[FileIndex] = MAP_FAILED;
  1989. }
  1990. FileSize = ALIGN_RANGE_UP(FileSize, sizeof(INT));
  1991. //
  1992. // Get a separate seed for the random actions.
  1993. //
  1994. ActionSeed = time(NULL);
  1995. //
  1996. // Perform the memory map operations. This test picks a random offset and
  1997. // writes the low byte of the offset to the offet's location in a shared
  1998. // memory object. Memory mapped regions of the object that are mapped
  1999. // shared are then tested at random offsets to determine if the correct
  2000. // value, or 0, is stored there.
  2001. //
  2002. for (Iteration = 0; Iteration < Iterations; Iteration += 1) {
  2003. //
  2004. // Pick a random file and a random action.
  2005. //
  2006. FileIndex = rand() % FileCount;
  2007. snprintf(FileName,
  2008. sizeof(FileName),
  2009. "shmst-%06x",
  2010. FileIndex);
  2011. Action = rand_r(&ActionSeed) % MemoryMapTestActionCount;
  2012. //
  2013. // If the file has yet to be created, then the action must be map.
  2014. //
  2015. if (MapBuffer[FileIndex] == MAP_FAILED) {
  2016. Action = MemoryMapTestActionMap;
  2017. //
  2018. // Otherwise if the file is set to be mapped again, go unmap it first.
  2019. //
  2020. } else if (Action == MemoryMapTestActionMap) {
  2021. Action = MemoryMapTestActionUnmap;
  2022. }
  2023. switch (Action) {
  2024. case MemoryMapTestActionMap:
  2025. assert(MapBuffer[FileIndex] == MAP_FAILED);
  2026. SimultaneousObjects += 1;
  2027. if (SimultaneousObjects > MaxSimultaneousObjects) {
  2028. MaxSimultaneousObjects = SimultaneousObjects;
  2029. }
  2030. //
  2031. // Open the shared memory object read/write.
  2032. //
  2033. OpenFlags = O_RDWR;
  2034. File = shm_open(FileName, OpenFlags, 0);
  2035. if (File < 0) {
  2036. PRINT_ERROR("Failed to open shm object %s (flags %x): %s.\n",
  2037. FileName,
  2038. OpenFlags,
  2039. strerror(errno));
  2040. Failures += 1;
  2041. continue;
  2042. }
  2043. //
  2044. // Use ftruncate to make the shared memory object bigger so that
  2045. // access to the mapped region succeeds.
  2046. //
  2047. Result = ftruncate(File, FileSize);
  2048. if (Result != 0) {
  2049. PRINT_ERROR("Failed to ftruncate shm object %s: %s.\n",
  2050. FileName,
  2051. strerror(errno));
  2052. Failures += 1;
  2053. }
  2054. //
  2055. // Mmap the file privately with read/write permissions.
  2056. //
  2057. MapBuffer[FileIndex] = mmap(0,
  2058. FileSize,
  2059. PROT_READ | PROT_WRITE,
  2060. MAP_SHARED,
  2061. File,
  2062. 0);
  2063. //
  2064. // Close the file.
  2065. //
  2066. if (close(File) != 0) {
  2067. PRINT_ERROR("Failed to close: %s.\n", strerror(errno));
  2068. Failures += 1;
  2069. }
  2070. //
  2071. // Increment the failure count if the mapping failed.
  2072. //
  2073. if (MapBuffer[FileIndex] == MAP_FAILED) {
  2074. PRINT_ERROR("Failed to map shm object %s rw + private: %s.\n",
  2075. FileName,
  2076. strerror(errno));
  2077. Failures += 1;
  2078. continue;
  2079. }
  2080. break;
  2081. case MemoryMapTestActionUnmap:
  2082. assert(MapBuffer[FileIndex] != MAP_FAILED);
  2083. //
  2084. // Unmap the shared memory object.
  2085. //
  2086. Result = munmap(MapBuffer[FileIndex], FileSize);
  2087. if (Result != 0) {
  2088. PRINT_ERROR("Failed to unmap %s.\n", strerror(errno));
  2089. Failures += 1;
  2090. continue;
  2091. }
  2092. MapBuffer[FileIndex] = MAP_FAILED;
  2093. SimultaneousObjects -= 1;
  2094. break;
  2095. case MemoryMapTestActionMappedWrite:
  2096. assert(MapBuffer[FileIndex] != MAP_FAILED);
  2097. Offset = rand() % FileSize;
  2098. DEBUG_PRINT("Writing shm object %s at offset %x.\n",
  2099. FileName,
  2100. Offset);
  2101. MapBuffer[FileIndex][Offset] = (BYTE)Offset;
  2102. break;
  2103. case MemoryMapTestActionMappedRead:
  2104. assert(MapBuffer[FileIndex] != MAP_FAILED);
  2105. Offset = rand() % FileSize;
  2106. DEBUG_PRINT("Reading shm object %s at offset %x.\n",
  2107. FileName,
  2108. Offset);
  2109. Value = MapBuffer[FileIndex][Offset];
  2110. if ((Value != 0) && (Value != (BYTE)Offset)) {
  2111. PRINT_ERROR("Read data shm object %s at offset %x came back "
  2112. "%x, should have been %x or 0.\n",
  2113. FileName,
  2114. Offset,
  2115. Value,
  2116. (BYTE)Offset);
  2117. Failures += 1;
  2118. }
  2119. break;
  2120. case MemoryMapTestActionSync:
  2121. assert(MapBuffer[FileIndex] != MAP_FAILED);
  2122. DEBUG_PRINT("Syncing shm object %s.\n", FileName);
  2123. Result = msync(MapBuffer[FileIndex], FileSize, MS_SYNC);
  2124. if (Result != 0) {
  2125. PRINT_ERROR("Failed to msync shm object %s: %s.\n",
  2126. FileName,
  2127. strerror(errno));
  2128. Failures += 1;
  2129. continue;
  2130. }
  2131. break;
  2132. case MemoryMapTestActionFileWrite:
  2133. assert(MapBuffer[FileIndex] != MAP_FAILED);
  2134. //
  2135. // Open the file and write to it using the file I/O APIs.
  2136. //
  2137. OpenFlags = O_RDWR;
  2138. File = shm_open(FileName, OpenFlags, 0);
  2139. if (File < 0) {
  2140. if (errno != ENOENT) {
  2141. PRINT_ERROR("Failed to open shm object %s (flags %x): "
  2142. "%s.\n",
  2143. FileName,
  2144. OpenFlags,
  2145. strerror(errno));
  2146. Failures += 1;
  2147. }
  2148. continue;
  2149. }
  2150. Offset = rand() % FileSize;
  2151. Result = lseek(File, Offset, SEEK_SET);
  2152. if (Result < 0) {
  2153. PRINT_ERROR("Seek on shm object %s offset %d failed.\n",
  2154. FileName,
  2155. Offset);
  2156. Failures += 1;
  2157. break;
  2158. }
  2159. DEBUG_PRINT("Writing to shm object %s at offset %x.\n",
  2160. FileName,
  2161. Offset);
  2162. do {
  2163. BytesComplete = write(File, &Offset, 1);
  2164. } while ((BytesComplete < 0) && (errno == EINTR));
  2165. if (BytesComplete != 1) {
  2166. PRINT_ERROR("Write failed. Wrote %d of 1 byte: %s.\n",
  2167. BytesComplete,
  2168. strerror(errno));
  2169. Failures += 1;
  2170. }
  2171. if (close(File) != 0) {
  2172. PRINT_ERROR("Failed to close: %s.\n", strerror(errno));
  2173. Failures += 1;
  2174. break;
  2175. }
  2176. //
  2177. // If the write succeeded and the mapped buffer at this offset
  2178. // does not match the low byte of the offset, then something is
  2179. // wrong.
  2180. //
  2181. if ((BytesComplete == 1) &&
  2182. (MapBuffer[FileIndex][Offset] != (BYTE)Offset)) {
  2183. PRINT_ERROR("Wrote to %s at offset %x with value %x, but "
  2184. "mapped buffer read %x.\n",
  2185. FileName,
  2186. Offset,
  2187. (BYTE)Offset,
  2188. MapBuffer[FileIndex][Offset]);
  2189. Failures += 1;
  2190. }
  2191. break;
  2192. case MemoryMapTestActionFileRead:
  2193. assert(MapBuffer[FileIndex] != MAP_FAILED);
  2194. //
  2195. // Open the file and read from it using the file I/O APIs.
  2196. //
  2197. OpenFlags = O_RDONLY;
  2198. File = shm_open(FileName, OpenFlags, 0);
  2199. if (File < 0) {
  2200. if (errno != ENOENT) {
  2201. PRINT_ERROR("Failed to open shm object %s (flags %x): "
  2202. "%s.\n",
  2203. FileName,
  2204. OpenFlags,
  2205. strerror(errno));
  2206. Failures += 1;
  2207. }
  2208. continue;
  2209. }
  2210. Offset = rand() % FileSize;
  2211. Result = lseek(File, Offset, SEEK_SET);
  2212. if (Result < 0) {
  2213. PRINT_ERROR("Seek on shm object %s offset %d failed.\n",
  2214. FileName,
  2215. Offset);
  2216. Failures += 1;
  2217. break;
  2218. }
  2219. DEBUG_PRINT("Reading from shm object %s at offset %x.\n",
  2220. FileName,
  2221. Offset);
  2222. Value = 0;
  2223. do {
  2224. BytesComplete = read(File, &Value, 1);
  2225. } while ((BytesComplete < 0) && (errno == EINTR));
  2226. if (close(File) != 0) {
  2227. PRINT_ERROR("Failed to close: %s.\n", strerror(errno));
  2228. Failures += 1;
  2229. break;
  2230. }
  2231. if (BytesComplete < 0) {
  2232. PRINT_ERROR("Read failed. Read %d of 1 bytes: %s.\n",
  2233. BytesComplete,
  2234. strerror(errno));
  2235. Failures += 1;
  2236. break;
  2237. }
  2238. //
  2239. // The value should be 0 or the low byte of the offset.
  2240. //
  2241. if ((BytesComplete == 1) &&
  2242. ((Value != 0) && (Value != (BYTE)Offset))) {
  2243. PRINT_ERROR("Read shm object %s at offset %x. Read value %x "
  2244. "but expected 0 or %x.\n",
  2245. FileName,
  2246. Offset,
  2247. Value,
  2248. (BYTE)Offset);
  2249. Failures += 1;
  2250. }
  2251. //
  2252. // The value should match what the mapped buffer sees unless the
  2253. // value is 0 and the mapped buffer is now equal to the low by
  2254. // of the offset.
  2255. //
  2256. if ((BytesComplete == 1) &&
  2257. (Value != MapBuffer[FileIndex][Offset]) &&
  2258. ((Value != 0) ||
  2259. (MapBuffer[FileIndex][Offset] != (BYTE)Offset))) {
  2260. PRINT_ERROR("Read shm object %s at offset %x. Read value %x "
  2261. "but expected %x.\n",
  2262. FileName,
  2263. Offset,
  2264. Value,
  2265. MapBuffer[FileIndex][Offset]);
  2266. Failures += 1;
  2267. }
  2268. break;
  2269. default:
  2270. assert(FALSE);
  2271. break;
  2272. }
  2273. if ((Iteration % Percent) == 0) {
  2274. PRINT("s");
  2275. }
  2276. }
  2277. //
  2278. // Unmap all the files that remain mapped.
  2279. //
  2280. for (FileIndex = 0; FileIndex < FileCount; FileIndex += 1) {
  2281. if (MapBuffer[FileIndex] != MAP_FAILED) {
  2282. Result = munmap(MapBuffer[FileIndex], FileSize);
  2283. if (Result != 0) {
  2284. PRINT_ERROR("Failed to unmap %p: %s.\n",
  2285. MapBuffer[FileIndex],
  2286. strerror(errno));
  2287. Failures += 1;
  2288. }
  2289. }
  2290. }
  2291. PRINT("\nMax usage: %d shm objects, %I64d bytes.\n",
  2292. MaxSimultaneousObjects,
  2293. (ULONGLONG)MaxSimultaneousObjects * (ULONGLONG)FileSize);
  2294. Failures += PrintTestTime(&StartTime);
  2295. RunMemoryMapShmSharedTestEnd:
  2296. if (MapBuffer != NULL) {
  2297. free(MapBuffer);
  2298. }
  2299. return Failures;
  2300. }
  2301. static
  2302. VOID
  2303. MemoryMapTestExpectedSignalHandler (
  2304. INT SignalNumber,
  2305. siginfo_t *SignalInformation,
  2306. PVOID Context
  2307. )
  2308. /*++
  2309. Routine Description:
  2310. This routine handles expected signals generated by the memory map tests.
  2311. Arguments:
  2312. SignalNumber - Supplies the signal number.
  2313. SignalInformation - Supplies a pointer to the signal information.
  2314. Context - Supplies a pointer to the signal context.
  2315. Return Value:
  2316. None.
  2317. --*/
  2318. {
  2319. DEBUG_PRINT("Caught expected signal %d, code %d in process %d.\n",
  2320. SignalNumber,
  2321. SignalInformation->si_code,
  2322. SignalInformation->si_pid);
  2323. exit(SignalNumber);
  2324. return;
  2325. }
  2326. static
  2327. VOID
  2328. MemoryMapTestUnexpectedSignalHandler (
  2329. INT SignalNumber,
  2330. siginfo_t *SignalInformation,
  2331. PVOID Context
  2332. )
  2333. /*++
  2334. Routine Description:
  2335. This routine handles unexpected signals generated by the memory map tests.
  2336. Arguments:
  2337. SignalNumber - Supplies the signal number.
  2338. SignalInformation - Supplies a pointer to the signal information.
  2339. Context - Supplies a pointer to the signal context.
  2340. Return Value:
  2341. None.
  2342. --*/
  2343. {
  2344. PRINT_ERROR("Caught unexpected signal %d, code %d in process %d.\n",
  2345. SignalNumber,
  2346. SignalInformation->si_code,
  2347. SignalInformation->si_pid);
  2348. exit(0);
  2349. return;
  2350. }
  2351. ULONG
  2352. PrintTestTime (
  2353. struct timeval *StartTime
  2354. )
  2355. /*++
  2356. Routine Description:
  2357. This routine prints the total time it took to run the test, given the
  2358. starting time of the test.
  2359. Arguments:
  2360. StartTime - Supplies a pointer to the test's start time.
  2361. Return Value:
  2362. Returns the number of failures.
  2363. --*/
  2364. {
  2365. struct timeval EndTime;
  2366. ULONG Failures;
  2367. INT Result;
  2368. struct timeval TotalTime;
  2369. Failures = 0;
  2370. //
  2371. // Record the end time and display the total time, in seconds.
  2372. //
  2373. Result = gettimeofday(&EndTime, NULL);
  2374. if (Result != 0) {
  2375. PRINT_ERROR("Failed to get time of day: %s.\n", strerror(errno));
  2376. Failures += 1;
  2377. goto PrintTestTimeEnd;
  2378. }
  2379. TotalTime.tv_sec = EndTime.tv_sec - StartTime->tv_sec;
  2380. TotalTime.tv_usec = EndTime.tv_usec - StartTime->tv_usec;
  2381. if (TotalTime.tv_usec < 0) {
  2382. TotalTime.tv_sec -= 1;
  2383. TotalTime.tv_usec += 1000000;
  2384. }
  2385. PRINT("Time: %d.%06d seconds.\n", TotalTime.tv_sec, TotalTime.tv_usec);
  2386. PrintTestTimeEnd:
  2387. return Failures;
  2388. }
  2389. ULONG
  2390. MemoryMapEmptyTest (
  2391. INT FileSize
  2392. )
  2393. /*++
  2394. Routine Description:
  2395. This routine tests access to an empty file that has been memory mapped.
  2396. Arguments:
  2397. FileSize - Supplies the size of the file to create, if any.
  2398. Return Value:
  2399. Returns the number of failures encountered.
  2400. --*/
  2401. {
  2402. pid_t Child;
  2403. struct sigaction ExpectedSignalAction;
  2404. ULONG Failures;
  2405. INT File;
  2406. CHAR FileName[16];
  2407. PBYTE MapBuffer;
  2408. INT OpenFlags;
  2409. pid_t Process;
  2410. INT Result;
  2411. BOOL SharedMemoryObject;
  2412. INT Status;
  2413. BYTE Value;
  2414. pid_t WaitChild;
  2415. Failures = 0;
  2416. File = -1;
  2417. MapBuffer = MAP_FAILED;
  2418. //
  2419. // Create a file or shared memory object private to this process.
  2420. //
  2421. Process = getpid();
  2422. snprintf(FileName,
  2423. sizeof(FileName),
  2424. "mmbt-%06x",
  2425. Process);
  2426. //
  2427. // Determine whether to test a file or shared memory object.
  2428. //
  2429. OpenFlags = O_RDWR | O_CREAT;
  2430. if ((rand() % 2) == 0) {
  2431. SharedMemoryObject = TRUE;
  2432. File = shm_open(FileName,
  2433. OpenFlags,
  2434. MEMORY_MAP_TEST_CREATE_PERMISSIONS);
  2435. } else {
  2436. SharedMemoryObject = FALSE;
  2437. DEBUG_PRINT("Creating file %s.\n", FileName);
  2438. File = open(FileName, OpenFlags, MEMORY_MAP_TEST_CREATE_PERMISSIONS);
  2439. }
  2440. if (File < 0) {
  2441. PRINT_ERROR("Failed to create file %s (flags %x): %s.\n",
  2442. FileName,
  2443. OpenFlags,
  2444. strerror(errno));
  2445. Failures += 1;
  2446. goto MemoryMapEmptyTestEnd;
  2447. }
  2448. //
  2449. // Test access beyond the end of the file. It should SIGBUS.
  2450. //
  2451. DEBUG_PRINT("Testing access beyond end of file %s.\n", FileName);
  2452. MapBuffer = mmap(0, FileSize, PROT_WRITE | PROT_READ, MAP_PRIVATE, File, 0);
  2453. if (MapBuffer == MAP_FAILED) {
  2454. PRINT_ERROR("Failed to map file %s starting at offset 0 for %x "
  2455. "bytes: %s\n",
  2456. FileName,
  2457. FileSize,
  2458. strerror(errno));
  2459. Failures += 1;
  2460. goto MemoryMapEmptyTestEnd;
  2461. }
  2462. //
  2463. // Set up the signal handler expecting a SIGBUS fault.
  2464. //
  2465. ExpectedSignalAction.sa_sigaction = MemoryMapTestExpectedSignalHandler;
  2466. sigemptyset(&(ExpectedSignalAction.sa_mask));
  2467. ExpectedSignalAction.sa_flags = SA_SIGINFO;
  2468. sigaction(SIGBUS, &ExpectedSignalAction, NULL);
  2469. //
  2470. // Fork to have the child try to write to the section.
  2471. //
  2472. Child = fork();
  2473. if (Child == 0) {
  2474. sigaction(SIGBUS, &ExpectedSignalAction, NULL);
  2475. *MapBuffer = 0x1;
  2476. PRINT_ERROR("Wrote to mapping at %p for empty file %s.\n",
  2477. MapBuffer,
  2478. FileName);
  2479. exit(0);
  2480. } else {
  2481. WaitChild = waitpid(Child, &Status, 0);
  2482. if (WaitChild == -1) {
  2483. PRINT_ERROR("Failed to wait for child %d: %s.\n",
  2484. Child,
  2485. strerror(errno));
  2486. } else {
  2487. assert(WaitChild == Child);
  2488. if (WEXITSTATUS(Status) != SIGBUS) {
  2489. PRINT_ERROR("Child %d exited with status %d, expected %d.\n",
  2490. Child,
  2491. WEXITSTATUS(Status),
  2492. SIGBUS);
  2493. Failures += 1;
  2494. }
  2495. }
  2496. }
  2497. //
  2498. // Fork to have the child try to read from the section.
  2499. //
  2500. Child = fork();
  2501. if (Child == 0) {
  2502. sigaction(SIGBUS, &ExpectedSignalAction, NULL);
  2503. Value = *MapBuffer;
  2504. PRINT_ERROR("Read %x from mapping at %p for empty file %s.\n",
  2505. Value,
  2506. MapBuffer,
  2507. FileName);
  2508. exit(0);
  2509. } else {
  2510. WaitChild = waitpid(Child, &Status, 0);
  2511. if (WaitChild == -1) {
  2512. PRINT_ERROR("Failed to wait for child %d: %s.\n",
  2513. Child,
  2514. strerror(errno));
  2515. } else {
  2516. assert(WaitChild == Child);
  2517. if (WEXITSTATUS(Status) != SIGBUS) {
  2518. PRINT_ERROR("Child %d exited with status %d, expected %d.\n",
  2519. Child,
  2520. WEXITSTATUS(Status),
  2521. SIGBUS);
  2522. Failures += 1;
  2523. }
  2524. }
  2525. }
  2526. MemoryMapEmptyTestEnd:
  2527. if (MapBuffer != MAP_FAILED) {
  2528. Result = munmap(MapBuffer, FileSize);
  2529. if (Result != 0) {
  2530. PRINT_ERROR("Failed to unmap file %s at %p: %s.\n",
  2531. FileName,
  2532. MapBuffer,
  2533. strerror(errno));
  2534. Failures += 1;
  2535. }
  2536. }
  2537. if (File >= 0) {
  2538. Result = close(File);
  2539. if (Result != 0) {
  2540. PRINT_ERROR("Failed to close file %s: %s.\n",
  2541. FileName,
  2542. strerror(errno));
  2543. Failures += 1;
  2544. }
  2545. if (SharedMemoryObject != FALSE) {
  2546. Result = shm_unlink(FileName);
  2547. } else {
  2548. Result = unlink(FileName);
  2549. }
  2550. if (Result != 0) {
  2551. PRINT_ERROR("Failed to unlink file %s: %s.\n",
  2552. FileName,
  2553. strerror(errno));
  2554. Failures += 1;
  2555. }
  2556. }
  2557. return Failures;
  2558. }
  2559. ULONG
  2560. MemoryMapTruncateTest (
  2561. INT FileSize
  2562. )
  2563. /*++
  2564. Routine Description:
  2565. This routine tests writing to a file beyond the point of truncation.
  2566. Arguments:
  2567. FileSize - Supplies the size of the file to use.
  2568. Return Value:
  2569. Returns the number of failures encountered.
  2570. --*/
  2571. {
  2572. pid_t Child;
  2573. struct sigaction ExpectedSignalAction;
  2574. ULONG Failures;
  2575. INT File;
  2576. CHAR FileName[16];
  2577. PBYTE MapBuffer;
  2578. INT NewFileSize;
  2579. INT OpenFlags;
  2580. LONG PageSize;
  2581. pid_t Process;
  2582. INT Result;
  2583. BOOL SharedMemoryObject;
  2584. INT Status;
  2585. struct sigaction UnexpectedSignalAction;
  2586. BYTE Value;
  2587. pid_t WaitChild;
  2588. Failures = 0;
  2589. File = -1;
  2590. MapBuffer = MAP_FAILED;
  2591. //
  2592. // Create a file private to this process.
  2593. //
  2594. Process = getpid();
  2595. snprintf(FileName,
  2596. sizeof(FileName),
  2597. "mmbt-%06x",
  2598. Process);
  2599. //
  2600. // Determine whether to create a file or shared memory object.
  2601. //
  2602. OpenFlags = O_RDWR | O_CREAT;
  2603. if ((rand() % 2) == 0) {
  2604. SharedMemoryObject = TRUE;
  2605. DEBUG_PRINT("Creating shm object %s.\n", FileName);
  2606. File = shm_open(FileName,
  2607. OpenFlags,
  2608. MEMORY_MAP_TEST_CREATE_PERMISSIONS);
  2609. } else {
  2610. SharedMemoryObject = FALSE;
  2611. DEBUG_PRINT("Creating file %s.\n", FileName);
  2612. File = open(FileName, OpenFlags, MEMORY_MAP_TEST_CREATE_PERMISSIONS);
  2613. }
  2614. if (File < 0) {
  2615. PRINT_ERROR("Failed to create file %s (flags %x): %s.\n",
  2616. FileName,
  2617. OpenFlags,
  2618. strerror(errno));
  2619. Failures += 1;
  2620. goto MemoryMapTruncateFileTestEnd;
  2621. }
  2622. //
  2623. // Map the whole file read/write.
  2624. //
  2625. MapBuffer = mmap(0, FileSize, PROT_WRITE | PROT_READ, MAP_PRIVATE, File, 0);
  2626. if (MapBuffer == MAP_FAILED) {
  2627. PRINT_ERROR("Failed to map file %s starting at offset 0 for %x "
  2628. "bytes: %s\n",
  2629. FileName,
  2630. FileSize,
  2631. strerror(errno));
  2632. Failures += 1;
  2633. goto MemoryMapTruncateFileTestEnd;
  2634. }
  2635. //
  2636. // Use ftruncate to extend the file size to the supplied size.
  2637. //
  2638. DEBUG_PRINT("Testing file access after truncate.\n");
  2639. Result = ftruncate(File, FileSize);
  2640. if (Result != 0) {
  2641. PRINT_ERROR("ftruncate failed to increase file size to %x: %s.\n",
  2642. FileSize,
  2643. strerror(errno));
  2644. Failures += 1;
  2645. goto MemoryMapTruncateFileTestEnd;
  2646. }
  2647. //
  2648. // Initialize the unexpected signal handler and try to write to the last
  2649. // byte of the file.
  2650. //
  2651. UnexpectedSignalAction.sa_sigaction = MemoryMapTestUnexpectedSignalHandler;
  2652. sigemptyset(&(UnexpectedSignalAction.sa_mask));
  2653. UnexpectedSignalAction.sa_flags = SA_SIGINFO;
  2654. sigaction(SIGBUS, &UnexpectedSignalAction, NULL);
  2655. *(MapBuffer + FileSize - 0x1) = 0x1;
  2656. //
  2657. // Trucate the file down by a page so that the last byte is no longer
  2658. // mapped. Keep in mind that only whole pages beyond the end of the new
  2659. // file size get unmapped.
  2660. //
  2661. PageSize = sysconf(_SC_PAGE_SIZE);
  2662. NewFileSize = FileSize - PageSize;
  2663. Result = ftruncate(File, NewFileSize);
  2664. if (Result != 0) {
  2665. PRINT_ERROR("ftruncate failed to decrease file size from %x to %x: "
  2666. "%s.\n",
  2667. FileSize,
  2668. NewFileSize,
  2669. strerror(errno));
  2670. Failures += 1;
  2671. goto MemoryMapTruncateFileTestEnd;
  2672. }
  2673. //
  2674. // Writing within the new file size should still work.
  2675. //
  2676. *(MapBuffer + NewFileSize - 0x1) = 0x1;
  2677. //
  2678. // Now set up for an expected fault.
  2679. //
  2680. ExpectedSignalAction.sa_sigaction = MemoryMapTestExpectedSignalHandler;
  2681. sigemptyset(&(ExpectedSignalAction.sa_mask));
  2682. ExpectedSignalAction.sa_flags = SA_SIGINFO;
  2683. //
  2684. // Fork to have the child try to write to the last byte in the orignal
  2685. // file size.
  2686. //
  2687. Child = fork();
  2688. if (Child == 0) {
  2689. sigaction(SIGBUS, &ExpectedSignalAction, NULL);
  2690. *(MapBuffer + FileSize - 0x1) = 0x1;
  2691. PRINT_ERROR("Wrote beyond the end of file %s with mapping at %p.\n",
  2692. FileName,
  2693. MapBuffer);
  2694. exit(0);
  2695. } else {
  2696. WaitChild = waitpid(Child, &Status, 0);
  2697. if (WaitChild == -1) {
  2698. PRINT_ERROR("Failed to wait for child %d: %s.\n",
  2699. Child,
  2700. strerror(errno));
  2701. } else {
  2702. assert(WaitChild == Child);
  2703. if (WEXITSTATUS(Status) != SIGBUS) {
  2704. PRINT_ERROR("Child %d exited with status %d, expected %d.\n",
  2705. Child,
  2706. WEXITSTATUS(Status),
  2707. SIGBUS);
  2708. Failures += 1;
  2709. }
  2710. }
  2711. }
  2712. //
  2713. // Fork to have the child try to read from the last byte in the orignal
  2714. // file size.
  2715. //
  2716. Child = fork();
  2717. if (Child == 0) {
  2718. sigaction(SIGBUS, &ExpectedSignalAction, NULL);
  2719. Value = *(MapBuffer + FileSize - 0x1);
  2720. PRINT_ERROR("Read %x from beyond the end of file %s with mapping at "
  2721. "%p.\n",
  2722. Value,
  2723. FileName,
  2724. MapBuffer);
  2725. exit(0);
  2726. } else {
  2727. WaitChild = waitpid(Child, &Status, 0);
  2728. if (WaitChild == -1) {
  2729. PRINT_ERROR("Failed to wait for child %d: %s.\n",
  2730. Child,
  2731. strerror(errno));
  2732. } else {
  2733. assert(WaitChild == Child);
  2734. if (WEXITSTATUS(Status) != SIGBUS) {
  2735. PRINT_ERROR("Child %d exited with status %d, expected %d.\n",
  2736. Child,
  2737. WEXITSTATUS(Status),
  2738. SIGBUS);
  2739. Failures += 1;
  2740. }
  2741. }
  2742. }
  2743. MemoryMapTruncateFileTestEnd:
  2744. if (MapBuffer != MAP_FAILED) {
  2745. Result = munmap(MapBuffer, FileSize);
  2746. if (Result != 0) {
  2747. PRINT_ERROR("Failed to unmap file %s at %p: %s.\n",
  2748. FileName,
  2749. MapBuffer,
  2750. strerror(errno));
  2751. Failures += 1;
  2752. }
  2753. }
  2754. if (File >= 0) {
  2755. Result = close(File);
  2756. if (Result != 0) {
  2757. PRINT_ERROR("Failed to close file %s: %s.\n",
  2758. FileName,
  2759. strerror(errno));
  2760. Failures += 1;
  2761. }
  2762. if (SharedMemoryObject != FALSE) {
  2763. Result = shm_unlink(FileName);
  2764. } else {
  2765. Result = unlink(FileName);
  2766. }
  2767. if (Result != 0) {
  2768. PRINT_ERROR("Failed to unlink file %s: %s.\n",
  2769. FileName,
  2770. strerror(errno));
  2771. Failures += 1;
  2772. }
  2773. }
  2774. return Failures;
  2775. }
  2776. ULONG
  2777. MemoryMapReadOnlyTest (
  2778. INT FileSize
  2779. )
  2780. /*++
  2781. Routine Description:
  2782. This routine tests writing to a read-only memory mapped section.
  2783. Arguments:
  2784. FileSize - Supplies the size of the file to use, if needed.
  2785. Return Value:
  2786. Returns the number of errors encountered.
  2787. --*/
  2788. {
  2789. pid_t Child;
  2790. struct sigaction ExpectedSignalAction;
  2791. ULONG Failures;
  2792. INT File;
  2793. CHAR FileName[16];
  2794. PBYTE MapBuffer;
  2795. INT OpenFlags;
  2796. pid_t Process;
  2797. INT Result;
  2798. BOOL SharedMemoryObject;
  2799. INT Status;
  2800. struct sigaction UnexpectedSignalAction;
  2801. BYTE Value;
  2802. pid_t WaitChild;
  2803. Failures = 0;
  2804. File = -1;
  2805. MapBuffer = MAP_FAILED;
  2806. //
  2807. // Create a file private to this process.
  2808. //
  2809. Process = getpid();
  2810. snprintf(FileName,
  2811. sizeof(FileName),
  2812. "mmbt-%06x",
  2813. Process);
  2814. //
  2815. // Determine whether to create a regular file or shared memory object.
  2816. //
  2817. OpenFlags = O_RDWR | O_CREAT;
  2818. if ((rand() % 2) == 0) {
  2819. SharedMemoryObject = TRUE;
  2820. DEBUG_PRINT("Creating shm object %s.\n", FileName);
  2821. File = shm_open(FileName,
  2822. OpenFlags,
  2823. MEMORY_MAP_TEST_CREATE_PERMISSIONS);
  2824. } else {
  2825. SharedMemoryObject = FALSE;
  2826. DEBUG_PRINT("Creating file %s.\n", FileName);
  2827. File = open(FileName, OpenFlags, MEMORY_MAP_TEST_CREATE_PERMISSIONS);
  2828. }
  2829. if (File < 0) {
  2830. PRINT_ERROR("Failed to create file %s (flags %x): %s.\n",
  2831. FileName,
  2832. OpenFlags,
  2833. strerror(errno));
  2834. Failures += 1;
  2835. goto MemoryMapReadOnlyFileTestEnd;
  2836. }
  2837. //
  2838. // Map the whole file read-only.
  2839. //
  2840. MapBuffer = mmap(0, FileSize, PROT_READ, MAP_PRIVATE, File, 0);
  2841. if (MapBuffer == MAP_FAILED) {
  2842. PRINT_ERROR("Failed to map file %s starting at offset 0 for %x "
  2843. "bytes: %s\n",
  2844. FileName,
  2845. FileSize,
  2846. strerror(errno));
  2847. Failures += 1;
  2848. goto MemoryMapReadOnlyFileTestEnd;
  2849. }
  2850. //
  2851. // Use ftruncate to extend the file size to the supplied size.
  2852. //
  2853. DEBUG_PRINT("Testing read-only mapping write access.\n");
  2854. Result = ftruncate(File, FileSize);
  2855. if (Result != 0) {
  2856. PRINT_ERROR("ftruncate failed to increase file size to %x: %s.\n",
  2857. FileSize,
  2858. strerror(errno));
  2859. Failures += 1;
  2860. goto MemoryMapReadOnlyFileTestEnd;
  2861. }
  2862. //
  2863. // Initialize the unexpected signal handler and try a read from the memory
  2864. // map.
  2865. //
  2866. UnexpectedSignalAction.sa_sigaction = MemoryMapTestUnexpectedSignalHandler;
  2867. sigemptyset(&(UnexpectedSignalAction.sa_mask));
  2868. UnexpectedSignalAction.sa_flags = SA_SIGINFO;
  2869. sigaction(SIGSEGV, &UnexpectedSignalAction, NULL);
  2870. Value = *MapBuffer;
  2871. DEBUG_PRINT("Successfully read %x from buffer %p for file %s.\n",
  2872. Value,
  2873. MapBuffer,
  2874. FileName);
  2875. //
  2876. // Now set up for an expected fault.
  2877. //
  2878. ExpectedSignalAction.sa_sigaction = MemoryMapTestExpectedSignalHandler;
  2879. sigemptyset(&(ExpectedSignalAction.sa_mask));
  2880. ExpectedSignalAction.sa_flags = SA_SIGINFO;
  2881. //
  2882. // Fork to have the child try to write to the first byte in the file.
  2883. //
  2884. Child = fork();
  2885. if (Child == 0) {
  2886. sigaction(SIGSEGV, &ExpectedSignalAction, NULL);
  2887. *MapBuffer = 0x1;
  2888. PRINT_ERROR("Wrote to a read-only mapping at %p for file %s.\n",
  2889. MapBuffer,
  2890. FileName);
  2891. exit(0);
  2892. } else {
  2893. WaitChild = waitpid(Child, &Status, 0);
  2894. if (WaitChild == -1) {
  2895. PRINT_ERROR("Failed to wait for child %d: %s.\n",
  2896. Child,
  2897. strerror(errno));
  2898. } else {
  2899. assert(WaitChild == Child);
  2900. if (WEXITSTATUS(Status) != SIGSEGV) {
  2901. PRINT_ERROR("Child %d exited with status %d, expected %d.\n",
  2902. Child,
  2903. WEXITSTATUS(Status),
  2904. SIGSEGV);
  2905. Failures += 1;
  2906. }
  2907. }
  2908. }
  2909. MemoryMapReadOnlyFileTestEnd:
  2910. if (MapBuffer != MAP_FAILED) {
  2911. Result = munmap(MapBuffer, FileSize);
  2912. if (Result != 0) {
  2913. PRINT_ERROR("Failed to unmap file %s at %p: %s.\n",
  2914. FileName,
  2915. MapBuffer,
  2916. strerror(errno));
  2917. Failures += 1;
  2918. }
  2919. }
  2920. if (File >= 0) {
  2921. Result = close(File);
  2922. if (Result != 0) {
  2923. PRINT_ERROR("Failed to close file %s: %s.\n",
  2924. FileName,
  2925. strerror(errno));
  2926. Failures += 1;
  2927. }
  2928. if (SharedMemoryObject != FALSE) {
  2929. Result = shm_unlink(FileName);
  2930. } else {
  2931. Result = unlink(FileName);
  2932. }
  2933. if (Result != 0) {
  2934. PRINT_ERROR("Failed to unlink file %s: %s.\n",
  2935. FileName,
  2936. strerror(errno));
  2937. Failures += 1;
  2938. }
  2939. }
  2940. return Failures;
  2941. }
  2942. ULONG
  2943. MemoryMapNoAccessTest (
  2944. INT FileSize
  2945. )
  2946. /*++
  2947. Routine Description:
  2948. This routine tests access of a file that is memory mapped with no access
  2949. permissions.
  2950. Arguments:
  2951. FileSize - Supplies the size of the file to use, if needed.
  2952. Return Value:
  2953. Returns the number of errors encountered.
  2954. --*/
  2955. {
  2956. pid_t Child;
  2957. struct sigaction ExpectedSignalAction;
  2958. ULONG Failures;
  2959. INT File;
  2960. CHAR FileName[16];
  2961. PBYTE MapBuffer;
  2962. INT OpenFlags;
  2963. pid_t Process;
  2964. INT Result;
  2965. BOOL SharedMemoryObject;
  2966. INT Status;
  2967. BYTE Value;
  2968. pid_t WaitChild;
  2969. Failures = 0;
  2970. File = -1;
  2971. MapBuffer = MAP_FAILED;
  2972. //
  2973. // Create a file private to this process.
  2974. //
  2975. Process = getpid();
  2976. snprintf(FileName,
  2977. sizeof(FileName),
  2978. "mmbt-%06x",
  2979. Process);
  2980. OpenFlags = O_RDWR | O_CREAT;
  2981. if ((rand() % 2) == 0) {
  2982. SharedMemoryObject = TRUE;
  2983. DEBUG_PRINT("Creating shm object %s.\n", FileName);
  2984. File = shm_open(FileName,
  2985. OpenFlags,
  2986. MEMORY_MAP_TEST_CREATE_PERMISSIONS);
  2987. } else {
  2988. SharedMemoryObject = FALSE;
  2989. DEBUG_PRINT("Creating file %s.\n", FileName);
  2990. File = open(FileName, OpenFlags, MEMORY_MAP_TEST_CREATE_PERMISSIONS);
  2991. }
  2992. if (File < 0) {
  2993. PRINT_ERROR("Failed to create file %s (flags %x): %s.\n",
  2994. FileName,
  2995. OpenFlags,
  2996. strerror(errno));
  2997. Failures += 1;
  2998. goto MemoryMapNoAccessFileTestEnd;
  2999. }
  3000. //
  3001. // Map the whole file with no access permissions..
  3002. //
  3003. MapBuffer = mmap(0, FileSize, PROT_NONE, MAP_PRIVATE, File, 0);
  3004. if (MapBuffer == MAP_FAILED) {
  3005. PRINT_ERROR("Failed to map file %s starting at offset 0 for %x "
  3006. "bytes: %s\n",
  3007. FileName,
  3008. FileSize,
  3009. strerror(errno));
  3010. Failures += 1;
  3011. goto MemoryMapNoAccessFileTestEnd;
  3012. }
  3013. //
  3014. // Use ftruncate to extend the file size to the supplied size.
  3015. //
  3016. DEBUG_PRINT("Testing no access mapping access.\n");
  3017. Result = ftruncate(File, FileSize);
  3018. if (Result != 0) {
  3019. PRINT_ERROR("ftruncate failed to increase file size to %x: %s.\n",
  3020. FileSize,
  3021. strerror(errno));
  3022. Failures += 1;
  3023. goto MemoryMapNoAccessFileTestEnd;
  3024. }
  3025. //
  3026. // Set up for an expected fault.
  3027. //
  3028. ExpectedSignalAction.sa_sigaction = MemoryMapTestExpectedSignalHandler;
  3029. sigemptyset(&(ExpectedSignalAction.sa_mask));
  3030. ExpectedSignalAction.sa_flags = SA_SIGINFO;
  3031. sigaction(SIGSEGV, &ExpectedSignalAction, NULL);
  3032. //
  3033. // Fork to have the child try to write to the first byte in the file.
  3034. //
  3035. Child = fork();
  3036. if (Child == 0) {
  3037. sigaction(SIGSEGV, &ExpectedSignalAction, NULL);
  3038. *MapBuffer = 0x1;
  3039. PRINT_ERROR("Wrote to a no-access mapping at %p for file %s.\n",
  3040. MapBuffer,
  3041. FileName);
  3042. exit(0);
  3043. } else {
  3044. WaitChild = waitpid(Child, &Status, 0);
  3045. if (WaitChild == -1) {
  3046. PRINT_ERROR("Failed to wait for child %d: %s.\n",
  3047. Child,
  3048. strerror(errno));
  3049. } else {
  3050. assert(WaitChild == Child);
  3051. if (WEXITSTATUS(Status) != SIGSEGV) {
  3052. PRINT_ERROR("Child %d exited with status %d, expected %d.\n",
  3053. Child,
  3054. WEXITSTATUS(Status),
  3055. SIGSEGV);
  3056. Failures += 1;
  3057. }
  3058. }
  3059. }
  3060. //
  3061. // Fork to have the child try to read the first byte in the file.
  3062. //
  3063. Child = fork();
  3064. if (Child == 0) {
  3065. sigaction(SIGSEGV, &ExpectedSignalAction, NULL);
  3066. Value = *MapBuffer;
  3067. PRINT_ERROR("Read %x from a no-access mapping at %p for file %s.\n",
  3068. Value,
  3069. MapBuffer,
  3070. FileName);
  3071. exit(0);
  3072. } else {
  3073. WaitChild = waitpid(Child, &Status, 0);
  3074. if (WaitChild == -1) {
  3075. PRINT_ERROR("Failed to wait for child %d: %s.\n",
  3076. Child,
  3077. strerror(errno));
  3078. } else {
  3079. assert(WaitChild == Child);
  3080. if (WEXITSTATUS(Status) != SIGSEGV) {
  3081. PRINT_ERROR("Child %d exited with status %d, expected %d.\n",
  3082. Child,
  3083. WEXITSTATUS(Status),
  3084. SIGSEGV);
  3085. Failures += 1;
  3086. }
  3087. }
  3088. }
  3089. MemoryMapNoAccessFileTestEnd:
  3090. if (MapBuffer != MAP_FAILED) {
  3091. Result = munmap(MapBuffer, FileSize);
  3092. if (Result != 0) {
  3093. PRINT_ERROR("Failed to unmap file %s at %p: %s.\n",
  3094. FileName,
  3095. MapBuffer,
  3096. strerror(errno));
  3097. Failures += 1;
  3098. }
  3099. }
  3100. if (File >= 0) {
  3101. Result = close(File);
  3102. if (Result != 0) {
  3103. PRINT_ERROR("Failed to close file %s: %s.\n",
  3104. FileName,
  3105. strerror(errno));
  3106. Failures += 1;
  3107. }
  3108. if (SharedMemoryObject != FALSE) {
  3109. Result = shm_unlink(FileName);
  3110. } else {
  3111. Result = unlink(FileName);
  3112. }
  3113. if (Result != 0) {
  3114. PRINT_ERROR("Failed to unlink file %s: %s.\n",
  3115. FileName,
  3116. strerror(errno));
  3117. Failures += 1;
  3118. }
  3119. }
  3120. return Failures;
  3121. }
  3122. ULONG
  3123. MemoryMapAnonymousTest (
  3124. INT FileSize
  3125. )
  3126. /*++
  3127. Routine Description:
  3128. This routine tests anonymous memory mapped sections that are not backed
  3129. by a file.
  3130. Arguments:
  3131. FileSize - Supplies the size of the file to use, if needed.
  3132. Return Value:
  3133. Returns the number of errors encountered.
  3134. --*/
  3135. {
  3136. pid_t Child;
  3137. ULONG Failures;
  3138. PBYTE MapBuffer;
  3139. INT Result;
  3140. INT Status;
  3141. BYTE Value;
  3142. pid_t WaitChild;
  3143. Failures = 0;
  3144. //
  3145. // Create an anonymous memory mapping of the given size.
  3146. //
  3147. DEBUG_PRINT("Creating an anonymous memory mapping.\n");
  3148. MapBuffer = mmap(0,
  3149. FileSize,
  3150. PROT_READ | PROT_WRITE,
  3151. MAP_ANONYMOUS | MAP_PRIVATE,
  3152. -1,
  3153. 0);
  3154. if (MapBuffer == MAP_FAILED) {
  3155. PRINT_ERROR("Failed to create anonymous memory mapping of size 0x%x "
  3156. "bytes: %s.\n",
  3157. FileSize,
  3158. strerror(errno));
  3159. Failures += 1;
  3160. goto MemoryMapAnonymousTestEnd;
  3161. }
  3162. //
  3163. // Read the memory mapping to make sure it reads zero.
  3164. //
  3165. if ((*MapBuffer != 0) || (*(MapBuffer + 1) != 0)) {
  3166. PRINT_ERROR("Failed to read zero from anonymous mapping.\n");
  3167. Failures += 1;
  3168. }
  3169. //
  3170. // Write to the memory mapping.
  3171. //
  3172. *MapBuffer = 0x1;
  3173. //
  3174. // Fork to have the child read the first byte to validate it shares with
  3175. // the parent. Then have the child write the second byte so the parent can
  3176. // validate that it's mapping did not change.
  3177. //
  3178. Child = fork();
  3179. if (Child == 0) {
  3180. Failures = 0;
  3181. Value = *MapBuffer;
  3182. if (Value != 0x1) {
  3183. PRINT_ERROR("Anonymous child failed to read first byte. Expected "
  3184. "1, read %x.\n",
  3185. Value);
  3186. Failures += 1;
  3187. }
  3188. Value = *(MapBuffer + 1);
  3189. if (Value != 0) {
  3190. PRINT_ERROR("Anonymous child failed to read first byte. Expected "
  3191. "0, read %x.\n",
  3192. Value);
  3193. Failures += 1;
  3194. }
  3195. *(MapBuffer + 1) = 0x2;
  3196. exit(Failures);
  3197. } else {
  3198. WaitChild = waitpid(Child, &Status, 0);
  3199. if (WaitChild == -1) {
  3200. PRINT_ERROR("Failed to wait for child %d: %s.\n",
  3201. Child,
  3202. strerror(errno));
  3203. } else {
  3204. assert(WaitChild == Child);
  3205. if (!WIFEXITED(Status)) {
  3206. PRINT_ERROR("Child %d returned with status %x\n",
  3207. Status);
  3208. Failures += 1;
  3209. }
  3210. Failures += WEXITSTATUS(Status);
  3211. }
  3212. }
  3213. //
  3214. // Because the mapping was private, the parent should be able to re-read
  3215. // the second byte and see a 0 and not a 2.
  3216. //
  3217. Value = *(MapBuffer + 1);
  3218. if (Value != 0) {
  3219. PRINT_ERROR("Anonymous parent failed to read 0 from mapping %p. Read "
  3220. "%x.\n",
  3221. MapBuffer + 1,
  3222. Value);
  3223. Failures += 1;
  3224. }
  3225. MemoryMapAnonymousTestEnd:
  3226. if (MapBuffer != MAP_FAILED) {
  3227. Result = munmap(MapBuffer, FileSize);
  3228. if (Result != 0) {
  3229. PRINT_ERROR("Anonymous failed to unmap memory map at %p: %s.\n",
  3230. MapBuffer,
  3231. strerror(errno));
  3232. Failures += 1;
  3233. }
  3234. }
  3235. return Failures;
  3236. }
  3237. ULONG
  3238. MemoryMapSharedAnonymousTest (
  3239. INT FileSize
  3240. )
  3241. /*++
  3242. Routine Description:
  3243. This routine tests shared anonymous memory mapped sections that are not
  3244. backed by a file.
  3245. Arguments:
  3246. FileSize - Supplies the size of the file to use, if needed.
  3247. Return Value:
  3248. Returns the number of errors encountered.
  3249. --*/
  3250. {
  3251. pid_t Child;
  3252. ULONG Failures;
  3253. PBYTE MapBuffer;
  3254. INT Result;
  3255. INT Status;
  3256. BYTE Value;
  3257. pid_t WaitChild;
  3258. Failures = 0;
  3259. //
  3260. // Create an anonymous memory mapping of the given size.
  3261. //
  3262. DEBUG_PRINT("Creating an anonymous shared memory mapping.\n");
  3263. MapBuffer = mmap(0,
  3264. FileSize,
  3265. PROT_READ | PROT_WRITE,
  3266. MAP_ANONYMOUS | MAP_SHARED,
  3267. -1,
  3268. 0);
  3269. if (MapBuffer == MAP_FAILED) {
  3270. PRINT_ERROR("Failed to create shared anonymous memory mapping of size "
  3271. "0x%x bytes: %s.\n",
  3272. FileSize,
  3273. strerror(errno));
  3274. Failures += 1;
  3275. goto MemoryMapSharedAnonymousTestEnd;
  3276. }
  3277. //
  3278. // Read the memory mapping to make sure it reads zero.
  3279. //
  3280. if ((*MapBuffer != 0) || (*(MapBuffer + 1) != 0)) {
  3281. PRINT_ERROR("Failed to read zero from shared anonymous mapping.\n");
  3282. Failures += 1;
  3283. }
  3284. //
  3285. // Write to the memory mapping.
  3286. //
  3287. *MapBuffer = 0x1;
  3288. //
  3289. // Fork to have the child read the first byte to validate it shares with
  3290. // the parent. Then have the child write the second byte so the parent can
  3291. // validate that it's mapping changed.
  3292. //
  3293. Child = fork();
  3294. if (Child == 0) {
  3295. Failures = 0;
  3296. Value = *MapBuffer;
  3297. if (Value != 0x1) {
  3298. PRINT_ERROR("Shared anonymous child failed to read first byte. "
  3299. "Expected 1, read %x.\n",
  3300. Value);
  3301. Failures += 1;
  3302. }
  3303. Value = *(MapBuffer + 1);
  3304. if (Value != 0) {
  3305. PRINT_ERROR("Shared anonymous child failed to read first byte. "
  3306. "Expected 0, read %x.\n",
  3307. Value);
  3308. Failures += 1;
  3309. }
  3310. *(MapBuffer + 1) = 0x2;
  3311. exit(Failures);
  3312. } else {
  3313. WaitChild = waitpid(Child, &Status, 0);
  3314. if (WaitChild == -1) {
  3315. PRINT_ERROR("Failed to wait for child %d: %s.\n",
  3316. Child,
  3317. strerror(errno));
  3318. } else {
  3319. assert(WaitChild == Child);
  3320. if (!WIFEXITED(Status)) {
  3321. PRINT_ERROR("Child %d returned with status %x\n",
  3322. Status);
  3323. Failures += 1;
  3324. }
  3325. Failures += WEXITSTATUS(Status);
  3326. }
  3327. }
  3328. //
  3329. // Because the mapping was private, the parent should be able to re-read
  3330. // the second byte and see a 0 and not a 2.
  3331. //
  3332. Value = *(MapBuffer + 1);
  3333. if (Value != 0x2) {
  3334. PRINT_ERROR("Shared anonymous parent failed to read 0x2 from mapping "
  3335. "%p. Read %x.\n",
  3336. MapBuffer + 1,
  3337. Value);
  3338. Failures += 1;
  3339. }
  3340. MemoryMapSharedAnonymousTestEnd:
  3341. if (MapBuffer != MAP_FAILED) {
  3342. Result = munmap(MapBuffer, FileSize);
  3343. if (Result != 0) {
  3344. PRINT_ERROR("Shared anonymous failed to unmap memory map at %p: "
  3345. "%s.\n",
  3346. MapBuffer,
  3347. strerror(errno));
  3348. Failures += 1;
  3349. }
  3350. }
  3351. return Failures;
  3352. }