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 C87CF385AF9D for ; Fri, 7 Jul 2023 21:19:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C87CF385AF9D 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=1688764752; 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=RHGwjKmev3sqqbxYRVIjb2MRMk7dH2mulSQmli40MGQ=; b=iAmeBV1fS0QWvGjfcRR0np+cAIILRUj/Y37x+UGHp0T1QIrjlC9x+fYJsXf2WXvCdRHPDH 38yl+blF+ol+47Yr+eACAGzoDSdsW+ZGr7Yd31qs04FCWB6TahBqICGIgQrHkcb64zJzMh X1hIkQEbjxfBwHCyKAqH5Elee0V5L9o= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-586-FJCULuIlPsSlkbdlU5WTAw-1; Fri, 07 Jul 2023 17:19:11 -0400 X-MC-Unique: FJCULuIlPsSlkbdlU5WTAw-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-313f2430134so1173596f8f.3 for ; Fri, 07 Jul 2023 14:19:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688764750; x=1691356750; 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=RHGwjKmev3sqqbxYRVIjb2MRMk7dH2mulSQmli40MGQ=; b=Fcc9UX2h7faTHnvuXSyg+P+yTxGp0Q/IsgzXW/qiWpX4NPdWlv50FdDuyGVNpeIz3j d9qdWM1V3UanBSPevXgfxFAdXAbuECxqFKDKhFBImWe9Uesw1DhT2ORYXOTcHGwRrQjL yrrLzwlYBkD3AeQpPlFIoCiq8uSusMxRuo6rxl4ITWjkiPHP/+GNlBmNCwavqQqu7JAL Yh5VMbs6mdzUeUtXBZm1SaIFTQxEvJXikO2bMAjZ0kgeLUQzgIrON57+Ijg3h3lIDL8K LdYRQqW97Nh5m/+RDQqVhfS7xPA953TYZBvVnxd9Qvozt/zaymRFzBLGR+K3zsra0VmX Aezw== X-Gm-Message-State: ABy/qLYYFQUA3VKD3Oa8k+dMKcNg7jN6zqg1GEy28pzwdO4UhVrfsszt v2jKNKsEPy0iHLuG0rAKuSqQmUANYyLZ4D3rhdpyPiAggT36OU8WQCT7qSk04+EzIgVXdfLz8ew r8dmyR8kPFJGZMDwqYuqizExJX1z3Gg== X-Received: by 2002:a1c:4b18:0:b0:3fb:ab76:164b with SMTP id y24-20020a1c4b18000000b003fbab76164bmr4694085wma.13.1688764749886; Fri, 07 Jul 2023 14:19:09 -0700 (PDT) X-Google-Smtp-Source: APBJJlETghzNC/k54lUpawrdBEiw9LSqsFpI8eQFIWoHxR+PosLAj28b2yuMisybaPzeo2yd0itx9w== X-Received: by 2002:a1c:4b18:0:b0:3fb:ab76:164b with SMTP id y24-20020a1c4b18000000b003fbab76164bmr4694075wma.13.1688764749505; Fri, 07 Jul 2023 14:19:09 -0700 (PDT) Received: from localhost (2.72.115.87.dyn.plus.net. [87.115.72.2]) by smtp.gmail.com with ESMTPSA id f22-20020a1cc916000000b003fc00972c3esm2080537wmb.48.2023.07.07.14.19.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jul 2023 14:19:09 -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: <2d33fb4e-380a-310c-d4b4-cf3dfe69eaec@palves.net> References: <0ef54ed920e1262a07f4a061ecc08bea8fcaee23.1678987897.git.aburgess@redhat.com> <01059f8a-0e59-55b5-f530-190c26df5ba3@palves.net> <87pm53k7om.fsf@redhat.com> <2d33fb4e-380a-310c-d4b4-cf3dfe69eaec@palves.net> Date: Fri, 07 Jul 2023 22:19:08 +0100 Message-ID: <87jzvbju7n.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-07-07 17:28, Andrew Burgess wrote: >> 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? > > Hmm. If we're changing how the array is built completely, you could go a step > further and convert to a .def file (like e.g., gdb/unwind_stop_reasons.def) that is > included by both ax.h and tracepoint.cc. That would keep the enum names and their > string representations in the same place. It would also dispense with the > diagnostics pragmas, as the problem scenario goes away by design. Will do this next week and repost. Thanks, Andrew > > Pedro Alves > >> >> 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 >>