ああ、QueryPerformanceCounterが失敗しないと記載されたドキュメントへの反例を見つけた
本文の状態
日本語全文を表示中
詳細モードで約1分の本文を読めます。
同じ出来事の情報源
この情報源を基点に整理
Andrej Karpathy 厳選
Raymond Chen氏が、WindowsのQueryPerformanceCounter関数が失敗しないという公式ドキュメントの記述に反する事例を発見したことを報告した。
Source Article
元記事を日本語で読む
本文に関係しない購読案内、埋め込み通知、サイト内プロモーションは除いています。
もちろん、ルールを破れば何でも起こり得ます。
この投稿「QueryPerformanceCounterが『決して失敗しない』というドキュメントへの反例を見つけた」は、The Old New Thingに最初に掲載されました。
原文を表示
In the documentation that describes the high-resolution timestamps, there is a question-and-answer section.
Under what circumstances does QueryPerformanceFrequency return FALSE, or QueryPerformanceCounter return zero?
This won’t occur on any system that runs Windows XP or later.
People on Stack Overflow have noted, “Nuh-uh! I can get it to fail even on Windows XP and later!”
The function can fail with error code ERROR_NOACCESS (Invalid access to memory location) if the variable is not double-word (8-byte) aligned.
So who’s right?
The documentation assumes that you are passing valid parameters. Specifically, the pointer parameter is a valid pointer to a writable LARGE_INTEGER structure. And that means that it’s not a null pointer, it’s not a pointer to unallocated memory, it’s not a pointer to read-only memory, it’s not a pointer to memory that is freed while the function is executing, you don’t write to the memory while QueryPerformanceCounter is running, and it’s a pointer to properly aligned LARGE_INTEGER structure. (There are probably other ways the parameter can be invalid; those are just the ones I could think of off the top of my head.)
The LARGE_INTEGER structure as LONGLONG alignment, which on Windows means 8-byte alignment, because the default structure packing is /Zp8. Therefore, your output LARGE_INTEGER was not valid. (Indeed, in the example given, it’s not even a LARGE_INTEGER!)
If you pass invalid parameters, then you have broken the basic ground rules for programming, and the results will be unpredictable. The function might return failure, it might return success but produce garbage results, it might crash your process, it might merely corrupt your process in a way that causes it to crash 10 minutes later. Everything is on the table (as long as it doesn’t cross a security boundary).
The way they got it to fail was by passing invalid parameters. Clearly the function can’t succeed if you don’t give a valid place to put the answer.
But just to forestall the “Nuh uh”-ers, I made an update to the documentation for QueryPerformanceCounter:
On systems that run Windows XP or later, the function will always succeed when given valid parameters and will thus never return zero.
CategoryTopics
Author
Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.
今日のまとめ
AIデイリーブリーフで今日の重要ニュースをまとめ読み