Reonarudo / pcb2photon

Image converter to .photon files used by the Anycubic's 3D UV Photon enabling it to produce UV PCB masks.
39 stars 4 forks source link

Swift fatal error on Ubuntu 16.04 (also command line arguments parsing issues) #4

Open tlongeri opened 6 years ago

tlongeri commented 6 years ago

Hi, I built pcb2photon on Ubuntu 16.04 as follows:

$ git clone https://github.com/Reonarudo/pcb2photon.git pcb2photon
Cloning into 'pcb2photon'...
remote: Counting objects: 433, done.
remote: Compressing objects: 100% (54/54), done.
remote: Total 433 (delta 50), reused 73 (delta 28), pack-reused 331
Receiving objects: 100% (433/433), 15.72 MiB | 2.15 MiB/s, done.
Resolving deltas: 100% (193/193), done.
Checking connectivity... done.
$ cd pcb2photon
$ swift build
Fetching https://github.com/SwiftGL/Image.git
Cloning https://github.com/SwiftGL/Image.git
Resolving https://github.com/SwiftGL/Image.git at 2.2.0
Compile Swift Module 'SGLImage' (6 sources)
/home/tomas/installations/test/pcb2photon/.build/checkouts/Image.git-8898945789819335455/Sources/SGLImage/Image.swift:119:29: warning: 'deallocate(capacity:)' is deprecated: Swift currently only supports freeing entire heap blocks, use deallocate() instead
        buffer.baseAddress!.deallocate(capacity: buffer.count)
                            ^
Compile Swift Module 'pcb2photon' (6 sources)
Linking ./.build/x86_64-unknown-linux/debug/pcb2photon
$ cp test.png .build/debug
$ cd .build/debug
$ ./pcb2photon test.png
$

When running pcb2photon at first I got no responsiveness at all (terminated instantly with no shell output and no files being generated), but soon I realized some error messages do work

$ ./pcb2photon test.png -t
Error: 01 Invalid value for t.
$

I have zero experience with swift, but after looking at the code I changed the following (lines 147-152 of Converter.swift):

                    var newFileNames : [String] = []
                    i+=1
                    while i < min(filesToConvert.count, Int(argCount)){
                        newFileNames.append(CommandLine.arguments[i])
                        i+=1
                    }

into

                    self.conversionOptions.output = []
                    i+=1
                    let i0 = i
                    while i < min(i0 + filesToConvert.count, Int(argCount)){
                        self.conversionOptions.output.append(CommandLine.arguments[i])
                        i+=1
                    }

Now, this didn't fix the non-responsiveness if I didn't specify an output file, or if I didn't specify anything at all, but at least I could get past the parsing of the arguments now.

After building with that change, I tried again:

$ ./pcb2photon test.png -o test.out
Fatal error: 'try!' expression unexpectedly raised an error: The operation could not be completed: file /home/buildnode/jenkins/workspace/oss-swift-4.1-package-linux-ubuntu-16_04/swift/stdlib/public/core/ErrorType.swift, line 184
Current stack trace:
0    libswiftCore.so                    0x00007f2bada2c680 _swift_stdlib_reportFatalErrorInFile + 221
1    libswiftCore.so                    0x00007f2bad79b38c <unavailable> + 1368972
2    libswiftCore.so                    0x00007f2bad9d52f2 <unavailable> + 3703538
3    libswiftCore.so                    0x00007f2bad9d6759 <unavailable> + 3708761
4    libswiftCore.so                    0x00007f2bad79aa86 <unavailable> + 1366662
5    libswiftCore.so                    0x00007f2bad9d50bb <unavailable> + 3702971
6    libswiftCore.so                    0x00007f2bad79aa86 <unavailable> + 1366662
7    libswiftCore.so                    0x00007f2bad9080b9 <unavailable> + 2863289
8    libswiftCore.so                    0x00007f2bad7d8840 swift_unexpectedError + 318
Illegal instruction (core dumped)
$

This time it took a while before crashing, so it looked like it was actually doing something. I omitted logging messages I added into the code, but I'm quite sure that the arguments were parsed correctly and it crashed deeper in the program.

That's as far as I got. I'm not sure what is causing this error, so I would really appreciate it if you help out. This seems like a great tool that could easily save someone like me several hours of work (who just discovered the most available means of getting some quick PCBs has turned out to be printing them in our DLP printer).

Reonarudo commented 6 years ago

Hello, First of all, welcome to swift programming! 🎉 Indeed there is a bug in there! But your attempt at fixing, while having the right idea, would still not work in some cases. I'm pushing a "swifty" way to do it without using a raw loop also in an attempt to show how elegant swift can be 😉 (it could have been done using the while and some auxiliar values like you were doing) This also allowed me to find another bug and I will create a proper test suite to integrate with travis to better test the tool.

Thank you

Reonarudo commented 6 years ago

@tlongeri can you try the branch issue-4 and tell me if it works for you now?

tlongeri commented 6 years ago

Thank you, that fixes the non-responsiveness on output files! However, the second crash is still happening for me in the exact same way. I downloaded swift 4.0 (I was using 4.1.2) to see if it made a difference, but I got a similar crash:

$ ./pcb2photon test.png
fatal error: 'try!' expression unexpectedly raised an error: The operation could not be completed: file /home/buildnode/jenkins/workspace/oss-swift-4.0-package-linux-ubuntu-16_04/swift/stdlib/public/core/ErrorType.swift, line 181
Current stack trace:
0    libswiftCore.so                    0x00007f5bd4220a30 _swift_stdlib_reportFatalErrorInFile + 221
1    libswiftCore.so                    0x00007f5bd3f28090 closure #1 in closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) + 284
2    libswiftCore.so                    0x00007f5bd41c6eb1 <unavailable> + 4148913
3    libswiftCore.so                    0x00007f5bd41dd569 <unavailable> + 4240745
4    libswiftCore.so                    0x00007f5bd3f2796b <unavailable> + 1399147
5    libswiftCore.so                    0x00007f5bd413ff39 <unavailable> + 3596089
6    libswiftCore.so                    0x00007f5bd417dd1f <unavailable> + 3849503
7    libswiftCore.so                    0x00007f5bd3f2796b <unavailable> + 1399147
8    libswiftCore.so                    0x00007f5bd40c0020 specialized _assertionFailure(_:_:file:line:flags:) + 144
9    libswiftCore.so                    0x00007f5bd3f754b0 swift_unexpectedError + 371
10   pcb2photon                         0x000000000041839c <unavailable> + 99228
11   pcb2photon                         0x0000000000418ddc <unavailable> + 101852
12   pcb2photon                         0x0000000000415dd4 <unavailable> + 89556
13   pcb2photon                         0x000000000040b319 <unavailable> + 45849
14   pcb2photon                         0x000000000040aee6 <unavailable> + 44774
15   pcb2photon                         0x000000000040afac <unavailable> + 44972
16   pcb2photon                         0x000000000040b012 <unavailable> + 45074
17   pcb2photon                         0x000000000040b0f3 <unavailable> + 45299
18   libswiftCore.so                    0x00007f5bd40df659 <unavailable> + 3200601
19   libswiftCore.so                    0x00007f5bd3eee860 Sequence.forEach(_:) + 15
20   pcb2photon                         0x0000000000409cf8 <unavailable> + 40184
21   pcb2photon                         0x0000000000417e14 <unavailable> + 97812
22   libc.so.6                          0x00007f5bd205f740 __libc_start_main + 240
23   pcb2photon                         0x0000000000407c19 <unavailable> + 31769
Illegal instruction (core dumped)

I traced it to line 51 of PhotonFileHandler.swift with the help of GDB and the dumped core file though (had to run ulimit -c unlimited for it to be correctly dumped on the local directory)... Not that I've done something like this before

$ gdb ./pcb2photon core
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./pcb2photon...done.
[New LWP 9891]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./pcb2photon test.png'.
Program terminated with signal SIGILL, Illegal instruction.
#0  0x00007f5bd40c00b0 in _T0s17_assertionFailures5NeverOs12StaticStringV_SSAE4fileSu4lines6UInt32V5flagstFTfq4nxnnn_n ()
   from /home/tomas/installations/swift-4.0-RELEASE-ubuntu16.04/usr/lib/swift/linux/libswiftCore.so
(gdb) backtrace
#0  0x00007f5bd40c00b0 in _T0s17_assertionFailures5NeverOs12StaticStringV_SSAE4fileSu4lines6UInt32V5flagstFTfq4nxnnn_n ()
   from /home/tomas/installations/swift-4.0-RELEASE-ubuntu16.04/usr/lib/swift/linux/libswiftCore.so
#1  0x00007f5bd3f75623 in swift_unexpectedError ()
   from /home/tomas/installations/swift-4.0-RELEASE-ubuntu16.04/usr/lib/swift/linux/libswiftCore.so
#2  0x000000000041839c in _T010pcb2photon10PhotonFileC08templateB13File1440x256033_342D0A6CB419A824D654AD685C748CFFLL10Foundation4DataVfg (self=...) at /home/tomas/installations/test/pcb2photon/Sources/pcb2photon/PhotonFileHandler.swift:51
#3  0x0000000000418ddc in _T010pcb2photon10PhotonFileC4data10Foundation4DataVyF (self=...)
    at /home/tomas/installations/test/pcb2photon/Sources/pcb2photon/PhotonFileHandler.swift:115
#4  0x0000000000415dd4 in _T010pcb2photon17SGLImageConverterC7convertAA8PCBImageVyF (self=...)
    at /home/tomas/installations/test/pcb2photon/Sources/pcb2photon/ImageFileDecoders.swift:94
#5  0x000000000040b319 in _T010pcb2photon9ConverterC7convert33_5272CA17FCBFDD0928345297909191B7LLySS_SSSg4intotKF (fileName=..., 
    newFile=..., self=...) at /home/tomas/installations/test/pcb2photon/Sources/pcb2photon/Converter.swift:66
#6  0x000000000040aee6 in _T010pcb2photon9ConverterC10staticModeyyFySS_SStKcfU0_ ()
    at /home/tomas/installations/test/pcb2photon/Sources/pcb2photon/Converter.swift:35
#7  0x000000000040afac in _T010pcb2photon9ConverterC10staticModeyyFySS_SStKcfU0_TA ()
#8  0x000000000040b012 in _T0S2Ss5Error_pIxxxzo_SS_SStsAA_pIxizo_TR ()
#9  0x000000000040b0f3 in _T0S2Ss5Error_pIxxxzo_SS_SStsAA_pIxizo_TRTA ()
#10 0x00007f5bd40df659 in _T0s8SequencePsE7forEachyy7ElementQzKcKFTfq4gn_n ()
   from /home/tomas/installations/swift-4.0-RELEASE-ubuntu16.04/usr/lib/swift/linux/libswiftCore.so
#11 0x00007f5bd3eee86f in _T0s8SequencePsE7forEachyy7ElementQzKcKF ()
   from /home/tomas/installations/swift-4.0-RELEASE-ubuntu16.04/usr/lib/swift/linux/libswiftCore.so
#12 0x0000000000409cf8 in _T010pcb2photon9ConverterC10staticModeyyF (self=...)
    at /home/tomas/installations/test/pcb2photon/Sources/pcb2photon/Converter.swift:35
#13 0x0000000000417e14 in main () at /home/tomas/installations/test/pcb2photon/Sources/pcb2photon/main.swift:17

This is what line 51 looks like:

        return try! Data(contentsOf: URL(fileURLWithPath: Bundle.main.path(forResource: "template", ofType: "photon") ?? ""))

I also thought maybe I was missing some swift dependency or something, since I had installed swift "manually" instead of using your install script for swiftenv, so I tried that then.

$ bash install-swift.sh
echo "Installing Swift for Linux"
Installing Swift for Linux
echo "-Ubuntu Trusty 14.04"
-Ubuntu Trusty 14.04
echo "      Updating, Fetching & Installing Ubuntu Trusty 14.04 dependencies..."
      Updating, Fetching & Installing Ubuntu Trusty 14.04 dependencies...
sudo apt-get -y update
[sudo] password for tomas: 
Hit:1 https://dl.winehq.org/wine-builds/ubuntu xenial InRelease
Get:2 https://raw.githubusercontent.com/runtimeco/debian-mynewt/master latest InRelease [1.762 B]
Hit:3 https://deb.nodesource.com/node_8.x xenial InRelease
Ign:4 http://dl.google.com/linux/chrome/deb stable InRelease
Ign:5 http://dl.google.com/linux/talkplugin/deb stable InRelease               
Hit:6 http://dl.google.com/linux/chrome/deb stable Release                     
Hit:7 http://dl.google.com/linux/talkplugin/deb stable Release                 
Get:10 http://security.ubuntu.com/ubuntu xenial-security InRelease [107 kB]    
Hit:11 http://cl.archive.ubuntu.com/ubuntu xenial InRelease                    
Get:12 http://cl.archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]   
Get:13 http://cl.archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB] 
Get:14 http://cl.archive.ubuntu.com/ubuntu xenial-backports/main Sources [4.476 B]
Get:15 http://cl.archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [6.732 B]
Get:16 http://cl.archive.ubuntu.com/ubuntu xenial-backports/main i386 Packages [6.720 B]
Get:17 http://cl.archive.ubuntu.com/ubuntu xenial-backports/main Translation-en [4.180 B]
Hit:18 https://packagecloud.io/slacktechnologies/slack/debian jessie InRelease 
Hit:19 http://ppa.launchpad.net/adriansmith/upm/ubuntu xenial InRelease        
Hit:20 http://ppa.launchpad.net/nathan-renniewaldock/flux/ubuntu xenial InRelease
Hit:21 http://repo.steampowered.com/steam precise InRelease                    
Hit:22 https://download.sublimetext.com apt/stable/ InRelease                  
Hit:23 http://ppa.launchpad.net/octave/stable/ubuntu xenial InRelease          
Hit:24 http://ppa.launchpad.net/qbittorrent-team/qbittorrent-stable/ubuntu xenial InRelease
Fetched 347 kB in 11s (31,3 kB/s)                                              
Reading package lists... Done
N: Skipping acquire of configured file 'main/binary-i386/Packages' as repository 'https://raw.githubusercontent.com/runtimeco/debian-mynewt/master latest InRelease' doesn't support architecture 'i386'
sudo apt-get -y install clang libicu-dev git
Reading package lists... Done
Building dependency tree       
Reading state information... Done
git is already the newest version (1:2.7.4-0ubuntu1.4).
libicu-dev is already the newest version (55.1-7ubuntu0.4).
clang is already the newest version (1:3.8-33ubuntu3.1).
0 upgraded, 0 newly installed, 0 to remove and 62 not upgraded.
sudo apt-get -y install clang-3.6
Reading package lists... Done
Building dependency tree       
Reading state information... Done
clang-3.6 is already the newest version (1:3.6.2-3ubuntu2).
0 upgraded, 0 newly installed, 0 to remove and 62 not upgraded.
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.6 100
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100
echo "-Installing Swift using SwiftEnv"
-Installing Swift using SwiftEnv
echo "      Downloading SwiftEnv"
      Downloading SwiftEnv

git clone https://github.com/kylef/swiftenv ~/.swiftenv
Cloning into '/home/tomas/.swiftenv'...
remote: Counting objects: 1126, done.
remote: Total 1126 (delta 0), reused 0 (delta 0), pack-reused 1125
Receiving objects: 100% (1126/1126), 508.08 KiB | 843.00 KiB/s, done.
Resolving deltas: 100% (685/685), done.
Checking connectivity... done.
export SWIFTENV_ROOT="$HOME/.swiftenv" >> ~/.bash_profile
export PATH="$SWIFTENV_ROOT/bin:$SWIFTENV_ROOT/shims:$PATH"
if [ -f ".swift-version" ] || [ -n "$SWIFT_VER" ]; then
  swiftenv install -s
else
  swiftenv rehash
fi
echo "Finished installing Swift!"
Finished installing Swift!

Then I added the swiftenv directory to my PATH, then I ran swiftenv install 4.0 and then I tried ./pcb2photon test.png again only to get the exact same error message.

I don't know if it's an issue with swift or the code to be honest. I'm hoping the error makes sense to you!

Reonarudo commented 6 years ago

Ok I have t further investigate it. It should work fine both with swift 4.0 and 4.1.2. Also swiftenv is not mine ;) When you used swiftenv you chose ubuntu 14.04, I think you are using ubuntu 16.04, no? Can you check that you have the template.photon file?

tlongeri commented 6 years ago

I know swiftenv isn't yours, I just wanted to follow the exact steps you used in case I missed something!

I am indeed using Ubuntu 16.04 (like the title says). When I ran swiftenv install 4.0 it downloaded the 16.04 version of swift. Before I tried using swiftenv, I also directly downloaded swift for Ubuntu 16.04 from the swift website.

I do have the aux/template.photon file, but I checked and noticed Bundle.main.path(forResource: "template", ofType: "photon") returns nil, which I'm guessing isn't supposed to happen.

Reonarudo commented 6 years ago

Indeed it is not supposed to return nil, I have a solution but I will only be able to commit it by tomorrow.

tlongeri commented 6 years ago

It turns out that just copying the template.photon file to the .build/debug directory made it work! :tada: Apparently it was looking for it in the wrong place.

$ ./pcb2photon test.png
Created file: <CFURL 0x1adb740 [0x7fce70491a88]>{string = file:///home/tomas/installations/test/pcb2photon/.build/x86_64-unknown-linux/debug/test.png.photon, encoding = 134217984, base = (null)}
$

This is what I'll be doing for now!

Edit: may I suggest adding a check on the input image dimensions? I was momentarily scared that when I placed my 2560x1440 bmp file (instead of 1440x2560) the output file's figure was garbage. Again, thanks for this useful tool!

Reonarudo commented 6 years ago

@tlongeri cool. Still, I am removing that dependency so we can avoid this can of issues in the future and have better code. Yes, I will make so it automatically turns the image if it has exactly 2560x1440px. I will also add other features like automatic scaling etc as described in the README.