by ksherlock · 4y ago · 0 comments
eg, from the 4.4 BSD, circa 1993:
https://minnie.tuhs.org/cgi-bin/utree.pl?file=4.4BSD/usr/src...
void *
calloc(num, size)
size_t num;
register size_t size;
{
register void *p;
size *= num;
if (p = malloc(size))
bzero(p, size);
return(p);
}[0] https://msrc-blog.microsoft.com/2021/04/29/badalloc-memory-a...
[1] Full list: https://www.cisa.gov/uscert/ics/advisories/icsa-21-119-04
[1]: https://github.com/rofl0r/musl/blob/d05aaedaabd4f5472c233dbb...
Makes me wonder what kind of compiler flags muslibc uses to prevent this.
musl probably uses -fno-builtin to disable any special handling of "builtin" library functions like malloc and memcpy?
Yeah, I also noticed that is undefined behavior!
> Makes me wonder what kind of compiler flags muslibc uses to prevent this.
As far as I can see, the only related flags are `-ffreestanding`, `-fno-builtin` and `-fno-strict-aliasing` but I think none of these flags prevent the undefined behavior if 1) either the malloc() implementation gets inlined into calloc(), or 2) calloc() gets inlined into the caller, so that the compiler realizes that `p` is not actually an array of `size_t` or compatible integers.
So perhaps musl also avoids undefined behavior by forcing `malloc()` and `calloc()` to not be inlined into the caller? Although I'm not entirely sure that actually completely prevents undefined behavior, I think it would just avoid it assuming how compilers usually/currently work?
Edit: On further investigation, I think what I said above is wrong. I think `-ffreestanding` and `-fno-builtin` should be enough to prevent undefined behavior. I wasn't considering the case where `((size_t) *p)[-1]` points to a struct like this:
struct alloc {
size_t something;
unsigned char buffer[]; // flexible array member
}
In this case, then I think the code is not triggering undefined behavior when used with those compiler flags (which prevent the compiler from using its knowledge about malloc's special semantics).
Edit 2: On second thought, even then I think it might trigger undefined behavior because the C standard doesn't guarantee that there's no padding at the end of such a struct?It then does the typical unsatisfying clang thing of unrolling and vectorizing a _lot_ with unclear benefits: https://godbolt.org/z/eG33YM7qs
Edit: Maybe Clang abuses the bitand with an int 7 to remove the code?
> 7.24.3.2 The calloc function
> […]
> Returns
> The calloc function returns either a pointer to the allocated space or a null pointer if the space cannot be allocated or if the product nmemb * size would wraparound size_t.
Gah! "Wraparound" is not a verb! You'd think someone writing a computer language specification would try to use (human) language correctly.
> the process by which a value is reduced modulo 2ᴺ, where N is the width of the resulting type
Am I the only one that doesn’t think this is “sweet”?
I mean I know what it does and why and how it works, but IMHO a good language should let you express your intention and algorithm in human terms and logic in which there is no -1 value for an unsigned integer type.
I'd rewrite it as `if (n != 0 && m > SIZE_MAX / n)`. Seems legit to me.
With these functions, it could be written as: if (__builtin_mul_overflow(n, m, &n))
It seems the C committee at some point makes things harder than they should. And true, the C committee is not preventing anyone from using something else
(Another reason why we should retire C for most projects)
if (n && m > SIZE_MAX / n)Shouldn't the case of wraparound be caught by testing the resulting size as following?
s = n*m;
if (n && s >= n && s >= m) /* no overflow */ ... p = m n
If that is gonna overflow, p > SIZE_MAX.Then it would be that (by substitution)
m n > SIZE_MAX
so m > SIZE_MAX/n
except if n = 0. In the latter case, m can be as big as it wants--p will always evaluate to 0.And (size_t)-1 is a silly way to write SIZE_MAX (but it saves you a dependency).
Edit: inverted meaning since check is for error, not for ok case.