This is an archive of the discontinued LLVM Phabricator instance.

[BuildLibCalls] Add noundef to the returned pointers of allocators and argument of free
ClosedPublic

Authored by aqjune on Sep 20 2020, 2:13 AM.

Details

Summary

This patch adds noundef to the returned pointers of allocators (malloc, calloc, ...)
and the pointer argument of free.
The returned pointer of allocators cannot be poison or (partially) undef.
Since the pointer that is given to free should precisely have zero offset,
it cannot be poison or (partially) undef too.

For the size arguments of allocators, noundef wasn't attached simply because
I wasn't sure whether attaching it is okay or not.

Diff Detail

Event Timeline

aqjune created this revision.Sep 20 2020, 2:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 20 2020, 2:13 AM
aqjune requested review of this revision.Sep 20 2020, 2:13 AM

If it is desirable to clarify the behavior of malloc and free in LangRef as well, I'm willing to do it.

The definition of malloc that is consistent with this patch is:

  • If an undefined value is given to malloc, it allocates a heap with one of possible values nondeterministically chosen. For example, malloc(16 + (undef & 4)) allocates a heap whose size is one of 16 ~ 19.
  • If a poison value is given to malloc, it raises UB.

The definition of free is as follows:

  • If an undefined value or poison is given to free, it is UB.

I remember that malloc also had an issue about when it is returning null. This issue is orthogonal with this patch, but if needed I'm happy to discuss it as well.

jdoerfert accepted this revision.Sep 27 2020, 9:49 AM

LGTM. I think this is the right way to handle this. the size can be partially undef (="non-deterministic & variable") but everything else better is not.

This revision is now accepted and ready to land.Sep 27 2020, 9:49 AM