mirror of
https://github.com/netbirdio/gomobile-tvos-fork.git
synced 2026-05-22 18:43:29 -07:00
mobile/bind: fix a reference count race with the garbage collectors
Each side of the language barrier maintains a map of reference numbers to objects. Each entry has a reference count that exactly matches the number of active proxy objects on the other side. When a reference crosses the barrier, the count is incremented and when a proxy finalizer is run, the count is decremented. If the count reaches 0, the reference number and its object are removed from the map. There is a possibility that a reference number is passed to the other side, and the last proxy is then immediately garbage collected and finalized. The reference counter then reaches 0 before the other side has converted the reference number to its object, crashing the program. This is possible in both Go/Java/ObjC but is most likely to happen in ObjC because its own automatic reference count runtime frees objects as soon as they are statically never referenced again. Fix the race by always incrementing the reference count before sending a reference across the barrier. When converting the reference back into an object on the other side, decrement the counter again. Only the new ObjC test fails without this fix, but I left the Java counterpart in for good measure. Change-Id: I92743aabec275b4a5b82b952052e7e284872ce02 Reviewed-on: https://go-review.googlesource.com/21311 Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
+13
-1
@@ -502,8 +502,20 @@ public class SeqTest extends InstrumentationTestCase {
|
||||
Secondpkg.Ser ser = Testpkg.NewSer();
|
||||
}
|
||||
|
||||
public void testIDup() {
|
||||
public void testRoundtripEquality() {
|
||||
Testpkg.I want = new AnI();
|
||||
assertTrue("java object passed through Go should not be wrapped", want == Testpkg.IDup(want));
|
||||
Testpkg.InterfaceDupper idup = new Testpkg.InterfaceDupper.Stub(){
|
||||
@Override public Testpkg.Interface IDup(Testpkg.Interface i) {
|
||||
return i;
|
||||
}
|
||||
};
|
||||
assertTrue("Go interface passed through Java should not be wrapped", Testpkg.CallIDupper(idup));
|
||||
Testpkg.ConcreteDupper cdup = new Testpkg.ConcreteDupper.Stub(){
|
||||
@Override public Testpkg.Concrete CDup(Testpkg.Concrete c) {
|
||||
return c;
|
||||
}
|
||||
};
|
||||
assertTrue("Go struct passed through Java should not be wrapped", Testpkg.CallCDupper(cdup));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user