Skip to content

Fix stack reserve to read from the COFF file header - #150

Merged
dra27 merged 2 commits into
ocaml:masterfrom
MisterDA:fix-stack-reserve
Jan 16, 2025
Merged

dra27 merged 2 commits into
ocaml:masterfrom
MisterDA:fix-stack-reserve

Conversation

@MisterDA

@MisterDA MisterDA commented Jan 14, 2025 •

Copy link
Copy Markdown
Contributor

An invocation of flexlink (with some debug patches added) would raise:

> flexlink -exe -chain msvc64 -merge-manifest -stack 33554432 -link /ENTRY:wmainCRTStartup  -o runtime/ocamlrun.exe runtime/prims.obj runtime/libcamlrun.lib ws2_32.lib ole32.lib uuid.lib advapi32.lib shell32.lib version.lib shlwapi.lib synchronization.lib
Raised by primitive operation at Coff.int16 in file "coff.ml", line 242, characters 30-49
Called from Coff.Stacksize.set_stack_reserve in file "coff.ml", line 1055, characters 12-26
Called from Reloc.patch_output in file "reloc.ml", line 654, characters 16-54
Cannot set stack reserve: Invalid_argument("index out of bounds")

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". 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.

The first patch fixes the problem, the second renames the variables to follow the documentation more closely.

@nojb nojb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would have left out the second commit :), but I'm not against it.

LGTM

Comment thread coff.ml Outdated
@@ -1047,7 +1047,7 @@ module Stacksize = struct
let hdr_offset = int16 (read ic 0x3c 2) 0 in

@nojb nojb Jan 14, 2025 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it's a LONG. I'll update the commit!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the offset fits in 16 bits, I suppose it's safe to forget the high-order bit from int32 to int.

@MisterDA

Copy link
Copy Markdown
Contributor Author

How come the bug hasn't been noticed since 2012?…

@nojb

nojb commented Jan 14, 2025

Copy link
Copy Markdown
Collaborator

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.

@MisterDA
MisterDA force-pushed the fix-stack-reserve branch 2 times, most recently from f8f6ba6 to 87c83da Compare January 14, 2025 23:34

@dra27 dra27 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread coff.ml Resolved
@MisterDA
MisterDA force-pushed the fix-stack-reserve branch 2 times, most recently from 42c9f36 to 81933a4 Compare January 15, 2025 16:08
Comment thread coff.ml Outdated
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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I've removed it.

Comment thread coff.ml
Comment on lines +1069 to +1066
emit_int32 oc size_of_stack_reserve;
if pe_format = `PE32plus then emit_int32 oc 0l;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we expand this to support stack sizes bigger than $2^{32}-1$ bytes?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@MisterDA MisterDA changed the title Fix stack reserve Fix stack reserve to read from the COFF file header Jan 15, 2025
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
@dra27
dra27 merged commit 2d7a03e into ocaml:master Jan 16, 2025
@dra27

dra27 commented Jan 16, 2025

Copy link
Copy Markdown
Member

Thank you!

@MisterDA
MisterDA deleted the fix-stack-reserve branch January 16, 2025 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants