Skip to content

[Fix] Resolve cache obj double free #16

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

Merged
merged 7 commits into from
Aug 2, 2025
Merged

[Fix] Resolve cache obj double free #16

merged 7 commits into from
Aug 2, 2025

Conversation

haochengxia
Copy link
Collaborator

No description provided.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @haochengxia, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request primarily focuses on resolving a critical memory management issue within the libCacheSim Python bindings, specifically preventing potential double-free scenarios for cache objects. Beyond this crucial fix, it significantly enhances the project's build system to support new, optional cache algorithms (like GLCACHE, LRB, and 3LCache) and introduces a robust, comprehensive test suite to ensure the correctness and stability of the cache implementations.

Highlights

  • Memory Management Fix: Addressed potential double-free issues for cache objects returned from C++ to Python. This was achieved by explicitly managing object lifetimes using py::return_value_policy::reference and std::unique_ptr within the Pybind11 bindings for methods like find, insert, to_evict, and object creation functions.
  • New Cache Feature Integration: Integrated support for optional GLCACHE, LRB, and 3L_CACHE algorithms. This involved adding new CMake options, configuring the build system to find and link necessary external dependencies like XGBoost and LightGBM conditionally, and updating the Python API to expose relevant parameters.
  • Enhanced Build Process: Streamlined the project's build process by ensuring the libCacheSim submodule is always rebuilt with the correct configurations. Redundant manual build steps were removed from the install.sh script, centralizing the build logic within CMakeLists.txt.
  • Comprehensive Test Suite: Introduced a new, extensive test suite (tests/test_cache.py) utilizing pytest. This suite validates the functionality, edge cases, statistics, and operations of various cache algorithms, including both core and newly integrated optional ones, ensuring robust behavior.
  • Python API Refinements: Updated Python cache class parameters and docstrings in libcachesim/cache.py (e.g., for LeCaR, ClockPro, LRUProb) to align with underlying C++ changes and new feature capabilities, improving consistency and usability.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request addresses a critical memory management issue by implementing py::return_value_policy::reference and std::unique_ptr with custom deleters in src/export_cache.cpp. It also introduces a comprehensive test suite in tests/test_cache.py and streamlines the build process with changes in CMakeLists.txt and pyproject.toml. The changes improve the stability, maintainability, and test coverage of the project.

CMakeLists.txt Outdated
Comment on lines 116 to 120
find_package(xgboost REQUIRED)
include_directories(${XGBOOST_INCLUDE_DIR})
list(APPEND optional_dependency_libs xgboost::xgboost)
add_compile_definitions(ENABLE_GLCACHE=1)
message(STATUS "XGBOOST_INCLUDE_DIR=${XGBOOST_INCLUDE_DIR}")
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Consider adding a check to ensure that XGBOOST_INCLUDE_DIR is not already defined before setting it. This can prevent potential conflicts if the variable is defined elsewhere.

if(ENABLE_GLCACHE)
    find_package(xgboost REQUIRED)
    if(NOT DEFINED XGBOOST_INCLUDE_DIR)
        include_directories(${XGBOOST_INCLUDE_DIR})
    endif()
    list(APPEND optional_dependency_libs xgboost::xgboost)
    add_compile_definitions(ENABLE_GLCACHE=1)
    message(STATUS "XGBOOST_INCLUDE_DIR=${XGBOOST_INCLUDE_DIR}")
endif()

@@ -360,7 +362,7 @@ def __init__(
update_weight: bool = True,
lru_weight: float = 0.5,
):
cache_specific_params = f"update-weight={update_weight}, lru-weight={lru_weight}"
cache_specific_params = f"update-weight={int(update_weight)}, lru-weight={lru_weight}"
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

It's good to see the explicit conversion of update_weight to an integer. This ensures that the value passed to the underlying C++ code is of the correct type. Without this conversion, there could be unexpected behavior or errors due to type mismatches.

cache_specific_params = f"update-weight={int(update_weight)}, lru-weight={lru_weight}"

@@ -1,13 +1,7 @@
git submodule update --init --recursive
# Sync submodules
git submodule update --recursive --remote
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Consider adding a check to ensure that the git submodule update command succeeds. This can be done by checking the exit code of the command and exiting the script with an error message if it fails.

Suggested change
git submodule update --recursive --remote
git submodule update --recursive --remote
if [ $? -ne 0 ]; then
echo "Error: git submodule update failed"
exit 1
fi

Comment on lines +283 to +284
cache_obj_t* obj = self.find(&self, &req, update_cache);
return py::cast(obj, py::return_value_policy::reference);
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Using py::cast with py::return_value_policy::reference is the correct way to return a reference to a C++ object from a pybind11 function. This ensures that the Python object holds a reference to the C++ object, and that the C++ object is not deleted while the Python object is still alive.

Suggested change
cache_obj_t* obj = self.find(&self, &req, update_cache);
return py::cast(obj, py::return_value_policy::reference);
cache_obj_t* obj = self.find(&self, &req, update_cache);
return py::cast(obj, py::return_value_policy::reference);

Comment on lines +309 to 310
return self.get_occupied_byte(&self) + req.obj_size > self.cache_size;
},
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The condition self.get_occupied_byte(&self) + req.obj_size > self.cache_size is used to determine if eviction is needed. This is a good optimization to avoid unnecessary eviction attempts when there is enough space in the cache.

Suggested change
return self.get_occupied_byte(&self) + req.obj_size > self.cache_size;
},
return self.get_occupied_byte(&self) + req.obj_size > self.cache_size;

@haochengxia haochengxia merged commit 7ec91a9 into main Aug 2, 2025
32 checks passed
@haochengxia haochengxia deleted the hxia/fix branch August 2, 2025 00:27
@haochengxia haochengxia restored the hxia/fix branch August 2, 2025 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy