AFL-fuzz
去官網
1 | $ wget http://lcamtuf.coredump.cx/afl/releases/afl-latest.tgz |
1 | $ wget https://ftp.gnu.org/gnu/binutils/binutils-2.25.tar.gz |
Test
1 | $ mkdir afl_in afl_out |
去官網
1 | $ wget http://lcamtuf.coredump.cx/afl/releases/afl-latest.tgz |
1 | $ wget https://ftp.gnu.org/gnu/binutils/binutils-2.25.tar.gz |
Test
1 | $ mkdir afl_in afl_out |
在Stackoverflow 看到的,最近剛好再看Effective Modern C++,覺得有用,紀錄起來。
1 | #include <type_traits> |
最近想從IP Camera和FFMpeg搭上陷,不過IP Cam走得不是標準的v4l2這套,只好另尋他路。
看了幾篇文章,時做了之後可以用,增添了自信心。
紀錄一下整個思路。
仙分配一塊記憶體,當作IO Context的Buffer使用
1 | const int iBufSize = 32 * 1024; |
向FFMpeg要求建立AVIOContext
1 | int ReadFunc(void* ptr, uint8_t* buf, int buf_size) |
上面的ReadFunc
和SeekFunc
就代表Read和Seek的動作,而pContext
就是有關的上下文,ReadFunc
和SeekFunc
被呼叫時,pContext會被傳入ptr中。因此需要自行轉型成正確的上下文。
建立AVFormatContex
,這邊我是採用比較偷懶的作法,我已經知道InputFormat了,所以不需要Probe。如果要Probe可以參考Reference的連結。
1 | AVFormatContext* pCtx = avformat_alloc_context(); |
依照FFMpeg正常的流程使用
1 | if (avformat_open_input(&pCtx, "", 0, 0)) != 0) |
接著就跟一般無異了
使用完後需要釋放資源
1 | avformat_close_input(pCtx); // AVFormatContext is released by avformat_close_input |
又一個實驗性質的玩意,由於目前沒有IDE支持,一切從零開始。
1 | module Math; |
命名成add.ixx
接著main進來了
1 | #include <stdio.h> |
以下是編譯過程
1 | C:\> cl /c /experimental:module add.ixx |
一切都看起來很美好
如果我們新增一個sub.ixx
呢
1 | module Math; |
main也順勢改寫
1 | int main() |
這下子編譯救出錯了
1 | C:\> cl /c /experimental:module add.ixx |
這個情況看起來Math.ifc被sub.ixx複寫了,以至於add的資訊消失。雖然可以用Module subdomain斃掉,不過這樣子很難用啊。
試著在ixx當中放一些Template function,不過完全無效。還是只能放在Header file當中。
有了Module之後,除了Template這種避不開的之外,Header file的重要性可能會大大降低 (再也不用擔心Winsock.h 跟 Windows.h的恩怨情仇了)。也能加速奇編譯速度。不過還在實驗階段,靜觀其變吧。
Visual C++ 2015提供了一個Coroutine的實驗性實作,未來可能會被列入C++17當中,因此先來體驗一下
這寫法就跟Python差不多了,直接用yield
就行了
不過跟Javascript的Generator還是有一定程度的不同
1 | #include <iostream> |
以下是個示範程式
1 | #include <future> |
future<void>
和await
的語法實在是有點突兀(尤其是其他語言都用async
和await
之後)
如果我們拿掉await
會造成怎樣的結果
1 | void noncoro() { |
可以看到兩個函數的不同
calculate_the_answer
-> Child Thread Returncalculate_the_answer
-> Main Thread Return這個範例有點複雜,有時間在研究吧
1 | #include <windows.h> |
– Resumable functions in C++
– Coroutines in Visual Studio 2015 - Update 1
– Stackless coroutines with Visual Studio 2015
1 | template <typename T> |
1 | #include <utility> |
1 | template <typename T, typename ...Args> |
由於Ubuntu自帶的NodeJS版本實在太舊了,只好自行安裝,動手跑一遍流程,記錄下來。目前的NodeJS還無法完全支援ES6,所以還需要Babel之類的轉成ES5執行。
1 | $ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash |
1 | $ npm i -g babel-cli |
1 | $ mkdir test && cd test |
以下是測試程式,用到了ES6的let
和ES7的async
,await
等Feature。
1 | function sleep(ms = 0) { |
執行的時候遇到困難
1 | $ babel-node test.js |
因為Babel沒辦法正確的轉譯ES7語法
1 | $ npm i babel-preset-es2015 babel-preset-stage-0 --save-dev |
在目前目錄上新增.babelrc`
1 | { |
這樣程式就能漲常執行。
當然,可能還有跑在Browser的需求,因此轉成ES5還是必須的。
1 | $ babel test.js --out-file test.compiled.js |
在est.compile.js爭最前面加上
1 | require("babel-polyfill"); |
1 | $ node test.compiled.js |
這下正常了
夜路走多了總是會碰到鬼,講述一下遇到的情形
我們有兩個Shared library,liba.so
和libb.so
內容類似如此
1 | #include <stdio.h> |
和
1 | #include <stdio.h> |
如果有個程式這樣寫
1 | void outer_a(); |
當我們編譯這個程式
1 | $ gcc main.c liba.so libb.so -o main |
結果匯出呼常理預料之外ㄝ,取決於Linking順序,跟我們想要的結果不同。
將其中一個inner function加上static
即可。如
1 | static void inner() |
這樣結果就可以正常運作了,不過不適用於C++ Member Function之類的,我遇到的情形不是用這種寫法。
跟上面的方法類似,只需要將其中之一的inner隱藏寢來不可見就行了,將需要的Function輸出
1 | #define EXPORTED __attribute__((visibility("default"))) |
編譯的時候就選擇
1 | $ gcc -fPIC -shared -fvisibility=hidden b.c -o libb.so |
就可以達成想要的結果了
– how to link with two shared libraries with many conflicting functions
我只是個Javascript初新者,寫出來的可能也會務人子弟。
只是對Javascript的Asynchronous Programming Model有興趣,紀錄一下。
在最初的Javascript的設計,程式只能這樣寫。一層又一層的Callback。不蛋難以維護,並且容易出錯。
1 | function wait(func, val) { |
ES6之後引季Promise,可以將Callback從水平拓展變成垂直拓展,讓程式碼更像是一般Synchronous Programming的方式
1 | function wait(func, val) { |
不過ES6同樣引入了Generator,可以用Generator模擬Synchronous Programming的方式
asyncTaskFlow是個Generator就是控制流,只是我們要以這個順序執行
比起Promise更像是我們熟悉的方式
1 | function wait(func) { |
跟C#一樣,用Async和Awit作為最終解決方案,雖然這還處在ES7 Draft,不過日後應該會被採用
可以參考 ES7 async functions和Simplifying Asynchronous Coding with ES7 Async Functions
1 | async function finish(val) { |
1 | function wait(func) { |
有時候想要在Linux的Terminal想要印出彩色字串方便,可以有以下方法
1 | #include <stdio.h> |
這方法簡單,不過麻煩的是要手動加上Color跟Reset標籤在文字前後。
突然想到可以用C++11的新特性User defined literal
來簡化,可以減少不少手動置入的風險。也可以練習User defined literal的如何使用。
1 | #include <stdio.h> |
不過User Defined Literal前的字串似乎不能寫成”123 456”_RED這種形式。有點可惜
```