11

How to run compiled files directly using Google Native Client (PNaCl)? It tried checking their documentation. It said that -

Native Client is a sandbox for running compiled C and C++ code in the browser efficiently and securely, independent of the user’s operating system.

But in their documentation, they only deal with sources of the application. Is there any way to run compiled code directly? I want to run files with .exe and .deb extensions


I'm not limiting the answer to Native Client. Any mechanism which can do that sort of work will work for me.

Arulx Z
  • 193
  • 1
  • 13
  • 2
    An .exe would only work on windows. So it will not be possible to run that independent of the user’s operating system. – wimh Aug 29 '15 at 10:54
  • 1
    In Linux, a `.deb` file is not a directly executable format but is an archive unpacked by the Debian packaging system to install something on a Linux system. – Paul Sep 08 '15 at 03:32
  • @Wimmel Systems are Turing complete. Hence it's not impossible. – Arulx Z Oct 24 '15 at 08:32

2 Answers2

16

You can't run pre-compiled code within NaCl or PNaCl. You have to use the compilers provided by the SDK. There are three main reasons for this:

  • NaCl is an execution sandbox which relies on crafting machine code (x86-32, x86-64, ARM, MIPS) in a very particular way. This is regular machine code from the CPU's point of view, but allows the sandbox to run a validator and make sure that the code can't do anything malicious. This is called Software Fault Isolation, and is explained in this paper. The other ISA sandboxes are also documented.
  • PNaCl targets NaCl, but is an architecture-agnostic intermediate representation. This means that you ship what can be thought of as bytecode, and the browser figures out which type of machine code (x86-32, x86-64, ARM, MIPS) to generate based on the user's machine. The developer doesn't generate 4 binaries.
  • In both above cases, the code can execute as-is on Windows, MacOSX, Linux, ChromeOS, and (while not usually shipping) Android. This means that the NaCl sandbox presents itself as an operating system, and offers the same APIs. These APIs are different from other OSes, though they're pretty close to POSIX especially if you use nacl_io.

The above points require that you use the compilers provided by the SDK.

It is technically possible to run binaries built for other architectures or operating systems since the system is Turing-complete. That's what QEMU does, what Rosetta did, what Transmeta did, and what the Android Runtime for Chome (ARC) enables. This usually requires binary translation and emulation of all operating system calls. This is technically difficult to implement, and often has severe performance cost. I do not recommend exploring this option.

JF Bastien
  • 6,673
  • 2
  • 26
  • 34
  • 5
    Addressing the bounty which states "I want to know if is there any way to still do the task using other mechanisms": I'm the tech lead of the PNaCl project. If you find a way to run a `.exe` or `.deb` file beyond using a binary translator as App Runtime for Chrome (ARC) does then do let us know: you've found an exploit on Chrome and can probably claim from the Chrome Vulnerability Reward Program! – JF Bastien Aug 31 '15 at 17:46
  • I mean I am not just limiting the answer to chrome or PNaCl. I want to know if there is any other way apart from that. Maybe even a native app which can do that. I'll surely keep you updated if I found any way to do that using chrome. By the way, you've reached a milestone by creating PNaCl. Do keep up the good work! – Arulx Z Sep 01 '15 at 11:21
  • @ArulxZ if you're willing to run outside of the web then you can use plugins, but that's beyond the original question. It still won't be portable (cross-ISA and cross-OS) without some serious emulation. – JF Bastien Sep 01 '15 at 15:13
4

As @JFBastien pointed out, emulation is the only option to execute pre-compiled native code in the browser environment. But it is an option nevertheless. Depending on your demands on performance, it might even be a viable option.

Click here for example to boot up an emulator running Windows (a very old version though) in your browser.

From the menu pick, for example, notepad.exe (using the cursor down key on your keyboard) and hit enter. There you have it: an unmodified, precompiled, native notepad.exe running inside your browser! (and probably even faster than back in the day when this OS was new).

There are a lot of emulators written in Javascript all around the web. Running a small Linux distribution with usable performance and even with networking(!), graphics and sound is actually possible. Check out the OpenRISC emulator. You can even run an ssh daemon and log into it from your local machine!

regular
  • 7,697
  • 1
  • 14
  • 19