Commit graph

638 commits

Author SHA1 Message Date
Ivo Anjo
64fef3b870 Add explicit compiler fence when pushing frames to ensure safe profiling
**What does this PR do?**

This PR tweaks the `vm_push_frame` function to add an explicit compiler
fence (`atomic_signal_fence`) to ensure profilers that use signals
to interrupt applications (stackprof, vernier, pf2, Datadog profiler)
can safely sample from the signal handler.

**Motivation:**

The `vm_push_frame` was specifically tweaked in
https://github.com/ruby/ruby/pull/3296 to initialize the a frame
before updating the `cfp` pointer.

But since there's nothing stopping the compiler from reordering
the initialization of a frame (`*cfp =`) with the update of the cfp
pointer (`ec->cfp = cfp`) we've been hesitant to rely on this on
the Datadog profiler.

In practice, after some experimentation + talking to folks, this
reordering does not seem to happen.

But since modern compilers have a way for us to exactly tell them
not to do the reordering (`atomic_signal_fence`), this seems even
better.

I've actually extracted `vm_push_frame` into the "Compiler Explorer"
website, which you can use to see the assembly output of this function
across many compilers and architectures: https://godbolt.org/z/3oxd1446K

On that link you can observe two things across many compilers:
1. The compilers are not reordering the writes
2. The barrier does not change the generated assembly output
   (== has no cost in practice)

**Additional Notes:**

The checks added in `configure.ac` define two new macros:
* `HAVE_STDATOMIC_H`
* `HAVE_DECL_ATOMIC_SIGNAL_FENCE`

Since Ruby generates an arch-specific `config.h` header with
these macros upon installation, this can be used by profilers
and other libraries to test if Ruby was compiled with the fence enabled.

**How to test the change?**

As I mentioned above, you can check https://godbolt.org/z/3oxd1446K
to confirm the compiled output of `vm_push_frame` does not change
in most compilers (at least all that I've checked on that site).
2024-07-03 18:08:57 +09:00
Nobuyoshi Nakada
d122a68863
Exported symbols in DLL on Windows are managed by win32/mkexports.rb 2024-06-22 18:48:14 +09:00
Yuta Saito
73b7eebf07 build: fix crossruby build by handling empty ac_abs_builddir
`ac_abs_builddir` can be empty when the build is top-level (not
subdirs, and Ruby is usually the case). In such case, the MINIRUBY is
expanded to `$RUBY -I -r'$(arch)-fake'`, which interprets `-r$(arch)-fake`
as an argument to `-I` option. This led to:
- Not loading the fake config file
- Then not setting `CROSS_COMPILING` constant during extmk.rb execution
- Then misusing cross-compiled `./miniruby` instead of baseruby to generate
  files used in exts.

This commit fixes the issue by handling the empty `ac_abs_builddir` case
properly.
2024-06-22 15:12:00 +09:00
Nobuyoshi Nakada
cab0d03037
Get rid of nesting backquotes
Also executing variable containing an option may not be portable.
Follow up of dd378c5a24.
2024-06-22 10:58:32 +09:00
Nobuyoshi Nakada
dd378c5a24
Expand --with-opt-dir arguments 2024-06-22 02:16:16 +09:00
Yusuke Endoh
ac9e84df3d Support LCOV 2.0
LCOV 2.0, a GCOV frontend, seems to have stricter error checking
2024-06-21 14:48:44 +09:00
ydah
2eb31a6254 Fix a typo
s/sepcifier/specifier/
2024-06-12 15:45:06 +09:00
KJ Tsanaktsidis
0ccb80d6bf Extract hardening CFLAGS to a special $hardenflags variable
This changes the automatic detection of -fstack-protector,
-D_FORTIFY_SOURCE, and -mbranch-protection to write to $hardenflags
instead of $XCFLAGS. The definition of $cflags is changed to
"$hardenflags $orig_cflags $optflags $debugflags $warnflags" to match.

Furthermore, these flags are _prepended_ to $hardenflags, rather than
appended.

The implications of doing this are as follows:

* If a CRuby builder specifies cflags="-mbranch-protection=foobar" at
  the ./configure script, and the configure script detects that
  -mbranch-protection=pac-ret is accepted, then GCC will be invoked as
  "gcc -mbranch-protection=pac-ret -mbranch-protection=foobar". Since
  the last flags take precedence, that means that user-supplied values
  of these flags in $cflags will take priority.
* Likewise, if a CRuby builder explicitly specifies
  "hardenflags=-mbranch-protection=foobar", because we _prepend_ to
  $hardenflags in our autoconf script, we will still invoke GCC as
  "gcc -mbranch-protection=pac-ret -mbranch-protection=foobar".
* If a CRuby builder specifies CFLAGS="..." at the configure line,
  automatic detection of hardening flags is ignored as before.
* C extensions will _also_ be built with hardening flags now as well
  (this was not the case by default before because the detected flags
  went into $XCFLAGS).

Additionally, as part of this work, I changed how the detection of
PAC/BTI in Context.S works. Rather than appending the autodetected
option to ASFLAGS, we simply compile a set of test programs with the
actual CFLAGS in use to determine what PAC/BTI settings were actually
chosen by the builder. Context.S is made aware of these choices through
some custom macros.

The result of this work is that:

* Ruby will continue to choose some sensible defaults for hardening
  options for the C compiler
* Distributors are able to specify CFLAGS that are consistent with their
  distribution and override these defaults
* Context.S will react to whatever -mbranch-protection is actually in
  use, not what was autodetected
* Extensions get built with hardening flags too.

[Bug #20154]
[Bug #20520]
2024-06-11 20:48:55 +10:00
Nobuyoshi Nakada
906a86e4de
Use dllexport as RUBY_FUNC_EXPORTED on Windows 2024-06-09 16:55:27 +09:00
Jeremy Evans
029d92b898 Disable __builtin_setjmp usage on linux-aarch64
It is questionable whether __builtin_setjmp should default to yes
at all, but since it appears to still have problems on this platform,
it seems safest to disable it.

Fixes [Bug #14480]
2024-06-06 15:46:41 -07:00
Nobuyoshi Nakada
5308da5e1c
Add dependencies of configure.ac 2024-06-01 14:21:45 +09:00
Nobuyoshi Nakada
a41e6f3873
GCC LD does not support .debug_macinfo yet
Lower debug info level if it is warned, not checks with werror_flag to
fail due to this warning.
2024-05-29 17:40:59 +09:00
Nobuyoshi Nakada
5fa6ba9568 [Bug #20500] Search non-default directories for jemalloc
Co-Authored-by: lish82 (Hiroki Katagiri)
2024-05-23 13:16:48 +09:00
Nobuyoshi Nakada
1e08a9f0e9 [Bug #20499] Use Xcode owned tools for Xcode clang
Xcode has its own version tools that may be incompatible with genuine
LLVM tools, use the tools in the same directory.
2024-05-22 13:49:05 +09:00
Hiroshi SHIBATA
f8e6752219
Revert "[Bug #20499] Use consistent version tools with CC"
This reverts commit 8277cf0799.

This change break to build with `rbenv install ruby-dev` with the following error.

```
touch yjit/target/release/libyjit.a
transdb.h updated
./tool/darwin-ar: line 6: /nm: No such file or directory
./tool/darwin-ar: line 6: exec: /nm: cannot execute: No such file or directory
partial linking yjit/target/release/libyjit.a into yjit/target/release/libyjit.o
```
2024-05-22 10:45:02 +09:00
Nobuyoshi Nakada
8277cf0799
[Bug #20499] Use consistent version tools with CC
As Apple Xcode is relocatable and selectable with `xcode-select`, use
consistent versions of commands in the same location.
2024-05-21 17:03:34 +09:00
Nobuyoshi Nakada
fa26ef5bf7 Fix the end of "compiler section" 2024-05-20 13:54:08 +09:00
Nobuyoshi Nakada
18eaf0be90 [Bug #20494] Search non-default directories for GMP
Co-Authored-by: lish82 (Hiroki Katagiri)
2024-05-20 13:54:08 +09:00
卜部昌平
bb5a538207 use of stdckdint.h
C23 is going to have this header.  The industry is already moving
towards accepting it; OSes and compilers started to implement theirs.

Why not detect its presence and if any, prefer over other ways.

See also:

- https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2683.pdf
- https://reviews.freebsd.org/D41734
- https://reviews.llvm.org/D157331
- https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=8441841a1b985d68245954af1ff023db121b0635
2024-04-27 21:55:28 +09:00
Nobuyoshi Nakada
7951b349ab
Suppress useless linker warnings totally on macOS 2024-04-19 16:33:16 +09:00
Matt Valentine-House
a2ea4ec30c Add --with-shared-gc build flag 2024-04-15 19:50:47 +01:00
Nobuyoshi Nakada
66bfcba587 Not all nms support the --help option 2024-04-16 01:57:22 +09:00
Nobuyoshi Nakada
3ac6a03b2e
Revert "hijack SIGCHLD handler for internal use"
This reverts commit 054a412d54.
SIGCHLD `waidpid`, `waitpid_lock` and related code, have been removed
at ruby/ruby#7527.
2024-04-04 21:48:14 +09:00
Matt Valentine-House
5903fdf43e
[DOC] Fix wheather -> whether typos in configure. 2024-04-02 21:53:56 +09:00
Nobuyoshi Nakada
376ae22235
Manage required baseruby version in one place
Add a Ruby script mode to `tool/missing-baseruby.bat` that checks if
`RUBY_VERSION` meets the required version.  This will enable similar
checks on mswin as well.
2024-03-31 00:54:38 +09:00
Nobuyoshi Nakada
f697d3242f
Manage required baseruby version in one place 2024-03-30 18:34:45 +09:00
Nobuyoshi Nakada
0fb39ab1b9
Clean intermediate files and debug info for each target
By replacing `ALLOBJS` suffix with intermediate file suffixes instead
of roughly removing by wildcards.  Made `cleanlibs` append `.dSYM`
suffix for each word in `TARGET_SO`, not the end of the entire list.
2024-03-10 22:12:00 +09:00
Nobuyoshi Nakada
b543a4e9da
Remove -f option for codesign
Recent `codesign` seems to emit a "replacing existing signature"
warning even if it was an adhoc signature added by the linker.  Since
"adhoc" signatures do not cause a failure when replaced, it is just
unnecessary and noisy.
2024-03-06 20:17:44 +09:00
Yuta Saito
b000e7bb74 configure.ac: Append POSTLINK to LINK_SO for all platforms
The `POSTLINK` variable had been used only for darwin platforms, but
it's now used for WebAssembly/WASI as well. To make shared extension
libraries properly post-processed, we need to append `POSTLINK` to
`LINK_SO` but it was done only for darwin platforms. This commit
generalizes the appending to all platforms.
2024-03-02 17:07:37 +09:00
Yuta Saito
6e2880b388 [wasm-pic] Add LDSHARED definition for WASI platform
We are going to add dynamic linking support for WASI platform. The
`LDSHARED` definition is used to link shared libraries for building ruby
binaries and extensions.
2024-03-01 03:16:59 +09:00
Yuta Saito
3f633e57ac [wasm-pic] Remove --pass-arg=asyncify-ignore-imports from POSTLINK
Before PIC era, we could assume that the stack is not unwound by
imported functions since all imported functions are WASI syscalls and
they don't use Asyncify at all. However, PIC binary can import functions
from other modules and we cannot guarantee that they won't unwind
the stack.
2024-02-29 19:57:49 +09:00
Nobuyoshi Nakada
87c4c6c082
Install binary executable files to architecture dependent path 2024-02-18 15:07:42 +09:00
Takashi Kokubun
bc7266c5ce
Bump the required BASERUBY version to 3.0 (#9976) 2024-02-15 23:13:45 -08:00
Takashi Kokubun
b4ed5b7dfe
Bump the required BASERUBY version to 2.7 (#9566)
[[Misc #16671]](https://bugs.ruby-lang.org/issues/16671)
2024-01-16 15:54:17 -08:00
Yuta Saito
02973b78f4 [Bug #20085] Use consistent default options for -mbranch-protection
We need to use the same options for both C compiler and assembler
when `-mbranch-protection` is guessed by configure. Otherwise,
`coroutine/arm64/Context.{h,S}` will use incompatible PAC strategies.
2023-12-28 12:20:50 +09:00
Nobuyoshi Nakada
2a4a84664a [Bug #20088] Fix ARCH_FLAG for cross compiling 2023-12-27 19:11:54 +09:00
JP Camara
8782e02138 KQueue support for M:N threads
* Allows macOS users to use M:N threads (and technically FreeBSD, though it has not been verified on FreeBSD)

* Include sys/event.h header check for macros, and include sys/event.h when present

* Rename epoll_fd to more generic kq_fd (Kernel event Queue) for use by both epoll and kqueue

* MAP_STACK is not available on macOS so conditionall apply it to mmap flags

* Set fd to close on exec

* Log debug messages specific to kqueue and epoll on creation

* close_invalidate raises an error for the kqueue fd on child process fork. It's unclear rn if that's a bug, or if it's kqueue specific behavior

Use kq with rb_thread_wait_for_single_fd

* Only platforms with `USE_POLL` (linux) had changes applied to take advantage of kernel event queues. It needed to be applied to the `select` so that kqueue could be properly applied

* Clean up kqueue specific code and make sure only flags that were actually set are removed (or an error is raised)

* Also handle kevent specific errnos, since most don't apply from epoll to kqueue

* Use the more platform standard close-on-exec approach of `fcntl` and `FD_CLOEXEC`. The io-event gem uses `ioctl`, but fcntl seems to be the recommended choice. It is also what Go, Bun, and Libuv use

* We're making changes in this file anyways - may as well fix a couple spelling mistakes while here

Make sure FD_CLOEXEC carries over in dup

* Otherwise the kqueue descriptor should have FD_CLOEXEC, but doesn't and fails in assert_close_on_exec
2023-12-20 16:23:38 +09:00
Jeremy Evans
7dca6b53a9
Add tool/missing-baseruby.bat, used when BASERUBY not available
Previously, the embedded semicolon in BASERUBY if BASERUBY is
not available breaks tarball builds without BASERUBY when using
OpenBSD make, due to the inability to escape MFLAGS correctly.
This moves the same BASERUBY code into a separate file, avoiding
the MFLAGS quoting issue.

BASERUBY must be passed to build-ext because it is required
by ripper since the introduction of lrama.

Fixes [Bug #19683]

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
2023-12-19 21:30:47 -08:00
Takashi Kokubun
eb872d1752 RJIT: Share rb_vm_insns_count for vm_insns_count 2023-12-18 23:55:40 -08:00
Nobuyoshi Nakada
2c2c6bc3fa
Fix ARCH_FLAG when cross-compiling on macOS 2023-12-15 15:19:16 +09:00
Takashi Kokubun
0f1c7e3bcb
RJIT: Just skip generating code for aarch64/arm64 (#9221) 2023-12-13 09:36:06 -08:00
Nobuyoshi Nakada
75f4a687ed
Fix a typo for RJIT_TARGET_OK (#8222) 2023-12-12 23:28:22 -08:00
Nobuyoshi Nakada
1cba1b3992
Check windres message using the found windres only if needed 2023-11-24 20:00:13 +09:00
Pierrick Bouvier
784fdecc4c
windows-arm64 support (#8995)
* [win32] fix compilation for windows-arm64

Credits to MSYS2 Ruby package using this patch.

* [win32] nm use full options

Fix compilation error when using MSYS2 environment.
Credits to MSYS2 Ruby package using this patch.

* [win32] detect llvm-windres (used for windows-arm64)

When adding preprocessor option for llvm-windres (using clang as
parameter), it fails. Thus, do not add this.

It's needed to be able to compile windows-arm64 version, because MSYS2
toolchain is LLVM based (instead of GCC/binutils).

* [win32] pioinfo detection for windows-arm64

This fixes "unexpected ucrtbase.dll" for native windows-arm64 ruby
binary. It does not solve issue with x64 version emulated on this
platform.

Value of pioinfo pointer can be found in ucrtbase.dll at latest adrp/add
sequence before return of _isatty function. This works for both release
and debug ucrt.

Due to the nature of aarch64 ISA (vs x86 or x64), it's needed to
disassemble instructions to retrieve offset value, which is a bit more
complicated than matching specific string patterns.

Details about adrp/add usage can be found in this blog post:
https://devblogs.microsoft.com/oldnewthing/20220809-00/?p=106955
For instruction decoding, the Arm documentation was used as a reference.
2023-11-23 17:17:28 +09:00
Hiroshi SHIBATA
f100c6477a
[Bug #20004] Revert "[Feature #19422] Enable shared by default on macOS"
This reverts commit 9694445051.

  This change broke our release CI.

  1792764457 (step):16:44

  Invoking `/Users/runner/work/actions/actions/snapshot-master/ruby -rrubygems /Users/runner/work/actions/actions/snapshot-master/bin/gem --backtrace build lib/bundler/bundler.gemspec` failed with output:
  ----------------------------------------------------------------------
  dyld[42417]: Library not loaded: '/usr/local/lib/libruby.3.3.dylib'
    Referenced from: '/Users/runner/work/actions/actions/snapshot-master/ruby'
    Reason: tried: '/usr/local/lib/libruby.3.3.dylib' (no such file), '/usr/lib/libruby.3.3.dylib' (no such file)
  ----------------------------------------------------------------------
2023-11-21 20:21:18 +09:00
Nobuyoshi Nakada
22939382a8 [Bug #18286] Make builtin binary if sharable in universal binaries 2023-11-09 16:01:01 +09:00
Nobuyoshi Nakada
40d40a651e Revert "Disable iseq-dumped builtin module for universal x86_64/arm64 binaries"
This reverts commit 1d5598fe0d.
2023-11-09 16:01:01 +09:00
Ben Hamilton
1d5598fe0d Disable iseq-dumped builtin module for universal x86_64/arm64 binaries
During the build, Ruby has special logic to serialize its own builtin
module to disk using the binary iseq format during the build (I assume
for speed so it doesn't have to parse builtin every time it starts
up).

However, since iseq format is architecture-specific, when building on
x86_64 for universal x86_64 + arm64, the serialized builtin module is
written with the x86_64 architecture of the build machine, which fails
this check whenever ruby imports the builtin module on arm64:

1fdaa06660/compile.c (L13243)

Thankfully, there's logic to disable this feature for cross-compiled builds:

1fdaa06660/builtin.c (L6)

This disables the iseq logic for universal builds as well.

Fixes [Bug #18286]
2023-11-09 12:24:01 +09:00
Nobuyoshi Nakada
6031fdc632
Select proper dsymutil for gcc 13 2023-11-07 23:19:51 +09:00
Nobuyoshi Nakada
f8456b650b
Ignore duplicate libraries warnings from gcc 13 2023-11-07 23:19:50 +09:00