22 Commits
Author SHA1 Message Date
Amit ad3df93bed Add Go wrappers for Get/SetVirtualDiskInformation win32 APIs
SetVirtualDiskInformation API is required for running confidential windows containers. The
VHDs used for starting confidential pods/UVMs need to have a specific disk
identifier. These newly added APIs will be used when preparing the VHDs for confidential
pods.

Signed-off-by: Amit <ambarve@microsoft.com>
2025-10-27 09:08:22 -07:00
Amit fd34511314 Switch to Go 1.23 and fix CI failures
Some new upcoming changes require us to use Go 1.23. However, if we switch to Go 1.23 some
new linter errors are showing up. This commit fixes most of the errors and adds an
exclusion for integer overflow errors.

Signed-off-by: Amit <ambarve@microsoft.com>
2025-10-27 09:08:22 -07:00
Hamza El-Saawy 3c9576c934 Update go1.21 and CI (#315)
* Update to go 1.21

Use `atomic.Bool` stdlib instead of including our own.

Include `tools\mkwinsyscall` updates from go-winio/283 to switch to
`syscallN`.
Note: removed `// TODO` about `print`/`ln`, since the latter adds spaces
between args when printing, which is undesired.

Also update CI to run steps on windows-2022 instead of windows-2019,
similar to our hcsshim CI.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

* PR: simplify type checking

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

---------

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
2024-04-09 16:07:04 -04:00
Hamza El-Saawy e268c11e27 Add lint and go generate steps to CI (#254)
* Add lint and go generate stages to CI

Add CI step to verify `go generate` was run on repo.
Add linter stage to CI along with linter config file,
`.golangci.yml`.
Will likely prefer revive over static-check.

Updated README Contributing section on linting requirements.

Added sequence ordering to make sure lint and go generate stages run
before tests and build.
This way, build and tests are not run on code that could potentially:

    1. not build due to `gofmt` issues;
    2. contain bugs;
    3. have to be re-submitted after issues are fixed; or
    4. contain outdated Win32 syscall or other auto-generated files.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

* Fixed linter issues

Code changes to satisfy linters:

 - Ran `gofmt -s -w` on repo.
 - Broke up long lines.
 - When possible, changed names with incorrect initialism formatting
   - Added exceptions for exported variables.
 - Added exceptions for ALL_CAPS_WITH_UNDERSCORES code.
   - Switched to using `windows` or `syscall` definitions if possible;
     especially if some constants were unused.
 - Added `_ =` to satisfy error linter, and acknowledge that errors are
   being ignored.
 - Switched to using `errors.Is` and `As` in places, elsewhere added
   exceptions if error value was known to be `syscall.Errno`.
 - Removed bare returns.
 - Prevented variables from being overshadowed in certain places
   (ignoring cases of overshadowing `err`).
 - Renamed variables and functions (eg, `len`, `eventMetadata.bytes`) to
   prevent shadowing pre-built functions and imported pacakges.
 - Removed unused method receivers.
 - Added exceptions to certain unused (unexported) constants and
   functions.
   - Deleted unused `once` from `pkg/etw.providerMap`.
 - Renamed `noop.go` files to `main_other.go` or `doc.go`, to better fit
   style recommendations.
 - Added exceptions for non-secure use of SHA1 and weak crypto
   libraries.
 - Replaced `ioutil` with `io` and `os` (and `t.TempDir` in tests).
 - Added fully exhaustive checks for `switch` statements in `pkg/etw`.
 - Defined constant strings for `tools/mkwinsyscall`.
 - Removed unnecessary conversions.
 - Made sure `context.Cancel` was called.

Additionally, added `//go:build windows" constraints on files with
unexported code, since linter will complain about unused code on
non-Windows platforms.

Added a stub `main() {}` for `mkwinsyscall` for non-Windows builds, just in
case `//go:generate` directives are added to OS-agnostic files.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

* PR: spelling, constants, fuzzing

Moved HVSocket fuzzing tests to separate file with go 1.18 build
constraint.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
2022-08-23 15:05:05 -04:00
Hamza El-Saawy 35837cff61 Update mkwinsyscall and make stand alone tool (#248)
* Add latest mkwinsyscall file

Copied file from:
https://github.com/golang/sys/blob/bc2c85ada10aa9b6aa9607e9ac9ad0761b95cf1d/windows/mkwinsyscall/mkwinsyscall.go

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

* Update to match hcsshim mksyscall_windows

Added hr error return type, cmdline flags for winio import, utf16, and
sorting.

Generated file has build constrain comment.

Input files can be specified with a glob pattern.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

* PR: update comment, use glob

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
2022-06-16 14:05:19 -04:00
Danny Canter 7de02db153 Fix 'OpenVirtualDiskParameters' BOOL fields (#226)
* Fix 'OpenVirtualDiskParameters' BOOL fields

While reworking the vhd package I'd mistakingly replaced some of the fields
in the OpenVersion2 structure with the incorrect types. They're defined as
Windows BOOLS which is just a type alias for an int, and I'd put their type as
go bools.

As the type is already passed into some functions, to avoid a breaking change just
convert the incorrect type to a new structure with the correct definition internally.
Truthfully this turns out a bit better as supplying a bool makes more sense and is
more go friendly.

Signed-off-by: Daniel Canter <dcanter@microsoft.com>
2022-02-14 21:11:00 -08:00
Michael Hofmann c4cf81c481 Replace github.com/pkg/errors with stdlib errors
Since Go version 1.13, the standard library has built-in support for
wrapping and unwrapping of error values.  This commit bumps the minimum
version required by the module from 1.12 to 1.13, replaces all calls to
errors.Wrap/f with fmt.Errorf and removes the now unneeded dependency on
github.com/pkg/errors.

Signed-off-by: Michael Hofmann <michael.hofmann@bitgestalt.com>
2021-12-01 11:09:15 +01:00
Kevin Parsons 9f0ab2cc0a Fix syscall error handling in vhd and pkg/security
These packages had incorrect error handling for their generated syscall
bindings. The functions they were calling returned errors directly, but
the binding was written such that the generated code was calling
GetLastError instead.

Thankfully, this did not affect the detection of whether or not an error
had occurred, it only caused the value returned in the case of an error
to be not the right error code.

Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
2021-04-26 15:08:11 -07:00
Paul "TBBle" Hampson bc48b61dc9 Make local mksyscall_windows.go forks less forked
At this point points of difference between the three local
mksyscall_windows.go implementations and
golang.org/x/sys/windows/mkwinsyscall v0.0.0-20210104204734-6f8348627aad
are:
- pkg/etw: Deduplicates imported functions due to multiple calling APIs
- pkg/security and vhd: Forced UTF-16 mode, not checking A/W suffix. I
  also removed cosmetic differences between these two implementations.

`go generate` reports no changes with these updates.

Signed-off-by: Paul "TBBle" Hampson <Paul.Hampson@Pobox.com>
2021-02-11 22:50:55 +11:00
Paul "TBBle" Hampson 1390d1a07b Pull sync generated syscalls
This moves all the current users of
$GOROOT/src/syscall/mksyscall_windows.go to instead use
golang.org/x/sys/windows/mkwinsyscall, as directed by the version of the
former in Go 1.15.

It also syncs the local forks of mksyscall_windows.go with the latest
version of golang.org/x/sys/windows/mkwinsyscall/mkwinsyscall.go, so
that the local patches can be easily seen in a side-by-side comparison.
Significant changes compared to the in-tree forked versions:
* *bool parameters are read back through a temp-var, not directly like
  other pointer parameters.
* ?-suffixed function names support testing for function presence before
  calling. This replaces a local implementation of this in
  pkg/security, which was not actually used anyway. The upstream version
  correctly supports functions that don't already have an error return.
* `errnoErr(0)` is now useful, so each call of `errnoErr` doesn't need
  to be protected with a check for 0 first.
* The generated functions are now sorted. This of course produced a
  *lot* of churn in the generated files.

vhd\vhd.go was changed to generate syscalls into zvhd_windows.go, since
regeneration removes the build tag added by hand in
9d8277341f.

After all that, I also ran
```
go generate . .\pkg\etw\ .\pkg\process\ .\pkg\security\ .\vhd\
```
to update all the existing generated code.

Signed-off-by: Paul "TBBle" Hampson <Paul.Hampson@Pobox.com>
2021-02-11 22:47:40 +11:00
Kathryn Baldauf 68cdd9bd9d Merge pull request #191 from uhthomas/master
Add build guards for all Windows imports
2021-02-10 17:11:08 -08:00
Thomas Way 9d8277341f Add build guards for all Windows imports 2021-01-21 23:24:01 +00:00
Daniel Canter 83fd17628f Remove error if ATTACH_VIRTUAL_DISK_PARAMETERS wasn't version 2
* Version 2 didn't exist in RS5 so it's a bit silly to error out in this case.

Signed-off-by: Daniel Canter <dcanter@microsoft.com>
2021-01-18 02:50:23 -08:00
Daniel Canter e9604c63ae Add new virtdisk calls/refactor vhd package
This change adds bindings for AttachVirtualDisk and GetVirtualDiskPhysicalPath, some helper functions including
one to create a differencing vhd, exports the create and open parameters, exports the create flags and adds a couple
of missing flags. This will be used mainly to go hand in hand with the new computestorage calls added to hcsshim as all of the
vhd creation and setup needs to be done manually for these calls. https://github.com/microsoft/hcsshim/pull/881.

Signed-off-by: Daniel Canter <dcanter@microsoft.com>
2020-11-30 08:23:06 -08:00
Justin Terry (VM) 5adf6781fa Fix regression in DetachVhd
When opening a disk it for deatch using 'Version == 2' parameters it is
required that the'accessMask == VirtualDiskAccessNone'. Also updates the
virtual disk 'flag' parameter to match the set specified by the product when
activating the layer.

Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
2019-08-06 11:28:36 -07:00
Justin 4de24ed3e8 Merge pull request #111 from Microsoft/jjh/openvhd
Add OpenVirtualDisk to vhd library
2019-03-12 15:15:28 -07:00
John Howard dcdaf955de Add OpenVirtualDisk to vhd package
Signed-off-by: John Howard <jhoward@microsoft.com>
2019-03-12 11:57:15 -07:00
CodeLingo Bot ea6bc01ef5 Fix function comments based on best practices from Effective Go
Signed-off-by: CodeLingo Bot <bot@codelingo.io>
2019-03-11 00:56:50 +00:00
John Howard 275ee76c20 Add DetachVhd entrypoint
Signed-off-by: John Howard <jhoward@microsoft.com>
2018-08-23 15:01:31 -07:00
John Howard 09c0aff8d5 Convert all files to unix line endings
Signed-off-by: John Howard <jhoward@microsoft.com>
2018-08-23 14:56:23 -07:00
Stefan Wernli 24d09c6f4b Adding VHD library with simple function, updating mksyscall
Adds the basic structs for VHD function calls and includes one simple wrapper for VHDX creation.
This includes the updated version of mksyscall_windows.go, which previously assumed any api that doesn't end in "W" must not be unicode, and encodes the strings incorrectly. This hard-codes for unicode support and generates the syscalls from there.
2017-07-11 21:12:04 -07:00
Stefan Wernli 1bbb8c8d24 Copying mksyscall_windows from golang 2017-07-11 20:18:55 -07:00