-
Notifications
You must be signed in to change notification settings - Fork 855
jax_fingerprint: Address user arg slot exhaustion #13082
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
Open
bneradt
wants to merge
1
commit into
apache:master
Choose a base branch
from
bneradt:jax-user-arg-exhaustion-fix-asf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /** @file | ||
| * | ||
| * Shared context map for jax_fingerprint plugin instances. | ||
| * | ||
| * When multiple jax_fingerprint.so instances are loaded (e.g., one per | ||
| * fingerprinting method), they share a single user arg slot. This map | ||
| * stores the JAxContext for each method, keyed by method name. | ||
| * | ||
| * @section license License | ||
| * | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "config.h" | ||
| #include "context.h" | ||
|
|
||
| #include <string> | ||
| #include <string_view> | ||
| #include <unordered_map> | ||
| #include <version> | ||
|
|
||
| /** | ||
| * @brief Container holding JAxContext instances for multiple methods. | ||
| * | ||
| * ATS has a limited number of user arg slots (~4 per type). When loading | ||
| * many jax_fingerprint instances, we share a single slot and store all | ||
| * contexts in this map, keyed by method name. | ||
| */ | ||
| class ContextMap | ||
| { | ||
| public: | ||
| ~ContextMap() | ||
| { | ||
| for (auto &pair : m_contexts) { | ||
| delete pair.second; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @brief Store a context for a method. | ||
| * @param[in] method_name The method name (e.g., "JA3", "JA4"). | ||
| * @param[in] ctx The context to store. Ownership is transferred. | ||
| */ | ||
| void | ||
| set(std::string_view method_name, JAxContext *ctx) | ||
| { | ||
| auto it = find_context(method_name); | ||
| if (it != m_contexts.end()) { | ||
| delete it->second; | ||
| it->second = ctx; | ||
| } else { | ||
| m_contexts.emplace(std::string{method_name}, ctx); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @brief Retrieve a context for a method. | ||
| * @param[in] method_name The method name. | ||
| * @return The context, or nullptr if not found. | ||
| */ | ||
| JAxContext * | ||
| get(std::string_view method_name) | ||
| { | ||
| auto it = find_context(method_name); | ||
| return it != m_contexts.end() ? it->second : nullptr; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Remove a context for a method. | ||
| * @param[in] method_name The method name. | ||
| */ | ||
| void | ||
| remove(std::string_view method_name) | ||
| { | ||
| auto it = find_context(method_name); | ||
| if (it != m_contexts.end()) { | ||
| delete it->second; | ||
| m_contexts.erase(it); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @brief Check if the map is empty. | ||
| * @return True if no contexts are stored. | ||
| */ | ||
| bool | ||
| empty() const | ||
| { | ||
| return m_contexts.empty(); | ||
| } | ||
|
|
||
| private: | ||
| using ContextStorage = std::unordered_map<std::string, JAxContext *, StringHash, std::equal_to<>>; | ||
|
|
||
| /** Find context by method name with C++20 generic lookup fallback. | ||
| * | ||
| * C++20 generic unordered lookup allows finding with std::string_view in a | ||
| * std::unordered_map<std::string, ...> without creating a temporary string. | ||
| * For standard libraries without this feature, we fall back to constructing | ||
| * a std::string for the lookup. | ||
| * | ||
| * @param[in] method_name The method name to look up. | ||
| * @return Iterator to the found element, or end() if not found. | ||
| */ | ||
| ContextStorage::iterator | ||
| find_context(std::string_view method_name) | ||
| { | ||
| #ifdef __cpp_lib_generic_unordered_lookup | ||
| return m_contexts.find(method_name); | ||
| #else | ||
| return m_contexts.find(std::string{method_name}); | ||
| #endif | ||
bneradt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** const_iterator @overload */ | ||
| ContextStorage::const_iterator | ||
| find_context(std::string_view method_name) const | ||
| { | ||
| #ifdef __cpp_lib_generic_unordered_lookup | ||
| return m_contexts.find(method_name); | ||
| #else | ||
| return m_contexts.find(std::string{method_name}); | ||
| #endif | ||
| } | ||
|
|
||
| ContextStorage m_contexts; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.