123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162(****************************************************************************)(* *)(* 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/>. *)(* *)(****************************************************************************)(** Heap addresses *)openMopsa_utilsopenVar(** Kind of heap addresses, used to store extra information. *)typeaddr_kind=..letaddr_kind_compare_chain:(addr_kind->addr_kind->int)ref=ref(funa1a2->comparea1a2)letaddr_kind_pp_chain:(Format.formatter->addr_kind->unit)ref=ref(funfmta->Exceptions.panic"addr_kind_pp_chain: unknown address")letpp_addr_kindfmtak=!addr_kind_pp_chainfmtakletcompare_addr_kindak1ak2=ifak1==ak2then0else!addr_kind_compare_chainak1ak2letregister_addr_kind(info:addr_kindTypeExt.info)=addr_kind_compare_chain:=info.compare!addr_kind_compare_chain;addr_kind_pp_chain:=info.print!addr_kind_pp_chain;()(** Addresses are grouped by static criteria to make them finite *)typeaddr_partitioning=..typeaddr_partitioning+=|G_all(** Group all addresses into one *)letaddr_partitioning_compare_chain:(addr_partitioning->addr_partitioning->int)ref=ref(funa1a2->comparea1a2)letaddr_partitioning_pp_chain:(Format.formatter->addr_partitioning->unit)ref=ref(funfmtg->matchgwith|G_all->Format.pp_print_stringfmt"*"|_->Exceptions.panic"pp_addr_partitioning: not registered")(** Command line option to use hashes as address format *)letopt_hash_addr=reffalseletpp_addr_partitioning_hashfmt(g:addr_partitioning)=lets=Format.asprintf"%a"!addr_partitioning_pp_chainginletmd5=Digest.strings|>Digest.to_hexinFormat.pp_print_stringfmt(String.submd507)(** Print a partitioning policy. Flag [full] overloads the option
[opt_hash_addr] and displays the full partitioning string (not its hash,
which is useful for creating unique names of addresses) *)letpp_addr_partitioning?(full=false)fmtak=matchakwith|G_all->!addr_partitioning_pp_chainfmtak|_->if!opt_hash_addr&¬fullthenpp_addr_partitioning_hashfmtakelse!addr_partitioning_pp_chainfmtakletpp_addr_partitioning_fullfmtak=!addr_partitioning_pp_chainfmtakletcompare_addr_partitioninga1a2=ifa1==a2then0else!addr_partitioning_compare_chaina1a2letregister_addr_partitioning(info:addr_partitioningTypeExt.info)=addr_partitioning_compare_chain:=info.compare!addr_partitioning_compare_chain;addr_partitioning_pp_chain:=info.print!addr_partitioning_pp_chain;()(** Heap addresses. *)typeaddr={addr_kind:addr_kind;(** Kind of the address. *)addr_partitioning:addr_partitioning;(** Partitioning policy of the address *)addr_mode:mode;(** Assignment mode of address (string or weak) *)}letakindaddr=addr.addr_kindletpp_addrfmta=Format.fprintffmt"@@%a%a%s"pp_addr_kinda.addr_kind(funfmt->function|G_all->()|p->Format.fprintffmt":%a"(pp_addr_partitioning~full:false)p)a.addr_partitioning(matcha.addr_modewithWEAK->":w"|STRONG->"")(** Get the unique name of an address. This is safer and faster than calling
[Format.asprintf "%s" pp_addr a] when [opt_hash_addr] is set. *)letaddr_uniq_namea=Format.asprintf"@@%a%a%s"pp_addr_kinda.addr_kind(funfmt->function|G_all->()|p->Format.fprintffmt":%a"(pp_addr_partitioning~full:true)p)a.addr_partitioning(matcha.addr_modewithWEAK->":w"|STRONG->"")letcompare_addrab=ifa==bthen0elseCompare.compose[(fun()->compare_addr_kinda.addr_kindb.addr_kind);(fun()->compare_addr_partitioninga.addr_partitioningb.addr_partitioning);(fun()->compare_modea.addr_modeb.addr_mode);]letaddr_mode(a:addr)(omode:modeoption):mode=matchomodewith|None->a.addr_mode|Somem->m(** Address variables *)typevar_kind+=|V_addr_attrofaddr*stringlet()=register_var{compare=(funnextv1v2->matchvkindv1,vkindv2with|V_addr_attr(a1,attr1),V_addr_attr(a2,attr2)->Compare.compose[(fun()->compare_addra1a2);(fun()->compareattr1attr2)]|_->nextv1v2);print=(funnextfmtv->matchvkindvwith|V_addr_attr(addr,attr)->Format.fprintffmt"%a.%s"pp_addraddrattr|_->nextfmtv)}letmk_addr_attraddrattrtyp=letname=Format.asprintf"%s.%s"(addr_uniq_nameaddr)attrinmkvname(V_addr_attr(addr,attr))~mode:addr.addr_modetyp