[Bug] dlopen() fails to find already-loaded module — name mismatch between dlmodule_find() and _dlmodule_set_name()
メンテナーはふだん 2 日以内に返信
@BernardXiong がすでに取り組んでいます。
2026年3月19日 から。
評価
この issue はまだ評価されていません。
説明
RT-Thread Version
master (latest)
Hardware Type/Architectures
N/A — platform-independent logic bug in components/libc/posix/libdl/
Develop Toolchain
Microsoft VScode
Describe the bug
Bug Description
Summary
When dlopen() is called with a full file path (e.g. "/mnt/sdcard/apps/clock.so"), it passes the full path to dlmodule_find() for lookup. However, during module loading, _dlmodule_set_name() strips the path and extension, storing only the bare filename (e.g. "clock") into module->parent.name. Since dlmodule_find() uses rt_object_find() which matches against object->name, the lookup always fails for previously loaded modules.
This causes:
- Module reloaded on every
dlopen()call — thenref++branch is never reached - Memory leak — duplicate module instances accumulate in RAM
dlclose()cannot properly clean up — eachdlopen()returns a different handle pointing to a different copy
Root Cause Analysis
Step 1: dlopen() passes full path to dlmodule_find()
File: dlopen.c, line 38-44
void *dlopen(const char *filename, int flags)
{
// ...
fullpath = (char *)filename; /* e.g. "/mnt/sdcard/apps/clock.so" */
rt_enter_critical();
module = dlmodule_find(fullpath); // <-- passes full path as-is
if (module != RT_NULL)
{
rt_exit_critical();
module->nref++; // <-- NEVER reached due to this bug
}
else
{
rt_exit_critical();
module = dlmodule_load(fullpath); // <-- always falls through here
}
// ...
}
Step 2: dlmodule_find() does direct name matching with no path processing
File: dlmodule.c, line 1130-1142
struct rt_dlmodule *dlmodule_find(const char *name)
{
rt_object_t object;
struct rt_dlmodule *ret = RT_NULL;
object = rt_object_find(name, RT_Object_Class_Module);
// ↑ tries to match name == "/mnt/sdcard/apps/clock.so"
// but no module is registered under that name
if (object) {
ret = (struct rt_dlmodule *) object;
}
return ret;
}
Step 3: _dlmodule_set_name() strips path and extension before storing
File: dlmodule.c, line 74-99
static void _dlmodule_set_name(struct rt_dlmodule *module, const char *path)
{
// ...
while (*ptr != '\0')
{
if (*ptr == '/')
first = ptr + 1; // skip to after last '/'
if (*ptr == '.')
end = ptr - 1; // stop before last '.'
ptr ++;
}
// For path "/mnt/sdcard/apps/clock.so":
// → stores "clock" into object->name
}
The Mismatch
| Operation | Key used | Value |
|---|---|---|
dlmodule_find() query |
full path | "/mnt/sdcard/apps/clock.so" |
object->name stored by _dlmodule_set_name() |
stripped name | "clock" |
These will never match.
Steps to Reproduce
#include <dlfcn.h>
/* First dlopen — module loaded, name stored as "clock" */
void *h1 = dlopen("/mnt/sdcard/apps/clock.so", 0);
/* Second dlopen — should find existing module and nref++,
but actually loads a second copy because find fails */
void *h2 = dlopen("/mnt/sdcard/apps/clock.so", 0);
/* h1 != h2 — two independent copies of clock.so now in memory */
Expected Behavior
The second dlopen() call should find the already-loaded module, increment nref, and return the same module handle.
Actual Behavior
Every dlopen() call with a full path loads a new copy of the module. The nref++ reuse branch in dlopen() is effectively dead code — nref is only ever incremented to 1 inside dlmodule_load() itself (line 753), but never reaches 2+ through the intended dlopen() reuse path.
Suggested Fix
Apply the same path-stripping logic before calling dlmodule_find() in dlopen(). Minimal patch:
/* dlopen.c — extract module name from path before find */
void *dlopen(const char *filename, int flags)
{
struct rt_dlmodule *module;
char module_name[RT_NAME_MAX];
RT_ASSERT(filename != RT_NULL);
/* Strip path and extension — same logic as _dlmodule_set_name() */
{
const char *first, *end, *ptr;
int size;
ptr = first = filename;
end = filename + rt_strlen(filename);
while (*ptr != '\0')
{
if (*ptr == '/')
first = ptr + 1;
if (*ptr == '.')
end = ptr - 1;
ptr++;
}
size = end - first + 1;
if (size > RT_NAME_MAX) size = RT_NAME_MAX;
rt_memset(module_name, 0x00, sizeof(module_name));
rt_strncpy(module_name, first, size);
module_name[RT_NAME_MAX - 1] = '\0';
}
rt_enter_critical();
module = dlmodule_find(module_name); /* now matches object->name */
if (module != RT_NULL)
{
rt_exit_critical();
module->nref++;
}
else
{
rt_exit_critical();
module = dlmodule_load(filename); /* still pass full path for file I/O */
}
return (void *)module;
}
Alternatively, the stripping logic could be moved into dlmodule_find() itself, but that may affect other callers (e.g. dlrun() which already passes a bare module name).
Additional Notes
-
Dead code on
dlopen.cline 28: The conditionif (0) //filename[0] != '/'permanently disables the relative-path handling branch. This appears to be a debugging leftover. -
Secondary bug in
_dlmodule_set_name(): The function finds the last.in the entire path string, not just in the filename portion. For paths like/mnt/v1.2/app.so,endwould point into the directory component (v1instead ofapp), producing an incorrect module name.
Other additional context
No response
- 主要言語
- C
- スター
- 12.4k
- フォーク
- 5.5k
- 平均マージ
- 3日 7時間
- マージ済み PR(30日)
- 79
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
RT-Thread/rt-thread のほかの issue
-
Arch: ARM/AArch64 Doc
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
RT-Thread/rt-thread#12082 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
RT-Thread/rt-thread#11951 · コメント 2 件 ·
メンテナーはふだん 2 日以内に返信
-
[Bug] [libc] timer_gettime 拆秒前将总纳秒截断为 uint32_t,剩余时间查询错误再び着手できるかも @Yunlk が 28 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンin progress
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
RT-Thread/rt-thread#11924 · コメント 7 件 · 担当者 1 名 ·
メンテナーはふだん 2 日以内に返信
-
[Bug] dfs_v2 tmpfs: ftruncate(fd, 0) frees the file buffer but reports success — use-after-free read/write and double free from a legal POSIX call対応中かも @remukisama が 33 日前に担当しました。 オープンin progress
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
RT-Thread/rt-thread#11902 · コメント 4 件 · 担当者 1 名 ·
メンテナーはふだん 2 日以内に返信
-
[bsp][stm32][bluepill] README「快速上手」缺少重新生成 MDK 工程这一步,按文档操作无法编译通过再び着手できるかも @moment-NEW が 50 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンin progress
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
RT-Thread/rt-thread#11818 · コメント 4 件 · 担当者 1 名 ·
メンテナーはふだん 2 日以内に返信
RT-Thread/rt-thread の issue をすべて見る
似ている issue
-
Builder: a structure opened without calculation data shows the Diffraction (fibre) view by defaultオープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
FriendsofECCE/ECCE#1245 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
apache/nuttx-apps#4108 · コメント 4 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 84/100
VirtualBox/virtualbox#996 ·
メンテナーはふだん 6 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
trezor/trezor-firmware#8357 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信