Rust-GCC / Rust-GCC/gccrs

GCCRS does not check integers for zero before division

Open
#793 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C++
Stars
2.9k
Forks
231
Avg merge
19h 55m
Merged PRs (30d)
67

Description

As seen here, when dividing a variable with a statically unknown value by itself, GCCRS generates an instruction to generate the statically known value 1, based on the assumption that the denominator will not be zero.

        mov     eax, 1
        ret

However, this is not undefined behaviour in Rust, and should panic instead. The Rust reference compiler version 1.56.0 (using the default LLVM backend) also avoids using a division instruction and generates a static 1 instead, but first checks to see if the denominator is zero.

        test    rdi, rdi
        je      .LBB0_2
        mov     eax, 1
        ret
.LBB0_2:
        push    rax
        lea     rdi, [rip + str.0]
        lea     rdx, [rip + .L__unnamed_1]
        mov     esi, 25
        call    qword ptr [rip + core::panicking::panic@GOTPCREL]
        ud2

The reference compiler also avoids this when using the GCC backend.

       test    rdi, rdi
       je      .L14
       mov     eax, 1
       ret
.L14:
       push    rax
       mov     rdx, QWORD PTR global_3_example_955dbcfe_cgu_0@GOTPCREL[rip]
       mov     esi, 25
       mov     rdi, QWORD PTR str.0@GOTPCREL[rip]
       call    core::panicking::panic@PLT

The version of GCCRS being tested was gcc version 12.0.0 20210917 (experimental) (Compiler-Explorer-Build-gcc-5f0df4812c37fc428b5508e019e9fb7f8a7b77b1-binutils-2.36.1).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the linked Compiler Explorer examples and compare GCCRS with the reference compiler for division by a possibly zero value. Trace the GCCRS integer-division handling; done means zero denominators panic while nonzero values still produce the optimized result shown in the report.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.