Skip to content

Commit

Permalink
Add workaround for bulkBarrierPreWrite: unaligned arguments panic
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Dec 12, 2024
1 parent ad61b2e commit fd825c5
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
6 changes: 4 additions & 2 deletions app/src/main/java/io/nekohasekai/sagernet/group/RawUpdater.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.nekohasekai.sagernet.ktx.*
import libcore.Libcore
import moe.matsuri.nb4a.Protocols
import moe.matsuri.nb4a.proxy.config.ConfigBean
import moe.matsuri.nb4a.utils.Util
import org.ini4j.Ini
import org.json.JSONArray
import org.json.JSONObject
Expand Down Expand Up @@ -66,10 +67,11 @@ object RawUpdater : GroupUpdater() {
setURL(subscription.link)
setUserAgent(subscription.customUserAgent.takeIf { it.isNotBlank() } ?: USER_AGENT)
}.execute()
proxies = parseRaw(response.contentString)
proxies = parseRaw(Util.getStringBox(response.contentString))
?: error(app.getString(R.string.no_proxies_found))

subscription.subscriptionUserinfo = response.getHeader("Subscription-Userinfo")
subscription.subscriptionUserinfo =
Util.getStringBox(response.getHeader("Subscription-Userinfo"))
}

val proxiesMap = LinkedHashMap<String, AbstractBean>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import io.nekohasekai.sagernet.databinding.LayoutAssetsBinding
import io.nekohasekai.sagernet.ktx.*
import io.nekohasekai.sagernet.widget.UndoSnackbarManager
import libcore.Libcore
import moe.matsuri.nb4a.utils.Util
import org.json.JSONObject
import java.io.File
import java.io.FileWriter
Expand Down Expand Up @@ -283,7 +284,7 @@ class AssetsActivity : ThemedActivity() {
setURL("https://api.github.com/repos/$repo/releases/latest")
}.execute()

val release = JSONObject(response.contentString)
val release = JSONObject(Util.getStringBox(response.contentString))
val tagName = release.optString("tag_name")

if (tagName == localVersion) {
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/io/nekohasekai/sagernet/utils/Cloudflare.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.nekohasekai.sagernet.utils.cf.RegisterRequest
import io.nekohasekai.sagernet.utils.cf.UpdateDeviceRequest
import libcore.Libcore
import moe.matsuri.nb4a.utils.JavaUtil.gson
import moe.matsuri.nb4a.utils.Util

// kang from wgcf
object Cloudflare {
Expand Down Expand Up @@ -37,8 +38,9 @@ object Cloudflare {
setUserAgent("okhttp/3.12.1")
}.execute()

Logs.d(response.contentString)
val device = gson.fromJson(response.contentString, DeviceResponse::class.java)
Logs.d(Util.getStringBox(response.contentString))
val device =
gson.fromJson(Util.getStringBox(response.contentString), DeviceResponse::class.java)
val accessToken = device.token

client.newRequest().apply {
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/moe/matsuri/nb4a/utils/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package moe.matsuri.nb4a.utils
import android.annotation.SuppressLint
import android.content.Context
import android.util.Base64
import libcore.StringBox
import java.io.ByteArrayOutputStream
import java.text.SimpleDateFormat
import java.util.*
Expand Down Expand Up @@ -181,4 +182,10 @@ object Util {
}
}

fun getStringBox(b: StringBox?): String {
if (b != null && b.value != null) {
return b.value
}
return ""
}
}
1 change: 1 addition & 0 deletions libcore/build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

source ../../env_go.sh || true
source ../buildScript/init/env_ndk.sh

BUILD=".build"
Expand Down
12 changes: 12 additions & 0 deletions libcore/fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package libcore

// https://github.com/golang/go/issues/46893
// TODO: remove after `bulkBarrierPreWrite: unaligned arguments` fixed

type StringBox struct {
Value string
}

func wrapString(value string) *StringBox {
return &StringBox{Value: value}
}
20 changes: 14 additions & 6 deletions libcore/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ type HTTPRequest interface {
}

type HTTPResponse interface {
GetHeader(string) string
GetHeader(string) *StringBox
GetContent() ([]byte, error)
GetContentString() (string, error)
GetContentString() (*StringBox, error)
WriteTo(path string) error
}

Expand Down Expand Up @@ -204,7 +204,7 @@ type httpResponse struct {
}

func (h *httpResponse) errorString() string {
content, err := h.GetContentString()
content, err := h.getContentString()
if err != nil {
return fmt.Sprint("HTTP ", h.Status)
}
Expand All @@ -214,8 +214,8 @@ func (h *httpResponse) errorString() string {
return fmt.Sprint("HTTP ", h.Status, ": ", content)
}

func (h *httpResponse) GetHeader(key string) string {
return h.Header.Get(key)
func (h *httpResponse) GetHeader(key string) *StringBox {
return wrapString(h.Header.Get(key))
}

func (h *httpResponse) GetContent() ([]byte, error) {
Expand All @@ -226,7 +226,15 @@ func (h *httpResponse) GetContent() ([]byte, error) {
return h.content, h.contentError
}

func (h *httpResponse) GetContentString() (string, error) {
func (h *httpResponse) GetContentString() (*StringBox, error) {
content, err := h.getContentString()
if err != nil {
return nil, err
}
return wrapString(content), nil
}

func (h *httpResponse) getContentString() (string, error) {
content, err := h.GetContent()
if err != nil {
return "", err
Expand Down

0 comments on commit fd825c5

Please sign in to comment.