[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Gambas-bugtracker] Bug #3256: Two Gambas JIT bugs on the error path
[Thread Prev] | [Thread Next]
- Subject: [Gambas-bugtracker] Bug #3256: Two Gambas JIT bugs on the error path
- From: <bugtracker@xxxxxxxxxxxxxx>
- Date: Mon, 16 Nov 2026 23:27:22 GMT
- To: brian@xxxxxxxxxxxxxxxx,bugtracker@xxxxxxxxxxxxxxxxxxxxxx
http://gambaswiki.org/bugtracker/edit?object=BUG.3256&from=L21haW4- Brian G reported a new bug. Summary ------- Two Gambas JIT bugs on the error path Type : Bug Priority : High Gambas version : Master Product : JIT compiler Description ----------- Found while testing the ShmMem library. Both reproduce with the small projects attached, which have nothing to do with ShmMem, both happen with gcc as the JIT compiler, and both disappear when the JIT is turned off (gbx3 -j). Both release a value one time too many after an error, so the program crashes later or corrupts memory. Gambas: master at 8d2fdc7cf (3.22.99), JIT compiled with gcc 13 Platform: Linux x86_64 gambas-jit-fix.patch: proposed fix for both, against main/lib/jit/gb.jit/jit.h and jit_after.h Run a repro with: cd <repro dir> gbc3 -ag . GB_JIT_CC=gcc GB_JIT_CFLAGS=-O0 gbx3 .GB_JIT_CC=gcc gbx3 .gbx3 -j . # JIT with gcc -O0 # JIT with the default -O2 -march=native -mtune=native # JIT off: correct _______________________________________________________________________________________________________________ Bug 1: arguments released twice when a called function raises an error (bug1-error-path-double-release) A Fast method calls a non-JIT method that raises an error. The arguments of the call are released twice, and an object passed as an argument loses a reference: it is freed while it is still used. ' Holder.class (Fast) Public Vars As New Store Public Sub _put(value As Variant, SymName As String, ...) vars[SymName] = value ' Store._put (not Fast) raises an error End ' Main Dim o As New Collection Try h["x"] = o Print Object.Count(o) ' JIT: crash, gbx3 -j: 1 gcc -O0: crash gcc -O2: correct in this repro, but the same code in a larger method lost the reference with -O2 too: it depends on the register allocation of the function gbx3 -j: correct Cause _jit_print_catch() in jit_after.h: if (SP > sp) sp = SP; else SP = sp; LEAVE_SUPER(); if (sp > ssp) { JIT.release_many(sp, sp - ssp); SP = sp = ssp; Before the call, the JIT code pushes the arguments and sets SP = sp. The called function raises an error. EXEC_leave_drop() releases its parameters and lowers SP. longjmp returns to the JIT function. Its local sp still counts the pushed arguments: sp is not volatile and was changed after setjmp, so its value is undefined. With -O0 it is the value with the arguments pushed, with -O2 sometimes an older one kept in a register. sp > SP, so SP = sp, and release_many releases the arguments a second time. _jit_end_try() has the same line. Fix Every call macro and every THROW in jit.h sets SP = sp first, and the interpreter only lowers SP when it releases what it owns. So SP is always right in the error handler, and the local sp must not be used: sp = SP; in _jit_print_catch() and _jit_end_try(). _____________________________________________________________________________________________ Bug 2: a value popped for a call argument is released again by Try (bug2-try-popped-arg-released) A Fast method uses Try around a call to another Fast method, the argument is computed with &, and the called method raises an error. The concatenated string is released twice and the string memory is corrupted: the program crashes a little later, in the repro after a few iterations. Public Function Lookup(SegName As String) As Variant Dim ns As Variant[] Try ns = Resolve(SegName & ":") ' Resolve raises an error If Error Then Return Null Return ns[0] End gcc -O0, gcc -O2: crash gbx3 -j: correct Resolve(SegName), Resolve(Left(SegName, 3)) and Resolve("lit") do not crash. Cause The direct call is generated as ({SP=sp;jit_finder_3_(({PUSH_s(p0);PUSH_t(GET_CHAR(58));CALL_SUBR_CODE(6, ..., 0x3C02);POP_s();}));}) SP=sp is done before the argument is computed. CALL_SUBR_CODE sets SP = sp with the two strings pushed; the concatenation leaves its result there. POP_s() gives the result to the C argument and lowers the local sp, but not SP. The global SP still counts the slot. The called function starts with sp = SP, one slot too high. When it raises the error, _jit_end_try() of the caller releases everything from SP down to EP, and that includes the popped string, which the callee already received and released. The original if (SP > sp) sp = SP; else SP = sp; takes the higher SP too, so this does not depend on the fix for bug 1. Fix The POP_* macros lower SP with sp: #define POP_s() ({ sp--; SP = sp; JIT.unborrow(sp); *((GB_STRING*)sp); }) A value above sp is never alive, so SP never counts a value that was given away. The vararg arguments of a direct call are pushed below sp and stay counted. A cleaner fix may be to emit SP=sp; in jit_body.c after the arguments of a direct call are computed instead of before. System information ------------------ [System] Gambas=3.22.99 8d2fdc7cf (jit-libtcc) OperatingSystem=Linux Distribution=Linux Mint 22.3 (Zena) Kernel=7.2.0-070200-generic Architecture=x86_64 Cores=8 Memory=13923M Language=en_US.UTF-8 Platform=x11 Desktop=MATE DesktopResolution=96 DesktopScale=7 WidgetTheme=proxy Font=Ubuntu Mono,10 DarkTheme=False [Programs] appimage-builder=appimage-builder: 1.1.0 dpkg=Debian 'dpkg' package management program version 1.22.6 (amd64). gcc=gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 git=git version 2.43.0 msgmerge=msgmerge (GNU gettext-tools) 0.21 rpmbuild=RPM version 4.18.2 tcc=tcc version 0.9.27 (x86_64 Linux) [Libraries] Cairo=libcairo.so.2.11800.0 Curl=libcurl.so.4.8.0 DBus=libdbus-1.so.3.32.4 GDK2=libgdk-x11-2.0.so.0.2400.33 GDK3=libgdk-3.so.0.2409.32 GStreamer=libgstreamer-1.0.so.0.2402.0 GTK+2=libgtk-x11-2.0.so.0.2400.33 GTK+3=libgtk-3.so.0.2409.32 OpenGL=libGL.so.1.7.0 Poppler=libpoppler.so.134.0.0 QT5=libQt5Core.so.5.15.13 QT6=libQt6Core.so.6.4.2 RSvg=librsvg-2.so.2.50.0 SDL=libSDL2-2.0.so.0.3000.0 SQLite3=libsqlite3.so.0.8.6 [Environment] CLUTTER_BACKEND=x11 COMPIZ_CONFIG_PROFILE=mint DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus DESKTOP_SESSION=mate DISPLAY=:0 EDITOR=kwrite GB_GUI=gb.qt6 GDMSESSION=mate GDM_LANG=en_US GIO_LAUNCHED_DESKTOP_FILE_PID=3482031 GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1 GTK3_MODULES=xapp-gtk3-module GTK_MODULES=gail:atk-bridge GTK_OVERLAY_SCROLLING=0 HOME=<home> LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8 LC_ADDRESS=en_US.UTF-8 LC_IDENTIFICATION=en_US.UTF-8 LC_MEASUREMENT=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_NAME=en_US.UTF-8 LC_NUMERIC=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_TELEPHONE=en_US.UTF-8 LC_TIME=en_US.UTF-8 LIBVIRT_DEFAULT_URI=qemu:///system LOGNAME=<user> MATE_DESKTOP_SESSION_ID=this-is-deprecated NVM_BIN=<home>/.nvm/versions/node/v24.11.0/bin NVM_CD_FLAGS= NVM_DIR=<home>/.nvm NVM_INC=<home>/.nvm/versions/node/v24.11.0/include/node PAPERSIZE=a4 PATH=<home>/.nvm/versions/node/v24.11.0/bin:<home>/.config/emacs/bin:<home>/.local/bin:<home>/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin PICO_SDK_PATH=<home>/Projects/picomite/pico/pico-sdk PWD=<home> QT_ACCESSIBILITY=1 QT_FONT_DPI=96 QT_LOGGING_RULES=*.debug=false QT_SCALE_FACTOR=1 SESSION_MANAGER=local/<hostname>:@/tmp/.ICE-unix/4134,unix/<hostname>:/tmp/.ICE-unix/4134 SHELL=/usr/bin/gsh SHLVL=0 SSH_AUTH_SOCK=/run/user/1000/keyring/ssh TZ=:/etc/localtime USER=<user> XAUTHORITY=<home>/.Xauthority XDG_ACTIVATION_TOKEN=mate-panel-4661-<hostname>-gambas3-82_TIME609513372 XDG_CONFIG_DIRS=/etc/xdg/xdg-mate:/etc/xdg XDG_CURRENT_DESKTOP=MATE XDG_DATA_DIRS=/usr/share/mate:<home>/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/<user> XDG_RUNTIME_DIR=/run/user/1000 XDG_SEAT=seat0 XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0 XDG_SESSION_CLASS=user XDG_SESSION_DESKTOP=mate XDG_SESSION_ID=c2 XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0 XDG_SESSION_TYPE=x11 XDG_VTNR=7 gshlibrary=<home>/Projects/gshlibrary gshproject=<home>/Projects/gsh ----[ Gambas bugtracker-list is hosted by https://www.hostsharing.net ]----
| [Gambas-bugtracker] Bug #3256: Two Gambas JIT bugs on the error path | <bugtracker@xxxxxxxxxxxxxx> |
| [Gambas-bugtracker] Bug #3256: Two Gambas JIT bugs on the error path | <bugtracker@xxxxxxxxxxxxxx> |
| [Gambas-bugtracker] Bug #3256: Two Gambas JIT bugs on the error path | <bugtracker@xxxxxxxxxxxxxx> |
| [Gambas-bugtracker] Bug #3256: Two Gambas JIT bugs on the error path | <bugtracker@xxxxxxxxxxxxxx> |
| [Gambas-bugtracker] Bug #3256: Two Gambas JIT bugs on the error path | <bugtracker@xxxxxxxxxxxxxx> |
| [Gambas-bugtracker] Bug #3256: Two Gambas JIT bugs on the error path | <bugtracker@xxxxxxxxxxxxxx> |