Skip to content

Conversation

@dearblue
Copy link
Contributor

@dearblue dearblue commented Sep 1, 2024

Static proc objects defined as methods may be placed in 4-byte alignments in 32-bit environments.
This may be misinterpreted as an immediate value depending on the address.

Since C11 and C++11 have additional language features for byte alignment, corresponding compilers use them to define the mrb_alignas() macro.
For earlier compilers, they use their own extensions to define the mrb_alignas() macro.

GCC supports __attribute__((aligned(alignment))) since at least version 2.95.3 (1999).
https://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#IDX305
According to GPT-4, support was added in version 2.7 (1995).

It is not known which version of Visual C++ added support for __declspec(align(n)).
According to GPT-4, at least Visual C++ 6.0 (1998) seems to support it.
Also, the documentation of past Intel C/C++ compilers that support __declspec(align(n)) makes reference to support with Visual C++ 4.2 (1996).
https://www.intel.com/content/dam/www/public/ijkk/jp/ja/documents/developer/ccomp40j.pdf


I noticed this problem when building mruby for i386 with clang (version 18.1.5) on FreeBSD 14.1 amd64.

Running bin/mrbtest crashes due to SIGSEGV.
I use gdb because lldb did not track 32-bit binaries well.

  • build configuration file

    MRuby::Lockfile.disable rescue nil
    
    MRuby::Build.new do
      toolchain "clang"
    end
    
    MRuby::Build.new("host32") do
      toolchain "clang"
      enable_debug
      enable_test
      enable_bintest
    
      [*compilers, linker].each do |bin|
        bin.flags << "-m32"
      end
    
      gem core: "mruby-eval"
    end
  • result with gdb

    % gdb --args build/host32/bin/mrbtest -v
    GNU gdb (GDB) 14.1 [GDB v14.1 for FreeBSD]
    Copyright (C) 2023 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-portbld-freebsd14.0".
    Type "show configuration" for configuration details.
    For bug reporting instructions, please see:
    <https://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 build/host32/bin/mrbtest...
    (gdb) r
    
    ...SNIP...
    
    Binding#eval : .
    Binding#local_variables : .
    Binding#local_variable_set : .
    Binding#local_variable_get : .
    Binding#eval with Binding.new via UnboundMethod :
    Program received signal SIGSEGV, Segmentation fault.
    Address not mapped to object.
    0x004b9b3f in mrb_check_type (mrb=0x20a13800, x=..., t=MRB_TT_PROC) at /var/tmp/mruby-56c3d74/src/object.c:404
    404         ename = RSTRING_PTR(mrb_obj_as_string(mrb, x));
    (gdb) p (void *)x.w
    $1 = (void *) 0x4579f4 <new_proc>

Static proc objects defined as methods may be placed in 4-byte alignments in 32-bit environments.
This may be misinterpreted as an immediate value depending on the address.

Since C11 and C++11 have additional language features for byte alignment, corresponding compilers use them to define the `mrb_alignas()` macro.
For earlier compilers, they use their own extensions to define the `mrb_alignas()` macro.

GCC supports `__attribute__((aligned(alignment)))` since at least version 2.95.3 (1999).
https://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#IDX305
According to GPT-4, support was added in version 2.7 (1995).

It is not known which version of Visual C++ added support for `__declspec(align(n))`.
According to GPT-4, at least Visual C++ 6.0 (1998) seems to support it.
Also, the documentation of past Intel C/C++ compilers that support `__declspec(align(n))` makes reference to support with Visual C++ 4.2 (1996).
https://www.intel.com/content/dam/www/public/ijkk/jp/ja/documents/developer/ccomp40j.pdf
@dearblue
Copy link
Contributor Author

I had the same problem when I tested 32-bit binaries built with mingw32 on FreeBSD with wine.
Applying this patch fixed the problem.

@matz matz merged commit 704ad87 into mruby:master Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants