Skip to content

Commit

Permalink
Handle ForeingTypes in staticdata.c
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Nov 25, 2022
1 parent 68fc828 commit 2ff47e6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/datatype.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,15 +832,13 @@ JL_DLLEXPORT int jl_reinit_foreign_type(jl_datatype_t *dt,
{
if (!jl_is_foreign_type(dt))
return 0;
jl_datatype_layout_t *layout = (jl_datatype_layout_t *)
jl_gc_perm_alloc(sizeof(jl_datatype_layout_t) + sizeof(jl_fielddescdyn_t),
0, 4, 0);
*layout = *(dt->layout);
const jl_datatype_layout_t *layout = dt->layout;
jl_fielddescdyn_t * desc =
(jl_fielddescdyn_t *) ((char *)layout + sizeof(*layout));
assert(!desc->markfunc);
assert(!desc->sweepfunc);
desc->markfunc = markfunc;
desc->sweepfunc = sweepfunc;
dt->layout = layout;
return 1;
}

Expand Down
17 changes: 15 additions & 2 deletions src/staticdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ External links:

#include "julia.h"
#include "julia_internal.h"
#include "julia_gcext.h"
#include "builtin_proto.h"
#include "processor.h"
#include "serialize.h"
Expand Down Expand Up @@ -1438,10 +1439,14 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED
if (dt->layout != NULL) {
size_t nf = dt->layout->nfields;
size_t np = dt->layout->npointers;
size_t fieldsize = jl_fielddesc_size(dt->layout->fielddesc_type);
size_t fieldsize = 0;
uint8_t is_foreign_type = dt->layout->fielddesc_type == 3;
if (!is_foreign_type) {
fieldsize = jl_fielddesc_size(dt->layout->fielddesc_type);
}
char *flddesc = (char*)dt->layout;
size_t fldsize = sizeof(jl_datatype_layout_t) + nf * fieldsize;
if (dt->layout->first_ptr != -1)
if (!is_foreign_type && dt->layout->first_ptr != -1)
fldsize += np << dt->layout->fielddesc_type;
uintptr_t layout = LLT_ALIGN(ios_pos(s->const_data), sizeof(void*));
write_padding(s->const_data, layout - ios_pos(s->const_data)); // realign stream
Expand All @@ -1450,6 +1455,13 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED
arraylist_push(&s->relocs_list, (void*)(reloc_offset + offsetof(jl_datatype_t, layout))); // relocation location
arraylist_push(&s->relocs_list, (void*)(((uintptr_t)ConstDataRef << RELOC_TAG_OFFSET) + layout)); // relocation target
ios_write(s->const_data, flddesc, fldsize);
if (is_foreign_type) {
// make sure we have space for the extra hidden pointers
// zero them since they will need to be re-initialized externally
assert(fldsize == sizeof(jl_datatype_layout_t));
jl_fielddescdyn_t dyn = {0, 0};
ios_write(s->const_data, (char*)&dyn, sizeof(jl_fielddescdyn_t));
}
}
}
else if (jl_is_typename(v)) {
Expand Down Expand Up @@ -2912,6 +2924,7 @@ static void jl_restore_system_image_from_stream_(ios_t *f, jl_image_t *image, jl
assert(*(void**)dt == (void*)newdt);
*newdt = *(jl_datatype_t*)dt; // copy the datatype fields (except field 1, which we corrupt above)
newdt->name = name;
assert(newdt->layout->fielddesc_type != 3);
}
}
// we should never see these pointers again, so scramble their memory, so any attempt to look at them crashes
Expand Down
1 change: 1 addition & 0 deletions test/gcext/Foreign/deps/foreignlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "julia.h"
#include "julia_gcext.h"

// TODO make these atomics
int nmarks = 0;
int nsweeps = 0;

Expand Down
2 changes: 2 additions & 0 deletions test/gcext/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ check: $(BIN)/gcext$(EXE) $(BIN)/LocalTest.jl $(BIN)/Foreign/deps/foreignlib$(DY

clean:
-rm -f $(BIN)/gcext-debug$(EXE) $(BIN)/gcext$(EXE)
-rm -f $(BIN)/Foreign/deps/foreignlib$(DYLIB)
-rm -f $(BIN)/Foreign/deps/foreignlib-debug$(DYLIB)

.PHONY: release debug clean check

Expand Down
2 changes: 2 additions & 0 deletions test/gcext/gcext-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ end
@testset "Package with foreign type" begin
load_path = joinpath(@__DIR__, "Foreign")
push!(LOAD_PATH, load_path)
# Force recaching
Base.compilecache(Base.identify_package("Foreign"))
try
(@eval (using Foreign))
@test Base.invokelatest(Foreign.get_nmark) == 0
Expand Down

0 comments on commit 2ff47e6

Please sign in to comment.