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

-autofree tries to free const fixed size arrays #21211

Open
spiveeworks opened this issue Apr 7, 2024 · 1 comment
Open

-autofree tries to free const fixed size arrays #21211

spiveeworks opened this issue Apr 7, 2024 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs. OS: Windows Bugs/feature requests, that are specific to Windows OS.

Comments

@spiveeworks
Copy link
Contributor

spiveeworks commented Apr 7, 2024

Describe the bug

When declaring a fixed size array that requires some kind of cleanup, using -autofree causes cgen to call array_free on the array itself.

This bug can be triggered declaring such an array, or simply by importing time, since time declares an array like this.

Reproduction Steps

const my_words := ["foo", "bar", "baz", "quux"]

for word in words {
    println("${word}")
}

run with `v -autofree run fixed_size_autofree_bug.v

Expected Behavior

The program to print the words and exit cleanly.

Current Behavior

The program crashes during _vcleanup:

foo
bar
baz
quux
Unhandled Exception 0xC0000374
14: RtlIsZeroMemory            ?? : address = 0x1f434d9aac0
13: RtlIsZeroMemory            ?? : address = 0x1f434d9ad80
12: RtlpNtSetValueKey          ?? : address = 0x1f434d9abf0
11: RtlpNtSetValueKey          ?? : address = 0x1f434d9ad30
10: RtlpNtSetValueKey          ?? : address = 0x1f434d9ad90
9 : RtlGetCurrentServiceSessionId  ?? : address = 0x1f434d9abb0
8 : RtlFreeHeap                ?? : address = 0x1f434d9add0
7 : free_base                  ?? : address = 0x1f434d9adc0
6 : v_free                     ?? : address = 0x1f434d9adf0
5 : array_free                 ?? : address = 0x1f434d9ad20
4 : vcleanup                   ?? : address = 0x1f434d9ac20
3 : wmain                      ?? : address = 0x1f434d9ada0
2 : __scrt_common_main_seh     D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
1 : BaseThreadInitThunk        ?? : address = 0x1f434d9adb0
0 : RtlUserThreadStart         ?? : address = 0x1f434d9aba0

Possible Solution

The C code generates these definitions:

// ...line 2059
Array_fixed_string_4 _const_main__words; // inited later
// ...line 13095
void _vcleanup(void) {
	// Cleanups for module main :
	array_free(&_const_main__words);

	// Cleanups for module builtin :
	IError_free(&_const_none__);

	// Cleanups for module strconv :

	// Cleanups for module math.bits :

	// Cleanups for module strings :

	array_free(&as_cast_type_indexes);
}

The line array_free(&_const_main__words); is what causes the crash. Instead of generating this it should be generating some other cleanup code.

Additional Information/Context

Running on Windows with msvc.

This bug can also be triggered by simply importing time in a program that uses -autofree, due to a fixed-size array of strings being declared there:
https://github.com/vlang/v/blob/master/vlib/time/time.v#L4

V version

V 0.4.5 ad17be5

Environment details (OS name and version, etc.)

V full version: V 0.4.5 386bd77.ad17be5
OS: windows, Microsoft Windows 10 Pro v19045 64-bit
Processor: 4 cpus, 64bit, little endian,

getwd: C:\Users\Owner\Code\v-experiments\fixed_size_autofree_bug
vexe: C:\Program Files\v\v.exe
vexe mtime: 2024-04-07 05:22:56

vroot: contains spaces, value: C:\Program Files\v
VMODULES: OK, value: C:\Users\Owner.vmodules
VTMP: OK, value: C:\Users\Owner\AppData\Local\Temp\v_0

Git version: git version 2.37.2.windows.2
Git vroot status: weekly.2024.14-26-gad17be5d (1 commit(s) behind V master)
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,
operable program or batch file.

thirdparty/tcc: N/A

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@spiveeworks spiveeworks added the Bug This tag is applied to issues which reports bugs. label Apr 7, 2024
@felipensp felipensp added the OS: Windows Bugs/feature requests, that are specific to Windows OS. label Apr 7, 2024
@felipensp
Copy link
Member

It seems a windows-only issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. OS: Windows Bugs/feature requests, that are specific to Windows OS.
Projects
None yet
Development

No branches or pull requests

2 participants