OLD | NEW |
(Empty) | |
| 1 .. _overview: |
| 2 |
| 3 ################## |
| 4 Technical Overview |
| 5 ################## |
| 6 |
| 7 .. contents:: |
| 8 :local: |
| 9 :backlinks: none |
| 10 :depth: 2 |
| 11 |
| 12 Introduction |
| 13 ============ |
| 14 |
| 15 **Native Client** is an open-source technology for running native compiled code |
| 16 in the browser, with the goal of maintaining the OS portability and safety |
| 17 that people expect from web apps. Native Client expands web programming |
| 18 beyond JavaScript, enabling developers to enhance their web applications |
| 19 using their preferred language. This document describes a few of the key |
| 20 benefits of Native Client, as well as current limitations, common use cases, |
| 21 and tips for getting started with Native Client. |
| 22 |
| 23 Google has implemented the open-source |
| 24 `Native Client project <http://www.chromium.org/nativeclient>`_ in the Chrome |
| 25 browser on Windows, Mac, Linux, and Chrome OS. The **Native Client Software |
| 26 Development Kit (SDK)**, itself an open-source project, lets developers create |
| 27 web applications that use Native Client and run in Chrome across OS |
| 28 platforms. In addition to software libraries, the SDK includes a toolchain |
| 29 tailored to generate executable Native Client code, as well as a variety of |
| 30 code examples and documentation. |
| 31 |
| 32 A web application that uses Native Client generally consists of a combination |
| 33 of JavaScript, HTML, CSS, and a Native Client module that is written in a |
| 34 language supported by a Native Client compiler. The Native Client SDK |
| 35 currently supports C and C++. As compilers for additional languages are |
| 36 developed, the SDK will be updated to support those languages as well. |
| 37 |
| 38 .. image:: /images/NaclBlock.png |
| 39 |
| 40 Why use Native Client? |
| 41 ====================== |
| 42 |
| 43 The Native Client open-source technology is designed to run native compiled |
| 44 code securely inside browsers. Native Client puts web applications on the same |
| 45 playing field as local applications, providing the raw speed needed to compete |
| 46 with traditional software like 3D games, video editing, and other applications. |
| 47 Native Client also gives languages like C and C++ (and eventually others as |
| 48 well) the same level of portability and safety that JavaScript provides on the |
| 49 web today. |
| 50 |
| 51 Here are some of the key features that Native Client offers: |
| 52 |
| 53 * **Graphics, audio, and much more:** Run native code modules that render 2D |
| 54 and 3D graphics, play audio, respond to mouse and keyboard events, run on |
| 55 multiple threads, and access memory directly---all without requiring |
| 56 the user to install a plug-in. |
| 57 * **Portability:** Write your apps once and you'll be able to run them on |
| 58 Windows, Linux, Mac, and Chrome OS. |
| 59 * **Security:** Installing a desktop app or a browser plug-in can present |
| 60 serious security risks and deter potential users. Native Client uses a |
| 61 double sandbox designed to protect resources on the user's system. This |
| 62 framework offers the safety of traditional web apps in addition to the |
| 63 performance benefits of native compiled code, without requiring users to |
| 64 install a plug-in. |
| 65 * **Easy migration path to the web:** Many developers and companies have |
| 66 years of work invested in existing desktop applications. Native Client |
| 67 makes the transition from desktop app to web app significantly easier |
| 68 because Native Client supports C and C++ (and will continue to add more |
| 69 languages). |
| 70 * **Performance:** Native Client allows your app to run at a speed comparable |
| 71 to a desktop app. This capability enables demanding applications such as |
| 72 console-quality games to run inside the browser. |
| 73 |
| 74 Common use cases |
| 75 ================ |
| 76 |
| 77 Native Client enables you to extend web apps running in the browser with custom |
| 78 features and proprietary code. Typical use cases for Native Client include the |
| 79 following: |
| 80 |
| 81 * **Existing software components:** With its native language support |
| 82 (currently C and C++), Native Client enables you to reuse current software |
| 83 modules in a web app---you don't need to spend time reinventing and debugging |
| 84 code that's already proven to work well. |
| 85 * **Legacy desktop applications:** Native Client provides a smooth migration |
| 86 path from desktop application to web app. You can port and recompile |
| 87 existing code for the computation engine of your application directly to |
| 88 Native Client, and need repurpose only the user interface and event |
| 89 handling portions to the new browser platform. Native Client allows you to |
| 90 embed existing functionality directly into the browser at the same time |
| 91 your application takes advantage of things the browser does well: handling |
| 92 user interaction and processing events, based on the latest developments in |
| 93 HTML5. |
| 94 * **Enterprise applications that require heavy computation:** Native Client |
| 95 handles the number crunching required by large-scale enterprise apps. To |
| 96 ensure protection of user data, Native Client enables you to build complex |
| 97 cryptographic algorithms directly into the browser so that unencrypted data |
| 98 never goes out over the network. |
| 99 * **Multimedia apps:** Codecs for processing sounds, images, and movies can |
| 100 be added to the browser in a Native Client web app. |
| 101 * **Games:** Native Client enables a web app to run close to native speed, |
| 102 reusing existing multithreaded/multicore C/C++ code bases, combined with |
| 103 low-level access to low-latency audio and (coming soon) networking APIs and |
| 104 OpenGL ES with programmable shaders. Programming to Native Client also |
| 105 enables your binary to run unchanged across many platforms. Native Client |
| 106 is a natural fit for running a physics engine or artificial intelligence |
| 107 module that powers a sophisticated web game. |
| 108 |
| 109 Compiling existing native code for your app helps protect the investment you've |
| 110 made in research and development. In addition to the protection offered by |
| 111 Native Client compile-time restrictions, users benefit from the security |
| 112 offered by its runtime validator. The validator decodes modules and limits the |
| 113 instructions that can run in the browser, and the sandboxed environment proxies |
| 114 system calls Users can run Native Client apps without installing a new |
| 115 application or a browser plug-in, and you can update your app without requiring |
| 116 user intervention. |
| 117 |
| 118 Current limitations |
| 119 =================== |
| 120 |
| 121 Native Client currently has the following limitations: |
| 122 |
| 123 * no support for hardware exceptions |
| 124 * no support for process creation / subprocesses |
| 125 * no support for raw TCP/UDP sockets (analogous versions---websockets for TCP |
| 126 and peer connect for UDP---are in the works and will be available soon) |
| 127 * no support for query to available memory |
| 128 * inline assembly must be compatible with the Native Client validator (you |
| 129 can use the ncval utility in the SDK to check) |
| 130 |
| 131 Some of these limitations are required to execute code safely in a browser. To |
| 132 understand the reasons for these limitations read the `Google Chrome technical |
| 133 booklet <http://www.google.com/googlebooks/chrome/index.html>`_ and the `Native |
| 134 Client technical paper |
| 135 <http://src.chromium.org/viewvc/native_client/data/docs_tarball/nacl/googleclien
t/native_client/documentation/nacl_paper.pdf>`_ |
| 136 (PDF). |
| 137 |
| 138 How Native Client works |
| 139 ======================= |
| 140 |
| 141 In order to be safe and portable for the web, the executable native code that |
| 142 is produced must be sandbox-safe. More specifically, it cannot cause any |
| 143 malicious or harmful effects on an end-user's machine. Native Client achieves |
| 144 this security through a number of techniques, including API restrictions and |
| 145 use of a modified compilation process. |
| 146 |
| 147 Even though it provides software-based fault isolation on the x86 architecture, |
| 148 Native Client still maintains the speed of native code---or thereabout. As of |
| 149 current timings, the Native Client sandbox adds only about 5% overhead, which |
| 150 makes it possible to do serious rendering and serious number-crunching in the |
| 151 browser without a big performance hit. |
| 152 |
| 153 A compiled Native Client module (.nexe file) is loaded into a web page through |
| 154 an <embed> element. Once uploaded to a server, the .html page and the .nexe |
| 155 file(s) define a web application. |
| 156 |
| 157 To the Native Client runtime system, a Native Client module is simply a set of |
| 158 machine code, formatted to adhere to a few special rules. No matter whether the |
| 159 code starts out as C or C++ or any other language, the Native Client runtime |
| 160 system performs the steps shown in the following figure: |
| 161 |
| 162 .. image:: /images/NaClExecution.png |
| 163 |
| 164 To ensure that system resources are untouched, the Native Client runtime system |
| 165 prevents the following unsafe activities: |
| 166 |
| 167 * Manipulating devices or files directly (instead, a special file system API |
| 168 is provided) |
| 169 * Directly accessing the operating system |
| 170 * Using self-modifying code to hide the code's intent (such as attempts to |
| 171 write to protected memory) |
| 172 |
| 173 Salt & Pepper |
| 174 ------------- |
| 175 |
| 176 Native Client product names follow a salt & pepper theme. Native Client, |
| 177 abbreviated to **NaCl**, started the salt & pepper naming theme. |
| 178 |
| 179 The Pepper Plug-in API (PPAPI), called **Pepper** for convenience, is an |
| 180 open-source, cross-platform API for web browser plug-ins. From the point of |
| 181 view of Native Client, Pepper is a set of APIs that allow a C or C++ Native |
| 182 Client module to communicate with the hosting browser and get access to |
| 183 system-level functions in a safe and portable way. One of the security |
| 184 constraints in Native Client is that modules cannot make any OS-level calls. |
| 185 Pepper provides analogous APIs that modules can target instead. You can use the |
| 186 Pepper APIs to: |
| 187 |
| 188 * `talk to the JavaScript code in your application |
| 189 <https://developers.google.com/native-client/devguide/coding/message-system>`_ |
| 190 from the C++ code in your NaCl module |
| 191 * `do FileIO |
| 192 <https://developers.google.com/native-client/devguide/coding/FileIO>`_ |
| 193 * `play audio |
| 194 <https://developers.google.com/native-client/devguide/coding/audio>`_ |
| 195 * `render 3D graphics |
| 196 <https://developers.google.com/native-client/devguide/coding/3D-graphics>`_ |
| 197 |
| 198 The figure below illustrates how Pepper serves as a bridge between Native |
| 199 Client modules and the browser: |
| 200 |
| 201 .. image:: /images/pepper.jpg |
| 202 |
| 203 Pepper includes both a C API and a C++ API. The C++ API is a set of bindings |
| 204 written on top of the C API. You can access reference documentation for both |
| 205 the C API and the C++ API through the left-nav on this site. For additional |
| 206 information about Pepper, see `Pepper Concepts |
| 207 <http://code.google.com/p/ppapi/wiki/Concepts>`_. |
| 208 |
| 209 Application Structure |
| 210 ===================== |
| 211 |
| 212 A Native Client application is divided into three main parts: |
| 213 |
| 214 * **HTML/JavaScript application:** Provides the user interface and event |
| 215 handling mechanisms as well as the main HTML document; can perform |
| 216 computation as well. |
| 217 * **Pepper API:** Enables the JavaScript code and the Native Client module to |
| 218 send messages to each other, and provides interfaces that allow Native |
| 219 Client modules to create and use browser resources. |
| 220 * **Native Client module:** Typically, performs numerical computation and |
| 221 other compute-intensive tasks. Handles large-scale data manipulation. Also |
| 222 provides event handling APIs for apps such as games where the user interface |
| 223 is integrated into the code that is run natively. |
| 224 |
| 225 The following diagram shows how Pepper provides the bridge between the Native |
| 226 Client module's code and the browser's JavaScript: |
| 227 |
| 228 .. image:: /images/ProgramStructure.png |
| 229 :align: center |
| 230 :alt: Program Structure |
| 231 |
| 232 Files in a Native Client application |
| 233 ------------------------------------ |
| 234 |
| 235 A Native Client application consists of a set of files: |
| 236 |
| 237 * The **HTML web page**, **CSS**, and **JavaScript** files. Most Native Client |
| 238 apps contain at least an HTML document, optional CSS for styling of the web |
| 239 page, and one or more supporting JavaScript files for user interface |
| 240 objects, event handling, and simple programming tasks and calculations |
| 241 suitable for the JavaScript layer. |
| 242 * The **Native Client module**. This module contains the native compiled |
| 243 code, and uses the Pepper Library (included in the SDK), which provides the |
| 244 bridge between the Native Client module and JavaScript and browser |
| 245 resources. Currently, the SDK supports the C and C++ languages for Native |
| 246 Client modules. When compiled, the extension for this filename is |
| 247 *.nexe*. |
| 248 * A **manifest** file that specifies modules to load for different |
| 249 processors. This file includes a set of key-value pairs, where a key is the |
| 250 end-user's processor (for example, x86-32, x86-64, or ARM) and the |
| 251 corresponding value is the URL of the module compiled for that processor. |
| 252 The extension for this filename is *.nmf*. |
| 253 |
| 254 HTML file |
| 255 ^^^^^^^^^ |
| 256 |
| 257 The HTML file contains the ``<embed>`` tag that loads the Native Client module. |
| 258 For example:: |
| 259 |
| 260 <embed name="nacl_module" |
| 261 id="hello_world" |
| 262 width=0 height=0 |
| 263 src="hello_world.nmf" |
| 264 type="application/x-nacl"> |
| 265 |
| 266 The mimetype for a Native Client module, specified in the ``type`` attribute, is |
| 267 ``application/x-nacl``. |
| 268 |
| 269 Native Client modules are operating system independent, but they are not (yet) |
| 270 processor independent. Therefore, you must compile separate versions of a Native |
| 271 Client module for x86 32-bit, x86 64-bit, ARM, and other processors. The |
| 272 manifest file, specified in the ``src`` attribute of the ``<embed>`` |
| 273 tag, specifies which version of the Native Client module to load depending on |
| 274 the end-user's processor. |
| 275 |
| 276 Native Client module |
| 277 ^^^^^^^^^^^^^^^^^^^^ |
| 278 |
| 279 You can write a Native Client module in C or C++, and use existing libraries |
| 280 and modules compatible with that language. After you've implemented your |
| 281 functions in the module, you can use the Pepper API to pass messages to and |
| 282 receive messages from the JavaScript/browser side of the application. |
| 283 |
| 284 |
| 285 Creating a Native Client application |
| 286 ==================================== |
| 287 |
| 288 Native Client is extremely flexible, allowing for numerous ways to develop an |
| 289 application. The following is one of the more common approaches: |
| 290 |
| 291 1. :doc:`Download <sdk/download>` the Native Client SDK. You may also need to |
| 292 download and install Python, which is required to run a number of tools in |
| 293 the SDK. |
| 294 2. Write the application: |
| 295 |
| 296 a. Create a user interface using HTML, JavaScript, and CSS. |
| 297 b. If necessary, port existing libraries to compile with your Native Client |
| 298 module. If you find yourself missing a common library, have a look at |
| 299 `NaCl ports <http://code.google.com/p/naclports>`_, a repository for open |
| 300 source libraries. Contributions are welcome. |
| 301 c. Create the Native Client module and `build |
| 302 <https://developers.google.com/native-client/devguide/devcycle/building>`_ |
| 303 it using one of the NaCl toolchains in the SDK. |
| 304 3. `Run |
| 305 <https://developers.google.com/native-client/devguide/devcycle/running>`_ the |
| 306 application. |
| 307 4. `Distribute |
| 308 <https://developers.google.com/native-client/devguide/distributing>`_ the |
| 309 application. |
| 310 |
| 311 For detailed, step-by-step instructions on how to create a Native Client |
| 312 application, see the `Getting Started Tutorial |
| 313 <https://developers.google.com/native-client/devguide/tutorial>`_. The tutorial |
| 314 includes a basic set of template files that you can modify for your project. |
| 315 |
| 316 Using existing code |
| 317 =================== |
| 318 |
| 319 The Native Client SDK comes with a modified version of the GNU toolchain |
| 320 (including GCC and G++) that produces sandbox-safe native code to be run in the |
| 321 browser. This opens the browser to 3D games, video editing, and other |
| 322 applications that can be moved to the web with relatively little effort. Some |
| 323 work is required, however, to keep code safe for execution on the web. For |
| 324 instance, OS-level calls must be redirected to target the Pepper API, and there |
| 325 are also restrictions on how file IO and threading operations work. |
| 326 |
| 327 Distributing a Native Client application |
| 328 ======================================== |
| 329 |
| 330 The Chrome browser only runs Native Client applications published through the |
| 331 `Chrome Web Store |
| 332 <https://chrome.google.com/webstore/search/%22native%20client%22%20OR%20nativecl
ient%20OR%20nacl>`_ |
| 333 (CWS). To distribute an application, you must compile the application for both |
| 334 the 32-bit and 64-bit x86 architectures, and publish the application in the |
| 335 CWS. |
| 336 |
| 337 Native Client has not been standardized yet, and currently it requires |
| 338 compilation for specific instruction set architectures. Google only allows |
| 339 Native Client applications into the CWS if the applications are available for |
| 340 both 32-bit and 64-bit x86 architectures; developers are also strongly |
| 341 encouraged to provide a build for the ARM architecture. The intent behind the |
| 342 current policy of requiring applications to be compiled for multiple |
| 343 architectures is not to bake one instruction set into the web, and to make sure |
| 344 that future architectures are supported as well. |
| 345 |
| 346 Note that this is only a temporary requirement: The ultimate plan is to create |
| 347 a new version of Native Client executables that can run on any processor. This |
| 348 new Native Client technology is called Portable Native Client (PNaCl, |
| 349 pronounced "pinnacle"), and it is already under development. Instead of |
| 350 generating x86 code or ARM code, PNaCl transforms high-level code into bitcode |
| 351 using a compiler based on the open source LLVM (low-level virtual machine) |
| 352 project. When the browser downloads the bitcode, PNaCl then translates it to |
| 353 native machine code and validates it in the same way Native Client validates |
| 354 machine code today. |
| 355 |
| 356 PNaCl could potentially give Native Client the same reach as JavaScript, but |
| 357 there are still hurdles to overcome. Part of the problem is that PNaCl has more |
| 358 overhead. Currently PNaCl can execute much faster than JavaScript, but it does |
| 359 not yet start up as quickly. Before officially launching PNaCl, Google wants to |
| 360 make sure to close that gap. |
| 361 |
| 362 The Native Client SDK |
| 363 ===================== |
| 364 |
| 365 The Native Client SDK includes the following: |
| 366 |
| 367 * GNU-based toolchains: gcc, g++, as, ld, gdb, and other tools customized for |
| 368 Native Client |
| 369 * API libraries (Pepper, POSIX) |
| 370 * Makefiles for building Native Client applications |
| 371 * Examples |
| 372 * Documentation |
| 373 |
| 374 Versioning |
| 375 ========== |
| 376 |
| 377 Chrome releases on a six week cycle, and developer versions of Chrome are |
| 378 pushed to the public beta channel three weeks before release. As with any |
| 379 software, each release of Chrome includes changes to Native Client and the |
| 380 Pepper interfaces that may require modification to existing applications. |
| 381 However, modules compiled for one version of Pepper/Chrome should generally |
| 382 work with subsequent versions of Pepper/Chrome. The SDK includes multiple |
| 383 `versions <https://developers.google.com/native-client/version>`_ of the Pepper |
| 384 APIs to help developers make adjustments to API changes and take advantage of |
| 385 new features. |
OLD | NEW |