ffos:NuwaTemplateProcess

Java安全之freemaker模版注入 FreeMarker 是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。在线手册:什么是 FreeMarker?- FreeMarker 中文官方参考手册模板文件存放在Web服务器上,当访问指定模版文件时, FreeMarker会动态转换模板,用最新的数据内容替换模板中${...}的部分,然后返回渲染结果。${...}: FreeMarker将会输出真实的值来替换大括号内的表达式,这样的表达式被称为(插值) 阅读详情

NuwaTemplateProcess

Nuwa is a mechanism to fork content processes from a template process. The template process is called Nuwa process. The processes forked from Nuwa process are parasited processes. The basic idea of Nuwa is running Nuwa process like a normal content process, but freeze all its threads after running into a stable state. All threads of Nuwa process will be frozen at a freeze point. The freeze points can be poll, epoll, pthread_mutex_lock, pthread_cond_wait, ... etc, functions that block a thread and release CPU to other threads or processes. For most programs, include B2G, the threads are not always busy. They usually sleep at some point by calling one of above functions. Nuwa process is frozen at freeze points to keep itself at some stable state. Then, Nuwa process forks itself, and its children are used as content processes. All threads are rebuilt and restarted from their freeze points at parasited processes.

Contents

Freeze Points

Nuwa make wrappers for all functions being freeze points. All these wrappers are in BionicGlue.cpp. We use --wrap arguments to make GNU LD redirect all function calls of freeze points to respective wrappers. In most time, these wrapper will call respective back functions. For example, the wrapper of pthread_mutex_lock() will call pthread_mutex_lock() to do the task. These wrappers will be blocked on a lock, sThreadFreezeLock, for freezing the calling thread once Nuwa process run into a stable state. We say Nuwa process is frozen if all threads are blocked at these wrappers.

Threads

For Linux system, all threads are not surviving after fork. So, we need to create a new thread and running the frozen context for each thread in Nuwa process; parasite the frozen context on the new thread. To parasite the frozen context on the new thread, we reuse the stack of the frozen thread in the new thread. Each process forked from Nuwa process owns independent memory space, so it does not inference each other to reuse the stack of frozen stack.

 pthread_attr_setstack(attr, stack_ptr, stack_size)

is called to assign a stack for a thread created with the given attributes.

The context of a thread is comprised by registers and a stack. The registers are saved and restored by calling setjmp() and longjmp(). setjmp() is called at freeze point just before freezing the thread. longjmp() is called just at beginning of the new thread. Nuwa passes a special start routine for calling pthread_create(). The routine will call longjmp() ASAP to parasite the context of frozen thread on the new thread.

TLS

Thread local storage, aka TLS, is a part of a thread. TLS provides a separated storage for each thread, the data stored in TLS are accessed through a key. Nuwa process will remember all keys ever been used, and read and remember the associated data just before freeze point for every thread, then restore them at parasited threads. See

  • SaveTLSInfo(),
  • RestoreTLSInfo(),
  • __wrap_pthread_key_create(), and
  • __wrap_pthread_key_delete()

in BionicGlue.cpp.

Thread ID

The thread ID of parasited threads are different from frozen threads. So, Nuwa make a wrapper for pthread_self() to map thread ID of parasited threads to thread IDs of frozen threads. Some code use Linux depend gettid() syscall, I don't see any reason why people use gettid instead of pthread_self() for most situation. So, change the code using gettid() to call pthread_self() instead. Even more worse, some code call syscall() to invoke gettid. A wrapper of syscall() affects a lot of code, studying more is required before making a decision.

PIPE and Socketpair

For B2G, a lot of PIPEs and sockets are used for synchronization between threads for dispatching tasks among threads. For Linux, file descriptors will be shared after a fork without CLOEXEC. It is fine for normal files but not for communication channels. So, PIPEs and sockets should be recreated. Nuwa remember what pipes and socketpairs are created by Nuwa process and create new ones for them for each parasited process. Since file descriptors of new PIPEs and sockets are not the same as what in Nuwa process, dup2() is called for replacing the file description of old file descriptor by a new PIPE or socket.

Mutics and Conditions

For mutics, there at most one of threads will own the lock. Nuwa freeze just before pthread_mutex_lock(), it means still not owning the mutex. So, the wrapper needs only to call pthread_mutex_lock() at parasited thread. If the mutex is already owned by another thread, and the frozen thread is blocked by calling pthread_mutex_lock(), not by sThreadFreezeLock, the wrapper also calls pthread_mutex_lock() and be blocked again at the parasited thread. So, the wrapper of pthread_mutex_lock() always redo the lock in parasited threads.

For conditions of pthread, it is more complex since it can depend on a mutex. Once the pthread_cond_wait() is called, it means the given mutex, if have one, is released. The give mutex is acquired again when the call is returned. For parasited thread, we need to acquire the lock before calling pthread_cond_wait() to restore the context. So, it can be one of two situations, the mutex is hold by some one or free/released. pthread_mutex_trylock() can be used to test if a mutex have been acquired. For free/released situation, pthread_mutex_trylock() will acquire the mutex and pthreaad_cond_wait() will put the mutex back to the released state, so it is fine. For hold state, pthread_mutex_trylock() can not acquire the mutex, pthread_cond_wait() will put the mutex to the released state different from original hold state. So, we need to acquire the mutex to put is back to hold state after the parasited thread was blocked by pthread_cond_wait(). So, the wrapper will ask the main thread to acquire the mutex again, then the state of the mutex will be consistent with Nuwa process.

Dark Magic of Stack

Binder

Binder is a special feature of Android, it does not follow the paradigm of UN*X/Linux. ...

IPC

IPC of Gecko/B2G is comprised by various protocols. The peers of a protocol instance are represented by a pair of actors; For example PBlobParent and PBlobChild are base classes of PBlob actors of parent and child side respective. PBlob* are generated by the IPDL code generator. Nuwa does not do any special for child actors for parasite processes since a new memory space are created and COW. But, for parent actors in the parent process, Nuwa should clone them and replace their IPC channels with new ones. Nuwa process will create new socket pairs for actors as the IPC channel between the new parasite process and the parent process.

IPC socketpairs.png

For running IPC between Nuwa process and the parent process, the IO thread and the main thread should not be frozen. For the main thread, no any special should be done, it just continue the control flow after fork. But, for the IO thread, it should create a parasite point just before the fork to tell Nuwa where to restart the thread. See

  • ParasitePointThreadNuwa() in BionicGlue.cpp, and
  • DoParasitePoint() in ContentChild.cpp.

Defense

Share Data Among Content Processes

With Nuwa, static data could be shared among content processes with just one copy and copy-on-write. If you think some deserve to be shared, you should load it at the Nuwa process before being frozen. PreloadSlowThings() is a good place to load/or initialize the data. Please check dom/ipc/preload.js of m-c.

Preloading

Preloading in B2G is a mechanism that loads assets and modules prior to opening an app to improve the app loading time. Preloading in Nuwa can leverage Nuwa's copy-on-write advantage to minimize memory space occupied by preloaded data shared accross process. However, there are 2 reasons that make preloading not be able to run in Nuwa.

  • Preloading modules initiate asynchronous tasks, some of the tasks are even dispatched to chrome process through IPC. The asynchronous natural makes Nuwa hard to determine whether or not all of the tasks are done and to decide when to freeze. A callback invoked after Nuwa frozen can make Nuwa's main thread try to acquire a lock held by a frozen thread, which results in deadlock.
  • Some modules setup ppmm/cpmm-based communication channels in there initialization processes. Since the channels are not based on IPDL, Nuwa won't duplicate the channels for the newly-forked process, thus the communication channels have to be built again after a process has been forked from Nuwa.

To address the first issue, a method of determining whether the task are all done or not has been proposed. The idea is that we monitor each thread in chrome process, and see if there's a moment that all of the threads are idle. Since we are not accessing network during boot, having all of the threads idle means all of the tasks previously generated are consumed, and Nuwa can freeze. The second issue is addressed by splitting preload.js into 2 parts. The first part is where to put modules that are good to be loaded in Nuwa, while the second part contains modules that are not suitable to be loaded in Nuwa and scripts that rebuild communication channels. See

  • Bug 970307
Xilinx ZYNQ 7000学习笔记二(FSBL代码分析启动-汇编代码) Xilinx ZYNQ 7000学习笔记 阅读详情

相关推荐

STM32HAL库学习笔记九——LCD显示

本文不会对驱动编写做特别详细的描述,以记录CubeMX的部署流程为主。

weixin_44762233的博客 6924

template.process(root, out)的用法(shiro项目中来的九)

假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击http://106.12.206.16:8080/qingruihappy/index.html 1 Writer out = new BufferedWriter(new OutputS...

weixin_33901926的博客 9038

STM32F429移植LVGL9.3.0:从源码配置到双缓冲优化实战

本文详细介绍了如何在STM32F429平台上移植LVGL 9.3.0图形库,涵盖从源码准备、工程配置、显示驱动适配到双缓冲优化等关键步骤。重点解析了利用STM32F429的硬件特性实现高效内存管理和流畅显示性能的实战技巧,为嵌入式GUI开发提供完整解决方案。

q6r7s8t9的博客 1165

FreeMarker模板使用方法讲解

freemarker简介与初级使用

zhanggnol的blog 2万+

centos-stream-9上安装nvidia驱动和cuda-toolkit

centos-stream-9上安装nvidia驱动和cuda-toolkit

qq_19072921的博客 3032

Altium AD20的PCB板框绘制、定义板子形状、重新设置原点、放置尺寸标注

AD20的板框绘制与以前的版本不同,以前板框是在KeepOut层,而在AD20中没有这个层,绘制板框需要到机械层Mechanical1。 板框大小评估 首先进行板框大小评估。 工具 - 器件摆放 - 在矩形区域摆放。 之后通过鼠标框选矩形区域,元件会自动按照框选的范围进行摆放。这个操作可以重复多次,最后大致得出需要的板框尺寸。确定好后右键。 板框绘制,重新定义板子形状 点击下方的 Mechanical1,切换到机械层。放置线条,根据元件摆放来绘制PCB大致的形状,将元件完全包裹。

Mark_md的博客 10万+

Freemarker实现网页静态化

1.什么是freemarker FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。 目前企业中:主要用Freemarker做静态页面或是页面展示 2.freemarker的使用方法 这里使...

GuiSu97的博客 728

从零开始之stm32之CAN通信

CAN通信极简教程(小白友好版),从小白的视角了解并实现简单的STM32F103的CAN通信,直接上手。

qq_30993593的博客 1316

Java模板引擎Freemarker2.x进阶指南

目录 前言 Java模板引擎Freemarker2.x进阶详情 一、关于静态化页面 二、网页静态化特点 三、关于Freemarker 四、Freemarker 的使用步骤 五、Freemarker 的使用 六、Freemarker 常用语法 七、相关文档 前言 在项目需要实现静态化页面,拿到专业的前端人员定义好的模板,就可以开工咯,为什么要使用 Freemarker...

Spring Boot-Common On With You 2537

Python爬虫实战:模拟登录B站 – 处理Cookie和加密参数

由于B站在登录过程中采用了多种安全措施,包括加密的请求参数和cookie验证,模拟登录成为一项具有挑战性的任务。首先,我们需要获取B站的登录页面,并解析出其中的CSRF令牌和其他隐藏的字段。B站的登录接口通常会包含一些动态的加密参数,我们需要在登录前通过GET请求获取这些信息。本文详细介绍了如何使用Python模拟登录B站,解决加密参数的处理、图形验证码的识别以及登录后的数据抓取。库中的加密方法加密登录请求中的密码。一旦登录成功,我们可以通过维护会话状态,抓取登录后的数据,如个人主页、评论、观看记录等。

2201_76125261的博客 1315

PyCharm 怎么不升级直接使用Jupyter Notebook

在PyCharm 中需要升级到 PyCharm Professional 版本,才能直接使用 Jupyter Notebook。从截图来看,Jupyter Notebook 已经成功启动,并且正在运行。这将在默认网络浏览器中打开一个新的标签页,显示文件浏览器界面。可以在这里创建和编辑。文件添加到 PyCharm 项目中,并使用纯文本编辑功能进行修改。支持与外部工具集成,可以配置它以调用命令行工具或脚本来启动。,参数可以根据需要设置,工作目录设置为项目根目录。,你仍然可以在本地环境中安装和运行。

Davina_yu的博客 1009

模板方法模式(Template Method)

一、模板方法模式介绍 模板方法模式:定义一个操作中的算法骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的 某些特定步骤。 核心:处理某个流程的代码已经都具备,但是其中某个节点的代码暂时不能确定。因此,我们采用工厂方法模式,将这个节点的代码实现转移给 子类完成。即:处理步骤在父类中定义好,具体的实现延迟到子类中定义。 说白了,就是将一些相同操作的代...

weixin_30487317的博客 241

【嘉立创EDA】PCB金属包边设计快捷方法

本文使用嘉立创EDA,描述如何在PCB设计时,快速增加边框包边设计,特别是金属包边(伪)——这里标记(伪)是因为该这里的包边特指不含边缘壁。请参考本文时特别注意!本文将此过程记录,以供有需要的读者参考。

l64481000的博客 1775

解决freemarker生成的静态文件中文乱码的问题

1、思想:统一字符集查看了很多教程,说是要以GBK来获取模板,又要以utf-8输出文件,或许是有道理的,但是总感觉怪怪的,逻辑有的不是很通,我最后解决这个问题,就是一点:统一所有字符集2、将模板文件设置为utf-8编码格式3、设置Configuration默认编码为utf-8如果是整合spring的话,可以使用在配置文件中加载FreeMarkerConfigurer对象,设置默认编码格式为utf-...

DragonFreedom的博客 5677

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

FreeMarker template使用

赋值:<#assign val = 10> 输出:${val} 算数运算: <#-- 支持"+"、"-"、"*"、"/"、"%"运算符 --> <#assign number1 = 10> <#assign number2 = 5> "+" : ${number1 + number2} "-" : ${number1 - number2} "*" : ${number1 * number2} "/".

weixin_38320510的博客 6573

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

freeMarker学习笔记

FreeMarker 概述 FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。 HTML静态化也是某些缓存策略使用的手段,对于系统中频繁使用数据库查询但是内容更新很小的应用,可以使用FreeMarke...

zzzliuke的博客 266

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

template用法及常见错误

template用法及常见错误

weixin_41786823的博客 2370

嵌入式30秒上云:MQTT v5新特性与Topic规划全解析

MQTT是物联网设备上云的主流轻量级协议,其低带宽消耗与灵活发布订阅模型,使其在嵌入式领域广泛落地。MQTT v5版本在协议层引入会话过期、主题别名、用户属性等特性,不仅优化了窄带设备的流量开销,也让业务语义表达更清晰。Topic作为消息路由的核心,合理规划能够显著提升系统的可扩展性;而免费平台常以连接数、消息速率等配额换取入门门槛,开发者需在原型阶段就关注这些隐性限制。从设备接入的标准化流程、ClientID与密钥设计,到重连抖动、遗嘱消息等工程细节,嵌入式工程师可以利用一套通用方法,在ESP32、STM

weixin_33912638的博客 552

java ftl 渲染_使用freemarker对模板进行渲染

import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStreamWriter;import java.io.StringWriter;import java.util.HashMap;import java.util.Map;import org...

weixin_42300896的博客 471

process_template_responseprocess

https://www.cnblogs.com/zhaof/p/6281541.htmlhttps://www.cnblogs.com/sss4/p/7106033.html总结:中间件 process_request process_view 是从前往后执行process_exception process_response 是从后往前执行如果process_view返回的对象有render 则...

pzqingchong的专栏 1663

通过template来把参数添加至对应的字符串中

<br />1.  getTemplate()方法分别用来获取模板,以及配置好参数的模板<br /> /** * 根据TransCode读取模板,模板中的参数被处理 * @param transCode * @param params * @return */ public String getTemplate(String transCode, Map params) { Assert.hasLength(transCode, "TransCode不能为空

linwei_1029的专栏 3772

在使用phpword中TemplateProcessor进行模板变量替换时,出现变量无法替换现象!

在做项目时用到,phpword中TemplateProcessor API接口进word模板文件中使用变量替换内容,在进行一个文档内容替换时出现变量无法替换,后面排除法,一块一块的删除word中文件内容看是哪个变量不能替换影响到了,试到最后发现可能是文档内容太长引起的。 一开始还以为是否是phpword中有配置文件做了限制,最后查百度才发现是 php正则失效-最大回溯(p...

dnpao的专栏 6055

SpringBoot利用FreeMarker渲染指定模板

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> 模板位置 注:springboot中freemarker默认...

盲目的拾荒者的博客 1421

nuwa创建新进程源码分析

nuwa.cpp的作用,fork 对线程进行相关冻结设置复制等。线程级别的设置等。。。。 https://developer.mozilla.org/en-US/docs/Archive/B2G_OS/Security/B2G_IPC_internals https://blog.csdn.net/hunter___/article/details/82912266   具体介绍如下内容:...

hunter 366
上一篇: ffos:B2G IPC internals,nuwa process and b2g process
下一篇: android:android booting
nedwons
博客等级 码龄9年 311粉丝 572原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值