13. jsc.exe works! - WebKit porting to Mona OS


Previously on porting Webkit to Mona, jsc.exe is killed. After a long long printf debug, I concluded that malloc(dlmalloc) on Mona seems to have a bug. Since it re-allocates a region which is not free-ed yet. So two objects are allocated on same place!
Or JavaScriptCore corrupts memory structure used on malloc.


Although JavaScriptCore uses two memory allocation base. One is malloc/new, the other is virtual memory (reserve/commit), the first implementation for Mona used malloc only. I thought it's safe to emulate virual memory using malloc, but it made debugging GC harder.
So rewrote OSAllocater using SharedMemory. And it works :D

Here is a whole diff.

diff --git a/Source/JavaScriptCore/bytecode/SamplingTool.cpp b/Source/JavaScriptCore/bytecode/SamplingTool.cpp
index 8a867f0..de50999 100644
--- a/Source/JavaScriptCore/bytecode/SamplingTool.cpp
+++ b/Source/JavaScriptCore/bytecode/SamplingTool.cpp
@@ -101,6 +101,13 @@ static void sleepForMicroseconds(unsigned us)
     Sleep(ms);
 }
 
+#elif OS(MONA)
+
+static void sleepForMicroseconds(unsigned us)
+{
+    sleep(1);
+}
+
 #else 
 
 static void sleepForMicroseconds(unsigned us)
diff --git a/Source/JavaScriptCore/wtf/Atomics.h b/Source/JavaScriptCore/wtf/Atomics.h
index 76b7c72..21e69dd 100644
--- a/Source/JavaScriptCore/wtf/Atomics.h
+++ b/Source/JavaScriptCore/wtf/Atomics.h
@@ -69,6 +69,7 @@
 #include <atomic.h>
 #elif OS(ANDROID)
 #include <sys/atomics.h>
+#elif defined(MONA)
 #elif COMPILER(GCC) && !OS(SYMBIAN)
 #if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2))
 #include <ext/atomicity.h>
@@ -108,6 +109,11 @@ inline int atomicDecrement(int volatile* addend) { return static_cast<int>(atomi
 inline int atomicIncrement(int volatile* addend) { return __atomic_inc(addend); }
 inline int atomicDecrement(int volatile* addend) { return __atomic_dec(addend); }
 
+#elif defined(MONA)
+#include <assert.h>
+inline int atomicIncrement(int volatile* addend) { assert(0); return -1; }
+inline int atomicDecrement(int volatile* addend) { assert(0); return -1; }
+
 #elif COMPILER(GCC) && !CPU(SPARC64) && !OS(SYMBIAN) // sizeof(_Atomic_word) != sizeof(int) on sparc64 gcc
 #define WTF_USE_LOCKFREE_THREADSAFEREFCOUNTED 1
 
diff --git a/Source/JavaScriptCore/wtf/MathExtras.h b/Source/JavaScriptCore/wtf/MathExtras.h
index 76ff970..eb9124d 100644
--- a/Source/JavaScriptCore/wtf/MathExtras.h
+++ b/Source/JavaScriptCore/wtf/MathExtras.h
@@ -254,7 +254,7 @@ inline int clampToInteger(unsigned x)
     return static_cast<int>(x);
 }
 
-#if !COMPILER(MSVC) && !(COMPILER(RVCT) && PLATFORM(BREWMP)) && !OS(SOLARIS) && !OS(SYMBIAN)
+#if !COMPILER(MSVC) && !(COMPILER(RVCT) && PLATFORM(BREWMP)) && !OS(SOLARIS) && !OS(SYMBIAN) && !OS(MONA)
 using std::isfinite;
 using std::isinf;
 using std::isnan;
diff --git a/Source/JavaScriptCore/wtf/OSAllocatorMona.cpp b/Source/JavaScriptCore/wtf/OSAllocatorMona.cpp
new file mode 100644
index 0000000..a90a57e
--- /dev/null
+++ b/Source/JavaScriptCore/wtf/OSAllocatorMona.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2010 Apple Inc. All rights reserved.
+ * Copyright (C) 2011 Higepon.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "OSAllocator.h"
+#include <map>
+#include <monapi.h>
+
+#include <wtf/Assertions.h>
+
+namespace WTF {
+
+static void* vmAlloc(size_t bytes) {
+	MonAPI::SharedMemory* shm = new MonAPI::SharedMemory(bytes);
+	if (shm->map() != M_OK) {
+		monapi_fatal("map failed");
+	}
+	return shm->data();
+}
+
+void* OSAllocator::reserveUncommitted(size_t bytes, Usage, bool writable, bool executable, bool)
+{
+	return vmAlloc(bytes);
+}
+
+void* OSAllocator::reserveAndCommit(size_t bytes, Usage, bool writable, bool executable, bool)
+{
+	return vmAlloc(bytes);
+}
+
+void OSAllocator::commit(void* address, size_t bytes, bool writable, bool executable)
+{
+}
+
+void OSAllocator::decommit(void* address, size_t bytes)
+{
+}
+
+void OSAllocator::releaseDecommitted(void* address, size_t bytes)
+{
+}
+
+} // namespace WTF
diff --git a/Source/JavaScriptCore/wtf/PageBlock.cpp b/Source/JavaScriptCore/wtf/PageBlock.cpp
index 5a7f892..2f08f62 100644
--- a/Source/JavaScriptCore/wtf/PageBlock.cpp
+++ b/Source/JavaScriptCore/wtf/PageBlock.cpp
@@ -30,6 +30,10 @@
 #include <unistd.h>
 #endif
 
+#if OS(MONA)
+#include <sys/types.h>
+#endif
+
 #if OS(WINDOWS)
 #include <malloc.h>
 #include <windows.h>
@@ -80,6 +84,13 @@ inline size_t systemPageSize()
 #endif
 }
 
+#elif OS(MONA)
+
+inline size_t systemPageSize()
+{
+  return (size_t)(MAP_PAGE_SIZE);
+}
+
 #endif
 
 size_t pageSize()
diff --git a/Source/JavaScriptCore/wtf/Platform.h b/Source/JavaScriptCore/wtf/Platform.h
index d7a77c1..0f48b29 100644
--- a/Source/JavaScriptCore/wtf/Platform.h
+++ b/Source/JavaScriptCore/wtf/Platform.h
@@ -695,7 +695,7 @@
 
 #if !OS(WINDOWS) && !OS(SOLARIS) && !OS(QNX) \
     && !OS(SYMBIAN) && !OS(HAIKU) && !OS(RVCT) \
-    && !OS(ANDROID) && !PLATFORM(BREWMP)
+  && !OS(ANDROID) && !PLATFORM(BREWMP) && !OS(MONA)
 #define HAVE_TM_GMTOFF 1
 #define HAVE_TM_ZONE 1
 #define HAVE_TIMEGM 1
@@ -784,13 +784,15 @@
 
 #define HAVE_ERRNO_H 1
 /* As long as Haiku doesn't have a complete support of locale this will be disabled. */
-#if !OS(HAIKU)
+#if !OS(HAIKU) && !OS(MONA)
 #define HAVE_LANGINFO_H 1
 #endif
 #define HAVE_MMAP 1
 #define HAVE_SBRK 1
 #define HAVE_STRINGS_H 1
+#if !OS(MONA)
 #define HAVE_SYS_PARAM_H 1
+#endif
 #define HAVE_SYS_TIME_H 1
 
 #endif
diff --git a/Source/JavaScriptCore/wtf/StackBounds.cpp b/Source/JavaScriptCore/wtf/StackBounds.cpp
index aa3a9b4..cb7f182 100644
--- a/Source/JavaScriptCore/wtf/StackBounds.cpp
+++ b/Source/JavaScriptCore/wtf/StackBounds.cpp
@@ -153,6 +153,15 @@ void StackBounds::initialize()
     m_bound = estimateStackBound(m_origin);
 }
 
+#elif OS(MONA)
+
+void StackBounds::initialize()
+{
+  // TODO: MONA thread
+    m_origin = (void*)0xF0000000;
+    m_bound = (void*)(reinterpret_cast<uint32_t>(m_origin) - 4 * 1024 * 1024);;
+}
+
 #elif OS(UNIX)
 
 void StackBounds::initialize()