From 44460e84c014ddb024a31dfd160aef871299cbad Mon Sep 17 00:00:00 2001
From: Robo <hop2deep@gmail.com>
Date: Tue, 9 Feb 2021 09:21:49 -0800
Subject: [PATCH] chore: cherry-pick 0c8b6e41 from v8 (#27672)

* chore: cherry-pick 0c8b6e41 from v8

Backports https://chromium-review.googlesource.com/c/v8/v8/+/2679688

* update patches

Co-authored-by: Electron Bot <electron@github.com>
---
 patches/v8/.patches                           |  1 +
 ...cos_11_2_code_page_decommit_failures.patch | 42 +++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 patches/v8/mac_wasm_work_around_macos_11_2_code_page_decommit_failures.patch

diff --git a/patches/v8/.patches b/patches/v8/.patches
index c1cd326e70b7..ad912cca6640 100644
--- a/patches/v8/.patches
+++ b/patches/v8/.patches
@@ -8,3 +8,4 @@ do_not_export_private_v8_symbols_on_windows.patch
 revert_cleanup_switch_offset_of_to_offsetof_where_possible.patch
 fix_build_deprecated_attirbute_for_older_msvc_versions.patch
 chore_disallow_copying_cppheapcreateparams.patch
+mac_wasm_work_around_macos_11_2_code_page_decommit_failures.patch
diff --git a/patches/v8/mac_wasm_work_around_macos_11_2_code_page_decommit_failures.patch b/patches/v8/mac_wasm_work_around_macos_11_2_code_page_decommit_failures.patch
new file mode 100644
index 000000000000..7758aa2fa86c
--- /dev/null
+++ b/patches/v8/mac_wasm_work_around_macos_11_2_code_page_decommit_failures.patch
@@ -0,0 +1,42 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: deepak1556 <hop2deep@gmail.com>
+Date: Mon, 8 Feb 2021 13:20:09 -0800
+Subject: Work around MacOS 11.2 code page decommit failures
+
+MacOS 11.2 refuses to set "no access" permissions on memory that
+we previously used for JIT-compiled code. It is still unclear
+whether this is WAI on the part of the kernel. In the meantime,
+as a workaround, we use madvise(..., MADV_FREE_REUSABLE) instead
+of mprotect(..., NONE) when discarding code pages. This is inspired
+by what Chromium's gin platform does.
+
+Fixed: v8:11389
+Change-Id: I866586932573b4253002436ae5eee4e0411c45fc
+Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2679688
+Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
+Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
+Auto-Submit: Jakob Kummerow <jkummerow@chromium.org>
+Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
+Cr-Commit-Position: refs/heads/master@{#72559}
+
+diff --git a/src/base/platform/platform-posix.cc b/src/base/platform/platform-posix.cc
+index ab0d7839a453d87a8404ca791c1022871e8fd606..67746173d582c5c404083be8ef3fa36c7dfc8cfe 100644
+--- a/src/base/platform/platform-posix.cc
++++ b/src/base/platform/platform-posix.cc
+@@ -415,6 +415,16 @@ bool OS::SetPermissions(void* address, size_t size, MemoryPermission access) {
+ 
+   int prot = GetProtectionFromMemoryPermission(access);
+   int ret = mprotect(address, size, prot);
++
++  // MacOS 11.2 on Apple Silicon refuses to switch permissions from
++  // rwx to none. Just use madvise instead.
++#if defined(V8_OS_MACOSX)
++  if (ret != 0 && access == OS::MemoryPermission::kNoAccess) {
++    ret = madvise(address, size, MADV_FREE_REUSABLE);
++    return ret == 0;
++  }
++#endif
++
+   if (ret == 0 && access == OS::MemoryPermission::kNoAccess) {
+     // This is advisory; ignore errors and continue execution.
+     USE(DiscardSystemPages(address, size));