From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from omta40.uswest2.a.cloudfilter.net (omta40.uswest2.a.cloudfilter.net [35.89.44.39]) by sourceware.org (Postfix) with ESMTPS id 18F2A3858D20 for ; Thu, 11 Apr 2024 16:07:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 18F2A3858D20 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 18F2A3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=35.89.44.39 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712851656; cv=none; b=gzY42bGl/Ckpye0q6KS8eoC8rKj3YTdKO7sKRSEAl9fiJoZ/AQPJ49nkTjF2TGayFiYUUzcL4FOj+sq6LW3h8AoNi5r2hztibFZryyMVK638onOVs6poL0WwzM1SRfXL2/cAz1P9RGhYeacf75TVgCeOvMT6wiE7WjyJFynPorQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712851656; c=relaxed/simple; bh=gI7gLMBuu2l1si+kho5tS5BDuZrG5/mhG1Dvp0oJhiQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=unhq5CDBdbsg5/7yx32tVYHzYcZ3c0/psuxFwp0N2h6dtL0KCS8NG0ekm7cbXvf/oCoRaIjL8UL2mMQfflzYpw8wDvSV6BveZuTa8FVzX4KRHtzYg4Ystk4ts12Kkf1hOoyhDYxpZIihrTnCZ/ScddRqTTuIVrpuSCkIngZkN5U= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from eig-obgw-6009a.ext.cloudfilter.net ([10.0.30.184]) by cmsmtp with ESMTPS id uPGyrxmYGPM1huwxYrLzur; Thu, 11 Apr 2024 16:07:32 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id uwxXreIsrnNCOuwxXrSBwh; Thu, 11 Apr 2024 16:07:31 +0000 X-Authority-Analysis: v=2.4 cv=DKCJ4TNb c=1 sm=1 tr=0 ts=66180ac3 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=raytVjVEu-sA:10 a=Qbun_eYptAEA:10 a=8AHkEIZyAAAA:8 a=mDV3o1hIAAAA:8 a=G2-NJ86LyE33P_fNtbAA:9 a=rRLpFlB8T1cA:10 a=2ic9gv2lzLIA:10 a=_FVE-zBwftR9WsbkzFJk:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Type:MIME-Version:Message-ID:In-Reply-To:Date:References :Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=/X0KUtOANSeUB5L6G8q4uoTPI99T0Q3UrJmMCWBOgWs=; b=B2M5v3TuhCoDGrKqqZvdNnUatw H00jiqW8USUGFE0B2Ljzd1gNWRr+dU6hwJ9NGC08yaZMZc2O4uBJmEeOWvteSRZOWtH7Zusdg84Kh GPFfZGAVJ+xYJpgTjH9aJqaV0; Received: from 97-122-82-115.hlrn.qwest.net ([97.122.82.115]:40710 helo=murgatroyd) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1ruwxX-003f9J-0N; Thu, 11 Apr 2024 10:07:31 -0600 From: Tom Tromey To: Tom de Vries Cc: gdb-patches@sourceware.org Subject: Re: [PATCH] [gdb/python] Throw MemoryError in inferior.read_memory if malloc fails References: <20240411105257.15421-1-tdevries@suse.de> X-Attribution: Tom Date: Thu, 11 Apr 2024 10:07:29 -0600 In-Reply-To: <20240411105257.15421-1-tdevries@suse.de> (Tom de Vries's message of "Thu, 11 Apr 2024 12:52:57 +0200") Message-ID: <875xwn51dq.fsf@tromey.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 97.122.82.115 X-Source-L: No X-Exim-ID: 1ruwxX-003f9J-0N X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-82-115.hlrn.qwest.net (murgatroyd) [97.122.82.115]:40710 X-Source-Auth: tom+tromey.com X-Email-Count: 2 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfC4/i4PCgbYPBjxcGQpdXkcWK5lXSVWZTVh/j78CBuZ1D9ntma7oIKaVgm+aiMv8olCi5fEQup8gD/ZSwF11YSiwuYI2JLyOmzX1kpdh7JFuobTuZx7r R6btCyJti6gn8m+0XjMKKM7Kz0wK0/Ui3m5SnX36biSdIfUcgKfFWMriA4Yy25+jzNABC5V/UlBahJGfSdo4nuoQ/6h6VYjxS4g= X-Spam-Status: No, score=-3015.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,JMQ_SPF_NEUTRAL,KAM_SHORT,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: >>>>> "Tom" == Tom de Vries writes: Tom> PR python/31631 reports a gdb internal error when doing: Tom> ... Tom> (gdb) python gdb.selected_inferior().read_memory (0, 0xffffffffffffffff) Tom> utils.c:709: internal-error: virtual memory exhausted. Tom> A problem internal to GDB has been detected, Tom> further debugging may prove unreliable. Tom> ... Tom> Fix this by throwing a python MemoryError, such that we have instead: Tom> ... Tom> (gdb) python gdb.selected_inferior().read_memory (0, 0xffffffffffffffff) Tom> Python Exception : Tom> Error occurred in Python. Tom> (gdb) Tom> ... I tend to think you will regret opening this door, because I imagine there are a large number of ways to crash gdb by passing nonsensical values to Python APIs. Tom> @request("readMemory") Tom> @capability("supportsReadMemoryRequest") Tom> def read_memory(*, memoryReference: str, offset: int = 0, count: int, **extra): Tom> addr = int(memoryReference, 0) + offset Tom> - buf = gdb.selected_inferior().read_memory(addr, count) Tom> + oom = False Tom> + try: Tom> + buf = gdb.selected_inferior().read_memory(addr, count) Tom> + except MemoryError: Tom> + oom = True Tom> + if oom: Tom> + raise DAPException("Out of memory") This should probably chain the memory error in the except block and re-throw. See https://peps.python.org/pep-3134/ However I don't really understand why this is needed. Isn't the exception already propagated back to the server thread? Tom> + /* We used to use xmalloc, which does this trick to avoid malloc Tom> + returning a nullptr for a valid reason. Keep doing the same. */ Tom> + if (length == 0) Tom> + length = 1; This is most likely a workaround for vendor implementations of malloc that return NULL for malloc(0). See https://www.gnu.org/software/gnulib/manual/html_node/malloc.html However, here this is not necessary, because a 0-length memory read is meaningless, and so this case can simply be reported as an error. Tom