<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://www.mono-project.com/atom.xml" rel="self" type="application/atom+xml" /><link href="https://www.mono-project.com/" rel="alternate" type="text/html" /><updated>2024-12-20T11:26:14+00:00</updated><id>https://www.mono-project.com/atom.xml</id><title type="html">Mono Project</title><subtitle>Mono&apos;s web site.</subtitle><entry><title type="html">Native Library Loading in .NET 5</title><link href="https://www.mono-project.com/news/2020/08/24/native-loader-net5/" rel="alternate" type="text/html" title="Native Library Loading in .NET 5" /><published>2020-08-24T00:00:00+00:00</published><updated>2020-08-24T00:00:00+00:00</updated><id>https://www.mono-project.com/news/2020/08/24/native-loader-net5</id><content type="html" xml:base="https://www.mono-project.com/news/2020/08/24/native-loader-net5/"><![CDATA[<p>After years of work, Mono can now be built out of the <a href="https://github.com/dotnet/runtime">dotnet/runtime repository</a> in a .NET 5-compatible mode! This mode means numerous changes in the available APIs, managed and embedding, as well as internal runtime behavioral changes to better align Mono with CoreCLR and the .NET ecosystem.</p>

<p>One area with multiple highly impactful changes to the runtime internals is library loading. For managed assemblies, Mono now follows the algorithms outlined on <a href="https://docs.microsoft.com/en-us/dotnet/core/dependency-loading/loading-managed">this page</a>, which result from the removal of <code>AppDomain</code>s and the new <code>AssemblyLoadContext</code> APIs. The only exception to this is that Mono still supports bundles registered via the embedding API, and so the runtime will check that as part of the probing logic.</p>

<p>The managed loading changes are fairly clear and well documented, but unmanaged library loading has changed in numerous ways, some of them far more subtle.</p>

<h2 id="summary-of-changes">Summary of changes</h2>

<ul>
  <li><a href="https://docs.microsoft.com/en-us/dotnet/core/dependency-loading/loading-unmanaged">New P/Invoke resolution algorithm</a></li>
  <li>Dropped support for DllMap</li>
  <li>Unmanaged library loading defaults to <code>RTLD_LOCAL</code></li>
  <li>Added support for <code>DefaultDllImportSearchPathsAttribute</code></li>
  <li>On non-Windows platforms, Mono and CoreCLR no longer attempt to probe for A/W variants of symbols</li>
  <li>Default loader log level changed from INFO to DEBUG, and new log entries added for the new algorithm</li>
</ul>

<p>More detail where appropriate in the sections below.</p>

<h2 id="dropped-support-for-dllmap">Dropped support for DllMap</h2>

<p>The new unmanaged loading algorithm makes no mention of DllMap, as Mono has removed its functionality almost entirely in .NET 5. DllMap’s XML config files have have been disabled on every platform out of security concerns. The DllMap embedding APIs are also disabled on desktop platforms, though this may change.</p>

<p>In place of DllMap, users are encouraged to utilize the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.nativelibrary?view=netcore-3.1">NativeLibrary</a> resolution APIs, which are set in managed code, and the <a href="https://docs.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting#step-3---prepare-runtime-properties">runtime hosting properties</a>, which are set by embedders with the <code>monovm_initialize</code> function.</p>

<p>We recognize that this does not sufficiently cover some existing mono/mono scenarios. If the NativeLibrary APIs are insufficient for your use case, please <a href="https://github.com/dotnet/runtime/issues/new/choose">tell us about it</a>! We’re always looking to improve our interop functionality, and in particular with .NET 6 will be evaluating <code>NativeLibrary</code>, so community input would be greatly appreciated.</p>

<h2 id="unmanaged-library-loading-defaults-to-rtld_local">Unmanaged library loading defaults to <code>RTLD_LOCAL</code></h2>

<p>A more subtle, yet no less impactful change is that native library loading now defaults to <code>RTLD_LOCAL</code> to be consistent with CoreCLR and Windows, as opposed to our historical behavior of <code>RTLD_GLOBAL</code>. What this means in practice is that on Unix-like platforms, libraries are no longer loaded into a single global namespace and when looking up symbols, the library <em>must</em> be correctly specified. This change prevents symbol collision, and will both break and enable various scenarios and libraries. For more information on the difference, see <a href="https://linux.die.net/man/3/dlopen">the dlopen man page</a>.</p>

<p>For an example: historically in Mono on Linux, it was possible to load library <code>foo</code> containing symbol <code>bar</code>, and then invoke <code>bar</code> with a P/Invoke like so:</p>

<pre><code class="language-c#">// note the incorrect library name
[DllImport("asdf")]
public static extern int bar();
</code></pre>

<p>This will no longer work. For that P/Invoke to function correctly, the attribute would need to use the correct library name: <code>[DllImport("foo")]</code>. A lot of code in the wild that was using incorrect library names will need to be updated. However, this means that when loading two libraries containing the same symbol name, there is no longer a conflict.</p>

<p>There have been some embedding API changes as part of this. <code>MONO_DL_MASK</code> is no longer a full mask, as <code>MONO_DL_GLOBAL</code> has been introduced to specify <code>RTLD_GLOBAL</code>. If both <code>MONO_DL_LOCAL</code> and <code>MONO_DL_GLOBAL</code>, are set, Mono will use local. See mono/utils/mono-dl-fallback.h for more info.</p>

<p>This also means that dynamically linking libmonosgen and attempting to resolve Mono symbols from <code>dlopen(NULL, ...)</code> will no longer work. <code>__Internal</code> has been preserved as a Mono-specific extension, but its meaning has been expanded. When P/Invoking into <code>__Internal</code>, the runtime will check both <code>dlopen(NULL)</code> and the runtime library in the case that they differ, so that users attempting to call Mono APIs with <code>__Internal</code> will not have those calls break.</p>

<h2 id="added-support-for-defaultdllimportsearchpathsattribute">Added support for <code>DefaultDllImportSearchPathsAttribute</code></h2>

<p>Mono now supports the <code>DefaultDllImportSearchPathsAttribute</code> attribute, which can be found in <code>System.Runtime.InteropServices</code>. In particular, passing <code>DllImportSearchPath.AssemblyDirectory</code> is now required to have the loader search the executing assembly’s directory for native libraries, and the other Windows-specific loader options should be passed down when appropriate.</p>

<h2 id="fin">Fin</h2>

<p>And that’s it! If you have any further questions, feel free to ping us on <a href="https://aka.ms/dotnet-discord">Discord</a> or <a href="https://gitter.im/mono/mono">Gitter</a>.</p>]]></content><author><name>Ryan Lucia</name></author><category term="news" /><category term="loader" /><summary type="html"><![CDATA[After years of work, Mono can now be built out of the dotnet/runtime repository in a .NET 5-compatible mode! This mode means numerous changes in the available APIs, managed and embedding, as well as internal runtime behavioral changes to better align Mono with CoreCLR and the .NET ecosystem.]]></summary></entry><entry><title type="html">Plastic SCM: A Full Version Control Stack built with Mono</title><link href="https://www.mono-project.com/news/2019/02/13/plastic-scm-a-full-version-control-stack-built-with-mono/" rel="alternate" type="text/html" title="Plastic SCM: A Full Version Control Stack built with Mono" /><published>2019-02-13T00:00:00+00:00</published><updated>2019-02-13T00:00:00+00:00</updated><id>https://www.mono-project.com/news/2019/02/13/plastic-scm-a-full-version-control-stack-built-with-mono</id><content type="html" xml:base="https://www.mono-project.com/news/2019/02/13/plastic-scm-a-full-version-control-stack-built-with-mono/"><![CDATA[<p><em>Note: This is a guest post by Jordi Mon Companys from Códice Software, a long-time Mono user, about how they used Mono to develop their flagship product.</em></p>

<p><a href="https://www.plasticscm.com?utm_source=Mono%20Blog&amp;utm_medium=post&amp;utm_campaign=Mono%20success%20story&amp;utm_term=DotNet&amp;utm_content=Open%20Source">Plastic SCM</a>
is a full version control stack. This means Plastic SCM
comprises a full repo management core, command line (until here it would
be the equivalent to bare Git), native GUIs on Linux, macOS and Windows,
web interfaces, diff and merge tools (the equivalent to Meld, WinMerge
or Kdiff3), and also a cloud hosting for repositories. Add Visual Studio
plugins, integrations with the major Continuous Integration systems,
IDEs and issue trackers.</p>

<p>Plastic SCM was first released in 2006 and didn't stop evolving in the
last 13+ years, with 2-3 new public versions every week for the last 2 years.</p>

<p>Overall Plastic SCM sums more than 1.5 million lines of code and 95% of
them written in C#. This means we have extensively used Mono for everything
non-Windows since the very early days, now almost a decade and a half
ago.</p>

<p>And here goes the story.</p>

<h2 id="mono-shone-light-down-the-cross-platform-way">Mono shone light down the cross-platform way</h2>

<p>When the first lines of Plastic SCM were written back in September 2005,
the decision to go for C# was already made. But we knew a new version
control system could only be considered as a serious alternative if it
was truly cross-platform. Windows-only was not a choice.</p>

<p>Why then, we decided to go for .NET/C# instead of something more
portable like Java, or C/C++? The reason was clear: because Mono
existed. We had never decided to use C# if Mono hadn't been there already.
It promised a way to have cross-platform .NET and we invested heavily on
it. How did it work out? Read along fellow monkeys!</p>

<h2 id="fun-with-cross-platform-winforms">Fun with cross-platform WinForms</h2>

<p>Code once, run everywhere. That's what we embraced when we started
doing our first tests with WinForms on Linux.</p>

<p><img src="/images/2019-02-13-plastic-scm-mono/plastic-1-on-linux.jpg" alt="Plastic 1 on Linux" /></p>

<p>With very minor changes, the full Windows version was able to run on
Linux and macOS (through X11). We later rewrote most of the controls we
were using on WinForms to give them a more consistent look and feel:</p>

<p><img src="/images/2019-02-13-plastic-scm-mono/plastic-2-on-linux.jpg" alt="Plastic 2 on Linux" /></p>

<p><img src="/images/2019-02-13-plastic-scm-mono/plastic-2-on-linux-2.jpg" alt="Plastic 2 on Linux" /></p>

<p>We also did this as a workaround to basically skip some well-known
issues with some standard controls. Obviously, desktop GUIs were not a
big thing in Mono, and we felt like pioneers finding our way through a
wild territory :-)</p>

<h2 id="mono-on-solaris---a-story-from-the-good-old-days">Mono on Solaris - a story from the good-old days</h2>

<p>Something many won't know is that for a couple of years we were the
unofficial maintainers of the <a href="http://blog.plasticscm.com/2010/10/welcome-crazy-monkeys-mono-on-solaris.html?utm_source=Mono%20Blog&amp;utm_medium=post&amp;utm_campaign=Mono%20success%20story&amp;utm_term=DotNet&amp;utm_content=Open%20Source">Mono port for
Solaris</a>.</p>

<p>We were lucky enough to hire a former Mono hacker, our friend Dick
Porter, who enrolled to help us porting Plastic SCM to exotic platforms
like Solaris and HP-UX.</p>

<p>By that time, we still relied on WinForms everywhere, which was a
challenge on its own.</p>

<p>You can see how Plastic SCM running on Solaris looked like:</p>

<p><img src="/images/2019-02-13-plastic-scm-mono/plastic-2.7-on-solaris-cde.jpg" alt="Plastic 2.7 on Solaris" /></p>

<p>And:</p>

<p><img src="/images/2019-02-13-plastic-scm-mono/plastic-on-solaris-sparc.png" alt="Plastic on Solaris SPARC" /></p>

<p>We were super excited about it because it allowed us to run Plastic SCM
on some old Sun workstations we had around. And they featured SPARC
CPUs, 64-bit big endian and everything. In fact, we found and protected
some edge cases caused by big endian :-).</p>

<h2 id="from-boehm-to-sgen">From Boehm to sgen</h2>

<p>We were hit by some of the limitations of Boehm GC, so we happily
provided the developers working on the new sgen collector with a memory
hungry Plastic SCM environment. We used to run some memory intensive
automated tests for them so we mutually benefit from the effort.</p>

<p>This was mostly before everyone moved to the cloud, so we ran most of
these tests in about 300 real machines controlled by our in-house PNUnit
test environment.</p>

<h2 id="native-guis">Native GUIs</h2>

<p>Depending on X11 to run our GUI on macOS wasn't greatly perceived by
hardcore Apple users who prefer a smooth experience. So, we decided to
radically change our approach to GUI development. We committed to create
native GUIs for each of our platforms.</p>

<ul>
  <li>
    <p>Windows would still benefit from the same original codebase. But,
removing the common denominator requirements allowed us to introduce
new controls and enrich the overall experience.</p>
  </li>
  <li>
    <p>The macOS GUI would be rewritten taking advantage of MonoMac, which
later became XamarinMac, the technology we still use. It was going
to be an entirely new codebase that only shared the core operations
with Windows, while the entire intermediate layer would be developed
from scratch.</p>
  </li>
  <li>
    <p>And finally, we decided to go for a native GTKSharp-based GUI for
Linux. In fact, it would be a good exercise to see how much of the
common layer could be actually shared between macOS and Linux. It
worked quite well.</p>
  </li>
</ul>

<p>Some takeaways from this new approach:</p>

<ul>
  <li>
    <p>We decided to entirely skip "visual designer tools". ResX on
WinForms proved to be a nightmare when used cross-platform, and
depending on locating controls by hand with a designer on a canvas
wasn't good to keep consistent margins, spacing and so on. So, we
went cowboy: every single GUI you see in Plastic SCM now (except the
older ones in WinForms) is basically coded, not designed. Every
button is created with "new Button()", and so on. It can sound
like a slowdown, but it certainly pays off when maintaining code:
you spend much <a href="http://blog.semanticmerge.com/2014/03/gui-development-design-or-code-imperative.html?utm_source=Mono%20Blog&amp;utm_medium=post&amp;utm_campaign=Mono%20success%20story&amp;utm_term=DotNet&amp;utm_content=Open%20Source">less time dealing with code than
designers</a>.</p>
  </li>
  <li>
    <p>We created our own <a href="http://blog.plasticscm.com/2019/01/guitestsharp-multiplatform-gui-testing-dotnet.html?utm_source=Mono%20Blog&amp;utm_medium=post&amp;utm_campaign=Mono%20success%20story&amp;utm_term=DotNet&amp;utm_content=Open%20Source">automated GUI test
environment</a>
to test the Linux and macOS GUIs. There weren't any cross-platform
solutions for Mono, so we decided to create our own.</p>
  </li>
  <li>
    <p>We realized how much better GTK was and is than any other solution
from a programmer’s perspective. We love to code GTK. Yes, it is
also possibly the ugliest in visual terms of them all, but you
can't have it all :-)</p>
  </li>
</ul>

<p>This is how Plastic SCM looks now, enjoy:</p>

<p><img src="/images/2019-02-13-plastic-scm-mono/macplastic-brex.png" alt="Mac Branch Explorer" /></p>

<p><img src="/images/2019-02-13-plastic-scm-mono/windows-diff-window.png" alt="Windows Diff Window" /></p>

<p><img src="/images/2019-02-13-plastic-scm-mono/windows-brex.png" alt="Windows Branch Explorer" /></p>

<h2 id="but-wait-wouldnt-mono-affect-performance">But wait! Wouldn’t Mono affect performance?</h2>

<p>Many of you might think: how can a version control be written in
Mono/C# and expect to compete against Git or Subversion or even
Perforce which are all written in C or a C/C++ combination?</p>

<p>Speed was an obsession for us since day one, and we found C# to be
quite capable if used carefully. The only downside is that when you are
in a C#/managed world you tend to think allocating memory is free and
you pay for it when that happens (something that radically changed with
the arrival of .NET Core and the entire Span&lt;T&gt; and their focus on
making the platform a real option for highly scalable and performant
solutions). But, over the years we learned a few lessons, started to be
much more aware of the importance of saving allocations, and the results
backed up that reasoning.</p>

<p>Below you can see how a 2019 version of Plastic SCM compares to Git and
a major commercial version control competitor performing quite common
operations:</p>

<p><img src="/images/2019-02-13-plastic-scm-mono/performance-benchmark.png" alt="Performance Benchmark" /></p>

<p>As you can see, Plastic SCM consistently beats Git, which we believe is
quite an achievement considering it is written in .NET/Mono/C# instead
of system-level C.</p>

<h2 id="heavy-loaded-servers">Heavy loaded servers</h2>

<p>In terms of pure scalability, we also achieve quite good results
compared to commercial version controls:</p>

<p><img src="/images/2019-02-13-plastic-scm-mono/scalability-benchmark.png" alt="Scalability Benchmark" /></p>

<p>We don't compare to Git here since what we are running are pure
centralized operations (direct checkin or commit if you prefer)
something Git can't do. Plastic SCM can work in Git or SVN modes, local
repos or direct connection to a central server.</p>

<p>In fact, some of our highest loaded servers on production run on
Linux/Mono serving more than 3000 developers on a big enterprise setup.
A single server handles most of the load singlehandedly :-)</p>

<h2 id="mono-is-indeed-for-code-monkeys">Mono is indeed for code monkeys</h2>

<p>If you’ve read the whole use case you already know that we have been
using Mono for the purpose of providing a full version control stack
since 2006! That is for almost 13 years, right after the company was
founded and the first product of our portfolio was delivered.</p>

<p>After all this time it has helped us build and distribute the same
product across the different environments: a full stack version control
system that is pioneering software configuration management in many
areas. The results are there and hey, we are demanding: versatility,
performance, scalability and resilience are not an option for our
clients, or us. Given the structural relevance of SCM tools to any
software project, it is paramount for Plastic SCM to deliver a solid
product across all platforms, and we do it. To us Mono = cross-platform
and that is a huge advantage since we can focus on functionality,
roadmap and support while Mono makes the product one same experience
everywhere. Mono is definitely a foundational part of toolkit.</p>]]></content><author><name>Jordi Mon Companys</name></author><category term="news" /><category term="version control" /><category term="semantic merge" /><summary type="html"><![CDATA[Note: This is a guest post by Jordi Mon Companys from Códice Software, a long-time Mono user, about how they used Mono to develop their flagship product.]]></summary></entry><entry><title type="html">Giving Mono Souper Powers</title><link href="https://www.mono-project.com/news/2018/12/06/souper/" rel="alternate" type="text/html" title="Giving Mono Souper Powers" /><published>2018-12-06T00:00:00+00:00</published><updated>2018-12-06T00:00:00+00:00</updated><id>https://www.mono-project.com/news/2018/12/06/souper</id><content type="html" xml:base="https://www.mono-project.com/news/2018/12/06/souper/"><![CDATA[<p>By virtue of using LLVM, Mono has access to a wide suite of tools and
optimization backends. A lot of active research uses LLVM IR. One such
research project, Souper, tries to brute-force a search for missed
optimizations in our emitted code. The .NET community may have software
projects that benefit from using Souper directly to generate code, rather
than waiting for us to find ways to automate those optimizations ourselves.
This algorithm can generate code that would be very challenging
for a traditional compiler to find.</p>

<p>The Mono .NET VM is a rather nimble beast. Rather than requiring all
users to live with the performance characteristics of a given policy, we
often choose to create multiple backends and bindings that exploit
what’s best of the native platform while presenting a common
interface. Part of this is the choice between using an interpreter, a
Just-In-Time compiler, or an Ahead-Of-Time compiler.</p>

<p>AOT compilation is attractive to some projects for the
combination of optimized code with low start-up time. This is the
classic advantage of native code over code from a JIT or interpreter.
AOT code is often much worse than code from a JIT because of a need for
indirection in code that references objects in run-time memory. It’s important
for AOT code to exploit every possible optimization to make up for this
disadvantage. For this, we increasingly rely on optimizations performed by LLVM.</p>

<p>LLVM’s optimization passes can analyze a program globally. It is able to see
through layers of abstractions and identify repeated or needless operations in a program’s
global flow. Likewise, it can examine the operations in a small segment of code and
make them perfect with respect to one another. Sometimes though, we fail to optimize code.
Classic compilers work by analyzing the control-flow and dataflow of a program
and matching on specific patterns such as stores to variables that aren’t used later
and constants that are stored to variables rather than being propagated everywhere
they can be. If the pattern matches, the transformation can take place.
Sometimes the code we feed into LLVM does not match the patterns
of inefficiency that it looks for, and we don’t get an optimization.</p>

<p>What’s worse is that we don’t know that we hit this optimization blocker. We
don’t know what we expect from code until it’s a problem and we’re
really spending time optimizing it. Spotting trends in generated machine
code across thousands of methods is incredibly labor intensive. Often only
really bad code that runs many many times will catch attention. Fixing every
single missed optimization and finding every single missed optimization becomes
a chicken-and-egg problem.</p>

<p>The solution to some manifestations of this problem is the use of
superoptimizers. The academic discipline of superoptimizers is very
old. The idea is to treat the code that was written as more
of a restriction, a specification. The superoptimizer generates a ton
of native code and checks the ways in which it behaves
differently than the written code. If it can generate a faster native
code sequence than the compiler generated while keeping behavior exactly the
same, it wins.</p>

<p>This “exactly the same” part can be incredibly expensive if not done
correctly. The computational effort involved has historically kept superoptimization from being
used very often. Since then, it has gotten a lot easier to run computationally intensive jobs.
Computer hardware has become orders of magnitude more powerful.
Theorems around equivalence checking and control-flow
representations made more powerful claims and used algorithms with better running times.
We are therefore seeing superoptimization research reemerge at this time.</p>

<p>One superoptimizer in particular, named <a href="https://arxiv.org/abs/1711.04422">Souper</a>, has
reached maturity while interoperating with the industry standard
code generator (LLVM) and the industry standard SMT engine (Z3). It has
kickstarted a renewed faith in researchers that superoptimization is a reasonable
policy. It can take the LLVM IR that a compiler was going to feed into LLVM, and
compute better IR. This can sometimes take a lot of time, and the
code emitted is the result of a process that isn’t auditable. The pipeline is
placing total faith in Souper for the correctness of generated
code.</p>

<p>It’s mostly useful for compiler engineers to use to tell that
optimizations were missed, and to identify how to fix that using
conventional pattern matching over the program’s control-flow and dataflow graphs.
That said, Souper offers the ability to
drop in for clang and to generate the code that is run. Some projects are
eager to make any trade-offs for performance that are acceptable.
Other projects may want to get a feel for how fast they could run
if they were to invest making sure Mono generates good code.
If the compile time increase doesn’t discourage them, many
projects may find some benefit in such an optimizing compiler.</p>

<p>I recommend that curious readers install Z3, get a checkout of</p>

<p><a href="https://github.com/google/souper">https://github.com/google/souper</a>,</p>

<p>and complete the compilation process described in that documentation.</p>

<p>When AOTing code with Mono, they’re going to want to pass the
commandline flags named there into the <code>---aot=llvmopts=</code> argument.</p>

<p>As of the time of this writing, that is</p>

<pre><code class="language-bash">llvmopts="-load /path/to/libsouperPass.so -souper -z3-path=/usr/bin/z3"
</code></pre>

<p>Mono will then allow Souper to step in during the middle of the LLVM
compilation and try it’s best at brute-forcing some better code. If
there’s anything short and fast that does the job better, it will be
found.</p>

<p>It is frankly amazing that Mono can get such extensive optimizations simply
by compiling to LLVM IR. Without changing a single line of Mono’s source, we
changed our compilation pipeline in truly dramatic ways. This shows off the
lack of expectations that Mono has about the layout of our generated code. This shows off the
flexibility of LLVM as a code generation framework and to Mono as an embedded runtime.
Embedders using Mono should consider
using our LLVM backend with this and other third-party LLVM optimization passes.
Feedback about the impact of our research on real-world programs will help us decide what we
should be using by default.</p>]]></content><author><name>Alexander Kyte</name></author><category term="news" /><category term="runtime" /><summary type="html"><![CDATA[By virtue of using LLVM, Mono has access to a wide suite of tools and optimization backends. A lot of active research uses LLVM IR. One such research project, Souper, tries to brute-force a search for missed optimizations in our emitted code. The .NET community may have software projects that benefit from using Souper directly to generate code, rather than waiting for us to find ways to automate those optimizations ourselves. This algorithm can generate code that would be very challenging for a traditional compiler to find.]]></summary></entry><entry><title type="html">Writing a JIT Compiler in C#</title><link href="https://www.mono-project.com/news/2018/09/11/csharp-jit/" rel="alternate" type="text/html" title="Writing a JIT Compiler in C#" /><published>2018-09-11T00:00:00+00:00</published><updated>2018-09-11T00:00:00+00:00</updated><id>https://www.mono-project.com/news/2018/09/11/csharp-jit</id><content type="html" xml:base="https://www.mono-project.com/news/2018/09/11/csharp-jit/"><![CDATA[<p>During the 2018 Microsoft Hack Week, members of the Mono team explored the idea
of replacing the Mono’s code generation engine written in C with a code
generation engine written in C#.</p>

<p>In this blog post we describe our motivation, the interface between the native
Mono runtime and the managed compiler and how we implemented the new managed
compiler in C#.</p>

<h2 id="motivation">Motivation</h2>

<p>Mono’s runtime and JIT compiler are entirely written in C, a highly portable
language that has served the project well.
Yet, we feel jealous of our own users that get to write code in a high-level
language and enjoy the safety, the luxury and reap the benefits of writing code
in a high-level language, while the Mono runtime continues to be written in C.</p>

<p>We decided to explore whether we could make Mono’s compilation engine pluggable
and then plug a code generator written entirely in C#.
If this were to work, we could more easily prototype, write new optimizations
and make it simpler for developers to safely try changes in the JIT.</p>

<p>This idea has been explored by research projects like the
<a href="https://www.jikesrvm.org/">JikesRVM</a>,
<a href="https://github.com/beehive-lab/Maxine-VM">Maxime</a> and
<a href="https://www.graalvm.org/">Graal</a> for Java.
In the .NET world, the Unity team wrote an IL compiler to C++ compiler called
<a href="https://docs.unity3d.com/Manual/IL2CPP-HowItWorks.html">il2cpp</a>.
They also experimented with a <a href="http://xoofx.com/blog/2018/04/12/writing-managed-jit-in-csharp-with-coreclr/">managed
JIT</a>
recently.</p>

<p>In this blog post, we discuss the prototype that we built.
The code mentioned in this blog post can be found here:
<a href="https://github.com/lambdageek/mono/tree/mjit/mcs/class/Mono.Compiler">https://github.com/lambdageek/mono/tree/mjit/mcs/class/Mono.Compiler</a></p>

<h2 id="interfacing-with-the-mono-runtime">Interfacing with the Mono Runtime</h2>

<p>The Mono runtime provides various services, just-in-time compilation, assembly
loading, an IO interface, thread management and debugging capabilities.
The code generation engine in Mono is called <code>mini</code> and is used both for static
compilation and just-in-time compilation.</p>

<p>Mono’s code generation has a number of dimensions:</p>

<ul>
  <li>Code can be either interpreted, or compiled to native code</li>
  <li>When compiling to native code, this can be done just-in-time, or it can be batch compiled, also known as ahead-of-time compilation.</li>
  <li>Mono today has two code generators, the light and fast <code>mini</code> JIT engine, and the heavy duty engine based on the LLVM optimizing compiler.  These two are not really completely unaware of the other, Mono’s LLVM support reuses many parts of the <code>mini</code> engine.</li>
</ul>

<p>This project started with a desire to make this division even more clear, and
to swap up the native code generation engine in ‘mini’ with one that could be
completely implemented in a .NET language.
In our prototype we used C#, but other languages like F# or IronPython could be used as well.</p>

<p>To move the JIT to the managed world, we introduced the <code>ICompiler</code> interface
which must be implemented by your compilation engine, and it is invoked on
demand when a specific method needs to be compiled.</p>

<p>This is the interface that you must implement:</p>

<pre><code class="language-csharp">interface ICompiler {
    CompilationResult CompileMethod (IRuntimeInformation runtimeInfo,
                                     MethodInfo methodInfo,
                                     CompilationFlags flags,
                                     out NativeCodeHandle nativeCode);

    string Name { get; }
}
</code></pre>

<p>The <code>CompileMethod ()</code> receives a <code>IRuntimeInformation</code> reference, which
provides services for the compiler as well as a <code>MethodInfo</code> that represents
the method to be compiled and it is expected to set the <code>nativeCode</code> parameter
to the generated code information.</p>

<p>The <code>NativeCodeHandle</code> merely represents the generated code address and its length.</p>

<p>This is the <code>IRuntimeInformation</code> definition, which shows the methods available
to the <code>CompileMethod</code> to perform its work:</p>

<pre><code class="language-csharp">interface IRuntimeInformation {
    InstalledRuntimeCode InstallCompilationResult (CompilationResult result,
                                                   MethodInfo methodInfo,
                                                  NativeCodeHandle codeHandle);

    object ExecuteInstalledMethod (InstalledRuntimeCode irc,
                                   params object[] args);

    ClassInfo GetClassInfoFor (string className);

    MethodInfo GetMethodInfoFor (ClassInfo classInfo, string methodName);

    FieldInfo GetFieldInfoForToken (MethodInfo mi, int token);

    IntPtr ComputeFieldAddress (FieldInfo fi);

    /// For a given array type, get the offset of the vector relative to the base address.
    uint GetArrayBaseOffset(ClrType type);
}
</code></pre>

<p>We currently have one implementation of <code>ICompiler</code>, we call it the the “<code>BigStep</code>” compiler.
When wired up, this is what the process looks like when we compile a method with it:</p>

<p><a href="/images/2018-09-12-csharp-jit/mini-runtime.1.svg"><img src="/images/2018-09-12-csharp-jit/mini-runtime.1.svg" alt="Managed JIT overview" /></a></p>

<p>The <code>mini</code> runtime can call into managed code via <code>CompileMethod</code> upon a
compilation request.
For the code generator to do its work, it needs to obtain some information
about the current environment.
This information is surfaced by the <code>IRuntimeInformation</code> interface.
Once the compilation is done, it will return a blob of native instructions to
the runtime.
The returned code is then “installed” in your application.</p>

<p>Now there is a trick question: Who is going to compile the compiler?</p>

<p>The compiler written in C# is initially executed with one of the built-in
engines (either the
<a href="/news/2017/11/13/mono-interpreter/">interpreter</a>,
or the JIT engine).</p>

<h2 id="the-bigstep-compiler">The BigStep Compiler</h2>

<p>Our first <code>ICompiler</code> implementation is called the
<a href="https://github.com/lambdageek/mono/tree/mjit/mcs/class/Mono.Compiler/Mono.Compiler.BigStep">BigStep</a>
compiler.</p>

<p>This compiler was designed and implemented by a developer (Ming Zhou) not
affiliated with Mono Runtime Team.
It is a perfect showcase of how the work we presented through this project can
quickly enable a third-party to build their own compiler without much hassle
interacting with the runtime internals.</p>

<p>The BigStep compiler implements an IL to LLVM compiler.
This was convenient to build the proof of concept and ensure that the design
was sound, while delegating all the hard compilation work to the LLVM compiler
engine.</p>

<p>A lot can be said when it comes to the design and architecture of a compiler,
but our main point here is to emphasize how easy it can be, with what we have
just introduced to Mono runtime, to bridge IL code with a customized backend.</p>

<p>The IL code is streamed into to the compiler interface through an iterator,
with information such as op-code, index and parameters immediately available to
the user.
See below for more details about the prototype.</p>

<h2 id="hosted-compiler">Hosted Compiler</h2>

<p>Another beauty of moving parts of the runtime to the managed side is that we
can test the JIT compiler <em>without</em> recompiling the native runtime, so
essentially developing a normal C# application.</p>

<p>The <code>InstallCompilationResult ()</code> can be used to register compiled method with
the runtime and the <code>ExecuteInstalledMethod ()</code> are can be used to invoke a
method with the provided arguments.</p>

<p>Here is an example how this is used code:</p>

<pre><code class="language-csharp">public static int AddMethod (int a, int b) {
  return a + b;
}

[Test]
public void TestAddMethod ()
{
  ClassInfo ci = runtimeInfo.GetClassInfoFor (typeof (ICompilerTests).AssemblyQualifiedName);
  MethodInfo mi = runtimeInfo.GetMethodInfoFor (ci, "AddMethod");
  NativeCodeHandle nativeCode;

  CompilationResult result = compiler.CompileMethod (runtimeInfo, mi, CompilationFlags.None, out nativeCode);
  InstalledRuntimeCode irc = runtimeInfo.InstallCompilationResult (result, mi, nativeCode);

  int addition = (int) runtimeInfo.ExecuteInstalledMethod (irc, 1, 2);
  Assert.AreEqual (addition, 3);
}
</code></pre>

<p>We can ask the host VM for the actual result, assuming it’s our gold standard:</p>

<pre><code class="language-csharp">int mjitResult = (int) runtimeInfo.ExecuteInstalledMethod (irc, 666, 1337);
int hostedResult = AddMethod (666, 1337);
Assert.AreEqual (mjitResult, hostedResult);
</code></pre>

<p>This eases development of a compiler tremendously.</p>

<p>We don’t need to eat our own dog food during debugging, but when we feel ready
we can flip a switch and use the compiler as our system compiler.
This is actually what happens if you run <code>make -C mcs/class/Mono.Compiler run-test</code>
in the <a href="https://github.com/lambdageek/mono/tree/mjit">mjit branch</a>: We use this
API to test the managed compiler while running on the regular Mini JIT.</p>

<h2 id="native-to-managed-to-native-wrapping-mini-jit-into-icompiler">Native to Managed to Native: Wrapping Mini JIT into <code>ICompiler</code></h2>

<p>As part of this effort, we also wrapped Mono’s JIT in the <code>ICompiler</code> interface.</p>

<p><a href="/images/2018-09-12-csharp-jit/mini-runtime.2.svg"><img src="/images/2018-09-12-csharp-jit/mini-runtime.2.svg" alt="MiniCompiler" /></a></p>

<p><code>MiniCompiler</code> calls back into native code and invokes the regular Mini JIT.
It works surprisingly well, however there is a caveat: Once back in the native
world, the Mini JIT doesn’t need to go through <code>IRuntimeInformation</code> and just
uses its old ways to retrieve runtime details.
Though, we can turn this into an incremental process now: We can identify those
parts, add them to <code>IRuntimeInformation</code> and change Mini JIT so that it uses
the new API.</p>

<h2 id="conclusion">Conclusion</h2>

<p>We strongly believe in a long-term value of this project.
A code base in managed code is more approachable for developers and thus easier
to extend and maintain.
Even if we never see this work upstream, it helped us to better understand the
boundary between runtime and JIT compiler, and who knows, it might will help us
to integrate RyuJIT into Mono one day 😉</p>

<p>We should also note that <code>IRuntimeInformation</code> can be implemented by any other
.NET VM: Hello <code>CoreCLR</code> folks 👋</p>

<p>If you are curious about this project, ping us on our <a href="https://gitter.im/mono/mono-mjit">Gitter
channel</a>.</p>

<hr />

<h3 id="appendix-converting-stack-based-opcodes-into-register-operations">Appendix: Converting Stack-Based OpCodes into Register Operations</h3>

<p>Since the target language was LLVM IR, we had to build a translator that
converted the stack-based operations from IL into the register-based operations
of LLVM.</p>

<p>Since many potential target are register based, we decided to design a
framework to make it reusable of the part where we interpret the IL logic. To
this goal, we implemented an engine to turn the stack-based operations into the
register operations.</p>

<p>Consider the <code>ADD</code> operation in IL.
This operation pops two operands from the stack, performing addition and pushing back the result to the stack. This is documented in ECMA 335 as follows:</p>

<pre><code class="language-text">  Stack Transition:
      ..., value1, value2 -&gt; ..., result
</code></pre>

<p>The actual kind of addition that is performed depends on the types of the
values in the stack.
If the values are integers, the addition is an integer addition.
If the values are floating point values, then the operation is a floating point
addition.</p>

<p>To re-interpret this in a register-based semantics, we treat each pushed frame
in the stack as a different temporary value.
This means if a frame is popped out and a new one comes in, although it has the
same stack depth as the previous one, it’s a new temporary value.</p>

<p>Each temporary value is assigned a unique name.
Then an IL instruction can be unambiguously presented in a form using temporary names instead of stack changes.
For example, the <code>ADD</code> operation becomes</p>

<pre><code class="language-text">Temp3 := ADD Temp1 Temp2
</code></pre>

<p>Other than coming from the stack, there are other sources of data during
evaluation: local variables, arguments, constants and instruction offsets (used
for branching).
These sources are typed differently from the stack temporaries, so that the
downstream processor (to talk in a few) can properly map them into their
context.</p>

<p>A third problem that might be common among those target languages is the
jumping target for branching operations.
IL’s branching operation assumes an implicit target should the result be taken:
The next instruction.
But branching operations in LLVM IR must explicitly declare the targets for
both taken and not-taken paths.
To make this possible, the engine performs a pre-pass before the actual
execution, during which it gathers all the explicit and implicit targets.
In the actual execution, it will emit branching instructions with both targets.</p>

<p>As we mentioned earlier, the execution engine is a common layer that merely
translates the instruction to a more generic form.
It then sends out each instruction to <code>IOperationProcessor</code>, an interface that
performs actual translation.
Comparing to the instruction received from <code>ICompiler</code>, the presentation here,
<code>OperationInfo</code>, is much more consumable:
In addition to the op codes, it has an array of the input operands, and a result operand:</p>

<pre><code class="language-csharp">public class OperationInfo
{
  ... ...
  internal IOperand[] Operands { get; set; }
  internal TempOperand Result { get; set; }
  ... ...
}
</code></pre>

<p>There are several types of the operands: <code>ArgumentOperand</code>, <code>LocalOperand</code>,
<code>ConstOperand</code>, <code>TempOperand</code>, <code>BranchTargetOperand</code>, etc.
Note that the result, if it exists, is always a <code>TempOperand</code>.
The most important property on <code>IOperand</code> is its <code>Name</code>, which unambiguously
defines the source of data in the IL runtime.
If an operand with the same name comes in another operation, it unquestionably
tells us the very same data address is targeted again.
It’s paramount to the processor to accurately map each name to its own storage.</p>

<p>The processor handles each operand according to its type.
For example, if it’s an argument operand, we might consider retrieving the
value from the corresponding argument.
An x86 processor may map this to a register.
In the case of LLVM, we simply go to fetch it from a named value that is
pre-allocated at the beginning of method construction.
The resolution strategy is similar for other operands:</p>

<ul>
  <li><code>LocalOperand</code>: fetch the value from pre-allocated address</li>
  <li><code>ConstOperand</code>: use the const value carried by the operand</li>
  <li><code>BranchTargetOperand</code>: use the index carried by the operand</li>
</ul>

<p>Since the temp value uniquely represents an expression stack frame from CLR
runtime, it will be mapped to a register.
Luckily for us, LLVM allows infinite number of registers, so we simply name a
new one for each different temp operand.
If a temp operand is reused, however, the very same register must as well.</p>

<p>We use <a href="https://github.com/Microsoft/LLVMSharp/">LLVMSharp</a> binding to
communicate with LLVM.</p>]]></content><author><name>Ludovic Henry, Miguel de Icaza, Aleksey Kliger, Bernhard Urban and Ming Zhou</name></author><category term="news" /><category term="runtime" /><summary type="html"><![CDATA[During the 2018 Microsoft Hack Week, members of the Mono team explored the idea of replacing the Mono’s code generation engine written in C with a code generation engine written in C#.]]></summary></entry><entry><title type="html">Introducing Mono on AIX and IBM i</title><link href="https://www.mono-project.com/news/2018/05/29/mono-on-aix-and-ibm-i/" rel="alternate" type="text/html" title="Introducing Mono on AIX and IBM i" /><published>2018-05-29T00:00:00+00:00</published><updated>2018-05-29T00:00:00+00:00</updated><id>https://www.mono-project.com/news/2018/05/29/mono-on-aix-and-ibm-i</id><content type="html" xml:base="https://www.mono-project.com/news/2018/05/29/mono-on-aix-and-ibm-i/"><![CDATA[<!-- Edited by Mark Waterbury -->

<p><em>Note: This is a guest post by Calvin Buckley (@NattyNarwhal on GitHub)
introducing the community port of Mono to IBM AIX and IBM i. If you’d like to
help with this community port please contact the maintainers on Gitter.</em></p>

<p><a href="/images/mono-os400.png"><img src="/images/mono-os400.png" alt="C# REPL running under IBM i" /></a></p>

<p>You might have noticed this in the
<a href="http://www.mono-project.com/docs/about-mono/releases/5.12.0/">Mono 5.12 release notes</a>,
Mono now includes support for
<a href="/docs/about-mono/supported-platforms/aix/">IBM AIX and IBM i</a>; two very
different yet (mostly!) compatible operating systems. This post should serve as
an introduction to this port.</p>

<h2 id="what-does-it-take-to-port-mono">What does it take to port Mono?</h2>

<p>Porting Mono to a new operating system is not as hard as you might think!
Pretty much the entire world is POSIX compliant these days, and Mono is a large
yet manageable codebase due to a low number of dependencies, use of plain C99,
and an emphasis on portability. Most common processor architectures in use are
supported by the code generator, though more obscure ISAs will have some caveats.</p>

<p>Pretty much all of the work you do will be twiddling <code>#ifdefs</code> to accommodate for
the target platform’s quirks; such as missing or different preprocessor
definitions and functions, adding the platform to definitions so it is
supported by core functionality, and occasionally having to tweak the runtime
or build system to handle when the system does something completely differently
than others. In the case of AIX and IBM i, I had to do all of these things.</p>

<h2 id="where-would-i-be-without-ibm"><a href="https://www.youtube.com/watch?v=dYBfdtMAD2Q">Where would I be without IBM?</a></h2>

<p>For some background on what needed to happen, we can start by giving some
background on our target platforms.</p>

<p>Both of our targets run on 64-bit PowerPC processors in big endian mode.
<a href="/docs/about-mono/supported-platforms/powerpc/">Mono does support PowerPC</a>,
and Bernhard Urban maintains it. What is odd about the calling conventions on
AIX (shared occasionally by Linux) is the use of <em>function descriptors</em>, which
means that pointers to functions do not point to code, but instead point to
metadata about them. This can cause bugs in the JIT if you are not careful to
consume or produce function descriptors instead of raw pointers when needed.
Because the runtime is better tested on 64-bit PowerPC, and machines are fast
enough that the extra overhead is not significant, we always build a 64-bit
runtime.</p>

<p>In addition to a strange calling convention, AIX also has a different binary
format - that means that currently, the ahead-of-time compiler does not work.
While most Unix-like operating systems use ELF, AIX (and by extension, IBM i
for the purposes of this port) use XCOFF, a subset of the Windows PE binary
format.</p>

<p>AIX is a Unix (descended from the System V rather than the BSD side of the
family) that runs on PowerPC systems. Despite being a Unix, it has some quirks
of its own, that I will describe in this article.</p>

<h3 id="unix-whats-a-unix">Unix? What’s a Unix?</h3>

<p>IBM i (formerly known as i5/OS or OS/400) is decidedly not a Unix. Unlike Unix,
it has an object-based filesystem where all objects are mapped into a single
humongous address space, backed on disk known as single level storage – real
main storage (RAM) holds pages of objects “in use” and acts as a cache for
objects that reside permanently on disk. Instead of flat files, IBM i uses
database tables as the means to store data. (On IBM i, all files are database
tables, and a file is just one of the “object types” supported by IBM i; others
include libraries and programs.) Programs on IBM i are not simple native
binaries, but instead are “encapsulated” objects that contain an intermediate
form, called Machine Interface instructions, (similar to MSIL/CIL) that is then
translated and optimized ahead-of-time for the native hardware (or upon first
use); this also provides part of the security model and has allowed users to
transition from custom CISC CPUs to enhanced PowerPC variants, without having
to recompile their programs from the original source code.</p>

<p>This sounds similar to running inside of WebAssembly rather than any kind of
Unix – So, then, how do you port programs dependent on POSIX? IBM i provides an
environment called PASE (Portable Application Solutions Environment) that
provides binary compatibility for AIX executables,  for a large subset of the
AIX ABI, within the IBM i. But Unix and IBM i are totally different; Unix has
files and per-process address spaces, and IBM i normally does not, so how do
you make these incongruent systems work?</p>

<p>To try to bridge the gap, IBM i also has an “Integrated File System” that
supports byte-stream file objects in a true hierarchical file system directory
hierarchy. For running Unix programs that expect their own address space, IBM i
provides something called “teraspace” that provides a large private address
space per process or job. This requires IBM i to completely changes the MMU
mode and does a cache/TLB flush every time it enters and exits the Unix world,
making system calls somewhat expensive; in particular, forking and I/O. While
some system calls are not implemented, there are more than enough to port
non-trivial AIX programs to the PASE environment, even with its quirks and
performance limitations. You could even build them entirely inside of the PASE
environment.</p>

<p>A port to the native IBM i environment outputting MI code with the ahead of
time compiler has been considered, but would take a lot of work to write an MI
backend for the JIT, use the native APIs in the runtime, and handle how the
environment is different from anything else Mono runs on. As such, I instead
PASE and AIX for the ease of porting existing POSIX compatible code.</p>

<h2 id="what-happened-to-port-it">What happened to port it?</h2>

<p>The port came out of some IBM i users expressing an interest in wanting to run
.NET programs on their systems. A friend of mine involved in the IBM i
community had noticed I was working on a (mostly complete, but not fully
working) Haiku port, and approached me to see if it could be done. Considering
that that I now had experience with porting Mono to new platforms, and there
was already a PowerPC JIT, I decided to take the challenge.</p>

<p>The primary porting target was IBM i, with AIX support being a by-product.
Starting by building on IBM i, I set up a chroot environment to work in,
(chroot support was added to PASE fairly recently), setting up a toolchain with
AIX packages. Initial bring-up of the port happened on IBM i, up to the point
where the runtime was built, but execution of generated code was not happening.
One problem with building on IBM i, however, is that the performance
limitations really start to show. While building took the same amount of time
on the system I had access to (dual POWER6, taking about roughly 30 minutes to
build the runtime) as AIX due to it mostly being computation, the configure
script was extremely impacted due to its emphasis on many small reads and
writes with lots of forking. Whereas it took AIX 5 minutes and Linux 2 minutes
to run through the configure script, it took IBM i well over an hour to run
through all of it. (Ouch!)</p>

<p>At this point, I submitted the initial branch as a pull request for review. A
lot of back and forth went on to work on the underlying bugs as well as
following proper style and practices for Mono. I set up an AIX VM on the
machine, and switched to cross-compiling from AIX to IBM i; targeting both
platforms with the same source and binary. Because I was not building on IBM i
any longer, I had to periodically copy binaries over to IBM i, to check if Mono
was using missing libc functions or system calls, or if I had tripped on some
behaviour that PASE exhibits differently from AIX. With the improved iteration
time, I could start working on the actual porting work much more quickly.</p>

<p>To help with matters where I was unsure exactly how AIX worked, David Edelsohn
from IBM helped by explaining how AIX handles things like calling conventions,
libraries, issues with GCC, and best practices for dealing with porting things
to AIX.</p>

<h2 id="what-needed-to-change">What needed to change?</h2>

<p>There are some unique aspects of AIX and the subset that PASE provides, beyond
the usual <code>#ifdef</code> handling.</p>

<h3 id="what-did-we-start-with">What did we start with?</h3>

<p>One annoyance I had was how poor the GNU tools are on AIX. GNU binutils are
effectively useless on AIX, so I had to explicitly use IBM’s binutils, and deal
with some small problems related to autotools with environment variables and
assumption of GNU ld features in makefiles. I had also dealt with some issues
in older versions of GCC (which is actually fairly well supported on AIX, all
things considered) that made me upgrade to a newer version. However, GCC’s
“fixincludes” tool to try to mend GCC compatibility issues in system header
files in fact mangled them, causing them to be missing some definitions found
in libraries. (Sometimes they were in libc, but never defined in the headers in
the first place!)</p>

<p>Improper use of function pointers was sometimes a problem. Based on the advice
of Bernhard, there was a problem with the function descriptors <code>#ifdefs</code>, which
had caused a mix-up interpreting function pointers as code. Once that had been
fixed, Mono was running generated code on AIX for the first time – quite a
sight to behold!</p>

<h3 id="whats-a-naidne">What’s a “naidnE?”</h3>

<p>One particularly nerve-racking issue that bugged me while trying to bootstrap
was with the Decimal type returning a completely bogus value when dividing,
causing a non-sense overflow condition. Because of constant inlining, this
occurred when building the BCL, so it was hard to put off. With some careful
debugging from my friend, comparing the variable state between x86 and PPC when
dividing a decimal, we had determined exactly where the incorrect endianness
handling had taken place and I had came up with a fix.</p>

<p>While Mono has historically handled different endianness just fine, Mono has
started to replace portions of its own home-grown BCL with CoreFX, (the
open-source Microsoft BCL) and it did not have the same rigor towards
endianness issues. Mono does patch CoreFX code, but it sometimes pulls in new
code that has not had endianness (or other such possible compatibility issues)
worked out yet and thus requires further patching. In this case, the code had
already been fixed for big endian before, but pulling in updated code from
CoreFX had created a new problem with endianness.</p>

<h3 id="whats-rtld_member">What’s “RTLD_MEMBER?”</h3>

<p>On AIX, there are two ways to handle libraries. One is your typical System V
style linking with .so libraries; this isn’t used by default, but can be
forced. The other way is the “native” way to do it, where objects are stored
in an archive (.a) typically used for holding objects used for static linking.
Because AIX always uses position-independent code, multiple objects are
combined into a single object and then inserted into the archive. You can then
access the library like normal. Using this technique, you can even fit multiple
shared objects of the same version into a single archive! This took only
minimal changes to support; I only had to adjust the dynamic library loader to
tell it to look inside of archive files, and some build system tweaks to point
it to the proper archive and objects to look for. (Annoyingly, we have to
hardcode some version names of library objects. Even then, the build system
still needs revision for cases when it assumes that library names are just the
name and an extension.)</p>

<h3 id="whats-undefined-behaviour">What’s “undefined behaviour?”</h3>

<p>When Mono tries to access an object reference, and the reference (a pointer) is
null, (that is, zero) Mono does not normally check to see if the pointer is
null. On most operating systems, when a process accesses invalid memory such as
a null pointer, it sends the process a signal (such as SIGSEGV) and if the
program does not handle that signal, it will terminate the program. Normally,
Mono registers a signal handler, and instead of checking for null, it would
just try to dereference a null pointer anyways to let the signal handler
interrupt and return an exception to managed code instead. AIX doesn’t do that
– it lets programs dereference null pointers anyway! What gives?</p>

<p>Accessing memory via a null pointer is not actually defined by the ANSI C
standards – this is a case of a dreaded <em>undefined behaviour</em>. Mono relied on
the assumption that most operating systems did it in the typical way of sending
a signal to the process. What AIX instead does is to implement a “null page”
mapped at 0x0 and accepts reads and writes to it. (You could also execute from
it, but since all zeroes is an invalid opcode on PowerPC, this does not do much
but throw an illegal instruction signal at the process.) This is a historical
decision, relating back to code optimizations implemented in older IBM
compilers made where they used speculative execution in compiler-generated code
during the 1980s for improved performance when evaluating complex logical
expressions. Because we cannot rely on handling a signal to catch the null
dereference, we can instead force the behaviour to always check if pointers are
null, (normally reserved for runtime debugging) to be on all the time.</p>

<h3 id="whats-so-boring-about-tls">What’s so boring about TLS?</h3>

<p>BoringSSL is required to get modern TLS required by newer websites. The build
system, instead of autotools and make, is CMake based. Luckily, this worked
fine on AIX, though I had to apply some massaging for it to do 64-bit library
mangling. For a while, I was stumped by an illegal instruction error, that
turned out to be due to not linking in pthread to the library, and it not
warning about it.</p>

<p>It turns out that even though BoringSSL was now working, one cipher suite
(secp256r1) was not, so sites using that cipher were broken. To try to test it,
I had gone “yak shaving” to build what was needed for the test harness
according to the README; Ninja and Go. I had a heck of a time trying to build
Go on a PPC Linux system to triage, but as it turned out, I did not actually
need it anyway – Mono had tweaked the build system so that it was not needed
after all; I just had to flip a CMake flag to let it build the tests and run
them manually. After figuring out what exactly was wrong, it turned out to be
an endianness issue in an optimized path. A fix was attempted for it, but in
the end, only disabling it worked and let the cipher run fine on big endian
PowerPC. Since the code came from Google code that has been rewritten in both
BoringSSL and OpenSSL upstream’s latest sources, it is due to be replaced the
next time Mono’s BoringSSL fork gets updated.</p>

<h3 id="what-else">What else?</h3>

<p>I had an issue with I/O getting some spurious and strange issues with
threading. Threads would complain that they had an unexpected errno of 0.
(indicating success) What happened was that AIX does not assume that all
programs are thread-safe by default, so errno was not thread-local. One small
<code>#define</code> later, and that was fixed. (Miguel de Icaza was amused that some
operating systems still consider thread safety to be an advanced feature. 🙂)</p>

<p>We also found a cosmetic issue with uname. Most Unices put their version in the
release field of the uname structure, and things like the kernel type in the
version field. AIX and PASE however, put the major version in the version field,
and the minor version in the release field. A simple <code>sprintf</code> for the AIX case
was enough to fix this.</p>

<p>PASE has many quirks – this necessitated some patches to work around
deficiencies; from bugs to unimplemented functions. I aim to target IBM i 7.1
or newer, so I worked around some bugs that have been fixed in newer versions.
A lot of this I cleaned up with some more preprocessor definitions.</p>

<h2 id="whats-next">What’s next?</h2>

<p>Now that Mono runs on these platforms, there’s still a lot of work left to be
done. The ahead of time compiler needs to be reworked to emit XCOFF-compatible
code, libgdiplus needs to be ported, Roslyn is broken on ppc64be, continuous
integration would be useful to detect build failures, the build system is still
a bit weird regarding AIX libraries, and plenty more where that came from.
Despite all this, the fact the port works well enough already in its current
state should provide a solid foundation to work with, going forward.</p>]]></content><author><name>Calvin Buckley</name></author><category term="news" /><category term="porting" /><summary type="html"><![CDATA[]]></summary></entry><entry><title type="html">Mono and WebAssembly - Updates on Static Compilation</title><link href="https://www.mono-project.com/news/2018/01/16/mono-static-webassembly-compilation/" rel="alternate" type="text/html" title="Mono and WebAssembly - Updates on Static Compilation" /><published>2018-01-16T00:00:00+00:00</published><updated>2018-01-16T00:00:00+00:00</updated><id>https://www.mono-project.com/news/2018/01/16/mono-static-webassembly-compilation</id><content type="html" xml:base="https://www.mono-project.com/news/2018/01/16/mono-static-webassembly-compilation/"><![CDATA[<p>As you may know we have been working on <a href="/news/2017/08/09/hello-webassembly">bringing Mono to the
WebAssembly platform</a>.
As part of the effort we have been pursuing two strategies; one that
uses the new Mono IL interpreter to run managed code at runtime, and
one that uses full static (AOT) compilation to create one <code>.wasm</code> file
that can be executed natively by the browser.</p>

<p>We intend the former to be used for quickly reloading C# code and
prototyping and the latter for publishing your final application, with
all the optimizations enabled.  The interpreter work has now been
integrated into Mono’s source code and we are using it to develop,
port and tune the managed libraries to work on WebAssembly.</p>

<p>This post is about the progress that we have been making on doing
static compilation of .NET code to run on WebAssembly.</p>

<p><img src="/images/2018-01-16-mono-static-webassembly-compilation/diagram1.png" alt="mono-wasm in action" /></p>

<p>WebAssembly static compilation in Mono is orchestrated with the
<code>mono-wasm</code> command-line tool. This program takes IL assemblies as
input and generates a series of files in an output directory, notably
an <code>index.wasm</code> file containing the WebAssembly code for your
assemblies as well as all other dependencies (the Mono runtime, the C
library and the <code>mscorlib.dll</code> library).</p>

<pre><code>$ cat hello.cs
class Hello {
  static int Main(string[] args) {
    System.Console.WriteLine("hello world!");
    return 0;
  }
}
$ mcs -nostdlib -noconfig -r:../../dist/lib/mscorlib.dll hello.cs -out:hello.exe
$ mono-wasm -i hello.exe -o output
$ ls output
hello.exe        index.html        index.js        index.wasm        mscorlib.dll
</code></pre>

<p><code>mono-wasm</code> uses a version of the Mono compiler that, given C#
assemblies, generates LLVM bitcode suitable to be passed to the LLVM
WebAssembly backend. Similarly, we have been building the Mono runtime
and a C library with a version of <code>clang</code> that also generates LLVM
WebAssembly bitcode.</p>

<p>Until recently, <code>mono-wasm</code> was linking all the bitcode into a single
LLVM module then performing the WebAssembly code generation on
it. While this created a functional <code>.wasm</code> file, this had the
downside of taking a significant amount of time (half a minute on a
recent MacBook Pro) every time we were building a project as a lot of
code was in play. Some of the code, the runtime bits and the
<code>mscorlib.dll</code> library, never changed and yet were still being
processed for WebAssembly code generation every time.</p>

<p>We were thrilled to hear in late November of last year that the LLVM
linker (<code>lld</code>) <a href="https://lld.llvm.org/WebAssembly.html">was getting WebAssembly
support</a>.</p>

<p>Since then, we changed our <code>mono-wasm</code> tool to perform incremental
compilation of project dependencies into separate <code>.wasm</code> files, and
we integrated <code>lld</code>’s new WebAssembly driver in the tool. Thanks to
this approach, we now perform WebAssembly code generation only when
required, and in our testing builds now complete in less than a second
once the dependencies (runtime bits and <code>mscorlib.dll</code>) have already
been compiled into WebAssembly.</p>

<p><img src="/images/2018-01-16-mono-static-webassembly-compilation/diagram2.png" alt="mono-wasm's new linking phase" /></p>

<p>Additionally, <code>mono-wasm</code> used to use the LLVM WebAssembly target to
create source files that would then be passed to the
<a href="https://github.com/WebAssembly/binaryen">Binaryen</a> toolchain to
create the <code>.wasm</code> code.  We have been testing the backend’s ability
to generate <code>.wasm</code> object files directly (with the
<code>wasm32-unknown-unknown-wasm</code> triple) and so far it seems promising
enough that we changed <code>mono-wasm</code> accordingly. We also noticed a
slight decrease in build time.</p>

<table>
  <thead>
    <tr>
      <th> </th>
      <th>Old toolchain</th>
      <th>New toolchain (First Compile)</th>
      <th>New toolchain (Rebuild)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Full application build</td>
      <td>~40s</td>
      <td>~30s</td>
      <td>&lt;1s</td>
    </tr>
    <tr>
      <td>Hello World program</td>
      <td>~40s</td>
      <td>&lt;1s</td>
      <td>&lt;1s</td>
    </tr>
  </tbody>
</table>

<p>There is still a lot of work to do on bringing C# to WebAssembly, but
we are happy with this new approach and the progresses we are
making. Feel free to watch this space for further updates. You can
also track the work on the <a href="https://github.com/lrz/mono-wasm">mono-wasm GitHub
repository</a>.</p>

<p>For those of you that want to take this for a spin you can download a
<a href="https://github.com/lrz/mono-wasm/releases">preview release</a>, unzip
and run “make” in the samples.  This currently requires MacOS High
Sierra to run.</p>]]></content><author><name>Laurent Sansonetti</name></author><category term="news" /><category term="runtime" /><summary type="html"><![CDATA[As you may know we have been working on bringing Mono to the WebAssembly platform. As part of the effort we have been pursuing two strategies; one that uses the new Mono IL interpreter to run managed code at runtime, and one that uses full static (AOT) compilation to create one .wasm file that can be executed natively by the browser.]]></summary></entry><entry><title type="html">Mono’s New .NET Interpreter</title><link href="https://www.mono-project.com/news/2017/11/13/mono-interpreter/" rel="alternate" type="text/html" title="Mono’s New .NET Interpreter" /><published>2017-11-13T00:00:00+00:00</published><updated>2017-11-13T00:00:00+00:00</updated><id>https://www.mono-project.com/news/2017/11/13/mono-interpreter</id><content type="html" xml:base="https://www.mono-project.com/news/2017/11/13/mono-interpreter/"><![CDATA[<p>Mono is complementing its Just-in-Time compiler and its static
compiler with a .NET interpreter allowing a few new ways of running
your code.</p>

<p>In 2001 when the Mono project started, we wrote an interpreter for the
.NET instruction set and we used this to bootstrap a self-hosted .NET
development environment on Linux.</p>

<p>At the time we considered the interpreter a temporary tool that we
could use while we built a Just-in-Time (JIT) compiler. The
interpreter (<code>mint</code>) and the JIT engine (<code>mono</code>) existed side-by-side
until we could port the JIT engine to all the platforms that we
supported.</p>

<p>When generics were introduced, the engineering cost of keeping both
the interpreter and the JIT engine was not worth it, and we did not
see much value in the extra work to keep it around, so we removed the
interpreter.</p>

<p>We later introduced full static compilation of .NET code. This is a
technology that we introduced to target platforms that do not allow
for dynamic code generation. iOS was the main driver for this, but it
opened the doors to allow Mono to run on gaming consoles like the
PlayStation and the Xbox.</p>

<p>The main downside of full static compilation is that a completely new
executable has to be recreated every time that you update your
code. This is a slow process and one that was not suitable for
interactive development that is practiced by some.</p>

<p>For example, some game developers like to adjust and tweak their game
code, without having to trigger a full recompilation. The static
compilation makes this scenario impractical, so they resort to
embedding a scripting language into their game code to quickly iterate
and tune their projects.</p>

<p>This lack of .NET dynamic capabilities also prevented many interesting
uses of .NET as a teaching or prototyping tool in these environments.
Things like Xamarin Workbooks, or simple scripting could not use .NET
languages and had to resort to other solutions on these platforms.</p>

<p>Frank Krueger, while building his <a href="http://continuous.codes/">Continuous
IDE</a>, needed such environment on iOS so much
that he wrote his own .NET interpreter using F# to bring his vision of
having a complete development environment for .NET on the iPad.</p>

<p>To address these issues, and to support some internal Microsoft
products, we brought Mono’s interpreter back to life, and it is back
with a twist.</p>

<h2 id="new-mono-interpreter">New Mono Interpreter</h2>

<p>We resuscitated Mono’s old interpreter and upgraded its .NET support,
adding the support for generics and upgraded it to run .NET as it
exists in 2017.  Next is adding support for mixed-mode
execution.</p>

<p>It is one of the ways that Mono runs on WebAssembly today for example
(the other being the static compilation using LLVM)</p>

<p>The interpreter is now part of mainline Mono and it passes a large
part of our extensive test suites, you can use it today when building
Mono from source code, like this:</p>

<pre><code class="language-bash">$ mono --interpreter yourassembly.exe
...
</code></pre>

<h2 id="mixed-mode-execution">Mixed Mode Execution</h2>

<p>While the interpreter alone is now in great shape, we are currently
working on a configuration that will allow us to mix both interpreted
code with statically compiled code or Just-in-Time compiled code, we
call this mixed mode execution.</p>

<p>For platforms like iOS, PlayStation and Xbox, this means that you can
precompile your core libraries or core application, and still support
loading and executing code dynamically.  Gaining the benefits of
having all your core libraries optimized with LLVM, but still have the
flexibility of running some dynamic code.</p>

<p>This will allow game developers to prototype, experiment and tweak
their games using .NET languages on their system without having to
recompile their applications.</p>

<p>It will open the doors for scriptable applications on device using
.NET languages as well.</p>

<h2 id="future-work">Future work</h2>

<p>We are extending the capabilities of the interpreter to handle various
interesting scenarios. These are some of the projects ahead of us:</p>

<h2 id="improvements-for-statically-compiled-mono">Improvements for Statically Compiled Mono</h2>

<p>The full ahead-of-time compilation versions of Mono (iOS, Consoles) do
not ship with an implementation of <code>System.Reflection.Emit</code>. This made
sense as the capability could not be supported, but now that we have
an interpreter, we can.</p>

<p>There are several uses for this.</p>

<p>The <code>System.Linq.Expressions</code> API which is used extensively by many
advanced scenarios like Entity Framework or by users leveraging the C#
compiler to parse expressions into expression trees, you have probably
seen the code in scenarios like this:</p>

<pre><code class="language-csharp">Expression sum = a + b;
var adder = sum.Compile ();
adder ();
</code></pre>

<p>In Full AOT scenarios, the way that we made Entity Framework and the
above work was to ship an interpreter for the above <code>Expression</code>
class. This expression interpreter has limitations, and is also a
large one.</p>

<p>By enabling <code>System.Reflection.Emit</code> powered by the interpreter we can
remove a lot of code.</p>

<p>This will also allow the scripting languages that have been built for
.NET to work on statically compiled environments, like IronPython,
IronRuby and IronScheme.</p>

<p>To allow this, we are completing the work for mixed-mode
execution. That means that the interpreted code complements existing
statically compiled .NET code.</p>

<h2 id="better-isolation">Better Isolation</h2>

<p>Earlier on this post, I mentioned that one of the idioms that we
previously failed to address was the hot-reloading of code by
developers that deployed their app and tweaked their game code (or
their code for that matter) live.</p>

<p>We are completing our support for AppDomains to enable this scenario.</p>

<h2 id="researching-mixed-mode-options">Researching Mixed Mode Options</h2>

<p>The interpreter is a lighter option to run some code. We found that
certain programs can run faster by being interpreted than being
executed with the JIT engine.</p>

<p>We intend to explore a mixed mode of execution, sometimes called
tiered compilation.</p>

<p>We could instruct the interpreter to execute code that is known to not
be performance sensitive - for example, static constructors or other
initialization code that only runs once to reduce both memory usage,
generated code usage and execution time.</p>

<p>Another consideration is to run code in interpreted mode, and if we
exceed some threshold switch to a JIT compiled implementation of the
method, or use attributes to annotate methods that are worth the
trouble and methods that are not worth the trouble optimizing.</p>]]></content><author><name>Miguel de Icaza</name></author><category term="news" /><category term="runtime" /><summary type="html"><![CDATA[Mono is complementing its Just-in-Time compiler and its static compiler with a .NET interpreter allowing a few new ways of running your code.]]></summary></entry><entry><title type="html">Mono’s 2017 Google Summer of Code</title><link href="https://www.mono-project.com/news/2017/10/05/google-summer-of-code/" rel="alternate" type="text/html" title="Mono’s 2017 Google Summer of Code" /><published>2017-10-05T00:00:00+00:00</published><updated>2017-10-05T00:00:00+00:00</updated><id>https://www.mono-project.com/news/2017/10/05/google-summer-of-code</id><content type="html" xml:base="https://www.mono-project.com/news/2017/10/05/google-summer-of-code/"><![CDATA[<p>This Summer of Code, the Mono project had many exciting submissions. It’s been great to see what our applicants have been able to accomplish. Some were very familiar with the codebases they worked on, while others had to learn quickly. Let’s summarize how they spent this summer.</p>

<h2 id="cppsharp-defect-removal-and-general-feature-work">CppSharp Defect Removal And General Feature Work</h2>

<p><a href="http://www.mono-project.com/community/google-summer-of-code/reports/2017/cppsharp-mohit-mohta/">Mohit Mohta</a> and <a href="http://www.mono-project.com/community/google-summer-of-code/reports/2017/cppsharp-kimon-topouzidis/">Kimon Topouzidis</a> chose to address a number of bugs and add features to the code of CppSharp. Std::string was added, stacks were fixed, options were added, structure packing was added, and primitive types support was improved. They both seem to have learned a lot about the workflow of methodical debugging of systems code.</p>

<h2 id="clang-sanitizers">Clang Sanitizers</h2>

<p>Many software bugs don’t result in immediate errors and crashes. Some corrupt program state in such a way that a cryptic error is seen much later. In the worst case, each such delayed crash may have a different stack trace. Many of these bugs have root causes that can be spotted in a running program the second they go wrong. The tooling to do so has only recently been able to spot race conditions, which can be some of the worst of these bugs. Clang has integrated a number of such sanitizers.</p>

<p><a href="http://www.mono-project.com/community/google-summer-of-code/reports/2017/clang-sanitizers/">Armin Hasitzka</a> chose to use clang’s runtime sanitizers for race conditions and for memory safety to automatically catch Mono bugs. In his efforts, he ran into false positives and legitimate bugs alike. He fixed a number of bugs, helped silence false positives, and left behind infrastructure to automatically catch regressions as they appear.</p>

<h2 id="cppsharp-qt-bindings-and-maintenance">CppSharp Qt Bindings And Maintenance</h2>

<p><a href="http://www.mono-project.com/community/google-summer-of-code/reports/2017/cppsharp-dimitar-dobrev/">Dimitar Dobrev</a> is familiar to the Mono project. He has done the Google Summer of Code with Mono in 2015, and has helped maintain CppSharp since.</p>

<p>This summer, he sought to commit his time to developing the Qt bindings further. In the development of CppSharp, the problem of mapping C# types onto C++ generics arose. There were many potential solutions, but very few retained the feeling of the underlying API. After some experimentation, the hard problems were solved.</p>

<p>As the summer came to an end, he fixed the minor issues that arose during tests of QtSharp. The burden of maintaining the project and responding to bugs from the community did not stop for Dimiar, resulting in partial completion of milestones yet significant overall contribution. Development of QtSharp proceeds alongside his ongoing maintenance work and contributions.</p>

<h2 id="monodevelop-cc-extension-feature-enhancements">MonoDevelop C/C++ Extension Feature Enhancements</h2>

<p>The CBinding extension for MonoDevelop adds a lot of great functionality for working with C and C++ projects. It is still a work in progress, and <a href="http://www.mono-project.com/community/google-summer-of-code/reports/2017/monodevelop-c-cpp-extension/">Anubhav Singh</a> wanted to add some more functionality. He focused on bringing support for Windows compilers and for CMake. He also chose this moment to update the extension to reflect the newer APIs of MonoDevelop. In the process, he had to begin the process of upstreaming some changes to MonoDevelop.</p>

<h2 id="c-compiler-caching-with-cscache">C# Compiler Caching with CSCache</h2>

<p>Something often mentioned around a warm laptop with spinning fans is how nice C developers have it. CCache enables someone to recompile large C projects after minor modifications in a very small amount of time. Going beyond the build system skipping recompilation, the system compiler is wrapped by a program that spits back the old output in a fraction of the time that a compiler takes. This is a trick that managed languages haven’t learned until now.</p>

<p><a href="http://www.mono-project.com/community/google-summer-of-code/reports/2017/cscache-report/">Daniel Calancea</a> created a tool which wraps mcs and understands the commands sent to it. If it is invoked with the same files and the same options twice, it checks that all of the hashes of all of the files are the same between runs. If so, it returns the output of the C# compiler the first time. Equally important is that this tool will return the same return codes as the first run, and will integrate as seamlessly into any build system as ccache does. It even reports the same warnings that the initial compiler did.</p>

<p>Daniel published this tool for Windows and Linux to Nuget.</p>

<h2 id="import-of-systemiopipespipestream-from-corefx">Import of System.IO.Pipes.PipeStream from CoreFX</h2>

<p>Mono’s implementation of System.IO.Pipes has historically not had some features available to the CLR. After msbuild was made open source, users found that Mono unfortunately could not build in parallel because of the API differences. CoreFX brought with it the promise of a System.IO.Pipes.PipeStream that would enable parallel msbuild. CoreFX’s API surface was not strictly a superset of Mono’s though. Mono implemented a couple of endpoints that CoreFX did not, and we used those endpoints in other places in the BCL.</p>

<p><a href="http://www.mono-project.com/community/google-summer-of-code/reports/2017/corefx-System.IO.Pipes/">Georgios Athanasopoulos</a> chose to do the work required to make Mono work with CoreFX’s PipeStream. Modifying both CoreFX and Mono was required. Mono’s build system had to choose to use the new implementation files, rather than looking for them in the BCL directory. His work was a success. Finishing early, he chose to experimentally enable a parallel msbuild and test it. Things seem to be mostly working.</p>

<h2 id="lamdba-debugger-support">Lamdba Debugger Support</h2>

<p>Often when debugging C# code in the middle of a large project, it’s important to invoke code to understand how variables are behaving in a segment of code. Sometimes, the code that one wishes to invoke hasn’t been written yet. The developer is left squinting at variables, invoking existing methods, and manually running code in their head. Much better would be to enable the developer to write a new function and invoke it on the variables in question. Interpreted languages offer support for this without much trouble usually because code doesn’t have as much metadata associated with it, and because they have integrated compilers for the debugged languages.</p>

<p>This summer, <a href="http://www.mono-project.com/community/google-summer-of-code/reports/2017/lambda-support-in-debugger-expression-evaluator/">Haruka Matsumoto</a> worked on a system that enables developers to use these arbitrary code snippets entered into the debugger. Mono runs the debugger and the debuggee in separate running instances of the runtime. As the running mono runtime for the application being debugged doesn’t have access to a C# compiler, this code has to be compiled by the debugger. The debugger uses Roslyn to compile the code segments, and this assembly is sent to the debugged application’s runtime.</p>

<p>This is made more difficult by the fact that the debugger is trying to run a Lambda that has access to the variables and methods defined in the functions the debugger is currently debugging. Shorter method names need to resolve to what they would if the original function had used them, and variables should be accessible by name. Issues with private types are potentially unsolvable without special casing, as mono prevents arbitrary code from modifying private fields. Haruka handled these and other difficult considerations, and delivered a very strong prototype of Lambda support in the integrated runtime debugger. It should be immediately useful for anybody who spends a lot of time using mono to debug C# code.</p>

<h2 id="import-synchronization-primitives-from-corert">Import Synchronization Primitives from CoreRT</h2>

<p>It is often the case that small differences in the implementations of core runtime functions can result in perceived bugs introduced by switching runtimes. The differences are due to depending on API behavior that may not be entirely defined by the specification, but works in a certain case on a certain machine. This sensitivity is nowhere more baffling to debug than around threading and synchronization primitives. The .NET Core Project contains an open-source, cross-platform implementation of C# synchronization primitives. We expect this to receive much community development and user testing. We hoped to import them to gain both consistent behavior and quality.</p>

<p>This summer, <a href="https://github.com/mono/mono/pull/5054">Alexander Efremov</a> imported EventWaitHandle, AutoResetEvent, ManualResetEvent, Mutex and Semaphore into Mono. He both manually integrated these libraries into Mono and automated the process of building them. System.Private.CoreLib.Native was successfully added to mono. System.Threading was identified as the next API to import, in order to enable importing Thread from CoreFX.</p>]]></content><author><name>Alexander Kyte</name></author><category term="news" /><category term="gsoc" /><summary type="html"><![CDATA[This Summer of Code, the Mono project had many exciting submissions. It’s been great to see what our applicants have been able to accomplish. Some were very familiar with the codebases they worked on, while others had to learn quickly. Let’s summarize how they spent this summer.]]></summary></entry><entry><title type="html">Mono 5.2 is out!</title><link href="https://www.mono-project.com/news/2017/08/14/mono-5-2-is-out/" rel="alternate" type="text/html" title="Mono 5.2 is out!" /><published>2017-08-14T00:00:00+00:00</published><updated>2017-08-14T00:00:00+00:00</updated><id>https://www.mono-project.com/news/2017/08/14/mono-5-2-is-out</id><content type="html" xml:base="https://www.mono-project.com/news/2017/08/14/mono-5-2-is-out/"><![CDATA[<p>Mono 5.2 is out in the <a href="/download/stable/">stable channel</a> !</p>

<p>Check out our <a href="/docs/about-mono/releases/5.2.0/">release notes</a>
for more details about what is new on Mono 5.2.</p>

<p>This release was made up of nearly 1000 commits since Mono 5.0 and is the result
of many months of work by the Mono team and contributors!</p>]]></content><author><name>Alexander Köplinger</name></author><category term="news" /><category term="releases" /><summary type="html"><![CDATA[Mono 5.2 is out in the stable channel !]]></summary></entry><entry><title type="html">Mono’s New Profiler API</title><link href="https://www.mono-project.com/news/2017/08/14/new-profiler-api/" rel="alternate" type="text/html" title="Mono’s New Profiler API" /><published>2017-08-14T00:00:00+00:00</published><updated>2017-08-14T00:00:00+00:00</updated><id>https://www.mono-project.com/news/2017/08/14/new-profiler-api</id><content type="html" xml:base="https://www.mono-project.com/news/2017/08/14/new-profiler-api/"><![CDATA[<p>As part of our ongoing efforts to improve Mono’s profiling infrastructure, in
Mono 5.6, we will be shipping an overhaul of Mono’s profiler API. This is the
part of Mono’s embedding API that deals with instrumenting managed programs for
the purpose of collecting data regarding allocations, CPU usage, code coverage,
and other data produced at runtime.</p>

<p>The old API had some limitations that prevented some features and
capabilities from being implemented.   The upgrade to the API will allow us to:</p>

<ul>
  <li>Reconfigure the profiling features at runtime</li>
  <li>Look at the values of incoming parameters and return values.</li>
  <li>Ability to instrument the managed allocators, thus allowing these to be profiled.</li>
</ul>

<p>This is what we did.</p>

<h2 id="reconfigure-profiling-at-runtime">Reconfigure Profiling at Runtime</h2>

<p>We wanted the ability to reconfigure the profiling option at runtime.
This was not possible with the old API because none of the API
functions took an argument representing the profiler whose options
should be changed.</p>

<p>This means that it was only possible to change options of the most
recently installed profiler, and this was not guaranteed to be the one you
wanted.   Additionally, doing so it was not thread safe.</p>

<p>Why would we want to change profiling options at runtime, you might
wonder?  Suppose you know that only a particular area of your program
has performance issues and you’re only interested in data gathered
while your program is executing that code.  With this capability, you
can turn off profiling features such as allocations and statistical
sampling until you get to the point you want to profile, and then turn
them on programmatically. This can significantly reduce the noise
caused by unneeded data in a profiling session.</p>

<h2 id="call-context-introspection">Call Context Introspection</h2>

<p>Call context introspection allows a profiler to instrument the
prologue and/or epilogue of any method and gain access to arguments
(including the <code>this</code> reference), local variables, and the return
value.</p>

<p>This opens up countless possibilities for instrumenting framework
methods to learn how a program is utilizing facilities like the thread
pool, networking, reflection and so on.  It can also be useful for
debugging, especially if dealing with assemblies for which the source
code is not available.</p>

<h2 id="instrumenting-managed-allocators">Instrumenting Managed Allocators</h2>

<p>Another improvement we were able to make thanks to the redesigned API was to
use instrumented managed allocators when profiling. In the past, we would
disable managed allocators entirely when profiling. This would slow down
allocation-heavy programs significantly. Now, we insert a call back to the
profiler API at the end of managed allocators if profiling is enabled.</p>

<h2 id="simpler-to-work-with">Simpler to Work With</h2>

<p>On top of these major features, the new API is also simply more pleasant to
use. In  particular, you no longer have to worry about setting event flags; you
simply install a callback and you will get events. Also, you no longer have to
use callback installation functions which take multiple callback arguments.
Every kind of callback now has exactly one function to install it. This means
you will no longer have code such as
<code>mono_profiler_install_assembly (NULL, NULL, load_asm, NULL);</code> where it can be
unclear which argument corresponds to which callback. Finally, several unused,
deprecated, or superseded features and callbacks have been removed.</p>

<h2 id="breaking-change">Breaking Change</h2>

<p>The new API completely replaces the old one, so this is a breaking change. We
try very hard to not break API/ABI compatibility in Mono’s embedding API, but
after much consideration and evaluation of the alternatives, a breaking change
was deemed to be the most sensible way forward. To aid with the transition to
the new API, Mono will detect and refuse to load profiler modules that use
the old API. Developers who wish to support both the old and new APIs by
compiling separate versions of their profiler module may find the new
<code>MONO_PROFILER_API_VERSION</code> macro useful.</p>

<p>A presentation with more details is available in
<a href="https://dl.xamarin.com/uploads/rpi4dr14sjp/Mono_New_Profiler_API.pptx">PowerPoint</a>
and
<a href="https://dl.xamarin.com/uploads/jvbvew1yo5e/Mono's%20New%20Profiler%20API.pdf">PDF</a>
formats.</p>]]></content><author><name>Alex Rønne Petersen</name></author><category term="news" /><category term="profiler" /><category term="runtime" /><summary type="html"><![CDATA[As part of our ongoing efforts to improve Mono’s profiling infrastructure, in Mono 5.6, we will be shipping an overhaul of Mono’s profiler API. This is the part of Mono’s embedding API that deals with instrumenting managed programs for the purpose of collecting data regarding allocations, CPU usage, code coverage, and other data produced at runtime.]]></summary></entry></feed>