public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug analyzer/106626] Improvements to wording of -Wanalyzer-out-of-bounds
Date: Thu, 22 Jun 2023 02:06:17 +0000	[thread overview]
Message-ID: <bug-106626-4-Gnf9wu7yS9@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-106626-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106626

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by David Malcolm <dmalcolm@gcc.gnu.org>:

https://gcc.gnu.org/g:0e466e978c728697f18c67c4eace9ba4633f9ef5

commit r14-2029-g0e466e978c728697f18c67c4eace9ba4633f9ef5
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Wed Jun 21 21:49:00 2023 -0400

    analyzer: add text-art visualizations of out-of-bounds accesses [PR106626]

    This patch extends -Wanalyzer-out-of-bounds so that, where possible, it
    will emit a text art diagram visualizing the spatial relationship between
    (a) the memory region that the analyzer predicts would be accessed, versus
    (b) the range of memory that is valid to access - whether they overlap,
    are touching, are close or far apart; which one is before or after in
    memory, the relative sizes involved, the direction of the access (read vs
    write), and, in some cases, the values of data involved.  This diagram
    can be suppressed using -fdiagnostics-text-art-charset=none.

    For example, given:

    int32_t arr[10];

    int32_t int_arr_read_element_before_start_far(void)
    {
      return arr[-100];
    }

    it emits:

    demo-1.c: In function âint_arr_read_element_before_start_farâ:
    demo-1.c:7:13: warning: buffer under-read [CWE-127]
[-Wanalyzer-out-of-bounds]
        7 |   return arr[-100];
          |          ~~~^~~~~~
      âint_arr_read_element_before_start_farâ: event 1
        |
        |    7 |   return arr[-100];
        |      |          ~~~^~~~~~
        |      |             |
        |      |             (1) out-of-bounds read from byte -400 till byte
-397 but âarrâ starts at byte 0
        |
    demo-1.c:7:13: note: valid subscripts for âarrâ are â[0]â to
â[9]â

     
âââââââââââââââââââââââââââââ
      âread of âint32_tâ (4 bytes)â
     
âââââââââââââââââââââââââââââ
                    ^
                    â
                    â
     
âââââââââââââââââââââââââââââ
            
ââââââââââ¬âââââââââ¬ââââââââââ
      â                           â              â  [0]   â  ...   â 
 [9]   â
      â    before valid range     â             
ââââââââââ´âââââââââ´ââââââââââ¤
      â                           â              ââarrâ (type:
âint32_t[10]â)â
     
âââââââââââââââââââââââââââââ
            
âââââââââââââââââââââââââââââ
     
âââââââââââââââ¬ââââââââââââââ¤âââââââ¬âââââââ¤âââââââââââââââ¬ââââââââââââââ¤
                    â                    â                     â
      
â­âââââââââââââ´ââââââââââââ® 
 â­âââââ´âââââ®       
â­ââââââââ´âââââââ®
       ââ ï¸  under-read of 4 bytesâ   â396 bytesâ        âsize: 40
bytesâ
      
â°ââââââââââââââââââââââââ⯠
 â°âââââââââ⯠      
â°âââââââââââââââ¯

    and given:

      #include <string.h>

      void
      test_non_ascii ()
      {
        char buf[5];
        strcpy (buf, "æå­åã");
      }

    it emits:

    demo-2.c: In function âtest_non_asciiâ:
    demo-2.c:7:3: warning: stack-based buffer overflow [CWE-121]
[-Wanalyzer-out-of-bounds]
        7 |   strcpy (buf, "æå­åã");
          |   ^~~~~~~~~~~~~~~~~~~~~~~~
      âtest_non_asciiâ: events 1-2
        |
        |    6 |   char buf[5];
        |      |        ^~~
        |      |        |
        |      |        (1) capacity: 5 bytes
        |    7 |   strcpy (buf, "æå­åã");
        |      |   ~~~~~~~~~~~~~~~~~~~~~~~~
        |      |   |
        |      |   (2) out-of-bounds write from byte 5 till byte 12 but
âbufâ ends at byte 5
        |
    demo-2.c:7:3: note: write of 8 bytes to beyond the end of âbufâ
        7 |   strcpy (buf, "æå­åã");
          |   ^~~~~~~~~~~~~~~~~~~~~~~~
    demo-2.c:7:3: note: valid subscripts for âbufâ are â[0]â to
â[4]â

     
âââââââ¬ââââââ¬ââââââ¬âââââ¬âââââââââââ¬âââââ¬âââââ¬âââââ¬âââââ¬âââââ¬âââââ¬âââââââ
      â [0] â [1] â [2] â[3] â[4] ââ[5] â[6] â[7] â[8]
â[9] â[10]â[11]â [12] â
     
âââââââ¼ââââââ¼ââââââ¼âââââ¼âââââ¤ââââââ¼âââââ¼âââââ¼âââââ¼âââââ¼âââââ¼âââââ¼âââââââ¤
      â0xe6 â0x96 â0x87
â0xe5â0xadââ0x97â0xe5â0x8câ0x96â0xe3â0x81â0x91â 0x00 â
     
âââââââ´ââââââ´ââââââ¼âââââ´âââââ´â´âââââ¼âââââ´âââââ´âââââ¼âââââ´âââââ´âââââ¼âââââââ¤
      â     U+6587      â    U+5b57     â    U+5316    â    U+3051   
âU+0000â
     
âââââââââââââââââââ¼ââââââââââââââââ¼âââââââââââââââ¼âââââââââââââââ¼âââââââ¤
      â       æ        â      å­       â      å      â      ã   
  â NUL  â
     
âââââââââââââââââââ´ââââââââââââââââ´âââââââââââââââ´âââââââââââââââ´âââââââ¤
      â                  string literal (type: âchar[13]â)               
   â
     
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
         â     â     â    â    â     â    â    â    â    â 
  â    â     â
         â     â     â    â    â     â    â    â    â    â 
  â    â     â
         v     v     v    v    v     v    v    v    v    v    v    v     v
     
âââââââ¬âââââââââââââââââ¬ââââââââââââââââââââââââââââââââââââââââââââââââ
      â [0] â      ...       â[4] ââ                                 
       â
     
âââââââ´âââââââââââââââââ´âââââ¤â
           after valid range            â
      â  âbufâ (type: âchar[5]â)  ââ                             
           â
     
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
     
âââââââââââââââ¬ââââââââââââââ¤ââââââââââââââââââââââ¬âââââââââââââââââââââ¤
                    â                                   â
           â­âââââââââ´âââââââââ®           
  â­ââââââââââââ´âââââââââââ®
           âcapacity: 5 bytesâ              ââ ï¸  overflow of 8
bytesâ
           â°âââââââââââââââââ⯠          
  â°âââââââââââââââââââââââ¯

    showing that the overflow occurs partway through the UTF-8 encoding of
    the U+5b57 code point.

    There are lots more examples in the test suite.

    It doesn't show up in this email, but the above diagrams are colorized
    to constrast the valid and invalid access ranges.

    gcc/ChangeLog:
            PR analyzer/106626
            * Makefile.in (ANALYZER_OBJS): Add analyzer/access-diagram.o.
            * doc/invoke.texi (Wanalyzer-out-of-bounds): Add description of
            text art.
            (fanalyzer-debug-text-art): New.

    gcc/analyzer/ChangeLog:
            PR analyzer/106626
            * access-diagram.cc: New file.
            * access-diagram.h: New file.
            * analyzer.h (class region_offset): Add default ctor.
            (region_offset::make_byte_offset): New decl.
            (region_offset::concrete_p): New.
            (region_offset::get_concrete_byte_offset): New.
            (region_offset::calc_symbolic_bit_offset): New decl.
            (region_offset::calc_symbolic_byte_offset): New decl.
            (region_offset::dump_to_pp): New decl.
            (region_offset::dump): New decl.
            (operator<, operator<=, operator>, operator>=): New decls for
            region_offset.
            * analyzer.opt
            (-param=analyzer-text-art-string-ellipsis-threshold=): New.
            (-param=analyzer-text-art-string-ellipsis-head-len=): New.
            (-param=analyzer-text-art-string-ellipsis-tail-len=): New.
            (-param=analyzer-text-art-ideal-canvas-width=): New.
            (fanalyzer-debug-text-art): New.
            * bounds-checking.cc: Include "intl.h", "diagnostic-diagram.h",
            and "analyzer/access-diagram.h".
            (class out_of_bounds::oob_region_creation_event_capacity): New.
            (out_of_bounds::out_of_bounds): Add "model" and "sval_hint"
            params.
            (out_of_bounds::mark_interesting_stuff): Use the base region.
            (out_of_bounds::add_region_creation_events): Use
            oob_region_creation_event_capacity.
            (out_of_bounds::get_dir): New pure vfunc.
            (out_of_bounds::maybe_show_notes): New.
            (out_of_bounds::maybe_show_diagram): New.
            (out_of_bounds::make_access_diagram): New.
            (out_of_bounds::m_model): New field.
            (out_of_bounds::m_sval_hint): New field.
            (out_of_bounds::m_region_creation_event_id): New field.
            (concrete_out_of_bounds::concrete_out_of_bounds): Update for new
            fields.
            (concrete_past_the_end::concrete_past_the_end): Likewise.
            (concrete_past_the_end::add_region_creation_events): Use
            oob_region_creation_event_capacity.
            (concrete_buffer_overflow::concrete_buffer_overflow): Update for
            new fields.
            (concrete_buffer_overflow::emit): Replace call to
            maybe_describe_array_bounds with maybe_show_notes.
            (concrete_buffer_overflow::get_dir): New.
            (concrete_buffer_over_read::concrete_buffer_over_read): Update for
            new fields.
            (concrete_buffer_over_read::emit): Replace call to
            maybe_describe_array_bounds with maybe_show_notes.
            (concrete_buffer_overflow::get_dir): New.
            (concrete_buffer_underwrite::concrete_buffer_underwrite): Update
            for new fields.
            (concrete_buffer_underwrite::emit): Replace call to
            maybe_describe_array_bounds with maybe_show_notes.
            (concrete_buffer_underwrite::get_dir): New.
            (concrete_buffer_under_read::concrete_buffer_under_read): Update
            for new fields.
            (concrete_buffer_under_read::emit): Replace call to
            maybe_describe_array_bounds with maybe_show_notes.
            (concrete_buffer_under_read::get_dir): New.
            (symbolic_past_the_end::symbolic_past_the_end): Update for new
            fields.
            (symbolic_buffer_overflow::symbolic_buffer_overflow): Likewise.
            (symbolic_buffer_overflow::emit): Call maybe_show_notes.
            (symbolic_buffer_overflow::get_dir): New.
            (symbolic_buffer_over_read::symbolic_buffer_over_read): Update for
            new fields.
            (symbolic_buffer_over_read::emit): Call maybe_show_notes.
            (symbolic_buffer_over_read::get_dir): New.
            (region_model::check_symbolic_bounds): Add "sval_hint" param.  Pass
            it and sized_offset_reg to diagnostics.
            (region_model::check_region_bounds): Add "sval_hint" param, passing
            it to diagnostics.
            * diagnostic-manager.cc
            (diagnostic_manager::emit_saved_diagnostic): Pass logger to
            pending_diagnostic::emit.
            * engine.cc: Add logger param to pending_diagnostic::emit
            implementations.
            * infinite-recursion.cc: Likewise.
            * kf-analyzer.cc: Likewise.
            * kf.cc: Likewise.  Add nullptr for new param of
            check_region_for_write.
            * pending-diagnostic.h: Likewise in decl.
            * region-model-manager.cc
            (region_model_manager::get_or_create_int_cst): Convert param from
            poly_int64 to const poly_wide_int_ref &.
            (region_model_manager::maybe_fold_binop): Support type being NULL
            when checking for floating-point types.
            Check for (X + Y) - X => Y.  Be less strict about types when
folding
            associative ops.  Check for (X + Y) * CST => (X * CST) + (Y * CST).
            * region-model-manager.h
            (region_model_manager::get_or_create_int_cst): Convert param from
            poly_int64 to const poly_wide_int_ref &.
            * region-model.cc: Add logger param to pending_diagnostic::emit
            implementations.
            (region_model::check_external_function_for_access_attr): Update
            for new param of check_region_for_write.
            (region_model::deref_rvalue): Use nullptr rather than NULL.
            (region_model::get_capacity): Handle RK_STRING.
            (region_model::check_region_access): Add "sval_hint" param; pass it
to
            check_region_bounds.
            (region_model::check_region_for_write): Add "sval_hint" param;
            pass it to check_region_access.
            (region_model::check_region_for_read): Add NULL for new param to
            check_region_access.
            (region_model::set_value): Pass rhs_sval to
            check_region_for_write.
            (region_model::get_representative_path_var_1): Handle SK_CONSTANT
            in the check for infinite recursion.
            * region-model.h (region_model::check_region_for_write): Add
            "sval_hint" param.
            (region_model::check_region_access): Likewise.
            (region_model::check_symbolic_bounds): Likewise.
            (region_model::check_region_bounds): Likewise.
            * region.cc (region_offset::make_byte_offset): New.
            (region_offset::calc_symbolic_bit_offset): New.
            (region_offset::calc_symbolic_byte_offset): New.
            (region_offset::dump_to_pp): New.
            (region_offset::dump): New.
            (struct linear_op): New.
            (operator<, operator<=, operator>, operator>=): New, for
            region_offset.
            (region::get_next_offset): New.
            (region::get_relative_symbolic_offset): Use ptrdiff_type_node.
            (field_region::get_relative_symbolic_offset): Likewise.
            (element_region::get_relative_symbolic_offset): Likewise.
            (bit_range_region::get_relative_symbolic_offset): Likewise.
            * region.h (region::get_next_offset): New decl.
            * sm-fd.cc: Add logger param to pending_diagnostic::emit
            implementations.
            * sm-file.cc: Likewise.
            * sm-malloc.cc: Likewise.
            * sm-pattern-test.cc: Likewise.
            * sm-sensitive.cc: Likewise.
            * sm-signal.cc: Likewise.
            * sm-taint.cc: Likewise.
            * store.cc (bit_range::contains_p): Allow "out" to be null.
            * store.h (byte_range::get_start_bit_offset): New.
            (byte_range::get_next_bit_offset): New.
            * varargs.cc: Add logger param to pending_diagnostic::emit
            implementations.

    gcc/testsuite/ChangeLog:
            PR analyzer/106626
            * gcc.dg/analyzer/data-model-1.c (test_16): Update for
            out-of-bounds working.
            * gcc.dg/analyzer/out-of-bounds-diagram-1-ascii.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-1-debug.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-1-emoji.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-1-json.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-1-sarif.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-1-unicode.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-10.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-11.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-12.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-13.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-14.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-15.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-2.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-3.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-4.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-5-ascii.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-5-unicode.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-6.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-7.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-8.c: New test.
            * gcc.dg/analyzer/out-of-bounds-diagram-9.c: New test.
            * gcc.dg/analyzer/pattern-test-2.c: Update expected results.
            * gcc.dg/analyzer/pr101962.c: Update expected results.
            * gcc.dg/plugin/analyzer_gil_plugin.c:  Add logger param to
            pending_diagnostic::emit implementations.

    Signed-off-by: David Malcolm <dmalcolm@redhat.com>

      parent reply	other threads:[~2023-06-22  2:06 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-15 13:08 [Bug analyzer/106626] New: " dmalcolm at gcc dot gnu.org
2022-08-15 18:48 ` [Bug analyzer/106626] " cvs-commit at gcc dot gnu.org
2022-12-01  2:31 ` cvs-commit at gcc dot gnu.org
2022-12-01  2:31 ` cvs-commit at gcc dot gnu.org
2022-12-01  2:31 ` cvs-commit at gcc dot gnu.org
2022-12-01  2:31 ` cvs-commit at gcc dot gnu.org
2022-12-01  2:31 ` cvs-commit at gcc dot gnu.org
2023-04-07 12:36 ` dmalcolm at gcc dot gnu.org
2023-06-22  2:06 ` cvs-commit at gcc dot gnu.org [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-106626-4-Gnf9wu7yS9@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).