[PATCH] D56760: Add a new builtin: __builtin_dynamic_object_size

Erik Pilkington via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 15 16:52:51 PST 2019


erik.pilkington created this revision.
erik.pilkington added reviewers: george.burgess.iv, ahatanak, rjmccall, rsmith.
Herald added subscribers: kristina, dexonsmith, jkorous.

This builtin has the same UI as `__builtin_object_size`, but has the potential to be evaluated dynamically. It is meant to be used as a drop-in replacement for libraries that use `__builtin_object_size` when a dynamic checking mode is enabled. For instance, `__builtin_object_size` fails to provide any extra checking in the following function:

  void f(size_t alloc) {
    char* p = malloc(alloc);
    strcpy(p, "foobar"); // expands to __builtin___strcpy_chk(p, "foobar", __builtin_object_size(p, 0))
  }

This is an overflow if `alloc < 7`, but because LLVM can't fold the object size intrinsic statically, it folds `__builtin_object_size` to `-1`. With __builtin_dynamic_object_size, `alloc` is passed through to `__builtin___strcpy_chk`.

rdar://problem/32212419 ER: evaluate builtin_objectsize (or a successor) at runtime, at least when alloc_size is available

Thanks for taking a look!
Erik


Repository:
  rC Clang

https://reviews.llvm.org/D56760

Files:
  clang/docs/LanguageExtensions.rst
  clang/include/clang/Basic/Builtins.def
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Analysis/CFG.cpp
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/CodeGen/CodeGenFunction.h
  clang/lib/Sema/SemaChecking.cpp
  clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
  clang/test/CodeGen/alloc-size.c
  clang/test/CodeGen/catch-undef-behavior.c
  clang/test/CodeGen/object-size.c
  clang/test/CodeGen/object-size.cpp
  clang/test/Sema/builtin-object-size.c

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56760.181923.patch
Type: text/x-patch
Size: 59378 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190116/df21abc6/attachment-0001.bin>


More information about the cfe-commits mailing list