Native Client 资料
Native Client:本地程序(C/C++和目前不支持的其他程序)沙箱
? ? ?[JavaScript通過瀏覽器(解釋引擎)來完成功能,HTML5只是擴展了部分功能]
Native Client 適合純計算(CPU+內(nèi)存)本地程序,不適合 創(chuàng)建進程/直接訪問文件/無限制訪問網(wǎng)絡(luò) 程序
========================================================================================================
本地程序的生命周期
(1)瀏覽器加載一個帶有“application/x-nacl-srpc”MIME類型的embed標記的頁面。
(2)渲染引擎:調(diào)用RenderView::CreatePluginDelegate函數(shù),創(chuàng)建一個針對application/x-nacl-srpc類型的NaCl插件。同時驗證瀏覽器是否打開了Native Client功能,如果沒有則阻止創(chuàng)建NaCl插件。
(3)渲染引擎:調(diào)用NaCl插件的NP_Initialize函數(shù)。
(4)NaCl插件:下載NaCl模塊(即本地程序)
?????? 當(dāng)瀏覽器開始下載NaCl模塊時調(diào)用NPP_NewStream函數(shù),進一步調(diào)用PluginNpapi::NewStream函數(shù),返回NP_NORMAL,表示NaCl插件已經(jīng)準備好接受二進制數(shù)據(jù)。由于NaCl插件放入了瀏覽器沙箱,所以不能訪問文件系統(tǒng)通過返回NP_ASFILEONLY。
?????? 瀏覽器調(diào)用NPP_Write,NaCl插件則將二進制數(shù)據(jù)寫入一個StreamShmBuffer對象。
?????? 下載完成以后,瀏覽器調(diào)用NPP_DestroyStream函數(shù)銷毀輸入流。調(diào)用Plugin::Load函數(shù),傳入?yún)?shù)包括nexe二進制程序的StreamShmBuffer對象的指針。
(5)NaCl插件:Plugin::Load函數(shù)加載NaCl模塊
?????? NaCl插件檢驗nexe文件是否在白名單里面(不允許加載本地存儲的NaCl模塊),再檢驗nexe文件的合法ELF格式,之后創(chuàng)建一個plugin::ServiceRuntime對象來抽象NaCl加載器的實例。
創(chuàng)建一個nacl::SelLdrLauncher對象,包含nexe文件的URL和文件描述符。調(diào)用nacl::SelLdrLauncher::Start函數(shù),進而調(diào)用LaunchNaClProcess函數(shù),進而發(fā)送ViewHostMsg_LaunchNaCl的IPC消息給瀏覽器,通知創(chuàng)建一個加載器進程。
(6)瀏覽器:創(chuàng)建一個加載器進程
?????? 創(chuàng)建一個channel5的已連接socket對,創(chuàng)建一個Chrome IPC 通道。創(chuàng)建一個加載器進程,之后觸發(fā)NaClProcessHost::OnProcessLaunched回調(diào)函數(shù),發(fā)送一個ViewHostMsg_LaunchNacl消息,包含channel5句柄、加載器進程ID和加載進程句柄。之后再發(fā)送NaClProcessMsg_Start消息,這時,NaCl插件和加載器有了彼此的channel5句柄,可以相互通信了。
(7)加載器:NaClProcessMsg_Start消息的處理函數(shù)調(diào)用加載器的SelMain函數(shù)
?????? SelMain函數(shù)加載NaCl模塊,創(chuàng)建一個BoundSocket,等待NaCl插件的連接。
(8)NaCl插件:通過SharedMemory將NaCl模塊傳給加載器進程,向加載器發(fā)送start_module消息
(9)加載器:收到start_module消息后,啟動NaCl模塊,等待調(diào)用。
(10)NaCl插件:Plugin::Load調(diào)用OnLoad處理函數(shù)通知JavaScript引擎NaCl模塊已準備好。至此,NaCl插件已經(jīng)準備好處理JavaScript與NaCl模塊之間的調(diào)用。
========================================================================================================
Native Client目前支持2D圖形、立體音頻、URL獲取、沙箱化的本地文件訪問和與JavaScript的異步消息通信
========================================================================================================
http://www.chromium.org/nativeclient
https://developers.google.com/native-client/
http://www.chromium.org/nativeclient/design-documents/native-client-integration-with-chrome
http://www.chromium.org/nativeclient/getting-started/the-life-of-a-native-client-module
http://www.chromium.org/nativeclient/getting-started/getting-started-background-and-basics
內(nèi)層沙箱:控制系統(tǒng)調(diào)用和跳轉(zhuǎn)
外層沙箱:系統(tǒng)調(diào)用白名單
服務(wù)運行時:模擬系統(tǒng)調(diào)用
服務(wù)運行時與NaCl程序同進程,后者通過API訪問前者,前者通過x86的段頁式內(nèi)存阻止NaCl程序的非法內(nèi)存訪問
服務(wù)運行時(64K):前4K受讀寫保護,用于檢查空指針;剩下的60K實現(xiàn)“trampoline”調(diào)用門和“springboard”返回門
========================================================================================================
Native Client線程通過PepperAPI與瀏覽器的代理線程通信
瀏覽器線程通過夾層API-系統(tǒng)API與OS內(nèi)核線程通信
CAR可以起到擴展瀏覽器的作用(一旦OS有變化,不需重新編譯瀏覽器,只需發(fā)行CAR)
由JS調(diào)NaCl程序
NativeClient本身是平臺相關(guān)的和可信的,可以訪問所有系統(tǒng)調(diào)用接口
========================================================================================================
執(zhí)行過程(本地程序編譯成中間碼->解碼/驗證->受控運行)
To the Native Client runtime system, a Native Client module is simply a set of machine code, formatted to adhere to a few special rules. No matter whether the code starts out as C or C++ or any other language, the Native Client runtime system performs the steps shown in the following figure:
To ensure that system resources are untouched, the Native Client runtime system prevents the following unsafe activities:
- Manipulating devices or files directly (instead, a special file system API is provided)
- Directly accessing the operating system
- Using self-modifying code to hide the code's intent (such as attempts to write to protected memory)
Native Client is ideal for application components requiring?pure computation. It is not appropriate for modules?requiring process creation, direct file system access, or?unrestricted access to the network
Native Client executes code that is compiled by a special compiler that prevents you from using certain types of operations and provides some sandboxing and such; it is intended to allow you to write native code but still enforce many of the security restrictions that the browser already follows. For things that are possible, it's great -- as long as you can get it to work in the browser you want to use. Currently it's supported by Google Chrome, but I haven't heard for sure that it works in any other browser.
Conversely, with NPAPI or ActiveX (and see?FireBreath?which allows you to target both types simultaneously) you write native code and do more or less anything that a normal application could do (except in IE on Vista/Win7 w/ UAC enabled where you are in low integrity mode).
The biggest disadvantage to NaCl is probably that you can't access hardware; since it's sandboxed, you're a bit more limited as to what networking things you can do and a lot more limited as to what devices you can interface with.
======================================================================
Java’s security measures are chaperones. They’re always there and always checking your actions. NaCl’s mechanisms are just rules. They’re checked once, and then the program is on its own. NaCl promises to be faster than Java
======================================================================
running a subset of Intel x86 or ARM native code using software-based fault isolation
======================================================================
Native Client is specifically designed to run native code securely inside web browsers,?it puts web applications on "the same playing field" as local applications, providing the raw speed needed to compete with traditional software on 3D games, video editing, and more
======================================================================
"If we're successful with this [Native Client] project, we will make other languages more useful in the context of the web. We want to create a system that gives languages like C and C++ – but eventually others as well – the same excellent level of portability and safety that JavaScript provides on the web today."
Native Client?is a software "sandbox" meant to?securely run native code inside a browser
native languages such as C and C++ – which have access to a machine's underlying components –?were not. Native Client seeks to add such protection.
google_native_client_from_all_sides
google_native_client_from_all_sides_2
google_native_client_from_all_sides_3
google_native_client_from_all_sides_4
With the 32-bit x86 instruction set, Native Client uses the segment registers to restrict where in memory a program can read and write data and to ensure that a program doesn't jump to code outside a certain range of memory. But it also includes a modifiedcompiler and a code verifier that work to keep code jumps in line???????????????
An ordinary program will read a data value from memory into a register and then jump to the address that value represents. But with Native Client, the compiler performs a bit of arithmetic on that value?before?the jump to ensure it doesn't target bad instructions, and then the code verifier double-checks the compiler's work.
This proposition fits quite nicely with Chrome OS, the fledgling Google operating system that puts?all applications inside the browser. With Chrome OS, running existing 3D games and other desktop applications isn't really an option. But the Native Client project pre-dates Google's operating system effort, and the ultimate goal is to bring a new breed of applications to the entire web.????????????????????
"Our goal is to have an execution arm that can have no side effects – zero interaction with the outside world – and that's what we think we have achieved with the sandbox," says Brad Chen. "But the thing is that if you can't interact with the outside world, including the browser, you can't actually do anything. That's where these Pepper interfaces come in. They're designed to expose to Native Client exactly what is also being exposed via JavaScript."
=====================================================================
native code’s primary benefit lies in memory layout and access patterns, not instruction set benefits such as SIMD
mozillas-rejection-of-nativeclient-hurts-the-open-web
=====================================================================
總結(jié)
以上是生活随笔為你收集整理的Native Client 资料的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 沙箱(Sandbox)
- 下一篇: 浏览器架构