From nobody Thu May 16 10:52:12 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1684157141; cv=none; d=zohomail.com; s=zohoarc; b=Kn1KIN1CW9v2HTfinRBqF5reHXXhv58tmN8hBMwxW0Nl5e2ogonPpC777n2z06kAGUmESJZaOrTrrkd6jtDebImXlHnY4zh/r4yy8qd/xC80FCyEZpqQnUE7pyJgWFW0PEQbaInBMWmsBHxrik7McUJ1uHErtAkxFRvV+1o/2Qs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1684157141; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=2gejgFtOIKkxVCEgs2subIwJOc9s9H8jJPdkSyz9dqY=; b=dbvxqftQdvjIiWRu/FxGRkCzQD6OtnurnNYp9ycSO20Ltfujet1fDXhuXxqBJZodI38UauvqaALfIeMcfAXeTa1u1EAWAI0WjRJiGU1FazgbBoPFfQ7eeLh4LJPlO2nncOY3XuqYBbQDOmpcLr82muHSlKocpuSiP0MXxvRw1FM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16841571410391013.5445840216735; Mon, 15 May 2023 06:25:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pyYC7-0000a2-3s; Mon, 15 May 2023 09:24:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pyYC2-0000V4-8K for qemu-devel@nongnu.org; Mon, 15 May 2023 09:24:51 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pyYC0-0005fZ-PG for qemu-devel@nongnu.org; Mon, 15 May 2023 09:24:50 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-267-aQuLJGY4MHSiaQXVHA_cTA-1; Mon, 15 May 2023 09:24:45 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A71A8800047; Mon, 15 May 2023 13:24:44 +0000 (UTC) Received: from localhost (unknown [10.39.208.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 379BD1121314; Mon, 15 May 2023 13:24:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684157088; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2gejgFtOIKkxVCEgs2subIwJOc9s9H8jJPdkSyz9dqY=; b=gCTid0S01K3MqDLoFf3IU13ilRaFWN3Skl2fIEZbvXST8nDi74FJjUg5JhISpAlyRVvPKM oGkhCrgbYkiMnqlE6LORDF99ZtuEU5j9SwwoI+eLFv6ret2ntdgSn9s+d0Xg+9CnvwCIq7 xnDOc74vtRmgoLcjOtb0CZSrgprgrl0= X-MC-Unique: aQuLJGY4MHSiaQXVHA_cTA-1 From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Cc: kraxel@redhat.com, berrange@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Stefan Weil Subject: [PATCH] win32: wrap socket close() with an exception handler Date: Mon, 15 May 2023 17:24:40 +0400 Message-Id: <20230515132440.1025315-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=marcandre.lureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1684157142797100005 From: Marc-Andr=C3=A9 Lureau Since commit abe34282 ("win32: avoid mixing SOCKET and file descriptor space"), we set HANDLE_FLAG_PROTECT_FROM_CLOSE on the socket FD, to prevent closing the HANDLE with CloseHandle. This raises an exception which under gdb is fatal, and qemu exits. Let's catch the expected error instead. Note: this appears to work, but the mingw64 macro is not well documented or tested, and it's not obvious how it is meant to be used. Signed-off-by: Marc-Andr=C3=A9 Lureau --- include/sysemu/os-win32.h | 4 ++++ util/oslib-win32.c | 23 +++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/sysemu/os-win32.h b/include/sysemu/os-win32.h index 15c296e0eb..65f6c9ea57 100644 --- a/include/sysemu/os-win32.h +++ b/include/sysemu/os-win32.h @@ -259,6 +259,10 @@ ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t l= en, int flags); ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen); =20 +EXCEPTION_DISPOSITION +win32_close_exception_handler(struct _EXCEPTION_RECORD*, void*, + struct _CONTEXT*, void*); + #ifdef __cplusplus } #endif diff --git a/util/oslib-win32.c b/util/oslib-win32.c index a98638729a..fafbab80b4 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -479,6 +479,13 @@ int qemu_bind_wrap(int sockfd, const struct sockaddr *= addr, return ret; } =20 +EXCEPTION_DISPOSITION +win32_close_exception_handler(struct _EXCEPTION_RECORD*, + void*, struct _CONTEXT*, void*) +{ + return EXCEPTION_EXECUTE_HANDLER; +} + #undef close int qemu_close_socket_osfhandle(int fd) { @@ -504,12 +511,16 @@ int qemu_close_socket_osfhandle(int fd) return -1; } =20 - /* - * close() returns EBADF since we PROTECT_FROM_CLOSE the underlying ha= ndle, - * but the FD is actually freed - */ - if (close(fd) < 0 && errno !=3D EBADF) { - return -1; + __try1(win32_close_exception_handler) { + /* + * close() returns EBADF since we PROTECT_FROM_CLOSE the underlying + * handle, but the FD is actually freed + */ + if (close(fd) < 0 && errno !=3D EBADF) { + return -1; + } + } + __except1 { } =20 if (!SetHandleInformation((HANDLE)s, flags, flags)) { --=20 2.40.1