Live data from Hacker News

-fbounds-safety: Enforcing bounds safety for C

clang.llvm.org

11–20 of 129 posts

Re: -fbounds-safety: Enforcing bounds safety for C

#11
post #2

I want an OS distro where all C code is compiled this way. OpenBSD maybe? or a fork of CheriBSD? macOS clang has supported -fbounds-safety for a while, but I"m not sure how extensively it is used.

It is called Solaris, and has this enabled since 2015 on SPARC.

https://docs.oracle.com/en/operating-systems/solaris/oracle-...

Re: -fbounds-safety: Enforcing bounds safety for C

#12
post #2

I want an OS distro where all C code is compiled this way. OpenBSD maybe? or a fork of CheriBSD? macOS clang has supported -fbounds-safety for a while, but I"m not sure how extensively it is used.

Fedora and its kernels are built with GCC's _FORTIFY_SOURCE and I've seen modules crash for out of bounds reads.

Re: -fbounds-safety: Enforcing bounds safety for C

#13
post #10

template struct Slice { T* data = nullptr; size_t size = nullptr; T& operator[](size_t index) { if (index >= size) crash_the_program(); return data[index]; } }; If you're considering this extension, just use C++ and 5 lines of standard, portable, no-weird-annotations code instead.

Even better, starting with C++26, and considered to be done with DR for previous versions, hardned runtimes now have a portable way to be configured across compilers, instead of each having their own approach. However, you still need something like -fbounds-safety in C++, due to the copy-paste compatibility with C, and too many people writing Orthodox C++, C with Classes, Better C, kind of code, that we cannot get ri…

I'm sure std::span is great, but I like mine better :)

I find it a bit hard to justify using the STL when a single include costs 250ms compile time per compile unit.

The fact that I don't have to step through this in the debugger is also a bonus:

  template 
  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto subspan() const noexcept
      -> span {
    static_assert(_Offset ::subspan(): Offset out of range");
    static_assert(_Count == dynamic_extent || _Count ::subspan(): Offset + Count out of range");

    using _ReturnType = span;
    return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
  }

Re: -fbounds-safety: Enforcing bounds safety for C

#14

template struct Slice { T* data = nullptr; size_t size = nullptr; T& operator[](size_t index) { if (index >= size) crash_the_program(); return data[index]; } }; If you're considering this extension, just use C++ and 5 lines of standard, portable, no-weird-annotations code instead.

The extension is for hardening legacy C code without breaking ABI.

Re: -fbounds-safety: Enforcing bounds safety for C

#15

template struct Slice { T* data = nullptr; size_t size = nullptr; T& operator[](size_t index) { if (index >= size) crash_the_program(); return data[index]; } }; If you're considering this extension, just use C++ and 5 lines of standard, portable, no-weird-annotations code instead.

You should tell the LLVM folks, I guess they didn't know about this.

Re: -fbounds-safety: Enforcing bounds safety for C

#16

template struct Slice { T* data = nullptr; size_t size = nullptr; T& operator[](size_t index) { if (index >= size) crash_the_program(); return data[index]; } }; If you're considering this extension, just use C++ and 5 lines of standard, portable, no-weird-annotations code instead.

Or just do it in C.

  #define span(T) struct span_##T { size_t len; T *data; }
  #define span_access(T, x, i) (*({              \
    span(T) *_v = (x);                           \
    auto _i = (i);                               \
    if (((size_t)_i) >= _v->len) abort();        \
    &_v->data[_i];                               \
  }))
https://godbolt.org/z/TvxseshGc

Re: -fbounds-safety: Enforcing bounds safety for C

#17
post #2

I want an OS distro where all C code is compiled this way. OpenBSD maybe? or a fork of CheriBSD? macOS clang has supported -fbounds-safety for a while, but I"m not sure how extensively it is used.

does any distro uses clang? I thought all linux kernels were compiled using gcc.

Not a Linux distro, but FreeBSD uses Clang.

And Android uses Clang for its Linux kernel.

-fbounds-safety is not yet available in upstream Clang though:

> NOTE: This is a design document and the feature is not available for users yet.

Re: -fbounds-safety: Enforcing bounds safety for C

#18
post #2

I want an OS distro where all C code is compiled this way. OpenBSD maybe? or a fork of CheriBSD? macOS clang has supported -fbounds-safety for a while, but I"m not sure how extensively it is used.

Fedora and its kernels are built with GCC's _FORTIFY_SOURCE and I've seen modules crash for out of bounds reads.

_FORTIFY_SOURCE is way smaller in scope (as in, closes less vulnerabilities) than -fbounds-safety.

Re: -fbounds-safety: Enforcing bounds safety for C

#19
post #10

Earlier quoted context omitted.

Even better, starting with C++26, and considered to be done with DR for previous versions, hardned runtimes now have a portable way to be configured across compilers, instead of each having their own approach. However, you still need something like -fbounds-safety in C++, due to the copy-paste compatibility with C, and too many people writing Orthodox C++, C with Classes, Better C, kind of code, that we cannot get ri…

I'm sure std::span is great, but I like mine better :) I find it a bit hard to justify using the STL when a single include costs 250ms compile time per compile unit. The fact that I don't have to step through this in the debugger is also a bonus: template [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto subspan() const noexcept -> span { static_assert(_Offset ::subspan (): Offset out of range"); static_assert(_Coun…

Only if not able to do import std, or pre-compiled headers, and not using modern IDEs with "just my code" filters.

As someone that enjoys C++ since 1993, alongside other ecosystems, many pain points on using C++ complaints are self inflicted, by avoiding using modern tools.

Heck, C++ had nice .NET and Java alike frameworks, with bounds checking even, before those two systems came to exist, and nowadays all those frameworks are mostly gone with exception of Qt and C++ Builder ones, due to bias.

Post reply on HN