123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159(**************************************************************************)(* This file is part of BINSEC. *)(* *)(* Copyright (C) 2016-2026 *)(* CEA (Commissariat à l'énergie atomique et aux énergies *)(* alternatives) *)(* *)(* 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, version 2.1. *)(* *)(* It 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. *)(* *)(* See the GNU Lesser General Public License version 2.1 *)(* for more details (enclosed in the file licenses/LGPLv2.1). *)(* *)(**************************************************************************)typeerror=Undefined|Unimplemented|Unsupported|FailuremoduleOpcode=Basic_types.StringmoduleStatistics=structtypet={decoded:intOpcode.Htbl.t;undefined:intOpcode.Htbl.t;unimplemented:intOpcode.Htbl.t;unsupported:intOpcode.Htbl.t;failure:intOpcode.Htbl.t;}letempty()={decoded=Opcode.Htbl.create17;undefined=Opcode.Htbl.create7;unimplemented=Opcode.Htbl.create7;unsupported=Opcode.Htbl.create7;failure=Opcode.Htbl.create7;}letadd_bytestblopcode=matchOpcode.Htbl.findtblopcodewith|exceptionNot_found->Opcode.Htbl.addtblopcode1|n->Opcode.Htbl.replacetblopcode(n+1)letsumtbl=Opcode.Htbl.fold(fun_nr->n+r)tbl0letppppf{decoded;undefined;unimplemented;unsupported;failure}=Format.fprintfppf"@[<v 0>Decoded (unique): %d (%d)@ Undefined (unique): %d (%d)@ \
Unimplemented (unique): %d (%d)@ Unsupported (unique): %d (%d)@ Misc \
errors (unique): %d (%d)@]"(sumdecoded)(Opcode.Htbl.lengthdecoded)(sumundefined)(Opcode.Htbl.lengthundefined)(sumunimplemented)(Opcode.Htbl.lengthunimplemented)(sumunsupported)(Opcode.Htbl.lengthunsupported)(sumfailure)(Opcode.Htbl.lengthfailure)end(* Create a dummy instruction.
This is used for "unfailing" mode where something is always returned, even in
cases of Parser.Error.
*)letempty_instruction=Instruction.Generic.create0""(Mnemonic.unsupported())letdie=Dhunk.singleton(Dba.Instr.stop(SomeDba.KO))moduleMake(L:Logger.S)=structletstats=Statistics.empty()letpp_statisticsppf()=Statistics.ppppfstatsletincr_successopcode=Statistics.add_bytesstats.decodedopcodeletincr_errorerropcode=matcherrwith|Undefined->Statistics.add_bytesstats.undefinedopcode|Unimplemented->Statistics.add_bytesstats.unimplementedopcode|Unsupported->Statistics.add_bytesstats.unsupportedopcode|Failure->Statistics.add_bytesstats.failureopcodeletfindkeykvs=List.assockeykvs(* Some conversion functions from parsed categorized value to the expected types
in Instruction.Generic.create *)letto_hex_opcode=function|Parse_helpers.Message.Value.Inth->Z.format"%02x"h|Parse_helpers.Message.Value.Strs->sletto_mnemonic=function|Parse_helpers.Message.Value.Strs->Mnemonic.supportedsFormat.pp_print_string|_->raiseNot_foundletjust_integer=function|Parse_helpers.Message.Value.Intn->Z.to_intn|_->raiseNot_foundletcompare_labeled_instruction(caddr1,_i1)(caddr2,_i2)=Dba_types.Caddress.comparecaddr1caddr2letto_blockaddr_instr_list=(* Blocks returned by Unisimi's ARM decoded are not necessarily ordered.
We need to do it here. The specific comparison functions explicits
assumptions about what is expected (same virtual addresses and differences
of identifiers).
*)List.sortcompare_labeled_instructionaddr_instr_list|>List.mapsnd|>Dhunk.of_listletunsupported(ins:Instruction.Generic.t)=Dhunk.singleton(Dba.Instr.stop(Some(Dba.Unsupported(Mnemonic.to_stringins.mnemonic))))letbasic_instructioninfos=letopcode=to_hex_opcode(find"opcode"infos)inletmnemonic=to_mnemonic(find"mnemonic"infos)inletsize=just_integer(find"size"infos)inInstruction.Generic.createsizeopcodemnemonicletparse_bases=letlexbuf=Lexing.from_stringsinmatchbasic_instruction(Parser.decoder_baseLexer.tokenlexbuf)with|exception(Parser.Error|Failure_|Not_found)->(empty_instruction,die,SomeFailure)|ins->(ins,unsupportedins,SomeFailure)letparse_messages=L.debug~level:1"@[<v 0>Parsing %s@]"s;letlexbuf=Lexing.from_stringsinmatchParser.decoder_msg_eofLexer.tokenlexbufwith|exception(Parser.Error|Failure_)->letpos=Lexing.lexeme_start_plexbufinL.error"@[<v 0>Probable parse error at line %d, column %d@ Lexeme was: %s@ \
Entry was: %s@ Getting basic infos only ... @]"pos.pos_lnum(pos.pos_cnum-pos.pos_bol)(Lexing.lexemelexbuf)s;parse_bases|_,Undefined->(empty_instruction,die,SomeUndefined)|infos,((Unimplemented|Unsupported_|Precise_)assemantics)->(matchbasic_instructioninfoswith|exceptionNot_found->(empty_instruction,die,SomeFailure)|ins->(matchsemanticswith|Undefined->assertfalse|Unimplemented->(ins,unsupportedins,SomeUnimplemented)|Unsupported_->(ins,unsupportedins,SomeUnsupported)|Precisebody->(ins,to_blockbody,None)))end