Skip to content

Commit f300e82

Browse files
authored
Merge pull request #118 from arik-so/arik/2023/09/find-route-segfault-fix
Fix findRoute segfault
2 parents 4323dca + e03076f commit f300e82

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

ci/LDKSwift/Tests/LDKSwiftTests/HumanObjectPeerTestInstance.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,39 @@ public class HumanObjectPeerTestInstance {
676676

677677
let recreatedInvoice = Bolt11Invoice.fromStr(s: invoice.toStr())
678678
XCTAssertTrue(recreatedInvoice.isOk())
679+
680+
// find route
681+
682+
do {
683+
let payerPubkey = peer1.channelManager.getOurNodeId()
684+
let payeePubkey = peer2.channelManager.getOurNodeId()
685+
let paymentParameters = PaymentParameters.initForKeysend(payeePubkey: payeePubkey, finalCltvExpiryDelta: 3, allowMpp: false)
686+
687+
let amount = invoice.amountMilliSatoshis()!
688+
let routeParameters = RouteParameters(paymentParamsArg: paymentParameters, finalValueMsatArg: amount)
689+
let randomSeedBytes: [UInt8] = [UInt8](repeating: 0, count: 32)
690+
let scoringParams = ProbabilisticScoringDecayParameters.initWithDefault()
691+
let networkGraph = peer1.constructor!.netGraph!
692+
let scorer = ProbabilisticScorer(decayParams: scoringParams, networkGraph: networkGraph, logger: logger)
693+
let score = scorer.asScore()
694+
695+
let scoreParams = ProbabilisticScoringFeeParameters.initWithDefault()
696+
697+
let foundRoute = Bindings.findRoute(
698+
ourNodePubkey: payerPubkey,
699+
routeParams: routeParameters,
700+
networkGraph: networkGraph,
701+
firstHops: usableChannelsA,
702+
logger: logger,
703+
scorer: score,
704+
scoreParams: scoreParams,
705+
randomSeedBytes: randomSeedBytes
706+
)
707+
708+
let route = foundRoute.getValue()!
709+
let fees = route.getTotalFees()
710+
print("found route fees: \(fees)")
711+
}
679712

680713
let channelManagerConstructor = peer1.constructor!
681714
let invoicePaymentResult = Bindings.payInvoice(invoice: invoice, retryStrategy: Bindings.Retry.initWithAttempts(a: 3), channelmanager: channelManagerConstructor.channelManager)

out/Bindings.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,9 @@ public class Bindings {
12361236
if let firstHops = firstHops {
12371237

12381238
let firstHopsVector = Vec_ChannelDetailsZ(
1239-
array: firstHops, instantiationContext: "Bindings.swift::\(#function):\(#line)")
1239+
array: firstHops, instantiationContext: "Bindings.swift::\(#function):\(#line)"
1240+
)
1241+
.dangle()
12401242

12411243
firstHopsVectorPointer = UnsafeMutablePointer<LDKCVec_ChannelDetailsZ>.allocate(capacity: 1)
12421244
firstHopsVectorPointer!.initialize(to: firstHopsVector.cType!)

out/traits/Router.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ extension Bindings {
269269
if let firstHops = firstHops {
270270

271271
let firstHopsVector = Vec_ChannelDetailsZ(
272-
array: firstHops, instantiationContext: "Router.swift::\(#function):\(#line)")
272+
array: firstHops, instantiationContext: "Router.swift::\(#function):\(#line)"
273+
)
274+
.dangle()
273275

274276
firstHopsVectorPointer = UnsafeMutablePointer<LDKCVec_ChannelDetailsZ>.allocate(capacity: 1)
275277
firstHopsVectorPointer!.initialize(to: firstHopsVector.cType!)
@@ -323,7 +325,9 @@ extension Bindings {
323325
if let firstHops = firstHops {
324326

325327
let firstHopsVector = Vec_ChannelDetailsZ(
326-
array: firstHops, instantiationContext: "Router.swift::\(#function):\(#line)")
328+
array: firstHops, instantiationContext: "Router.swift::\(#function):\(#line)"
329+
)
330+
.dangle()
327331

328332
firstHopsVectorPointer = UnsafeMutablePointer<LDKCVec_ChannelDetailsZ>.allocate(capacity: 1)
329333
firstHopsVectorPointer!.initialize(to: firstHopsVector.cType!)

src/generation/base_type_generator.mts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,13 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
735735
// memoryManagementInfix = ''
736736
// }
737737
}
738+
} else if (argument.isAsteriskPointer && this.isPointerArgumentNullable(argument, containerType)) {
739+
// if we're taking a nullable argument that needs to be passed to Rust as a nullable pointer,
740+
// the initialization of the argument will occur inside an if let block, and the value
741+
// to which the pointer will later refer must not get immediately cleaned up.
742+
// It is not quite clear to me how to determine when to, eventually, clear that memory,
743+
// which is why for the time being, this actually results in some additional direct leaks.
744+
memoryManagementInfix = '.dangle()';
738745
}
739746
} else if (argument.type instanceof RustPrimitiveWrapper && argument.type.isDeallocatable()) {
740747
// memoryManagementInfix = '.dangle()';

0 commit comments

Comments
 (0)