From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by sourceware.org (Postfix) with ESMTPS id DA22C3858C2F for ; Thu, 8 Jun 2023 17:53:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DA22C3858C2F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-514ae02330cso198973a12.0 for ; Thu, 08 Jun 2023 10:53:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686246795; x=1688838795; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=mjzxcZOMBVZG+EvGK8+txNB3do7j0fWJ2yisubaRzOY=; b=hcHKXQfpzW0h6HNwCcojLpLrmMaYFm1l+PveEjgviuDbWdTwegeFpy5gSWmaJjT4TC Ha5jbWSQfSGicku3LQDMd29ltplr0LIy+s1VEfgP8B6lDiJbrzs4SyDMF8e27oC70teX DE3NIonLAT2M1faC2md37u06CRGvvV6MSqnboKYQsXV5o4/5IEBJ2c23PBgZhBbyAgTu qn4ecrGhZ34UGx3BpWgi3HC1qhZv/kxZIAaHTvatax7YoVq5/3y553osf4F2TanzRKvz neBVllzj6svFnkP4wt0XyRxyg2wsZ4PKb52vrUmSq93fjH8zX6zSqBjvJBw3oQ+wGCAs n2lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686246795; x=1688838795; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mjzxcZOMBVZG+EvGK8+txNB3do7j0fWJ2yisubaRzOY=; b=VoZJZdP7F2vBHXxXBQQJbk6+FDycJizLeqeEA2nxoh8pslvE7F3OEa59O91n9Tzvg/ W1pB6VLwPIccQB2Rx8SzoNw8RW/csxD2x2rBimiE+FmXxH1CIb8JYi/TaUrGGAp/BPZX c4/w5QX4OxBB8ndcDWC86lwzQOHMvMXJzsDzsn933CqJjfTOfPM2oO+E2twE4WDpZAfx KK9mnFdjx5AsByZzxhYiBlOHRj6EaFR/eC05h9y05FJMnaJEMcT0R6xjNFyc8rmwfrdA HU35d50Pew8sRtmzMVm8wnA5ctrRf2f8DM5DYAvrn5K5yHRRfC1OiYagmjpexAhjiq+u 824A== X-Gm-Message-State: AC+VfDz74Sd/4SoWpN92ELEy6Dh1PtVVZLPM+q4Z+xD2SvwnjkaG5B7X +VJ1Qb+FXPWnX99Vlw8GAaVd X-Google-Smtp-Source: ACHHUZ5f0KPV+UICu85ONMDOWtTn7SYDI4XKsnZbhRk1EsKBkDz/9c6RlpLZ1DKPp/HuE50q8r6bbA== X-Received: by 2002:a17:906:648e:b0:94a:5f0d:d9d6 with SMTP id e14-20020a170906648e00b0094a5f0dd9d6mr6103439ejm.4.1686246795213; Thu, 08 Jun 2023 10:53:15 -0700 (PDT) Received: from smtpclient.apple ([2a00:cc47:21fb:f100:5c7:9e40:49bd:38e7]) by smtp.gmail.com with ESMTPSA id h17-20020a1709063b5100b0097886136c02sm124891ejf.151.2023.06.08.10.53.13 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2023 10:53:14 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.500.231\)) Subject: Re: [PATCH] analyzer: Standalone OOB-warning [PR109437, PR109439] From: Maxim Kuvyrkov In-Reply-To: <20230606114858.447221-1-vultkayn@gcc.gnu.org> Date: Thu, 8 Jun 2023 21:53:02 +0400 Cc: gcc-patches@gcc.gnu.org, Benjamin Priour , dmalcolm@redhat.com Content-Transfer-Encoding: quoted-printable Message-Id: <225A7AAD-E31D-4BF1-AC1F-CE160989FE60@linaro.org> References: <20230606114858.447221-1-vultkayn@gcc.gnu.org> To: priour.be@gmail.com X-Mailer: Apple Mail (2.3731.500.231) X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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: > On Jun 6, 2023, at 15:48, Benjamin Priour via Gcc-patches = wrote: >=20 > From: Benjamin Priour >=20 > This patch enchances -Wanalyzer-out-of-bounds that is no longer paired > with a -Wanalyzer-use-of-uninitialized-value on out-of-bounds-read. >=20 > This also fixes PR analyzer/109437. > Before there could always be at most one OOB-read warning per frame = because > -Wanalyzer-use-of-uninitialized-value always terminates the analysis > path. >=20 > PR analyzer/109439 >=20 > gcc/analyzer/ChangeLog: >=20 > * bounds-checking.cc (region_model::check_symbolic_bounds): > Returns whether the BASE_REG region access was OOB. > (region_model::check_region_bounds): Likewise. > * region-model.cc (region_model::get_store_value): Creates an > unknown svalue on OOB-read access to REG. > (region_model::check_region_access): Returns whether an > unknown svalue needs be created. > (region_model::check_region_for_read): Passes > check_region_access return value. > * region-model.h: Update prior function definitions. >=20 > gcc/testsuite/ChangeLog: >=20 > * gcc.dg/analyzer/out-of-bounds-2.c: Cleaned test for > uninitialized-value warning. > * gcc.dg/analyzer/out-of-bounds-5.c: Likewise. > * gcc.dg/analyzer/pr101962.c: Likewise. > * gcc.dg/analyzer/realloc-5.c: Likewise. > * gcc.dg/analyzer/pr109439.c: New test. > --- Hi Benjamin, This patch makes two tests fail on arm-linux-gnueabihf. Probably, they = need to be updated as well. Would you please investigate? Let me know = if it doesn't easily reproduce for you, and I'll help.=20 =3D=3D=3D g++ tests =3D=3D=3D Running g++:g++.dg/analyzer/analyzer.exp ... FAIL: g++.dg/analyzer/pr100244.C -std=3Dc++14 (test for warnings, line = 17) FAIL: g++.dg/analyzer/pr100244.C -std=3Dc++17 (test for warnings, line = 17) FAIL: g++.dg/analyzer/pr100244.C -std=3Dc++20 (test for warnings, line = 17) =3D=3D=3D gcc tests =3D=3D=3D Running gcc:gcc.dg/analyzer/analyzer.exp ... FAIL: gcc.dg/analyzer/pr101962.c (test for warnings, line 19) Thanks, -- Maxim Kuvyrkov https://www.linaro.org > gcc/analyzer/bounds-checking.cc | 30 +++++++++++++------ > gcc/analyzer/region-model.cc | 22 +++++++++----- > gcc/analyzer/region-model.h | 8 ++--- > .../gcc.dg/analyzer/out-of-bounds-2.c | 1 - > .../gcc.dg/analyzer/out-of-bounds-5.c | 2 -- > gcc/testsuite/gcc.dg/analyzer/pr101962.c | 1 - > gcc/testsuite/gcc.dg/analyzer/pr109439.c | 12 ++++++++ > gcc/testsuite/gcc.dg/analyzer/realloc-5.c | 1 - > 8 files changed, 51 insertions(+), 26 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/analyzer/pr109439.c >=20 > diff --git a/gcc/analyzer/bounds-checking.cc = b/gcc/analyzer/bounds-checking.cc > index 3bf542a8eba..479b8e4b88d 100644 > --- a/gcc/analyzer/bounds-checking.cc > +++ b/gcc/analyzer/bounds-checking.cc > @@ -767,9 +767,11 @@ public: > } > }; >=20 > -/* Check whether an access is past the end of the BASE_REG. */ > +/* Check whether an access is past the end of the BASE_REG. > + Return TRUE if the access was valid, FALSE otherwise. > +*/ >=20 > -void > +bool > region_model::check_symbolic_bounds (const region *base_reg, > const svalue *sym_byte_offset, > const svalue *num_bytes_sval, > @@ -800,6 +802,7 @@ region_model::check_symbolic_bounds (const region = *base_reg, > offset_tree, > num_bytes_tree, > capacity_tree)); > + return false; > break; > case DIR_WRITE: > ctxt->warn (make_unique (base_reg, > @@ -807,9 +810,11 @@ region_model::check_symbolic_bounds (const region = *base_reg, > offset_tree, > num_bytes_tree, > capacity_tree)); > + return false; > break; > } > } > + return true; > } >=20 > static tree > @@ -822,9 +827,11 @@ maybe_get_integer_cst_tree (const svalue *sval) > return NULL_TREE; > } >=20 > -/* May complain when the access on REG is out-of-bounds. */ > +/* May complain when the access on REG is out-of-bounds. > + Return TRUE if the access was valid, FALSE otherwise. > +*/ >=20 > -void > +bool > region_model::check_region_bounds (const region *reg, > enum access_direction dir, > region_model_context *ctxt) const > @@ -839,14 +846,14 @@ region_model::check_region_bounds (const region = *reg, > (e.g. because the analyzer did not see previous offsets on the = latter, > it might think that a negative access is before the buffer). */ > if (base_reg->symbolic_p ()) > - return; > + return true; >=20 > /* Find out how many bytes were accessed. */ > const svalue *num_bytes_sval =3D reg->get_byte_size_sval (m_mgr); > tree num_bytes_tree =3D maybe_get_integer_cst_tree (num_bytes_sval); > /* Bail out if 0 bytes are accessed. */ > if (num_bytes_tree && zerop (num_bytes_tree)) > - return; > + return true; >=20 > /* Get the capacity of the buffer. */ > const svalue *capacity =3D get_capacity (base_reg); > @@ -877,13 +884,13 @@ region_model::check_region_bounds (const region = *reg, > } > else > byte_offset_sval =3D reg_offset.get_symbolic_byte_offset (); > - check_symbolic_bounds (base_reg, byte_offset_sval, = num_bytes_sval, > + return check_symbolic_bounds (base_reg, byte_offset_sval, = num_bytes_sval, > capacity, dir, ctxt); > - return; > } >=20 > /* Otherwise continue to check with concrete values. */ > byte_range out (0, 0); > + bool oob_safe =3D true; > /* NUM_BYTES_TREE should always be interpreted as unsigned. */ > byte_offset_t num_bytes_unsigned =3D wi::to_offset (num_bytes_tree); > byte_range read_bytes (offset, num_bytes_unsigned); > @@ -899,10 +906,12 @@ region_model::check_region_bounds (const region = *reg, > case DIR_READ: > ctxt->warn (make_unique (reg, diag_arg, > out)); > + oob_safe =3D false; > break; > case DIR_WRITE: > ctxt->warn (make_unique (reg, diag_arg, > out)); > + oob_safe =3D false; > break; > } > } > @@ -911,7 +920,7 @@ region_model::check_region_bounds (const region = *reg, > do a symbolic check here because the inequality check does not = reason > whether constants are greater than symbolic values. */ > if (!cst_capacity_tree) > - return; > + return oob_safe; >=20 > byte_range buffer (0, wi::to_offset (cst_capacity_tree)); > /* If READ_BYTES exceeds BUFFER, we do have an overflow. */ > @@ -929,13 +938,16 @@ region_model::check_region_bounds (const region = *reg, > case DIR_READ: > ctxt->warn (make_unique (reg, diag_arg, > out, byte_bound)); > + oob_safe =3D false; > break; > case DIR_WRITE: > ctxt->warn (make_unique (reg, diag_arg, > out, byte_bound)); > + oob_safe =3D false; > break; > } > } > + return oob_safe; > } >=20 > } // namespace ana > diff --git a/gcc/analyzer/region-model.cc = b/gcc/analyzer/region-model.cc > index 3bb3df2f063..fb96cd54940 100644 > --- a/gcc/analyzer/region-model.cc > +++ b/gcc/analyzer/region-model.cc > @@ -2396,7 +2396,8 @@ region_model::get_store_value (const region = *reg, > if (reg->empty_p ()) > return m_mgr->get_or_create_unknown_svalue (reg->get_type ()); >=20 > - check_region_for_read (reg, ctxt); > + if (check_region_for_read (reg, ctxt)) > + return m_mgr->get_or_create_unknown_svalue(reg->get_type()); >=20 > /* Special-case: handle var_decls in the constant pool. */ > if (const decl_region *decl_reg =3D reg->dyn_cast_decl_region ()) > @@ -2802,19 +2803,22 @@ region_model::get_string_size (const region = *reg) const > } >=20 > /* If CTXT is non-NULL, use it to warn about any problems accessing = REG, > - using DIR to determine if this access is a read or write. */ > + using DIR to determine if this access is a read or write. > + Return TRUE if an UNKNOWN_SVALUE needs be created. */ >=20 > -void > +bool > region_model::check_region_access (const region *reg, > enum access_direction dir, > region_model_context *ctxt) const > { > /* Fail gracefully if CTXT is NULL. */ > if (!ctxt) > - return; > + return false; >=20 > + bool need_unknown_sval =3D false; > check_region_for_taint (reg, dir, ctxt); > - check_region_bounds (reg, dir, ctxt); > + if (!check_region_bounds (reg, dir, ctxt)) > + need_unknown_sval =3D true; >=20 > switch (dir) > { > @@ -2827,6 +2831,7 @@ region_model::check_region_access (const region = *reg, > check_for_writable_region (reg, ctxt); > break; > } > + return need_unknown_sval; > } >=20 > /* If CTXT is non-NULL, use it to warn about any problems writing to = REG. */ > @@ -2838,13 +2843,14 @@ region_model::check_region_for_write (const = region *dest_reg, > check_region_access (dest_reg, DIR_WRITE, ctxt); > } >=20 > -/* If CTXT is non-NULL, use it to warn about any problems reading = from REG. */ > +/* If CTXT is non-NULL, use it to warn about any problems reading = from REG. > + Returns TRUE if an unknown svalue needs be created. */ >=20 > -void > +bool > region_model::check_region_for_read (const region *src_reg, > region_model_context *ctxt) const > { > - check_region_access (src_reg, DIR_READ, ctxt); > + return check_region_access (src_reg, DIR_READ, ctxt); > } >=20 > /* Concrete subclass for casts of pointers that lead to trailing = bytes. */ > diff --git a/gcc/analyzer/region-model.h b/gcc/analyzer/region-model.h > index fe3db0b0c98..12f84b20463 100644 > --- a/gcc/analyzer/region-model.h > +++ b/gcc/analyzer/region-model.h > @@ -553,22 +553,22 @@ private: >=20 > void check_for_writable_region (const region* dest_reg, > region_model_context *ctxt) const; > - void check_region_access (const region *reg, > + bool check_region_access (const region *reg, > enum access_direction dir, > region_model_context *ctxt) const; > - void check_region_for_read (const region *src_reg, > + bool check_region_for_read (const region *src_reg, > region_model_context *ctxt) const; > void check_region_size (const region *lhs_reg, const svalue = *rhs_sval, > region_model_context *ctxt) const; >=20 > /* Implemented in bounds-checking.cc */ > - void check_symbolic_bounds (const region *base_reg, > + bool check_symbolic_bounds (const region *base_reg, > const svalue *sym_byte_offset, > const svalue *num_bytes_sval, > const svalue *capacity, > enum access_direction dir, > region_model_context *ctxt) const; > - void check_region_bounds (const region *reg, enum access_direction = dir, > + bool check_region_bounds (const region *reg, enum access_direction = dir, > region_model_context *ctxt) const; >=20 > void check_call_args (const call_details &cd) const; > diff --git a/gcc/testsuite/gcc.dg/analyzer/out-of-bounds-2.c = b/gcc/testsuite/gcc.dg/analyzer/out-of-bounds-2.c > index 1330090f348..336f624441c 100644 > --- a/gcc/testsuite/gcc.dg/analyzer/out-of-bounds-2.c > +++ b/gcc/testsuite/gcc.dg/analyzer/out-of-bounds-2.c > @@ -82,5 +82,4 @@ void test5 (void) > /* { dg-warning "heap-based buffer over-read" "bounds warning" { = target *-*-* } test5 } */ > /* { dg-message "read of 4 bytes from after the end of the region" = "num bad bytes note" { target *-*-* } test5 } */ >=20 > - /* { dg-warning "use of uninitialized value" "uninit warning" { = target *-*-* } test5 } */ > } > diff --git a/gcc/testsuite/gcc.dg/analyzer/out-of-bounds-5.c = b/gcc/testsuite/gcc.dg/analyzer/out-of-bounds-5.c > index 2a61d8ca236..568f9cad199 100644 > --- a/gcc/testsuite/gcc.dg/analyzer/out-of-bounds-5.c > +++ b/gcc/testsuite/gcc.dg/analyzer/out-of-bounds-5.c > @@ -68,7 +68,6 @@ void test8 (size_t size, size_t offset) > char dst[size]; > memcpy (dst, src, size + offset); /* { dg-line test8 } */ > /* { dg-warning "over-read" "warning" { target *-*-* } test8 } */ > - /* { dg-warning "use of uninitialized value" "warning" { target = *-*-* } test8 } */ > /* { dg-warning "overflow" "warning" { target *-*-* } test8 } */ > } >=20 > @@ -78,7 +77,6 @@ void test9 (size_t size, size_t offset) > int32_t dst[size]; > memcpy (dst, src, 4 * size + 1); /* { dg-line test9 } */ > /* { dg-warning "over-read" "warning" { target *-*-* } test9 } */ > - /* { dg-warning "use of uninitialized value" "warning" { target = *-*-* } test9 } */ > /* { dg-warning "overflow" "warning" { target *-*-* } test9 } */ > } >=20 > diff --git a/gcc/testsuite/gcc.dg/analyzer/pr101962.c = b/gcc/testsuite/gcc.dg/analyzer/pr101962.c > index 08c0aba5cbf..b878aad9cf1 100644 > --- a/gcc/testsuite/gcc.dg/analyzer/pr101962.c > +++ b/gcc/testsuite/gcc.dg/analyzer/pr101962.c > @@ -24,7 +24,6 @@ test_1 (void) > __analyzer_eval (a !=3D NULL); /* { dg-warning "TRUE" } */ > return *a; /* { dg-line test_1 } */ >=20 > - /* { dg-warning "use of uninitialized value '\\*a'" "warning" { = target *-*-* } test_1 } */ > /* { dg-warning "stack-based buffer over-read" "warning" { target = *-*-* } test_1 } */ > } >=20 > diff --git a/gcc/testsuite/gcc.dg/analyzer/pr109439.c = b/gcc/testsuite/gcc.dg/analyzer/pr109439.c > new file mode 100644 > index 00000000000..01c87cf171c > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/analyzer/pr109439.c > @@ -0,0 +1,12 @@ > +int would_like_only_oob (int i) > +{ > + int arr[] =3D {1,2,3,4,5,6,7}; > + arr[10] =3D 9; /* { dg-warning "stack-based buffer overflow" } */ > + arr[11] =3D 15; /* { dg-warning "stack-based buffer overflow" } = */ > + int y1 =3D arr[9]; /* { dg-warning "stack-based buffer over-read" = } */ > + /* { dg-bogus "use of uninitialized value" "" { target = *-*-* } .-1 } */ > + > + arr[18] =3D 15; /* { dg-warning "stack-based buffer overflow" } = */ > + =20 > + return y1; > +} > diff --git a/gcc/testsuite/gcc.dg/analyzer/realloc-5.c = b/gcc/testsuite/gcc.dg/analyzer/realloc-5.c > index 137e05b87aa..f65f2c6ca25 100644 > --- a/gcc/testsuite/gcc.dg/analyzer/realloc-5.c > +++ b/gcc/testsuite/gcc.dg/analyzer/realloc-5.c > @@ -40,7 +40,6 @@ void test_1 () >=20 > /* { dg-warning "UNKNOWN" "warning" { target *-*-* } eval } */ > /* { dg-warning "heap-based buffer over-read" "warning" { target = *-*-* } eval } */ > - /* { dg-warning "use of uninitialized value" "warning" { target = *-*-* } eval } */ > } >=20 > free (q); > --=20 > 2.34.1 >=20