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
Pspice安装教程:Windows系统完整指南(含环境变量设置) 详细讲解在Windows系统中完成Pspice安装的全过程,涵盖安装步骤与关键的环境变量设置,帮助用户顺利运行仿真工具。适合初学者快速上手pspice安装教程中的核心环节。 阅读详情

相关推荐

Codesys软运动控制进阶:用SMC_FreeEncoder为ECAT轴搭建一个“虚拟手轮”调试工具

本文详细介绍了如何利用Codesys的SMC_FreeEncoder功能为ECAT轴搭建虚拟手轮调试工具,解决自动化设备开发中的硬件依赖问题。通过软件模拟手轮脉冲信号,实现无硬件依赖的运动控制逻辑验证,显著提升调试效率。适用于汇川AM400等PLC平台,特别适合机械安装滞后或伺服驱动器故障时的调试场景。

weixin_33737774的博客 486

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+LAN8720实战:5分钟搞定LWIP裸机网络通讯(附完整代码)

本文详细介绍了如何在STM32F429微控制器上,结合LAN8720 PHY芯片,在裸机环境下快速部署和配置LWIP协议栈以实现网络通讯。内容涵盖硬件RMII接口连接、STM32CubeMX关键配置、代码移植与定制,以及常见问题排查方法,帮助工程师在资源受限的嵌入式系统中高效实现稳定的以太网功能。

2y3u4i5o6p的博客 973

FreeMarker模板使用方法讲解

freemarker简介与初级使用

zhanggnol的blog 2万+

逆向实战:手把手教你分析Google DroidGuard虚拟机的加密与反调试(附Python脚本)

本文深入解析Google DroidGuard虚拟机的加密机制与反调试策略,提供从环境配置到逆向分析的完整实战指南。通过IDA Pro、Frida等工具链,详细剖析DroidGuard的信号拦截、内存校验等保护机制,并附Python脚本实现自动化分析,助力Android安全研究人员突破GMS核心组件的安全防护。

weixin_32428571的博客 319

RabbitMQ高级特性 - 生产者消息确认机制

为了保证信息 从生产者 发送到 队列,因此引入了生产者的消息确认机制.通过事务机制实现.通过发送确认机制(confirm 和 return)实现.因为事务机制比较消耗性能,在实际工作中用的也不多,因此这里主要介绍confirm 和 return机制来实现发送放的确认.a)confirm 确认模式如上图,confirm 确认模式主要保障于 生产者 到 交换机 的消息可靠性.如果 Exchange成功收到消息,那么 ConfirmCallback 这个回调 ack 参数就为true。

CYK_byte的博客 1889

Freemarker实现网页静态化

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

GuiSu97的博客 727

华为OD机试真题 新系统2026-04-01 C语言 实现【空间占用计算】

员工A的磁盘空间经常被耗尽,他需要找到占用空间最大的目录或文件,然后决定如何清理文件释放空间。给定某一目录,请编写程序帮助他统计该目录内一级子目录和文件的占用空间,并返回目标目录一级子项(文件或子目录)中占用空间最大的项。

MISAYAONE的博客 235

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

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

Spring Boot-Common On With You 2537

Qt for WebAssembly环境搭建

一、前言 二、Ubuntu 1、在线安装Qt15 在线安装下载器:https://download.qt.io/official_releases/online_installers/ 安装时记得勾选WebAssembly,如下: 2、安装依赖 # Install Python sudo apt-get install python2.7 # Install CMake (optional, only needed for tests and building Binaryen) sudo apt

贝勒里恩的博客 3675

AHB2APB_bridge 设计

此处研究AHB Slave到 APB master的桥接器,实现接口转化,例如下图之后是参数描述

Starry__的博客 2203

模板方法模式(Template Method)

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

weixin_30487317的博客 240

13.0 针对跳转不存在Activity崩溃处理,改成弹Toast提示,去掉崩溃弹窗,避免app崩溃退出

在开发过程中,在app跳转到某个没注册的Acitivity时,往往会出现崩溃的场景,这样也不好,接下来就从framework中来拦截这个崩溃,改成toast提示,

安卓兼职framework和app工程师的博客 54

FreeMarker template使用

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

weixin_38320510的博客 6572

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

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

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

DragonFreedom的博客 5677

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

freeMarker学习笔记

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

zzzliuke的博客 265

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的专栏 1662

通过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

Template Method(模板方法)模式

1.概述   在面向对象开发过程中,通常我们会遇到这样的一个问题:我们知道一个算法所需的关键步骤,并确定了这些步骤的执行顺序。但是某些步骤的具体实现是未知的,或者说某些步骤的实现与具体的环境相关。例子1:银行业务办理流程在银行办理业务时,一般都包含几个基本固定步骤:取号排队-&gt;办理具体业务-&gt;对银行工作人员进行评分。取号取号排队和对银行工作人员进行评分业务逻辑是一样的。但是办理...

weixin_34319111的博客 180

利用freemaker生成静态页面

利用freemaker生成静态页面   FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写          FreeMarker被设计用来生成HTML Web页面,特别是基于MVC模式的应用程序          虽然FreeMarker具有一些编程的能力,但通常由Java程序准备要显示的数据,由FreeMarker生成页面,通过模

u014704496的专栏 694

ffos:B2G IPC internals,nuwa process and b2g process

https://developer.mozilla.org/en-US/docs/Archive/B2G_OS/Security/B2G_IPC_internals Architecture In FirefoxOS we have a Multi-process Architecture where the apps on the phone are running in a differ...

hunter 334

b2g process 和nuwa process 通信

它们通信的目的在于: b2g要发消息给nuwa,让它fork出web app进程。它们发生在nuwa进程已经完全启动,并准备好fork 其他app子进程。猜想,用的是socket pair 建立关系,ipdl(最终用的是共享内存读写)进行线程级别的信息交互。socket pair只是用于建立通道??? nuwa的操作,从b2g开始fork出两个进程之后,兵分两路,nuwa进程完成自己的所有初始...

hunter 455
上一篇: 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、付费专栏及课程。

余额充值
⌂ 换网址