Skip to content

Commit e27f725

Browse files
committed
Avoid following weak_ptrs
Previously, we treated weak_ptrs as normal types and we recursed within them, following the internal data pointer and possibly causing crashes. We really shouldn't be following them, so I added a custom type to simply abort processing. If we want to handle them (ie: check if they are valid, and follow them if so), that should be fairly easy with the work there is here so far.
1 parent fb58cce commit e27f725

File tree

7 files changed

+126
-3
lines changed

7 files changed

+126
-3
lines changed

dev.oid.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ containers = [
4545
"PWD/types/caffe2_blob_type.toml",
4646
"PWD/types/std_variant.toml",
4747
"PWD/types/thrift_isset_type.toml",
48+
"PWD/types/weak_ptr_type.toml",
4849
]

src/ContainerInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ namespace fs = std::filesystem;
6262
X(ENUM_MAP_TYPE) \
6363
X(BOOST_BIMAP_TYPE) \
6464
X(STD_VARIANT_TYPE) \
65-
X(THRIFT_ISSET_TYPE)
65+
X(THRIFT_ISSET_TYPE) \
66+
X(WEAK_PTR_TYPE)
6667

6768
enum ContainerTypeEnum {
6869
#define X(name) name,

src/OICodeGen.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,10 +761,11 @@ bool OICodeGen::enumerateTemplateParamIdxs(drgn_type* type,
761761
}
762762
}
763763

764-
// Do not add `shared_ptr<void>` or `unique_ptr<void>`
764+
// Do not add `shared_ptr<void>`, `unique_ptr<void>`, or `weak_ptr<void>`
765765
// to `containerTypeMapDrgn`.
766766
if (containerInfo.ctype == SHRD_PTR_TYPE ||
767-
containerInfo.ctype == UNIQ_PTR_TYPE) {
767+
containerInfo.ctype == UNIQ_PTR_TYPE ||
768+
containerInfo.ctype == WEAK_PTR_TYPE) {
768769
drgn_qualified_type t{};
769770
// We checked that this succeeded in the previous loop
770771
drgn_template_parameter_type(&tParams[0], &t);

src/TreeBuilder.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,9 @@ void TreeBuilder::processContainer(const Variable& variable, Node& node) {
618618
return;
619619
}
620620
break;
621+
case WEAK_PTR_TYPE:
622+
// Do not handle weak pointers beyond their static size for now.
623+
break;
621624
case SHRD_PTR_TYPE:
622625
case UNIQ_PTR_TYPE:
623626
node.pointer = next();

test/ci.oid.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ containers = [
4141
"../types/caffe2_blob_type.toml",
4242
"../types/std_variant.toml",
4343
"../types/thrift_isset_type.toml",
44+
"../types/weak_ptr_type.toml",
4445
]
4546

4647
[headers]

test/integration/std_smart_ptr.toml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,97 @@ definitions = '''
179179
}
180180
]
181181
'''
182+
[cases.weak_ptr_int64_empty]
183+
param_types = ["std::weak_ptr<std::uint64_t>&"]
184+
setup = "return std::weak_ptr<std::uint64_t>();"
185+
expect_json = '''
186+
[
187+
{
188+
"staticSize": 16,
189+
"exclusiveSize": 16,
190+
"dynamicSize": 0,
191+
"NOT": "members"
192+
}
193+
]
194+
'''
195+
[cases.weak_ptr_int64_void_empty]
196+
param_types = ["std::weak_ptr<void>&"]
197+
setup = "return std::weak_ptr<void>();"
198+
expect_json = '''
199+
[
200+
{
201+
"staticSize": 16,
202+
"exclusiveSize": 16,
203+
"dynamicSize": 0,
204+
"NOT": "members"
205+
}
206+
]
207+
'''
208+
[cases.weak_ptr_int64_present]
209+
param_types = ["std::weak_ptr<std::uint64_t>&"]
210+
setup = '''
211+
static std::shared_ptr<std::uint64_t> shd = std::make_shared<std::uint64_t>(0xDEADBEEF);
212+
std::weak_ptr<std::uint64_t> weak = shd;
213+
return weak;
214+
'''
215+
expect_json = '''
216+
[
217+
{
218+
"staticSize": 16,
219+
"exclusiveSize": 16,
220+
"dynamicSize": 0,
221+
"NOT": "members"
222+
}
223+
]
224+
'''
225+
[cases.weak_ptr_int64_expired]
226+
param_types = ["std::weak_ptr<std::uint64_t>&"]
227+
setup = '''
228+
std::shared_ptr<std::uint64_t> shd = std::make_shared<std::uint64_t>(0xDEADBEEF);
229+
std::weak_ptr<std::uint64_t> weak = shd;
230+
return weak;
231+
'''
232+
expect_json = '''
233+
[
234+
{
235+
"staticSize": 16,
236+
"exclusiveSize": 16,
237+
"dynamicSize": 0,
238+
"NOT": "members"
239+
}
240+
]
241+
'''
242+
[cases.weak_ptr_int64_present_chase]
243+
param_types = ["std::weak_ptr<std::uint64_t>&"]
244+
cli_options = ["--chase-raw-pointers"]
245+
setup = '''
246+
static std::shared_ptr<std::uint64_t> shd = std::make_shared<std::uint64_t>(0xDEADBEEF);
247+
std::weak_ptr<std::uint64_t> weak = shd;
248+
return weak;
249+
'''
250+
expect_json = '''
251+
[
252+
{
253+
"staticSize": 16,
254+
"exclusiveSize": 16,
255+
"dynamicSize": 0,
256+
"NOT": "members"
257+
}
258+
]
259+
'''
260+
[cases.weak_ptr_int64_expired_chase]
261+
param_types = ["std::weak_ptr<std::uint64_t>&"]
262+
cli_options = ["--chase-raw-pointers"]
263+
setup = '''
264+
return std::make_shared<std::uint64_t>(0xDEADBEEF);
265+
'''
266+
expect_json = '''
267+
[
268+
{
269+
"staticSize": 16,
270+
"exclusiveSize": 16,
271+
"dynamicSize": 0,
272+
"NOT": "members"
273+
}
274+
]
275+
'''

types/weak_ptr_type.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[info]
2+
typeName = "std::weak_ptr"
3+
numTemplateParams = 1
4+
ctype = "WEAK_PTR_TYPE"
5+
header = "memory"
6+
ns = ["namespace std"]
7+
replaceTemplateParamIndex = []
8+
9+
[codegen]
10+
decl = """
11+
template<typename T>
12+
void getSizeType(const %1%<T> &s_ptr, size_t& returnArg);
13+
"""
14+
15+
# Weak pointers do not have ownership, so let's not follow them (for now)
16+
func = """
17+
template<typename T>
18+
void getSizeType(const %1%<T> &s_ptr, size_t& returnArg)
19+
{
20+
SAVE_SIZE(sizeof(%1%<T>));
21+
}
22+
"""

0 commit comments

Comments
 (0)