Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[scudo] Update error handling for secondary cache entry count #95595

Merged
merged 3 commits into from
Jun 17, 2024

Conversation

JoshuaMBa
Copy link
Contributor

@JoshuaMBa JoshuaMBa commented Jun 14, 2024

Initially, the scudo allocator would return an error if the user attempted to set the cache capacity
(i.e. the number of possible entries in the cache) above the maximum cache capacity.
Now the allocator will resort to using the maximum cache capacity in this event.
An error will still be returned if the user attempts to set the number of entries to a negative value.

…e option handler.

Initially, the scudo allocator would return an error if the user attempted to set the cache
capacity (i.e. the number of possible entries in the cache) above the maximum cache capacity.
Now the allocator will resort to using the maximum cache capacity in this event. An
error will still be returned if the user attempts to set the number of entries to a
negative value.
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Collaborator

llvmbot commented Jun 14, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Joshua Baehring (JoshuaMBa)

Changes

…e option handler.

Initially, the scudo allocator would return an error if the user attempted to set the cache capacity (i.e. the number of possible entries in the cache) above the maximum cache capacity. Now the allocator will resort to using the maximum cache capacity in this event. An error will still be returned if the user attempts to set the number of entries to a negative value.


Full diff: https://github.com/llvm/llvm-project/pull/95595.diff

2 Files Affected:

  • (modified) compiler-rt/lib/scudo/standalone/secondary.h (+4-4)
  • (modified) compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp (+3-3)
diff --git a/compiler-rt/lib/scudo/standalone/secondary.h b/compiler-rt/lib/scudo/standalone/secondary.h
index d8c9f5bcfcaf6..0e0788f830431 100644
--- a/compiler-rt/lib/scudo/standalone/secondary.h
+++ b/compiler-rt/lib/scudo/standalone/secondary.h
@@ -391,10 +391,10 @@ template <typename Config> class MapAllocatorCache {
       return true;
     }
     if (O == Option::MaxCacheEntriesCount) {
-      const u32 MaxCount = static_cast<u32>(Value);
-      if (MaxCount > Config::getEntriesArraySize())
-        return false;
-      atomic_store_relaxed(&MaxEntriesCount, MaxCount);
+      if (Value < 0) return false;
+      atomic_store_relaxed(
+          &MaxEntriesCount,
+          Min<u32>(static_cast<u32>(Value), Config::getEntriesArraySize()));
       return true;
     }
     if (O == Option::MaxCacheEntrySize) {
diff --git a/compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp b/compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp
index 8f0250e88ebf3..af69313214ea6 100644
--- a/compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp
@@ -192,9 +192,9 @@ TEST_F(MapAllocatorTest, SecondaryIterate) {
 
 TEST_F(MapAllocatorTest, SecondaryOptions) {
   // Attempt to set a maximum number of entries higher than the array size.
-  EXPECT_FALSE(
-      Allocator->setOption(scudo::Option::MaxCacheEntriesCount, 4096U));
-  // A negative number will be cast to a scudo::u32, and fail.
+  EXPECT_TRUE(Allocator->setOption(scudo::Option::MaxCacheEntriesCount, 4096U));
+
+  // Attempt to set an invalid (negative) number of entries
   EXPECT_FALSE(Allocator->setOption(scudo::Option::MaxCacheEntriesCount, -1));
   if (Allocator->canCache(0U)) {
     // Various valid combinations.

Copy link

github-actions bot commented Jun 14, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@JoshuaMBa JoshuaMBa changed the title [scudo] Update error handling for cache entry count in secondary cach… [scudo] Update error handling for seondary cache entry count Jun 14, 2024
Copy link
Contributor

@cferris1000 cferris1000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cferris1000 cferris1000 merged commit 16aa39a into llvm:main Jun 17, 2024
6 checks passed
Copy link

@JoshuaMBa Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Caslyn added a commit that referenced this pull request Jun 18, 2024
Configs that use `MapAllocatorNoCache` for its secondary cache will
return `false` if `setOption` is called with some [specific
options](https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/scudo/standalone/secondary.h#L104),
and this breaks with the `SecondaryOptions` test.

This change will gate testing all `setOption` expectations for cache
options only if the allocator cache is enabled. This will unblock
[github.com//pull/95595
](#95595) from merging into
Fuchsia.
jrguzman-ms pushed a commit to msft-mirror-aosp/platform.external.scudo that referenced this pull request Jun 18, 2024
Configs that use `MapAllocatorNoCache` for its secondary cache will
return `false` if `setOption` is called with some [specific
options](https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/scudo/standalone/secondary.h#L104),
and this breaks with the `SecondaryOptions` test.

This change will gate testing all `setOption` expectations for cache
options only if the allocator cache is enabled. This will unblock
[github.com/llvm/llvm-project/pull/95595
](llvm/llvm-project#95595) from merging into
Fuchsia.

GitOrigin-RevId: 6f13f0b3fe57d2f59b8d456446fe87f3fef686d0
Change-Id: I3751320a700d1b4ac629cd7901f5bc51f3f47f2f
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
Configs that use `MapAllocatorNoCache` for its secondary cache will
return `false` if `setOption` is called with some [specific
options](https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/scudo/standalone/secondary.h#L104),
and this breaks with the `SecondaryOptions` test.

This change will gate testing all `setOption` expectations for cache
options only if the allocator cache is enabled. This will unblock
[github.com/llvm/pull/95595
](llvm#95595) from merging into
Fuchsia.
@JoshuaMBa JoshuaMBa changed the title [scudo] Update error handling for seondary cache entry count [scudo] Update error handling for secondary cache entry count Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants