From nobody Fri Sep 20 20:29:32 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3F6A1C77B70 for ; Fri, 14 Apr 2023 06:10:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229920AbjDNGKn (ORCPT ); Fri, 14 Apr 2023 02:10:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229476AbjDNGKl (ORCPT ); Fri, 14 Apr 2023 02:10:41 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D38E459C5 for ; Thu, 13 Apr 2023 23:10:39 -0700 (PDT) X-UUID: 1185d82cda8b11eda9a90f0bb45854f4-20230414 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=K5oB4l0Vb3M1xYybDiOMAFZ9n2MsdIcmscCAPi5IQEM=; b=Eh9yAyYCQRAG0NKyCrc8G7tklHDdIPrmWIJWgzrmVg8seCrTIIwtoUdQY5fKjRXb8Yp3V71rt6Vn7D2YRhLxmzjZ4ThEjfSE1b5Ectj5kuddrNSEE0gXD1cIJg9M/hwN4+UEiL0ZlynZTsDNIxiodDXdCskZqIKTbsxbZtxn8Pg=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.22,REQID:cbf65f1b-d6df-4957-b999-ea8f75a215c7,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:120426c,CLOUDID:064af0ea-db6f-41fe-8b83-13fe7ed1ef52,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:11|1,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-UUID: 1185d82cda8b11eda9a90f0bb45854f4-20230414 Received: from mtkmbs10n2.mediatek.inc [(172.21.101.183)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 619629966; Fri, 14 Apr 2023 14:10:34 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs11n2.mediatek.inc (172.21.101.187) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Fri, 14 Apr 2023 14:10:33 +0800 Received: from mszsdtlt102.gcn.mediatek.inc (10.16.4.142) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Fri, 14 Apr 2023 14:10:33 +0800 From: Haibo Li To: Russell King , AngeloGioacchino Del Regno , Linus Walleij , , CC: Matthias Brugger , Haibo Li , , Russell King , Alex Sverdlin , , , , , Subject: [PATCH v3] ARM:unwind:fix unwind abort for uleb128 case Date: Fri, 14 Apr 2023 14:10:31 +0800 Message-ID: <20230414061031.141430-1-haibo.li@mediatek.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When unwind instruction is 0xb2,the subsequent instructions are uleb128 bytes. For now,it uses only the first uleb128 byte in code. For vsp increments of 0x204~0x400,use one uleb128 byte like below: 0xc06a00e4 : 0x80b27fac Compact model index: 0 0xb2 0x7f vsp =3D vsp + 1024 0xac pop {r4, r5, r6, r7, r8, r14} For vsp increments larger than 0x400,use two uleb128 bytes like below: 0xc06a00e4 : @0xc0cc9e0c Compact model index: 1 0xb2 0x81 0x01 vsp =3D vsp + 1032 0xac pop {r4, r5, r6, r7, r8, r14} The unwind works well since the decoded uleb128 byte is also 0x81. For vsp increments larger than 0x600,use two uleb128 bytes like below: 0xc06a00e4 : @0xc0cc9e0c Compact model index: 1 0xb2 0x81 0x02 vsp =3D vsp + 1544 0xac pop {r4, r5, r6, r7, r8, r14} In this case,the decoded uleb128 result is 0x101(vsp=3D0x204+(0x101<<2)). While the uleb128 used in code is 0x81(vsp=3D0x204+(0x81<<2)). The unwind aborts at this frame since it gets incorrect vsp. To fix this,add uleb128 decode to cover all the above case. Signed-off-by: Haibo Li --- v3: -As AngeloGioacchino Del Regno suggested,improve the comment and compress t= he code v2: - As Linus Walleij and Alexandre Mergnat suggested,add comments for unwind_= decode_uleb128 - As Alexandre Mergnat suggested,change variables declaration in Alphabetic= al order --- arch/arm/kernel/unwind.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/arch/arm/kernel/unwind.c b/arch/arm/kernel/unwind.c index 53be7ea6181b..9d2192156087 100644 --- a/arch/arm/kernel/unwind.c +++ b/arch/arm/kernel/unwind.c @@ -308,6 +308,29 @@ static int unwind_exec_pop_subset_r0_to_r3(struct unwi= nd_ctrl_block *ctrl, return URC_OK; } =20 +static unsigned long unwind_decode_uleb128(struct unwind_ctrl_block *ctrl) +{ + unsigned long bytes =3D 0; + unsigned long insn; + unsigned long result =3D 0; + + /* + * unwind_get_byte() will advance `ctrl` one instruction at a time, so + * loop until we get an instruction byte where bit 7 is not set. + * + * Note: This decodes a maximum of 4 bytes to output 28 bits data where + * max is 0xfffffff: that will cover a vsp increment of 1073742336, hence + * it is sufficient for unwinding the stack. + */ + do { + insn =3D unwind_get_byte(ctrl); + result |=3D (insn & 0x7f) << (bytes * 7); + bytes++; + } while (!!(insn & 0x80) && (bytes !=3D sizeof(result))); + + return result; +} + /* * Execute the current unwind instruction. */ @@ -361,7 +384,7 @@ static int unwind_exec_insn(struct unwind_ctrl_block *c= trl) if (ret) goto error; } else if (insn =3D=3D 0xb2) { - unsigned long uleb128 =3D unwind_get_byte(ctrl); + unsigned long uleb128 =3D unwind_decode_uleb128(ctrl); =20 ctrl->vrs[SP] +=3D 0x204 + (uleb128 << 2); } else { --=20 2.25.1