Skip to content

Commit 790df3c

Browse files
CaslynCQ Bot
authored andcommitted
[dl] Implement RTLD_NOLOAD
Implement RTLD_NOLOAD for libdl and add a basic test. Bug: 392191799 Fixed: 348727901 Change-Id: Icc9f48f14c4bbffbdc21969e451c5d30161b0caf Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1193402 Commit-Queue: Caslyn Tonelli <[email protected]> Fuchsia-Auto-Submit: Caslyn Tonelli <[email protected]> Reviewed-by: Roland McGrath <[email protected]>
1 parent fce922e commit 790df3c

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

sdk/lib/dl/runtime-dynamic-linker.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ class RuntimeDynamicLinker {
105105
return diag.ok(found);
106106
}
107107

108+
if (mode & OpenFlags::kNoload) {
109+
return diag.ok(nullptr);
110+
}
111+
108112
// A Module for `file` does not yet exist; create a new LinkingSession
109113
// to perform the loading and linking of the file and all its dependencies.
110114
LinkingSession<Loader> linking_session{modules_, max_static_tls_modid_};

sdk/lib/dl/test/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ test("dl-unittests") {
4040
"dl-load-tests-fail.cc",
4141
"dl-load-tests-global.cc",
4242
"dl-load-tests-initfini.cc",
43+
"dl-load-tests-open-flags.cc",
4344
"dl-load-tests-tls.cc",
4445
"dl-load-tests.h",
4546
"dl-system-tests.cc",

sdk/lib/dl/test/dl-impl-tests.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class DlImplTests : public Base {
2828
// since the error message returned from the libdl implementation will be the
2929
// same regardless of the OS.
3030
static constexpr bool kCanMatchExactError = true;
31-
// TODO(https://fxbug.dev/348727901): Implement RTLD_NOLOAD
32-
static constexpr bool kSupportsNoLoadMode = false;
3331
// TODO(https://fxbug.dev/342480690): Support Dynamic TLS
3432
static constexpr bool kSupportsDynamicTls = false;
3533
// TODO(https://fxbug.dev/382529434): Have dlclose() run finalizers
@@ -50,6 +48,11 @@ class DlImplTests : public Base {
5048
auto result = dynamic_linker_->Open<typename Base::Loader>(
5149
file, mode, std::bind_front(&Base::RetrieveFile, this));
5250
if (result.is_ok()) {
51+
// If RTLD_NOLOAD was passed and we have a NULL return value, there is no
52+
// module to track.
53+
if ((mode & RTLD_NOLOAD) && !result.value()) {
54+
return result;
55+
}
5356
// TODO(https://fxbug.dev/382527519): RuntimeDynamicLinker should have a
5457
// `RunInitializers` method that will run this with proper synchronization.
5558
static_cast<RuntimeModule*>(result.value())->InitializeModuleTree();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2025 The Fuchsia Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "dl-load-tests.h"
6+
7+
namespace {
8+
9+
using dl::testing::DlTests;
10+
using dl::testing::TestModule;
11+
12+
TYPED_TEST_SUITE(DlTests, dl::testing::TestTypes);
13+
14+
// If RTLD_NOLOAD is passed and the module is not found, NULL is returned but an
15+
// error is not expected.
16+
TYPED_TEST(DlTests, RtldNoLoadBasic) {
17+
const std::string kRet17File = TestModule("ret17");
18+
19+
auto open = this->DlOpen(kRet17File.c_str(), RTLD_NOW | RTLD_LOCAL | RTLD_NOLOAD);
20+
ASSERT_TRUE(open.is_ok()) << open.error_value();
21+
EXPECT_EQ(open.value(), nullptr);
22+
23+
// TODO(https://fxbug.dev/325494556): check that dlerror() returns NULL.
24+
}
25+
26+
// TODO(https://fxbug.dev/348727901): test how RTLD_NOLOAD can be used in
27+
// combination with other flags, for example with RTLD_GLOBAL to promote a
28+
// module.
29+
30+
} // namespace

sdk/lib/dl/test/dl-tests-base.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ class DlTestsBase : public ::testing::Test {
3434
// Whether the dlopen implementation validates the mode argument.
3535
static constexpr bool kCanValidateMode = true;
3636

37-
// Whether the test fixture's implementation supports `RTLD_NOLOAD`
38-
static constexpr bool kSupportsNoLoadMode = true;
39-
4037
// Whether the test fixture will always prioritize a loaded module in symbol
4138
// resolution, regardless of whether it is a global module.
4239
static constexpr bool kStrictLoadOrderPriority = false;

sdk/lib/ld/test/modules/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ per_test_suffices = [
7575
"PrecedenceInDepResolution",
7676
"StartupModulesBasic",
7777
"StartupModulesPriorityOverGlobal",
78+
"RtldNoLoadBasic",
7879
]
7980

8081
# This reaches all the runtime deps of //sdk/lib/dl/test:dl-unittests.

0 commit comments

Comments
 (0)