From nobody Tue Dec 2 02:30:06 2025 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 500DF36C0B0 for ; Wed, 19 Nov 2025 14:33:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.177.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763562840; cv=none; b=EeaIcGfSok+sboUtknqB4/ZOzRGfgwVgWVQtXPZH6FeYgPzxtaVrJT/Fs/bN09ydvwtJjj9SRD+ZGM+Ygb0Vgha/+jOgt/NB09Y2wQBi/JZgiKvVg54Qx3CGoj2Mx7Neyx9wtU3/JvQ2C/G9ew7sJpQWIPxoOVZjsNUuaDoku1Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763562840; c=relaxed/simple; bh=Bkw5njLypzARFwZCnNosvIVfb346eHdmoGekhIaJMOA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bd6b2/MR67Z32rOuBTufZCNtRRqOktZtPPNRs/V59szoBMAKOcDWsUwizlO0ZUwToBj2AFr28hFVCbZER0vcc2/ASLZ7sN+hea2X65ifuLp5f9Q7Hycr3EMK4zGBxAemExp/Bv4eQAQLNoBH9E7gk1sHxwsAHYONEOANCVnL8Ck= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=dVBbQ6Aw; arc=none smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="dVBbQ6Aw" Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 5AJEQwrQ032329; Wed, 19 Nov 2025 14:33:07 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2025-04-25; bh=r6DHo XToyeMrK0vIiucnvHJDqi+ZsZ7Jo4qUgVmjP9c=; b=dVBbQ6Aw3ykCEdEi4YZRp PrFS+bY37EOqpSo1Qox/WLBsznr/mCpp1Az57jeOsR2oezZ0H3S0/gqgrM4s/eBB f9gquMyCSMvopM0k20daPd2R4cReGMOvYKB2KULbdF7q7Utfq9pRrgd139dMW1QE x6GAz2RmyKxRheBr4NZxkOMQMMYjqyxauNL06SMV7qedL4QAS8bOE0y44xLUp4Kg 6Q2PcgO9JFwKpD13JH4+d+yYiOiUpjSNk+yaZamvE+6JcZ8nYSmCNpIEidBjFchr i/Flz5zV7rDvFXJ2duT42n2a/wgCkoe3XJf9Fk0dOi8Eis0RaVQC9KRtOBDeaNE9 A== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 4aejbbf9xq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 19 Nov 2025 14:33:06 +0000 (GMT) Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 5AJCn346002499; Wed, 19 Nov 2025 14:33:05 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 4aefyanrfq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 19 Nov 2025 14:33:05 +0000 Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 5AJEWnnK022495; Wed, 19 Nov 2025 14:33:05 GMT Received: from laptop-dell-latitude7430.nl.oracle.com (dhcp-10-154-160-110.vpn.oracle.com [10.154.160.110]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 4aefyanr5p-9; Wed, 19 Nov 2025 14:33:04 +0000 From: Alexandre Chartre To: linux-kernel@vger.kernel.org, mingo@kernel.org, jpoimboe@kernel.org, peterz@infradead.org, david.laight.linux@gmail.com Cc: alexandre.chartre@oracle.com Subject: [PATCH v5 08/30] objtool: Extract code to validate instruction from the validate branch loop Date: Wed, 19 Nov 2025 15:32:25 +0100 Message-ID: <20251119143247.3944213-9-alexandre.chartre@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20251119143247.3944213-1-alexandre.chartre@oracle.com> References: <20251119143247.3944213-1-alexandre.chartre@oracle.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2025-11-19_04,2025-11-18_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 malwarescore=0 suspectscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2510240000 definitions=main-2511190116 X-Authority-Analysis: v=2.4 cv=BoqQAIX5 c=1 sm=1 tr=0 ts=691dd522 b=1 cx=c_pps a=WeWmnZmh0fydH62SvGsd2A==:117 a=WeWmnZmh0fydH62SvGsd2A==:17 a=6UeiqGixMTsA:10 a=VkNPw1HP01LnGYTKEx00:22 a=yPCof4ZbAAAA:8 a=45N9rFkX92oTsxCXvtEA:9 X-Proofpoint-ORIG-GUID: 3WXS4HG-dP5XL52rh158zQpq668gIk7Q X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMTE1MDAzMiBTYWx0ZWRfX0kIFTx+j79sB R7aEDQPleef+cVMXirEsPubAf9gizjpX1VAwdiAwrHQ1ECKV0FxlDaf74jCC6Rvi3uvlia7ce2K 6A/AgSOftk2jw9gWg8/KxI/MN9nOnUV9MtJwAfwDEQtbrofPIdo8Hq267ao8K0wDTJZjsHWP+MD APrNZkLok7sjB3tHZpNoH2I9HiPUHRJZ088iX06/J3XaNHJRfyB3yOPqYU8E8BB3ciRWq/gf7Se D49CIuc1LnXXQTwN2Pyfc/O5JMtcXZTrDgkVbsPNptTIbTkgsgbohIcbiDvzWI5MbOAlSc1psHQ H8w0UQmrnpJUoj71DyxgxrdLoJ9QHI+rwbqYrWmNxSGY1nE5shGU5Ny4lWfGhZe7Js23lv5EXP4 5xABwxhCTlm3gMppvinF6v6utl97iQ== X-Proofpoint-GUID: 3WXS4HG-dP5XL52rh158zQpq668gIk7Q Content-Type: text/plain; charset="utf-8" The code to validate a branch loops through all instructions of the branch and validate each instruction. Move the code to validate an instruction to a separated function. Signed-off-by: Alexandre Chartre --- tools/objtool/check.c | 386 ++++++++++++++++++++++-------------------- 1 file changed, 205 insertions(+), 181 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index dd61c3242def5..9c1888e21c1d8 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -3706,253 +3706,277 @@ static void checksum_update_insn(struct objtool_f= ile *file, struct symbol *func, checksum_update(func, insn, &offset, sizeof(offset)); } =20 -/* - * Follow the branch starting at the given instruction, and recursively fo= llow - * any other branches (jumps). Meanwhile, track the frame pointer state at - * each instruction and validate all the rules described in - * tools/objtool/Documentation/objtool.txt. - */ static int validate_branch(struct objtool_file *file, struct symbol *func, - struct instruction *insn, struct insn_state state) + struct instruction *insn, struct insn_state state); + +static int validate_insn(struct objtool_file *file, struct symbol *func, + struct instruction *insn, struct insn_state *statep, + struct instruction *prev_insn, struct instruction *next_insn, + bool *dead_end) { struct alternative *alt; - struct instruction *next_insn, *prev_insn =3D NULL; u8 visited; int ret; =20 - if (func && func->ignore) - return 0; + /* + * Any returns before the end of this function are effectively dead + * ends, i.e. validate_branch() has reached the end of the branch. + */ + *dead_end =3D true; =20 - while (1) { - next_insn =3D next_insn_to_validate(file, insn); + visited =3D VISITED_BRANCH << statep->uaccess; + if (insn->visited & VISITED_BRANCH_MASK) { + if (!insn->hint && !insn_cfi_match(insn, &statep->cfi)) + return 1; =20 - if (opts.checksum && func && insn->sec) - checksum_update_insn(file, func, insn); + if (insn->visited & visited) + return 0; + } else { + nr_insns_visited++; + } =20 - if (func && insn_func(insn) && func !=3D insn_func(insn)->pfunc) { - /* Ignore KCFI type preambles, which always fall through */ - if (is_prefix_func(func)) - return 0; + if (statep->noinstr) + statep->instr +=3D insn->instr; =20 - if (file->ignore_unreachables) - return 0; + if (insn->hint) { + if (insn->restore) { + struct instruction *save_insn, *i; =20 - WARN("%s() falls through to next function %s()", - func->name, insn_func(insn)->name); - func->warned =3D 1; + i =3D insn; + save_insn =3D NULL; =20 - return 1; - } + sym_for_each_insn_continue_reverse(file, func, i) { + if (i->save) { + save_insn =3D i; + break; + } + } =20 - visited =3D VISITED_BRANCH << state.uaccess; - if (insn->visited & VISITED_BRANCH_MASK) { - if (!insn->hint && !insn_cfi_match(insn, &state.cfi)) + if (!save_insn) { + WARN_INSN(insn, "no corresponding CFI save for CFI restore"); return 1; + } =20 - if (insn->visited & visited) - return 0; - } else { - nr_insns_visited++; + if (!save_insn->visited) { + /* + * If the restore hint insn is at the + * beginning of a basic block and was + * branched to from elsewhere, and the + * save insn hasn't been visited yet, + * defer following this branch for now. + * It will be seen later via the + * straight-line path. + */ + if (!prev_insn) + return 0; + + WARN_INSN(insn, "objtool isn't smart enough to handle this CFI save/re= store combo"); + return 1; + } + + insn->cfi =3D save_insn->cfi; + nr_cfi_reused++; } =20 - if (state.noinstr) - state.instr +=3D insn->instr; + statep->cfi =3D *insn->cfi; + } else { + /* XXX track if we actually changed statep->cfi */ =20 - if (insn->hint) { - if (insn->restore) { - struct instruction *save_insn, *i; + if (prev_insn && !cficmp(prev_insn->cfi, &statep->cfi)) { + insn->cfi =3D prev_insn->cfi; + nr_cfi_reused++; + } else { + insn->cfi =3D cfi_hash_find_or_add(&statep->cfi); + } + } =20 - i =3D insn; - save_insn =3D NULL; + insn->visited |=3D visited; =20 - sym_for_each_insn_continue_reverse(file, func, i) { - if (i->save) { - save_insn =3D i; - break; - } - } + if (propagate_alt_cfi(file, insn)) + return 1; =20 - if (!save_insn) { - WARN_INSN(insn, "no corresponding CFI save for CFI restore"); - return 1; - } + if (insn->alts) { + for (alt =3D insn->alts; alt; alt =3D alt->next) { + ret =3D validate_branch(file, func, alt->insn, *statep); + if (ret) { + BT_INSN(insn, "(alt)"); + return ret; + } + } + } =20 - if (!save_insn->visited) { - /* - * If the restore hint insn is at the - * beginning of a basic block and was - * branched to from elsewhere, and the - * save insn hasn't been visited yet, - * defer following this branch for now. - * It will be seen later via the - * straight-line path. - */ - if (!prev_insn) - return 0; + if (skip_alt_group(insn)) + return 0; =20 - WARN_INSN(insn, "objtool isn't smart enough to handle this CFI save/r= estore combo"); - return 1; - } + if (handle_insn_ops(insn, next_insn, statep)) + return 1; =20 - insn->cfi =3D save_insn->cfi; - nr_cfi_reused++; - } + switch (insn->type) { =20 - state.cfi =3D *insn->cfi; - } else { - /* XXX track if we actually changed state.cfi */ + case INSN_RETURN: + return validate_return(func, insn, statep); =20 - if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) { - insn->cfi =3D prev_insn->cfi; - nr_cfi_reused++; - } else { - insn->cfi =3D cfi_hash_find_or_add(&state.cfi); - } + case INSN_CALL: + case INSN_CALL_DYNAMIC: + ret =3D validate_call(file, insn, statep); + if (ret) + return ret; + + if (opts.stackval && func && !is_special_call(insn) && + !has_valid_stack_frame(statep)) { + WARN_INSN(insn, "call without frame pointer save/setup"); + return 1; } =20 - insn->visited |=3D visited; + break; =20 - if (propagate_alt_cfi(file, insn)) - return 1; + case INSN_JUMP_CONDITIONAL: + case INSN_JUMP_UNCONDITIONAL: + if (is_sibling_call(insn)) { + ret =3D validate_sibling_call(file, insn, statep); + if (ret) + return ret; =20 - if (insn->alts) { - for (alt =3D insn->alts; alt; alt =3D alt->next) { - ret =3D validate_branch(file, func, alt->insn, state); - if (ret) { - BT_INSN(insn, "(alt)"); - return ret; - } + } else if (insn->jump_dest) { + ret =3D validate_branch(file, func, + insn->jump_dest, *statep); + if (ret) { + BT_INSN(insn, "(branch)"); + return ret; } } =20 - if (skip_alt_group(insn)) + if (insn->type =3D=3D INSN_JUMP_UNCONDITIONAL) return 0; =20 - if (handle_insn_ops(insn, next_insn, &state)) - return 1; - - switch (insn->type) { - - case INSN_RETURN: - return validate_return(func, insn, &state); + break; =20 - case INSN_CALL: - case INSN_CALL_DYNAMIC: - ret =3D validate_call(file, insn, &state); + case INSN_JUMP_DYNAMIC: + case INSN_JUMP_DYNAMIC_CONDITIONAL: + if (is_sibling_call(insn)) { + ret =3D validate_sibling_call(file, insn, statep); if (ret) return ret; + } =20 - if (opts.stackval && func && !is_special_call(insn) && - !has_valid_stack_frame(&state)) { - WARN_INSN(insn, "call without frame pointer save/setup"); - return 1; - } + if (insn->type =3D=3D INSN_JUMP_DYNAMIC) + return 0; =20 - break; + break; =20 - case INSN_JUMP_CONDITIONAL: - case INSN_JUMP_UNCONDITIONAL: - if (is_sibling_call(insn)) { - ret =3D validate_sibling_call(file, insn, &state); - if (ret) - return ret; + case INSN_SYSCALL: + if (func && (!next_insn || !next_insn->hint)) { + WARN_INSN(insn, "unsupported instruction in callable function"); + return 1; + } =20 - } else if (insn->jump_dest) { - ret =3D validate_branch(file, func, - insn->jump_dest, state); - if (ret) { - BT_INSN(insn, "(branch)"); - return ret; - } - } + break; =20 - if (insn->type =3D=3D INSN_JUMP_UNCONDITIONAL) - return 0; + case INSN_SYSRET: + if (func && (!next_insn || !next_insn->hint)) { + WARN_INSN(insn, "unsupported instruction in callable function"); + return 1; + } + + return 0; =20 + case INSN_STAC: + if (!opts.uaccess) break; =20 - case INSN_JUMP_DYNAMIC: - case INSN_JUMP_DYNAMIC_CONDITIONAL: - if (is_sibling_call(insn)) { - ret =3D validate_sibling_call(file, insn, &state); - if (ret) - return ret; - } + if (statep->uaccess) { + WARN_INSN(insn, "recursive UACCESS enable"); + return 1; + } =20 - if (insn->type =3D=3D INSN_JUMP_DYNAMIC) - return 0; + statep->uaccess =3D true; + break; =20 + case INSN_CLAC: + if (!opts.uaccess) break; =20 - case INSN_SYSCALL: - if (func && (!next_insn || !next_insn->hint)) { - WARN_INSN(insn, "unsupported instruction in callable function"); - return 1; - } + if (!statep->uaccess && func) { + WARN_INSN(insn, "redundant UACCESS disable"); + return 1; + } =20 - break; + if (func_uaccess_safe(func) && !statep->uaccess_stack) { + WARN_INSN(insn, "UACCESS-safe disables UACCESS"); + return 1; + } =20 - case INSN_SYSRET: - if (func && (!next_insn || !next_insn->hint)) { - WARN_INSN(insn, "unsupported instruction in callable function"); - return 1; - } + statep->uaccess =3D false; + break; =20 - return 0; + case INSN_STD: + if (statep->df) { + WARN_INSN(insn, "recursive STD"); + return 1; + } =20 - case INSN_STAC: - if (!opts.uaccess) - break; + statep->df =3D true; + break; =20 - if (state.uaccess) { - WARN_INSN(insn, "recursive UACCESS enable"); - return 1; - } + case INSN_CLD: + if (!statep->df && func) { + WARN_INSN(insn, "redundant CLD"); + return 1; + } =20 - state.uaccess =3D true; - break; + statep->df =3D false; + break; =20 - case INSN_CLAC: - if (!opts.uaccess) - break; + default: + break; + } =20 - if (!state.uaccess && func) { - WARN_INSN(insn, "redundant UACCESS disable"); - return 1; - } + *dead_end =3D insn->dead_end; =20 - if (func_uaccess_safe(func) && !state.uaccess_stack) { - WARN_INSN(insn, "UACCESS-safe disables UACCESS"); - return 1; - } + return 0; +} =20 - state.uaccess =3D false; - break; +/* + * Follow the branch starting at the given instruction, and recursively fo= llow + * any other branches (jumps). Meanwhile, track the frame pointer state at + * each instruction and validate all the rules described in + * tools/objtool/Documentation/objtool.txt. + */ +static int validate_branch(struct objtool_file *file, struct symbol *func, + struct instruction *insn, struct insn_state state) +{ + struct instruction *next_insn, *prev_insn =3D NULL; + bool dead_end; + int ret; =20 - case INSN_STD: - if (state.df) { - WARN_INSN(insn, "recursive STD"); - return 1; - } + if (func && func->ignore) + return 0; =20 - state.df =3D true; - break; + while (1) { + next_insn =3D next_insn_to_validate(file, insn); =20 - case INSN_CLD: - if (!state.df && func) { - WARN_INSN(insn, "redundant CLD"); - return 1; - } + if (opts.checksum && func && insn->sec) + checksum_update_insn(file, func, insn); =20 - state.df =3D false; - break; + if (func && insn_func(insn) && func !=3D insn_func(insn)->pfunc) { + /* Ignore KCFI type preambles, which always fall through */ + if (is_prefix_func(func)) + return 0; =20 - default: - break; + if (file->ignore_unreachables) + return 0; + + WARN("%s() falls through to next function %s()", + func->name, insn_func(insn)->name); + func->warned =3D 1; + + return 1; } =20 - if (insn->dead_end) - return 0; + ret =3D validate_insn(file, func, insn, &state, prev_insn, next_insn, + &dead_end); + if (dead_end) + break; =20 if (!next_insn) { if (state.cfi.cfa.base =3D=3D CFI_UNDEFINED) @@ -3970,7 +3994,7 @@ static int validate_branch(struct objtool_file *file,= struct symbol *func, insn =3D next_insn; } =20 - return 0; + return ret; } =20 static int validate_unwind_hint(struct objtool_file *file, --=20 2.43.5