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

Memory address errors on linux x64 #7

Open
Gelbpunkt opened this issue Oct 21, 2020 · 18 comments
Open

Memory address errors on linux x64 #7

Gelbpunkt opened this issue Oct 21, 2020 · 18 comments
Labels
bug Something isn't working

Comments

@Gelbpunkt
Copy link

Hi, I am using the crystal-language extension as mentioned in the README and set the path to the prebuilt crystalline version that I downloaded from the releases.
Once I start VSCode and hover something, it crashes:

[Info  - 7:31:25 PM] Crystalline LSP server is ready.
Invalid memory access (signal 11) at address 0x0
[0xe755e6] ???
Invalid memory access (signal 11) at address 0x7f5bf8b6d380
[0xd8f6f2] ???
[0x1fab153] ???
[Info  - 7:31:26 PM] Connection to server got closed. Server will restart.
[Error - 7:31:26 PM] Request textDocument/hover failed.
Error: Connection got disposed.
	at Object.dispose (/home/jens/.vscode/extensions/crystal-lang-tools.crystal-lang-0.6.2/node_modules/vscode-jsonrpc/lib/main.js:904:25)
	at Object.dispose (/home/jens/.vscode/extensions/crystal-lang-tools.crystal-lang-0.6.2/node_modules/vscode-languageclient/lib/client.js:74:35)
	at LanguageClient.handleConnectionClosed (/home/jens/.vscode/extensions/crystal-lang-tools.crystal-lang-0.6.2/node_modules/vscode-languageclient/lib/client.js:2309:42)
	at LanguageClient.handleConnectionClosed (/home/jens/.vscode/extensions/crystal-lang-tools.crystal-lang-0.6.2/node_modules/vscode-languageclient/lib/main.js:155:15)
	at closeHandler (/home/jens/.vscode/extensions/crystal-lang-tools.crystal-lang-0.6.2/node_modules/vscode-languageclient/lib/client.js:2296:18)
	at CallbackList.invoke (/home/jens/.vscode/extensions/crystal-lang-tools.crystal-lang-0.6.2/node_modules/vscode-jsonrpc/lib/events.js:62:39)
	at Emitter.fire (/home/jens/.vscode/extensions/crystal-lang-tools.crystal-lang-0.6.2/node_modules/vscode-jsonrpc/lib/events.js:121:36)
	at closeHandler (/home/jens/.vscode/extensions/crystal-lang-tools.crystal-lang-0.6.2/node_modules/vscode-jsonrpc/lib/main.js:240:26)
	at CallbackList.invoke (/home/jens/.vscode/extensions/crystal-lang-tools.crystal-lang-0.6.2/node_modules/vscode-jsonrpc/lib/events.js:62:39)
	at Emitter.fire (/home/jens/.vscode/extensions/crystal-lang-tools.crystal-lang-0.6.2/node_modules/vscode-jsonrpc/lib/events.js:121:36)
	at StreamMessageReader.fireClose (/home/jens/.vscode/extensions/crystal-lang-tools.crystal-lang-0.6.2/node_modules/vscode-jsonrpc/lib/messageReader.js:111:27)
	at Socket.<anonymous> (/home/jens/.vscode/extensions/crystal-lang-tools.crystal-lang-0.6.2/node_modules/vscode-jsonrpc/lib/messageReader.js:151:46)
	at Socket.emit (events.js:228:7)
	at Pipe.<anonymous> (net.js:664:12)
[Info  - 7:31:26 PM] Crystalline LSP server is ready

I am using Fedora Linux 32 (x86_64) with Crystal 0.35.1 and VSCode 1.50.1, the Crystal Language extension version is 0.6.2

@elbywan
Copy link
Owner

elbywan commented Oct 21, 2020

Hi @Gelbpunkt, thanks for reporting the issue.

Once I start VSCode and hover something, it crashes:

Sorry to read this. I'll try my best to fix the problem, but unfortunately I don't have access to a machine running Linux, so I'll have to guess a bit.

Would you mind trying to run the following code with multithreading enabled and see if it runs fine?

require "compiler/crystal/**"

puts "Main thread: #{Thread.current}"

sources = [
  Crystal::Compiler::Source.new("hello.cr", %(puts "Hello world!")),
]

trigger = Channel(Nil).new
sync = Channel(String).new

Thread.new do
  puts "Thread > #{Thread.current}"
  kill_thread = Channel(Nil).new
  spawn same_thread: true do
    puts "Thread > Spawn on thread: #{Thread.current}"
    begin
      trigger.receive
      puts "Thread > Compiling…"
      compiler = Crystal::Compiler.new
      compiler.no_codegen = true
      puts "Thread > Before compilation."
      result = compiler.compile(sources, "")
      puts "Thread > Compilation done."
      sync.send "Ok: #{!result.nil?}"
    rescue e
      sync.send "Error: #{e}"
    end
    kill_thread.send nil
  end
  kill_thread.receive
end

puts "Main > Waiting…"

trigger.send nil
puts sync.receive

puts "Main > Bye!"

Basically saving the code in a file and running in a terminal:

crystal run file.cr -Dpreview_mt --debug

@elbywan elbywan added the bug Something isn't working label Oct 21, 2020
@Gelbpunkt
Copy link
Author

Gelbpunkt commented Oct 21, 2020

I am not sure what I should do with this. I started with errors like this:

--: : command not found
Showing last frame. Use --error-trace for full trace.

In crystal/crystal-0.35.1-1/share/crystal/src/llvm/lib_llvm.cr:13:17

 13 | VERSION = {{`#{LibLLVM::LLVM_CONFIG} --version`.chomp.stringify}}
                  ^

which fixed itself after installing llvm-devel (version 10).
It then failed at compiling with this error:

/usr/bin/ld: /home/jens/crystal/crystal-0.35.1-1/share/crystal/src/llvm/ext/llvm_ext.o: in function `llvm::MetadataTracking::track(llvm::Metadata*&)':
llvm_ext.cc:(.text._ZN4llvm16MetadataTracking5trackERPNS_8MetadataE[_ZN4llvm16MetadataTracking5trackERPNS_8MetadataE]+0x42): undefined reference to `llvm::MetadataTracking::track(void*, llvm::Metadata&, llvm::PointerUnion<llvm::MetadataAsValue*, llvm::Metadata*>)'
collect2: error: ld returned 1 exit status
Error: execution of command failed with code: 1: `cc "${@}" -o /home/jens/.cache/crystal/crystal-run-test.tmp  -rdynamic -L/home/jens/crystal/crystal-0.35.1-1/bin/../lib/crystal/lib -lz `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libssl || printf %s '-lssl -lcrypto'` `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libcrypto || printf %s '-lcrypto'` /home/jens/crystal/crystal-0.35.1-1/share/crystal/src/llvm/ext/llvm_ext.o `"/usr/bin/llvm-config" --libs --system-libs --ldflags 2> /dev/null` -lstdc++ -lpcre -lm -lgc -lpthread /home/jens/crystal/crystal-0.35.1-1/share/crystal/src/ext/libcrystal.a -levent_pthreads -levent  -levent  -lrt -ldl`

I am guessing this is a LLVM version mismatch so I will compile crystal from source

@elbywan
Copy link
Owner

elbywan commented Oct 21, 2020

@Gelbpunkt Yes I'm sorry the snippet I posted forces you to compile the crystal compiler from source, which can be complicated.

I think we can attempt to reproduce the issue with less accurate but simpler code first:

crystal run file.cr -Dpreview_mt --debug
# file.cr

puts "Main thread: #{Thread.current}"

trigger = Channel(Nil).new
sync = Channel(String).new

Thread.new do
  puts "Thread > #{Thread.current}"
  kill_thread = Channel(Nil).new
  spawn same_thread: true do
    puts "Thread > Spawn #{Fiber.current} on thread: #{Thread.current}"
    begin
      trigger.receive
      GC.collect
      sync.send "Ok"
    rescue e
      sync.send "Error: #{e}"
    end
    kill_thread.send nil
  end
  kill_thread.receive
end

puts "Main > Waiting…"

trigger.send nil
puts sync.receive

puts "Main > Bye!"

@Gelbpunkt
Copy link
Author

While that did not run on a self-compiled crystal 1.0.0-dev:

In test.cr:1:1

 1 | puts "Main thread: #{Thread.current}"
     ^
Error: can't find file 'prelude'

it ran fine on the 0.35.1 release tarball:

Main thread: #<Thread:0x7fb540671f50>
Main > WaitingThread > #<Thread:0x7fb540671af0>
Thread > Spawn #<Fiber:0x7fb5406729a0> on thread: #<Thread:0x7fb540671af0>
Ok
Main > Bye!

@elbywan
Copy link
Owner

elbywan commented Oct 21, 2020

Hmm then I was wrong about that piece of code I guess. I was thinking that spawning a thread manually using Thread.new could behave differently in different OSes but it looks like it's working fine.

If it's not too much to ask, could you try building crystalline from source and try with the produced binary instead of using the prebuilt one?

@Gelbpunkt
Copy link
Author

It took a long while due to issues with LLVM again, ended up building crystal from source and then compiling crystalline without shards as that forkbombed itself.

The resulting binary works fine (yay!), so I assume the issue is with the build setup you are using.

@elbywan
Copy link
Owner

elbywan commented Oct 23, 2020

It took a long while due to issues with LLVM again, ended up building crystal from source and then compiling crystalline without shards as that forkbombed itself.

The resulting binary works fine (yay!), so I assume the issue is with the build setup you are using.

Sorry you had to go through this, it's a good thing that it worked in the end 👍.

The build system uses an alpine linux docker image to build a static and supposedly portable binary, I will try my best to check why it is crashing on Fedora but without access to a linux machine it's going to be a bit complicated.

@tducasse
Copy link

Just FYI, I just got the exact same issue on my windows machine, using WSL2

@elbywan
Copy link
Owner

elbywan commented Oct 28, 2020

Hi @tducasse, thanks for reporting!

Out of curiosity, are you able to build it from sources and run it as well?

@tducasse
Copy link

Yeah I'll try that today or tomorrow, maybe even try to find a way to fix the dockerfile if I find the time :)

@tducasse
Copy link

tducasse commented Oct 28, 2020

okay so I've tried multiple things, but I basically did not succeed in compiling from sources, but I think WSL might be the issue here, since WSL has a tendency to look like it's working but actually it's not 😬

What I've tried though (in order to fix the Dockerfile that builds the static binary), is building from the official docker image (crystallang/crystal:0.35.1-alpine-build - dockerfile available here), because it looks like it's got all the right deps, using something like:

docker run --rm -it -v $PWD:/app -w /app crystallang/crystal:0.35.1-alpine-build shards build crystalline --release --static --progress --no-debug -Dpreview_mt

This yelled at me telling me that it could not find llvm_ext.o, so I created a Dockerfile to compile it myself (I've docker exec -ti into the container to check that g++ is installed and that the files exist):

FROM crystallang/crystal:0.35.1-alpine-build

RUN g++ -c usr/share/crystal/src/llvm/ext/llvm_ext.cc -I/usr/lib/llvm9/include -o /usr/share/crystal/src/llvm/ext/llvm_ext.o

This goes further, but still fails:

Dependencies are satisfied
Building: crystalline
Error target crystalline failed to compile:
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: _main.o: in function `~procProc(Nil)@/usr/share/crystal/src/gc/boehm.cr:302':
main_module:(.text+0x6e5d): undefined reference to `GC_set_stackbottom'
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: _main.o: in function `*Fiber::new<Pointer(Void), Thread>:Fiber':
main_module:(.text+0x770f): undefined reference to `GC_get_my_stackbottom'
collect2: error: ld returned 1 exit status
Error: execution of command failed with code: 1: `cc "${@}" -o /app/bin/crystalline  -rdynamic -static -L/usr/bin/../lib/crystal/lib -lyaml  -lz `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libssl || printf %s '-lssl -lcrypto'` `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libcrypto || printf %s '-lcrypto'` /usr/share/crystal/src/llvm/ext/llvm_ext.o `"/usr/bin/llvm-config" --libs --system-libs --ldflags --link-static 2> /dev/null` -lstdc++ -lpcre -lm -lgc -lpthread /usr/share/crystal/src/ext/libcrystal.a -levent_pthreads -levent -lrt`

So now I'm not entirely sure what the problem is, but I think it might be related to this issue about multi threads. Happy to help and delve further into the issue, but keep in mind I'm way out of my league here, as I've not even written my first line of Crystal code yet 🤣

@tducasse
Copy link

tducasse commented Oct 29, 2020

As far as I can tell, this is a known issue with the Dpreview_mt flag, because the crystal devs have come up with a patch that they manually apply when they compile bwdgc, but they don't do it on their alpine version.
There's currently an approved PR solving exactly this issue, and it would most probably allow us to use the previous Dockerfile I linked (crystallang/crystal:0.35.1-alpine-build) and do:

FROM crystallang/crystal:0.35.1-alpine-build

# not sure whether it's llvm10 or 9 here, depends on their Dockerfile
RUN g++ -c usr/share/crystal/src/llvm/ext/llvm_ext.cc -I/usr/lib/llvm10/include -o /usr/share/crystal/src/llvm/ext/llvm_ext.o

Just FYI, I was able to manually compile a static binary using this Dockerfile (inspired by the PR I linked to):

FROM alpine:3.12 as runtime

RUN \
  apk add --update --no-cache --force-overwrite \
    # core dependencies
    gcc gmp-dev libevent-static musl-dev pcre-dev \
    # stdlib dependencies
    libxml2-dev openssl-dev openssl-libs-static tzdata yaml-dev yaml-static zlib-static \
    # dev tools
    make git patch autoconf automake libtool

ARG patch
COPY ${patch} /tmp/feature-thread-stackbottom-upstream.patch

ARG gc_version
ARG libatomic_ops_version
RUN git clone https://github.com/ivmai/bdwgc \
 && cd bdwgc \
 && git checkout ${gc_version} \
 && git clone https://github.com/ivmai/libatomic_ops \
 && (cd libatomic_ops && git checkout ${libatomic_ops_version}) \
 \
 && patch -p1 < /tmp/feature-thread-stackbottom-upstream.patch \
 \
 && ./autogen.sh \
 && ./configure --disable-debug --disable-shared --enable-large-config \
 && make -j$(nproc) CFLAGS=-DNO_GETCONTEXT \
 && make install

# Remove build tools from image now that libgc is built
RUN apk del -r --purge autoconf automake libtool

ARG crystal_targz
COPY ${crystal_targz} /tmp/crystal.tar.gz

RUN \
  tar -xz -C /usr --strip-component=1  -f /tmp/crystal.tar.gz \
    --exclude */lib/crystal/lib \
    --exclude */share/crystal/src/llvm/ext/llvm_ext.o \
    --exclude */share/crystal/src/ext/libcrystal.a && \
  rm /tmp/crystal.tar.gz

# Build libcrystal
RUN \
  cd /usr/share/crystal && \
  cc -fPIC -c -o src/ext/sigfault.o src/ext/sigfault.c && \
  ar -rcs src/ext/libcrystal.a src/ext/sigfault.o

CMD ["/bin/sh"]

FROM runtime as build

RUN \
  apk add --update --no-cache --force-overwrite \
    llvm10-dev llvm10-static g++

RUN g++ -c usr/share/crystal/src/llvm/ext/llvm_ext.cc -I/usr/lib/llvm10/include -o /usr/share/crystal/src/llvm/ext/llvm_ext.o

CMD ["/bin/sh"]

And running:

# build the image, in a folder containing the Dockerfile
# I downloaded the crystal tar from the official release channel
# and the patch from https://github.com/crystal-lang/distribution-scripts/blob/master/linux/files/feature-thread-stackbottom-upstream.patch
docker build \
--build-arg gc_version=v8.0.4 \
--build-arg libatomic_ops_version=v7.6.10 \
--build-arg patch=./feature-thread-stackbottom-upstream.patch \
--build-arg crystal_targz=crystal-0.35.1-1-linux-x86_64.tar.gz \
-t crystalbuilder .

# build crystalline, in the crystalline folder
docker run --rm -it -v $PWD:/app -w /app crystalbuilder crystal build src/crystalline.cr -o bin/crystalline --static --release --no-debug --progress -Dpreview_mt

@elbywan
Copy link
Owner

elbywan commented Oct 31, 2020

Happy to help and delve further into the issue, but keep in mind I'm way out of my league here, as I've not even written my first line of Crystal code yet 🤣

@tducasse Thanks a bunch for investigating, you've done an amazing work!

I will rewrite the alpine Dockerfile to include the GC patch until the original pull request gets merged (and I also realized that I forgot the preview_mt flag 🤦). It should solve the issue hopefully.

@elbywan elbywan mentioned this issue Oct 31, 2020
@tducasse
Copy link

No worries, happy to help 🙂

@elbywan elbywan closed this as completed Apr 12, 2021
@astrolemonade
Copy link

astrolemonade commented Mar 19, 2023

This still happens on Solus Linux

$ crystal --version
Crystal 1.8.0-dev [fc2250af9] (2023-03-17)

$ shards --version
Shards 0.17.2 [a894d1c] (2022-12-28)

$ llvm-config --version
14.0.6

$ gcc --version
gcc (Solus) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ uname -a
Linux astrolemonade 6.1.5-229.current #1 SMP PREEMPT_DYNAMIC Thu Jan 12 20:29:03 UTC 2023 x86_64 GNU/Linux

I receive this:

Invalid memory access (signal 11) at address 0x7f41e56e4000
[Info  - 1:11:34 AM] LSP server is ready.

@elbywan

Code:
the default shard init app code modified to

module Ll
  VERSION = "0.1.0"
  name = "something"
  nickname = "astrolemonade"
  handle = name + "@" + nickname
  puts handle
  print("ha")
end

With the Github release it works.

@elbywan
Copy link
Owner

elbywan commented Mar 20, 2023

Hey @astrolemonade,

This still happens on Solus Linux

I am sorry to read this. I do not have access to a Linux environment so I am afraid I cannot do anything to fix this yet (and the Invalid memory access error is really too generic to infer anything from it).

With the Github release it works.

I guess using docker or similar to build crystalline should also work on any linux distro 🤞.

@uncomfyhalomacro
Copy link

This still happens on Solus Linux

$ crystal --version
Crystal 1.8.0-dev [fc2250af9] (2023-03-17)

$ shards --version
Shards 0.17.2 [a894d1c] (2022-12-28)

$ llvm-config --version
14.0.6

$ gcc --version
gcc (Solus) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ uname -a
Linux astrolemonade 6.1.5-229.current #1 SMP PREEMPT_DYNAMIC Thu Jan 12 20:29:03 UTC 2023 x86_64 GNU/Linux

I receive this:

Invalid memory access (signal 11) at address 0x7f41e56e4000
[Info  - 1:11:34 AM] LSP server is ready.

@elbywan

Code: the default shard init app code modified to

module Ll
  VERSION = "0.1.0"
  name = "something"
  nickname = "astrolemonade"
  handle = name + "@" + nickname
  puts handle
  print("ha")
end

With the Github release it works.

I could confirm with openSUSE Tumbleweed as well. I am building it inside OBS and I was looking at this issue since I thought I was just paranoid.

Well we can still keep this issue open until there is a solution right?

@elbywan elbywan reopened this Mar 28, 2023
@uncomfyhalomacro
Copy link

I tested a non release build of crystalline 0.10.0 and got this errors.

Invalid memory access (signal 11) at address 0x30
Invalid memory access (signal 11) at address 0x0
[0x95eec6] [0x95eec6] *Exception::CallStack::print_backtrace:Nil +118 in /home/uncomfy/development/obs/home:uncomfyhalomacro/crystalline/crystalline-0.10.0/crystalline
[0x93a436] ~procProc(Int32, Pointer(LibC::SiginfoT), Pointer(Void), Nil) +310 in /home/uncomfy/development/obs/home:uncomfyhalomacro/crystalline/crystalline-0.10.0/crystalline
[0x7fdef381c330] *Exception::CallStack::print_backtrace:Nil +118 in /home/uncomfy/development/obs/home:uncomfyhalomacro/crystalline/crystalline-0.10.0/crystalline
[0x93a436] ?? +140595544834864 in /lib64/libc.so.6
[0x0] ~procProc(Int32, Pointer(LibC::SiginfoT), Pointer(Void), Nil) +310 in /home/uncomfy/development/obs/home:uncomfyhalomacro/crystalline/crystalline-0.10.0/crystalline
[0x7fdef381c330] ?? +140595544834864 in /lib64/libc.so.6
[0xa710b0] *Crystal::Scheduler#reschedule:Nil +16 in /home/uncomfy/development/obs/home:uncomfyhalomacro/crystalline/crystalline-0.10.0/crystalline
[0x7fdef140bc00] ???

Process crystalline stderr finished
Invalid memory access (signal 11) at address 0x30
Invalid memory access (signal 11) at address 0x0
[0x95eec6] [0x95eec6] *Exception::CallStack::print_backtrace:Nil +118 in /home/uncomfy/development/obs/home:uncomfyhalomacro/crystalline/crystalline-0.10.0/crystalline
[0x93a436] ~procProc(Int32, Pointer(LibC::SiginfoT), Pointer(Void), Nil) +310 in /home/uncomfy/development/obs/home:uncomfyhalomacro/crystalline/crystalline-0.10.0/crystalline
[0x7f519d8fe330] ?? +139988512531248 in /lib64/libc.so.6
[0xa710b0] *Exception::CallStack::print_backtrace:Nil +118 in /home/uncomfy/development/obs/home:uncomfyhalomacro/crystalline/crystalline-0.10.0/crystalline
[0x93a436] *Crystal::Scheduler#reschedule:Nil +16 in /home/uncomfy/development/obs/home:uncomfyhalomacro/crystalline/crystalline-0.10.0/crystalline
[0x7f519b5a1640] ~procProc(Int32, Pointer(LibC::SiginfoT), Pointer(Void), Nil) +310 in /home/uncomfy/development/obs/home:uncomfyhalomacro/crystalline/crystalline-0.10.0/crystalline
[0x7f519d8fe330] ?? +139988512531248 in /lib64/libc.so.6
[0x0] ???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants