<commit_before>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runtime

import (
	"unsafe"
)

//go:cgo_import_dynamic runtime._AddVectoredExceptionHandler AddVectoredExceptionHandler "kernel32.dll"
//go:cgo_import_dynamic runtime._CloseHandle CloseHandle "kernel32.dll"
//go:cgo_import_dynamic runtime._CreateEventA CreateEventA "kernel32.dll"
//go:cgo_import_dynamic runtime._CreateThread CreateThread "kernel32.dll"
//go:cgo_import_dynamic runtime._CreateWaitableTimerA CreateWaitableTimerA "kernel32.dll"
//go:cgo_import_dynamic runtime._CryptAcquireContextW CryptAcquireContextW "advapi32.dll"
//go:cgo_import_dynamic runtime._CryptGenRandom CryptGenRandom "advapi32.dll"
//go:cgo_import_dynamic runtime._CryptReleaseContext CryptReleaseContext "advapi32.dll"
//go:cgo_import_dynamic runtime._DuplicateHandle DuplicateHandle "kernel32.dll"
//go:cgo_import_dynamic runtime._ExitProcess ExitProcess "kernel32.dll"
//go:cgo_import_dynamic runtime._FreeEnvironmentStringsW FreeEnvironmentStringsW "kernel32.dll"
//go:cgo_import_dynamic runtime._GetEnvironmentStringsW GetEnvironmentStringsW "kernel32.dll"
//go:cgo_import_dynamic runtime._GetProcAddress GetProcAddress "kernel32.dll"
//go:cgo_import_dynamic runtime._GetStdHandle GetStdHandle "kernel32.dll"
//go:cgo_import_dynamic runtime._GetSystemInfo GetSystemInfo "kernel32.dll"
//go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext "kernel32.dll"
//go:cgo_import_dynamic runtime._LoadLibraryW LoadLibraryW "kernel32.dll"
//go:cgo_import_dynamic runtime._LoadLibraryA LoadLibraryA "kernel32.dll"
//go:cgo_import_dynamic runtime._NtWaitForSingleObject NtWaitForSingleObject "ntdll.dll"
//go:cgo_import_dynamic runtime._ResumeThread ResumeThread "kernel32.dll"
//go:cgo_import_dynamic runtime._SetConsoleCtrlHandler SetConsoleCtrlHandler "kernel32.dll"
//go:cgo_import_dynamic runtime._SetEvent SetEvent "kernel32.dll"
//go:cgo_import_dynamic runtime._SetProcessPriorityBoost SetProcessPriorityBoost "kernel32.dll"
//go:cgo_import_dynamic runtime._SetThreadPriority SetThreadPriority "kernel32.dll"
//go:cgo_import_dynamic runtime._SetUnhandledExceptionFilter SetUnhandledExceptionFilter "kernel32.dll"
//go:cgo_import_dynamic runtime._SetWaitableTimer SetWaitableTimer "kernel32.dll"
//go:cgo_import_dynamic runtime._Sleep Sleep "kernel32.dll"
//go:cgo_import_dynamic runtime._SuspendThread SuspendThread "kernel32.dll"
//go:cgo_import_dynamic runtime._WaitForSingleObject WaitForSingleObject "kernel32.dll"
//go:cgo_import_dynamic runtime._WriteFile WriteFile "kernel32.dll"
//go:cgo_import_dynamic runtime._timeBeginPeriod timeBeginPeriod "winmm.dll"

var (
	_AddVectoredExceptionHandler,
	_CloseHandle,
	_CreateEventA,
	_CreateThread,
	_CreateWaitableTimerA,
	_CryptAcquireContextW,
	_CryptGenRandom,
	_CryptReleaseContext,
	_DuplicateHandle,
	_ExitProcess,
	_FreeEnvironmentStringsW,
	_GetEnvironmentStringsW,
	_GetProcAddress,
	_GetStdHandle,
	_GetSystemInfo,
	_GetThreadContext,
	_LoadLibraryW,
	_LoadLibraryA,
	_NtWaitForSingleObject,
	_ResumeThread,
	_SetConsoleCtrlHandler,
	_SetEvent,
	_SetProcessPriorityBoost,
	_SetThreadPriority,
	_SetUnhandledExceptionFilter,
	_SetWaitableTimer,
	_Sleep,
	_SuspendThread,
	_WaitForSingleObject,
	_WriteFile,
	_timeBeginPeriod stdFunction
)

var _GetQueuedCompletionStatusEx stdFunction

// in sys_windows_386.s and sys_windows_amd64.s
func externalthreadhandler()
func exceptiontramp()
func firstcontinuetramp()
func lastcontinuetramp()

//go:nosplit
func getLoadLibrary() uintptr {
	return uintptr(unsafe.Pointer(_LoadLibraryW))
}

//go:nosplit
func getGetProcAddress() uintptr {
	return uintptr(unsafe.Pointer(_GetProcAddress))
}

func getproccount() int32 {
	var info systeminfo
	stdcall1(_GetSystemInfo, uintptr(unsafe.Pointer(&info)))
	return int32(info.dwnumberofprocessors)
}

const (
	currentProcess = ^uintptr(0) // -1 = current process
	currentThread  = ^uintptr(1) // -2 = current thread
)

var (
	kernel32Name                    = []byte("kernel32.dll\x00")
	addVectoredContinueHandlerName  = []byte("AddVectoredContinueHandler\x00")
	getQueuedCompletionStatusExName = []byte("GetQueuedCompletionStatusEx\x00")
)

func osinit() {
	setBadSignalMsg()

	kernel32 := stdcall1(_LoadLibraryA, uintptr(unsafe.Pointer(&kernel32Name[0])))

	externalthreadhandlerp = funcPC(externalthreadhandler)

	stdcall2(_AddVectoredExceptionHandler, 1, funcPC(exceptiontramp))
	addVectoredContinueHandler := uintptr(0)
	if kernel32 != 0 {
		addVectoredContinueHandler = stdcall2(_GetProcAddress, kernel32, uintptr(unsafe.Pointer(&addVectoredContinueHandlerName[0])))
	}
	if addVectoredContinueHandler == 0 || unsafe.Sizeof(&kernel32) == 4 {
		// use SetUnhandledExceptionFilter for windows-386 or
		// if VectoredContinueHandler is unavailable.
		// note: SetUnhandledExceptionFilter handler won't be called, if debugging.
		stdcall1(_SetUnhandledExceptionFilter, funcPC(lastcontinuetramp))
	} else {
		stdcall2(stdFunction(unsafe.Pointer(addVectoredContinueHandler)), 1, funcPC(firstcontinuetramp))
		stdcall2(stdFunction(unsafe.Pointer(addVectoredContinueHandler)), 0, funcPC(lastcontinuetramp))
	}

	stdcall2(_SetConsoleCtrlHandler, funcPC(ctrlhandler), 1)

	stdcall1(_timeBeginPeriod, 1)

	ncpu = getproccount()

	// Windows dynamic priority boosting assumes that a process has different types
	// of dedicated threads -- GUI, IO, computational, etc. Go processes use
	// equivalent threads that all do a mix of GUI, IO, computations, etc.
	// In such context dynamic priority boosting does nothing but harm, so we turn it off.
	stdcall2(_SetProcessPriorityBoost, currentProcess, 1)

	if kernel32 != 0 {
		_GetQueuedCompletionStatusEx = stdFunction(unsafe.Pointer(stdcall2(_GetProcAddress, kernel32, uintptr(unsafe.Pointer(&getQueuedCompletionStatusExName[0])))))
	}
}

var random_data [_HashRandomBytes]byte

//go:nosplit
func get_random_data(rnd *unsafe.Pointer, rnd_len *int32) {
	const (
		prov_rsa_full       = 1
		crypt_verifycontext = 0xF0000000
	)
	var handle uintptr
	*rnd = nil
	*rnd_len = 0
	if stdcall5(_CryptAcquireContextW, uintptr(unsafe.Pointer(&handle)), 0, 0, prov_rsa_full, crypt_verifycontext) != 0 {
		if stdcall3(_CryptGenRandom, handle, _HashRandomBytes, uintptr(unsafe.Pointer(&random_data[0]))) != 0 {
			*rnd = unsafe.Pointer(&random_data[0])
			*rnd_len = _HashRandomBytes
		}
		stdcall2(_CryptReleaseContext, handle, 0)
	}
}

func goenvs() {
	var p *uint16

	env := (*uint16)(unsafe.Pointer(stdcall0(_GetEnvironmentStringsW)))

	n := 0
	for p = env; *p != 0; n++ {
		p = (*uint16)(add(unsafe.Pointer(p), uintptr(findnullw(p)+1)))
	}

	envs = makeStringSlice(int(n))

	p = env
	for i := 0; i < n; i++ {
		envs[i] = gostringw(p)
		p = (*uint16)(add(unsafe.Pointer(p), uintptr(findnullw(p)+1)))
	}

	stdcall1(_FreeEnvironmentStringsW, uintptr(unsafe.Pointer(env)))
}

//go:nosplit
func exit(code int32) {
	stdcall1(_ExitProcess, uintptr(code))
}

//go:nosplit
func write(fd uintptr, buf unsafe.Pointer, n int32) int32 {
	const (
		_STD_OUTPUT_HANDLE = ^uintptr(10) // -11
		_STD_ERROR_HANDLE  = ^uintptr(11) // -12
	)
	var handle uintptr
	switch fd {
	case 1:<commit_msg>[dev.cc] runtime: fix windows goenvs conversion mistake<commit_after>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runtime

import (
	"unsafe"
)

//go:cgo_import_dynamic runtime._AddVectoredExceptionHandler AddVectoredExceptionHandler "kernel32.dll"
//go:cgo_import_dynamic runtime._CloseHandle CloseHandle "kernel32.dll"
//go:cgo_import_dynamic runtime._CreateEventA CreateEventA "kernel32.dll"
//go:cgo_import_dynamic runtime._CreateThread CreateThread "kernel32.dll"
//go:cgo_import_dynamic runtime._CreateWaitableTimerA CreateWaitableTimerA "kernel32.dll"
//go:cgo_import_dynamic runtime._CryptAcquireContextW CryptAcquireContextW "advapi32.dll"
//go:cgo_import_dynamic runtime._CryptGenRandom CryptGenRandom "advapi32.dll"
//go:cgo_import_dynamic runtime._CryptReleaseContext CryptReleaseContext "advapi32.dll"
//go:cgo_import_dynamic runtime._DuplicateHandle DuplicateHandle "kernel32.dll"
//go:cgo_import_dynamic runtime._ExitProcess ExitProcess "kernel32.dll"
//go:cgo_import_dynamic runtime._FreeEnvironmentStringsW FreeEnvironmentStringsW "kernel32.dll"
//go:cgo_import_dynamic runtime._GetEnvironmentStringsW GetEnvironmentStringsW "kernel32.dll"
//go:cgo_import_dynamic runtime._GetProcAddress GetProcAddress "kernel32.dll"
//go:cgo_import_dynamic runtime._GetStdHandle GetStdHandle "kernel32.dll"
//go:cgo_import_dynamic runtime._GetSystemInfo GetSystemInfo "kernel32.dll"
//go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext "kernel32.dll"
//go:cgo_import_dynamic runtime._LoadLibraryW LoadLibraryW "kernel32.dll"
//go:cgo_import_dynamic runtime._LoadLibraryA LoadLibraryA "kernel32.dll"
//go:cgo_import_dynamic runtime._NtWaitForSingleObject NtWaitForSingleObject "ntdll.dll"
//go:cgo_import_dynamic runtime._ResumeThread ResumeThread "kernel32.dll"
//go:cgo_import_dynamic runtime._SetConsoleCtrlHandler SetConsoleCtrlHandler "kernel32.dll"
//go:cgo_import_dynamic runtime._SetEvent SetEvent "kernel32.dll"
//go:cgo_import_dynamic runtime._SetProcessPriorityBoost SetProcessPriorityBoost "kernel32.dll"
//go:cgo_import_dynamic runtime._SetThreadPriority SetThreadPriority "kernel32.dll"
//go:cgo_import_dynamic runtime._SetUnhandledExceptionFilter SetUnhandledExceptionFilter "kernel32.dll"
//go:cgo_import_dynamic runtime._SetWaitableTimer SetWaitableTimer "kernel32.dll"
//go:cgo_import_dynamic runtime._Sleep Sleep "kernel32.dll"
//go:cgo_import_dynamic runtime._SuspendThread SuspendThread "kernel32.dll"
//go:cgo_import_dynamic runtime._WaitForSingleObject WaitForSingleObject "kernel32.dll"
//go:cgo_import_dynamic runtime._WriteFile WriteFile "kernel32.dll"
//go:cgo_import_dynamic runtime._timeBeginPeriod timeBeginPeriod "winmm.dll"

var (
	_AddVectoredExceptionHandler,
	_CloseHandle,
	_CreateEventA,
	_CreateThread,
	_CreateWaitableTimerA,
	_CryptAcquireContextW,
	_CryptGenRandom,
	_CryptReleaseContext,
	_DuplicateHandle,
	_ExitProcess,
	_FreeEnvironmentStringsW,
	_GetEnvironmentStringsW,
	_GetProcAddress,
	_GetStdHandle,
	_GetSystemInfo,
	_GetThreadContext,
	_LoadLibraryW,
	_LoadLibraryA,
	_NtWaitForSingleObject,
	_ResumeThread,
	_SetConsoleCtrlHandler,
	_SetEvent,
	_SetProcessPriorityBoost,
	_SetThreadPriority,
	_SetUnhandledExceptionFilter,
	_SetWaitableTimer,
	_Sleep,
	_SuspendThread,
	_WaitForSingleObject,
	_WriteFile,
	_timeBeginPeriod stdFunction
)

var _GetQueuedCompletionStatusEx stdFunction

// in sys_windows_386.s and sys_windows_amd64.s
func externalthreadhandler()
func exceptiontramp()
func firstcontinuetramp()
func lastcontinuetramp()

//go:nosplit
func getLoadLibrary() uintptr {
	return uintptr(unsafe.Pointer(_LoadLibraryW))
}

//go:nosplit
func getGetProcAddress() uintptr {
	return uintptr(unsafe.Pointer(_GetProcAddress))
}

func getproccount() int32 {
	var info systeminfo
	stdcall1(_GetSystemInfo, uintptr(unsafe.Pointer(&info)))
	return int32(info.dwnumberofprocessors)
}

const (
	currentProcess = ^uintptr(0) // -1 = current process
	currentThread  = ^uintptr(1) // -2 = current thread
)

var (
	kernel32Name                    = []byte("kernel32.dll\x00")
	addVectoredContinueHandlerName  = []byte("AddVectoredContinueHandler\x00")
	getQueuedCompletionStatusExName = []byte("GetQueuedCompletionStatusEx\x00")
)

func osinit() {
	setBadSignalMsg()

	kernel32 := stdcall1(_LoadLibraryA, uintptr(unsafe.Pointer(&kernel32Name[0])))

	externalthreadhandlerp = funcPC(externalthreadhandler)

	stdcall2(_AddVectoredExceptionHandler, 1, funcPC(exceptiontramp))
	addVectoredContinueHandler := uintptr(0)
	if kernel32 != 0 {
		addVectoredContinueHandler = stdcall2(_GetProcAddress, kernel32, uintptr(unsafe.Pointer(&addVectoredContinueHandlerName[0])))
	}
	if addVectoredContinueHandler == 0 || unsafe.Sizeof(&kernel32) == 4 {
		// use SetUnhandledExceptionFilter for windows-386 or
		// if VectoredContinueHandler is unavailable.
		// note: SetUnhandledExceptionFilter handler won't be called, if debugging.
		stdcall1(_SetUnhandledExceptionFilter, funcPC(lastcontinuetramp))
	} else {
		stdcall2(stdFunction(unsafe.Pointer(addVectoredContinueHandler)), 1, funcPC(firstcontinuetramp))
		stdcall2(stdFunction(unsafe.Pointer(addVectoredContinueHandler)), 0, funcPC(lastcontinuetramp))
	}

	stdcall2(_SetConsoleCtrlHandler, funcPC(ctrlhandler), 1)

	stdcall1(_timeBeginPeriod, 1)

	ncpu = getproccount()

	// Windows dynamic priority boosting assumes that a process has different types
	// of dedicated threads -- GUI, IO, computational, etc. Go processes use
	// equivalent threads that all do a mix of GUI, IO, computations, etc.
	// In such context dynamic priority boosting does nothing but harm, so we turn it off.
	stdcall2(_SetProcessPriorityBoost, currentProcess, 1)

	if kernel32 != 0 {
		_GetQueuedCompletionStatusEx = stdFunction(unsafe.Pointer(stdcall2(_GetProcAddress, kernel32, uintptr(unsafe.Pointer(&getQueuedCompletionStatusExName[0])))))
	}
}

var random_data [_HashRandomBytes]byte

//go:nosplit
func get_random_data(rnd *unsafe.Pointer, rnd_len *int32) {
	const (
		prov_rsa_full       = 1
		crypt_verifycontext = 0xF0000000
	)
	var handle uintptr
	*rnd = nil
	*rnd_len = 0
	if stdcall5(_CryptAcquireContextW, uintptr(unsafe.Pointer(&handle)), 0, 0, prov_rsa_full, crypt_verifycontext) != 0 {
		if stdcall3(_CryptGenRandom, handle, _HashRandomBytes, uintptr(unsafe.Pointer(&random_data[0]))) != 0 {
			*rnd = unsafe.Pointer(&random_data[0])
			*rnd_len = _HashRandomBytes
		}
		stdcall2(_CryptReleaseContext, handle, 0)
	}
}

func goenvs() {
	var p *uint16

	env := (*uint16)(unsafe.Pointer(stdcall0(_GetEnvironmentStringsW)))

	n := 0
	for p = env; *p != 0; n++ {
		p = (*uint16)(add(unsafe.Pointer(p), uintptr(findnullw(p)+1)*unsafe.Sizeof(*p)))
	}

	envs = makeStringSlice(int(n))

	p = env
	for i := 0; i < n; i++ {
		envs[i] = gostringw(p)
		p = (*uint16)(add(unsafe.Pointer(p), uintptr(findnullw(p)+1)*unsafe.Sizeof(*p)))
	}

	stdcall1(_FreeEnvironmentStringsW, uintptr(unsafe.Pointer(env)))
}

//go:nosplit
func exit(code int32) {
	stdcall1(_ExitProcess, uintptr(code))
}

//go:nosplit
func write(fd uintptr, buf unsafe.Pointer, n int32) int32 {
	const (
		_STD_OUTPUT_HANDLE = ^uintptr(10) // -11
		_STD_ERROR_HANDLE  = ^uintptr(11) // -12
	)
	var handle uintptr
	switch fd {
	case 1:
{'fraction_non_alphanumeric': 0.1003579295154185, 'fraction_numerical': 0.02477973568281938, 'mean_word_length': 6.575078206465068, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2821320', 'n_tokens_mistral': 5218, 'n_tokens_neox': 4748, 'n_words': 937}