Skip to content

Commit 1a940bf

Browse files
authored
[mlir][py] Fix nanobind uninitialized values (#148944)
After #143866, we no longer always write to `value`, causing it to be uninitialized. This can lead to mysterious crashes, e.g. in `python_test.py` / `testCustomAttribute` when we attempt to evaluate `TestAttr(42)`, it does not set `value`, but `mlirAttributeIsNull(value)` happens to return false for garbage memory, and we end up trying to interpret it as a function instead of skipping it. Fix this by only reading `value` if it has been assigned. If it hasn't, `return false` seems the right choice for all these methods, i.e. indicate that `from_python` failed.
1 parent 8d61073 commit 1a940bf

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

mlir/include/mlir/Bindings/Python/NanobindAdaptors.h

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ template <>
6565
struct type_caster<MlirAffineMap> {
6666
NB_TYPE_CASTER(MlirAffineMap, const_name("MlirAffineMap"))
6767
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
68-
if (auto capsule = mlirApiObjectToCapsule(src))
68+
if (auto capsule = mlirApiObjectToCapsule(src)) {
6969
value = mlirPythonCapsuleToAffineMap(capsule->ptr());
70-
return !mlirAffineMapIsNull(value);
70+
return !mlirAffineMapIsNull(value);
71+
}
72+
return false;
7173
}
7274
static handle from_cpp(MlirAffineMap v, rv_policy,
7375
cleanup_list *cleanup) noexcept {
@@ -85,9 +87,11 @@ template <>
8587
struct type_caster<MlirAttribute> {
8688
NB_TYPE_CASTER(MlirAttribute, const_name("MlirAttribute"))
8789
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
88-
if (auto capsule = mlirApiObjectToCapsule(src))
90+
if (auto capsule = mlirApiObjectToCapsule(src)) {
8991
value = mlirPythonCapsuleToAttribute(capsule->ptr());
90-
return !mlirAttributeIsNull(value);
92+
return !mlirAttributeIsNull(value);
93+
}
94+
return false;
9195
}
9296
static handle from_cpp(MlirAttribute v, rv_policy,
9397
cleanup_list *cleanup) noexcept {
@@ -106,9 +110,11 @@ template <>
106110
struct type_caster<MlirBlock> {
107111
NB_TYPE_CASTER(MlirBlock, const_name("MlirBlock"))
108112
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
109-
if (auto capsule = mlirApiObjectToCapsule(src))
113+
if (auto capsule = mlirApiObjectToCapsule(src)) {
110114
value = mlirPythonCapsuleToBlock(capsule->ptr());
111-
return !mlirBlockIsNull(value);
115+
return !mlirBlockIsNull(value);
116+
}
117+
return false;
112118
}
113119
};
114120

@@ -137,9 +143,11 @@ template <>
137143
struct type_caster<MlirDialectRegistry> {
138144
NB_TYPE_CASTER(MlirDialectRegistry, const_name("MlirDialectRegistry"))
139145
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
140-
if (auto capsule = mlirApiObjectToCapsule(src))
146+
if (auto capsule = mlirApiObjectToCapsule(src)) {
141147
value = mlirPythonCapsuleToDialectRegistry(capsule->ptr());
142-
return !mlirDialectRegistryIsNull(value);
148+
return !mlirDialectRegistryIsNull(value);
149+
}
150+
return false;
143151
}
144152
static handle from_cpp(MlirDialectRegistry v, rv_policy,
145153
cleanup_list *cleanup) noexcept {
@@ -163,9 +171,11 @@ struct type_caster<MlirLocation> {
163171
.attr("Location")
164172
.attr("current");
165173
}
166-
if (auto capsule = mlirApiObjectToCapsule(src))
174+
if (auto capsule = mlirApiObjectToCapsule(src)) {
167175
value = mlirPythonCapsuleToLocation(capsule->ptr());
168-
return !mlirLocationIsNull(value);
176+
return !mlirLocationIsNull(value);
177+
}
178+
return false;
169179
}
170180
static handle from_cpp(MlirLocation v, rv_policy,
171181
cleanup_list *cleanup) noexcept {
@@ -183,9 +193,11 @@ template <>
183193
struct type_caster<MlirModule> {
184194
NB_TYPE_CASTER(MlirModule, const_name("MlirModule"))
185195
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
186-
if (auto capsule = mlirApiObjectToCapsule(src))
196+
if (auto capsule = mlirApiObjectToCapsule(src)) {
187197
value = mlirPythonCapsuleToModule(capsule->ptr());
188-
return !mlirModuleIsNull(value);
198+
return !mlirModuleIsNull(value);
199+
}
200+
return false;
189201
}
190202
static handle from_cpp(MlirModule v, rv_policy,
191203
cleanup_list *cleanup) noexcept {
@@ -204,9 +216,11 @@ struct type_caster<MlirFrozenRewritePatternSet> {
204216
NB_TYPE_CASTER(MlirFrozenRewritePatternSet,
205217
const_name("MlirFrozenRewritePatternSet"))
206218
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
207-
if (auto capsule = mlirApiObjectToCapsule(src))
219+
if (auto capsule = mlirApiObjectToCapsule(src)) {
208220
value = mlirPythonCapsuleToFrozenRewritePatternSet(capsule->ptr());
209-
return value.ptr != nullptr;
221+
return value.ptr != nullptr;
222+
}
223+
return false;
210224
}
211225
static handle from_cpp(MlirFrozenRewritePatternSet v, rv_policy,
212226
handle) noexcept {
@@ -224,9 +238,11 @@ template <>
224238
struct type_caster<MlirOperation> {
225239
NB_TYPE_CASTER(MlirOperation, const_name("MlirOperation"))
226240
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
227-
if (auto capsule = mlirApiObjectToCapsule(src))
241+
if (auto capsule = mlirApiObjectToCapsule(src)) {
228242
value = mlirPythonCapsuleToOperation(capsule->ptr());
229-
return !mlirOperationIsNull(value);
243+
return !mlirOperationIsNull(value);
244+
}
245+
return false;
230246
}
231247
static handle from_cpp(MlirOperation v, rv_policy,
232248
cleanup_list *cleanup) noexcept {
@@ -246,9 +262,11 @@ template <>
246262
struct type_caster<MlirValue> {
247263
NB_TYPE_CASTER(MlirValue, const_name("MlirValue"))
248264
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
249-
if (auto capsule = mlirApiObjectToCapsule(src))
265+
if (auto capsule = mlirApiObjectToCapsule(src)) {
250266
value = mlirPythonCapsuleToValue(capsule->ptr());
251-
return !mlirValueIsNull(value);
267+
return !mlirValueIsNull(value);
268+
}
269+
return false;
252270
}
253271
static handle from_cpp(MlirValue v, rv_policy,
254272
cleanup_list *cleanup) noexcept {
@@ -269,9 +287,11 @@ template <>
269287
struct type_caster<MlirPassManager> {
270288
NB_TYPE_CASTER(MlirPassManager, const_name("MlirPassManager"))
271289
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
272-
if (auto capsule = mlirApiObjectToCapsule(src))
290+
if (auto capsule = mlirApiObjectToCapsule(src)) {
273291
value = mlirPythonCapsuleToPassManager(capsule->ptr());
274-
return !mlirPassManagerIsNull(value);
292+
return !mlirPassManagerIsNull(value);
293+
}
294+
return false;
275295
}
276296
};
277297

@@ -280,9 +300,11 @@ template <>
280300
struct type_caster<MlirTypeID> {
281301
NB_TYPE_CASTER(MlirTypeID, const_name("MlirTypeID"))
282302
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
283-
if (auto capsule = mlirApiObjectToCapsule(src))
303+
if (auto capsule = mlirApiObjectToCapsule(src)) {
284304
value = mlirPythonCapsuleToTypeID(capsule->ptr());
285-
return !mlirTypeIDIsNull(value);
305+
return !mlirTypeIDIsNull(value);
306+
}
307+
return false;
286308
}
287309
static handle from_cpp(MlirTypeID v, rv_policy,
288310
cleanup_list *cleanup) noexcept {
@@ -302,9 +324,11 @@ template <>
302324
struct type_caster<MlirType> {
303325
NB_TYPE_CASTER(MlirType, const_name("MlirType"))
304326
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
305-
if (auto capsule = mlirApiObjectToCapsule(src))
327+
if (auto capsule = mlirApiObjectToCapsule(src)) {
306328
value = mlirPythonCapsuleToType(capsule->ptr());
307-
return !mlirTypeIsNull(value);
329+
return !mlirTypeIsNull(value);
330+
}
331+
return false;
308332
}
309333
static handle from_cpp(MlirType t, rv_policy,
310334
cleanup_list *cleanup) noexcept {

0 commit comments

Comments
 (0)