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 with net.len() Unknown address family ~/v/vlib/net/address.c.v:111 #20635

Open
scriptmaster opened this issue Jan 23, 2024 · 8 comments · May be fixed by #20659
Open

-autofree with net.len() Unknown address family ~/v/vlib/net/address.c.v:111 #20635

scriptmaster opened this issue Jan 23, 2024 · 8 comments · May be fixed by #20659
Assignees
Labels
Autofree Bugs/feature requests, that are related to -autofree. Bug This tag is applied to issues which reports bugs.

Comments

@scriptmaster
Copy link
Contributor

scriptmaster commented Jan 23, 2024

Describe the bug

v -autofree -cg run autofree.v

================ V panic ================
   module: net
 function: len()
  message: Unknown address family
     file: /Users/.../v/vlib/net/address.c.v:111
   v hash: bb9eaca
=========================================
0   autofree                            0x00000001000872b8 net__Addr_len + 360
1   autofree                            0x0000000100092e40 picoev__listen + 1336
2   autofree                            0x000000010009267c picoev__new + 84
3   autofree                            0x0000000100093f3c main__main + 152
4   autofree                            0x0000000100095a54 main + 72
5   dyld                                0x00000001a70f7e50 start + 2544

Reproduction Steps

Run this code with: v -autofree -cg run autofree.v

module main

import picoev
import picohttpparser

fn main() {
	mut server := picoev.new(family: .ip, port: 8008, cb: perf_route)!
	server.serve()
}

fn perf_route(data voidptr, req picohttpparser.Request, mut res picohttpparser.Response) {
	res.http_ok()
	res.body('run with: v -autofree run autofree.v\n')
	// or with: v -autofree -cg autofree.v
	res.end()
}

Expected Behavior

Server should start similar to running without the -autofree flag

Current Behavior

Crashes:

================ V panic ================
   module: net
 function: len()
  message: Unknown address family
     file: /Users/.../v/vlib/net/address.c.v:111
   v hash: bb9eaca
=========================================
0   autofree                            0x00000001000872b8 net__Addr_len + 360
1   autofree                            0x0000000100092e40 picoev__listen + 1336
2   autofree                            0x000000010009267c picoev__new + 84
3   autofree                            0x0000000100093f3c main__main + 152
4   autofree                            0x0000000100095a54 main + 72
5   dyld                                0x00000001a70f7e50 start + 2544

Possible Solution

Tempfix: picoev fixed with detecting -autofree
Permfix: check and fix how autofree does for net.c.v

Additional Information/Context

No response

V version

0.4.4

Environment details (OS name and version, etc.)

Mac (same error on linux too)

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.

@scriptmaster scriptmaster added the Bug This tag is applied to issues which reports bugs. label Jan 23, 2024
@scriptmaster scriptmaster changed the title -autofree with net.len() Unknown address family /Users/msheriff/v/vlib/net/address.c.v:111 -autofree with net.len() Unknown address family ~/v/vlib/net/address.c.v:111 Jan 23, 2024
@scriptmaster
Copy link
Contributor Author

https://github.com/vlang/v/blob/master/vlib/picoev/socket_util.c.v#L126-L129

	// addr settings
	saddr := '${config.host}:${config.port}'
	addrs := net.resolve_addrs(saddr, config.family, .tcp) or { panic(err) }
	addr := addrs[0]
	alen := addr.len()

@scriptmaster
Copy link
Contributor Author

scriptmaster commented Jan 23, 2024

Added two more test-data to explain the issue:

v -autofree run examples/net_resolve.v

import net

for addr in [
	':8080' //           this works
	'127.0.0.1:12700' // this does not 
	'vlang.io:80',
	'google.com:80',
	'steampowered.com:80',
	'api.steampowered.com:80',
] {
	println('${addr}')

	for @type in [net.SocketType.tcp, .udp] {
		family := net.AddrFamily.unspec

		addrs := net.resolve_addrs(addr, family, @type) or {
			println('> None')
			continue
		}

		for a in addrs {
			f := a.family()
			println('> ${a} ${f} ${@type}')
		}
	}
}

@scriptmaster
Copy link
Contributor Author

scriptmaster commented Jan 23, 2024

v -autofree run examples/net_resolve.v                                                                                                ─╯
:8080
> :8080 ip tcp
> :8080 ip udp
127.0.0.1:12700
>  unknown enum value tcp
>  unknown enum value udp
vlang.io:80
>  unknown enum value tcp
> :80 ip tcp
> []:80 ip6 tcp
> []:80 ip6 tcp
>  unknown enum value udp
> :80 ip udp
> []:80 ip6 udp
> []:80 ip6 udp
google.com:80
>  unknown enum value tcp
> []:80 ip6 tcp
>  unknown enum value udp
> []:80 ip6 udp
steampowered.com:80
>  unknown enum value tcp
>  unknown enum value udp
api.steampowered.com:80
>  unknown enum value tcp
>  unknown enum value udp

Without autofree:

:8080
> 0.0.0.0:8080 ip tcp
> 0.0.0.0:8080 ip udp
127.0.0.1:12700
> 127.0.0.1:12700 ip tcp
> 127.0.0.1:12700 ip udp
vlang.io:80
> 104.21.22.45:80 ip tcp
> 172.67.202.167:80 ip tcp
> [2606:4700:3033::6815:162d]:80 ip6 tcp
> [2606:4700:3037::ac43:caa7]:80 ip6 tcp
> 104.21.22.45:80 ip udp
> 172.67.202.167:80 ip udp
> [2606:4700:3033::6815:162d]:80 ip6 udp
> [2606:4700:3037::ac43:caa7]:80 ip6 udp
google.com:80
> 142.250.193.142:80 ip tcp
> [2404:6800:4007:820::200e]:80 ip6 tcp
> 142.250.193.142:80 ip udp
> [2404:6800:4007:820::200e]:80 ip6 udp
steampowered.com:80
> 23.220.236.78:80 ip tcp
> 23.220.236.78:80 ip udp
api.steampowered.com:80
> 23.58.39.59:80 ip tcp
> 23.58.39.59:80 ip udp

@scriptmaster
Copy link
Contributor Author

scriptmaster commented Jan 23, 2024

if the host is set to ":" it works, for example:

picoev.new(host: ":", port: 8008, cb: perf_route)

this is because of these lines in vlib/net/address.c.v:resolve_ipaddrs:170-180

if addr[0] == `:` {
		match family {
			.ip6 {
				return [new_ip6(port, net.addr_ip6_any)]
			}
			.ip, .unspec {
				return [new_ip(port, net.addr_ip_any)]
			}
			else {}
		}
	}

@scriptmaster
Copy link
Contributor Author

The issue comes down to something wrong when pushing an item to an array when using -autofree

addresses << new_addr

@scriptmaster
Copy link
Contributor Author

same with println("string interpolation $myvar") with -autofree

@shove70
Copy link
Contributor

shove70 commented Jan 24, 2024

After simplifying:

fn foo () ![]int {
	mut arr := []int{}
	arr << 1
	return arr
}

fn main() {
	a := foo()!
	println(a)
}

v -autofree -cg run a.v:

[91]
a(69493,0x7ff8463cbb80) malloc: Double free of object 0x7f810ef05c10
a(69493,0x7ff8463cbb80) malloc: *** set a breakpoint in malloc_error_break to debug
Terminated by signal  6 (SIGABRT)

@shove70 shove70 self-assigned this Jan 24, 2024
shove70 added a commit to shove70/v that referenced this issue Jan 26, 2024
@shove70 shove70 linked a pull request Jan 26, 2024 that will close this issue
@felipensp felipensp added the Autofree Bugs/feature requests, that are related to -autofree. label Nov 3, 2024
@enghitalo enghitalo reopened this Nov 3, 2024
@enghitalo
Copy link
Contributor

enghitalo commented Nov 3, 2024

fn foo () ![]int {
	mut arr := []int{}
	arr << 1
	return arr
}

fn main() {
	a := foo()!
	println(a)
}

TCC

v -autofree -cg run <file>
[10843]
free(): double free detected in tcache 2
0x7336a4a9eb1c: at ???: RUNTIME ERROR: abort() called
0x7336a4a4526e: by ???
0x7336a4a288ff: by ???
0x7336a4a297b6: by ???
0x7336a4aa8fe5: by ???
0x7336a4aab54f: by ???
0x7336a4aadd9e: by ???
/tmp/v_1000/parse.01JBQQGPM5EFE4Z0GPSS78DPR0.tmp.c:7616: by _v_free
/tmp/v_1000/parse.01JBQQGPM5EFE4Z0GPSS78DPR0.tmp.c:6760: by array_free
/tmp/v_1000/parse.01JBQQGPM5EFE4Z0GPSS78DPR0.tmp.c:13518: by main__main
/tmp/v_1000/parse.01JBQQGPM5EFE4Z0GPSS78DPR0.tmp.c:13565: by main

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

Successfully merging a pull request may close this issue.

4 participants