ときどきの雑記帖 RE* (新南口)
廃棄物13号
たてつく
漢字で「楯突く」と書くのをつい最近知った😓
トヨクモ
南武線車内(のモニター)でよく流れているCMのあの会社 こんなことしてたのか。>Thanks OSS Award
トヨクモ株式会社の Thanks OSS Award 2022 に Prettier、Babel のメンテナーとして選んでいただきました(Babel の方は最近やれてないですが)
— sosuke (@__sosukesuzuki) October 17, 2022
これからも Prettier を中心に JavaScript のツールの維持に貢献していきますhttps://t.co/sFcJDO2qVE
渋谷駅工事
渋谷駅線路切り替え工事、山手線ホームが1つに!外回り一部区間運休 2023年1月7・8日 | RailLab ニュース(レイルラボ)
いよいよかと思って詳細を見ると
今回工事
STEP4
山手線外回りを西側に横移動し、山手線内・外回りを同一ホームにします。
以前からこう書かれていたんだろうけど気がついてなかった >外回りを西側に横移動
確かに外回りの現行ホームを廃止・撤去だけだとアレだものなあ。
あと次の段階として(これが最後?)
STEP5
山手線の線路・ホームの高さを上げます。
必要があるからやるんだろうけど、なんでだろう?
あるコンパイラ言語
[Q&A] あるコンパイラ言語の新旧の仕様の差について - Qiita
お知らせ
というツイートが流れてきたんだけど、つい昨日
都内に誕生する『ムーミン』の新しいショップやカフェの計画が発表! | マイナビニュース
「MOOMIN SHOP GINZA」は今年12月にオープン。『ムーミン』を体現した新しいMOOMIN SHOPで、『ムーミン』の原作者トーベ・ヤンソンが絵本などに用いていた色を使用したオリジナル商品のほか、ライフスタイル雑貨やアパレル、北欧からのインポート商品も数多く並ぶ。
次に、「MOOMIN CAFÉ」(仮称)は2023年春、渋谷にオープン予定。北欧の洗練された雰囲気を感じられ、オーガニック食材をふんだんに使用したメニューや、ドリンク、スイーツなどが、お腹も心も満たしてくれる。
という記事を見ていたので何が何やら。
printf
Generic print()/fprint()/sprint() functions inspired by high-level languages.
GitHub - mini-rose/print: Generic c print/fprint functions
はて、どんな実装なのだろうとみてみたら…😓
4294967295
この種のバグは大好物(違)なので記事を読みに行ったところ
This type of behavior is not supposed to happen for “safe” languages such as Python and PHP, as they check that all read and write operations are within the bounds of the buffer. However, the problem is that the vulnerability is present in the underlying “unsafe” C language…
とか書かれていたり、
4294967295
というある意味馴染み深い数字が出てきて、
思わず苦笑い。
51
「51日」ってどこから出てきた数字だろうと 疑問に思ったので元記事に当たってみた。
そこそこ分量のある記事なのでざっと眺めていって目についたのがこの辺。
A Reverse Engineer’s Perspective on the Boeing 787 ‘51 days’ Airworthiness Directive | IOActive
A Potential Explanation
The 6-byte EDE timestamp is the key to make sure everything goes smoothly in the CDN. The most significant bit in this timestamp is set 0 by definition, so ideally we have 0x7FFFFFFFFFFF as the maximum coherent value for the EDE timestamp.
The ES receives the data from the hosted application through PCI, running at 33MHz, so it would be reasonable to implement a counter at a similar clock frequency so the ASIC can use that clock reference to timestamp ready-to-go messages. So let’s assume the counter is ideally operating at 33MHz and the timestamp is somehow derived from that counter, also taking into account different parameters, such as delays and latencies due to moving data across the different interfaces (RMII, PCI, etc.).
By calculating the frequency at which an ideal counter (starting at 0) should be operating in order to wrap-around the EDE timestamp (0x800000000000) after 51 days, we obtain ~32MHz. That’s pretty close to our assumption.
ふむ。試しに簡単な計算をしてみると
>ruby -e "puts (0x7f_ff_ff_ff_ff_ff / (51*24*60*60*1000*1000))"
31
>ruby -e "puts (0x7f_ff_ff_ff_ff_ff / (51.0*24*60*60*1000*1000))"
31.939335592621415
なるほど。ところで元記事の最後には
For additional reading, please refer to the white paper of my original research, which was released during Black Hat 2019.
とあるのでそれではとリンク先 Arm IDA and Cross Check: Reversing the 787’s Core Network をみたら結構な分量がありますな (まかせた)。
参考
- Reverse Engineer’s Perspective on the Boeing 787 ‘51 days’ Directive | Hacker News
- [B! 航空] 新山祐介 (Yusuke Shinyama) on Twitter: “FAAが最近「ボーイング787は最長でも51日間に1度は完全にシャットダウンせよ」という勧告を出したのはなぜか? B787のソフトウェアはVxWorks 653上で動いており、各コンポーネント間の通信パケットに使われるタイムスタン… https://t.co/7z8OMdKw8H”
- [B!] A Reverse Engineer’s Perspective on the Boeing 787 ‘51 days’ Airworthiness Directive | IOActive
p
WG 14 Document log を眺めていたら
というのが目に入って さらに中身を読んでみると
I checked the following implementations to see if ’p’ is used by any of them:
- Linux (glibc): ’p’ is unused (extensions are ’c’, ’e’, ’m’ and ’x’).
- FreeBSD: ’p’ is unused (extensions are ’e’ and ’x’).
- NetBSD: ’p’ is unused (extensions are ’e’, ’f’, ’l’ and ’x’).
- OpenBSD: ’p’ is unused (extensions are ’e’ and ’x’).
- MacOS: ’p’ is unused (extensions are ’x’).
- Microsoft VS2019: ’p’ is unused (extensions are ’c’, ’n’, ’t’ and ’x’).
- QNX: ’p’ is unused (no extensions).
- HPUX: ’p’ is unused (no extensions)
というのがあり、それぞれのOSでのextensionが気になったので(ry
にしても、QNXとかHPUXまで挙げられているのが意外っちゃ意外である。
まずはLinux(glibc)。
glibc での注意
GNU C ライブラリでは、 mode に指定できる文字列として、以下の拡張が行われている:c (glibc 2.3.3 以降)
open 操作、それに続く read/write 操作の、 スレッドの取り消しポイント (cancellation points) を作成しない。 このフラグは fdopen() では無視される。 e (glibc 2.7 以降)
O_CLOEXEC フラグを有効にしてファイルをオープンする。詳細は open(2) を参照。このフラグは fdopen() では無視される。 m (glibc 2.3 以降)
I/O システムコール (read(2), write(2)) ではなく、 mmap(2) を使ってファイルにアクセスしようとする。 mmap(2) を使おうとするのは、読み出し用にオープンするファイルについてだけである。 x
ファイルを排他的にオープンする (open(2) の O_EXCL フラグと同様)。 ファイルがすでに存在する場合、 fopen() は失敗し、 errno に EEXIST がセットされる。 このフラグは fdopen() では無視される。
上記の文字に加えて、 fopen() と freopen() では mode に 以下の書式を 指定することができる。
,ccs=string
指定された string は、符号化文字集合の名前と解釈され、 ストリームではワイド文字のストリームとして扱われる。 内部変換関数で入出力時に文字集合 string との変換が行われる。 書式 ,ccs=string が指定されない場合は、 ストリームをワイド文字のストリームとして扱うかは 最初のファイル操作時に決定される。 最初のファイル操作がワイド文字操作であった場合は、 そのストリームはワイド文字のストリームとして扱われ、 符号化文字集合との変換を行う関数が読み込まれる。
x
はなんとなく記憶にあるけど他の三つは(たぶん)知らなかった。
つぎはFreeBSD。 fopen(3)
STANDARDS
The fopen() and freopen() functions conform to ISO/IEC 9899:1990 (“ISO C90”), with the exception of the “x” mode option which conforms to ISO/IEC 9899:2011 (“ISO C11”). The fdopen() function conforms to IEEE Std 1003.1-1988 (“POSIX.1”). The “e” mode option does not conform to any standard but is also supported by glibc. The fmemopen() function con- forms to IEEE Std 1003.1-2008 (“POSIX.1”). The “b” mode does not conform to any standard but is also supported by glibc.
NetBSD
fopen(3) - NetBSD Manual Pages
After one of those, the mode string can also include one or more of the following modifier letters:
`b’
The letterb' may appear either as a last character or as a char- acter between the characters in any of the two-character strings described above. This is strictly for compatibility with ANSI X3.159-1989 (``ANSI C89''), where it means open in ``binary'' mode which is identical to text mode here, so has no effect; the
b’ is ignored.
`e’
The letter `e’ in the mode string sets the close-on-exec (O_CLOEXEC) flag of the file descriptor, which means that it will not be available after an exec(3) system call. This is a non ANSI X3.159-1989 (``ANSI C89’’) extension.
`f’
The letter `f’ in the mode string restricts fopen() to regular files; if the file opened is not a regular file, fopen() will close it, and fail. This is a non ANSI X3.159-1989 (``ANSI C89’’) exten- sion.
`l’
The letter `l’ in the mode string turns the don’t-follow-symlinks (O_NOFOLLOW) flag of the file descriptor, which means that if the last path component is a symbolic link, it will not be followed. This is a non ANSI X3.159-1989 (``ANSI C89’’) extension.
`x’
The letter `x’ in the mode turns on exclusive open mode to the file (O_EXCL) which means that the file will not be created if it already exists. In that case fopen() will fail.
OpenBSD fopen(3) - OpenBSD manual pages
After any of the above prefixes, the mode string can also include zero or more of the following:
“e”
The close-on-exec flag is set on the underlying file descriptor of the new FILE. “x”
If the mode string starts with “w” or “a” then the function shall fail if the file specified by path already exists, as if the O_EXCL flag was passed to the open(2) function. It has no effect if used with fdopen() or the mode string begins with “r”.
最後にWindows(Microsoft Visual C++)。 これは抜き出すのが面倒だったのでリンクだけ。
fopen、_wfopen | Microsoft Learn
次のオプションを追加 mode して、より多くの動作を指定できます。
augumented regular expression
例のコードを眺めているとkorn shellそのものにも使われている 気配があったのでkorn shellのマニュアル(ドキュメント)に当たってみた。 すると
?(pattern-list) Matches zero or one occurrence of the given patterns. *(pattern-list) Matches zero or more occurrences of the given patterns. +(pattern-list) Matches one or more occurrences of the given patterns. @(pattern-list) Matches one of the given patterns. !(pattern-list) Matches anything except one of the given patterns.
Korn shell regular expressions
Korn shell regular expressions
In addition to the regular expression notations discussed above, the Korn Shell provides its own syntax.
The metacharacters used by this syntax are similar to those used by the generic regular expression handling notations:
Operator Meaning *(regexp) matches 0 or more instances of regexp +(regexp) matches 1 or more instances of regexp ?(regexp) matches 0 or 1 instances of regexp @(regexp1|regexp2|. . .) matches regexp1 or regexp2 or . . . !(regexp) matches any string except regexp The OR notation given for the @ metacharacter can be combined with any of the other metacharacters. For example, @(apple|pear|kumquat) matches
apple'' or ''pear'' or
kumquat’’, while !(apple|pear|kumquat) matches any string exceptapple'' or ''pear'' or
kumquat’'.
というのが見つかって、動作は(ほぼ)想定通りなんだけど 演算子(メタ文字)が「前置形式」なんだよねえ。
- ksh - マニュアルページセク ション 1: ユーザーコマンド
- Korn Shell globbing
- Globbing using Korn Shell
- Korn Shell 93 Extended Patterns « Musings
- String Operators (Learning the Korn Shell, 2nd Edition)
- shell - Regex in KornShell - Stack Overflow
- shell script - Match regex in ksh - Unix & Linux Stack Exchange
- ast/regcomp.c at master · att/ast
- ast/regnexec.c at master · att/ast
- ast/testmatch.dat at master · att/ast
- ast/regex.h at master · att/ast
git
そう言えばgitのソースコード読み全然進んでないと思いつつ。
- How I debugged and fixed git-grep macOS UTF-8 support : programming
- blog dds: 2022-10-12 — How I fixed git-grep macOS UTF-8 support
なかなか興味深い話だった。 簡単に書くと、 maxOS用のgit-grepが期待通り動いていないので調べてみたら… という話なんだけど
A little later I discovered (you guessed it, using git grep) that the call to setlocale(3) was taking place as a side-effect of the gettext(3) initialization. As gettext(3) isn’t configured under macOS, this initialization wasn’t taking place. I fixed this by moving the call to setlocale(3) into Git’s main routine in common-main.c.
To conclude, the experience of debugging and fixing git grip UTF-8 support on macOS, demonstrated to me, yet again, the importance of several of the 66 recommendations I make in the Effective Debugging book.
実際にコードを覗いてみると
修正後の
grep: fix multibyte regex handling under macOS · git/git@1819ad3
ではsetlocale(LC_CTYPE, "");
の呼び出しが
main()の中にあるのに対し
git/common-main.c
git/common-main.c at master · git/git
int main(int argc, const char **argv)
{
int result;
struct strbuf tmp = STRBUF_INIT;
trace2_initialize_clock();
/*
* Always open file descriptors 0/1/2 to avoid clobbering files
* in die(). It also avoids messing up when the pipes are dup'ed
* onto stdin/stdout/stderr in the child processes we spawn.
*/
sanitize_stdfds();
restore_sigpipe_to_default();
git_resolve_executable_dir(argv[0]);
setlocale(LC_CTYPE, "");
git_setup_gettext();
initialize_the_repository();
attr_start();
trace2_initialize();
trace2_cmd_start(argv);
trace2_collect_process_info(TRACE2_PROCESS_INFO_STARTUP);
if (!strbuf_getcwd(&tmp))
tmp_original_cwd = strbuf_detach(&tmp, NULL);
result = cmd_main(argc, argv);
/* Not exit(3), but a wrapper calling our common_exit() */
exit(result);
}
修正前の
History for common-main.c - git/git
ではmain()にsetlocale(LC_CTYPE, "");
はなく
git/common-main.c
git/common-main.c at 19d75948ef7abe9066efa4462108827d70565ae9 · git/git
int main(int argc, const char **argv)
{
int result;
struct strbuf tmp = STRBUF_INIT;
trace2_initialize_clock();
/*
* Always open file descriptors 0/1/2 to avoid clobbering files
* in die(). It also avoids messing up when the pipes are dup'ed
* onto stdin/stdout/stderr in the child processes we spawn.
*/
sanitize_stdfds();
restore_sigpipe_to_default();
git_resolve_executable_dir(argv[0]);
git_setup_gettext();
initialize_the_repository();
attr_start();
trace2_initialize();
trace2_cmd_start(argv);
trace2_collect_process_info(TRACE2_PROCESS_INFO_STARTUP);
if (!strbuf_getcwd(&tmp))
tmp_original_cwd = strbuf_detach(&tmp, NULL);
result = cmd_main(argc, argv);
/* Not exit(3), but a wrapper calling our common_exit() */
exit(result);
}
git_setup_gettext
の中でgettextのために行っていた
setlocaleの呼び出しが
(半ば意図せず)全体のlocale設定の初期化にもなっていた。
と。
git/gettext.c
git/gettext.c at master · git/git
void git_setup_gettext(void)
{
const char *podir = getenv(GIT_TEXT_DOMAIN_DIR_ENVIRONMENT);
char *p = NULL;
if (!podir)
podir = p = system_path(GIT_LOCALE_PATH);
if (!is_directory(podir)) {
free(p);
return;
}
bindtextdomain("git", podir);
setlocale(LC_MESSAGES, "");
setlocale(LC_TIME, "");
init_gettext_charset("git");
textdomain("git");
free(p);
}
そのためgettextを使わない設定でビルドすると localeの初期化も行われず、結果あのような動作になっていた。ということらしい。
関数型イカ娘(簡約!λカ娘)のWebサイト、見れなくなってる?
— mod_poppo (@mod_poppo) October 19, 2022
UTF-8が選ばれないのは、EBCDICと同じく1文字2byteのSJISにしないと、固定長レイアウトのファイル入出力処理のストレートコンバージョンが事実上できなくなっちゃうのが理由(同じ理由でEUCも選ばれない) https://t.co/ChJLkxs44O
— inose660 (@inose660) October 19, 2022