Repository navigation
Fix stack reserve to read from the COFF file header - #150
Conversation
c186b56 to
ea7ba74
Compare
nojb
left a comment
There was a problem hiding this comment.
Personally I would have left out the second commit :), but I'm not against it.
LGTM
| @@ -1047,7 +1047,7 @@ module Stacksize = struct | |||
| let hdr_offset = int16 (read ic 0x3c 2) 0 in | |||
There was a problem hiding this comment.
This is orthogonal to your patch, but isn't the hdr_offset 32-bit wide? https://en.wikipedia.org/wiki/Portable_Executable https://en.wikipedia.org/wiki/Portable_Executable#/media/File:Portable_Executable_32_bit_Structure_in_SVG_fixed.svg
There was a problem hiding this comment.
You're right, it's a LONG. I'll update the commit!
There was a problem hiding this comment.
If the offset fits in 16 bits, I suppose it's safe to forget the high-order bit from int32 to int.
|
How come the bug hasn't been noticed since 2012?… |
Well, the offset of the PE header (the value stored at 0x3c) is typically a rather small integer (something like 0xE8), stored little-endian, which fits nicely in 16 bits. |
f8f6ba6 to
87c83da
Compare
dra27
left a comment
There was a problem hiding this comment.
A tiny tweak to suggest, but otherwise thanks for this!
It's naturally great to fix this, but just to note that I don't think there was ever a problem with writing executables. The bug caused the wrong header size to be read but the optional header was being accessed at the correct offsets (and it was only be read to determine if it's PE32 or PE32+) - i.e. as long as the incorrectly read size was not less than 2 or greater than the size of the file, then the code worked correctly (and wrote the correctly sized 32 or 64bit value to the correct place in the executable).
While I started being a little grumpy about the renaming, actually I think it's well worth having on this occasion, thank you! The name machine was especially confusing as there is a field machine in the COFF header, but the field being read was actually the magic number from the Optional Header.
42c9f36 to
81933a4
Compare
| let size_of_stack_reserve_offset = pe_signature_offset + 4 + 20 + 72 in | ||
| (* begin match pe_format with | ||
| | `PE32 -> Printf.printf "SizeOfStackReserve 0x%08x\n%!" (int32 opthdr 72) | ||
| | `PE32plus -> Printf.printf "SizeOfStackReserve 0x%016Lx\n%!" (int64 opthdr 72) |
There was a problem hiding this comment.
There is no int64 function, but if there was, this would print the value of the field as hex (it's unsigned and little-endian). The bit twiddling is fastidious.
There was a problem hiding this comment.
I hadn't spotted this bit of the change the first time around - I'm not that fond of the commented out debugging bits in the codebase as it is, but I'm not sure that having commented out code which has never worked is a positive change! Can I suggest we just delete the original comment (which would have been there from debugging the original 32-bit implementation, I expect)?
There was a problem hiding this comment.
Done, I've removed it.
| emit_int32 oc size_of_stack_reserve; | ||
| if pe_format = `PE32plus then emit_int32 oc 0l; |
There was a problem hiding this comment.
Should we expand this to support stack sizes bigger than
There was a problem hiding this comment.
Unless there’s a call for it, no! I can’t find a reference to it, but I don’t think it’s supported even on 64bit Windows
The COFF File Header (Object and Image) [1] starts just after the signature "PE\0\0". The `read ic 0 20` seeks the offset 0 (the start) in the PE file, instead of the offset of the header. This leads to incorrectly reading and writing values in the PE headers. Fix reading the file address of new exe header, a LONG instead of a WORD. Thanks to Nicolás Ojeda Bär. Remove outdated debugging comment: it's printing a int32, but the field is actually an uint32 on PE32 and an uint64 on PE32+. [1]: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#coff-file-header-object-and-image
81933a4 to
a633e1c
Compare
|
Thank you! |
An invocation of flexlink (with some debug patches added) would raise:
After investigating, I've found out that flexlink incorrectly reads the PE executable. The COFF File Header (Object and Image) starts just after the signature
"PE\0\0". Theread ic 0 20seeks the offset 0 (the start) in the PE file, instead of the offset of the header. This leads to incorrectly readingand writingvalues in the PE headers.The first patch fixes the problem, the second renames the variables to follow the documentation more closely.