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

"obf name not found, this should never happen" when build with obfuscation enabled #19695

Closed
bencinn opened this issue Oct 29, 2023 · 5 comments · Fixed by #19778
Closed

"obf name not found, this should never happen" when build with obfuscation enabled #19695

bencinn opened this issue Oct 29, 2023 · 5 comments · Fixed by #19778
Labels
Bug This tag is applied to issues which reports bugs. Build V build error on any OS/CPU architecture. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@bencinn
Copy link

bencinn commented Oct 29, 2023

Describe the bug

obf name "main.xx" not found, this should never happen when building with -obf flags

Reproduction Steps

test.v:

fn add(a int, b int) int {
	return a + b
}

fn main() {
	println(add(1, 3))
}

v -o test -autofree -show-timings -obf -skip-unused -prod -arch x64 -os linux -cc gcc test.v
Edit: minimized the reproduction build step.
v -o test -obf test.v

Expected Behavior

It build

Current Behavior

54.470   ms Builder.front_stages.parse_files
0.083    ms Builder.resolve_deps
21.476   ms Builder.parse_imports
19.259   ms SCAN
52.204   ms PARSE
75.996   ms ALL_FRONT_STAGES
0.003    ms Checker.generic_insts_to_concrete
23.720   ms CHECK
4.851    ms TRANSFORM
0.037    ms Table.complete_interface_check
1.404    ms all_fn_const_and_global
2.410    ms mark_used
0.576    ms Gen.sort_structs
0.957    ms cgen init
V panic: cgen: fn_decl: obf name "main.add" not found, this should never happen
v hash: 99e0832

Possible Solution

No response

Additional Information/Context

No response

V version

0.4.2 00165d1

Environment details (OS name and version, etc.)

V full version: V 0.4.2 714a64d.00165d1
OS: linux, "Gentoo Linux"
Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz

getwd: /home/rori/code/stuff
vexe: /home/rori/code/v/v
vexe mtime: 2023-10-29 09:53:09

vroot: OK, value: /home/rori/code/v
VMODULES: OK, value: /home/rori/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.41.0
Git vroot status: weekly.2023.43-43-g00165d14
.git/config present: true

CC version: cc (Gentoo 13.2.1_p20230826 p7) 13.2.1 20230826
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

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.

@bencinn bencinn added the Bug This tag is applied to issues which reports bugs. label Oct 29, 2023
@ArtemkaKun ArtemkaKun added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Build V build error on any OS/CPU architecture. labels Oct 29, 2023
@bencinn
Copy link
Author

bencinn commented Oct 30, 2023

After I tried debugging, it seems that the g.obf_table is always empty?
I tried this.

// vlib/v/gen/c/cgen.v
	if g.pref.obfuscate {
		mut i := 0
		// fns
		for key, f in g.table.fns {
			println('key: ${key}, f: ${f}')
			if f.mod != 'main' &&  key != 'main' {
				continue
			}
			println('inserting key ${key} into the obf_table?')
			g.obf_table[key] = '_f${i}'
			i++
		}
		// ...
// vlib/v/gen/c/fn.v
if g.pref.obfuscate && g.cur_mod.name == 'main' && name.starts_with('main__') && !node.is_main
		&& node.name != 'str' {
		mut key := node.name
		if node.is_method {
			sym := g.table.sym(node.receiver.typ)
			key = sym.name + '.' + node.name
		}
		g.writeln('/* obf: ${key} */')
		name = g.obf_table[key] or {
	println('obf_table: ${g.obf_table}')
				println('${node}')
			panic('cgen: fn_decl: obf name "${key}" not found, this should never happen')
		}
	}
// ...
inserting key main.add into the obf_table?
...
obf_table: {}

Race condition?

@JalonSolov
Copy link
Contributor

All you need to show the error is v -obf test.v (since -o test is the default... it uses the base name of the file as the output filename).

@JalonSolov JalonSolov added the Status: Confirmed This bug has been confirmed to be valid by a contributor. label Oct 30, 2023
@bencinn
Copy link
Author

bencinn commented Oct 30, 2023

All you need to show the error is v -obf test.v (since -o test is the default... it uses the base name of the file as the output filename).

Thanks!

@bencinn
Copy link
Author

bencinn commented Oct 30, 2023

Ok more debug information.
It seems that the map obf_table got eaten/overwritten/deleted?

// vlib/v/gen/c/cgen.v
for key, f in g.table.fns {
			println('key: ${key}, f: ${f}')
			if f.mod != 'main' && key != 'main' {
				continue
			}
			println('inserting key ${key} into the obf_table?')
			g.obf_table[key] = '_f${i}'
			println('table after inserting key ${key}: ${g.obf_table}')
			i++
		}
// ...
table after inserting key main.main: {'main.add': '_f0', 'main.main': '_f1'}
...
obf_table: {}
...

@medvednikov
Copy link
Member

Good find, looks we are not testing -obf, need to add it to CI.

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. Build V build error on any OS/CPU architecture. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants