<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Aiur - ZelluX 的技术博客</title>
	<atom:link href="http://techblog.iamzellux.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://techblog.iamzellux.com</link>
	<description>Security, Kernel, Virtualization, Programming Languages</description>
	<lastBuildDate>Sun, 07 Mar 2010 05:40:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>跨站脚本攻击和BluePrint</title>
		<link>http://techblog.iamzellux.com/2010/03/blueprint/</link>
		<comments>http://techblog.iamzellux.com/2010/03/blueprint/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 15:47:13 +0000</pubDate>
		<dc:creator>zellux</dc:creator>
				<category><![CDATA[Computer System]]></category>
		<category><![CDATA[Paper]]></category>
		<category><![CDATA[S&P]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[XSS]]></category>

		<guid isPermaLink="false">http://techblog.iamzellux.com/?p=62794</guid>
		<description><![CDATA[Blueprint: Robust prevention of cross-site scripting attacks for existing browsers

这篇论文提出了一种防范是跨站脚本攻击(XSS)的新的方法，发在IEEE S&#038;P 2009上，作者是UIUC的Mike Ter Louw。]]></description>
			<content:encoded><![CDATA[<p>Blueprint: Robust prevention of cross-site scripting attacks for existing browsers</p>
<p>这篇论文提出了一种防范是跨站脚本攻击(XSS)的新的方法，发在IEEE S&amp;P 2009上，作者是UIUC的Mike Ter Louw。</p>
<p>所谓跨站脚本攻击，简单地说就是在网页中注入非法的脚本代码，从而达到攻击的效果。比较著名的例子有当年在MySpace上泛滥的<a href="http://en.wikipedia.org/wiki/Samy_(XSS)" target="_blank">Samy蠕虫</a>，通过特殊的脚本注入手段，每一位访问Samy主页的用户，他们的主页都会被修改加上一段Samy is my hero文字，并且他们的主页也会被植入攻击代码，从而把这段脚本扩散给更多的用户。</p>
<p>通常防范跨站脚本攻击的方式有两种。一种做在服务器端，为每一段用户上传的内容做检查，并剔除恶意代码。但这种方式很难保证能过滤掉所有的恶意字符串，一方面攻击方法防不甚防，有兴趣的朋友可以参考下<a href="http://ha.ckers.org/xss.html" target="_blank">XSS Cheat Sheet</a>，上面给出了很多一般人很难想到的攻击代码的组合方式。另一方面由于现在大多数论坛和博客都支持&lt;b&gt; &lt;i&gt;等修饰标签，所以简单的标签剔除或者重新编码都不可行。</p>
<p>另一种方法是做在浏览器端，但是由于浏览器无法区分某一段脚本到底是来源于不可信的用户还是可信的站点，所以这种方法实现起来也有很大的困难。</p>
<p>这里实现防范措施的一个难点在于，<strong>Web应用把生成HTML的返回给浏览器后，就不参与浏览器的HTML解析工作了</strong>。这样浏览器就不知道哪部分出现脚本是安全的，哪部分出现是不安全的。</p>
<p>BluePrint就着眼于这个点，提出了一种让Web应用“参与”HTML解析工作的设计。下面通过论文里面的一个例子，简单介绍下它的防范机制。</p>
<p>假如一位恶意的用户在一个博客上上传了这样一段含有恶意代码的留言：</p>
<pre class="brush: html">&lt;p&gt;
Here is a page you might find
&lt;b """&gt;&lt;script&gt;doEvil(. . .)&lt;/script&gt;"&gt;very&lt;/b&gt;
interesting:
&lt;a href=" &amp;#14; javasc&amp;#x0A;ript:doEvil(. . .);"&gt;
Link&lt;/a&gt;
&lt;/p&gt;&lt;p style="nop:expres/*xss*/sion(doEvil(. . .))"&gt;
Respectfully,
Eve
&lt;/p&gt;</pre>
<p>可以看到，这段代码里包含了很多可能引发脚本执行的代码，而要在服务器端把这些所有隐藏的攻击可能找出来是一件比较困难的事。那么BluePrint是怎么在不知道这段代码是否含有恶意代码的前提下处理的呢？</p>
<p>首先，这种由用户上传的不可信的字符串会先在服务器端被解析成一棵树，就像HTML在浏览器中被解析一样，这棵HTML解析树可以用一些简单的DOM API来生成，例如appendChild, createElement等。这些描述如何生成HTML解析树的方法会和数据值（URL、标签属性等）一起，通过特殊的编码（Base64）传递给浏览器。例如上面这段代码，最后在浏览器接收到的HTML中，会变成这样：</p>
<pre class="brush: html">&lt;code style="display:none;" id="__bp1"&gt;
=Enk/sCkhlcmUgaXMgYSBwYWdlIHlvdSBta...
=SkKICAgICI+dmVyeQ===C/k/QIGhlbHBmd...
=ECg===C/Enk/gCiAgUmVzcGVjdGZ1bGx5L...
&lt;/code&gt;&lt;script id="__bp1s"&gt;
__bp__.cxPCData("__bp1", "__bp1s");
&lt;/script&gt;</pre>
<p>在浏览器端，这段特殊的代码会被JS库解析成自定义的命令和数据格式，并由前面提到的DOM API动态生成这些HTML结点，从而达到和传统的方式一样的显示效果。当然可信的HTML代码，例如文章正文，还是按传统的方式传输的。</p>
<p>通过这种方式，<strong>BluePrint绕过了浏览器对不可信代码的解析，从而防止了不可信代码里内嵌的脚本的执行</strong>。</p>
<p>此外还有一些细节的问题，例如为什么使用Base64编码来描述自定义的命令和数据，而不是常用的例如UTF-8呢？这是因为使用UTF-8的话攻击者就有可能通过构造一段特殊的字符串，而这段字符串对应的编码恰好能起到攻击作用。而使用Base64编码就不会有这个问题。</p>
<p>攻击例子中的第5行和第7行还分别包括了通过恶意URL和CSS风格实现的代码，前面提到的措施还不足以防范这两种类型的攻击。论文里面也提到了相应的解决方案，这里不再赘述，有兴趣的朋友可以搜索论文阅读相关部分。</p>
<p>把BluePrint整合到现有的应用程序里也不难，只要把包含不可信内容显示部分的代码重新加一层包装就行了，像这样：</p>
<pre class="brush: php">// Code for trusted blog content
// appears untransformed aboveˆˆ.
&lt;?php foreach ($comments as $comment): ?&gt;
    &lt;li&gt;
        &lt;?php
$model = Blueprint::cxPCData($comment);
echo($model);
        ?&gt;
    &lt;/li&gt;
&lt;?php endforeach; ?&gt;</pre>
<p>在BluePrint的开销方面，包含25个用户评论的wordpress页面产生速度慢了55%，不过作者提到wordpress本身还有HTML解析和恶意代码检查过滤的功能，用了BluePrint后就不需要这些冗余的检查了，所以把这部分代码去掉会快不少。另外由于不可信内容都需要动态的被解码并创建相应的HTML结点，浏览器端的显示速度慢了很多，作者也解释到这种解析开销其实并不重要，因为通常看一篇博文的时候都是先看内容，由于文章内容本身是可信的，所以会以传统的方式传输并显示，若干秒后再显示评论也未必会对用户体验造成太大的影响。</p>
<p>这篇论文给我的感觉是思路很清晰，抓住了主要的难点后用了对应的方法绕过了浏览器的HTML解析。不过应用面上还有一些局限，只能防止不可信代码中脚本的执行，对于需要执行脚本的情形（例如Blogger上的Gadget）就不适用了。MIT去年发在EuroSys &#8216;09上的<a href="http://portal.acm.org/citation.cfm?id=1519091" target="_blank">BFlow</a>就是针对这样一种情形，通过类似于Flume的标签系统，使得不可信的脚本读取了隐私数据后就无法将它们传输给不可信的网站。</p>
<p>用beamer做了slides，在这里可以下载到: <a href="http://zellux-notes.googlecode.com/hg/slides/blueprint/">http://zellux-notes.googlecode.com/hg/slides/blueprint/</a></p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>2008/05/05 -- <a href="http://techblog.iamzellux.com/2008/05/subvirt-implementing-malware-with-virtual-machines/" title="SubVirt: Implementing malware with virtual machines">SubVirt: Implementing malware with virtual machines</a></li><li>2009/05/06 -- <a href="http://techblog.iamzellux.com/2009/05/eurosys-09-orchestra/" title="EuroSys 09 &#8211; Orchestra">EuroSys 09 &#8211; Orchestra</a></li><li>2009/03/18 -- <a href="http://techblog.iamzellux.com/2009/03/a-logic-of-authentication/" title="安全方面的经典论文：A Logic of Authentication">安全方面的经典论文：A Logic of Authentication</a></li><li>2008/06/19 -- <a href="http://techblog.iamzellux.com/2008/06/compatibility-is-not-transparency-vmm-detection-myths-and-realities/" title="HotOS 07 &#8211; Compatibility is Not Transparency">HotOS 07 &#8211; Compatibility is Not Transparency</a></li><li>2008/05/28 -- <a href="http://techblog.iamzellux.com/2008/05/application-level-isolation-and-recovery-with-solitude/" title="EuroSys 08 &#8211; Solitude: App-Level Isolation and Recovery">EuroSys 08 &#8211; Solitude: App-Level Isolation and Recovery</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://techblog.iamzellux.com/2010/03/blueprint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>记录GNU screen中的历史命令</title>
		<link>http://techblog.iamzellux.com/2010/03/keep-gnu-screen-history/</link>
		<comments>http://techblog.iamzellux.com/2010/03/keep-gnu-screen-history/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 16:14:57 +0000</pubDate>
		<dc:creator>zellux</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[screen]]></category>

		<guid isPermaLink="false">http://techblog.iamzellux.com/?p=62787</guid>
		<description><![CDATA[GNU screen中执行的历史命令保存在内存中，默认情况下并不会像在bash中直接执行的命令一样保存在.bash_history中，这在某些场合下带来了一定的不便。
]]></description>
			<content:encoded><![CDATA[<p>GNU screen中执行的历史命令保存在内存中，默认情况下并不会像在bash中直接执行的命令一样保存在.bash_history中，这在某些场合下带来了一定的不便。</p>
<p>在superuser上看到一个<a href="http://superuser.com/questions/37576/can-history-files-be-unified-in-" target="_blank">解决方法</a>，指定历史文件的读写方式为追加，并在每次命令行提示符显示的时候，自动更新bash的历史命令记录。要实现这个方法很简单，只要在.bashrc中加入下面两行代码即可</p>
<pre class="brush: bash">shopt -s histappend
export PROMPT_COMMAND="history -a; history -n"</pre>
<p>另外如果之前设置过PROMPT_COMMAND的话，只要在<span style="text-decoration: underline;">history -a</span>前加入<span style="text-decoration: underline;">$PROMPT_COMMAND;</span> 就行了。</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>2010/01/21 -- <a href="http://techblog.iamzellux.com/2010/01/grep-trick/" title="使用grep查找进程的技巧">使用grep查找进程的技巧</a></li><li>2008/10/23 -- <a href="http://techblog.iamzellux.com/2008/10/archlinux-svn-server/" title="ArchLinux中架设svn服务器">ArchLinux中架设svn服务器</a></li><li>2008/09/24 -- <a href="http://techblog.iamzellux.com/2008/09/the-slub-allocator/" title="The SLUB allocator">The SLUB allocator</a></li><li>2008/08/15 -- <a href="http://techblog.iamzellux.com/2008/08/why-does-switch_to-need-three-arguments/" title="switch_to宏的第三个参数">switch_to宏的第三个参数</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://techblog.iamzellux.com/2010/03/keep-gnu-screen-history/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>因为1个异常导入190个包</title>
		<link>http://techblog.iamzellux.com/2010/02/ubuntu-python-aport-bug/</link>
		<comments>http://techblog.iamzellux.com/2010/02/ubuntu-python-aport-bug/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 16:32:49 +0000</pubDate>
		<dc:creator>zellux</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://techblog.iamzellux.com/?p=62782</guid>
		<description><![CDATA[Ubuntu Apport系统中的一个bug导致了Python发生异常的时侯会额外的import 190个包。]]></description>
			<content:encoded><![CDATA[<p>今天reddit programming上的<a href="http://www.reddit.com/r/programming/comments/b6qho/ubuntu_python_raise_an_exception_import_190/" target="_blank">一个帖子</a>，提到了ubuntu上python-apport包的一个bug。</p>
<p>重现这个bug很简单：</p>
<pre class="brush: python">Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import sys
&gt;&gt;&gt; len(sys.modules)
35
&gt;&gt;&gt; foo
Traceback (most recent call last):
  File "", line 1, in
NameError: name 'foo' is not defined
&gt;&gt;&gt; len(sys.modules)
225
&gt;&gt;&gt;</pre>
<p>在执行foo之前，当前导入的包只有35个，执行之后却有了225个。问题出在Ubuntu的<a href="https://wiki.ubuntu.com/Apport" target="_blank">Apport</a>系统，它为Python添加了一个异常处理方法，这里面的bug导致了这些多余的包被导入。</p>
<p><a href="https://bugs.launchpad.net/ubuntu/+source/apport/+bug/528355" target="_blank">这个bug</a>已经被人提交并被确认，目前暂时还没有补丁，最简单的避免方法就是把python-apport包卸载 <img src='http://techblog.iamzellux.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>原文地址： <a href="http://rhodesmill.org/brandon/2010/ubuntu-exception-190-modules/">http://rhodesmill.org/brandon/2010/ubuntu-exception-190-modules/</a></p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>2009/12/17 -- <a href="http://techblog.iamzellux.com/2009/12/linux-xen-compiling-error-in-ubuntu/" title="Ubuntu下编译Linux-xen的问题">Ubuntu下编译Linux-xen的问题</a></li><li>2009/06/16 -- <a href="http://techblog.iamzellux.com/2009/06/svn-sms-notification/" title="SVN更新短信通知脚本">SVN更新短信通知脚本</a></li><li>2009/04/10 -- <a href="http://techblog.iamzellux.com/2009/04/tuple-pronunciation/" title="关于tuple的读音">关于tuple的读音</a></li><li>2009/04/07 -- <a href="http://techblog.iamzellux.com/2009/04/xen-test-script/" title="Xen DomainU自动测试脚本">Xen DomainU自动测试脚本</a></li><li>2008/11/17 -- <a href="http://techblog.iamzellux.com/2008/11/daily-notes-on-python1117-1123/" title="Daily Notes on Python[11.17-11.23]">Daily Notes on Python[11.17-11.23]</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://techblog.iamzellux.com/2010/02/ubuntu-python-aport-bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SICP里提到的画图语言</title>
		<link>http://techblog.iamzellux.com/2010/02/the-sicp-picture-language/</link>
		<comments>http://techblog.iamzellux.com/2010/02/the-sicp-picture-language/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 12:15:03 +0000</pubDate>
		<dc:creator>zellux</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[SICP]]></category>

		<guid isPermaLink="false">http://techblog.iamzellux.com/?p=62771</guid>
		<description><![CDATA[使用PLT Scheme中的sicp.plt包实现SICP上的图形程序]]></description>
			<content:encoded><![CDATA[<p>SICP第二章里提到了一种用来画图的Lisp方言，用来演示数据抽象和闭包的表达能力（见<a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-15.html#%_sec_2.2.4">http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-15.html#%_sec_2.2.4</a>）。</p>
<p>最近尝试了下，发现soegaard同学已经在PLT Scheme中实现了一个类似的库，可以很方便的在DrScheme上使用。</p>
<p>sicp.plt包使用很简单，在Language-&gt;Choose Language中选择Module，然后在需要用到这个包的时候用</p>
<pre class="brush: lisp">(require (planet soegaard/sicp:2:1/sicp))</pre>
<p>声明即可。第一次运行时DrScheme会自动下载这个包并安装，如果网络有限制可以先从<a href="http://planet.plt-scheme.org/display.ss?package=sicp.plt&amp;owner=soegaard">http://planet.plt-scheme.org/display.ss?package=sicp.plt&amp;owner=soegaard</a>下载然后在DrScheme中选择本地包安装。</p>
<p>另外SICP上使用的两个painter（wave和rogers）没有在这个包里提供，取而代之是diagonal-shading和einstein。</p>
<p>下面这个程序显示了一个简单的分形图像：</p>
<pre class="brush: lisp">#lang scheme

(require (planet "sicp.ss" ("soegaard" "sicp.plt" 2 1)))

(define (right-split painter n)
  (if (= n 0)
      painter
      (let ((smaller (right-split painter (- n 1))))
        (beside painter (below smaller smaller)))))

(define (up-split painter n)
  (if (= n 0)
      painter
      (let ((smaller (up-split painter (- n 1))))
        (below painter (beside smaller smaller)))))

(define (corner-split painter n)
  (if (= n 0)
      painter
      (let ((up (up-split painter (- n 1)))
            (right (right-split painter (- n 1))))
        (let ((top-left (beside up up))
              (bottom-right (below right right))
              (corner (corner-split painter (- n 1))))
          (beside (below painter top-left)
                  (below bottom-right corner))))))

(paint (corner-split diagonal-shading 4))</pre>
<p>程序输出：</p>
<p><a href="http://techblog.iamzellux.com/wp-content/uploads/2010/02/corner-split.png" rel="shadowbox[post-62771];player=img;"><img class="alignnone size-full wp-image-62772" title="corner-split" src="http://techblog.iamzellux.com/wp-content/uploads/2010/02/corner-split.png" alt="" width="128" height="128" /></a></p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>No Related Post</li></ul>]]></content:encoded>
			<wfw:commentRss>http://techblog.iamzellux.com/2010/02/the-sicp-picture-language/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>优化gitk的字体显示</title>
		<link>http://techblog.iamzellux.com/2010/02/gitk-font/</link>
		<comments>http://techblog.iamzellux.com/2010/02/gitk-font/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 11:08:35 +0000</pubDate>
		<dc:creator>zellux</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[Fonts]]></category>
		<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://techblog.iamzellux.com/?p=62763</guid>
		<description><![CDATA[gitk是用Tcl/Tk写的工具，默认使用Tk 8.4，不支持抗锯齿，因此字体显示很难看。好在Tk 8.5支持了部分抗锯齿字体，修改gitk使用Tk 8.5后显示效果会好一点。
]]></description>
			<content:encoded><![CDATA[<p>gitk是用Tcl/Tk写的工具，默认使用Tk 8.4，不支持抗锯齿，因此字体显示很难看。好在Tk 8.5支持了部分抗锯齿字体，修改gitk使用Tk 8.5后显示效果会好一点。</p>
<p>以Ubuntu为例，安装tk8.5包后，编辑/usr/bin/gitk文件，把开头调用wish的那行</p>
<pre class="brush: bash; gutter: false;">exec /usr/bin/wish "$0" -- "$@"</pre>
<p>改成</p>
<pre class="brush: bash; gutter: false;">exec /usr/bin/wish8.5 "$0" -- "$@"</pre>
<p>这样就能在gitk中开启抗锯齿了，虽然效果还不是很好。另外<a href="http://digilander.libero.it/mcostalba/" target="_blank">qgit</a>也是一个不错的选择。</p>
<p>参考链接：<a href="http://navarra.ca/?p=44" target="_blank">http://navarra.ca/?p=44</a></p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>2010/01/25 -- <a href="http://techblog.iamzellux.com/2010/01/git-completion/" title="Git命令行自动补全">Git命令行自动补全</a></li><li>2009/11/10 -- <a href="http://techblog.iamzellux.com/2009/11/git-modify-specified-commit/" title="在git中修改之前提交的内容">在git中修改之前提交的内容</a></li><li>2009/05/26 -- <a href="http://techblog.iamzellux.com/2009/05/install-git-gitoss-gitweb-in-archlinux/" title="ArchLinux下安装git, gitosis, gitweb服务">ArchLinux下安装git, gitosis, gitweb服务</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://techblog.iamzellux.com/2010/02/gitk-font/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WPtouch</title>
		<link>http://techblog.iamzellux.com/2010/02/wptouch/</link>
		<comments>http://techblog.iamzellux.com/2010/02/wptouch/#comments</comments>
		<pubDate>Sat, 13 Feb 2010 05:52:23 +0000</pubDate>
		<dc:creator>zellux</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[iPod Touch]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://techblog.iamzellux.com/?p=62753</guid>
		<description><![CDATA[这是一个让wordpress对移动设备更友好的插件，下载地址在<a href="http://wordpress.org/extend/plugins/wptouch/">http://wordpress.org/extend/plugins/wptouch/</a>，当然也可以在wordpress后台查找wptouch并安装。
]]></description>
			<content:encoded><![CDATA[<p>这是一个让wordpress对移动设备更友好的插件，下载地址在<a href="http://wordpress.org/extend/plugins/wptouch/">http://wordpress.org/extend/plugins/wptouch/</a>，当然也可以在wordpress后台查找wptouch并安装。</p>
<p>在iPod Touch上的效果图</p>
<p><a href="http://techblog.iamzellux.com/wp-content/uploads/2010/02/wptouch1.png" rel="shadowbox[post-62753];player=img;"><img class="alignnone size-medium wp-image-62759" title="wptouch1" src="http://techblog.iamzellux.com/wp-content/uploads/2010/02/wptouch1-200x300.png" alt="" width="200" height="300" /></a> <a href="http://techblog.iamzellux.com/wp-content/uploads/2010/02/wptouch2.png" rel="shadowbox[post-62753];player=img;"><img class="alignnone size-medium wp-image-62760" title="wptouch2" src="http://techblog.iamzellux.com/wp-content/uploads/2010/02/wptouch2-200x300.png" alt="" width="200" height="300" /></a></p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>2009/10/21 -- <a href="http://techblog.iamzellux.com/2009/10/pocket-informant/" title="推荐个iPhone/iPod Touch上的时间管理工具">推荐个iPhone/iPod Touch上的时间管理工具</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://techblog.iamzellux.com/2010/02/wptouch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linear Page Table: 更方便地访问页表</title>
		<link>http://techblog.iamzellux.com/2010/02/linear-page-table/</link>
		<comments>http://techblog.iamzellux.com/2010/02/linear-page-table/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 06:33:54 +0000</pubDate>
		<dc:creator>zellux</dc:creator>
				<category><![CDATA[Computer System]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[Memory Management]]></category>
		<category><![CDATA[Page Table]]></category>

		<guid isPermaLink="false">http://techblog.iamzellux.com/?p=62746</guid>
		<description><![CDATA[Linear page table又叫virtual page table，是一种方便虚拟机监控器(VMM)/操作系统(OS)/应用程序访问页表的技巧。Xen、64位Linux内核、JOS操作系统中都用到了这种技巧。这里以x86_32的虚拟内存管理为例，简单介绍一下它的实现和使用，如有错误敬请指出。]]></description>
			<content:encoded><![CDATA[<p>Linear page table又叫virtual page table，是一种方便虚拟机监控器(VMM)/操作系统(OS)/应用程序访问页表的技巧。Xen、64位Linux内核、JOS操作系统中都用到了这种技巧。这里以x86_32的虚拟内存管理为例，简单介绍一下它的实现和使用，如有错误敬请指出。</p>
<p>一般情况下，如果OS需要访问某个页表，需要将它映射到自己的虚拟空间中，然后再访问。这样带来两个问题，一是访问比较繁琐，需要临时的页映射；二是对于Exokernel这种fork等行为都是在用户态程序实现的系统，可能会增加一下安全上的问题。因为用户程序在fork的时候需要访问自己的页表，而这时候除非操作系统提供另一些权限控制更精确的系统调用，否则就很难让不可信的应用程序访问自己的页表且不做有害的改动。</p>
<p>Linear page table很好的解决了这两个问题。它的实现很简单，只需要在页目录中增加一项VPT，和一般的页目录项不同的是，<strong>这个VPT指向的是页目录本身</strong>。</p>
<p>这样带来了什么好处呢？借用一下<a href="http://pdos.csail.mit.edu/6.828/2009/lec/l5.html" target="_blank">MIT 6.828课件</a>上的图片来更好的说明这个问题</p>
<p><img class="alignnone" title="VPT" src="http://pdos.csail.mit.edu/6.828/2009/lec/vpt.png" alt="" width="612" height="400" /></p>
<p>增加了VPT后，通常的物理地址-&gt;虚拟地址的转换还是没变。和之前唯一的不同在于虚拟地址的页目录索引号(PDX)为之前设置的VPT的时候。</p>
<p>举个例子来说，假如现在要访问的虚拟地址是(VPT &lt;&lt; 22) | (VPT &lt;&lt; 12)，即这里的PDX和PTX都等于VPT的时候，整个转换过程是怎么样的呢（假设TLB miss的情况）？首先根据cr3中的物理地址，硬件开始查找页目录中的第VPT项，然后根据这一项中的物理地址，找到了下一级“页表”。注意这时候硬件以为自己得到的页表地址，实际上访问的还是页目录本身。同样，在这个“页表”中找到第VPT项指出去的最终页，得到了最终页的物理地址。因为PTX还是等于VPT，所以最后得到的物理地址还是页目录的。</p>
<p>也就是说，通常的页表访问的顺序是 CR3-&gt;页目录-&gt;页表-&gt;最终页，现在访问这个特殊地址的过程则成了 CR3-&gt;页目录-&gt;页目录-&gt;页目录，通过VPT这一项在页目录上绕了两圈后返回。</p>
<p>接下来，再来看看如何通过这个机制来访问某个页表，假如现在要访问第i个页目录项指向的页表上的第j项，那么我们应该构造这样一个特殊地址：</p>
<pre class="brush: c">(VPT &lt;&lt; 22) | (i &lt;&lt; 12) | (j * 4)</pre>
<p>即PDX=VPT, PTX=i, offset=j*4。通过这个地址就能得到需要的页表项了，另外由于(i &lt;&lt; 12) | (j * 4) = (i * 1024 + j) * 4，定义vpn为虚拟页的编号，vpn = i * 1024 + j，则这个地址可以转换为</p>
<pre class="brush: c">(VPT &lt;&lt; 22) + vpn * 4</pre>
<p>在JOS中，就是把vpt定义为一个uint32_t的数组，然后vpt[vpn]就是第vpn个虚拟页的页表项了。前面提到的另一个问题，如果要让用户以只读权限访问页表，又应该怎么做呢？很简单，在页目录中为用户设置另一个只读项，指向页目录自己就行了。</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>2009/05/12 -- <a href="http://techblog.iamzellux.com/2009/05/isca-09-multi-execution/" title="ISCA 09 &#8211; Multi-Execution">ISCA 09 &#8211; Multi-Execution</a></li><li>2009/05/11 -- <a href="http://techblog.iamzellux.com/2009/05/page-coloring/" title="Page Coloring">Page Coloring</a></li><li>2009/04/03 -- <a href="http://techblog.iamzellux.com/2009/04/vmware-linux-kernel-config/" title="VMware上能跑起来的Linux Kernel配置">VMware上能跑起来的Linux Kernel配置</a></li><li>2008/09/24 -- <a href="http://techblog.iamzellux.com/2008/09/the-slub-allocator/" title="The SLUB allocator">The SLUB allocator</a></li><li>2008/09/11 -- <a href="http://techblog.iamzellux.com/2008/09/sysenter-intro/" title="sysenter的介绍">sysenter的介绍</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://techblog.iamzellux.com/2010/02/linear-page-table/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>gcc中设置特定代码块的优化级别</title>
		<link>http://techblog.iamzellux.com/2010/02/code-specific-optimizations-in-gcc/</link>
		<comments>http://techblog.iamzellux.com/2010/02/code-specific-optimizations-in-gcc/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 14:16:34 +0000</pubDate>
		<dc:creator>zellux</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[gcc]]></category>

		<guid isPermaLink="false">http://techblog.iamzellux.com/?p=62740</guid>
		<description><![CDATA[gcc 4.4的特性<a href="http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Function-Specific-Option-Pragmas.html" target="_blank">Function Specific Option Pragmas</a>，在特定代码前保存当前的编译选项，然后对特定的代码使用O0优化级别，最后再恢复之前保存的编译选项。]]></description>
			<content:encoded><![CDATA[<p>今天碰到一个gcc优化相关的问题，为了让一个页变成脏页（页表中dirty位被置上），需要执行下面这段代码：</p>
<pre class="brush: c">uint32_t *page;
// ...
page[0] = page[0];</pre>
<p>最后一行代码很有可能被gcc优化掉，因为这段代码看起来没有任何实际的作用。那么如何防止gcc对这段代码做优化呢？</p>
<p>设置gcc编译时优化级别为-O0肯定是不合适的，这样对程序性能影响会比较大。stackoverflow上的Dietrich Epp给出了一个强制类型转换的方案：</p>
<pre class="brush: c">((unsigned char volatile *)page)[0] = page[0];</pre>
<p>通过volatile关键字禁止gcc的优化，和我之前采用的方法类似。</p>
<p>Plow同学给出了另一个利用gcc 4.4特性的方法：</p>
<pre class="brush: c">#pragma GCC push_options
#pragma GCC optimize ("O0")

your code

#pragma GCC pop_options</pre>
<p>这里用到了gcc 4.4的特性<a href="http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Function-Specific-Option-Pragmas.html" target="_blank">Function Specific Option Pragmas</a>，在特定代码前保存当前的编译选项，然后对特定的代码使用O0优化级别，最后再恢复之前保存的编译选项。</p>
<p>俺觉得这个特性有些场合下挺好用的，在这里分享下，虽然因为编译器版本问题最后我还是用了前面一种方法。</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>2010/01/28 -- <a href="http://techblog.iamzellux.com/2010/01/obfuscated-c/" title="这样也能算圆周率">这样也能算圆周率</a></li><li>2010/01/26 -- <a href="http://techblog.iamzellux.com/2010/01/force-int-0x80-for-syscall/" title="强制程序使用int 0&#215;80做系统调用">强制程序使用int 0&#215;80做系统调用</a></li><li>2008/08/20 -- <a href="http://techblog.iamzellux.com/2008/08/asmlinkage-and-regparm/" title="[zz]asmlinkage &amp; regparm">[zz]asmlinkage &amp; regparm</a></li><li>2008/08/20 -- <a href="http://techblog.iamzellux.com/2008/08/export-symbols-in-asm/" title="汇编文件中导出函数符号">汇编文件中导出函数符号</a></li><li>2008/04/15 -- <a href="http://techblog.iamzellux.com/2008/04/count-1-bits-in-an-int32/" title="《编程之美》上的一道题目的讨论">《编程之美》上的一道题目的讨论</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://techblog.iamzellux.com/2010/02/code-specific-optimizations-in-gcc/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ecb和cscope的结合使用</title>
		<link>http://techblog.iamzellux.com/2010/02/bind-cscope-to-ecb/</link>
		<comments>http://techblog.iamzellux.com/2010/02/bind-cscope-to-ecb/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 15:30:07 +0000</pubDate>
		<dc:creator>zellux</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[cscope]]></category>
		<category><![CDATA[ECB]]></category>
		<category><![CDATA[Emacs]]></category>

		<guid isPermaLink="false">http://techblog.iamzellux.com/?p=62712</guid>
		<description><![CDATA[前几天<a href="http://techblog.iamzellux.com/2010/02/ecb-beginning/" target="_blank">试用了下ECB</a>，非常喜欢它的定义列表和文件浏览历史的功能。但是却发现了另外一个问题：使用ECB之前我把整个窗口分成左右两块，左边是代码，右边是cscope的查找结果，现在开启ECB之后就不能再切一块窗口给cscope用了。
]]></description>
			<content:encoded><![CDATA[<p>前几天<a href="http://techblog.iamzellux.com/2010/02/ecb-beginning/" target="_blank">试用了下ECB</a>，非常喜欢它的定义列表和文件浏览历史的功能。但是却发现了另外一个问题：使用ECB之前我把整个窗口分成左右两块，左边是代码，右边是cscope的查找结果，现在开启ECB之后就不能再切一块窗口给cscope用了。</p>
<p>感谢<a href="http://stackoverflow.com/questions/2190062/how-to-remove-directory-window-and-add-a-cscope-window-in-emacs-ecb/2192059#2192059" target="_blank">stackoverflow上的sanitynic</a>，给出了自定义ECB窗口的参考。现在俺终于能把cscope窗口绑定到屏幕左下角啦。</p>
<p><a href="http://techblog.iamzellux.com/wp-content/uploads/2010/02/Screenshot-emacs@zellux-laptop-1.png" rel="shadowbox[post-62712];player=img;"><img class="alignnone size-medium wp-image-62726" title="Screenshot-emacs@zellux-laptop-1" src="http://techblog.iamzellux.com/wp-content/uploads/2010/02/Screenshot-emacs@zellux-laptop-1-300x170.png" alt="" width="300" height="170" /></a></p>
<p>自定义ECB layout其实也挺方便的，上图对应的配置为</p>
<pre class="brush: lisp">(ecb-layout-define "my-cscope-layout" left nil
                   (ecb-set-methods-buffer)
                   (ecb-split-ver 0.5 t)
                   (other-window 1)
                   (ecb-set-history-buffer)
                   (ecb-split-ver 0.25 t)
                   (other-window 1)
                   (ecb-set-cscope-buffer))

(defecb-window-dedicator ecb-set-cscope-buffer " *ECB cscope-buf*"
                         (switch-to-buffer "*cscope*"))

(setq ecb-layout-name "my-cscope-layout")

;; Disable buckets so that history buffer can display more entries
(setq ecb-history-make-buckets 'never)</pre>
<p>my-cscope-layout这个layout左边窗口分为三部分，最上面的函数列表占一半高度，中间为历史文件列表，下面为cscope的查找结果，它们各占四分之一的高度。</p>
<p>另外再简单提下cscope插件的安装和配置，使用前需确认当前系统已经安装了cscope，另外要有cscope-indexer这个脚本。在cscope/contrib目录下找到一个xcscope.el，复制到Emacs的插件目录中，并在Emacs初始化文件中加入</p>
<pre class="brush: lisp">(require 'xcscope)</pre>
<p>即可。某些发行版的包里面似乎没有cscope-indexer和xcscope.el，直接从网上下一个好了。</p>
<p>几个常用的快捷键：<br />
<code>C-c s I</code> 建立cscope索引<br />
<code>C-c s a</code> 设置搜索目录<br />
<code>C-c s d</code> 查找定义<br />
<code>C-c s s</code> 查找字符串<br />
<code>C-c s c</code> 查找调用者<br />
<code>C-c s n</code> 下一个查找结果<br />
<code>C-c s p</code> 上一个查找结果<br />
更多的快捷键可以通过<code>C-h b</code>在cscope-minor-mode区找到。</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>2010/02/02 -- <a href="http://techblog.iamzellux.com/2010/02/ecb-beginning/" title="ECB的简单配置和使用">ECB的简单配置和使用</a></li><li>2010/02/03 -- <a href="http://techblog.iamzellux.com/2010/02/configuring-semantic/" title="为特定的项目配置semantic">为特定的项目配置semantic</a></li><li>2009/12/01 -- <a href="http://techblog.iamzellux.com/2009/12/emacs-set-styles-by-projects/" title="Emacs中对不同项目指定不同的风格">Emacs中对不同项目指定不同的风格</a></li><li>2009/06/02 -- <a href="http://techblog.iamzellux.com/2009/06/emacs-company-mode/" title="Company-mode: Emacs自动补全">Company-mode: Emacs自动补全</a></li><li>2009/05/04 -- <a href="http://techblog.iamzellux.com/2009/05/caps-lock-to-ctrl/" title="给你的小指减负：将Caps Lock键改成Ctrl">给你的小指减负：将Caps Lock键改成Ctrl</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://techblog.iamzellux.com/2010/02/bind-cscope-to-ecb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CLRS Problem 11.1-4</title>
		<link>http://techblog.iamzellux.com/2010/02/clrs-problem-111-4/</link>
		<comments>http://techblog.iamzellux.com/2010/02/clrs-problem-111-4/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 03:28:42 +0000</pubDate>
		<dc:creator>zellux</dc:creator>
				<category><![CDATA[Algorithm/Mathematics]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[CLRS]]></category>

		<guid isPermaLink="false">http://10.132.140.73/wordpress/?p=221</guid>
		<description><![CDATA[问题简单来说就是给定一个<strong>未初始化的巨大的</strong>数组，然后通过它实现一个字典。所谓未初始化是指一开始里面元素的值都是随机的，巨大是指可以假设数组长度范围很大，对这个数组做初始化工作（例如清零）的代价自然也是很大。现在的问题是，利用这个数组设计出来的字典，要求初始化、查找、插入、删除操作都能在O(1)时间内完成。
]]></description>
			<content:encoded><![CDATA[<p>简单来说就是给定一个<strong>未初始化的巨大的</strong>数组，然后通过它实现一个字典。所谓未初始化是指一开始里面元素的值都是随机的，巨大是指可以假设数组长度范围很大，对这个数组做初始化工作（例如清零）的代价自然也是很大。现在的问题是，利用这个数组设计出来的字典，要求初始化、查找、插入、删除操作都能在O(1)时间内完成。</p>
<p>Intructor&#8217;s Manual 上的解答设计了一个很巧妙的验证策略。假设T为那个巨大的数组，S为辅助栈，那么对于一个键k，如果k存在于这个字典中，则T[k]保存的是 k在S中的位置j，而S[j]则保存了k值。即1 ≤ T[k] ≤ top[S], S[ T[k] ] = k, T [ S[j] ] = j，我们称这个条件为“<strong>验证环</strong>”。这个设计的关键在于T和S能够互相验证，从而排除了未初始化位置上随机值的干扰。</p>
<p>还有一个问题就是，键k对应的值v应该怎么保存呢？其实只要维护另外一个和T或者S平行的数组就行了，既然S的元素个数远小于T，选择和S平行即可。</p>
<p>根据这个验证策略，我们就能设计出词典的基本操作了：<br />
初始化：建立一个大小为0的栈<br />
查找：给定键k，检查 1 ≤ T [k] ≤ top[S] &amp;&amp; S[ T[k] ] = k，如果满足则返回对应值，否则返回NULL<br />
插入：如果键已经存在则直接替换；否则将新的键值入栈，并且维护T[k] ← top[S]<br />
删除：要确保两件事，一是验证环要被破坏，二是栈S的空洞要被填补。通过把栈顶的元素移动到要删除的元素位置，我们能同时确保这两点：</p>
<pre class="brush: text">S[ T[k] ] ← S[ top[S] ]
S[ T[k] ] ← S [ top[S] ]
T[ S[ T[k] ] ] ← T [k]
T[k] ← 0
top[S] ← top[S] − 1</pre>
<p>所有操作都能在O(1)时间内完成</p>
<p>原题：We wish to implement a dictionary by using direct addressing on a huge array. At the start, the array entries may contain garbage, and initializing the entire array is impractical because of its size. Describe a scheme for implementing a direct-address dictionary on a huge array. Each stored object should use O(1) space; the operations SEARCH, INSERT, and DELETE should take O(1) time each; and the initialization of the data structure should take O(1) time. (Hint: Use an additional stack, whose size is the number of keys actually stored in the dictionary, to help determine whether a given entry in the huge array is valid or not.)</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li>2009/06/01 -- <a href="http://techblog.iamzellux.com/2009/06/two-function-related-interview-questions/" title="两个和函数构造相关的趣味面试题">两个和函数构造相关的趣味面试题</a></li><li>2008/10/22 -- <a href="http://techblog.iamzellux.com/2008/10/rotating-calipers/" title="Rotating Calipers">Rotating Calipers</a></li><li>2008/09/08 -- <a href="http://techblog.iamzellux.com/2008/09/find-head-in-circular-linked-list/" title="带环链表求环的起点">带环链表求环的起点</a></li><li>2008/04/23 -- <a href="http://techblog.iamzellux.com/2008/04/sorting-networks/" title="Sorting Networks">Sorting Networks</a></li><li>2008/04/15 -- <a href="http://techblog.iamzellux.com/2008/04/count-1-bits-in-an-int32/" title="《编程之美》上的一道题目的讨论">《编程之美》上的一道题目的讨论</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://techblog.iamzellux.com/2010/02/clrs-problem-111-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
