Monday, July 25, 2022

[SOLVED] Which option enables object size tracking and __builtin_object_size?

Issue

When __builtin_object_size(ptr, 1) is used in code compiled without optimization (-O0), it always returns -1. In order to get actual object size, code must be compiled with at least -O1. I would like to enable it at -O0 too, but so far I am unable to find which option enables it. I checked outputs printed generated by gcc executed with options -Q --help=optimizers, -Q --help=common and -Q --help=c and found which options are added by -O1. Unfortunately when I added them manually to command line, __builtin_object_size still returned -1.

Do you know if it is possible to somehow enable this feature when compiling at -O0?

I am using gcc 4.8.4 on Linux/x86_64.

For reference I am adding code which I used for testing:

#include <stdio.h>
#include <stdlib.h>

inline void f(const char* ptr)
{
    printf("%d\n", (int)__builtin_object_size(ptr, 1));
}

int main()
{
    char* buf = malloc(10);
    f(buf);
    return 0;
}

Solution

This is not possible, gcc does not run analysis passes necessary to compute result of __builtin_object_size at -O0.



Answered By - yugr
Answer Checked By - Terry (WPSolving Volunteer)