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

Compiler bug: invalid Int32 when calling offsetof(lib union) #9744

Closed
HertzDevil opened this issue Sep 13, 2020 · 2 comments · Fixed by #13305
Closed

Compiler bug: invalid Int32 when calling offsetof(lib union) #9744

HertzDevil opened this issue Sep 13, 2020 · 2 comments · Fixed by #13305
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:codegen

Comments

@HertzDevil
Copy link
Contributor

{"" => [] of Int8}
{"" => [] of Int16}
{"" => [] of Int32}
{"" => [] of Int64}
{"" => [] of UInt8}
{"" => [] of UInt16}
{"" => [] of UInt32}

lib LibT
  union U
    x : Int32
    y : UInt8
  end
end

def instance_var_names(type : T.class) forall T
  {{ T.instance_vars.map(&.stringify) }}
end

p instance_var_names(LibT::U)
p offsetof(LibT::U, @y)

This snippet leads to the following compiler error on x86_64-unknown-linux-gnu 52c750e (LLVM 8.0.0):

Invalid Int32: 93825018395136 (ArgumentError)
  from ../../../../crystal/src/string.cr:428:5 in 'to_i32'
  from ../../../../crystal/src/compiler/crystal/codegen/codegen.cr:448:23 in 'accept'
  from ../../../../crystal/src/compiler/crystal/codegen/codegen.cr:2193:7 in 'prepare_call_args_non_external'
  from ../../../../crystal/src/compiler/crystal/codegen/call.cr:57:7 in 'visit'
  from ../../../../crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from ../../../../crystal/src/compiler/crystal/codegen/codegen.cr:628:9 in 'accept'
  from ../../../../crystal/src/compiler/crystal/codegen/codegen.cr:2158:7 in 'codegen'
  from ../../../../crystal/src/compiler/crystal/compiler.cr:172:16 in 'compile'
  from ../../../../crystal/src/compiler/crystal/command.cr:210:5 in 'run_command'
  from ../../../../crystal/src/compiler/crystal/command.cr:62:5 in 'run'
  from ../../../../crystal/src/crystal/main.cr:105:5 in 'main'
  from src/env/__libc_start_main.c:94:2 in 'libc_start_main_stage2'
Error: you've found a bug in the Crystal compiler. Please open an issue, including source code that will allow us to reproduce the bug: https://github.com/crystal-lang/crystal/issues

On 0.35.1 the snippet compiles, but it also produces very incorrect results, as offsetof(LibT::U, @y) should be 0 because it is a C union.

The invalid integer is different every time the snippet is run; it seems to be some kind of corrupted pointer. What I believe is that the compiler tries to fix the value of offsetof before all types are known and finalized.

@jhass jhass added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler labels Sep 13, 2020
@BlobCodes
Copy link
Contributor

Using Crystal 1.0.0, this compiles and gives the following output (LLVM 10.0.0, OpenSUSE Tumbleweed, x86_64-unknown-linux-gnu):

["x", "y"]
0

Seems to have been fixed in the meantime, can this be closed?

@HertzDevil
Copy link
Contributor Author

This was never fixed, and it was by sheer coincidence that the last offsetof returns 0. A debug build of LLVM would trigger an assertion failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:codegen
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants