Skip to content

Commit 0257caa

Browse files
author
Gerard Ziemski
committed
8261397: Try Catch Method Failing to Work When Dividing An Integer By 0
Reviewed-by: stuefe, prr, dcubed, dholmes
1 parent 8a2f589 commit 0257caa

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/hotspot/cpu/x86/vm_version_x86.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,11 @@ enum Extended_Family {
10631063
static bool supports_clflushopt() { return ((_features & CPU_FLUSHOPT) != 0); }
10641064
static bool supports_clwb() { return ((_features & CPU_CLWB) != 0); }
10651065

1066+
#ifdef __APPLE__
1067+
// Is the CPU running emulated (for example macOS Rosetta running x86_64 code on M1 ARM (aarch64)
1068+
static bool is_cpu_emulated();
1069+
#endif
1070+
10661071
// support functions for virtualization detection
10671072
private:
10681073
static void check_virtualizations();

src/hotspot/os_cpu/bsd_x86/os_bsd_x86.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,10 @@ bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,
454454

455455
#ifdef AMD64
456456
if (sig == SIGFPE &&
457-
(info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV)) {
457+
(info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV
458+
// Workaround for macOS ARM incorrectly reporting FPE_FLTINV for "div by 0"
459+
// instead of the expected FPE_FLTDIV when running x86_64 binary under Rosetta emulation
460+
MACOS_ONLY(|| (VM_Version::is_cpu_emulated() && info->si_code == FPE_FLTINV)))) {
458461
stub =
459462
SharedRuntime::
460463
continuation_for_implicit_exception(thread,

src/hotspot/os_cpu/bsd_x86/vm_version_bsd_x86.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,24 @@
2525
#include "precompiled.hpp"
2626
#include "runtime/os.hpp"
2727
#include "runtime/vm_version.hpp"
28+
29+
#ifdef __APPLE__
30+
31+
#include <sys/types.h>
32+
#include <sys/sysctl.h>
33+
34+
bool VM_Version::is_cpu_emulated() {
35+
int ret = 0;
36+
size_t size = sizeof(ret);
37+
// Is this process being ran in Rosetta (i.e. emulation) mode on macOS?
38+
if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1) {
39+
// errno == ENOENT is a valid response, but anything else is a real error
40+
if (errno != ENOENT) {
41+
warning("unable to lookup sysctl.proc_translated");
42+
}
43+
}
44+
return (ret==1);
45+
}
46+
47+
#endif
48+

0 commit comments

Comments
 (0)