fortrangoingonforty/fortty / 19adcf3

Browse files

Fix segfault caused by noexecstack linker flag

- Remove -Wl,-z,noexecstack flag which crashes gfortran trampolines
- Add -fno-trampolines compiler flag (best practice)
- Fix unused argument suppression using c_associated() instead of transfer()
- gfortran requires executable stack for internal procedure callbacks (do_render)
Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
19adcf3d5bb643e33da838da3d75e69af572fd6d
Parents
7c4a91a
Tree
6b47de8

2 changed files

StatusFile+-
M CMakeLists.txt 5 3
M src/window/window.f90 13 12
CMakeLists.txtmodified
@@ -5,9 +5,11 @@ project(fortty LANGUAGES Fortran C)
5
 set(CMAKE_C_STANDARD 11)
5
 set(CMAKE_C_STANDARD 11)
6
 set(CMAKE_C_STANDARD_REQUIRED ON)
6
 set(CMAKE_C_STANDARD_REQUIRED ON)
7
 
7
 
8
-# Compiler warnings
8
+# Compiler warnings and security flags
9
 if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
9
 if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
10
     add_compile_options(-Wall -Wextra -pedantic)
10
     add_compile_options(-Wall -Wextra -pedantic)
11
+    # Avoid trampolines which require executable stack
12
+    add_compile_options(-fno-trampolines)
11
 endif()
13
 endif()
12
 
14
 
13
 # Find dependencies
15
 # Find dependencies
@@ -112,8 +114,8 @@ endif()
112
 # For Linux, we need dl for GLAD's dlopen
114
 # For Linux, we need dl for GLAD's dlopen
113
 if(UNIX AND NOT APPLE)
115
 if(UNIX AND NOT APPLE)
114
     target_link_libraries(fortty PRIVATE dl)
116
     target_link_libraries(fortty PRIVATE dl)
115
-    # Mark stack as non-executable to suppress linker warning
117
+    # Note: Cannot use -Wl,-z,noexecstack as gfortran's internal procedures
116
-    target_link_options(fortty PRIVATE -Wl,-z,noexecstack)
118
+    # used as callbacks (do_render) require executable stack for trampolines
117
 endif()
119
 endif()
118
 
120
 
119
 # TODO: Link Cocoa framework for blur when CMake framework flag issue is resolved
121
 # TODO: Link Cocoa framework for blur when CMake framework flag issue is resolved
src/window/window.f90modified
@@ -231,8 +231,8 @@ contains
231
     type(c_ptr), value :: window
231
     type(c_ptr), value :: window
232
     integer(c_int), value :: width, height
232
     integer(c_int), value :: width, height
233
 
233
 
234
-    ! Unused argument (required by GLFW callback signature)
234
+    ! Suppress unused argument warning (required by GLFW callback signature)
235
-    if (.false.) print *, transfer(window, 0_c_int)
235
+    if (.false. .and. c_associated(window)) continue
236
 
236
 
237
     call glViewport(0, 0, width, height)
237
     call glViewport(0, 0, width, height)
238
 
238
 
@@ -248,8 +248,8 @@ contains
248
   subroutine window_refresh_callback(window) bind(C)
248
   subroutine window_refresh_callback(window) bind(C)
249
     type(c_ptr), value :: window
249
     type(c_ptr), value :: window
250
 
250
 
251
-    ! Unused argument (required by GLFW callback signature)
251
+    ! Suppress unused argument warning (required by GLFW callback signature)
252
-    if (.false.) print *, transfer(window, 0_c_int)
252
+    if (.false. .and. c_associated(window)) continue
253
 
253
 
254
     ! Trigger a redraw via the render callback
254
     ! Trigger a redraw via the render callback
255
     if (associated(render_callback)) then
255
     if (associated(render_callback)) then
@@ -525,8 +525,8 @@ contains
525
     character(len=4) :: utf8
525
     character(len=4) :: utf8
526
     integer :: utf8_len
526
     integer :: utf8_len
527
 
527
 
528
-    ! Unused argument (required by GLFW callback signature)
528
+    ! Suppress unused argument warning (required by GLFW callback signature)
529
-    if (.false.) print *, transfer(window, 0_c_int)
529
+    if (.false. .and. c_associated(window)) continue
530
 
530
 
531
     if (.not. associated(active_pty)) return
531
     if (.not. associated(active_pty)) return
532
 
532
 
@@ -658,8 +658,8 @@ contains
658
     integer(c_int), value :: error_code
658
     integer(c_int), value :: error_code
659
     type(c_ptr), value :: description
659
     type(c_ptr), value :: description
660
 
660
 
661
-    ! Unused argument (required by GLFW callback signature)
661
+    ! Suppress unused argument warning (required by GLFW callback signature)
662
-    if (.false.) print *, transfer(description, 0_c_int)
662
+    if (.false. .and. c_associated(description)) continue
663
 
663
 
664
     print *, "GLFW Error ", error_code
664
     print *, "GLFW Error ", error_code
665
   end subroutine error_callback
665
   end subroutine error_callback
@@ -670,8 +670,9 @@ contains
670
     real(c_double), value :: xoffset, yoffset
670
     real(c_double), value :: xoffset, yoffset
671
     integer :: scroll_lines
671
     integer :: scroll_lines
672
 
672
 
673
-    ! Unused arguments (required by GLFW callback signature)
673
+    ! Suppress unused argument warnings (required by GLFW callback signature)
674
-    if (.false.) print *, transfer(window, 0_c_int), xoffset
674
+    if (.false. .and. c_associated(window)) continue
675
+    if (.false. .and. xoffset > 0.0d0) continue
675
 
676
 
676
     if (.not. associated(active_term)) return
677
     if (.not. associated(active_term)) return
677
 
678
 
@@ -807,8 +808,8 @@ contains
807
     real(c_double), value :: xpos, ypos
808
     real(c_double), value :: xpos, ypos
808
     integer :: col, row
809
     integer :: col, row
809
 
810
 
810
-    ! Unused argument (required by GLFW callback signature)
811
+    ! Suppress unused argument warning (required by GLFW callback signature)
811
-    if (.false.) print *, transfer(window, 0_c_int)
812
+    if (.false. .and. c_associated(window)) continue
812
 
813
 
813
     ! Only update if actively selecting
814
     ! Only update if actively selecting
814
     if (.not. active_selection%selecting) return
815
     if (.not. active_selection%selecting) return