While you want the string type to eventually not use mutable struct, since they live on the heap (I mean the struct it self, why you can get a pointer to it, plus the other heap allocation for heap_string), though I (also) try it for an experiment:
julia> struct mystring
prefix::UInt64
heap_string::String
end
julia> mutable struct mystring2
prefix::UInt64
heap_string::String
end
julia> reinterpret(UInt64, str2.heap_string) # you have access to prefix, it's isbbits but not the rest of the struct or it as a whole
ERROR: ArgumentError: Source type for `reinterpret` must be isbits
julia> str = mystring2(0, "Palli"); p = pointer_from_objref(str)
Ptr{Nothing} @0x00007f0eea022630
julia> p2 = pointer_from_objref(mystring(0, "Palli"))
ERROR: pointer_from_objref cannot be used on immutable objects
for good reasons, but I can bypass (not in general):
julia> ccall(:jl_value_ptr, Ptr{Cvoid}, (Any,), mystring(0, "Palli"))
Ptr{Nothing} @0x00007f0ecba97e90
julia> p[1] # EDIT: solved below, was: I'm not sure how to deref the pointer, but by fixing I should be about to access the prefix, or its bytes, and the pointer with something similar to p[2]?
ERROR: MethodError: no method matching getindex(::Ptr{Nothing}, ::Int64)
julia> unsafe_store!(reinterpret(Ptr{UInt64}, p), 123)
Ptr{UInt64} @0x00007fb02b427c70
julia> str # so far so good I can mess with the prefix:
mystring2(0x000000000000007b, "Palli")
julia> unsafe_store!(reinterpret(Ptr{UInt64}, p+8), typemax(UInt64))
Ptr{UInt64} @0x00007fb02b427c78
julia> str # predictably, and what I actually aimed for:
mystring2(0x000000000000007b,
[1200858] signal (11.1): Segmentation fault
...
Another way to crash it sooner:
julia> GC.gc()
[1201791] signal (11.1): Segmentation fault
gc_mark_obj8 at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gc.c:1862
gc_mark_outrefs at /cache/build/builder-amdci4-4/julialang/julia-release-1-dot-10/src/gc.c:2641 [inlined]
..
[That file is gone in 1.12, so I’ll look into “fixing”/make work in 1.12, and it may never work for 1.10 LTS too. Or 1.11, though simple to backport to either I guess, not a priority to look into, I think the function simply moved.]
In 1.12 it’s the same (or similar looking) gc_mark_obj8 function, just moved to a different file, but actually in a line a bit lower:
[1204152] signal 11 (1): Segmentation fault
in expression starting at REPL[4]:1
gc_mark_obj8 at /cache/build/builder-amdci4-5/julialang/julia-master/src/gc-stock.c:1566 [inlined]
gc_mark_outrefs at /cache/build/builder-amdci4-5/julialang/julia-master/src/gc-stock.c:2327 [inlined]
I want to mess with the pointer, but also reliably for an immutable struct, to get a segfault, so I can fix the GC to ignore illegal pointers.