ampl / mp

An open-source library for mathematical programming
https://mp.ampl.com
Other
229 stars 42 forks source link

avoid load of misaligned address warning in BinaryReader #134

Closed svigerske closed 3 years ago

svigerske commented 3 years ago

I got this warning from a sanitizer when reading a .nl file with ReadNLFile:

src/amplmp/include/mp/nl-reader.h:1252:25: runtime error: load of misaligned address 0x7fa34d9211f1 for type 'const int', which requires 4 byte alignment
0x7fa34d9211f1: note: pointer points here
 00 00 00  6f 10 00 00 00 6f 02 00  00 00 76 0e 00 00 00 76  24 00 00 00 43 01 00 00  00 6f 10 00 00
              ^ 
src/amplmp/include/mp/nl-reader.h:1252:25: runtime error: load of misaligned address 0x7fa34d921669 for type 'const short int', which requires 2 byte alignment
0x7fa34d921669: note: pointer points here
 00 00 00  73 00 00 43 1d 00 00 00  73 00 00 43 1e 00 00 00  73 00 00 43 1f 00 00 00  73 00 00 43 20
              ^ 
src/amplmp/include/mp/nl-reader.h:1269:25: runtime error: load of misaligned address 0x7fa34d921706 for type 'const double', which requires 8 byte alignment
0x7fa34d921706: note: pointer points here
 00 00 00 6e 7e 69  f6 e5 71 bd 42 bf 76 02  00 00 00 6f 02 00 00 00  6e 96 0f f7 47 ce 78 71  3f 76
             ^ 

This pull request fixes this by copying the bytes to an aligned memory instead of casting.

glebbelov commented 3 years ago

Hi Stefan,

I cannot reproduce this. Could you provide the example, platform, compiler? You are on the master branch?

Gleb

svigerske commented 3 years ago

Save

#include "mp/nl-reader.h"

int main()
{
  mp::NullNLHandler<void*> handler;
  mp::ReadNLFile("alan.nl", handler);
  return EXIT_SUCCESS;
}

as pr134.cpp into the main source dir, put https://www.minlplib.org/nl/alan.nl next to it, and run

g++ pr134.cpp -Iinclude src/format.cc src/expr-info.cc src/nl-reader.cc src/os.cc src/posix.cc -fsanitize=undefined -g && ./a.out

With GCC 10 on ArchLinux, I get

include/mp/nl-reader.h:1246:25: runtime error: load of misaligned address 0x7fad530e81e2 for type 'const int', which requires 4 byte alignment
0x7fad530e81e2: note: pointer points here
 6f 31  0a 43 00 00 00 00 73 00  00 43 01 00 00 00 73 00  00 43 02 00 00 00 73 00  00 43 03 00 00 00
              ^ 
include/mp/nl-reader.h:1246:25: runtime error: load of misaligned address 0x7fad530e81e7 for type 'const short int', which requires 2 byte alignment
0x7fad530e81e7: note: pointer points here
 00 00 00 73 00  00 43 01 00 00 00 73 00  00 43 02 00 00 00 73 00  00 43 03 00 00 00 73 00  00 43 04
             ^ 
include/mp/nl-reader.h:1258:25: runtime error: load of misaligned address 0x7fad530e82c4 for type 'const double', which requires 8 byte alignment
0x7fad530e82c4: note: pointer points here
  00 00 00 00 56 76 62 27  76 62 d3 3f 01 00 00 00  d5 61 27 76 62 27 b6 3f  02 00 00 00 7c 62 27 76
              ^ 

I'm on master (the last release is already 5 years old).

Problem is that integers may not be stored at an address that is a multiple of SizeOf(Int), so converting the char* to an int* gives this error. Same for other types larger than 1 byte.

glebbelov commented 3 years ago

Yep I needed -fsanitize=undefined, then it works on other examples too.

I guess it should return this->Convert(value). Please add that, or I'll change the code myself.