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

may be a bug: the program handles thefindfunctab does not consider the case where the function address spans 4kb #93

Closed
fumeboy opened this issue Nov 30, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@fumeboy
Copy link

fumeboy commented Nov 30, 2023

// goloader/ld.go
	for k, _func := range linker._func {
		funcname := getfuncname(_func, module)
		x := linker.symMap[funcname].Offset
		b := x / pcbucketsize
		i := x % pcbucketsize / (pcbucketsize / nsub)
		for lb := b - len(funcbucket); lb >= 0; lb-- {
			funcbucket = append(funcbucket, findfuncbucket{
				idx: uint32(k)}) // notice here (A)
		}
		if funcbucket[b].subbuckets[i] == 0 && b != 0 && i != 0 { // and here (B)
			if k-int(funcbucket[b].idx) >= pcbucketsize/minfunc {
				return fmt.Errorf("over %d func in one funcbuckets", k-int(funcbucket[b].idx))
			}
			funcbucket[b].subbuckets[i] = byte(k - int(funcbucket[b].idx))
		}
	}

the findfunctab has bigbucket and subbucket.

first, this program set the bigbucket.idx with value of k, but should be k==0 ? 0 : k-1,
suppose the address of a function (k-1) happens to be [4096-100, 4096 + 100], and now pc is 4096+50, this program will determine that this pc belongs to the k th function, not the k-1 th function

首先, 这个程序假设每个大桶的起始索引, 都是起始地址在这 4096byte 内的第一个函数的索引, 然而有可能一个函数的地址, 恰好“骑”在这 4KB 的分界线上, 它的起始地址在前一个大桶, 但是终结地址在第二个 4096byte, 这意味着属于第二个 4096byte 的 pc 值, 有可能是第 k-1 函数, 而不是第 k 函数; 这个很危险, 会导致程序 throw

second, this program set the subbucket.idx but ignore the empty subbucket between 2 set subbucket, and these empty subbucket should be fill by prev bucket with non-zero value

其次, 这个程序设置了小桶的值后, 忽略了中间空桶的填充, 不过这个是小问题, 只是 findfunc 有可能从 0 开始查找, 这不会导致程序 throw

@fumeboy
Copy link
Author

fumeboy commented Nov 30, 2023

cc @eh-steve

@pkujhd pkujhd added the bug Something isn't working label Nov 30, 2023
@pkujhd
Copy link
Owner

pkujhd commented Dec 1, 2023

the wrong func id won't cause a crash . it gives generate-stack function wrong function name.
but still a case will be panic. when the last function cross 4k boundary, it will be visit out of bounds of pclntable.
i will fix it

@pkujhd pkujhd closed this as completed in 2fbc174 Dec 2, 2023
eh-steve pushed a commit to eh-steve/goloader that referenced this issue Dec 7, 2023
eh-steve pushed a commit to eh-steve/goloader that referenced this issue Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants