From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 DFE95385DC28 for ; Fri, 7 Jul 2023 16:28:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DFE95385DC28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688747294; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=06GD2dFhGKX0ZvQ4k5Nwe1IpqWmo9SaF2pWEvvd32jI=; b=gh4NvcyRlfqS3azLTOKvuolrQT4BseKRFWc19Zjt1U08QlaWdSyIYEKYlO+QhRxcsy524U JghuzCNpB3MXhsjrakLzhIDxGZPo+u0L+Mcg3NO4uVRQwZPHGvPl0QqRBPCmaS+mn3F9l2 g/aui6/etb+LcwsG6q2uyrEQEn5zUfo= Received: from mail-lf1-f71.google.com (mail-lf1-f71.google.com [209.85.167.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-652-c_c87OpMO16PqWvsmqxgDQ-1; Fri, 07 Jul 2023 12:28:13 -0400 X-MC-Unique: c_c87OpMO16PqWvsmqxgDQ-1 Received: by mail-lf1-f71.google.com with SMTP id 2adb3069b0e04-4fbcdca8f29so1454826e87.2 for ; Fri, 07 Jul 2023 09:28:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688747291; x=1691339291; h=mime-version:message-id:date:references:in-reply-to:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=06GD2dFhGKX0ZvQ4k5Nwe1IpqWmo9SaF2pWEvvd32jI=; b=e24irrihJUD8ruGcSmV0g2zWDNv+0mZ058MwHqiDH3RrvoFOQOn6Hwfuded4qeNr92 BMHUfiOO7LgG07RaoNvbL2RJvTPCeCj/UMd3FUiWkz4fwXM6VU1nFmU4q18Z+D2HK1ZB Y8KLOEsDmjmaj6eIM+9CZcqDmYHY/Sv9bzTMon+zAGbuKMDDbGfKZS5B40xI9yhKbxFS ZN0Arl/UoAEjsY34R6oelaLMq+8oT080vpPl3ODeRMB1Avf4dH366tw8nkr2IdHkQvzO gFoStUd5zTfwmJ5ZQyVlJ7JjYEXmNmTgBFIw6+7WnltzGvuHvqajyecZuyhxycP1Np/4 dzKA== X-Gm-Message-State: ABy/qLZk5PcPVOZ5aKfZqp/uIcTskPSfn7FCNlB/pXgD2Jr9cdNqEEiX QaNnv+3mywuSuZiFW5efKVQ/orSmnkKa72WzV9b8WYBrpMVNEy3Wb5OR4ecOqlTdHN0jhGWbGMd Fq4VE1EPWEJUc1/bRUhOkvcJK4LOPcg== X-Received: by 2002:a05:6512:eaa:b0:4fb:772a:af12 with SMTP id bi42-20020a0565120eaa00b004fb772aaf12mr5148624lfb.21.1688747291680; Fri, 07 Jul 2023 09:28:11 -0700 (PDT) X-Google-Smtp-Source: APBJJlE50+UpTe+scPBjLW8m7JRWCJOrpRjIy1vvtzYePWDytX5wNZAtzOv7x8AMcT/6ldmMx+Q0Pw== X-Received: by 2002:a05:6512:eaa:b0:4fb:772a:af12 with SMTP id bi42-20020a0565120eaa00b004fb772aaf12mr5148607lfb.21.1688747291309; Fri, 07 Jul 2023 09:28:11 -0700 (PDT) Received: from localhost (2.72.115.87.dyn.plus.net. [87.115.72.2]) by smtp.gmail.com with ESMTPSA id c26-20020a05600c0ada00b003fbcdba1a52sm2929827wmr.3.2023.07.07.09.28.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jul 2023 09:28:10 -0700 (PDT) From: Andrew Burgess To: Pedro Alves , gdb-patches@sourceware.org Subject: Re: [PATCHv5 03/11] gdbserver: allow agent expressions to fail with invalid memory access In-Reply-To: <01059f8a-0e59-55b5-f530-190c26df5ba3@palves.net> References: <0ef54ed920e1262a07f4a061ecc08bea8fcaee23.1678987897.git.aburgess@redhat.com> <01059f8a-0e59-55b5-f530-190c26df5ba3@palves.net> Date: Fri, 07 Jul 2023 17:28:09 +0100 Message-ID: <87pm53k7om.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-11.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,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Pedro Alves writes: > On 2023-03-16 17:36, Andrew Burgess via Gdb-patches wrote: >> This commit extends gdbserver to take account of a failed memory >> access from agent_mem_read, and to return a new eval_result_type >> expr_eval_invalid_memory_access. >> >> I have only updated the agent_mem_read calls related directly to >> reading memory, I have not updated any of the calls related to >> tracepoint data collection. This is just because I'm not familiar >> with that area of gdb/gdbserver, and I don't want to break anything, >> so leaving the existing behaviour untouched seems like the safest >> approach. > > I think this should update the gdbserver/tracepoint.cc:eval_result_names array at least, > though. Otherwise I think a memory error during collection is going to trigger undefined > behavior, crash if we're lucky. Good spot. What are your thoughts on the patch below that should address this issue? Thanks, Andrew --- commit 35e78a7699618f0bb1c111cdc54d9f9bcb0171c9 Author: Andrew Burgess Date: Fri Jul 7 17:18:46 2023 +0100 gdbserver: handle all eval_result_type values in tracepoint.cc It was pointed out[1] that after this commit: commit 3812b38d8de5804ad3eadd6c7a5d532402ddabab Date: Thu Oct 20 11:14:33 2022 +0100 gdbserver: allow agent expressions to fail with invalid memory access Now that agent expressions might fail with the error expr_eval_invalid_memory_access, we might overflow the eval_result_names array in tracepoint.cc. This is because the eval_result_names array does not include a string for either expr_eval_invalid_goto or expr_eval_invalid_memory_access. I don't know if having expr_eval_invalid_goto missing is also a problem, but it feels like eval_result_names should just include a string for every possible error. I could just add two more strings into the array, but I figured that, if I convert eval_result_names into a function containing a switch then I can make use of DIAGNOSTIC_ERROR_SWITCH to have the compiler ensure that we always cover every possible error code. I know (from the comments) that tracepoint.cc is built both into gdbserver, and into a shared library that the inferior might load (for fast tracepoint support), so I did consider whether this change could break the API of that shared library. However, eval_result_names is a static array, so I don't believe that this is part of the external API, as such, converting this array into a static function shouldn't impact any users. [1] https://inbox.sourceware.org/gdb-patches/01059f8a-0e59-55b5-f530-190c26df5ba3@palves.net/ diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc index 1f31e0cc05f..879ad734b66 100644 --- a/gdbserver/tracepoint.cc +++ b/gdbserver/tracepoint.cc @@ -857,17 +857,43 @@ EXTERN_C_POP static struct tracepoint *last_tracepoint; -static const char * const eval_result_names[] = - { - "terror:in the attic", /* this should never be reported */ - "terror:empty expression", - "terror:empty stack", - "terror:stack overflow", - "terror:stack underflow", - "terror:unhandled opcode", - "terror:unrecognized opcode", - "terror:divide by zero" - }; +/* Return a string representing the error in ERR. This should not be + called when ERR is expr_eval_no_error. */ + +static const char * const +eval_result_error_name (enum eval_result_type err) +{ + gdb_assert (err != expr_eval_no_error); + +DIAGNOSTIC_PUSH +DIAGNOSTIC_ERROR_SWITCH + switch (err) + { + case expr_eval_no_error: + break; + case expr_eval_empty_expression: + return "terror:empty expression"; + case expr_eval_empty_stack: + return "terror:empty stack"; + case expr_eval_stack_overflow: + return "terror:stack overflow"; + case expr_eval_stack_underflow: + return "terror:stack underflow"; + case expr_eval_unhandled_opcode: + return "terror:unhandled opcode"; + case expr_eval_unrecognized_opcode: + return "terror:unrecognized opcode"; + case expr_eval_divide_by_zero: + return "terror:divide by zero"; + case expr_eval_invalid_goto: + return "terror:invalid goto"; + case expr_eval_invalid_memory_access: + return "terror:invalid memory access"; + } +DIAGNOSTIC_POP + + gdb_assert_not_reached ("invalid error type"); +} #endif @@ -3447,7 +3473,8 @@ stop_tracing (void) else if (expr_eval_result != expr_eval_no_error) { trace_debug ("Stopping the trace because of an expression eval error"); - tracing_stop_reason = eval_result_names[expr_eval_result]; + tracing_stop_reason + = eval_result_error_name ((enum eval_result_type) expr_eval_result); tracing_stop_tpnum = error_tracepoint->number; } #ifndef IN_PROCESS_AGENT