当前位置:网站首页>RI Geng series: potential memory leak caused by abuse of mutable in Google Pb structure
RI Geng series: potential memory leak caused by abuse of mutable in Google Pb structure
2022-06-23 02:45:00 【mariolu】
The algorithm communication protocol sets a big pb, Then go to pb It's stuffed with features . These characteristics will be different redis Table query get .redis Query different tables , Parse the return data , Because the callback function is made concurrent and asynchronous , Therefore, not all feature requests are serially serialized , So there will be a concurrent contention problem .
For custom complex objects, it is not pld type ,protobuf (c++) There are two ways to set the infill class The value of the object , Namely set_allocated_ and mutable_. This mutable_ If abused in a non thread safe environment , There will be a potential memory leak , This problem is more hidden .
Suppose we have an example :
message browser_user_feature
{
feature_user_download user_download = 1;
feature_query_word query_word = 2;
}
message UserFeature {
browser_user_feature user_feature = 1;
}Then the user downloads the data and query The data is passed through different redis Table access . Then we register a callback function to parse , This is pseudo code
register_parse_func("key1", Analytic user_download Callback function for )
register_parse_func("key2", Analytic query_word Callback function for )
Analytic user_download Callback function for (data, size) {
result.mutable_user_feature()->mutable_user_download()->ParseFromString(data, size);
}
Analytic query_word Callback function for (data, size) {
result.mutable_user_feature()->mutable_query_word()->ParseFromString(data, size);
}that mutable This aspect internally determines whether a null pointer exists , If it's a null pointer , Do object memory allocation .
inline ::feature_process::browser_user_feature* UserFeature::mutable_browser_search_user_feature() {
if (browser_user_feature_ == nullptr) {
auto* p = CreateMaybeMessage<::feature_process::browser_user_feature>(GetArenaNoVirtual());
browser_user_feature_ = p;
}
// @@protoc_insertion_point(field_mutable:feature_process.UserFeature.browser_user_feature)
return browser_user_feature_;
}Use memory analysis tools :gperftools https://github.com/gperftools/gperftools, You will see a lot of memory allocated in this function CreateMaybeMessage. Note here mutable_xxx Not a thread safe function , So when it comes to distribution , There may be two or more assignments , Then even if the memory is released later, it will only be released once .
Then modify this bug What is the method of ? We need to make sure that we call it first in a place where there is no thread safety problem mutable Method , Pre allocate memory , No new memory will be created for subsequent use .
边栏推荐
- Circuit analysis (circuit principle)
- HTTP cache
- Evolution history of mobile communication
- WebService details
- method
- 5g spectrum
- Add other view components to the audio and video components of the applet
- EDI project cases of customers in medical device industry
- 862. triple sorting
- How to batch generate matrix 25 codes
猜你喜欢

2021-11-11

Reptile lesson 1

Application and challenge of ten billion level map data in Kwai security intelligence

Xgboost Guide

Quick sorting C language code + auxiliary diagram + Notes

Digital circuit logic design
![Buuctf misc-[actf freshman competition 2020]outline](/img/a4/ac9d14a69e0759d1e7c65740415bf7.jpg)
Buuctf misc-[actf freshman competition 2020]outline

5g core network and core network evolution

Analog Electronic Technology

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030
随机推荐
Xgboost Guide
Information theory and coding
Optimization method of live weak network
Hypervisor Necromancy; Recover kernel protector (1)
HTTP cache
How to batch make decreasing serial number barcode
Unity official case nightmare shooter development summary < I > realization of the role's attack function
Reinforcement learning series (III) -gym introduction and examples
Learning notes of recommendation system (1) - Collaborative Filtering - Theory
Circuit analysis (circuit principle)
Reinforcement learning series (IV) -policygradient example
Golang string comparison
February 6, 2022: Arithmetic Sequence Division II - subsequence. Give you an integer array n
Windows system poisoning, SQL Server database file recovery rescue and OA program file recovery
[data preparation and Feature Engineering] perceived data
Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027
6. template for integer and real number dichotomy
Web components series (I) - Overview
Problem thinking and analysis process
New uniapp+uniui background management uniuadmin