Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReleaseSmall runtime error incorrectly returned. #20701

Open
gcoakes opened this issue Jul 21, 2024 · 1 comment
Open

ReleaseSmall runtime error incorrectly returned. #20701

gcoakes opened this issue Jul 21, 2024 · 1 comment
Labels
arch-riscv RISCV-32 and RISCV-64 backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code. upstream An issue with a third party project that Zig uses.
Milestone

Comments

@gcoakes
Copy link
Contributor

gcoakes commented Jul 21, 2024

Zig Version

0.14.0-dev.367+a57479afc

Steps to Reproduce and Observed Behavior

  1. Clone my (embarrassingly incomplete) kernel:
$ git clone -b erroneous-error https://git.sr.ht/~gcoakes/kernel
  1. Ensure qemu-system-riscv64 is installed and available within your PATH environment variable:
$ which qemu-system-riscv64
  1. Run it using ReleaseSmall optimization mode (press CTRL+a, x to exit after it fails):
$ zig build run -Doptimize=ReleaseSmall
...
[ info] (default) 8273 + 49 > 8712
panic: failed to parse root task executable: ElfOutOfBounds
   0: 0x000000008020311e
   1: 0x000a7d7b20656369
  1. Observe an error printed in the last step and observe the impossibility of the conditional that produces that error (src/bootstrap/Elf.zig#L324):
    std.log.info("{} + {} > {}", .{ names_section.offset, names_section.size, data.len });
    if (names_section.offset + names_section.size > data.len)
        return error.ElfOutOfBounds;

The log statement in that function prints: 8273 + 49 > 8712

Note that the specific error which is returned seems to be that of the last defined return location within that function. If I comment out the conditional/return statement from above, it then starts returning the next last defined return error.

I apologize for not being able to minimize this further.

Expected Behavior

The run command is expected to exit without any errors being printed. This can be observed by running the same command with either -Doptimize=ReleaseSafe or no optimization hint (Debug mode):

$ zig build run -Doptimize=ReleaseSafe
...
[ info] (default) 20280 + 174 > 21616
@gcoakes gcoakes added the bug Observed behavior contradicts documented or intended behavior label Jul 21, 2024
@mlugg
Copy link
Member

mlugg commented Jul 21, 2024

I should note that this seemingly-impossible behavior could happen if your code exhibited Illegal Behavior -- LLVM can and sometimes will do incredibly strange things in such cases. However, the only thing that stands out to me as suspicious is your @alignCast, but if it were invalid ReleaseSafe would catch it, so this is most likely a bug.

(Note: I haven't confirmed this is an upstream bug, but it almost certainly is, so I'll label it as such for now.)

@mlugg mlugg added upstream An issue with a third party project that Zig uses. miscompilation The compiler reports success but produces semantically incorrect code. arch-riscv RISCV-32 and RISCV-64 backend-llvm The LLVM backend outputs an LLVM IR Module. labels Jul 21, 2024
@mlugg mlugg added this to the 0.15.0 milestone Jul 21, 2024
@andrewrk andrewrk modified the milestones: 0.15.0, unplanned Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-riscv RISCV-32 and RISCV-64 backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code. upstream An issue with a third party project that Zig uses.
Projects
None yet
Development

No branches or pull requests

3 participants