From patchwork Tue Sep 27 16:28:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 58078 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 B0D9A38582B4 for ; Tue, 27 Sep 2022 16:29:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B0D9A38582B4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664296185; bh=I0KfdyQSuWLllAJydpkhWpNxtkG2PjHqwebfVQIk53w=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=jTw40jLANJCUq9MXIkvdULxTcU3YHnFSarZOcvMM/AiaR2Oue6oB+UuzJOOqmPdW9 wrsScCjUat8WIqNQJGrw9tLXt/Z0m/7c9+DOlZ9zS+4Wa38aQouQ1nANtasObV7+xq GCTlgu0xZLBXc8e2RkWAs3xnzCq9cB8iItVy/YjU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oi1-x231.google.com (mail-oi1-x231.google.com [IPv6:2607:f8b0:4864:20::231]) by sourceware.org (Postfix) with ESMTPS id 5EB2E3858C50 for ; Tue, 27 Sep 2022 16:29:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5EB2E3858C50 Received: by mail-oi1-x231.google.com with SMTP id t62so12413721oie.10 for ; Tue, 27 Sep 2022 09:29:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date; bh=I0KfdyQSuWLllAJydpkhWpNxtkG2PjHqwebfVQIk53w=; b=16mLn+LKXEUtFVh5WDY7buF79dqhD1HNPGOsXKlbomRGkE1m7yGi4mH4HQlRX6fMra eSGODT6GCjKJ3QoZQcBnoFAckUBA7/sLs8C4+age+1tQLZaP4fJumU6Tm3J98dgWSXUt Rwsscuj4vNAOKO3nNtixoKBF8EU3xqB6/FU8ZNiHcUqlV3OROWx9cTduFyiNoB4mgh3g p6xGFshOklDJ6LTbuISaKc6Odvj97g2A5bs4F0zPtVwPZmrZDLJPbCAEDHFcaLpNjlxG LQu58qOOP/xwznqfWqLffE58XvTR/mij1Gz8c2eCRIrBmybGvj78KU6J1QmVVcyByxCF LOxA== X-Gm-Message-State: ACrzQf3BSxEalcwuqhg1VSw7Xy+NIj44TulzJ3QZj08Ou3baITbgda60 ghBoN+Nz4fboa9lPmwTv+mtY/A1rQ+JSpGrf8guFOYev0PWZ0w== X-Google-Smtp-Source: AMsMyM6nAXTnkY/PRDz90ig7Cv8Xa9GydWnxqIJsm+kaNVfsHeutaYYJVES40qIHPxY+gwWwMlvGeZKnZAJGN/3f8WI= X-Received: by 2002:aca:ebd7:0:b0:350:47c5:46c1 with SMTP id j206-20020acaebd7000000b0035047c546c1mr2223070oih.246.1664296150289; Tue, 27 Sep 2022 09:29:10 -0700 (PDT) MIME-Version: 1.0 Date: Tue, 27 Sep 2022 09:28:59 -0700 Message-ID: Subject: libgo patch committed: Synchronize empty struct field handling To: gcc-patches , gofrontend-dev X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Ian Lance Taylor via Gcc-patches From: Ian Lance Taylor Reply-To: Ian Lance Taylor Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This libgo patch by Funan Zeng synchronizes the handling of empty struct fields between the Go frontend and the libgo FFI code. In the compiler the logic for allocating one byte for the last field of a struct is: 1. the last field has zero size 2. the struct itself does not have zero size 3. the last field is not blank This patch adds the last two conditions to runtime.structToFFI. This is for https://go.dev/issue/55146. Bootstrapped and ran Go testsuite on x86_64-pc-linux-gnu. Committed to mainline. Ian 085bacba3502ff77c70a7660c19a68f50e9b7877 diff --git a/gcc/go/gofrontend/MERGE b/gcc/go/gofrontend/MERGE index f7a7985287d..73aa712dbdf 100644 --- a/gcc/go/gofrontend/MERGE +++ b/gcc/go/gofrontend/MERGE @@ -1,4 +1,4 @@ -42efec8c126cf3787bc7c89d9c7f224eff7c5a21 +0140cca9bc0fad1108c7ed369376ac71cc4bfecf The first line of this file holds the git revision number of the last merge done from the gofrontend repository. diff --git a/libgo/go/runtime/ffi.go b/libgo/go/runtime/ffi.go index cd8479ef551..86ce5b85d04 100644 --- a/libgo/go/runtime/ffi.go +++ b/libgo/go/runtime/ffi.go @@ -4,6 +4,7 @@ // Only build this file if libffi is supported. +//go:build libffi // +build libffi package runtime @@ -221,9 +222,6 @@ func stringToFFI() *__ffi_type { // structToFFI returns an ffi_type for a Go struct type. func structToFFI(typ *structtype) *__ffi_type { c := len(typ.fields) - if c == 0 { - return emptyStructToFFI() - } if typ.typ.kind&kindDirectIface != 0 { return ffi_type_pointer() } @@ -231,6 +229,7 @@ func structToFFI(typ *structtype) *__ffi_type { fields := make([]*__ffi_type, 0, c+1) checkPad := false lastzero := false + sawnonzero := false for i, v := range typ.fields { // Skip zero-sized fields; they confuse libffi, // and there is no value to pass in any case. @@ -239,10 +238,13 @@ func structToFFI(typ *structtype) *__ffi_type { // next field. if v.typ.size == 0 { checkPad = true - lastzero = true + if v.name == nil || *v.name != "_" { + lastzero = true + } continue } lastzero = false + sawnonzero = true if checkPad { off := uintptr(0) @@ -263,6 +265,10 @@ func structToFFI(typ *structtype) *__ffi_type { fields = append(fields, typeToFFI(v.typ)) } + if !sawnonzero { + return emptyStructToFFI() + } + if lastzero { // The compiler adds one byte padding to non-empty struct ending // with a zero-sized field (types.cc:get_backend_struct_fields).