Bug 569874 - [GTK] compiling SWT with gcc -std=c99 leads to crash in native file dialog
Summary: [GTK] compiling SWT with gcc -std=c99 leads to crash in native file dialog
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.18   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Platform-SWT-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-12-22 08:16 EST by Andrey Loskutov CLA
Modified: 2021-01-09 06:29 EST (History)
2 users (show)

See Also:


Attachments
crash dump (156.74 KB, text/x-log)
2020-12-22 08:16 EST, Andrey Loskutov CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andrey Loskutov CLA 2020-12-22 08:16:10 EST
Created attachment 285100 [details]
crash dump

See bug 569872.

Apply reverse patch https://git.eclipse.org/r/c/platform/eclipse.platform.swt/+/174052/1/bundles/org.eclipse.swt/Eclipse+SWT+PI/gtk/library/build.sh
to compile SWT with gcc -std=c99 option.

Compilation works (and we have SDK build with all SWT tests passing: https://download.eclipse.org/eclipse/downloads/drops4/I20201221-1800/), but resulting binary doesn't - it can start Eclipse SDK, but trying to open native file dialog results in the crash in GTK code. gdb backtrace on core file doesn't show anything useful.

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007fff63734757, pid=42147, tid=42148
#
# JRE version: OpenJDK Runtime Environment 18.9 (11.0.8+10) (build 11.0.8+10-LTS)
# Java VM: OpenJDK 64-Bit Server VM 18.9 (11.0.8+10-LTS, mixed mode, sharing, tiered, compressed oops, g1 gc, linux-amd64)
# Problematic frame:
# C  [libglib-2.0.so.0+0x39757]  g_path_is_absolute+0x7
#
# Core dump will be written. Default location: Core dumps may be processed with "/usr/libexec/abrt-hook-ccpp %s %c %p %u %g %t e %P %I %h" (or dumping to /data/eclipse4.19/eclipse/core.42147)
#
# If you would like to submit a bug report, please visit:
#   https://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

---------------  S U M M A R Y ------------

Command Line: -Dosgi.requiredJavaVersion=11 -Dosgi.dataAreaRequiresExplicitInit=true -Xms256m -Xcheck:jni -Xmx16G -Declipse.log.size.max=20000 -Dorg.eclipse.core.resources.allowEarlyInnerBuildLoopExit -Dorg.eclipse.swt.internal.gtk.noThemingFixes -Dorg.eclipse.swt.internal.gtk.enableStrictChecks -Dorg.eclipse.wst.validation.ui.disable.validation.context.menu=true -Declipse.p2.unsignedPolicy=allow --add-modules=ALL-SYSTEM /data/eclipse4.19/eclipse//plugins/org.eclipse.equinox.launcher_1.6.0.v20200915-1508.jar -data file:/tmp/wsp/ -os linux -ws gtk -arch x86_64 -showsplash -launcher /data/eclipse4.19/eclipse/eclipse -name Eclipse --launcher.library /data/eclipse4.19/eclipse//plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.2.0.v20200915-1442/eclipse_11201.so -startup /data/eclipse4.19/eclipse//plugins/org.eclipse.equinox.launcher_1.6.0.v20200915-1508.jar --launcher.appendVmargs -exitdata 57418028 -vm /usr/lib/jvm/java-11/bin/java -vmargs -Dosgi.requiredJavaVersion=11 -Dosgi.dataAreaRequiresExplicitInit=true -Xms256m -Xcheck:jni -Xmx16G -Declipse.log.size.max=20000 -Dorg.eclipse.core.resources.allowEarlyInnerBuildLoopExit -Dorg.eclipse.swt.internal.gtk.noThemingFixes -Dorg.eclipse.swt.internal.gtk.enableStrictChecks -Dorg.eclipse.wst.validation.ui.disable.validation.context.menu=true -Declipse.p2.unsignedPolicy=allow --add-modules=ALL-SYSTEM -jar /data/eclipse4.19/eclipse//plugins/org.eclipse.equinox.launcher_1.6.0.v20200915-1508.jar

Host: Intel(R) Xeon(R) W-2145 CPU @ 3.70GHz, 16 cores, 251G, Red Hat Enterprise Linux Workstation release 7.4 (Maipo)
Time: Tue Dec 22 11:27:49 2020 CET elapsed time: 60 seconds (0d 0h 1m 0s)

---------------  T H R E A D  ---------------

Current thread (0x00007ffff0017000):  JavaThread "main" [_thread_in_native, id=42148, stack(0x00007ffff7ecc000,0x00007ffff7fcd000)]

Stack: [0x00007ffff7ecc000,0x00007ffff7fcd000],  sp=0x00007ffff7fc9848,  free space=1014k
Native frames: (J=compiled Java code, A=aot compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [libglib-2.0.so.0+0x39757]  g_path_is_absolute+0x7

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  org.eclipse.swt.internal.gtk.GTK.gtk_file_chooser_set_current_folder(JJ)V+0
j  org.eclipse.swt.widgets.DirectoryDialog.openNativeChooserDialog()Ljava/lang/String;+182
j  org.eclipse.swt.widgets.DirectoryDialog.open()Ljava/lang/String;+1
j  org.eclipse.ui.internal.ide.ChooseWorkspaceDialog$4.widgetSelected(Lorg/eclipse/swt/events/SelectionEvent;)V+43
J 11104 c1 org.eclipse.swt.widgets.TypedListener.handleEvent(Lorg/eclipse/swt/widgets/Event;)V (1291 bytes) @ 0x00007fffdaa47394 [0x00007fffdaa42420+0x0000000000004f74]
J 10515 c2 org.eclipse.swt.widgets.EventTable.sendEvent(Lorg/eclipse/swt/widgets/Event;)V (592 bytes) @ 0x00007fffe0d0edf4 [0x00007fffe0d0ed40+0x00000000000000b4]
J 10825 c2 org.eclipse.swt.widgets.Display.runDeferredEvents()Z (109 bytes) @ 0x00007fffe0d56abc [0x00007fffe0d568a0+0x000000000000021c]
J 10595 c1 org.eclipse.swt.widgets.Display.readAndDispatch()Z (77 bytes) @ 0x00007fffda8c1d7c [0x00007fffda8c17e0+0x000000000000059c]
j  org.eclipse.jface.window.Window.runEventLoop(Lorg/eclipse/swt/widgets/Shell;)V+23
j  org.eclipse.jface.window.Window.open()I+49
j  org.eclipse.ui.internal.ide.ChooseWorkspaceDialog.prompt(Z)V+15
j  org.eclipse.ui.internal.ide.actions.OpenWorkspaceAction.showChooseWorkspaceDialog(Lorg/eclipse/ui/internal/ide/ChooseWorkspaceData;)V+22
j  org.eclipse.ui.internal.ide.actions.OpenWorkspaceAction.promptForWorkspace()Ljava/lang/String;+18
j  org.eclipse.ui.internal.ide.actions.OpenWorkspaceAction.run()V+1
j  org.eclipse.ui.internal.ide.actions.OpenWorkspaceAction$OpenDialogAction.run()V+4
j  org.eclipse.jface.action.Action.runWithEvent(Lorg/eclipse/swt/widgets/Event;)V+1
j  org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(Lorg/eclipse/swt/widgets/Event;Z)V+361
j  org.eclipse.jface.action.ActionContributionItem.lambda$4(Lorg/eclipse/swt/widgets/Event;)V+54
j  org.eclipse.jface.action.ActionContributionItem$$Lambda$388.handleEvent(Lorg/eclipse/swt/widgets/Event;)V+5
J 10515 c2 org.eclipse.swt.widgets.EventTable.sendEvent(Lorg/eclipse/swt/widgets/Event;)V (592 bytes) @ 0x00007fffe0d0edf4 [0x00007fffe0d0ed40+0x00000000000000b4]
J 9820 c2 org.eclipse.swt.widgets.Display.sendEvent(Lorg/eclipse/swt/widgets/EventTable;Lorg/eclipse/swt/widgets/Event;)V (34 bytes) @ 0x00007fffe0c31b48 [0x00007fffe0c31ae0+0x0000000000000068]
J 8929 c1 org.eclipse.swt.widgets.Widget.sendEvent(ILorg/eclipse/swt/widgets/Event;Z)V (88 bytes) @ 0x00007fffda661fa4 [0x00007fffda6619c0+0x00000000000005e4]
j  org.eclipse.swt.widgets.Widget.sendEvent(ILorg/eclipse/swt/widgets/Event;)V+4
j  org.eclipse.swt.widgets.Widget.notifyListeners(ILorg/eclipse/swt/widgets/Event;)V+19
j  org.eclipse.jface.action.ActionContributionItem.lambda$8(Lorg/eclipse/swt/widgets/Event;)V+87
j  org.eclipse.jface.action.ActionContributionItem$$Lambda$717.handleEvent(Lorg/eclipse/swt/widgets/Event;)V+1
J 10515 c2 org.eclipse.swt.widgets.EventTable.sendEvent(Lorg/eclipse/swt/widgets/Event;)V (592 bytes) @ 0x00007fffe0d0edf4 [0x00007fffe0d0ed40+0x00000000000000b4]
J 10825 c2 org.eclipse.swt.widgets.Display.runDeferredEvents()Z (109 bytes) @ 0x00007fffe0d56abc [0x00007fffe0d568a0+0x000000000000021c]
J 10595 c1 org.eclipse.swt.widgets.Display.readAndDispatch()Z (77 bytes) @ 0x00007fffda8c1d7c [0x00007fffda8c17e0+0x000000000000059c]
j  org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run()V+564
j  org.eclipse.core.databinding.observable.Realm.runWithDefault(Lorg/eclipse/core/databinding/observable/Realm;Ljava/lang/Runnable;)V+12
j  org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(Lorg/eclipse/e4/ui/model/application/MApplicationElement;Lorg/eclipse/e4/core/contexts/IEclipseContext;)Ljava/lang/Object;+57
j  org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(Lorg/eclipse/e4/ui/model/application/MApplicationElement;)V+20
j  org.eclipse.ui.internal.Workbench.lambda$3(Lorg/eclipse/swt/widgets/Display;Lorg/eclipse/ui/application/WorkbenchAdvisor;[I)V+393
j  org.eclipse.ui.internal.Workbench$$Lambda$139.run()V+12
j  org.eclipse.core.databinding.observable.Realm.runWithDefault(Lorg/eclipse/core/databinding/observable/Realm;Ljava/lang/Runnable;)V+12
j  org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Lorg/eclipse/swt/widgets/Display;Lorg/eclipse/ui/application/WorkbenchAdvisor;)I+16
j  org.eclipse.ui.PlatformUI.createAndRunWorkbench(Lorg/eclipse/swt/widgets/Display;Lorg/eclipse/ui/application/WorkbenchAdvisor;)I+2
j  org.eclipse.ui.internal.ide.application.IDEApplication.start(Lorg/eclipse/equinox/app/IApplicationContext;)Ljava/lang/Object;+113
j  org.eclipse.equinox.internal.app.EclipseAppHandle.run(Ljava/lang/Object;)Ljava/lang/Object;+138
j  org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(Ljava/lang/Object;)Ljava/lang/Object;+85
j  org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(Ljava/lang/Object;)Ljava/lang/Object;+79
j  org.eclipse.core.runtime.adaptor.EclipseStarter.run(Ljava/lang/Object;)Ljava/lang/Object;+99
j  org.eclipse.core.runtime.adaptor.EclipseStarter.run([Ljava/lang/String;Ljava/lang/Runnable;)Ljava/lang/Object;+132
v  ~StubRoutines::call_stub
j  jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Ljava/lang/reflect/Method;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+0 java.base@11.0.8
j  jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+100 java.base@11.0.8
j  jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+6 java.base@11.0.8
j  java.lang.reflect.Method.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+59 java.base@11.0.8
j  org.eclipse.equinox.launcher.Main.invokeFramework([Ljava/lang/String;[Ljava/net/URL;)V+201
j  org.eclipse.equinox.launcher.Main.basicRun([Ljava/lang/String;)V+159
j  org.eclipse.equinox.launcher.Main.run([Ljava/lang/String;)I+4
j  org.eclipse.equinox.launcher.Main.main([Ljava/lang/String;)V+10
v  ~StubRoutines::call_stub

siginfo: si_signo: 11 (SIGSEGV), si_code: 1 (SEGV_MAPERR), si_addr: 0xfffffffff08fa000
Comment 1 Andrey Loskutov CLA 2020-12-22 08:28:11 EST
Detailed steps to reproduce.

Open /org.eclipse.swt/Eclipse SWT PI/gtk/library/build.sh

Apply this patch:
###################################################
diff --git a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/build.sh b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/build.sh
index 0490465..61f0144 100755
--- a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/build.sh
+++ b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/build.sh
@@ -118,3 +118,3 @@
 		if [ "${CC}" = "" ]; then
-			export CC=gcc
+			export CC="gcc -std=c99"
 		fi
###################################################

Open org.eclipse.swt.gtk.linux.x86_64 project in workspace.
cd org.eclipse.swt/bin/library
make clean ; ./build.sh -gtk3 install
Refresh org.eclipse.swt.gtk.linux.x86_64 project in Eclipse.
Start Eclipse SDK from debugger.
Go to "File -> Open File"
Observe JVM crash.

I'm using GTK gtk3-3.22.30-3.el7.x86_64 on RHEL 7.4.
Comment 2 Sravan Kumar Lakkimsetti CLA 2020-12-22 09:49:43 EST
(In reply to Andrey Loskutov from comment #1)
> 
> Open org.eclipse.swt.gtk.linux.x86_64 project in workspace.
> cd org.eclipse.swt/bin/library
> make clean ; ./build.sh -gtk3 install
> Refresh org.eclipse.swt.gtk.linux.x86_64 project in Eclipse.

these steps can be replaced with this

Open org.eclipse.swt.gtk.linux.x86_64 project in workspace
perform an ant build on org.eclipse.swt.gtk.linux.x86_64/build.xml with target "build_libraries"

This will build swt libraries and refreshes the workspace.
Comment 3 Alexandr Miloslavskiy CLA 2021-01-07 12:01:17 EST
The first thing I encountered was a large swarm of compiler warnings, I made Bug Bug 570178 to fix that.

Among these warnings, there was also a warning relevant to the new '-std=c99':
----
c.c: In function ‘Java_org_eclipse_swt_internal_C_setenv’:
c.c:338:13: warning: implicit declaration of function ‘setenv’; did you mean ‘getenv’? [-Wimplicit-function-declaration]
  338 |  rc = (jint)setenv((const char *)lparg0, (const char *)lparg1, arg2);
      |             ^~~~~~
      |             getenv
----

I'm still investigating.
Comment 4 Alexandr Miloslavskiy CLA 2021-01-07 12:24:57 EST
Turns out that 'setenv()' problem is boring because it's not currently used in SWT.

Unfortunately I'm not able to reproduce the problem on my Ubuntu 20.04.

My steps:
1) Reverted commit de804534 to restore '-std=c99'
2) Built native libraries and made sure that '-std=c99' is present in compiler console output
3) Tried 'File | Open file'
4) Tried 'File | Open Projects from filesystem | Directory...'

I managed to reproduce a very similar crash when I mistakenly built/replaced native libraries while Eclipse was already running with DirectoryDialog open.
Andrey, is it possible that you did the same thing?

Replacing binaries from under a running process is not fair, of course.

Can anyone confirm that the problem is reproducible?
Comment 5 Andrey Loskutov CLA 2021-01-07 12:32:28 EST
(In reply to Alexandr Miloslavskiy from comment #4)
> Can anyone confirm that the problem is reproducible?

Please try this build: https://download.eclipse.org/eclipse/downloads/drops4/I20201221-1800/

Start SDK and try to use File -> Open.
I can reproduce the crash on RHEL 7.4 / GTK 3.22. May be this RHEL has too old standard C library or something like this.
Comment 6 Alexandr Miloslavskiy CLA 2021-01-07 12:38:23 EST
Interesting, with this build I also get the crash on my Ubuntu 20.04. I'll continue investigating, thanks!
Comment 7 Alexandr Miloslavskiy CLA 2021-01-07 13:38:27 EST
This happens because the binaries are compiled in a weird way.

The culprit in faulty build is this part of compiled 'Java_org_eclipse_swt_internal_gtk_OS_realpath' in native library:
----
call   0x7f7d4e535400 <realpath@plt>
movsxd r14,eax
----

When translated to human language, it means that compiler believes that realpath() returns (4-byte signed int) instead of (8-byte unsigned pointer).

When I build on my Ubuntu 20.04, I get the expected result:
----
call   0x7fffcc6f9570 <realpath@plt>
mov    r14,rax
----

And the crashes are not reproducible.

Going to try on CentOS 7.
Comment 8 Alexandr Miloslavskiy CLA 2021-01-07 14:07:28 EST
Alrighty, on CentOS 7 building the native libraries spits another warning:

----
os.c: In function 'Java_org_eclipse_swt_internal_gtk_OS_realpath':
os.c:20127:2: warning: implicit declaration of function 'realpath' [-Wimplicit-function-declaration]
  rc = (jlong)realpath((const char *)lparg0, (char *)lparg1);
----

To my understanding:
1) gcc defaults to '-std=gnu90', which also defines '_POSIX_C_SOURCE', which makes POSIX apis like 'realpath' visible.
2) with '-std=c99' specified, POSIX apis are no longer visible.
3) When C compiler encounters unknown function, it implicitly-declares it as a function returning int. I never understood this behavior, but oh well, compatibility...
4) Function actually returns a pointer, which now gets halved into a 4-byte signed int, discarding the other 4 bytes.

It seems that I did the right move in Bug 570178 where the final patch forces all warnings to be errors. Go vote for it ;)