Crash if using import "C" in main file
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
- Issue type
- Bug
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- go
- Domain
- networking
Research direction
Reproduce the crash on Windows arm64 using the reported configuration and wrapper package, focusing on the import "C" change in the main file and the New and Run entry points. Compare execution with and without cgo and capture the runtime failure; done means identifying the cause and adding a regression test or documented fix for the crashing setup.
Written by the indexing model from the issue text.
Description
i'm trying to use your library in my project, and want to wrap it for my project with go layer that exports dll file, and in this case im getting null pointer in runtime
step to reproduce:
- use Windows arm64
- import "C" in main file
- start sing-box instance
- runtime crashes with no error
sing-box config: { "log": { "level": "warning", "timestamp": true }, "dns": { "servers": [ { "tag": "remote", "address": "8.8.8.8", "detour": "proxy" } ], "final": "remote" }, "inbounds": [ { "type": "tun", "tag": "tun-in", "interface_name": "singbox_tun", "address": [ "172.19.0.1/30" ], "mtu": 9000, "auto_route": true, "strict_route": false, "stack": "gvisor", "sniff": true } ], "outbounds": [ { "type": "socks", "tag": "proxy", "server": "127.0.0.1", "server_port": 10808, "version": "5" }, { "type": "direct", "tag": "direct" }, { "type": "dns", "tag": "dns_out" } ], "route": { "auto_detect_interface": true, "rules": [ { "outbound": "direct", "ip_is_private": true }, { "outbound": "dns_out", "port": [ 53 ], "process_name": [ "xray.exe", "cracker.exe" ] }, { "outbound": "direct", "process_name": [ "xray.exe", "cracker.exe" ] }, { "outbound": "proxy", "network": "tcp,udp" } ] } }
Wrapper package that calls from main
import (
"context"
"fmt"
"os"
"os/user"
"strconv"
"github.com/sagernet/sing-box/experimental/deprecated"
"github.com/sagernet/sing-box/include"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common/json"
"github.com/sagernet/sing/service"
"github.com/sagernet/sing/service/filemanager"
box "github.com/sagernet/sing-box"
)
var (
globalCtx context.Context
)
type SingBox struct {
tun *box.Box
}
func New(configJson string) (*SingBox, error) {
configBytes := []byte(configJson)
options, err := json.UnmarshalExtendedContext[option.Options](globalCtx, configBytes)
if err != nil {
return nil, fmt.Errorf("Error while parse options from config json : ", err.Error())
}
ctx, cancel := context.WithCancel(globalCtx)
instance, err := box.New(box.Options{
Context: ctx,
Options: options,
})
if err != nil {
cancel()
return nil, fmt.Errorf("Error while creating sing box instance : ", err.Error())
}
return &SingBox{
tun: instance,
}, nil
}
func (s *SingBox) Run() error {
err := s.tun.Start()
if err != nil {
return fmt.Errorf("Error while starting sing box : ", err)
}
return nil
}
func (s *SingBox) Stop() error {
err := s.tun.Close()
if err != nil {
return fmt.Errorf("Error while stoping sing box : ", err)
}
return nil
}
func init() {
globalCtx = context.Background()
sudoUser := os.Getenv("SUDO_USER")
sudoUID, _ := strconv.Atoi(os.Getenv("SUDO_UID"))
sudoGID, _ := strconv.Atoi(os.Getenv("SUDO_GID"))
if sudoUID == 0 && sudoGID == 0 && sudoUser != "" {
sudoUserObject, _ := user.Lookup(sudoUser)
if sudoUserObject != nil {
sudoUID, _ = strconv.Atoi(sudoUserObject.Uid)
sudoGID, _ = strconv.Atoi(sudoUserObject.Gid)
}
}
if sudoUID > 0 && sudoGID > 0 {
globalCtx = filemanager.WithDefault(globalCtx, "", "", sudoUID, sudoGID)
}
globalCtx = service.ContextWith(globalCtx, deprecated.NewStderrManager(log.StdLogger()))
globalCtx = box.Context(globalCtx, include.InboundRegistry(), include.OutboundRegistry(), include.EndpointRegistry())
}```
i need to use it as library for KMM project, i'm not expirenced in go and cgo. thanks for answer in advance
- Dominant language
- Go
- Stars
- 241
- Forks
- 243
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from SagerNet/sing-tun
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
gvisor: LinkEndpointFilter.Attach never forwards nil, so GVisor.Close leaks all inbound dispatcher goroutines and fdsPossibly taken @arpitjain099 claimed this 50 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Darwin: utun process UUID headers are parsed as IP payloadPossibly taken @d3pre5s claimed this 4 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 25/100
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
-
TUN `mixed` stack: TCP never re-injected into tun0 on Linux kernel 7.1 (UDP works, `gvisor` works)Open
Difficulty 4/5 1-2 days Newbie friendliness 45/100
All issues in SagerNet/sing-tun
Similar issues
-
[Bug]: ap gateway image build cannot find local policies on WindowsPossibly taken @Jayzilva claimed this today. OpenAspect/Configuration Severity/Major
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
wso2/api-platform#3912 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
keyxmakerx/Chronicle#1721 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Gentleman-Programming/gentle-ai#5720 ·
Maintainers usually reply within 1 day
-
compress: block buffers get re-zeroed on every call, and the zeroing is immediately overwrittenOpen
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/ch-go#1218 ·
Maintainers usually reply within 13 days