SChernykh / p2pool

Decentralized pool for Monero mining
GNU General Public License v3.0
1.03k stars 123 forks source link

Fail to build due to compile flag -Wcast-align #283

Closed LithiumTins closed 8 months ago

LithiumTins commented 8 months ago

I tried to build p2pool on RISCV64 ArchLinux but failed. The errors are like:

/build/p2pool/src/p2pool/src/common.h: In member function ‘bool p2pool::raw_ip::operator==(const p2pool::raw_ip&) const’:
/build/p2pool/src/p2pool/src/common.h:443:37: error: cast from ‘const uint8_t*’ {aka ‘const unsigned char*’} to ‘const uint64_t*’ {aka ‘const long unsigned int*’} increases required alignment of target type [-Werror=cast-align]
  443 |                 const uint64_t* a = reinterpret_cast<const uint64_t*>(data);
      |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It seems the warning is caused by cast from uint8_t* to uint64_t* increases the alignment, and with -Wcast-align and -Werror the compilation is terminated.

I've carefully looked into this problem and found that:

  1. The compiler doesn't give out warnings on x86_64 no matter whether the alignment is increased and is well treated. However, it gives out warnings on RISCV64 when the alignment is increased even when the operand has been aligned as target type. For example, in the following code data is well-aligned and the warning above is still caused(the line number doesn't match): https://github.com/SChernykh/p2pool/blob/5988acb12b767f7237650840a8bc7e47114a4684/src/common.h#L426-L439
  2. I manually check every line that causes warning like this and almost all of them are well-aligned. There are some exceptions using function read_unaligned() in src\util.h , but I think it's ok because they do intend touse the pointer as an unaligned one. Here is one example: https://github.com/SChernykh/p2pool/blob/5988acb12b767f7237650840a8bc7e47114a4684/src/p2p_server.cpp#L1600-L1604

Due to the reasons above, the compile flag -Wcast-align actually help nothing because the project indeed use casts that increase the alignment , while it does stop p2pool to build on RISCV64. Thus I suggest remove this compile flag.

SChernykh commented 8 months ago

https://github.com/SChernykh/p2pool/commit/5daa2b9dfffcef72286b8429d4c3d65f535a70a8