Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug/pe: add Machine type with String method #60198

Open
jamietanna opened this issue May 15, 2023 · 17 comments
Open

debug/pe: add Machine type with String method #60198

jamietanna opened this issue May 15, 2023 · 17 comments

Comments

@jamietanna
Copy link

When using the debug/pe package to look up information about binaries, I noticed that there's a subtle API difference compared to debug/macho and debug/elf.

For instance, in the debug/elf package, Machine is defined as being able to call:

f.Machine.String()

Whereas when using the debug/pe package, Machine has no such definition in the FileHeader.

It would be good to align the API of this package alongside how we do it in the other packages:

  • Extract a type for Machine
  • Add a String() method
  • Add a GoString() method
@gopherbot gopherbot added this to the Proposal milestone May 15, 2023
@gopherbot
Copy link

Change https://go.dev/cl/508401 mentions this issue: debug/pe: add Machine type to align API with debug/elf and debug/macho

@jamietanna
Copy link
Author

Hey folks, just wondering if there's anything I can do to help this along? Appreciate it's only a small proposal and there are a lot of the bigger ones to go through at the moment!

@qmuntal
Copy link
Contributor

qmuntal commented Sep 14, 2023

Hi @jamietanna. I'm afraid this is a backwards incompatible change, as you are changing the definition of pe.FileHeader.

@jamietanna
Copy link
Author

That's fair - I'm sure there's a way we can make this non-breaking, while still allowing for this new metadata to be usable?

@josharian
Copy link
Contributor

Could we expose a new method in debug/* to get at the Machine? (What would it be called?)

@rsc
Copy link
Contributor

rsc commented Oct 11, 2023

We can add the type, but if we can't use it anywhere in the API, is there much value?

@josharian
Copy link
Contributor

Folks could type assert to the larger interface?

@rsc
Copy link
Contributor

rsc commented Oct 11, 2023

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc rsc changed the title proposal: debug/pe: Introduce a Machine type with a corresponding String() method proposal: debug/pe: add Machine type with String method Oct 24, 2023
@rsc
Copy link
Contributor

rsc commented Oct 24, 2023

There's not really a larger interface, just a different integer type. So you'd have to say pe.Machine(fh.Machine).String(). I guess that's fine but it's a little weird.

@jamietanna
Copy link
Author

Interesting. Any suggestions for how we can move forwards?

@rsc
Copy link
Contributor

rsc commented Oct 24, 2023

I believe the proposal is:

In package pe, add:

type Machine uint16
func (m Machine) String() string
func (m Machine) GoString() string

similar to elf.Machine. The existing Machine uint16 in type FileHeader would not change, for compatibility. Users who want to print a file header’s machine need to use

fmt.Println(pe.Machine(fh.Machine))

@jamietanna
Copy link
Author

jamietanna commented Oct 24, 2023

Gotcha, that makes sense, thanks!

Would we also be able to make the following change:

-	IMAGE_FILE_MACHINE_UNKNOWN     = 0x0
+	IMAGE_FILE_MACHINE_UNKNOWN     Machine = 0x0

Or would this introduce a breaking change? I think probably yes it would be a breaking change?

@rsc
Copy link
Contributor

rsc commented Oct 25, 2023

@jamietanna Yes, that would be a breaking change, and we can't do that.

@jamietanna
Copy link
Author

Gotcha, thanks!

I've amended the proposed code to fit within this, which should now no longer be a breaking change. Thanks for the feedback and suggestions folks.

I've also verified it with the following test:

package pe

import (
	"fmt"
	"testing"
)

func TestMachineToString(t *testing.T) {
	c := IMAGE_FILE_MACHINE_WCEMIPSV2
	fmt.Printf("c: %v\n", c)

	fmt.Printf("Machine(c): %v\n", Machine(c))
	fmt.Printf("Machine(c).GoString(): %v\n", Machine(c).GoString())

	c = 0
	fmt.Printf("c: %v\n", c)

	fmt.Printf("Machine(c): %v\n", Machine(c))
	fmt.Printf("Machine(c).GoString(): %v\n", Machine(c).GoString())

	c = 0x5128
	fmt.Printf("c: %v\n", c)

	fmt.Printf("Machine(c): %v\n", Machine(c))
	fmt.Printf("Machine(c).GoString(): %v\n", Machine(c).GoString())
}

Test output:

=== RUN   TestMachineToString
c: 361
Machine(c): IMAGE_FILE_MACHINE_WCEMIPSV2
Machine(c).GoString(): pe.IMAGE_FILE_MACHINE_WCEMIPSV2
c: 0
Machine(c): IMAGE_FILE_MACHINE_UNKNOWN
Machine(c).GoString(): pe.IMAGE_FILE_MACHINE_UNKNOWN
c: 20776
Machine(c): IMAGE_FILE_MACHINE_RISCV128
Machine(c).GoString(): pe.IMAGE_FILE_MACHINE_RISCV128

@rsc
Copy link
Contributor

rsc commented Oct 26, 2023

Based on the discussion above, this proposal seems like a likely accept.
— rsc for the proposal review group

In package pe, add:

type Machine uint16
func (m Machine) String() string
func (m Machine) GoString() string

similar to elf.Machine. The existing Machine uint16 in type FileHeader would not change, for compatibility. Users who want to print a file header’s machine need to use

fmt.Println(pe.Machine(fh.Machine))

@rsc
Copy link
Contributor

rsc commented Nov 2, 2023

No change in consensus, so accepted. 🎉
This issue now tracks the work of implementing the proposal.
— rsc for the proposal review group

In package pe, add:

type Machine uint16
func (m Machine) String() string
func (m Machine) GoString() string

similar to elf.Machine. The existing Machine uint16 in type FileHeader would not change, for compatibility. Users who want to print a file header’s machine need to use

fmt.Println(pe.Machine(fh.Machine))

@rsc rsc changed the title proposal: debug/pe: add Machine type with String method debug/pe: add Machine type with String method Nov 2, 2023
@rsc rsc modified the milestones: Proposal, Backlog Nov 2, 2023
@jamietanna
Copy link
Author

Awesome 👏🏽 for next steps, is it a case of the linked changeset getting reviewed when the core team have a chance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Accepted
Development

No branches or pull requests

6 participants