correctcomputation / checkedc-clang

This is the primary development repository for 3C, a tool for automatically converting legacy C code to the Checked C extension of C, which aims to enforce spatial memory safety. This repository is a fork of Checked C's.
14 stars 5 forks source link

Transparent unions break 3C declaration merging #521

Open mattmccutchen-cci opened 3 years ago

mattmccutchen-cci commented 3 years ago

The following code (a simplified version of what is currently happening in sys/socket_checked.h) is valid C and Checked C:

struct sockaddr1 {
  int xs;
};
struct sockaddr2 {
  unsigned int xu;
};

union sockaddr_p_tu {
  struct sockaddr1 *sap1;
  struct sockaddr2 *sap2;
} __attribute__((transparent_union));

extern void mybind(union sockaddr_p_tu sap);
extern void mybind(struct sockaddr1 *sap);

but produces the following error from 3C (I presume because struct sockaddr1 * has a pointer at the top level but union sockaddr_p_tu doesn't):

/home/matt/test/transparent-union.c:14:13: fatal error: merging failed for 'mybind' due to transplanting between pointers with different depths during internal merge for parameter 0
extern void mybind(struct sockaddr1 *sap);
            ^
Failure occurred while trying to add variables. Exiting.

If the Checked C compiler isn't changed to outright reject this code as part of microsoft/checkedc#441, then ideally 3C should reject it with an error message clear enough for end users to quickly identify the problem.

mattmccutchen-cci commented 3 years ago

For whatever reason, I thought more about this issue this morning. I'm just recording my thoughts and not asking to change the prioritization of this issue.

If we want 3C to be able to process programs that #include <sys/socket.h> and set _GNU_SOURCE (such as Icecast and ImageMagick in their original form, which would be really nice), I propose that we proceed as follows:

Hopefully things would work at that point. However, since we know Microsoft hasn't tested _GNU_SOURCE at all, I wouldn't be surprised if there are remaining glitches that require minor changes to the Checked C compiler, for instance.