123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138(****************************************************************************)(* *)(* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *)(* *)(* Copyright (C) 2017-2022 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/>. *)(* *)(****************************************************************************)openMopsaopenSig.Abstraction.DomainopenUniversal.AstopenC.AstopenC.Common.Points_toopenToptype('a,_)query+=Q_cpython_attached_callstack:addr->('a,Callstack.callstack)querylet()=register_query{join=(letf:typear.query_pool->(a,r)query->r->r->r=funnextqueryab->matchquerywith|Q_cpython_attached_callstack_->assertfalse|_->next.pool_joinqueryabinf);meet=(letf:typear.query_pool->(a,r)query->r->r->r=funnextqueryab->matchquerywith|Q_cpython_attached_callstack_->assertfalse|_->next.pool_meetqueryabinf)}moduleDomain=structmoduleCallstacks=structmoduleCallstackSet=Framework.Lattices.Powerset.Make(structtypet=Callstack.callstackletcompare=Callstack.compare_callstackletprint=unformatpp_callstackend)includeCallstackSetletmax_size=1letbound(x:t):t=matchxwith|NtswhenSet.cardinals<=max_size->x|_->TOPletjoina1a2=CallstackSet.joina1a2|>boundletaddvt=addvt|>boundendmoduleCallstackMap=Framework.Lattices.Partial_map.Make(Addr)(Callstacks)includeCallstackMapincludeFramework.Core.Id.GenDomainId(structtypenonrect=tletname="cpython.callstack_tracking"end)letchecks=[]letinit_manflow=Hashtbl.addC.Common.Builtins.builtin_functions"_mopsa_pyerr_bind_cs_to"();set_envT_curemptymanflow|>Option.someletevalexpmanflow=letrange=erangeexpinmatchekindexpwith|E_c_builtin_call("_mopsa_pyerr_bind_cs_to",[exc])->resolve_pointerexcmanflow>>$(funptflow->matchptwith|P_block({base_kind=Addra},_,_)->get_envT_curmanflow>>$funcurflow->set_envT_cur(adda(Callstacks.singleton(Flow.get_callstackflow))cur)manflow>>%funflow->Eval.singleton(mk_onerange)flow|_->assertfalse)|>OptionExt.return|_->Noneletexecstmtmanflow=matchskindstmtwith|S_freeaddr->get_envT_curmanflow>>$?funcurflow->letpost=ifmemaddrcurthenset_envT_cur(removeaddrcur)manflowelsePost.returnflowinpost>>%man.exec~route:(Belowname)stmt|>OptionExt.return|_->Noneletask:typer.('a,r)query->('a,t)man->'aflow->('a,r)casesoption=funquerymanflow->matchquerywith|Q_cpython_attached_callstacka->get_envT_curmanflow>>$?funcurflow->OptionExt.lift(funcs->assert(Callstacks.cardinalcs=1);Cases.singleton(Callstacks.choosecs)flow)(find_optacur)|_->Noneletprint_expr____=()letprint_stateprintera=pprint~path:[Key"C/Python callstack tracking"]printer(pboxCallstackMap.printa)letmerge___=assertfalseendlet()=register_standard_domain(moduleDomain)