From b4ac64edc4b135f40648327504e47f12067a1235 Mon Sep 17 00:00:00 2001
From: "shravanrn@gmail.com" <shravanrn@gmail.com>
Date: Thu, 9 Sep 2021 07:40:38 +0000
Subject: [PATCH] Bug 1729232 - Update wasm2c rt to ensure correct hunspell
 timeout r=glandium

Differential Revision: https://phabricator.services.mozilla.com/D125025
---
 config/external/wasm2c_sandbox_compiler/moz.yaml |  4 ++--
 third_party/wasm2c/wasm2c/wasm-rt-impl.c         |  2 +-
 third_party/wasm2c/wasm2c/wasm-rt-os-unix.c      |  4 ++++
 third_party/wasm2c/wasm2c/wasm-rt-os-win.c       | 16 ++++++++++++++++
 third_party/wasm2c/wasm2c/wasm-rt-os.h           |  3 +++
 third_party/wasm2c/wasm2c/wasm-rt-wasi.c         |  4 ++--
 6 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/config/external/wasm2c_sandbox_compiler/moz.yaml b/config/external/wasm2c_sandbox_compiler/moz.yaml
index 08d82ccb44176..b5152260a7fb5 100644
--- a/config/external/wasm2c_sandbox_compiler/moz.yaml
+++ b/config/external/wasm2c_sandbox_compiler/moz.yaml
@@ -9,8 +9,8 @@ origin:
   description: wasm2c fork used for rlbox sandboxing
   url: https://github.com/PLSysSec/wasm2c_sandbox_compiler
 
-  release: commit e78e291be785e7f6bf442ba16127e4cb666f8882 (2021-09-02T04:47:14Z).
-  revision: e78e291be785e7f6bf442ba16127e4cb666f8882
+  release: commit 551fd0e7d8f0349562d4402bc626a689e7ecde4f (2021-09-09T00:19:32Z).
+  revision: 551fd0e7d8f0349562d4402bc626a689e7ecde4f
 
   license: Apache-2.0
   license-file: LICENSE
diff --git a/third_party/wasm2c/wasm2c/wasm-rt-impl.c b/third_party/wasm2c/wasm2c/wasm-rt-impl.c
index 42173c0f246f3..2be22f2c8af03 100644
--- a/third_party/wasm2c/wasm2c/wasm-rt-impl.c
+++ b/third_party/wasm2c/wasm2c/wasm-rt-impl.c
@@ -134,7 +134,7 @@ void wasm_rt_allocate_memory(wasm_rt_memory_t* memory,
   }
 
   if (!addr) {
-    perror("mmap failed");
+    os_print_last_error("os_mmap failed.");
     abort();
   }
   int ret = os_mmap_commit(addr, byte_length, MMAP_PROT_READ | MMAP_PROT_WRITE);
diff --git a/third_party/wasm2c/wasm2c/wasm-rt-os-unix.c b/third_party/wasm2c/wasm2c/wasm-rt-os-unix.c
index 52b3ca30ef05b..39749a39beb75 100644
--- a/third_party/wasm2c/wasm2c/wasm-rt-os-unix.c
+++ b/third_party/wasm2c/wasm2c/wasm-rt-os-unix.c
@@ -268,6 +268,10 @@ int os_clock_getres(void* clock_data, int clock_id, struct timespec* out_struct)
   return ret;
 }
 
+void os_print_last_error(const char* msg) {
+  perror(msg);
+}
+
 #undef VERBOSE_LOG
 
 #else
diff --git a/third_party/wasm2c/wasm2c/wasm-rt-os-win.c b/third_party/wasm2c/wasm2c/wasm-rt-os-win.c
index 53d847b4976fe..6c733d3f94bc1 100644
--- a/third_party/wasm2c/wasm2c/wasm-rt-os-win.c
+++ b/third_party/wasm2c/wasm2c/wasm-rt-os-win.c
@@ -276,6 +276,22 @@ int os_clock_getres(void* clock_data, int clock_id, struct timespec* out_struct)
   return 0;
 }
 
+void os_print_last_error(const char* msg) {
+  DWORD errorMessageID  = GetLastError();
+  if (errorMessageID != 0) {
+    LPSTR messageBuffer = 0;
+    //The api creates the buffer that holds the message
+    size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+                                NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
+    (void) size;
+    //Copy the error message into a std::string.
+    printf("%s. %s\n", msg, messageBuffer);
+    LocalFree(messageBuffer);
+  } else {
+    printf("%s. No error code.\n", msg);
+  }
+}
+
 #undef VERBOSE_LOG
 #undef DONT_USE_VIRTUAL_ALLOC2
 
diff --git a/third_party/wasm2c/wasm2c/wasm-rt-os.h b/third_party/wasm2c/wasm2c/wasm-rt-os.h
index 45cf9995c5a04..ad36f3bbe0e99 100644
--- a/third_party/wasm2c/wasm2c/wasm-rt-os.h
+++ b/third_party/wasm2c/wasm2c/wasm-rt-os.h
@@ -43,4 +43,7 @@ void os_clock_cleanup(void** clock_data_pointer);
 int os_clock_gettime(void* clock_data, int clock_id, struct timespec* out_struct);
 int os_clock_getres(void* clock_data, int clock_id, struct timespec* out_struct);
 
+// print the error message
+void os_print_last_error(const char* msg);
+
 #endif
diff --git a/third_party/wasm2c/wasm2c/wasm-rt-wasi.c b/third_party/wasm2c/wasm2c/wasm-rt-wasi.c
index 698ff73b46bdf..d87499aea2f70 100644
--- a/third_party/wasm2c/wasm2c/wasm-rt-wasi.c
+++ b/third_party/wasm2c/wasm2c/wasm-rt-wasi.c
@@ -740,7 +740,7 @@ u32 Z_wasi_snapshot_preview1Z_clock_time_getZ_iiji(wasm_sandbox_wasi_data* wasi_
 
   struct timespec out_struct;
   int ret = os_clock_gettime(wasi_data->clock_data, clock_id, &out_struct);
-  u64 result = ((u64)out_struct.tv_sec)*1000000 + ((u64)out_struct.tv_nsec)/1000;
+  u64 result = ((u64)out_struct.tv_sec)*1000*1000*1000 + ((u64)out_struct.tv_nsec);
   wasm_i64_store(wasi_data->heap_memory, out, result);
   return ret;
 }
@@ -752,7 +752,7 @@ u32 Z_wasi_snapshot_preview1Z_clock_res_getZ_iii(wasm_sandbox_wasi_data* wasi_da
 
   struct timespec out_struct;
   int ret = os_clock_getres(wasi_data->clock_data, clock_id, &out_struct);
-  u64 result = ((u64)out_struct.tv_sec)*1000000 + ((u64)out_struct.tv_nsec)/1000;
+  u64 result = ((u64)out_struct.tv_sec)*1000*1000*1000 + ((u64)out_struct.tv_nsec);
   wasm_i64_store(wasi_data->heap_memory, out, result);
   return ret;
 }
-- 
GitLab