Bug 558681 - org.eclipse.swt.internal.Library does not always throw lib load failure error.
Summary: org.eclipse.swt.internal.Library does not always throw lib load failure error.
Status: VERIFIED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.15   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 4.15 M1   Edit
Assignee: Alexandre Honorat CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-12-29 18:36 EST by Alexandre Honorat CLA
Modified: 2020-01-08 03:30 EST (History)
6 users (show)

See Also:


Attachments
Corrupted libswt-awt (5.95 KB, application/octet-stream)
2020-01-06 13:32 EST, Alexandre Honorat CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Alexandre Honorat CLA 2019-12-29 18:36:49 EST
In the "public static void loadLibrary (String name, boolean mapName)" method
of the class "org.eclipse.swt.internal.Library", some errors may be catched and not thrown again.

Indeed the last two calls to the method "load" are immediately followed by a return statement without checking the result. The return statements should be conditional to the success of the "load" calls, as done earlier in the method loadLibrary. Otherwise the load may fail, catching an error which will not be propagated.

The faulty code is located on line 333-340 of the class, from the code available at : https://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/bundles/org.eclipse.swt/Eclipse%20SWT%20PI/common/org/eclipse/swt/internal/Library.java 
The fix is really easy: enclose lines 334 and 338 as condition of an if statement, where the block is only a return (as done on lines 327 and 328 for example).

For sure, if the native library is not loaded, an error will still occur sooner or later, however the original reason (and message) will be lost and debug will be more complicated (it happen to me with libswt-awt, fir which I will open another bug report).

To reproduce the bug, corrupt any shared object (.so/.dll) needed by swt (as libswt-awt for SWT_AWT class): for example transform the .so as a symbolic link towards a random text file.
Comment 1 Lakshmi P Shanmugam CLA 2020-01-02 00:41:30 EST
Thanks for the bug report. 
Can you please provide a gerrit patch with the changes - https://www.eclipse.org/swt/fixbugs.php
Comment 2 Alexandre Honorat CLA 2020-01-04 13:54:55 EST
Hello,

I pushed the following change:
https://git.eclipse.org/r/#/c/155215/

It's the first time I'm contributing directly to an Eclipse project, so please tell me if something's not done properly.

By the way, I tried to run tests from package org.eclipse.swt.tests.junit but I got a few errors and then it crashes on 3751th test even on master branch (so without my changes). I guess I missed some configurations steps.
Comment 3 Paul Pazderski CLA 2020-01-04 14:03:53 EST
(In reply to Alexandre Honorat from comment #2)
> It's the first time I'm contributing directly to an Eclipse project, so
> please tell me if something's not done properly.

Almost fine. Please include the bug number at begin of the commit message. Not only useful to later find the bug for a change but also a bot will automatically link the change here.

(In reply to Alexandre Honorat from comment #2)
> By the way, I tried to run tests from package org.eclipse.swt.tests.junit
> but I got a few errors and then it crashes on 3751th test even on master
> branch (so without my changes). I guess I missed some configurations steps.

Had you tried to launch the AllNonBrowserTests suite from within eclipse? I don't remember there is something special to do. Anyway Gerrit will run the tests on your change and since it is already finished I can say Gerrit likes your change. ;)
Comment 4 Andrey Loskutov CLA 2020-01-04 14:10:19 EST
(In reply to Paul Pazderski from comment #3)
> Anyway Gerrit will run the
> tests on your change and since it is already finished I can say Gerrit likes
> your change. ;)

AFAIK Gerrit tests for SWT are NOT running SWT library built by Gerrit build, means they are not saying anything about the actual change.
Comment 5 Alexandre Honorat CLA 2020-01-04 14:24:02 EST
(In reply to Paul Pazderski from comment #3)
> Almost fine. Please include the bug number at begin of the commit message.
> Not only useful to later find the bug for a change but also a bot will
> automatically link the change here.

Done ;)

(In reply to Paul Pazderski from comment #3) 
> Had you tried to launch the AllNonBrowserTests suite from within eclipse? I
> don't remember there is something special to do. Anyway Gerrit will run the
> tests on your change and since it is already finished I can say Gerrit likes
> your change. ;)

Ok I'll try this on Monday. Following answer of Andrey, it is probably safer if someone tests that manually then!
Comment 6 Lakshmi P Shanmugam CLA 2020-01-06 02:06:46 EST
> (In reply to Paul Pazderski from comment #3) 
> > Had you tried to launch the AllNonBrowserTests suite from within eclipse? I
> > don't remember there is something special to do. Anyway Gerrit will run the
> > tests on your change and since it is already finished I can say Gerrit likes
> > your change. ;)
> 
> Ok I'll try this on Monday. Following answer of Andrey, it is probably safer
> if someone tests that manually then!

@Alexandre,
Thanks for creating the patch!
I was wondering how/where did you find the corrupt swt library?

You can use a snippet from here (https://www.eclipse.org/swt/snippets/#awt) to manually test the failing case.
Which OS do you run on? Since the code is common to all platforms, we should give a quick test on different platforms. I've verified it on Mac. It'll be good to verify it on Linux and Windows.
Comment 8 Alexandre Honorat CLA 2020-01-06 13:28:43 EST
Hello,

thanks for the merge.

(In reply to Lakshmi Shanmugam from comment #6)
> @Alexandre,
> Thanks for creating the patch!
> I was wondering how/where did you find the corrupt swt library?

Ok, well, it is quite unfortunate (or fortunate?), but I cannot reproduce anymore on my machine the (non related) bug that lead to this corrupted swt lib. 
I will try again tomorrow on another machine. I explain after your second quote the closest step to reproduce, on any plateform.

Here is how I identified this bug:

1. We develop an eclipse product, using at some point the SWT_AWT class and one of its native method.
2. When our code executes the SWT_AWT class, it specifies an UnsatisfiedLinkError stating that the initFrame method of the SWT_AWT cannot be found.
3. When putting breakpoints on the swt internal lib loader, it turns out that libswt-awt-gtk-4928r15 cannot be loaded since the symbol SUNWprivate_1.1 is not defined in the .so (and thus is not loaded). And the same for version 4930r7. However, this original error is not thrown to the user, hence the patch I proposed.

We got this problem on two Linux platforms on Ubuntu 18 and 18.04, with Eclipse 2019-09 and Eclipse 2019-12, openjdk11 and oracle 13. A workaround is to use version 4926r21 of libswt-awt (which is not using symbol SUNWprivate_1.1) (can be check with 'objdump -T libswt-awt-gtk-4926r21.so'). 

What is even more surprising, is that the bug never occurred on another machine with a similar environment (same jvm, Eclipse and Linux distribution). My guess is that it is due to an Eclipse configuration of the jvm but we did not find yet exactly.


(In reply to Lakshmi Shanmugam from comment #6)
> You can use a snippet from here (https://www.eclipse.org/swt/snippets/#awt)
> to manually test the failing case.
> Which OS do you run on? Since the code is common to all platforms, we should
> give a quick test on different platforms. I've verified it on Mac. It'll be
> good to verify it on Linux and Windows.


From what you suggested, Snippet135 is already testing SWT_AWT and thus is close to our usage. However, in the configuration of the snippet project, libs are not loaded from jar, and the patch is not tested. If you manage to load from jar, here is a procedure to reproduce:

1. Corrupt libswt-awt-gtk-*.so from the eclipse.platform.swt.binaries project.
2. Create a new file libswt-awt-gtk-*.so with random data:
   'head -c 10737 </dev/urandom >libswt-awt-gtk-4932r7.so' on Linux.
3. Execute Snippet135 (with corrupted .so in a jar).

Without the patch, an UnsatisfiedLinkError is thrown, not giving the last error
Comment 9 Alexandre Honorat CLA 2020-01-06 13:32:26 EST
Created attachment 281386 [details]
Corrupted libswt-awt

This shared object is corrupted, using @LIBC instead of GLIBC.
On Linux, you can check that with the following command:
objdump -T libswt-awt-gtk-4932r7.so
Comment 10 Alexandre Honorat CLA 2020-01-06 13:47:17 EST
(In reply to Alexandre Honorat from comment #8)
> From what you suggested, Snippet135 is already testing SWT_AWT and thus is
> close to our usage. However, in the configuration of the snippet project,
> libs are not loaded from jar, and the patch is not tested. If you manage to
> load from jar, here is a procedure to reproduce:
> 
> 1. Corrupt libswt-awt-gtk-*.so from the eclipse.platform.swt.binaries
> project.
> 2. Create a new file libswt-awt-gtk-*.so with random data:
>    'head -c 10737 </dev/urandom >libswt-awt-gtk-4932r7.so' on Linux.
> 3. Execute Snippet135 (with corrupted .so in a jar).
> 
> Without the patch, an UnsatisfiedLinkError is thrown, not giving the last
> error

Sorry I typed Enter too soon ... So I managed to reproduce the bug fixed by this patch. Instructions to follow on Linux:

1. Download the attachment to this bug, containing a corrupted version of libswt-awt-gtk-4932r7.so (GLIBC has been replaced by @LIBC)

2. Move the corrupted lib to its normal path in the eclipse.platform.swt.binaries repo (bundles/org.eclipse.swt.gtk.linux.x86_64 in this case).

3. Copy the corrupted lib in the folder of SWT "/home/${USER}/.swt/lib/linux/x86_64/"

4. Run the Snippet135 code. With the patch, the error specifies why the lib cannot be loaded (@LIBC symbol not found). Without the patch, the error does not specify the most accurate reason (just stating that initFrame method is not found).

Result should be the same on all platforms (given that you know how to corrupt a dll on Windows, and the swt user path on MacOS).
Comment 11 Lakshmi P Shanmugam CLA 2020-01-07 03:08:47 EST
(In reply to Alexandre Honorat from comment #10)
> (In reply to Alexandre Honorat from comment #8)
> > From what you suggested, Snippet135 is already testing SWT_AWT and thus is
> > close to our usage. However, in the configuration of the snippet project,
> > libs are not loaded from jar, and the patch is not tested. If you manage to
> > load from jar, here is a procedure to reproduce:
> > 
> > 1. Corrupt libswt-awt-gtk-*.so from the eclipse.platform.swt.binaries
> > project.
> > 2. Create a new file libswt-awt-gtk-*.so with random data:
> >    'head -c 10737 </dev/urandom >libswt-awt-gtk-4932r7.so' on Linux.
> > 3. Execute Snippet135 (with corrupted .so in a jar).
> > 
> > Without the patch, an UnsatisfiedLinkError is thrown, not giving the last
> > error
> 
> Sorry I typed Enter too soon ... So I managed to reproduce the bug fixed by
> this patch. Instructions to follow on Linux:
> 
> 1. Download the attachment to this bug, containing a corrupted version of
> libswt-awt-gtk-4932r7.so (GLIBC has been replaced by @LIBC)
> 
> 2. Move the corrupted lib to its normal path in the
> eclipse.platform.swt.binaries repo (bundles/org.eclipse.swt.gtk.linux.x86_64
> in this case).
> 
> 3. Copy the corrupted lib in the folder of SWT
> "/home/${USER}/.swt/lib/linux/x86_64/"
> 
> 4. Run the Snippet135 code. With the patch, the error specifies why the lib
> cannot be loaded (@LIBC symbol not found). Without the patch, the error does
> not specify the most accurate reason (just stating that initFrame method is
> not found).
> 
> Result should be the same on all platforms (given that you know how to
> corrupt a dll on Windows, and the swt user path on MacOS).

Thanks, we used the same steps to verify the patch!
Comment 12 Sravan Kumar Lakkimsetti CLA 2020-01-07 05:36:59 EST
While running Snippet135 on Linux I got the following error

Exception in thread "main" org.eclipse.swt.SWTError: Unspecified error [Error while starting AWT] (java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons: 
	no swt-awt-gtk-4932r8 in java.library.path: [/usr/java/packages/lib, /usr/lib64, /lib64, /lib, /usr/lib]
	no swt-awt-gtk in java.library.path: [/usr/java/packages/lib, /usr/lib64, /lib64, /lib, /usr/lib]
	Can't load library: /home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk-4932r8.so
	Can't load library: /home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk.so
	/home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk-4932r8.so: /home/sravan/JAVA/jdk-13.0.1/lib/libjawt.so: version `SUNWprivate_1.1' not found (required by /home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk-4932r8.so)
)
	at org.eclipse.swt.SWT.error(SWT.java:4746)
	at org.eclipse.swt.awt.SWT_AWT.new_Frame(SWT_AWT.java:166)
	at org.eclipse.swt.snippets.Snippet135.main(Snippet135.java:155)
Caused by: java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons: 
	no swt-awt-gtk-4932r8 in java.library.path: [/usr/java/packages/lib, /usr/lib64, /lib64, /lib, /usr/lib]
	no swt-awt-gtk in java.library.path: [/usr/java/packages/lib, /usr/lib64, /lib64, /lib, /usr/lib]
	Can't load library: /home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk-4932r8.so
	Can't load library: /home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk.so
	/home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk-4932r8.so: /home/sravan/JAVA/jdk-13.0.1/lib/libjawt.so: version `SUNWprivate_1.1' not found (required by /home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk-4932r8.so)

	at org.eclipse.swt.internal.Library.loadLibrary(Library.java:342)
	at org.eclipse.swt.internal.Library.loadLibrary(Library.java:256)
	at org.eclipse.swt.awt.SWT_AWT.loadLibrary(SWT_AWT.java:77)
	at org.eclipse.swt.awt.SWT_AWT.new_Frame(SWT_AWT.java:162)
	... 1 more

Here is my configuration
Eclipse SDK
Version: 2020-03 (4.15)
Build id: I20200106-1805
OS: Linux, v.4.15.0-72-generic, x86_64 / gtk 3.22.30, WebKit 2.26.2
Java version: 13.0.1
Comment 13 Sravan Kumar Lakkimsetti CLA 2020-01-07 05:43:45 EST
(In reply to Sravan Kumar Lakkimsetti from comment #12)
> While running Snippet135 on Linux I got the following error
> 
> Exception in thread "main" org.eclipse.swt.SWTError: Unspecified error
> [Error while starting AWT] (java.lang.UnsatisfiedLinkError: Could not load
> SWT library. Reasons: 
> 	no swt-awt-gtk-4932r8 in java.library.path: [/usr/java/packages/lib,
> /usr/lib64, /lib64, /lib, /usr/lib]
> 	no swt-awt-gtk in java.library.path: [/usr/java/packages/lib, /usr/lib64,
> /lib64, /lib, /usr/lib]
> 	Can't load library:
> /home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk-4932r8.so
> 	Can't load library: /home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk.so
> 	/home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk-4932r8.so:
> /home/sravan/JAVA/jdk-13.0.1/lib/libjawt.so: version `SUNWprivate_1.1' not
> found (required by
> /home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk-4932r8.so)
> )
> 	at org.eclipse.swt.SWT.error(SWT.java:4746)
> 	at org.eclipse.swt.awt.SWT_AWT.new_Frame(SWT_AWT.java:166)
> 	at org.eclipse.swt.snippets.Snippet135.main(Snippet135.java:155)
> Caused by: java.lang.UnsatisfiedLinkError: Could not load SWT library.
> Reasons: 
> 	no swt-awt-gtk-4932r8 in java.library.path: [/usr/java/packages/lib,
> /usr/lib64, /lib64, /lib, /usr/lib]
> 	no swt-awt-gtk in java.library.path: [/usr/java/packages/lib, /usr/lib64,
> /lib64, /lib, /usr/lib]
> 	Can't load library:
> /home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk-4932r8.so
> 	Can't load library: /home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk.so
> 	/home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk-4932r8.so:
> /home/sravan/JAVA/jdk-13.0.1/lib/libjawt.so: version `SUNWprivate_1.1' not
> found (required by
> /home/sravan/.swt/lib/linux/x86_64/libswt-awt-gtk-4932r8.so)
> 
> 	at org.eclipse.swt.internal.Library.loadLibrary(Library.java:342)
> 	at org.eclipse.swt.internal.Library.loadLibrary(Library.java:256)
> 	at org.eclipse.swt.awt.SWT_AWT.loadLibrary(SWT_AWT.java:77)
> 	at org.eclipse.swt.awt.SWT_AWT.new_Frame(SWT_AWT.java:162)
> 	... 1 more
> 
> Here is my configuration
> Eclipse SDK
> Version: 2020-03 (4.15)
> Build id: I20200106-1805
> OS: Linux, v.4.15.0-72-generic, x86_64 / gtk 3.22.30, WebKit 2.26.2
> Java version: 13.0.1

I don't think this is related the code change done here. Raised bug 558874 to track this
Comment 14 Niraj Modi CLA 2020-01-08 03:30:21 EST
Verified the fix on Win10 using Build id: I20200106-1805.