123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226(****************************************************************************)(* *)(* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *)(* *)(* Copyright (C) 2017-2019 The MOPSA Project. *)(* *)(* This program is free software: you can redistribute it and/or modify *)(* it under the terms of the GNU Lesser General Public License as published *)(* by the Free Software Foundation, either version 3 of the License, or *)(* (at your option) any later version. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Lesser General Public License for more details. *)(* *)(* You should have received a copy of the GNU Lesser General Public License *)(* along with this program. If not, see <http://www.gnu.org/licenses/>. *)(* *)(****************************************************************************)(** Hooks are modules that can observe the execution of the transfer
functions without modifying their output. They can be used however to enrich the
analysis by adding information to the context.
*)openAst.StmtopenAst.ExpropenLatticeopenFlowopenContextopenPostopenEvalopenRouteopenManageropenAst.SemanticopenMopsa_utilsmoduletypeHOOK=sigvalname:stringvalinit:'actx->'actxvalon_before_exec:route->stmt->('a,'a)man->'aflow->'actxoptionvalon_after_exec:route->stmt->('a,'a)man->'aflow->'apost->'actxoptionvalon_before_eval:route->semantic->expr->('a,'a)man->'aflow->'actxoptionvalon_after_eval:route->semantic->expr->('a,'a)man->'aflow->'aeval->'actxoptionvalon_finish:('a,'a)man->'aflow->unitendmoduletypeSTATELESS_HOOK=sigvalname:stringvalinit:'actx->unitvalon_before_exec:route->stmt->('a,'a)man->'aflow->unitvalon_after_exec:route->stmt->('a,'a)man->'aflow->'apost->unitvalon_before_eval:route->semantic->expr->('a,'a)man->'aflow->unitvalon_after_eval:route->semantic->expr->('a,'a)man->'aflow->'aeval->unitvalon_finish:('a,'a)man->'aflow->unitendmoduleMakeStatefulHook(Hook:STATELESS_HOOK):HOOK=structletname=Hook.nameletinitctx=Hook.initctx;ctxleton_before_execroutestmtmanflow=Hook.on_before_execroutestmtmanflow;Noneleton_after_execroutestmtmanflowpost=Hook.on_after_execroutestmtmanflowpost;Noneleton_before_evalroutesemanticexpmanflow=Hook.on_before_evalroutesemanticexpmanflow;Noneleton_after_evalroutesemanticexpmanfloweval=Hook.on_after_evalroutesemanticexpmanfloweval;Noneleton_finish=Hook.on_finishend(** Registered hooks *)lethooks:(string,(moduleHOOK))Hashtbl.t=Hashtbl.create16(** Active hooks *)letactive_hooks:(string,(moduleHOOK))Hashtbl.t=Hashtbl.create16(** Initialized hooks *)letinitialized_hooks:(string,(moduleHOOK))Hashtbl.t=Hashtbl.create16(** Register a new hook *)letregister_hookhook=letmoduleH=(valhook:HOOK)inHashtbl.addhooksH.namehook(** Register a new stateless hook *)letregister_stateless_hookhook=letmoduleH=(valhook:STATELESS_HOOK)inHashtbl.addhooksH.name(moduleMakeStatefulHook(H))(** Check whether a hook exists *)letmem_hookname:bool=Hashtbl.memhooksname(** Find a hook by name *)letfind_hook(name:string):(moduleHOOK)=Hashtbl.findhooksnameletlist_hooks():(moduleHOOK)list=Hashtbl.fold(fun_hl->h::l)hooks[](** Initialize internals *)letinit()=()(** Initialize an active hook *)letinit_hookhookctx=ifHashtbl.meminitialized_hookshookthenctxelseifnot(Hashtbl.memactive_hookshook)thenExceptions.panic"Inactive hook %s cannot be initialized"hookelseleth=find_hookhookinletmoduleH=(valh:HOOK)inlet()=Hashtbl.addinitialized_hookshook(moduleH)inH.initctxletis_hook_activen:bool=Hashtbl.memactive_hooksn(** Initialize all active hooks *)letinit_active_hooksctx=Hashtbl.fold(funnamehookctx->init_hooknamectx)active_hooksctx(** Activate a registered hook *)letactivate_hookname=letmoduleH=(valfind_hookname)inHashtbl.addactive_hooksname(moduleH)(** Deactivate an active hook *)letdeactivate_hooknamemanflow=ifnot(Hashtbl.memactive_hooksname)then()elseleth=Hashtbl.findactive_hooksnameinletmoduleH=(valh:HOOK)inH.on_finishmanflow;Hashtbl.removeactive_hooksname;Hashtbl.removeinitialized_hooksname(** Fire [on_before_exec] event *)leton_before_execroutestmtmanflow=Hashtbl.fold(funnamehookacc->letflow=matchaccwith|None->flow|Somectx->Flow.set_ctxctxflowinletmoduleH=(valhook:HOOK)inmatchH.on_before_execroutestmtmanflowwith|None->acc|x->x)active_hooksNone(** Fire [on_after_exec] event *)leton_after_execroutestmtmanflowpost=Hashtbl.fold(funnamehookacc->letpost=matchaccwith|None->post|Somectx->Cases.set_ctxctxpostinletmoduleH=(valhook:HOOK)inmatchH.on_after_execroutestmtmanflowpostwith|None->acc|x->x)active_hooksNone(** Fire [on_before_eval] event *)leton_before_evalroutesemanticexpmanflow=Hashtbl.fold(funnamehookacc->letflow=matchaccwith|None->flow|Somectx->Flow.set_ctxctxflowinletmoduleH=(valhook:HOOK)inmatchH.on_before_evalroutesemanticexpmanflowwith|None->acc|x->x)active_hooksNone(** Fire [on_after_eval] event *)leton_after_evalroutesemanticexpmanflowevl=Hashtbl.fold(funnamehookacc->letevl=matchaccwith|None->evl|Somectx->Cases.set_ctxctxevlinletmoduleH=(valhook:HOOK)inmatchH.on_after_evalroutesemanticexpmanflowevlwith|None->acc|x->x)active_hooksNoneleton_finishmanflow=Hashtbl.iter(funnamehook->deactivate_hooknamemanflow)active_hooks