brotli icon indicating copy to clipboard operation
brotli copied to clipboard

panic

Open isning opened this issue 2 years ago • 8 comments

2024-06-02T03:32:31Z ERR plugins-storage/sources/gop-2044727677/src/github.com/the-ccsn/traefik-plugin-rewritebody/vendor/github.com/andybalholm/brotli/hash.go:118:9: panic: github.com/andybalholm/brotli.backwardReferencePenaltyUsingLastDistance(...) module=github.com/the-ccsn/traefik-plugin-rewritebody plugin=plugin-rewrite-body runtime=
2024-06-02T03:32:31Z ERR plugins-storage/sources/gop-2044727677/src/github.com/the-ccsn/traefik-plugin-rewritebody/vendor/github.com/andybalholm/brotli/h5.go:115:21: panic: github.com/andybalholm/brotli.FindLongestMatch(...) module=github.com/the-ccsn/traefik-plugin-rewritebody plugin=plugin-rewrite-body runtime=
2024-06-02T03:32:31Z ERR plugins-storage/sources/gop-2044727677/src/github.com/the-ccsn/traefik-plugin-rewritebody/vendor/github.com/andybalholm/brotli/backward_references.go:41:49: panic: github.com/andybalholm/brotli.createBackwardReferences(...) module=github.com/the-ccsn/traefik-plugin-rewritebody plugin=plugin-rewrite-body runtime=
2024-06-02T03:32:31Z ERR plugins-storage/sources/gop-2044727677/src/github.com/the-ccsn/traefik-plugin-rewritebody/vendor/github.com/andybalholm/brotli/encode.go:752:21: panic: github.com/andybalholm/brotli.encodeData(...) module=github.com/the-ccsn/traefik-plugin-rewritebody plugin=plugin-rewrite-body runtime=
2024-06-02T03:32:31Z ERR plugins-storage/sources/gop-2044727677/src/github.com/the-ccsn/traefik-plugin-rewritebody/vendor/github.com/andybalholm/brotli/encode.go:1135:6: panic: github.com/andybalholm/brotli.encoderCompressStream(...) module=github.com/the-ccsn/traefik-plugin-rewritebody plugin=plugin-rewrite-body runtime=
2024-06-02T03:32:31Z ERR plugins-storage/sources/gop-2044727677/src/github.com/the-ccsn/traefik-plugin-rewritebody/vendor/github.com/andybalholm/brotli/writer.go:70:5: panic: github.com/andybalholm/brotli.writeChunk(...) module=github.com/the-ccsn/traefik-plugin-rewritebody plugin=plugin-rewrite-body runtime=
2024-06-02T03:32:31Z ERR plugins-storage/sources/gop-2044727677/src/github.com/the-ccsn/traefik-plugin-rewritebody/vendor/github.com/andybalholm/brotli/writer.go:106:12: panic: github.com/andybalholm/brotli.Close(...) module=github.com/the-ccsn/traefik-plugin-rewritebody plugin=plugin-rewrite-body runtime=
2024-06-02T03:32:31Z ERR plugins-storage/sources/gop-2044727677/src/github.com/the-ccsn/traefik-plugin-rewritebody/compressutil/compressutil.go:106:10: panic: github.com/the-ccsn/traefik-plugin-rewritebody/compressutil.compressWithBrotli(...) module=github.com/the-ccsn/traefik-plugin-rewritebody plugin=plugin-rewrite-body runtime=
2024-06-02T03:32:31Z ERR plugins-storage/sources/gop-2044727677/src/github.com/the-ccsn/traefik-plugin-rewritebody/compressutil/compressutil.go:52:9: panic: github.com/the-ccsn/traefik-plugin-rewritebody/compressutil.Encode(...) module=github.com/the-ccsn/traefik-plugin-rewritebody plugin=plugin-rewrite-body runtime=
2024-06-02T03:32:31Z ERR plugins-storage/sources/gop-2044727677/src/github.com/the-ccsn/traefik-plugin-rewritebody/httputil/response_writer.go:92:18: panic: github.com/the-ccsn/traefik-plugin-rewritebody/httputil.SetContent(...) module=github.com/the-ccsn/traefik-plugin-rewritebody plugin=plugin-rewrite-body runtime=
Rewrite-Body | WARNING2024/06/02 03:32:31 restricted.go:50: Recovered from: reflect: call of reflect.Value.Uint on int Value

I'm currently working on a plugin for traefik(a reverse proxy app written in go) used for replacing response body, but for specified upstream, it produces panic. But for basic whoami testing and unit tests, it works well...

Does I miss anything? (Related code: https://github.com/the-ccsn/traefik-plugin-rewritebody/commit/378f703aa1728930a33d5159fbef2427483416a3)

version of brotli: v1.1.0

isning avatar Jun 02 '24 04:06 isning

A legible stack trace from the panic would make troubleshooting much easier.

andybalholm avatar Jun 03 '24 16:06 andybalholm

@ISNing I noticed the same problem and endet up here, too, after investigating it.

@andybalholm I cleaned up the stack trace a little bit:

vendor/github.com/andybalholm/brotli/hash.go:118:9: panic: github.com/andybalholm/brotli.backwardReferencePenaltyUsingLastDistance(...)
vendor/github.com/andybalholm/brotli/h5.go:115:21: panic: github.com/andybalholm/brotli.FindLongestMatch(...)
vendor/github.com/andybalholm/brotli/backward_references.go:41:49: panic: github.com/andybalholm/brotli.createBackwardReferences(...)
vendor/github.com/andybalholm/brotli/encode.go:752:21: panic: github.com/andybalholm/brotli.encodeData(...)
vendor/github.com/andybalholm/brotli/encode.go:1135:6: panic: github.com/andybalholm/brotli.encoderCompressStream(...)
vendor/github.com/andybalholm/brotli/writer.go:70:5: panic: github.com/andybalholm/brotli.writeChunk(...)
vendor/github.com/andybalholm/brotli/writer.go:106:12: panic: github.com/andybalholm/brotli.Close(...)
compressutil/compressutil.go:106:10: panic: github.com/the-ccsn/traefik-plugin-rewritebody/compressutil.compressWithBrotli(...)
compressutil/compressutil.go:52:9: panic: github.com/the-ccsn/traefik-plugin-rewritebody/compressutil.Encode(...)
httputil/response_writer.go:92:18: panic: github.com/the-ccsn/traefik-plugin-rewritebody/httputil.SetContent(...)
restricted.go:50: Recovered from: reflect: call of reflect.Value.Uint on int Value

I don't know anything about Go, but it looks like the bytes sent to the compressor themselves produce the error.

https://github.com/the-ccsn/traefik-plugin-rewritebody/blob/378f703aa1728930a33d5159fbef2427483416a3/compressutil/compressutil.go#L105-L122

func compressWithBrotli(bodyBytes []byte) ([]byte, error) {
	var buf bytes.Buffer
	brWriter := brotli.NewWriterLevel(&buf, brotli.DefaultCompression)


	if _, err := brWriter.Write(bodyBytes); err != nil {
		log.Printf("unable to recompress rewrited body: %v", err)


		return nil, err
	}


	if err := brWriter.Close(); err != nil {
		log.Printf("unable to close zlib writer: %v", err)


		return nil, err
	}


	return buf.Bytes(), nil
}

There seems to be no additional "interface" than the buf and the bodyBytes parameter...

Asking as a Golang noob:

  • Does buf need to be initialized somehow?
  • Can bodyBytes perhaps be mutated during the Write call?

Also, I find it interesting, that the error occurs in line 118:

https://github.com/andybalholm/brotli/blob/17e5901d050574f228e7d5a3f754a30a7cb55d55/hash.go#L117-L119

If I understand the error correctly. somehow there seems to be a negative or signed number, which shouldn't be here. Perhaps it's obvious to a Golang developer, but I can't see how that can happen.

daniel-rikowski avatar Jun 22 '24 09:06 daniel-rikowski

The stack traces reference a call to reflect.Value.Uint, but hash.go line 118 doesn't have such a call. Can you find the call reflect call that is panicking?

andybalholm avatar Jun 22 '24 15:06 andybalholm

Can you find the call reflect call that is panicking?

I tried, but was unsuccessful.

Then I found this: (from https://plugins.traefik.io/create)

Traefik plugins are developed using the Go language, and a Traefik plugin is just a Go package.

Rather than being pre-compiled and linked, however, plugins are executed on the fly by Yaegi, a Go interpreter that's embedded in the Traefik application proxy.

That would explain the oddities around this bug: It's not the code, it's the interpreter

daniel-rikowski avatar Jun 23 '24 07:06 daniel-rikowski

I'm going to venture a guess that the actual bug is in Yaegi.

andybalholm avatar Jun 24 '24 16:06 andybalholm

It seems I reproduced the panic by following example(If i didn't make any mistake on writing the code):

package main

func backwardReferencePenaltyUsingLastDistance(distance_short_code uint) uint {
        return uint(39) + ((0x1CA10 >> (distance_short_code & 0xE)) & 0xE)
}

func main() {
    println(backwardReferencePenaltyUsingLastDistance(uint(1)))
}
$ go run test.go

Produces the following output:

39
$ yaegi test.go

Produces the following output:

./test.go:4:9: panic: main.backwardReferencePenaltyUsingLastDistance(...)
./test.go:8:55: panic: main.main(...)
run: reflect: call of reflect.Value.Uint on int Value
goroutine 1 [running]:
runtime/debug.Stack()
        /usr/lib/go-1.21/src/runtime/debug/stack.go:24 +0x5e
github.com/traefik/yaegi/interp.(*Interpreter).Execute.func1()
        github.com/traefik/[email protected]/interp/program.go:146 +0x7e
panic({0xdbe800?, 0xc000486408?})
        /usr/lib/go-1.21/src/runtime/panic.go:914 +0x21f
github.com/traefik/yaegi/interp.runCfg.func1()
        github.com/traefik/[email protected]/interp/run.go:226 +0x177
panic({0xdbe800?, 0xc000486408?})
        /usr/lib/go-1.21/src/runtime/panic.go:914 +0x21f
github.com/traefik/yaegi/interp.runCfg.func1()
        github.com/traefik/[email protected]/interp/run.go:226 +0x177
panic({0xdbe800?, 0xc000486408?})
        /usr/lib/go-1.21/src/runtime/panic.go:914 +0x21f
reflect.Value.Uint(...)
        /usr/lib/go-1.21/src/reflect/value.go:2692
github.com/traefik/yaegi/interp.genValueUint.func2(0xd86580?)
        github.com/traefik/[email protected]/interp/value.go:482 +0xaf
github.com/traefik/yaegi/interp.add.func10(0x4717d3?)
        github.com/traefik/[email protected]/interp/op.go:104 +0x3a
github.com/traefik/yaegi/interp.runCfg(0xc0006f57c0, 0xc0000f26e0, 0xc0004b3930?, 0xd86780?)
        github.com/traefik/[email protected]/interp/run.go:234 +0x285
github.com/traefik/yaegi/interp.call.func9(0xc0000f2630)
        github.com/traefik/[email protected]/interp/run.go:1401 +0x7ec
github.com/traefik/yaegi/interp.runCfg(0xc0006f77c0, 0xc0000f2630, 0xc0004b39b8?, 0x0?)
        github.com/traefik/[email protected]/interp/run.go:234 +0x285
github.com/traefik/yaegi/interp.(*Interpreter).run(0xc000185440, 0xc0006f68c0, 0xc0000f2420?)
        github.com/traefik/[email protected]/interp/run.go:119 +0x3c5
github.com/traefik/yaegi/interp.(*Interpreter).Execute(0xc000185440, 0xc0006fc420)
        github.com/traefik/[email protected]/interp/program.go:172 +0x225
github.com/traefik/yaegi/interp.(*Interpreter).eval(0xc000185440, {0xc00058c400?, 0xf5?}, {0x7ffcdda34a25?, 0x200?}, 0x0?)
        github.com/traefik/[email protected]/interp/interp.go:563 +0x55
github.com/traefik/yaegi/interp.(*Interpreter).EvalPath(0xc000185440, {0x7ffcdda34a25, 0x9})
        github.com/traefik/[email protected]/interp/interp.go:512 +0xa6
main.runFile(0x7ffcdda34a25?, {0x7ffcdda34a25, 0x9}, 0x0)
        github.com/traefik/[email protected]/cmd/yaegi/run.go:153 +0xd7
main.run({0xc000036050?, 0x1, 0x1})
        github.com/traefik/[email protected]/cmd/yaegi/run.go:116 +0xb97
main.main()
        github.com/traefik/[email protected]/cmd/yaegi/yaegi.go:144 +0x2af

For latest version of yaegi, there's nothing different:

./test.go:4:9: panic: main.backwardReferencePenaltyUsingLastDistance(...)
./test.go:8:55: panic: main.main(...)
run: reflect: call of reflect.Value.Uint on int Value
goroutine 1 [running]:
runtime/debug.Stack()
        /usr/lib/go-1.21/src/runtime/debug/stack.go:24 +0x5e
github.com/traefik/yaegi/interp.(*Interpreter).Execute.func1()
        github.com/traefik/[email protected]/interp/program.go:146 +0x7e
panic({0xdbe800?, 0xc000012cc0?})
        /usr/lib/go-1.21/src/runtime/panic.go:914 +0x21f
github.com/traefik/yaegi/interp.runCfg.func1()
        github.com/traefik/[email protected]/interp/run.go:226 +0x177
panic({0xdbe800?, 0xc000012cc0?})
        /usr/lib/go-1.21/src/runtime/panic.go:914 +0x21f
github.com/traefik/yaegi/interp.runCfg.func1()
        github.com/traefik/[email protected]/interp/run.go:226 +0x177
panic({0xdbe800?, 0xc000012cc0?})
        /usr/lib/go-1.21/src/runtime/panic.go:914 +0x21f
reflect.Value.Uint(...)
        /usr/lib/go-1.21/src/reflect/value.go:2692
github.com/traefik/yaegi/interp.genValueUint.func2(0xd86580?)
        github.com/traefik/[email protected]/interp/value.go:482 +0xaf
github.com/traefik/yaegi/interp.add.func10(0x4717d3?)
        github.com/traefik/[email protected]/interp/op.go:104 +0x3a
github.com/traefik/yaegi/interp.runCfg(0xc000752b40, 0xc0000f26e0, 0xc00011f930?, 0xd86780?)
        github.com/traefik/[email protected]/interp/run.go:234 +0x285
github.com/traefik/yaegi/interp.call.func9(0xc0000f2630)
        github.com/traefik/[email protected]/interp/run.go:1401 +0x7ec
github.com/traefik/yaegi/interp.runCfg(0xc000758b40, 0xc0000f2630, 0xc00011f9b8?, 0x0?)
        github.com/traefik/[email protected]/interp/run.go:234 +0x285
github.com/traefik/yaegi/interp.(*Interpreter).run(0xc000185440, 0xc000753b80, 0xc0000f2420?)
        github.com/traefik/[email protected]/interp/run.go:119 +0x3c5
github.com/traefik/yaegi/interp.(*Interpreter).Execute(0xc000185440, 0xc00075d650)
        github.com/traefik/[email protected]/interp/program.go:172 +0x225
github.com/traefik/yaegi/interp.(*Interpreter).eval(0xc000185440, {0xc00010c300?, 0xf5?}, {0x7ffdef6d6a25?, 0x200?}, 0x0?)
        github.com/traefik/[email protected]/interp/interp.go:563 +0x55
github.com/traefik/yaegi/interp.(*Interpreter).EvalPath(0xc000185440, {0x7ffdef6d6a25, 0x9})
        github.com/traefik/[email protected]/interp/interp.go:512 +0xa6
main.runFile(0x7ffdef6d6a25?, {0x7ffdef6d6a25, 0x9}, 0x0)
        github.com/traefik/[email protected]/cmd/yaegi/run.go:153 +0xd7
main.run({0xc000036050?, 0x1, 0x1})
        github.com/traefik/[email protected]/cmd/yaegi/run.go:116 +0xb97
main.main()
        github.com/traefik/[email protected]/cmd/yaegi/yaegi.go:144 +0x2af

isning avatar Jul 27 '24 12:07 isning

Created issue for yaegi here: https://github.com/traefik/yaegi/issues/1653

isning avatar Jul 27 '24 13:07 isning

Created issue for yaegi here: https://github.com/traefik/yaegi/issues/1653

The issue of yaegi has been fixed, closing issue.

isning avatar Aug 02 '24 01:08 isning