Skip to content

Commit 2ff47e6

Browse files
committed
Handle ForeingTypes in staticdata.c
1 parent 68fc828 commit 2ff47e6

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

src/datatype.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -832,15 +832,13 @@ JL_DLLEXPORT int jl_reinit_foreign_type(jl_datatype_t *dt,
832832
{
833833
if (!jl_is_foreign_type(dt))
834834
return 0;
835-
jl_datatype_layout_t *layout = (jl_datatype_layout_t *)
836-
jl_gc_perm_alloc(sizeof(jl_datatype_layout_t) + sizeof(jl_fielddescdyn_t),
837-
0, 4, 0);
838-
*layout = *(dt->layout);
835+
const jl_datatype_layout_t *layout = dt->layout;
839836
jl_fielddescdyn_t * desc =
840837
(jl_fielddescdyn_t *) ((char *)layout + sizeof(*layout));
838+
assert(!desc->markfunc);
839+
assert(!desc->sweepfunc);
841840
desc->markfunc = markfunc;
842841
desc->sweepfunc = sweepfunc;
843-
dt->layout = layout;
844842
return 1;
845843
}
846844

src/staticdata.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ External links:
7777

7878
#include "julia.h"
7979
#include "julia_internal.h"
80+
#include "julia_gcext.h"
8081
#include "builtin_proto.h"
8182
#include "processor.h"
8283
#include "serialize.h"
@@ -1438,10 +1439,14 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED
14381439
if (dt->layout != NULL) {
14391440
size_t nf = dt->layout->nfields;
14401441
size_t np = dt->layout->npointers;
1441-
size_t fieldsize = jl_fielddesc_size(dt->layout->fielddesc_type);
1442+
size_t fieldsize = 0;
1443+
uint8_t is_foreign_type = dt->layout->fielddesc_type == 3;
1444+
if (!is_foreign_type) {
1445+
fieldsize = jl_fielddesc_size(dt->layout->fielddesc_type);
1446+
}
14421447
char *flddesc = (char*)dt->layout;
14431448
size_t fldsize = sizeof(jl_datatype_layout_t) + nf * fieldsize;
1444-
if (dt->layout->first_ptr != -1)
1449+
if (!is_foreign_type && dt->layout->first_ptr != -1)
14451450
fldsize += np << dt->layout->fielddesc_type;
14461451
uintptr_t layout = LLT_ALIGN(ios_pos(s->const_data), sizeof(void*));
14471452
write_padding(s->const_data, layout - ios_pos(s->const_data)); // realign stream
@@ -1450,6 +1455,13 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED
14501455
arraylist_push(&s->relocs_list, (void*)(reloc_offset + offsetof(jl_datatype_t, layout))); // relocation location
14511456
arraylist_push(&s->relocs_list, (void*)(((uintptr_t)ConstDataRef << RELOC_TAG_OFFSET) + layout)); // relocation target
14521457
ios_write(s->const_data, flddesc, fldsize);
1458+
if (is_foreign_type) {
1459+
// make sure we have space for the extra hidden pointers
1460+
// zero them since they will need to be re-initialized externally
1461+
assert(fldsize == sizeof(jl_datatype_layout_t));
1462+
jl_fielddescdyn_t dyn = {0, 0};
1463+
ios_write(s->const_data, (char*)&dyn, sizeof(jl_fielddescdyn_t));
1464+
}
14531465
}
14541466
}
14551467
else if (jl_is_typename(v)) {
@@ -2912,6 +2924,7 @@ static void jl_restore_system_image_from_stream_(ios_t *f, jl_image_t *image, jl
29122924
assert(*(void**)dt == (void*)newdt);
29132925
*newdt = *(jl_datatype_t*)dt; // copy the datatype fields (except field 1, which we corrupt above)
29142926
newdt->name = name;
2927+
assert(newdt->layout->fielddesc_type != 3);
29152928
}
29162929
}
29172930
// we should never see these pointers again, so scramble their memory, so any attempt to look at them crashes

test/gcext/Foreign/deps/foreignlib.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "julia.h"
44
#include "julia_gcext.h"
55

6+
// TODO make these atomics
67
int nmarks = 0;
78
int nsweeps = 0;
89

test/gcext/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ check: $(BIN)/gcext$(EXE) $(BIN)/LocalTest.jl $(BIN)/Foreign/deps/foreignlib$(DY
6060

6161
clean:
6262
-rm -f $(BIN)/gcext-debug$(EXE) $(BIN)/gcext$(EXE)
63+
-rm -f $(BIN)/Foreign/deps/foreignlib$(DYLIB)
64+
-rm -f $(BIN)/Foreign/deps/foreignlib-debug$(DYLIB)
6365

6466
.PHONY: release debug clean check
6567

test/gcext/gcext-test.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ end
4545
@testset "Package with foreign type" begin
4646
load_path = joinpath(@__DIR__, "Foreign")
4747
push!(LOAD_PATH, load_path)
48+
# Force recaching
49+
Base.compilecache(Base.identify_package("Foreign"))
4850
try
4951
(@eval (using Foreign))
5052
@test Base.invokelatest(Foreign.get_nmark) == 0

0 commit comments

Comments
 (0)