fileinfo: built-in magic database php_magic_database is not aligned, breaks on strict-alignment CPUs (ARMv5, 32-bit)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
- #25030 @lazerg による — マージされずにクローズ
評価
調査の方向性
ext/fileinfo/create_data_file.phpと、php_magic_databaseの宣言が生成されるext/fileinfo/data_file.cから始めます。他の宣言でZEND_SET_ALIGNEDがどのように使われているかを確認し、その後データファイルを再生成してアライメントを検証します。組み込みのmagicデータベースがアライメントされ、厳密なアライメントを要求するARMビルドでfinfo_open()とmime_content_type()が動作すれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Description
The following code:
<?php
var_dump(mime_content_type('/tmp/file.zip'));
Resulted in this output:
ValueError: Path must not be empty.
But I expected this output instead:
application/zip
Description:
On ARMv5 (32-bit cpu), built with GCC and -Os, finfo_open() and mime_content_type() fail in PHP 8.5.x with:
ValueError: Path must not be empty.
Cause:
php_magic_database (ext/fileinfo/data_file.c) is declared as const unsigned char[][], so the compiler only guarantees 1-byte alignment.
libmagic apprentice_map() casts the buffer to uint32_t * to check MAGICNO/VERSIONNO, then uses it as an array of struct magic (8-byte aligned).
On CPUs that don't support unaligned access, the header reads back wrong and the built-in database is rejected.
libmagic then falls back to apprentice_load(NULL), which calls the stream layer with an empty path and throws the ValueError.
Whether the bug appears depends on where the linker places the symbol (nm shows an address that isn't a multiple of 4 or 8). GCC on ARM normally word-aligns large arrays, but not with -Os.
Fix: declare the array with ZEND_SET_ALIGNED(8, ...) in create_data_file.php and regenerate data_file.c
Since ext/fileinfo/create_data_file.php is generated I will not provide a patch for it, but this is the AI generated proposal:
-echo 'const unsigned char php_magic_database[' . count($chunks) . '][' . CHUNK_SIZE . "] ZEND_NONSTRING = {\n";
+/* libmagic accesses the database as uint32_t and struct magic, which need 8-byte alignment */
+echo 'ZEND_SET_ALIGNED(8, const unsigned char php_magic_database[' . count($chunks) . '][' . CHUNK_SIZE . "]) ZEND_NONSTRING = {\n";
My own patch to solve my problem: directly in data_file.c
--- php-8.5.12/ext/fileinfo/data_file.c 2026-10-28 16:30:57.011450103 +0100
+++ php-8.5.12.modified/ext/fileinfo/data_file.c 2026-10-28 16:22:31.851759910 +0100
@@ -3 +3 @@
-const unsigned char php_magic_database[10113][1024] ZEND_NONSTRING = {
+const unsigned char php_magic_database[10113][1024] ZEND_NONSTRING attribute((aligned(8))) = {
PHP Version
PHP 8.5.12 (cli) (built: Oct 28 2026 16:52:49) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.5.12, Copyright (c) Zend Technologies
with Zend OPcache v8.5.12, Copyright (c), by Zend Technologies
Operating System
No response
- 主要言語
- C
- スター
- 41.7k
- フォーク
- 8.5k
- 平均マージ
- 2日 7時間
- マージ済み PR(30日)
- 154
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
php/php-src のほかの issue
-
Windows CLI: get_ps_title() passes bytes to GetConsoleTitleW() instead of characters, overflowing the 260-wchar_t buffer対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
メンテナーはふだん 1 日以内に返信
-
master: part of the mbstring bailout fix got lost in a merge対応中かも @lazerg が 9 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
Bug Status: Needs Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
メンテナーはふだん 1 日以内に返信
-
quoted_printable_encode() produces lines longer than 76 characters for bytes 0xF5 to 0xFF対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
Bug Status: Needs Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
php/php-src#24196 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
severity: low
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
luainkernel/lunatik#2221 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
zephyrproject-rtos/zephyr#124160 ·
メンテナーはふだん 3 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
davatorium/rofi#2347 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信
-
area:docs area:stdlib found-by:duet-audit kind:docs-drift
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
InauguralSystems/EigenScript#2251 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信