Home
%3CLINGO-SUB%20id%3D%22lingo-sub-850031%22%20slang%3D%22en-US%22%3ERe%3A%20Hot%20Patching%20SQL%20Server%20Engine%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-850031%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EGreat%20post!%3C%2FSTRONG%3EThe%20idea%20is%20very%20interesting%20and%20you%20explained%20the%20meaning%20of%20%22%3CSPAN%3Ehot%20patching%22%20great%3C%2FSPAN%3E.%20You%20provide%20the%26nbsp%3Binsights%20on%20the%20concept%20%22Hot%20Patching%22%20and%20the%20architecture%2C%20but%20after%20reading%20the%20post%20I%20feel%20that%20I%20got%20nothing%20which%20I%20can%20take%20with%20me%20and%20implement.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20you%20said%20%22%3CSPAN%3Ethis%20barely%20scratches%20the%20surface%20of%20hot%20patching%3C%2FSPAN%3E%22%2C%20so%20it%20is%20time%20for%20some%20C%2FC%2B%2B%20code%20to%20show%20how%20do%20you%26nbsp%3Bredirect%20function%20calls%20targeting%20the%20old%20function(s)%20to%20new%20replacement%20function(s).%20I%20know%20how%20to%20load%20classes%20dynamically%20and%20add%20functions%20for%20example%20but%20how%20do%20you%20redirect%20the%20call%20for%20existing%20function%20(in%20practical)%3F%20Can%20you%20provide%20a%20link%20maybe%20to%20give%20a%20starting%20point%20for%20developers%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EI%20loved%20the%20idea%2C%20and%20now%26nbsp%3B%3C%2FSPAN%3EI%20would%20like%20to%20implement%20same%20procedure%20in%20my%20app%20for%20example%2C%20but%20you%20do%20not%20give%20me%20any%20starting%20point.%20I%20want%20to%20see%20some%20code%20of%20what%20you%20do%20and%20not%20just%20the%20explanation%20of%20how%20it%20is%20done%20in%20general%26nbsp%3B%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Fhtml%2Fimages%2Femoticons%2Fsmile_40x40.gif%22%20alt%3D%22%3Asmile%3A%22%20title%3D%22%3Asmile%3A%22%20%2F%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26gt%3B%26gt%3B%26nbsp%3B%3CSPAN%3ELet%20us%20know%20via%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22mailto%3ASQLDBArchitects%40microsoft.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESQLDBArchitects%40microsoft.com%3C%2FA%3E%3CSPAN%3E%26nbsp%3Bif%20you%20would%20be%20interested.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ENot%20clear%20to%20me%20%22interesting%20in%20what%22.%26nbsp%3B%20You%20mean%20interesting%20to%20have%20the%20same%20procedure%20in%20SQL%20Server%20on-premises%3F%20If%20so%20then%20obviously%20it%20sound%20awesome.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-854807%22%20slang%3D%22en-US%22%3ERe%3A%20Hot%20Patching%20SQL%20Server%20Engine%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-854807%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20is%20very%20impressive.%20However%2C%20there%20is%20one%20statement%20that%20isn't%20quite%20true%3A%20%22%3CSPAN%3EWe%20have%20automatic%20scheduling%20that%20avoids%20deploying%20during%20an%20Azure%20region%E2%80%99s%20core%20business%20hours.%22%20Deployments%20routinely%20occur%20at%204pm%20or%20even%203pm%20in%20any%20given%20region.%20Azure%20SQL%20Database%20updates%20occurred%20today%20in%20North%20Europe%20(Ireland)%20at%204pm%20Ireland%20time%2C%20as%20one%20example%3A%20%22This%20notification%20is%20for%20upcoming%20scheduled%20maintenance%20to%20your%20Azure%20SQL%20Database%20instance(s)%20in%20North%20Europe.%20This%20is%20scheduled%20to%20occur%20between%2016%3A00%20UTC%20on%2014%20Sep%202019%20and%2007%3A00%20UTC%20on%2015%20Sep%202019.%22%20I%20suppose%20it%20depends%20on%20how%20you%20define%20%22core%20business%20hours%22.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-855197%22%20slang%3D%22en-US%22%3ERe%3A%20Hot%20Patching%20SQL%20Server%20Engine%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-855197%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F9067%22%20target%3D%22_blank%22%3E%40R%20A%3C%2FA%3E%26nbsp%3B-%20Thanks%20for%20the%20feedback!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20C%20code%20we%20use%20to%20perform%20the%20patching%20(instruction%20overwriting)%20is%20not%20too%20complicated%20and%20does%20pretty%20much%20what%20is%20described%20above.%20It%20goes%20roughly%20like%20this%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ELoad%20the%20patched%20image%20using%20LoadLibrary()%3C%2FLI%3E%0A%3CLI%3EFind%20the%20module%20base%20address%20of%20the%20image%20to%20be%20patched.%3C%2FLI%3E%0A%3CLI%3EUse%20the%20pre-computed%20function%20offset(s)%20within%20each%20image%20to%20acquire%20a%20void*%20to%20where%20the%20functions%20start.%3C%2FLI%3E%0A%3CLI%3EDeref%20the%20pointers%20and%20write%20JMP%20instructions%20directly%20to%20the%20memory%20address.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EBut%20it's%20only%20a%20small%20piece%20of%20the%20puzzle.%20The%20hardest%20part%20of%20hot%20patching%20is%20to%20prove%20that%20redirecting%20a%20function%20will%20do%20the%20right%20thing.%20For%20example%2C%20is%20it%20referencing%20other%20functions%20or%20globals%2C%20has%20it%20been%20inlined%2C%20are%20compiler%20optimizations%20going%20to%20cause%20trouble%20etc.%20This%20is%20what%20I%20briefly%20touch%20on%20in%20section%206%2C%20but%20this%20topic%20is%20so%20large%20that%20one%20could%20write%20a%20PhD%20thesis%20about%20it.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20I%20had%20to%20limit%20the%20scope%20to%20not%20turn%20the%20blog%20post%20into%20a%20book%20%3A)%3C%2Fimg%3E%20Given%20that%20the%20scope%20of%20doing%20it%20safely%20is%20beyond%20what%20I%20can%20cover%20here%2C%20I%20didn't%20want%20to%20include%20examples%20of%20doing%20something%20that%20would%20almost%20certainly%20go%20wrong.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20it's%20great%20to%20know%20that%20there%20are%20folks%20out%20there%20that%20want%20more%20technical%20details.%20Maybe%20I'll%20do%20a%20follow-up%20post%20later.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20Hans%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-855198%22%20slang%3D%22en-US%22%3ERe%3A%20Hot%20Patching%20SQL%20Server%20Engine%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-855198%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F380947%22%20target%3D%22_blank%22%3E%40m60freeman%3C%2FA%3E%26nbsp%3B-%20thanks%20for%20the%20feedback!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECore%20business%20hours%20is%20defined%20as%2008%3A00%20(8am)%20to%2017%3A00%20(5pm)%2C%20local%20time.%20In%20your%20example%20below%2C%2016%3A00%20UTC%20is%20actually%2017%3A00%20local%20time%20because%20during%20daylight%20savings%20time%20(DST)%2C%20GMT%20standard%20time%20is%20UTC%2B1%20hour%2C%20not%20UTC.%20You%20can%20see%20that%20on%20the%20end%20time%20-%20the%20deployment%20stops%20by%207am%20UTC%2C%20which%20is%208am%20local%20time.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet%20me%20know%20if%20you%20have%20further%20questions.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20Hans%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-856144%22%20slang%3D%22en-US%22%3ERe%3A%20Hot%20Patching%20SQL%20Server%20Engine%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-856144%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20%3Athumbs_up%3A%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-856968%22%20slang%3D%22en-US%22%3ERe%3A%20Hot%20Patching%20SQL%20Server%20Engine%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-856968%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F405799%22%20target%3D%22_blank%22%3E%40norheim%3C%2FA%3E%26nbsp%3BHere%20is%20a%20recent%20notice%20for%20West%20Europe%20for%2015%3A00%20UTC%2C%20and%20there%20have%20been%20many%20more%20like%20it%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CTABLE%3E%3CTBODY%3E%3CTR%3E%3CTD%3E%3CP%3E%3CSPAN%3ETRACKING%20ID%3A%20%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3ELKJH-5Z0%3C%2FSPAN%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%3E%3CP%3E%3CSPAN%3ETYPE%3A%20%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3EMaintenance%3C%2FSPAN%3E%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E%3CP%3E%3CSPAN%3ESTATUS%3A%20%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3ERescheduled%208%2F31%2F2019%206%3A41%3A07%20AM%20(UTC)%3C%2FSPAN%3E%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E%3CP%3E%3CSPAN%3ECOMMUNICATION%3A%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EThis%20notification%20is%20for%20previously%20scheduled%20maintenance%20to%20your%20Azure%20SQL%20Database%20instance(s)%20in%20West%20Europe.%20Unfortunately%2C%20the%20maintenance%20is%20taking%20longer%20than%20expected.%20The%20maintenance%20is%20currently%20paused%20and%20will%20be%20resumed%20at%20%3CSTRONG%3E15%3A00%20UTC%3C%2FSTRONG%3E%20on%2031%20Aug%202019%20and%20is%20expected%20to%20complete%20by%2006%3A00%20UTC%20on%2001%20Sep%202019.%20Feel%20free%20to%20refer%20to%20your%20portal%20for%20more%20information%20(%3CA%20href%3D%22https%3A%2F%2Faka.ms%2Fservicehealthpm%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Faka.ms%2Fservicehealthpm%3C%2FA%3E)%20and%20for%20any%20additional%20questions%2C%20please%20%3CA%20href%3D%22https%3A%2F%2Fportal.azure.com%2F%23blade%2FMicrosoft_Azure_Support%2FHelpAndSupportBlade%2Foverview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Econtact%20support%3C%2FA%3E.%3C%2FP%3E%3CTABLE%3E%3CTBODY%3E%3CTR%3E%3CTD%3E%3CP%3E%3CSTRONG%3EResource%20Group%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%3E%3CP%3E%3CSTRONG%3EServer%20Name%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%3E%3CP%3E%3CSTRONG%3EDatabase%20Name%20%2F%20Elastic%20Pool%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E%3CP%3E%3CEM%3Eredacted%3C%2FEM%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%3E%26nbsp%3B%3C%2FTD%3E%3CTD%3E%3CP%3E%3CEM%3Eredacted%3C%2FEM%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%3E%26nbsp%3B%3C%2FTD%3E%3CTD%3E%3CP%3E%3CEM%3Eredacted%3C%2FEM%3E%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E%3CP%3EIMPACTED%20SERVICE(S)%20AND%20REGION(S)%3C%2FP%3E%3CTABLE%3E%3CTBODY%3E%3CTR%3E%3CTD%3E%3CP%3E%3CSTRONG%3EService%20Name%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%3E%3CP%3E%3CSTRONG%3ERegion%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E%3CP%3ESQL%20Database%3C%2FP%3E%3C%2FTD%3E%3CTD%3E%3CP%3EWest%20Europe%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-849700%22%20slang%3D%22en-US%22%3EHot%20Patching%20SQL%20Server%20Engine%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-849700%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EAuthor%3A%20%3C%2FSTRONG%3EHans%20Olav%20Norheim%2C%20hansnor%40microsoft.com%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ETechnical%20reviewers%3A%3C%2FSTRONG%3EJim%20Cavalaris%2C%20Jimmy%20Cleary%2C%20Jim%20Radigan%2C%20Ashish%20Kishan%20Mansukhani%2C%20Alexey%20Yeltsov%2C%20Brian%20Gianforcaro%2C%20Jasraj%20Dange%2C%20Jon%20Jahren%2C%20Michael%20Nelson%2C%20Peter%20Carlin%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20world%20of%20cloud%20database%20services%2C%20few%20things%20are%20more%20important%20to%20customers%20than%20having%20uninterrupted%20access%20to%20their%20data.%26nbsp%3B%20In%20industries%20like%20online%20gaming%20and%20financial%20services%20that%20experience%20high%20transaction%20rates%2C%20even%20the%20smallest%20interruptions%20can%20potentially%20impact%20the%20end-user%E2%80%99s%20experience.%20%26nbsp%3BAzure%20SQL%20Database%20is%20evergreen%2C%20meaning%20that%20it%20always%20has%20the%20latest%20version%20of%20the%20SQL%20engine%2C%20but%20maintaining%20this%20evergreen%20state%20requires%20periodic%20updates%20to%20the%20service%20that%20can%20take%20the%20database%20offline%20for%20a%20second.%26nbsp%3B%20For%20this%20reason%2C%20our%20engineering%20team%20is%20continuously%20working%20on%20innovative%20technology%20improvements%20that%20reduce%20workload%20interruption.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EToday%E2%80%99s%20post%2C%20in%20collaboration%20with%20the%20Visual%20C%2B%2B%20Compiler%20team%2C%20covers%20how%20we%20patch%20SQL%20Server%20Engine%20without%20impacting%20workload%20%3CEM%3Eat%20all%3C%2FEM%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20498px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F131351i16614DA2427CA327%2Fimage-dimensions%2F498x299%3Fv%3D1.0%22%20width%3D%22498%22%20height%3D%22299%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EThis%20is%20what%20hot%20patching%20looks%20like%20under%20the%20covers.%20Read%20on%20if%20you%20want%20all%20the%20details!%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1820324989%22%20id%3D%22toc-hId-1820324989%22%20id%3D%22toc-hId-1820324989%22%20id%3D%22toc-hId-1820324989%22%20id%3D%22toc-hId-1820324989%22%20id%3D%22toc-hId-1820324989%22%20id%3D%22toc-hId-1820324989%22%3E1.%20The%20Challenge%3C%2FH2%3E%0A%3CP%3E%3CSPAN%3EThe%20SQL%20Engine%20we%20are%20running%20in%20Azure%20SQL%20Database%20is%20the%20very%20latest%20version%20of%20the%20same%20engine%20customers%20run%20on%20their%20own%20servers%2C%20except%20we%20manage%20and%20update%20it.%20To%20update%20SQL%20Server%20or%20the%20underlying%20infrastructure%20(i.e.%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fservices%2Fservice-fabric%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EService%20Fabric%3C%2FA%3E%3CSPAN%3Eor%20the%20operating%20system)%2C%20we%20must%20stop%20the%20SQL%20Server%20process.%20If%20that%20process%20hosts%20the%20primary%20database%20replica%2C%20we%20move%20the%20replica%20to%20another%20machine%20(requiring%20a%20failover).%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EDuring%20failover%2C%20the%20database%20may%20be%20offline%20for%20a%20second%20and%20still%20meet%20our%2099.995%25%20SLA.%20However%2C%20%26nbsp%3Bfailover%20of%20the%20primary%20replica%20impacts%20workload%20because%20it%20aborts%20in-flight%20queries%20and%20transactions.%20We%20built%20features%20such%20as%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Findexes%2Fguidelines-for-online-index-operations%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eresumable%20index%20(re)build%3C%2FA%3E%3CSPAN%3Eand%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-accelerated-database-recovery%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eaccelerated%20database%20recovery%3C%2FA%3E%3CSPAN%3Eto%20address%20these%20situations%2C%20but%20not%20all%20running%20operations%20are%20automatically%20resumable.%20It%20may%20be%20expensive%20to%20restart%20complex%20queries%20or%20transactions%20that%20were%20aborted%20due%20to%20an%20upgrade.%20So%20even%20though%20failovers%20are%20quick%2C%20we%20want%20to%20avoid%20them.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ESQL%20Server%20and%20the%20overall%20Azure%20platform%20invests%20significant%20engineering%20effort%20into%20platform%20availability%20and%20reliability.%20We%20can%2C%20for%20example%2C%20update%20the%20Azure%20host%20without%20restarting%20Guest%20VMs.%20In%20SQL%20DB%2C%20we%20have%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-high-availability%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Emultiple%20replicas%3C%2FA%3E%3CSPAN%3Eof%20every%20database.%20During%20upgrade%2C%20we%20ensure%20that%20hot%20standbys%20are%20available%20to%20take%20over%20immediately.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWe%E2%80%99ve%20worked%20closely%20with%20the%20broader%20Azure%20and%20Service%20Fabric%20teams%20to%20minimize%20the%20number%20of%20failovers.%20When%20we%20first%20decide%20to%20fail%20over%20a%20database%20for%20upgrade%2C%20we%20apply%20updates%20to%20all%20components%20in%20the%20stack%20at%20the%20same%20time%3A%20OS%2C%20Service%20Fabric%20and%20SQL%20Server.%20We%20have%20automatic%20scheduling%20that%20avoids%20deploying%20during%20an%20Azure%20region%E2%80%99s%20core%20business%20hours.%20Just%20before%20failover%2C%20we%20attempt%20to%20drain%20active%20transactions%20to%20avoid%20aborting%20them.%20We%20even%20utilize%20database%20workload%20patterns%20to%20perform%20failover%20at%20the%20best%20time%20for%20the%20workload.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EEven%20with%20all%20that%2C%20we%20don%E2%80%99t%20get%20away%20from%20the%20fact%20that%20to%20update%20SQL%20Engine%20to%20a%20new%20version%2C%20we%20must%20restart%20the%20process%20and%20fail%20over%20the%20database%E2%80%99s%20primary%20replica%20at%20least%20once.%20Or%20do%20we%3F%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--731831972%22%20id%3D%22toc-hId--731831972%22%20id%3D%22toc-hId--731831972%22%20id%3D%22toc-hId--731831972%22%20id%3D%22toc-hId--731831972%22%20id%3D%22toc-hId--731831972%22%20id%3D%22toc-hId--731831972%22%3E2.%20Hot%20Patching%20and%20Results%3C%2FH2%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPatch_(computing)%23Hot_patching%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EHot%20patching%3C%2FA%3E%3CSPAN%3Eis%20modifying%20in-memory%20code%20in%20a%20running%20process%20without%20restarting%20the%20process.%20In%20our%20case%2C%20it%20gives%20us%20the%20capability%20to%20modify%20C%2B%2B%20code%20in%20SQL%20Engine%20without%20restarting%20sqlservr.exe.%20Since%20we%20don%E2%80%99t%20restart%2C%20we%20don%E2%80%99t%20fail%20over%20the%20primary%20replica%20and%20interrupt%20the%20workload.%20We%20don't%20even%20need%20to%20pause%20SQL%20Server%20activity%20while%20we%20patch.%20Hot%20patching%20is%20unnoticed%20by%20the%20user%20workload%20(other%20than%20the%20patch%20payload%2C%20of%20course)!%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EHot%20patching%26nbsp%3Bdoes%20not%26nbsp%3B%3CEM%3Ereplace%3C%2FEM%3Etraditional%2C%20restarting%20upgrades%20%E2%80%93%20it%20complements%20them.%20Hot%20patching%20currently%20has%20limitations%20that%20make%20it%20unsuitable%20when%20there%20are%20a%20large%20number%20of%20changes%2C%20such%20as%20when%20a%20major%20new%20feature%20is%20introduced.%20But%20it%20is%20perfect%20for%20smaller%2C%20targeted%20changes.%20More%20than%2080%25%20of%20typical%20SQL%20bug%20fixes%20are%20hot%20patchable.%20Benefits%20of%20hot%20patching%20include%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3E%3CSTRONG%3EReduced%20workload%20disruption%3CBR%20%2F%3E%3C%2FSTRONG%3E%3C%2FSPAN%3E%3CSPAN%3ENo%20restart%20means%20no%20database%20failover%20and%20no%20workload%20impact.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3E%3CSTRONG%3EFaster%20bug%20fixes%3CBR%20%2F%3E%3C%2FSTRONG%3E%3C%2FSPAN%3E%3CSPAN%3EPreviously%2C%20we%20weighed%20the%20urgency%20of%20a%20bug%20fix%20vs.%20impact%20on%20customer%20workloads%20from%20deploying%20it.%20Sometimes%20we%20would%20deem%20a%20bug%20fix%20not%20important%20enough%20for%20worldwide%20rollout%20because%20of%20the%20workload%20impact.%20With%20hot%20patching%2C%20we%20can%20now%20deploy%20bug%20fixes%20worldwide%20right%20away.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3E%3CSTRONG%3EFeatures%20available%20sooner%3C%2FSTRONG%3E%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3EEven%20with%20the%20500%2C000%2B%20functional%20tests%20that%20we%20run%20several%20times%20per%20day%20and%20thorough%20testing%20of%20every%20new%20feature%2C%20sometimes%20we%20discover%20problems%20after%20a%20new%20feature%20has%20been%20made%20available%20to%20customers.%20In%20such%20cases%2C%20we%20may%20have%20to%20disable%20the%20feature%20or%20delay%20go-live%20until%20the%20next%20scheduled%20full%20upgrade.%20With%20hot%20patching%2C%20we%20can%20fix%20the%20problem%20and%20make%20the%20feature%20available%20sooner.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%3EWe%20did%20the%20first%20hot%20patch%20in%20production%20in%202018.%20Since%20then%2C%20we%20have%20hot%20patched%20%3CEM%3Emillions%3C%2FEM%3Eof%20SQL%20Servers%20every%20month.%20Hot%20patching%20increases%20SQL%20Database%20ship%20velocity%20by%2050%25%20while%20at%20the%20same%20time%20%3CEM%3Eimproving%3C%2FEM%3Eavailability.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1010978363%22%20id%3D%22toc-hId-1010978363%22%20id%3D%22toc-hId-1010978363%22%20id%3D%22toc-hId-1010978363%22%20id%3D%22toc-hId-1010978363%22%20id%3D%22toc-hId-1010978363%22%20id%3D%22toc-hId-1010978363%22%3E3.%20Testing%20and%20Safety%3C%2FH2%3E%0A%3CP%3EHot%20patching%20%3CEM%3Eis%3C%2FEM%3Eriskier%20than%20normal%20updates%20as%20it%20introduces%20new%20kinds%20of%20pitfalls%20and%20race%20conditions.%20For%20example%2C%20both%20new%20and%20old%20code%20may%20execute%20concurrently%20during%20patching.%20Adding%20a%20field%20to%20a%20class%20would%20probably%20crash.%20This%20makes%20tooling%20support%2C%20code%20review%2C%20reasoning%20and%20extensive%20testing%20a%20must.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EClose%20collaboration%20with%20the%20Visual%20C%2B%2B%20team%20was%20crucial.%20Furthermore%2C%20we%20did%20extensive%20validation%20before%20we%20performed%20the%20first%20hot%20patch%20in%20production.%20We%E2%80%99ve%20enhanced%20SQL%20test%20infrastructure%20to%20perform%20hot%20patching%20while%20running%20our%20500%2C000%20functional%20tests.%20Additionally%2C%20we%20run%20hot%20patch%20stress%2C%20where%20we%20patch%20and%20unpatch%20millions%20of%20times%20in%20a%20loop%2C%20while%20hitting%20the%20server%20with%20stress%20workload.%20When%20we%20finally%20roll%20the%20patch%20to%20clusters%2C%20the%20rollout%20is%20monitored%20by%20the%20system%20and%20automatically%20rolls%20back%20if%20something%20unforeseen%20happens.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1541178598%22%20id%3D%22toc-hId--1541178598%22%20id%3D%22toc-hId--1541178598%22%20id%3D%22toc-hId--1541178598%22%20id%3D%22toc-hId--1541178598%22%20id%3D%22toc-hId--1541178598%22%20id%3D%22toc-hId--1541178598%22%3E%3CSPAN%3E4.%20How%20Hot%20Patching%20Works%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3ENow%2C%20let%E2%80%99s%20dive%20into%20the%20technical%20details%20of%20how%20hot%20patching%20works.%20Replacing%20code%20in%20a%20running%20process%20is%20easy%3B%20%3CFONT%20face%3D%22courier%20new%2Ccourier%22%3Ememcpy()%3C%2FFONT%3Esolves%20that.%20Doing%20it%20without%20crashingis%20not.%20A%20running%20program%20is%20program%20code%20(processor%20instructions%20%E2%80%93%20the%20contents%20of%20the%20compiled%20image%20-%20DLLs%2FEXEs)%20and%20in-memory%20state.%20The%20in-memory%20state%20consists%20of%20thread%20contexts%20and%20shared%20heap%20state.%20Thread%20context%20includes%20the%20stack%20and%20local%20variables%2C%20as%20well%20as%20the%20instruction%20pointer%20for%20the%20currently%20executing%20instruction%20and%20other%20CPU%20registers.%20The%20heap%20includes%20allocated%20memory%20for%20classes%20and%20data%20structures%2C%20such%20as%20the%20buffer%20pool%2C%20procedure%20cache%20and%20running%20query%20execution%20plans.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20we%20simply%20copied%20the%20contents%20of%20a%20new%20program%20image%20on%20top%20of%20the%20old%20one%2C%20the%20program%20would%20crash%20at%20the%20most%20basic%20level.%20With%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fcpp%2Fbuild%2Fprofile-guided-optimizations%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eprofile-guided%20optimization%3C%2FA%3E%2C%20the%20new%20program%20image%20may%20be%20very%20different%20from%20the%20old.%20Functions%20may%20not%20be%20in%20the%20same%20place.%20The%20size%20may%20not%20even%20be%20the%20same%20if%20the%20patch%20required%20inserting%20code.%20The%20(unchanged)%20instruction%20pointer%20of%20a%20thread%20executing%20in%20one%20function%20would%20now%20point%20to%20some%20other%20random%20function%20where%20execution%20would%20continue.%20It%20would%20not%20be%20atomic%20and%20return%20addresses%20on%20stacks%20would%20be%20invalid.%20At%20best%20it%20would%20crash%2C%20at%20worst%20it%20would%20corrupt%20data.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20simplify%20the%20problem%2C%20we%20only%20update%20the%20function(s)%20that%20changed%20between%20the%20old%20and%20new%20program%20code.%20The%20basic%20primitive%20offered%20by%20hot%20patching%20is%20to%20replace%20the%20body%20of%20one%20or%20more%20C%2FC%2B%2B%20functions%20in%20a%20running%20image%20with%20new%20implementation(s).%20This%20happens%20at%20runtime%2C%20while%20threads%20are%20executing%20inside%20the%20process%2C%20%3CEM%3Eincluding%20in%20the%20function(s)%20being%20replaced%3C%2FEM%3E.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20778px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F131324i372761C34B25ACE0%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Fig1%20-%20Function%20Redirection.gif%22%20title%3D%22Fig1%20-%20Function%20Redirection.gif%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFigure%201.%20Hot%20Patching%20at%20function%2Fprocedure%20level.%20To%20update%20%E2%80%9Cblah%20%2B%3D%201%E2%80%9D%20inside%20function%20Foo()%2C%20we%20load%20the%20patched%20image%20into%20memory%20and%201)%20redirect%20all%20callees%20of%20Foo()%20back%20to%20the%20old%20image%20and%202)%20redirect%20Foo()%20to%20the%20new%20image.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThe%20hot%20patching%20process%20shown%20in%20Fig%201%20above%20proceeds%20as%20follows.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3E%3CSTRONG%3EModify%20C%2B%2B%20code%3CBR%20%2F%3E%3C%2FSTRONG%3ETo%20prepare%20a%20hot%20patch%2C%20we%20first%20make%20the%20necessary%20modifications%20to%20the%20C%2B%2B%20code%20of%20the%20function(s)%20we%20want%20to%20patch%20-%20say%20%3CFONT%20face%3D%22courier%20new%2Ccourier%22%3Evoid%20Foo()%3C%2FFONT%3E.%20We%20then%20build%20a%20new%20version%20of%20the%20program%20image%20%E2%80%93%20sqlservr_patched.exe%2C%20for%20example.%20We%20now%20have%20the%20existing%20(unchanged)%20sqlservr.exe%20that%20is%20currently%20running%2C%20and%20a%20sqlservr_patched.exe%20that%20is%20sitting%20on%20disk.%20This%20is%20the%20left%20side%20of%20Figure%201.%20In%20this%20example%2C%20we%20are%20modifying%20the%20%E2%80%9C%3CFONT%20face%3D%22courier%20new%2Ccourier%22%3Eblah%20%2B%3D%201%3C%2FFONT%3E%E2%80%9D%20statement%20inside%20void%20Foo().%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3ELoad%20patched%20image%20into%20memory%3CBR%20%2F%3E%3C%2FSTRONG%3ENext%2C%20we%20load%20sqlservr_patched.exe%20into%20the%20address%20space%20of%20the%20running%20process.%20An%20EXE%20or%20DLL%20is%20just%20a%20%E2%80%9Cdata%20file%E2%80%9D%2C%20where%20the%20%E2%80%9Cdata%E2%80%9D%20is%20CPU%20instructions.%20Loading%20it%20into%20a%20section%20of%20free%20memory%20does%20not%20disturb%20the%20running%20process.%20But%20loading%20it%20into%20memory%20doesn%E2%80%99t%20patch%20anything.%20We%20need%20to%20hook%20into%20the%20live%20code%20in%20the%20process.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3ERedirect%20function%20calls%3C%2FSTRONG%3E%3CBR%20%2F%3EWe%20hook%20into%20live%20code%20by%20redirecting%20function%20calls%20targeting%20the%20old%20function(s)%20in%20sqlservr.exe%20to%20new%20replacement%20function(s)%20in%20sqlservr_patched.exe.%20Outgoing%20calls%20from%20the%20function(s)%20are%20redirected%20back%20to%20the%20old%20image%20%E2%80%93%20we%20only%20want%20to%20patch%20the%20function%20itself%2C%20not%20its%20entire%20call%20tree.%20In%20Figure%201%2C%20blue%20are%20active%20functions%20and%20grey%20are%20inactive%20functions.%26nbsp%3BIt%E2%80%99s%20important%20that%20we%20wire%20up%20the%20reverse%20redirects%20first%20in%20step%20%E2%91%A0.%20There%20are%20threads%20executing%20Foo()%2C%20so%20the%20nanosecond%20we%20redirect%20it%20in%20step%20%E2%91%A1%2C%20threads%20will%20start%20executing%20the%20new%20Foo()%20function.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EIf%20things%20go%20wrong%2C%20we%20can%20detach%20the%20patch%20by%20undoing%20step%20%E2%91%A1%2C%20without%20workload%20impact.%20Even%20hot%20patches%20can%20have%20bugs!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-201631737%22%20id%3D%22toc-hId-201631737%22%20id%3D%22toc-hId-201631737%22%20id%3D%22toc-hId-201631737%22%20id%3D%22toc-hId-201631737%22%20id%3D%22toc-hId-201631737%22%20id%3D%22toc-hId-201631737%22%3E5.%20How%20Hot%20Patching%20Works%20at%20the%20Instruction%20Level%3C%2FH2%3E%0A%3CP%3EAbove%2C%20we%20explained%20how%20we%20%E2%80%9Credirect%E2%80%9D%20function%20calls.%20But%20function%20redirection%20isn%E2%80%99t%20something%20the%20processor%20has%20any%20concept%20of%20%E2%80%93%20it%20is%20executing%20instructions.%20What%20we%20%3CEM%3Eactually%3C%2FEM%3Edo%20is%20to%20insert%20(overwrite)%20a%20JMP%20(jump)%20instruction%20at%20the%20beginning%20of%20the%20function.%20When%20a%20patched%20function%20is%20entered%20and%20the%20processor%20encounters%20this%20JMP%2C%20we%20tell%20it%20to%20jump%20to%20the%20patched%20version%20of%20the%20function.%20Since%20this%20is%20a%20jump%2C%20not%20a%20function%20call%2C%20there%20is%20no%20extra%20stack%20frame.%20This%20means%20we%20don%E2%80%99t%20do%20anything%20special%20to%20return%20from%20the%20function%20%E2%80%93%20the%20RET%20instruction%20in%20the%20patched%20function%20is%20enough.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20reduced%20compiled%20program%20size%2C%20x64%20instructions%20are%20variable%20length%20-%202%20to%2015%20bytes.%20The%20first%20byte%20or%20two%20is%20the%20op-code%20(operation%20code).%20To%20execute%20an%20instruction%2C%20the%20processor%20begins%20by%20decoding%20the%20op-code%20pointed%20to%20by%20%40rip%20(the%20current%20instruction%20pointer%20register).%20Based%20on%20the%20op-code%2C%20the%20processor%20knows%20how%20long%20the%20instruction%20is.%20After%20the%20processor%20has%20executed%20the%20instruction%2C%20it%20increments%20%40rip%20by%20the%20instruction%20length%20and%20repeats.%20The%20smallest%20far-JMP%20instruction%20we%20can%20use%20is%202%20bytes%20for%20the%20op-code%20and%204%20bytes%20for%20the%20address%20offset.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20778px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F131327i8DB6AFC553B1D196%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Fig2%20-%20Naive%20Redirection.gif%22%20title%3D%22Fig2%20-%20Naive%20Redirection.gif%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFigure%202%20Na%C3%AFve%20function%20redirection%20at%20instruction%20level%20-%20the%20first%2010%20bytes%20of%20function%20Foo()%20shown.%20Redirection%20by%20overwriting%20the%20beginning%20of%20the%20function%20with%20a%20far%20jump%20(op-code%200xFF25)%20will%20crash%20when%20%40rip%20is%20incremented%20by%205%20and%20a%20partial%20address%20is%20interpreted%20as%20an%20op-code.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThere%20may%20be%20threads%20executing%20in%20the%20function%20we%20are%20patching.%20We%20don%E2%80%99t%20freeze%20them%20because%20that%20would%20impact%20the%20workload.%20Figure%202%20shows%20what%20would%20happen%20if%20we%20overwrote%20the%20beginning%20of%20the%20function%20with%20a%206-byte%20JMP%20while%20the%20processor%20was%20in%20the%20middle%20of%20executing%20the%20now-gone%20MOV%20instruction%20at%20offset%200.%20It%20would%20increment%20%40rip%20by%205%20and%20try%20to%20decode%20the%20byte%20at%20that%20address%20as%20an%20instruction.%20That%20isn%E2%80%99t%20an%20instruction%2C%20but%20instead%20part%20of%20the%20address%20offset%20we%20filled%20in.%20The%20program%20would%20crash.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20798px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F131340i49F640B11E075D66%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFigure%203%20Function%20redirection%20with%20function%20padding%20and%20a%20trampoline.%20We%20write%20a%20near-jump%20(0xEB)%20into%20the%20function%20and%20a%20far-jump%20(0xFF25)%20into%20the%20padding.%20This%20avoids%20partial%20instruction%20execution.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ETo%20solve%20this%2C%20we%20use%20a%20trick%20called%20a%20%3CEM%3Etrampoline%20%E2%80%93%20%3C%2FEM%3Esee%20Figure%203.%20We%20compile%20(link)%20all%20SQL%20Server%20images%20with%20%3CEM%3Efunction%20padding%3C%2FEM%3E%E2%80%93%206%20bytes%20of%20empty%20space%20between%20functions.%20We%20use%20this%20space%20to%20write%20the%206-byte%20JMP%20from%20before.%20We%20then%20write%20a%20smaller%202-byte%20JMP%20instruction%20into%20the%20beginning%20of%20the%20function%20that%20jumps%20to%20the%20function%20padding%20(it%20can%20only%20jump%20to%20offsets%20%2B%2F-127%20bytes).%20The%20compiler%20guarantees%20all%20functions%20start%20with%20an%20instruction%20%26gt%3B%3D%202%20bytes.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1944442072%22%20id%3D%22toc-hId-1944442072%22%20id%3D%22toc-hId-1944442072%22%20id%3D%22toc-hId-1944442072%22%20id%3D%22toc-hId-1944442072%22%20id%3D%22toc-hId-1944442072%22%20id%3D%22toc-hId-1944442072%22%3E6.%20Functional%20Correctness%20of%20Patches%3C%2FH2%3E%0A%3CP%3EWhen%20compiling%20a%20program%2C%20the%20compiler%20guarantees%20that%20your%20program%20is%20correct%20%3CEM%3Eas%20a%20whole%3C%2FEM%3E.%20It%20%3CEM%3Edoes%20not%3C%2FEM%3Eguarantee%20that%20any%20function%20looks%20like%20the%20source%20code.%20Compiler%20optimizations%20may%2C%20for%20example%2C%20inline%20a%20function%20into%20other%20functions%2C%20remove%20an%20if-branch%20never%20taken%2C%20change%20the%20number%20of%20parameters%20and%20many%20other%20tricks.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F131280iA457E28509EFE789%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_5.png%22%20title%3D%22clipboard_image_5.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFigure%204%20Compiler%20optimizations%20create%20trouble%20for%20hot%20patching.%20In%20this%20example%2C%20the%20compiler%20removed%20code%20needed%20by%20the%20hot%20patch.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThis%20spells%20trouble%20for%20hot%20patching%20because%20those%20optimizations%20may%20change%20when%20we%20modify%20code.%20Figure%204%20shows%20an%20example.%20Here%2C%20the%20compiler%20found%20that%20nobody%20ever%20calls%20ExecuteAction(ACTION2)%2C%20so%20it%20removed%20the%20switch-case%20for%20it.%20If%20we%20patch%20PatchedFunction()%20to%20start%20using%20ACTION2%2C%20it%20won%E2%80%99t%20work%20%E2%80%93%20we%20also%20need%20to%20patch%20ExecuteAction.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20where%20collaboration%20with%20the%20Visual%20C%2B%2B%20compiler%20team%20was%20crucial.%20By%20building%20hot%20patching%20into%20the%20compiler%2C%20it%20can%20detect%20and%20correctly%20handle%20such%20cases%20and%20we%20can%20guarantee%20that%20the%20hot%20patch%20is%20functionally%20equivalent%20to%20the%20source%20code.%20%3CEM%3EInlining%3C%2FEM%3Eand%20%3CEM%3Eglobals%3C%2FEM%3Ealso%20require%20special%20consideration%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EInlining%3A%3C%2FSTRONG%3EIf%20a%20function%20Parent()%20contains%20a%20call%20to%20Foo()%2C%20the%20compiler%20may%20inline%20Foo()%20into%20Parent()%20to%20avoid%20the%20overhead%20of%20a%20function%20call.%20Patching%20Foo()%20won%E2%80%99t%20fix%20the%20copy%20inside%20Parent().%20The%20solution%20is%20to%20also%20hot%20patch%20Parent().%20Parent()%20may%20also%20have%20been%20inlined%20some%20other%20place%20%E2%80%93%20if%20so%20we%20need%20to%20patch%20that%20function%20too%2C%20recursively.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EGlobals%3A%20%3C%2FSTRONG%3EA%20global%20variable%20is%20nothing%20but%20a%20compiler-assigned%20memory%20location%20inside%20the%20compiled%20image.%20A%20reference%20to%20the%20global%20variable%20in%20code%20translates%20to%20instructions%20referencing%20this%20memory%20location.%20This%20presents%20a%20problem%20for%20hot%20patching.%20sqlservr_patched.exe%3A%3AFoo()%20would%20be%20referencing%20a%20memory%20location%20inside%20sqlservr_patched.exe%20when%20the%20location%20of%20the%20global%20at%20runtime%20is%20in%20sqlservr.exe.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20solved%20by%20the%20compiler%20transforming%20global%20references%20to%20%3CEM%3Eproxy%20references%3C%2FEM%3E.%20These%20references%20are%20patched%20up%20at%20runtime%20to%20point%20to%20the%20correct%20location.%20Figure%205%20shows%20the%20compiler%20transformation.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20613px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F131281i60FC0AE0758F7B1F%2Fimage-dimensions%2F613x147%3Fv%3D1.0%22%20width%3D%22613%22%20height%3D%22147%22%20alt%3D%22clipboard_image_6.png%22%20title%3D%22clipboard_image_6.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFigure%205%20Compiler%20transformation%20of%20globals%20to%20proxy%20references%20that%20can%20be%20patched%20to%20the%20right%20address%20at%20runtime.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--607714889%22%20id%3D%22toc-hId--607714889%22%20id%3D%22toc-hId--607714889%22%20id%3D%22toc-hId--607714889%22%20id%3D%22toc-hId--607714889%22%20id%3D%22toc-hId--607714889%22%20id%3D%22toc-hId--607714889%22%3E7.%20Closing%20Words%20and%20Next%20Steps%3C%2FH2%3E%0A%3CP%3EWith%20the%20capability%20in%20place%2C%20we%20are%20now%20working%20to%20improve%20the%20tooling%20and%20remove%20limitations%20to%20make%20more%20changes%20hot%20patchable%20with%20quick%20turnaround.%20This%20benefits%20you%20as%20a%20customer%20on%20our%20platform%20by%20allowing%20us%20to%20fix%20bugs%20and%20issues%20quickly%20without%20impacting%20your%20workload.%20For%20now%2C%20hot%20patching%20is%20only%20available%20in%20Azure%20SQL%20Database%2C%20but%20some%20day%20it%20may%20also%20come%20to%20SQL%20Server.%20Let%20us%20know%20via%20%3CA%20href%3D%22mailto%3ASQLDBArchitects%40microsoft.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESQLDBArchitects%40microsoft.com%3C%2FA%3Eif%20you%20would%20be%20interested.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20hope%20this%20gave%20you%20a%20view%20into%20some%20of%20the%20exciting%20work%20we%20are%20doing.%20While%20this%20barely%20scratches%20the%20surface%20of%20hot%20patching%2C%20please%20leave%20comments%20and%20questions%20below%20or%20contact%20us%20on%20the%20email%20above%20if%20you%20would%20like%20to%20see%20more%20in-depth%20coverage%20of%20cool%20technology%20we%20work%20on.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-849700%22%20slang%3D%22en-US%22%3E%3CP%3ERead%20about%20how%20we%20patch%20(modify%20C%2B%2B%20code%20in)%20SQL%20Server%20engine%20without%20restarting%20the%20sqlservr.exe%20process%2C%20by%20manipulating%20CPU%20instructions%20in%20memory%20with%20live%20workload%20on%20the%20system!%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F131349iCE582F2B515A02BE%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-849700%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EInternals%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMaintenance%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-859673%22%20slang%3D%22en-US%22%3ERe%3A%20Hot%20Patching%20SQL%20Server%20Engine%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-859673%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F380947%22%20target%3D%22_blank%22%3E%40m60freeman%3C%2FA%3E%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWest%20Europe%20is%20in%20the%20Netherlands%2C%20which%20is%20following%20Central%20European%20Time%20-%20CET%20(UTC%2B1)%20in%20the%20winter%20and%20Central%20European%20Summer%20Time%20-%20CEST%20(UTC%2B2)%20in%20the%20summer.%20The%20notification%20you%20pasted%20above%20is%20for%2015%3A00%20UTC%2C%20which%20is%2017%3A00%20or%205pm%20in%20local%20time%20(CEST).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20Hans%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-859720%22%20slang%3D%22en-US%22%3ERe%3A%20Hot%20Patching%20SQL%20Server%20Engine%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-859720%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F405799%22%20target%3D%22_blank%22%3E%40norheim%3C%2FA%3E%26nbsp%3BThanks%20for%20the%20additional%20information.%20That%20makes%20me%20feel%20a%20lot%20better!%20I%20didn't%20realize%20the%20EU%20time%20zones%20moved%20forward%20an%20hour%20in%20the%20summer%20instead%20of%20back%20an%20hour%20like%20in%20the%20U.S.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-872095%22%20slang%3D%22en-US%22%3ERe%3A%20Hot%20Patching%20SQL%20Server%20Engine%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-872095%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20process%2C%20how%20you%20%22stay%20under%20the%20radar%22%20with%20anti-virus%20and%20advanced%20threat%20protection%20software.%26nbsp%3B%20I%20would%20think%20this%20process%20could%20easily%20be%20interpreted%20as%20an%20attack.%20%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-873213%22%20slang%3D%22en-US%22%3ERe%3A%20Hot%20Patching%20SQL%20Server%20Engine%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-873213%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F414247%22%20target%3D%22_blank%22%3E%40Trayce_Jordan%3C%2FA%3E%26nbsp%3B%2C%20if%20you%20execute%20this%20procedure%20from%20application%20which%20was%20configure%20in%20the%20white%20list%20of%20the%20%3CSPAN%3Eanti-virus%20app%20then%20what%20is%20the%20issue%3F%20Any%26nbsp%3Banti-virus%20app%20today%20has%20options%20to%20configure%20white%20list%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-873292%22%20slang%3D%22en-US%22%3ERe%3A%20Hot%20Patching%20SQL%20Server%20Engine%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-873292%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F414247%22%20target%3D%22_blank%22%3E%40Trayce_Jordan%3C%2FA%3E%26nbsp%3BThe%20actions%20we%20perform%20are%20of%20similar%20nature%20as%20what%20a%20debugger%20would%20do%2C%20so%20it's%20not%20necessarily%20the%20case%20that%20antivirus%20software%20would%20conclude%20it's%20malware.%20In%20either%20case%2C%20an%20exclusion%20can%20be%20registered%20as%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F9067%22%20target%3D%22_blank%22%3E%40R%20A%3C%2FA%3E%26nbsp%3Bpointed%20out.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

Author: Hans Olav Norheim, hansnor@microsoft.com

Technical reviewers: Jim Cavalaris, Jimmy Cleary, Jim Radigan, Ashish Kishan Mansukhani, Alexey Yeltsov, Brian Gianforcaro, Jasraj Dange, Jon Jahren, Michael Nelson, Peter Carlin

 

In the world of cloud database services, few things are more important to customers than having uninterrupted access to their data.  In industries like online gaming and financial services that experience high transaction rates, even the smallest interruptions can potentially impact the end-user’s experience.  Azure SQL Database is evergreen, meaning that it always has the latest version of the SQL engine, but maintaining this evergreen state requires periodic updates to the service that can take the database offline for a second.  For this reason, our engineering team is continuously working on innovative technology improvements that reduce workload interruption.

 

Today’s post, in collaboration with the Visual C++ Compiler team, covers how we patch SQL Server Engine without impacting workload at all.

 

image.pngThis is what hot patching looks like under the covers. Read on if you want all the details!

1. The Challenge

The SQL Engine we are running in Azure SQL Database is the very latest version of the same engine customers run on their own servers, except we manage and update it. To update SQL Server or the underlying infrastructure (i.e. Service Fabric or the operating system), we must stop the SQL Server process. If that process hosts the primary database replica, we move the replica to another machine (requiring a failover).

 

During failover, the database may be offline for a second and still meet our 99.995% SLA. However,  failover of the primary replica impacts workload because it aborts in-flight queries and transactions. We built features such as resumable index (re)build and accelerated database recovery to address these situations, but not all running operations are automatically resumable. It may be expensive to restart complex queries or transactions that were aborted due to an upgrade. So even though failovers are quick, we want to avoid them.

 

SQL Server and the overall Azure platform invests significant engineering effort into platform availability and reliability. We can, for example, update the Azure host without restarting Guest VMs. In SQL DB, we have multiple replicas of every database. During upgrade, we ensure that hot standbys are available to take over immediately.

 

We’ve worked closely with the broader Azure and Service Fabric teams to minimize the number of failovers. When we first decide to fail over a database for upgrade, we apply updates to all components in the stack at the same time: OS, Service Fabric and SQL Server. We have automatic scheduling that avoids deploying during an Azure region’s core business hours. Just before failover, we attempt to drain active transactions to avoid aborting them. We even utilize database workload patterns to perform failover at the best time for the workload.

 

Even with all that, we don’t get away from the fact that to update SQL Engine to a new version, we must restart the process and fail over the database’s primary replica at least once. Or do we?

 

2. Hot Patching and Results

Hot patching is modifying in-memory code in a running process without restarting the process. In our case, it gives us the capability to modify C++ code in SQL Engine without restarting sqlservr.exe. Since we don’t restart, we don’t fail over the primary replica and interrupt the workload. We don't even need to pause SQL Server activity while we patch. Hot patching is unnoticed by the user workload (other than the patch payload, of course)!

 

Hot patching does not replace traditional, restarting upgrades – it complements them. Hot patching currently has limitations that make it unsuitable when there are a large number of changes, such as when a major new feature is introduced. But it is perfect for smaller, targeted changes. More than 80% of typical SQL bug fixes are hot patchable. Benefits of hot patching include:

 

  • Reduced workload disruption
    No restart means no database failover and no workload impact.
  • Faster bug fixes
    Previously, we weighed the urgency of a bug fix vs. impact on customer workloads from deploying it. Sometimes we would deem a bug fix not important enough for worldwide rollout because of the workload impact. With hot patching, we can now deploy bug fixes worldwide right away.
  • Features available sooner
    Even with the 500,000+ functional tests that we run several times per day and thorough testing of every new feature, sometimes we discover problems after a new feature has been made available to customers. In such cases, we may have to disable the feature or delay go-live until the next scheduled full upgrade. With hot patching, we can fix the problem and make the feature available sooner.

We did the first hot patch in production in 2018. Since then, we have hot patched millions of SQL Servers every month. Hot patching increases SQL Database ship velocity by 50% while at the same time improving availability.

 

3. Testing and Safety

Hot patching is riskier than normal updates as it introduces new kinds of pitfalls and race conditions. For example, both new and old code may execute concurrently during patching. Adding a field to a class would probably crash. This makes tooling support, code review, reasoning and extensive testing a must.

 

Close collaboration with the Visual C++ team was crucial. Furthermore, we did extensive validation before we performed the first hot patch in production. We’ve enhanced SQL test infrastructure to perform hot patching while running our 500,000 functional tests. Additionally, we run hot patch stress, where we patch and unpatch millions of times in a loop, while hitting the server with stress workload. When we finally roll the patch to clusters, the rollout is monitored by the system and automatically rolls back if something unforeseen happens.

 

4. How Hot Patching Works

Now, let’s dive into the technical details of how hot patching works. Replacing code in a running process is easy; memcpy() solves that. Doing it without crashing is not. A running program is program code (processor instructions – the contents of the compiled image - DLLs/EXEs) and in-memory state. The in-memory state consists of thread contexts and shared heap state. Thread context includes the stack and local variables, as well as the instruction pointer for the currently executing instruction and other CPU registers. The heap includes allocated memory for classes and data structures, such as the buffer pool, procedure cache and running query execution plans.

 

If we simply copied the contents of a new program image on top of the old one, the program would crash at the most basic level. With profile-guided optimization, the new program image may be very different from the old. Functions may not be in the same place. The size may not even be the same if the patch required inserting code. The (unchanged) instruction pointer of a thread executing in one function would now point to some other random function where execution would continue. It would not be atomic and return addresses on stacks would be invalid. At best it would crash, at worst it would corrupt data.

 

To simplify the problem, we only update the function(s) that changed between the old and new program code. The basic primitive offered by hot patching is to replace the body of one or more C/C++ functions in a running image with new implementation(s). This happens at runtime, while threads are executing inside the process, including in the function(s) being replaced.

Fig1 - Function Redirection.gifFigure 1. Hot Patching at function/procedure level. To update “blah += 1” inside function Foo(), we load the patched image into memory and 1) redirect all callees of Foo() back to the old image and 2) redirect Foo() to the new image.

The hot patching process shown in Fig 1 above proceeds as follows.

  1. Modify C++ code
    To prepare a hot patch, we first make the necessary modifications to the C++ code of the function(s) we want to patch - say void Foo(). We then build a new version of the program image – sqlservr_patched.exe, for example. We now have the existing (unchanged) sqlservr.exe that is currently running, and a sqlservr_patched.exe that is sitting on disk. This is the left side of Figure 1. In this example, we are modifying the “blah += 1” statement inside void Foo().
  2. Load patched image into memory
    Next, we load sqlservr_patched.exe into the address space of the running process. An EXE or DLL is just a “data file”, where the “data” is CPU instructions. Loading it into a section of free memory does not disturb the running process. But loading it into memory doesn’t patch anything. We need to hook into the live code in the process.
  3. Redirect function calls
    We hook into live code by redirecting function calls targeting the old function(s) in sqlservr.exe to new replacement function(s) in sqlservr_patched.exe. Outgoing calls from the function(s) are redirected back to the old image – we only want to patch the function itself, not its entire call tree. In Figure 1, blue are active functions and grey are inactive functions. It’s important that we wire up the reverse redirects first in step ①. There are threads executing Foo(), so the nanosecond we redirect it in step ②, threads will start executing the new Foo() function.

If things go wrong, we can detach the patch by undoing step ②, without workload impact. Even hot patches can have bugs!

 

5. How Hot Patching Works at the Instruction Level

Above, we explained how we “redirect” function calls. But function redirection isn’t something the processor has any concept of – it is executing instructions. What we actually do is to insert (overwrite) a JMP (jump) instruction at the beginning of the function. When a patched function is entered and the processor encounters this JMP, we tell it to jump to the patched version of the function. Since this is a jump, not a function call, there is no extra stack frame. This means we don’t do anything special to return from the function – the RET instruction in the patched function is enough.

 

For reduced compiled program size, x64 instructions are variable length - 2 to 15 bytes. The first byte or two is the op-code (operation code). To execute an instruction, the processor begins by decoding the op-code pointed to by @rip (the current instruction pointer register). Based on the op-code, the processor knows how long the instruction is. After the processor has executed the instruction, it increments @rip by the instruction length and repeats. The smallest far-JMP instruction we can use is 2 bytes for the op-code and 4 bytes for the address offset.

Fig2 - Naive Redirection.gifFigure 2 Naïve function redirection at instruction level - the first 10 bytes of function Foo() shown. Redirection by overwriting the beginning of the function with a far jump (op-code 0xFF25) will crash when @rip is incremented by 5 and a partial address is interpreted as an op-code.

There may be threads executing in the function we are patching. We don’t freeze them because that would impact the workload. Figure 2 shows what would happen if we overwrote the beginning of the function with a 6-byte JMP while the processor was in the middle of executing the now-gone MOV instruction at offset 0. It would increment @rip by 5 and try to decode the byte at that address as an instruction. That isn’t an instruction, but instead part of the address offset we filled in. The program would crash.

image.pngFigure 3 Function redirection with function padding and a trampoline. We write a near-jump (0xEB) into the function and a far-jump (0xFF25) into the padding. This avoids partial instruction execution.

To solve this, we use a trick called a trampoline – see Figure 3. We compile (link) all SQL Server images with function padding – 6 bytes of empty space between functions. We use this space to write the 6-byte JMP from before. We then write a smaller 2-byte JMP instruction into the beginning of the function that jumps to the function padding (it can only jump to offsets +/-127 bytes). The compiler guarantees all functions start with an instruction >= 2 bytes.

 

6. Functional Correctness of Patches

When compiling a program, the compiler guarantees that your program is correct as a whole. It does not guarantee that any function looks like the source code. Compiler optimizations may, for example, inline a function into other functions, remove an if-branch never taken, change the number of parameters and many other tricks.

 

clipboard_image_5.pngFigure 4 Compiler optimizations create trouble for hot patching. In this example, the compiler removed code needed by the hot patch.

This spells trouble for hot patching because those optimizations may change when we modify code. Figure 4 shows an example. Here, the compiler found that nobody ever calls ExecuteAction(ACTION2), so it removed the switch-case for it. If we patch PatchedFunction() to start using ACTION2, it won’t work – we also need to patch ExecuteAction.

 

This is where collaboration with the Visual C++ compiler team was crucial. By building hot patching into the compiler, it can detect and correctly handle such cases and we can guarantee that the hot patch is functionally equivalent to the source code. Inlining and globals also require special consideration:

 

Inlining: If a function Parent() contains a call to Foo(), the compiler may inline Foo() into Parent() to avoid the overhead of a function call. Patching Foo() won’t fix the copy inside Parent(). The solution is to also hot patch Parent(). Parent() may also have been inlined some other place – if so we need to patch that function too, recursively.

 

Globals: A global variable is nothing but a compiler-assigned memory location inside the compiled image. A reference to the global variable in code translates to instructions referencing this memory location. This presents a problem for hot patching. sqlservr_patched.exe::Foo() would be referencing a memory location inside sqlservr_patched.exe when the location of the global at runtime is in sqlservr.exe.

 

This is solved by the compiler transforming global references to proxy references. These references are patched up at runtime to point to the correct location. Figure 5 shows the compiler transformation.

clipboard_image_6.pngFigure 5 Compiler transformation of globals to proxy references that can be patched to the right address at runtime.

7. Closing Words and Next Steps

With the capability in place, we are now working to improve the tooling and remove limitations to make more changes hot patchable with quick turnaround. This benefits you as a customer on our platform by allowing us to fix bugs and issues quickly without impacting your workload. For now, hot patching is only available in Azure SQL Database, but some day it may also come to SQL Server. Let us know via SQLDBArchitects@microsoft.com if you would be interested.

 

We hope this gave you a view into some of the exciting work we are doing. While this barely scratches the surface of hot patching, please leave comments and questions below or contact us on the email above if you would like to see more in-depth coverage of cool technology we work on.

11 Comments
R A
MVP

Great post! The idea is very interesting and you explained the meaning of "hot patching" great. You provide the insights on the concept "Hot Patching" and the architecture, but after reading the post I feel that I got nothing which I can take with me and implement.

 

As you said "this barely scratches the surface of hot patching", so it is time for some C/C++ code to show how do you redirect function calls targeting the old function(s) to new replacement function(s). I know how to load classes dynamically and add functions for example but how do you redirect the call for existing function (in practical)? Can you provide a link maybe to give a starting point for developers?

 

I loved the idea, and now I would like to implement same procedure in my app for example, but you do not give me any starting point. I want to see some code of what you do and not just the explanation of how it is done in general :smile:.

 

>> Let us know via SQLDBArchitects@microsoft.com if you would be interested.

Not clear to me "interesting in what".  You mean interesting to have the same procedure in SQL Server on-premises? If so then obviously it sound awesome.

Established Member

This is very impressive. However, there is one statement that isn't quite true: "We have automatic scheduling that avoids deploying during an Azure region’s core business hours." Deployments routinely occur at 4pm or even 3pm in any given region. Azure SQL Database updates occurred today in North Europe (Ireland) at 4pm Ireland time, as one example: "This notification is for upcoming scheduled maintenance to your Azure SQL Database instance(s) in North Europe. This is scheduled to occur between 16:00 UTC on 14 Sep 2019 and 07:00 UTC on 15 Sep 2019." I suppose it depends on how you define "core business hours".

Microsoft

Hi @R A - Thanks for the feedback!

 

The C code we use to perform the patching (instruction overwriting) is not too complicated and does pretty much what is described above. It goes roughly like this:

  1. Load the patched image using LoadLibrary()
  2. Find the module base address of the image to be patched.
  3. Use the pre-computed function offset(s) within each image to acquire a void* to where the functions start.
  4. Deref the pointers and write JMP instructions directly to the memory address.

But it's only a small piece of the puzzle. The hardest part of hot patching is to prove that redirecting a function will do the right thing. For example, is it referencing other functions or globals, has it been inlined, are compiler optimizations going to cause trouble etc. This is what I briefly touch on in section 6, but this topic is so large that one could write a PhD thesis about it.

 

So I had to limit the scope to not turn the blog post into a book :) Given that the scope of doing it safely is beyond what I can cover here, I didn't want to include examples of doing something that would almost certainly go wrong.

 

But it's great to know that there are folks out there that want more technical details. Maybe I'll do a follow-up post later.

 

- Hans

Microsoft

Hi @m60freeman - thanks for the feedback!

 

Core business hours is defined as 08:00 (8am) to 17:00 (5pm), local time. In your example below, 16:00 UTC is actually 17:00 local time because during daylight savings time (DST), GMT standard time is UTC+1 hour, not UTC. You can see that on the end time - the deployment stops by 7am UTC, which is 8am local time.

 

Let me know if you have further questions.

 

- Hans

R A
MVP

Thanks :thumbs_up:

Established Member

@norheim Here is a recent notice for West Europe for 15:00 UTC, and there have been many more like it:

 

TRACKING ID:
LKJH-5Z0

TYPE:
Maintenance

STATUS:
Rescheduled 8/31/2019 6:41:07 AM (UTC)

COMMUNICATION:

This notification is for previously scheduled maintenance to your Azure SQL Database instance(s) in West Europe. Unfortunately, the maintenance is taking longer than expected. The maintenance is currently paused and will be resumed at 15:00 UTC on 31 Aug 2019 and is expected to complete by 06:00 UTC on 01 Sep 2019. Feel free to refer to your portal for more information (https://aka.ms/servicehealthpm) and for any additional questions, please contact support.

Resource Group

  

Server Name

  

Database Name / Elastic Pool

redacted

 

redacted

 

redacted

IMPACTED SERVICE(S) AND REGION(S)

Service Name

Region

SQL Database

West Europe

Microsoft

Hi @m60freeman,

 

West Europe is in the Netherlands, which is following Central European Time - CET (UTC+1) in the winter and Central European Summer Time - CEST (UTC+2) in the summer. The notification you pasted above is for 15:00 UTC, which is 17:00 or 5pm in local time (CEST).

 

- Hans

Established Member

@norheim Thanks for the additional information. That makes me feel a lot better! I didn't realize the EU time zones moved forward an hour in the summer instead of back an hour like in the U.S.

Occasional Visitor

In this process, how you "stay under the radar" with anti-virus and advanced threat protection software.  I would think this process could easily be interpreted as an attack.   

R A
MVP

@Trayce_Jordan , if you execute this procedure from application which was configure in the white list of the anti-virus app then what is the issue? Any anti-virus app today has options to configure white list

Microsoft

@Trayce_Jordan The actions we perform are of similar nature as what a debugger would do, so it's not necessarily the case that antivirus software would conclude it's malware. In either case, an exclusion can be registered as @R A pointed out.