From patchwork Tue Apr 4 06:58:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 78869 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2822264vqo; Mon, 3 Apr 2023 23:58:56 -0700 (PDT) X-Google-Smtp-Source: AKy350au0mKAy0TbZTIThVe22wQCAdMwLFO/lurJjvlfyeuJ3wrT2G1wApnm+5idVCFvFbAKN7nF X-Received: by 2002:a05:6402:1a46:b0:4fd:2155:74ef with SMTP id bf6-20020a0564021a4600b004fd215574efmr1696721edb.19.1680591536100; Mon, 03 Apr 2023 23:58:56 -0700 (PDT) Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id f11-20020a056402150b00b004ad0affbc61si9685877edw.135.2023.04.03.23.58.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Apr 2023 23:58:56 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=f4VBaUgz; arc=fail (signature failed); spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 43E243857C55 for ; Tue, 4 Apr 2023 06:58:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 43E243857C55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680591531; bh=7r3EWOIslWOeZHq6SuMcKDG+GgDVfhmCtYlIy2vF2CU=; h=Date:Subject:To:Cc:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=f4VBaUgzyX3YycTVprQHa+hpDQ4MfpSXSG1DCdZQ56ukNmEjRPzuTzy6fCeFIZa0W PYxctoNgOvsiDXZZxWhGL+bSB2faTnwQzIIWEMbycOZVCRZYtB80lM/jazqOILjdfM nMz9NA/Z0ypJn+v62SvwENVTY/DG3/X5+DlkAR/Y= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2068.outbound.protection.outlook.com [40.107.22.68]) by sourceware.org (Postfix) with ESMTPS id 4E4573858C2D for ; Tue, 4 Apr 2023 06:58:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4E4573858C2D ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DcWHheIq+7FlJMtStC2Mh3nzcCk+05kFF34oMwjRWjFCvdiYOvdLl0WEhYEhZSFH5Zcvd6/r3tQ1my6CjPO7H2hQPMkW2IcsClFKk6pxXbV2g9+ejRcZ1MWf76Ysh8eo/MeQdfDLdlUwerJvnDfxDhaZsaqg2zmyM3KOJLhqrhZ3OIDa6QBX9xcZwSDfnFs+WLoMksv/qaI+cbVOHEepcSM/fvU7KzPsevIacOCK3I9LRs2XLHsx4uraEbfQeR+W9dYel8lmvcsqV0V8HyE8ubq8kxGcTnt2XDP1Y3REMCEuDN8uBHQII9gueC03bXUf5kYMxpiDjBBAg61irF5TUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=7r3EWOIslWOeZHq6SuMcKDG+GgDVfhmCtYlIy2vF2CU=; b=DWzavH18D6kfwMjIYOkGUnP17u0d9RQFPkcfdtq1nmQGridq6/OpiuyBZIB6gd1LVCvM4xIMLg/PIGQX2UoKpwy8YRZjqE5GV8bD73U+mGOZrlkRrBzlPu4yApYZ1DyMCCdh63i+KwgEJiVTWQcl+xe5Qhg+1Nx57PuPiRvUgX+NDOW5JI8d183vgI1pCSaLfPf1VUEjnFfcXv62BSOiy1ht2g3yrlyRZ+m0//JZJrmnw6Lm/ZuZUiBlR+xvlffI/EYSY3vtFrb8SUk9+x09K1qKiXzv0JT086ajfdq67mz15ZruXwv9t4P38iynx139enJ7RmC+nRTdUFLmflBQEg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by AM8PR04MB7314.eurprd04.prod.outlook.com (2603:10a6:20b:1df::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.35; Tue, 4 Apr 2023 06:58:39 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::154e:166d:ec25:531b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::154e:166d:ec25:531b%6]) with mapi id 15.20.6254.035; Tue, 4 Apr 2023 06:58:39 +0000 Message-ID: <39b50a10-ad10-aca0-2ef1-4d32be787762@suse.com> Date: Tue, 4 Apr 2023 08:58:37 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: [PATCH 2/8] x86: change fetch error handling in top-level function Content-Language: en-US To: Binutils Cc: "H.J. Lu" , Alan Modra References: <5dac45a8-cd5f-ee4d-52fc-7d283fc29ec4@suse.com> In-Reply-To: <5dac45a8-cd5f-ee4d-52fc-7d283fc29ec4@suse.com> X-ClientProxiedBy: FR2P281CA0006.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:a::16) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VE1PR04MB6560:EE_|AM8PR04MB7314:EE_ X-MS-Office365-Filtering-Correlation-Id: ac936a9b-5187-4099-16d1-08db34da04c6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dTGbV93EOMs7PJXnvljr57BT3WH8AYU/58IC0JX4ssT3njujCN6ZJs1fhClo4EZSo9Q4rAxQMYTZ740GvegomOZ7FW7xucjQaYY6mw15nIo82uMtXxDLnd91McLkdm+TICD4dwF6c21toajlg9SSzIwbv3QYpxxv7ieBmujEdmW9wV+6S1ec0RrxN3RgAi03B2dBD0hei3fihqIkm+O2mXAtGNAjXEjk295QYu3W5CZIu61ljmvDywVL/av9CovgYAH+B1YVTBPFJUU2g2DsNMrPJwJ4jvLbk4uGO2dT/Y6EdFql8fMSZRy5RI6eo2Zz9HPxSFImxzssnkubDdOjomGCWYOh5aNfjHi1IhW5sUOadO83zckEXcl0QTONut5i27lEvrfXPYnk8H8EIsPk+meKzggRDmhdLnJ2veG837KTJ+ewEWhvv1qrh2b/m64PoAMr9sYH7i8622ShZRQ75E9vUNaogJhSKbIws6aYJwwvieV4JKYlWL4nvMw4C8hfpvPC5My6DNlW6mr/Mmgb5+2l//vLStYI2J1k+0+5/maW1CRuXoSnn+5MhLu1Fja09uGuvmobt1yPzAmdl+uyTvMraVt3NgJdJUHhQHi42XurPbKvhxo5///UGFKgPf9z X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:VE1PR04MB6560.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(366004)(346002)(376002)(39860400002)(136003)(396003)(451199021)(41300700001)(5660300002)(8676002)(8936002)(38100700002)(6916009)(66556008)(66476007)(66946007)(36756003)(86362001)(31696002)(2906002)(4326008)(2616005)(966005)(31686004)(6486002)(186003)(6512007)(26005)(6506007)(54906003)(316002)(478600001)(45980500001)(43740500002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?JXXzfV51rmdM5TXCDO5Afu5FVHyX?= =?utf-8?q?3IJnDiy6Zlwu/7cGAcFJ25w01+3PxXriA4en4Zdh/fyXoPprJyteSyThbbcs/EK2L?= =?utf-8?q?I3A6ZWCv6xUik42yO2tdX1NBJSqN5TpXEA4EJng96XZF3OYd1MeIw+SlodfkRriw6?= =?utf-8?q?z4cq+oykdLdN+kNxq52pyF0NlthYp9uO8tRPjEilWUlxD87kYTMsiM6N591/dEP9l?= =?utf-8?q?nwNjC2tvWR3Cj41rSdLGKzLFZJOG5xbKRHYHiG7ut50XNTlo9mXOW/FHuLOqivCmA?= =?utf-8?q?4qkspS+nO9PXCDcx+Ks05itMflHLVNYf3aYrbs1qyfZTypJf/DmB3WHrDv/fp67mC?= =?utf-8?q?NAX2/UqrXWf5qOtxKEO/meHUYoTQAcYIgw/szP8rO2ff/VjNDyuu/rZW8ORSbhC52?= =?utf-8?q?0KJBEjM3xN3wTpN98VnWH0TOGMKzVkygssTPM70WkZMk0oXHxBJ3VbTmAig+ZYLZ2?= =?utf-8?q?7jZHh7JQtF/py0qiBO/93dZhVSSDzkePTCVT1fLQt32hDkAnCG6PyIh4AtzwnFe35?= =?utf-8?q?8VBqsZLI5mav/CWNXzGKjIg/metwEGAbqyH/dr05gxUB/u2gy1UIndAn+Mw2vZqfk?= =?utf-8?q?TrC/b4infEj7cG7sNCrvMc36qjh+UdY2tok+1fPn0HsLchmidZd774OCJH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9vuSz8p?= =?utf-8?q?vDcI8+nUQXP7?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: ac936a9b-5187-4099-16d1-08db34da04c6 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Apr 2023 06:58:39.2035 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: GGMTn1a8aj8h4oHIfSzLsMQMgjZauGjgCRnXe42BuOUT1KlGjFJXxMRwjaNfH7+oRRxJBXsUisHnkc+rxYpaQQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8PR04MB7314 X-Spam-Status: No, score=-3027.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_VALIDITY_RPBL, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jan Beulich via Binutils From: Jan Beulich Reply-To: Jan Beulich Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762227950310942302?= X-GMAIL-MSGID: =?utf-8?q?1762227950310942302?= ... and its direct helper get_sib(). Using setjmp()/longjmp() for fetch error handling is problematic, as per https://sourceware.org/pipermail/binutils/2023-March/126687.html. Start using more conventional error handling instead. Also introduce a fetch_modrm() helper, for subsequent re-use. --- a/opcodes/i386-dis.c +++ b/opcodes/i386-dis.c @@ -329,6 +329,49 @@ fetch_data (struct disassemble_info *inf return 1; } +static bool +fetch_code (struct disassemble_info *info, bfd_byte *until) +{ + int status = -1; + struct dis_private *priv = info->private_data; + bfd_vma start = priv->insn_start + (priv->max_fetched - priv->the_buffer); + + if (until <= priv->max_fetched) + return true; + + if (until <= priv->the_buffer + MAX_MNEM_SIZE) + status = (*info->read_memory_func) (start, + priv->max_fetched, + until - priv->max_fetched, + info); + if (status != 0) + { + /* If we did manage to read at least one byte, then + print_insn_i386 will do something sensible. Otherwise, print + an error. We do that here because this is where we know + STATUS. */ + if (priv->max_fetched == priv->the_buffer) + (*info->memory_error_func) (status, start, info); + return false; + } + + priv->max_fetched = until; + return true; +} + +static bool +fetch_modrm (instr_info *ins) +{ + if (!fetch_code (ins->info, ins->codep + 1)) + return false; + + ins->modrm.mod = (*ins->codep >> 6) & 3; + ins->modrm.reg = (*ins->codep >> 3) & 7; + ins->modrm.rm = *ins->codep & 7; + + return true; +} + static int fetch_error (const instr_info *ins) { @@ -9573,7 +9616,7 @@ get_valid_dis386 (const struct dis386 *d return get_valid_dis386 (dp, ins); } -static void +static bool get_sib (instr_info *ins, int sizeflag) { /* If modrm.mod == 3, operand must be register. */ @@ -9582,7 +9625,8 @@ get_sib (instr_info *ins, int sizeflag) && ins->modrm.mod != 3 && ins->modrm.rm == 4) { - FETCH_DATA (ins->info, ins->codep + 2); + if (!fetch_code (ins->info, ins->codep + 2)) + return false; ins->sib.index = (ins->codep[1] >> 3) & 7; ins->sib.scale = (ins->codep[1] >> 6) & 3; ins->sib.base = ins->codep[1] & 7; @@ -9590,6 +9634,8 @@ get_sib (instr_info *ins, int sizeflag) } else ins->has_sib = false; + + return true; } /* Like oappend (below), but S is a string starting with '%'. In @@ -9854,7 +9900,9 @@ print_insn (bfd_vma pc, disassemble_info ins.insn_codep = ins.codep; - FETCH_DATA (info, ins.codep + 1); + if (!fetch_code (info, ins.codep + 1)) + return fetch_error (&ins); + ins.two_source_ops = (*ins.codep == 0x62) || (*ins.codep == 0xc8); if (((ins.prefixes & PREFIX_FWAIT) @@ -9874,7 +9922,8 @@ print_insn (bfd_vma pc, disassemble_info unsigned char threebyte; ins.codep++; - FETCH_DATA (info, ins.codep + 1); + if (!fetch_code (info, ins.codep + 1)) + return fetch_error (&ins); threebyte = *ins.codep; dp = &dis386_twobyte[threebyte]; ins.need_modrm = twobyte_has_modrm[threebyte]; @@ -9897,17 +9946,13 @@ print_insn (bfd_vma pc, disassemble_info sizeflag ^= DFLAG; ins.end_codep = ins.codep; - if (ins.need_modrm) - { - FETCH_DATA (info, ins.codep + 1); - ins.modrm.mod = (*ins.codep >> 6) & 3; - ins.modrm.reg = (*ins.codep >> 3) & 7; - ins.modrm.rm = *ins.codep & 7; - } + if (ins.need_modrm && !fetch_modrm (&ins)) + return fetch_error (&ins); if (dp->name == NULL && dp->op[0].bytemode == FLOATCODE) { - get_sib (&ins, sizeflag); + if (!get_sib (&ins, sizeflag)) + return fetch_error (&ins); dofloat (&ins, sizeflag); } else @@ -9915,7 +9960,8 @@ print_insn (bfd_vma pc, disassemble_info dp = get_valid_dis386 (dp, &ins); if (dp != NULL && putop (&ins, dp->name, sizeflag) == 0) { - get_sib (&ins, sizeflag); + if (!get_sib (&ins, sizeflag)) + return fetch_error (&ins); for (i = 0; i < MAX_OPERANDS; ++i) { ins.obufp = ins.op_out[i];