<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Everett — personal blog</title>
    <description>Building software, exploring how things work, and lessons from work and life.</description>
    <language>en</language>
    <link>https://asp.run/</link>
    <atom:link href="https://asp.run/feed.xml" rel="self" type="application/rss+xml"/>
    <lastBuildDate>Sun, 15 Nov 2026 18:03:56 +0000</lastBuildDate>
    
      
      
      
      
      
      
      
      
      
      
      <item>
        <title>Building a JavaScript Runtime with deno_core (Translation)</title>
        <description>
        
        
        
        &lt;p&gt;Chinese adaptation by Everett. Original by Bartek Iwańczuk · 2022-07-29 · &lt;a href=&quot;https://deno.com/blog/roll-your-own-javascript-runtime&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot; class=&quot;&quot;&gt;Roll your own JavaScript runtime &lt;span aria-hidden=&quot;true&quot;&gt;↗&lt;/span&gt;&lt;/a&gt;
&lt;/p&gt;
        
        &lt;p&gt;Historical note: The July 2022 tutorial using Rust 1.62, deno_core 0.142.0, and tokio 1.19.2. Not revalidated against current versions.&lt;/p&gt;
        &lt;p&gt;This is a Chinese translation and adaptation of Bartek Iwańczuk’s Deno tutorial, with a short personal introduction. For the English tutorial, use the original linked above. The Chinese text preserves the July 2022 examples.&lt;/p&gt;

&lt;!-- more --&gt;


      </description>
        <pubDate>Sun, 31 Jul 2022 00:00:00 +0000</pubDate>
        <link>https://asp.run/2022/07/31/roll-your-own-javascript-runtime/?lang=en</link>
        
        
        <guid isPermaLink="true">https://xnu.app/2022/07/31/roll-your-own-javascript-runtime/</guid>
        <category>rust</category><category>deno</category>
        <category>deno</category>
      </item>
    
      
      
      
      
      
      
      
      
      
      
      <item>
        <title>Breakpoint Every UIPasteboard Method with LLDB</title>
        <description>
        
        
        
        
        &lt;p&gt;iOS 14 added a privacy feature: when an app reads content that another app copied to the clipboard, a brief banner is shown. Like this:
&lt;img src=&quot;https://asp.run/media/15930990964673.jpg&quot; alt=&quot;-w431&quot; width=&quot;862&quot; height=&quot;250&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can inspect every clipboard-related behavior in an app by setting a breakpoint on all &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIPasteboard&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;To break on every method of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIPasteboard&lt;/code&gt;, use the following command:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;breakpoint set -r &apos;\[UIPasteboard .*\]$&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Break at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; first, then type the command above in the LLDB console.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15930999653054.jpg&quot; alt=&quot;-w728&quot; width=&quot;1456&quot; height=&quot;520&quot; /&gt;&lt;/p&gt;

&lt;p&gt;(The screenshot above only captures part of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIPasteboard&lt;/code&gt; methods.)&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Give it a try — you’ll notice that, besides our own calls, the system itself occasionally triggers &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIPasteboard&lt;/code&gt;-related calls too.&lt;/p&gt;

&lt;p&gt;Pretty interesting :)&lt;/p&gt;


      </description>
        <pubDate>Thu, 25 Jun 2020 00:00:00 +0000</pubDate>
        <link>https://asp.run/2020/06/25/break-on-uipasteboard/?lang=en</link>
        
        
        <guid isPermaLink="true">https://xnu.app/2020/06/25/break-on-uipasteboard/</guid>
        <category>iOS14</category>
        <category>lldb</category>
      </item>
    
      
      
      
      
      
      
      
      
      
      
      <item>
        <title>How to Quickly List All +load Methods in an App</title>
        <description>
        
        
        
        
        &lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Objective-C +load&lt;/code&gt; method is both magical and evil.&lt;/p&gt;

&lt;!-- more --&gt;

&lt;ul&gt;
  &lt;li&gt;When a beginner discovers it, they marvel at its magic.&lt;/li&gt;
  &lt;li&gt;When an expert discovers it, they get addicted and can’t let go.&lt;/li&gt;
  &lt;li&gt;When a veteran discovers it, they are terrified by its evil.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most large apps have already, or are trying to, get rid of it. So how do you quickly see how many &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+load&lt;/code&gt; methods your app has — and how deep the poison runs?&lt;/p&gt;

&lt;p&gt;Picture this scenario:&lt;/p&gt;

&lt;p&gt;One day you’re happily debugging in Xcode.&lt;/p&gt;

&lt;p&gt;You open Xcode and press F5.&lt;/p&gt;

&lt;p&gt;Suddenly, you want to know how many &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+load&lt;/code&gt; methods are in the app.&lt;/p&gt;

&lt;p&gt;Hit Pause, then enter:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;br s -r &quot;\+\[.+ load\]$&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15880051383019.jpg&quot; alt=&quot;-w352&quot; width=&quot;704&quot; height=&quot;220&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Then enter:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;br list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15880052076002.jpg&quot; alt=&quot;-w930&quot; width=&quot;1600&quot; height=&quot;808&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You might be surprised to find just how many (or how few) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+load&lt;/code&gt; methods your app actually has.&lt;/p&gt;

&lt;h2 id=&quot;how-it-works&quot;&gt;How it works&lt;/h2&gt;

&lt;p&gt;It uses LLDB’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;breakpoint&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15880053337758.jpg&quot; alt=&quot;-w576&quot; width=&quot;1152&quot; height=&quot;136&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;br s -r &quot;regex&quot;
is just
breakpoint set -r &quot;regex&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It sets breakpoints by matching symbols with a regular expression.&lt;/p&gt;

&lt;h2 id=&quot;a-small-question&quot;&gt;A small question&lt;/h2&gt;

&lt;p&gt;So think about it: if the code in these &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+load&lt;/code&gt; methods crashes, can your crash monitoring (Bugly, etc.) catch it?&lt;/p&gt;

&lt;p&gt;90% of the time, the answer you’ll get is: “It won’t crash.”&lt;/p&gt;

&lt;p&gt;It reminds me of something Trump said: My “code” is perfect.&lt;/p&gt;

&lt;p&gt;Haha :)&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Pretty fun :)&lt;/p&gt;

&lt;p&gt;Sigh, I’m such a noob — time to go study “Full Method Hooking Based on Trampolines”.&lt;/p&gt;

&lt;p&gt;http://satanwoo.github.io/2020/04/26/TrampolineHookOpenSource/&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;If you enjoyed this, follow my WeChat official account to show some encouragement:&lt;/p&gt;


      </description>
        <pubDate>Tue, 28 Apr 2020 00:00:00 +0000</pubDate>
        <link>https://asp.run/2020/04/28/quick-list-load-method/?lang=en</link>
        
        
        <guid isPermaLink="true">https://xnu.app/2020/04/28/quick-list-load-method/</guid>
        <category>lldb</category><category>load</category><category>breakpoint</category>
        <category>lldb</category>
      </item>
    
      
      
      
      
      
      
      
      
      
      
      <item>
        <title>LLDB: How to Set a Breakpoint at a Function&apos;s return</title>
        <description>
        
        
        
        
        &lt;p&gt;You have a complex function with lots of lines and many returns inside. Single-stepping through it is slow — how can you quickly find which line it returns from?&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;For example, this code:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;void foo() {
    int i = arc4random() %100;
    
    if (i &amp;gt; 30) {
        if (i &amp;lt; 40) {
            return;
        }
        if (i &amp;gt; 77) {
            return;
        }
        if (i &amp;lt; 66) {
            return;
        }
    }
    
    switch (i) {
        case 0:
            return;
        case 1:
            return;
        case 2:
            return;
        case 3:
            return;
        case 4:
            return;
        default:
            return;
    }
}

int main(int argc, const char * argv[]) {
    foo();
    return 0;
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Suppose foo is a very long, very complex function with many returns. How do you know which line it returned from?&lt;/p&gt;

&lt;p&gt;You can use an lldb breakpoint:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;breakpoint set -p return
或者
br set -p return
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First add a breakpoint on the first line of foo.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15875739886610.jpg&quot; alt=&quot;&quot; width=&quot;520&quot; height=&quot;162&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Once the breakpoint triggers, type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;br set -p return&lt;/code&gt; in the console.
&lt;img src=&quot;https://asp.run/media/15875740456070.jpg&quot; alt=&quot;&quot; width=&quot;395&quot; height=&quot;190&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Then continue, and it will break at the line where the function returns.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15875741244090.jpg&quot; alt=&quot;&quot; width=&quot;479&quot; height=&quot;346&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Pretty interesting~&lt;/p&gt;


      </description>
        <pubDate>Thu, 23 Apr 2020 00:00:00 +0000</pubDate>
        <link>https://asp.run/2020/04/23/how-to-break-on-return/?lang=en</link>
        
        
        <guid isPermaLink="true">https://xnu.app/2020/04/23/how-to-break-on-return/</guid>
        <category>lldb</category><category>return</category><category>breakpoint</category>
        <category>lldb</category>
      </item>
    
      
      
      
      
      
      
      
      
      
      
      <item>
        <title>When Does an Objective-C Object Held by a C++ Class Get dealloc&apos;d?</title>
        <description>
        
        
        
        
        &lt;p&gt;When using Objective-C++, you can store Objective-C objects in a C++ struct or class.&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;struct CppStruct {
    MyObject *obj;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So I suddenly wondered: if CppStruct is destructed, will MyObject be dealloc’d? No need to overthink it — of course it will. But how is that achieved?&lt;/p&gt;

&lt;p&gt;Let’s write some code to verify it:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
@interface MyObject : NSObject
@end
@implementation MyObject
- (instancetype)init {
    self = [super init];
    if (self) {
        NSLog(@&quot;MyObject init&quot;);
    }
    return self;
}
- (void)dealloc {
    NSLog(@&quot;MyObject dealloc&quot;);
}
@end

struct CppStruct {
    MyObject *obj;
    CppStruct() {
        NSLog(@&quot;CppStruct constructor&quot;);
    }
    ~CppStruct() {
        NSLog(@&quot;CppStruct destructor&quot;);
    }
};

CppStruct * CreateStruct() {
    CppStruct * s = new CppStruct();
    s-&amp;gt;obj = [[MyObject alloc] init];
    return s;
}

void FreeStruct(CppStruct *s) {
    delete s;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        CppStruct *s = CreateStruct();
        FreeStruct(s);
    }
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It produces the following output:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15871350384682.jpg&quot; alt=&quot;&quot; width=&quot;292&quot; height=&quot;87&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As you can see, MyObject indeed got dealloc’d.&lt;/p&gt;

&lt;p&gt;So, let’s set a breakpoint on dealloc and see how it gets called.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15871351008102.jpg&quot; alt=&quot;&quot; width=&quot;331&quot; height=&quot;289&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In the image above there are two &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~CppStruct()&lt;/code&gt;, one of which calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;objc_storeStrong&lt;/code&gt;, and objc_storeStrong in turn triggers MyObject’s dealloc.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15871352168452.jpg&quot; alt=&quot;&quot; width=&quot;721&quot; height=&quot;266&quot; /&gt;&lt;/p&gt;

&lt;p&gt;From the image above, it must be the compiler that generated the code calling objc_storeStrong.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15871352826095.jpg&quot; alt=&quot;&quot; width=&quot;753&quot; height=&quot;310&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Looking at the Disassembly, it indeed generated the objc_storeStrong call code.
&lt;img src=&quot;https://asp.run/media/15871353987126.jpg&quot; alt=&quot;&quot; width=&quot;289&quot; height=&quot;103&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Solid confirmation.&lt;/p&gt;

&lt;p&gt;As for how LLVM generates this logic — I won’t dig into that here.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;p&gt;The debugging above used a compilable objc runtime. There are many on GitHub; you can just search for the corresponding objc version like this:&lt;/p&gt;

&lt;p&gt;https://github.com/search?q=objc+779.1
https://github.com/LGCooci/objc4_debug&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;From now on you can boldly use C++ structs to store Objective-C objects.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Pretty interesting :)&lt;/p&gt;


      </description>
        <pubDate>Fri, 17 Apr 2020 00:00:00 +0000</pubDate>
        <link>https://asp.run/2020/04/17/how-cpp-delete-dealloc-objc-object/?lang=en</link>
        
        
        <guid isPermaLink="true">https://xnu.app/2020/04/17/how-cpp-delete-dealloc-objc-object/</guid>
        <category>objc</category><category>dealloc</category>
        <category>探索</category>
      </item>
    
      
      
      
      
      
      
      
      
      
      
      <item>
        <title>Exploring the Hidden Character (LRM) in WhatsApp&apos;s App Name</title>
        <description>
        
        
        
        
        &lt;p&gt;Welcome to this episode of “Approaching Science: The Mysterious Symbol of WhatsApp”~&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h2 id=&quot;the-crime-scene&quot;&gt;The Crime Scene&lt;/h2&gt;

&lt;p&gt;About half a year ago, maybe even longer, while using frida-ios-dump, I accidentally noticed that the WhatsApp app’s name looked a bit odd.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;frida-ios-dump is a tool for decrypting (de-shelling) apps on jailbroken iOS (it can also list iOS apps).
Link: https://github.com/AloneMonkey/frida-ios-dump&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Take a close look at WhatsApp in the image below:
&lt;img src=&quot;https://asp.run/media/15855012198617.jpg&quot; alt=&quot;&quot; width=&quot;371&quot; height=&quot;121&quot; /&gt;&lt;/p&gt;

&lt;p&gt;WhatsApp’s name is aligned on the left differently from the other apps.&lt;/p&gt;

&lt;p&gt;… how many times have I hurried past it … how many times have I treated it as if it didn’t exist …&lt;/p&gt;

&lt;p&gt;Until today, I finally got curious for once and wanted to see why it wasn’t aligned here.&lt;/p&gt;

&lt;h2 id=&quot;getting-started&quot;&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;De-shell it, let’s de-shell it and try.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python dump.py net.whatsapp.WhatsApp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After de-shelling, it looks like the image below. There’s a question mark. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?WhatsApp.ipa&lt;/code&gt; — what is that question mark?&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15855017374127.jpg&quot; alt=&quot;&quot; width=&quot;237&quot; height=&quot;86&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I was about to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mv&lt;/code&gt; it to another folder to study it, and at that very moment…&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15855018704881.jpg&quot; alt=&quot;&quot; width=&quot;456&quot; height=&quot;81&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The mysterious character &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\342\200\216&lt;/code&gt; appeared. Just as I was getting curious…&lt;/p&gt;

&lt;h2 id=&quot;a-quick-search&quot;&gt;A Quick Search&lt;/h2&gt;

&lt;p&gt;Did a casual search … and actually found it :)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15855020124999.jpg&quot; alt=&quot;&quot; width=&quot;725&quot; height=&quot;183&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;https://graphemica.com/200E&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Opening it up, it actually has a meaning~ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left-to-right mark&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15855021218820.jpg&quot; alt=&quot;&quot; width=&quot;920&quot; height=&quot;376&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;the-truth-revealed&quot;&gt;The Truth Revealed&lt;/h2&gt;

&lt;p&gt;Wikipedia also explains it:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The left-to-right mark (LRM) is a control character (an invisible formatting character) used in computerized typesetting (including word processing in a program like Microsoft Word) of text that contains a mixture of left-to-right text (such as English or Russian) and right-to-left text (such as Arabic, Persian or Hebrew). It is used to set the way adjacent characters are grouped with respect to text direction.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;https://en.wikipedia.org/wiki/Left-to-right_mark&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here’s a translation:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The left-to-right mark (LRM) is a control character, or an invisible typesetting character. It is used in computerized bidirectional document typesetting.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let me put it even more plainly. The left-to-right mark is an invisible character used to include &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left-to-right&lt;/code&gt; text within a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;right-to-left&lt;/code&gt; typesetting language (such as Arabic).&lt;/p&gt;

&lt;p&gt;The example in the image below makes it clearer: after using the LRM, the displayed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C++&lt;/code&gt; (left-to-right) is included within Arabic (right-to-left).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15855024011241.jpg&quot; alt=&quot;&quot; width=&quot;792&quot; height=&quot;236&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;p&gt;There’s a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Left-to-right mark&lt;/code&gt;, and there’s also a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Right-to-left mark&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;https://en.wikipedia.org/wiki/Right-to-left_mark&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;going-further&quot;&gt;Going Further&lt;/h2&gt;

&lt;p&gt;Pulling out WhatsApp’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Info.plist&lt;/code&gt; file and taking a look, there doesn’t seem to be anything special.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15855026225466.jpg&quot; alt=&quot;&quot; width=&quot;487&quot; height=&quot;115&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Let’s look at it with a binary editor.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15855027112567.jpg&quot; alt=&quot;&quot; width=&quot;617&quot; height=&quot;134&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\342\200\216&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xE2 0x80 0x8E (e2808e)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15855027969727.jpg&quot; alt=&quot;&quot; width=&quot;880&quot; height=&quot;444&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This way, when the code fetches &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CFBundleDisplayName&lt;/code&gt; and concatenates it with other localized languages, it can ensure WhatsApp’s order is left-to-right.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Pretty interesting :)&lt;/p&gt;


      </description>
        <pubDate>Mon, 30 Mar 2020 00:00:00 +0000</pubDate>
        <link>https://asp.run/2020/03/30/left-to-right-mark/?lang=en</link>
        
        
        <guid isPermaLink="true">https://xnu.app/2020/03/30/left-to-right-mark/</guid>
        <category>unicode</category>
        <category>探索</category>
      </item>
    
      
      
      
      
      
      
      
      
      
      
      <item>
        <title>Running Android on an iPhone: My Experience</title>
        <description>
        
        
        
        &lt;p&gt;Historical note: Project Sandcastle on an iPhone 7 in March 2020. Not revalidated against current versions.&lt;/p&gt;
        &lt;p&gt;For the first time in history, the Android system can run on an iPhone. The current version (as of March 6, 2020) only supports the iPhone 7 / 7 Plus. (No requirement on the iOS version.)&lt;/p&gt;

&lt;p&gt;Project Sandcastle: Android for the iPhone 
Project page: https://projectsandcastle.org/&lt;/p&gt;

&lt;p&gt;I happened to have an iPhone 7 on hand, so I gave it a try. Let me briefly summarize the steps and share them with everyone. Ideally the steps are as follows, but due to macOS’s security mechanisms, steps 2 and 3 aren’t quite so smooth and pleasant.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Jailbreak using checkra1n&lt;/li&gt;
  &lt;li&gt;Run start_mac.sh&lt;/li&gt;
  &lt;li&gt;Run setup_mac.sh&lt;/li&gt;
&lt;/ol&gt;

&lt;!-- more --&gt;

&lt;h2 id=&quot;jailbreak&quot;&gt;Jailbreak&lt;/h2&gt;

&lt;p&gt;Download https://checkra.in/ and jailbreak by following the steps. (Connect the iPhone to the Mac via USB.)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15834927122755.jpg&quot; alt=&quot;-w480&quot; width=&quot;960&quot; height=&quot;684&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15834927589010.jpg&quot; alt=&quot;-w480&quot; width=&quot;960&quot; height=&quot;684&quot; /&gt;
&lt;img src=&quot;https://asp.run/media/15834927957183.jpg&quot; alt=&quot;-w246&quot; width=&quot;492&quot; height=&quot;168&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15834928119092.jpg&quot; alt=&quot;-w480&quot; width=&quot;960&quot; height=&quot;684&quot; /&gt;&lt;/p&gt;

&lt;p&gt;After jailbreaking, boot into iOS.&lt;/p&gt;

&lt;h2 id=&quot;download-the-android-build&quot;&gt;Download the Android Build&lt;/h2&gt;

&lt;p&gt;Download the Android Build at https://projectsandcastle.org/status.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15835109011989.jpg&quot; alt=&quot;&quot; width=&quot;890&quot; height=&quot;375&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Unzip the downloaded file.
&lt;img src=&quot;https://asp.run/media/15835110829022.jpg&quot; alt=&quot;-w234&quot; width=&quot;468&quot; height=&quot;404&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;setup_macsh&quot;&gt;setup_mac.sh&lt;/h2&gt;

&lt;p&gt;Connect the iPhone to the Mac via USB. Ideally, you just run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./setup_mac.sh&lt;/code&gt; on macOS. But it didn’t go smoothly for me. The bundled iproxy and the two dynamic libraries’ signatures still need to be trusted on first run. But since the functionality is really just iproxy’s functionality, let’s manually run the steps inside setup_mac.sh below.&lt;/p&gt;

&lt;p&gt;You can first ssh into iOS to make sure the manual connection works. Then refer to the steps below.&lt;/p&gt;

&lt;p&gt;(1)&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;iproxy 2222 44
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;(2)
Copy isetup to iOS at /tmp/setup.sh&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;scp -P2222 -o StrictHostKeyChecking=no isetup root@localhost:/tmp/setup.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;(3)
Two approaches:&lt;/p&gt;

&lt;p&gt;One, use a VPN.
Or, modify the network connectivity test address in setup.sh — for example, test against baidu, which is specifically for checking whether the network works (looks like foreigners use google for the equivalent test).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15835114471189.jpg&quot; alt=&quot;-w627&quot; width=&quot;1254&quot; height=&quot;450&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I recommend the VPN approach, because this script downloads a 470MB file. Without a VPN, downloading this file on my home Wi-Fi is very slow; with a VPN it seemed to finish in a few minutes.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15835117141314.jpg&quot; alt=&quot;&quot; width=&quot;215&quot; height=&quot;37&quot; /&gt;&lt;/p&gt;

&lt;p&gt;(4) Run /tmp/setup.sh&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15835063026640.jpg&quot; alt=&quot;-w462&quot; width=&quot;924&quot; height=&quot;212&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;entering-dfu-mode&quot;&gt;Entering DFU Mode&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Power off the iPhone.&lt;/li&gt;
  &lt;li&gt;Hold Volume Down + Power simultaneously for 10 seconds (as exactly 10 seconds as possible).&lt;/li&gt;
  &lt;li&gt;Release the Power button, but keep holding Volume Down.&lt;/li&gt;
  &lt;li&gt;The phone’s screen will stay black, indicating it has entered DFU mode.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then in DFU mode, run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./start_mac.sh&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Ideally, you’re done once it finishes running. But reality is harsh — I ran into the dialog below (inwardly, ten thousand alpacas galloped past, terrified I’d brick the phone…). Of course, you click Cancel.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15835119951796.jpg&quot; alt=&quot;-w420&quot; width=&quot;840&quot; height=&quot;394&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Go to System Preferences -&amp;gt; Security &amp;amp; Privacy -&amp;gt; General, and click Allow Anyway.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15835120714520.jpg&quot; alt=&quot;-w608&quot; width=&quot;1216&quot; height=&quot;306&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This still doesn’t work yet. Just in case, run this once first:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./load-linux.mac
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then you can click Open.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15835077352704.jpg&quot; alt=&quot;-w469&quot; width=&quot;938&quot; height=&quot;422&quot; /&gt;&lt;/p&gt;

&lt;p&gt;But at this point, re-running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;start_mac.sh&lt;/code&gt; doesn’t seem to “resume” anymore. Inwardly, X#@%$$. The “clever” me took a look at the start_mac.sh code, and it looks like you can run this step.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./load-linux.mac Android.lzma dtbpack
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally success — Android booted up on the iPhone.&lt;/p&gt;

&lt;h2 id=&quot;screenshots&quot;&gt;Screenshots&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15835124510615.jpg&quot; alt=&quot;&quot; width=&quot;375&quot; height=&quot;594&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15835124757092.jpg&quot; alt=&quot;&quot; width=&quot;387&quot; height=&quot;631&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;It runs pretty laggy.&lt;/li&gt;
  &lt;li&gt;Rebooting restores iOS.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Still pretty interesting. For a video, you can search everettjf on Douyin.&lt;/p&gt;


      </description>
        <pubDate>Fri, 06 Mar 2020 00:00:00 +0000</pubDate>
        <link>https://asp.run/2020/03/06/android-on-iphone/?lang=en</link>
        
        
        <guid isPermaLink="true">https://xnu.app/2020/03/06/android-on-iphone/</guid>
        <category>sandcastle</category>
        <category>尝鲜</category>
      </item>
    
      
      
      
      
      
      
      
      
      
      
      <item>
        <title>Redirecting iOS Standard Output to a UITextView</title>
        <description>
        
        
        
        &lt;p&gt;Historical note: An iOS stdout experiment from 2020 that accesses C library implementation details. Not revalidated against current versions.&lt;/p&gt;
        &lt;p&gt;I came across a pretty interesting piece of code: an iOS project that redirects the content printed by printf into a UITextView.&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;After extracting the code, it’s actually quite simple.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;

static print_cbk_t gPrintFunc;

static int stdout_redirect(void* prefix, const char* buffer, int size)
{
    if (gPrintFunc)
        gPrintFunc(buffer, size);
    return size;
}

void set_redirect_output(print_cbk_t f)
{
    gPrintFunc = f;

    setvbuf(stdout, 0, _IOLBF, 0); // stdout: line-buffered
    setvbuf(stderr, 0, _IONBF, 0); // stderr: unbuffered

    stdout-&amp;gt;_write = stdout_redirect;
    stderr-&amp;gt;_write = stdout_redirect;
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then you use it in the ViewController roughly like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
static UITextView *sTextView = nil;

static void current_output(const char* buff, int len) {
    NSString *text = [sTextView text];
    text = [text stringByAppendingFormat:@&quot;%s&quot;,buff];
    [sTextView setText:text];
}

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    sTextView = self.textView;
    
    set_redirect_output(current_output);
        
    printf(&quot;hello\n&quot;);
    printf(&quot;hello\n&quot;);
    printf(&quot;hello\n&quot;);
    printf(&quot;hello\n&quot;);

}
@end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After running it, printf output goes into the UITextView.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15832504339122.jpg&quot; alt=&quot;&quot; width=&quot;329&quot; height=&quot;144&quot; /&gt;&lt;/p&gt;

&lt;p&gt;There’s really not much to explain about how it works — the C library provides the support itself.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    setvbuf(stdout, 0, _IOLBF, 0); // stdout: line-buffered
    setvbuf(stderr, 0, _IONBF, 0); // stderr: unbuffered
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;These two functions set standard output to “line-buffered” and “unbuffered” respectively. In other words, content going into stdout and stderr is output either by line or directly. This way the UITextView receives it in a more timely manner.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Writing fluff pieces is just relaxing and fun~ As long as it’s interesting~&lt;/p&gt;


      </description>
        <pubDate>Tue, 03 Mar 2020 00:00:00 +0000</pubDate>
        <link>https://asp.run/2020/03/03/redirect-printf-to-textview/?lang=en</link>
        
        
        <guid isPermaLink="true">https://xnu.app/2020/03/03/redirect-printf-to-textview/</guid>
        <category>redirect</category><category>stdout</category><category>stderr</category>
        <category>stdout</category>
      </item>
    
      
      
      
      
      
      
      
      
      
      
      <item>
        <title>Renaming Symbols with __asm__</title>
        <description>
        
        
        
        
        &lt;p&gt;The objc source code contains code like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__asm__(&quot;_objc_retain&quot;)&lt;/code&gt;. What is this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__asm__&lt;/code&gt; thing?&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h2 id=&quot;discovery&quot;&gt;Discovery&lt;/h2&gt;

&lt;p&gt;Here it is in the source:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15820819662136.jpg&quot; alt=&quot;&quot; width=&quot;486&quot; height=&quot;337&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;objc source download https://opensource.apple.com/tarballs/objc4/&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After some searching, it turns out it can rename a symbol. Let’s write some test code:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;
void foo(void) __asm__(&quot;_objc_release&quot;);
void foo(void)  {
    printf(&quot;hello world&quot;);
}
int main(int argc, const char * argv[]) {
    foo();
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s debug it:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15820826016870.jpg&quot; alt=&quot;&quot; width=&quot;659&quot; height=&quot;250&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As you can see from the image above, the symbol in the callstack is not foo, but _objc_release.&lt;/p&gt;

&lt;h2 id=&quot;variables&quot;&gt;Variables&lt;/h2&gt;

&lt;p&gt;From the link below, we can also learn that you can rename a variable’s symbol too.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;int counter __asm__(&quot;counter_v1&quot;) = 0;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0491f/Cacgegch.html&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Seems like you could use this to do a simple export symbol obfuscation for a dylib?&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15820829854361.jpg&quot; alt=&quot;&quot; width=&quot;787&quot; height=&quot;200&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.kui0097a/armcc_bcfggcdh.htm&lt;/li&gt;
  &lt;li&gt;http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0491f/Cacgegch.html&lt;/li&gt;
  &lt;li&gt;https://stackoverflow.com/questions/1034852/adding-leading-underscores-to-assembly-symbols-with-gcc-on-win32&lt;/li&gt;
&lt;/ul&gt;


      </description>
        <pubDate>Wed, 19 Feb 2020 00:00:00 +0000</pubDate>
        <link>https://asp.run/2020/02/19/asm-rename-symbols/?lang=en</link>
        
        
        <guid isPermaLink="true">https://xnu.app/2020/02/19/asm-rename-symbols/</guid>
        
        <category></category><category></category>
      </item>
    
      
      
      
      
      
      
      
      
      
      
      <item>
        <title>Is C++ Static Variable Initialization Inside a Function Thread-Safe?</title>
        <description>
        
        
        
        
        &lt;p&gt;Conclusion first: yes, it’s thread-safe, and it even uses double-checked locking, so performance is pretty good.&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h2 id=&quot;exploration&quot;&gt;Exploration&lt;/h2&gt;

&lt;p&gt;When writing the C++ Singleton pattern, you’ll usually just smoothly write code like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Hello {
public:
    static Hello &amp;amp; instance() {
        static Hello obj;
        return obj;
    }
    
    Hello() {
        printf(&quot;constructor&quot;);
    }
    ~Hello() {
        printf(&quot;destructor&quot;);
    }
    
public:
    int x = 0;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So when you call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hello::instance()&lt;/code&gt;, is it thread-safe? Can the constructor of Hello be guaranteed to run exactly once?&lt;/p&gt;

&lt;p&gt;Let’s throw the code into Hopper and take a look.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15819530050437.jpg&quot; alt=&quot;-w1201&quot; width=&quot;1600&quot; height=&quot;237&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Looks like there’s a lock. So it’s thread-safe :)&lt;/p&gt;

&lt;h2 id=&quot;going-further&quot;&gt;Going Further&lt;/h2&gt;

&lt;p&gt;Notice these few methods:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;__cxa_guard_acquire
__cxa_atexit
__cxa_guard_release
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__cxa_atexit&lt;/code&gt; is likely there to call the destructor when the process exits.&lt;/p&gt;

&lt;p&gt;After some searching, you can find this source file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cxa_guard.cxx&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;https://opensource.apple.com/source/libcppabi/libcppabi-14/src/cxa_guard.cxx&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15819532682759.jpg&quot; alt=&quot;-w609&quot; width=&quot;1218&quot; height=&quot;740&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It implements a double-checked locking via obj_guard.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15819533607278.jpg&quot; alt=&quot;-w647&quot; width=&quot;1294&quot; height=&quot;956&quot; /&gt;&lt;/p&gt;

&lt;p&gt;inUse stores 0 and 1 in the first address pointed to by obj_guard, implementing the first check.&lt;/p&gt;

&lt;p&gt;Found this obj_guard in Hopper:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15819535672052.jpg&quot; alt=&quot;&quot; width=&quot;1600&quot; height=&quot;786&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://asp.run/media/15819535936394.jpg&quot; alt=&quot;-w705&quot; width=&quot;1410&quot; height=&quot;462&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This guard variable is a global variable.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;From now on you can happily use this pattern:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    static Hello &amp;amp; instance() {
        static Hello obj;
        return obj;
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;


      </description>
        <pubDate>Mon, 17 Feb 2020 00:00:00 +0000</pubDate>
        <link>https://asp.run/2020/02/17/how-cpp-static-object-in-function-work/?lang=en</link>
        
        
        <guid isPermaLink="true">https://xnu.app/2020/02/17/how-cpp-static-object-in-function-work/</guid>
        <category>cpp</category><category>threadsafe</category><category>local-object</category>
        <category>cpp</category>
      </item>
    
  </channel>
</rss>

