From patchwork Tue Aug 8 12:53:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 73814 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5E6D7385781F for ; Tue, 8 Aug 2023 12:53:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E6D7385781F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1691499231; bh=p2C+ulQRSH3Z1nLk+WQGbf6jVwhlBtwNiVWmtvmL7c8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=tssfUD11v83h5FHBm735XPwW3moDql9D+Eowj5btXkh18mX3D6RNQRhb2bYDUPJbe niIyWd/NSZi0atLVKXo5MXGM6UAdnVS0c8F2QDG7lAC+qR6BloAb9/pFMj0jXzaBVx lTt6ArN31rv8L2QGXvxg02jWRwjlddyRIwz5EdrA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 5AD15385842B for ; Tue, 8 Aug 2023 12:53:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5AD15385842B Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-190-ajyP4rLTOamDg2nXnAvnKQ-1; Tue, 08 Aug 2023 08:53:27 -0400 X-MC-Unique: ajyP4rLTOamDg2nXnAvnKQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 043151C07548 for ; Tue, 8 Aug 2023 12:53:27 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.12]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A565C40C6E8A for ; Tue, 8 Aug 2023 12:53:26 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH 1/2] libio: Introduce _IO_JUMPS_UNVALIDATED Message-ID: X-From-Line: cf56a61681926b77d13046526ff01e705fefbbfe Mon Sep 17 00:00:00 2001 Date: Tue, 08 Aug 2023 14:53:25 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" And refactor the macros a bit. Use a safer way to avoid aliasing issues. Reviewed-by: Adhemerval Zanella --- libio/libioP.h | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) base-commit: dcad5c8578130dec7f35fd5b0885304b59f9f543 diff --git a/libio/libioP.h b/libio/libioP.h index 745278e076..0c5276fc4b 100644 --- a/libio/libioP.h +++ b/libio/libioP.h @@ -104,21 +104,41 @@ #define _IO_CHECK_WIDE(THIS) \ (_IO_CAST_FIELD_ACCESS ((THIS), struct _IO_FILE, _wide_data) != NULL) + #if _IO_JUMPS_OFFSET -# define _IO_JUMPS_FUNC(THIS) \ - (IO_validate_vtable \ - (*(struct _IO_jump_t **) ((void *) &_IO_JUMPS_FILE_plus (THIS) \ - + (THIS)->_vtable_offset))) -# define _IO_JUMPS_FUNC_UPDATE(THIS, VTABLE) \ - (*(const struct _IO_jump_t **) ((void *) &_IO_JUMPS_FILE_plus (THIS) \ - + (THIS)->_vtable_offset) = (VTABLE)) # define _IO_vtable_offset(THIS) (THIS)->_vtable_offset #else -# define _IO_JUMPS_FUNC(THIS) (IO_validate_vtable (_IO_JUMPS_FILE_plus (THIS))) -# define _IO_JUMPS_FUNC_UPDATE(THIS, VTABLE) \ - (_IO_JUMPS_FILE_plus (THIS) = (VTABLE)) # define _IO_vtable_offset(THIS) 0 #endif + +/* Pointer to the active vtable pointer in a FILE object. */ +#define _IO_vtable_ptrptr(THIS) \ + ((void *) &_IO_JUMPS_FILE_plus (THIS) + _IO_vtable_offset (THIS)) + +/* Read the vtable pointer in THIS, avoiding an aliasing violation. + This performs no vtable validation, so should only be used for + explicit checks against known vtables. */ +#define _IO_JUMPS_UNVALIDATED(THIS) \ + ({ \ + struct _IO_jump_t *__jump_unvalidated; \ + memcpy (&__jump_unvalidated, _IO_vtable_ptrptr (THIS), \ + sizeof (__jump_unvalidated)); \ + __jump_unvalidated; \ + }) + +/* Like _IO_JUMPS_UNVALIDATED, but with validation. Used to implement + virtual member function calls below. */ +#define _IO_JUMPS_FUNC(THIS) \ + (IO_validate_vtable (_IO_JUMPS_UNVALIDATED (THIS))) + +/* Update the vtable pointer, avoiding an aliasing violation. */ +#define _IO_JUMPS_FUNC_UPDATE(THIS, VTABLE) \ + ({ \ + const struct _IO_jump_t *__jump_copy = (VTABLE); \ + memcpy (_IO_vtable_ptrptr (THIS), &__jump_copy, sizeof (__jump_copy)); \ + (void) 0; \ + }) + #define _IO_WIDE_JUMPS_FUNC(THIS) _IO_WIDE_JUMPS(THIS) #define JUMP_FIELD(TYPE, NAME) TYPE NAME #define JUMP0(FUNC, THIS) (_IO_JUMPS_FUNC(THIS)->FUNC) (THIS)