Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static executable v2 rebase against sbcl master #2

Open
wants to merge 4,023 commits into
base: master
Choose a base branch
from

Conversation

tgbugs
Copy link

@tgbugs tgbugs commented Jul 21, 2022

There were a few changes that prevented a straight forward rebase, but everything seems to be in order.

@tgbugs
Copy link
Author

tgbugs commented Jul 21, 2022

There is one commit that is slightly different for the 2.2.6 release, so I did a separate rebase for that one. https://github.com/tgbugs/sbcl/tree/static-executable-v2-2.2.6

@tgbugs
Copy link
Author

tgbugs commented Jul 21, 2022

Looks like there are some new issues that don't seem to appear in the 2.2.0 version. I don't a full understanding, but the missing symbols seem to be from the sbcl core and libc?

user@afb906682705 ~/sbcl $ /tmp/static-sbcl --core /tmp/sb-gmp-tester.core \
                 --non-interactive \
                 --no-sysinit --no-userinit \
                 --eval '(sb-ext:save-lisp-and-die "/tmp/sb-gmp-tester" :executable t :toplevel (lambda () (uiop:load-from-string *sb-gmp-tests*) (sb-rt:do-tests) (exit)) :compression t)'

This is SBCL 2.2.6, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
Missing required foreign symbol 'alloc_code_object'
Missing required foreign symbol 'allocation_tracker_sized'
Missing required foreign symbol 'allocation_tracker_counted'
Missing required foreign symbol 'make_list'
Missing required foreign symbol 'listify_rest_arg'
Missing required foreign symbol 'alloc_list'
Missing required foreign symbol 'alloc'
Missing required foreign symbol 'debug_print'
Missing required foreign symbol 'malloc'
Missing required foreign symbol 'free'
Missing required foreign symbol 'memmove'
Missing required foreign symbol 'os_get_errno'
Missing required foreign symbol 'strerror'
Missing required foreign symbol 'arch_write_linkage_table_entry'
Missing required foreign symbol 'undefined_alien_address'
Missing required foreign symbol 'lisp_linkage_table_n_prelinked'
Missing required foreign symbol 'dladdr'
Missing required foreign symbol 'getenv'
Missing required foreign symbol 'rename'
Missing required foreign symbol 'open'
Missing required foreign symbol 'close'
Missing required foreign symbol 'sb_mkstemp'
Missing required foreign symbol 'access'
Missing required foreign symbol 'isatty'
Missing required foreign symbol 'lseek_largefile'
Missing required foreign symbol 'read'
Missing required foreign symbol 'write'
Missing required foreign symbol 'pipe'
Missing required foreign symbol 'mkdir'
Missing required foreign symbol 'getcwd'
Missing required foreign symbol 'dup'
Missing required foreign symbol '_exit'
Missing required foreign symbol 'exit'
Missing required foreign symbol 'getpid'
Missing required foreign symbol 'getuid'
Missing required foreign symbol 'uid_username'
Missing required foreign symbol 'uid_homedir'
Missing required foreign symbol 'user_homedir'
Missing required foreign symbol 'wrapped_readlink'
Missing required foreign symbol 'sb_realpath'
Missing required foreign symbol 'unlink'
Missing required foreign symbol 'gethostname'
Missing required foreign symbol 'setsid'
Missing required foreign symbol 'ioctl'
Missing required foreign symbol 'getrusage'
Missing required foreign symbol 'poll'
Missing required foreign symbol 'select'
Missing required foreign symbol 'stat_wrapper'
Missing required foreign symbol 'lstat_wrapper'
Missing required foreign symbol 'fstat_wrapper'
Missing required foreign symbol 'get_timezone'
Missing required foreign symbol 'sb_nanosleep'
Missing required foreign symbol 'sb_nanosleep_double'
Missing required foreign symbol 'sb_nanosleep_float'
Missing required foreign symbol 'getitimer'
Missing required foreign symbol 'setitimer'
Missing required foreign symbol 'clock_gettime'
Missing required foreign symbol 'gettimeofday'
Missing required foreign symbol 'lisp_init_time'
Missing required foreign symbol 'sb_opendir'
Missing required foreign symbol 'sb_readdir'
Missing required foreign symbol 'sb_closedir'
Missing required foreign symbol 'sb_dirent_name'
Missing required foreign symbol 'lisp_package_vector'
Missing required foreign symbol 'memcpy'
Missing required foreign symbol 'sin'
Missing required foreign symbol 'cos'
Missing required foreign symbol 'tan'
Missing required foreign symbol 'atan'
Missing required foreign symbol 'atan2'
Missing required foreign symbol 'acos'
Missing required foreign symbol 'asin'
Missing required foreign symbol 'cosh'
Missing required foreign symbol 'sinh'
Missing required foreign symbol 'tanh'
Missing required foreign symbol 'asinh'
Missing required foreign symbol 'acosh'
Missing required foreign symbol 'atanh'
Missing required foreign symbol 'hypot'
Missing required foreign symbol 'exp'
Missing required foreign symbol 'log'
Missing required foreign symbol 'log10'
Missing required foreign symbol 'pow'
Missing required foreign symbol 'log1p'
Missing required foreign symbol 'sbcl_runtime'
Missing required foreign symbol 'posix_argv'
Missing required foreign symbol 'core_string'
Missing required foreign symbol 'dlopen'
Missing required foreign symbol 'dlclose'
Missing required foreign symbol 'dlerror'
Missing required foreign symbol 'dlsym'
Missing required foreign symbol 'callback_wrapper_trampoline'
Missing required foreign symbol 'os_context_pc'
Missing required foreign symbol 'set_os_context_pc'
Missing required foreign symbol 'os_context_register_addr'
Missing required foreign symbol 'os_context_flags_addr'
Missing required foreign symbol 'os_context_float_register_addr'
Missing required foreign symbol 'os_context_fp_control'
Missing required foreign symbol 'arch_get_fp_modes'
Missing required foreign symbol 'arch_set_fp_modes'
Missing required foreign symbol 'search_all_gc_spaces'
Missing required foreign symbol 'memcmp'
Missing required foreign symbol 'unblock_deferrable_signals'
Missing required foreign symbol 'block_deferrable_signals'
Missing required foreign symbol 'pthread_sigmask'
Missing required foreign symbol 'blockable_sigset'
Missing required foreign symbol 'kill'
Missing required foreign symbol 'killpg'
Missing required foreign symbol 'unblock_gc_signals'
Missing required foreign symbol 'install_handler'
Missing required foreign symbol 'install_sig_memory_fault_handler'
Missing required foreign symbol 'siginfo_code'
Missing required foreign symbol 'gc_card_table_nbits'
Missing required foreign symbol 'alloc_profiling'
Missing required foreign symbol 'os_allocate'
Missing required foreign symbol 'os_deallocate'
Missing required foreign symbol 'atomic_bump_static_space_free_ptr'
Missing required foreign symbol 'find_preceding_object'
Missing required foreign symbol 'VARYOBJ_SPACE_START'
Missing required foreign symbol 'varyobj_pages'
Missing required foreign symbol 'varyobj_free_pointer'
Missing required foreign symbol 'varyobj_holes'
Missing required foreign symbol 'memset'
Missing required foreign symbol 'varyobj_space_size'
Missing required foreign symbol 'alloc_immobile_fixedobj'
Missing required foreign symbol 'FIXEDOBJ_SPACE_START'
Missing required foreign symbol 'finalizer_thread_runflag'
Missing required foreign symbol 'finalizer_thread_wake'
Missing required foreign symbol 'finalizer_thread_wait'
Missing required foreign symbol 'finalizer_thread_stop'
Missing required foreign symbol 'rmdir'
Missing required foreign symbol 'sbcl_runtime_home'
Missing required foreign symbol 'DYNAMIC_SPACE_START'
Missing required foreign symbol 'next_free_page'
Missing required foreign symbol 'bytes_allocated'
Missing required foreign symbol 'static_space_free_pointer'
Missing required foreign symbol 'read_only_space_free_pointer'
Missing required foreign symbol 'primitive_object_size'
Missing required foreign symbol 'collect_garbage'
Missing required foreign symbol 'gc_stop_the_world'
Missing required foreign symbol 'gc_start_the_world'
Missing required foreign symbol 'dynamic_space_size'
Missing required foreign symbol 'release_gc_lock'
Missing required foreign symbol 'try_acquire_gc_lock'
Missing required foreign symbol 'empty_thread_recyclebin'
Missing required foreign symbol 'scrub_control_stack'
Missing required foreign symbol 'bytes_consed_between_gcs'
Missing required foreign symbol 'gc_logfile'
Missing required foreign symbol 'ext_find_page_index'
Missing required foreign symbol 'page_table'
Missing required foreign symbol 'gc_card_table_mask'
Missing required foreign symbol 'gc_card_mark'
Missing required foreign symbol 'generations'
Missing required foreign symbol 'generation_average_age'
Missing required foreign symbol 'futex_wake'
Missing required foreign symbol 'futex_wait'
Missing required foreign symbol 'pthread_create'
Missing required foreign symbol 'pthread_attr_setstack'
Missing required foreign symbol 'thread_control_stack_size'
Missing required foreign symbol 'new_thread_trampoline'
Missing required foreign symbol 'new_lisp_thread_attr'
Missing required foreign symbol 'free_thread_struct'
Missing required foreign symbol 'pthread_join'
Missing required foreign symbol 'protect_alien_stack_guard_page'
Missing required foreign symbol 'protect_binding_stack_guard_page'
Missing required foreign symbol 'protect_control_stack_guard_page'
Missing required foreign symbol 'alloc_thread_struct'
Missing required foreign symbol 'thread_start_sigset'
Missing required foreign symbol 'sigaddset'
Missing required foreign symbol 'raise'
Missing required foreign symbol 'pthread_kill'
Missing required foreign symbol 'thread_yield'
Missing required foreign symbol 'valid_lisp_pointer_p'
Missing required foreign symbol 'component_ptr_from_pc'
Missing required foreign symbol 'dynamic_values_bytes'
Missing required foreign symbol 'breakpoint_install'
Missing required foreign symbol 'breakpoint_remove'
Missing required foreign symbol 'breakpoint_do_displaced_inst'
Missing required foreign symbol 'disable_lossage_handler'
Missing required foreign symbol 'enable_lossage_handler'
Missing required foreign symbol 'lisp_startup_options'
Missing required foreign symbol 'internal_errors_enabled'
Missing required foreign symbol 'avx2_supported'
CORRUPTION WARNING in SBCL pid 26 tid 26:
Memory fault at 0x1020 (pc=0x50000060, fp=0x7f5ef53a7cd0, sp=0x7f5ef53a7928) tid 26
The integrity of this image is possibly compromised.
Continuing with fingers crossed.
CORRUPTION WARNING in SBCL pid 26 tid 26:
Memory fault at 0 (pc=0x52bc8d4e [code 0x52bc8cd0+0x7E ID 0x4142], fp=0x7f5ef53a71c0, sp=0x7f5ef53a71c0) tid 26
The integrity of this image is possibly compromised.
Continuing with fingers crossed.
CORRUPTION WARNING in SBCL pid 26 tid 26:
Memory fault at 0 (pc=0x52bc8d4e [code 0x52bc8cd0+0x7E ID 0x4142], fp=0x7f5ef53a6a40, sp=0x7f5ef53a6a40) tid 26
The integrity of this image is possibly compromised.
Continuing with fingers crossed.
CORRUPTION WARNING in SBCL pid 26 tid 26:
Memory fault at 0 (pc=0x52bc8d4e [code 0x52bc8cd0+0x7E ID 0x4142], fp=0x7f5ef53a62c0, sp=0x7f5ef53a62c0) tid 26
The integrity of this image is possibly compromised.
Continuing with fingers crossed.
CORRUPTION WARNING in SBCL pid 26 tid 26:
Memory fault at 0 (pc=0x52bc8d4e [code 0x52bc8cd0+0x7E ID 0x4142], fp=0x7f5ef53a5b40, sp=0x7f5ef53a5b40) tid 26
The integrity of this image is possibly compromised.
Continuing with fingers crossed.
CORRUPTION WARNING in SBCL pid 26 tid 26:
Memory fault at 0 (pc=0x52bc8d4e [code 0x52bc8cd0+0x7E ID 0x4142], fp=0x7f5ef53a53c0, sp=0x7f5ef53a53c0) tid 26
The integrity of this image is possibly compromised.
Continuing with fingers crossed.
CORRUPTION WARNING in SBCL pid 26 tid 26:
Memory fault at 0 (pc=0x52bc8d4e [code 0x52bc8cd0+0x7E ID 0x4142], fp=0x7f5ef53a4c40, sp=0x7f5ef53a4c40) tid 26
The integrity of this image is possibly compromised.
Continuing with fingers crossed.
CORRUPTION WARNING in SBCL pid 26 tid 26:
Memory fault at 0 (pc=0x52bc8d4e [code 0x52bc8cd0+0x7E ID 0x4142], fp=0x7f5ef53a44c0, sp=0x7f5ef53a44c0) tid 26
The integrity of this image is possibly compromised.
Continuing with fingers crossed.
fatal error encountered in SBCL pid 26 tid 26:
maximum interrupt nesting depth (8) exceeded

Welcome to LDB, a low-level debugger for the Lisp runtime environment.
ldb> 

snuglas and others added 28 commits August 1, 2022 20:55
Possibly avoids dynamic space relocation on FreeBSD.
It seems to add a random number to any addresss you give it,
which means that R/O always collided with dynamic.
Don't scan inherited symbols. This was about 4x faster in a system
having 1000 packages, each using on average 10 other packages,
and where many distinct symbols are spelled the same.
Do not assume that 'cc' is executed in src/runtime. In particular,
allow the root of SBCL's source tree to be one or more levels down
from where the compiler gets invoked. This places different demands
on where autogenerated headers are written to by 1st genesis and
where they are read from by make-target-1 (or equivalent).

And non-system includes should not use <> syntax (in main.c)
It doesn't work well on instance types, producing wider types.
This is a modified version of Douglas Katzman's commit 869f2ac
(Remove non-pauseless-threadstart code), which gets the
pauseless-threadstart feature to work on Windows.  Since Windows is the
last outstanding platform that builds with pauseless-threadstart
disabled by default, this commit also removes the pauseless-threadstart
feature flag entirely.

The original commit was modified so that it applies to 2.2.7 as well as
to remove the setup semaphore and to set th->no_tls_value_marker to
NO_TLS_VALUE_MARKER in alloc_thread_struct.

Note: The original commit was reverted by 15f2ccc (Revert "Remove
non-pauseless-threadstart code") due to failing tests.
I caused it in rev 3395391 by outdenting the WHEN without the
TYPECASE. They were both wrongly indented but I made it worse.
* a page with no object headers should have 0 generation mask, both
  in coreparse and after sweeping
* correctly zero-fill after the object offset array when saving
* and brothertree.h was sensitive to inclusion order
While we're at it, remove purify.c as irrelevant.
Slightly related- alphabetize the #include lines.
The wired address was just for debugging.
TLSF isn't used, but eventually I'd like all the 64-bit architectures
to have immobile-space. That time is not now.
Rev 0e3d1d6 which fixed the build failure reported in
https://groups.google.com/g/sbcl-devel/c/1oraPq21KnA/m/QT42XSLiCQAJ
for ppc64 caused a crash for ppc32 (and other 32-bit configs with LRAs
and threads - but there aren't any)

I couldn't figure out why it kept working under cross-make for me,
but banging my head against the desk sufficiently many times resulted
in my recalling that my native-on-native ppc32 build had #+sb-thread.
Lots of tests fail for not-too-scary reasons such as compiler notes,
CLOCK-GETTIME not being a thing, too much consing in assert-no-consing,
readlink not working the same as we expect (wtf?) etc, etc.
Probably the most interesting one was "unknown SC to SC-CASE"
from foreign.test.sh which revealed that SINGLE-FLOAT-BITS doesn't
cover all the cases allegedly permitted for its args/results.

But without this patch, half the suite failed with C runtime lossage
in all sorts of colorful ways. So ... just I can't even.
And rob 1 bit of that, so it's effectively 15 bits, not 16.
Then free up 2 more bits of 'type' for other use as I tried to do
in rev 938320b which was reverted. Taking a different bit in the
PTE should be fine, and the resulting code looks better for it.
When the number of more arguments overflow local-tn-limit they are
assigned the same number. Avoid doing that twice for a duplicate TN.

Reported by Qian Yun.
I don't know why rev 3bdd4d2 took out the lines
    (unless (cold-null (cold-fdefn-fun fdefn))
      (error "Duplicate DEFUN for ~S" warm-name))
from COLD-FSET, but it did, and of course in the interim there
was backsliding such that there is now a duplicate.

And lexical vars named DEFN and FDEFN are too confusing to
co-exist in the same function, so fix that.
snuglas and others added 7 commits September 16, 2022 12:02
On #+unix it's always and nothing but munmap().
os_deallocate() is a better name, so just make the obvious alterations.
which failed to compile ever since rev b8ff0a4 ("pass a GC space id")
but became more bitrotted with the recent rename of T to LISP_T.
Too many C warnings in that janky logic to know what's going on anyway.
Without a shred of documentation in sight, I'm guessing that this
is what's needed to fix the problem reported in
https://sourceforge.net/p/sbcl/mailman/message/37703368/

Not gonna try this patch on the other architecture/OS combinations
that seem to lack the proper incantation, so we'll at least know if
we're fixing bugs in a vacuum (based on complaints or lack of).
and I don't know what "alignment passed from os_validate" means,
so rewrite that comment block.
Nobody I surveyed guessed correctly that "validate" meant "give me memory"
@tgbugs tgbugs force-pushed the static-executable-v2 branch 2 times, most recently from 7525ae4 to 7729ff3 Compare September 17, 2022 16:26
daewok and others added 16 commits September 17, 2022 13:21
Add a new C function - arch_read_linkage_entry - that backs out the true
foreign address of the corresponding symbol from the data in the linkage table.

Add a Lisp function - FIND-LINKAGE-TABLE-FOREIGN-SYMBOL-ADDRESS - that uses
this new C function to return the address, while accounting for undefined
symbols (so as to not accidentally return a reference to a guard page).
If we can't get a runtime handle, we can't be sure that
FIND-DYNAMIC-FOREIGN-SYMBOL-ADDRESS will work, so teach
FIND-FOREIGN-SYMBOL-ADDRESS to look up symbols via dlsym or by backing out the
address from the linkage table. Replace most calls to
FIND-DYNAMIC-FOREIGN-SYMBOL-ADDRESS with FIND-FOREIGN-SYMBOL-ADDRESS.

This gets us a step closer to having #-os-provides-dlopen work.

Additionally, this lets us create a static executable with musl libc. Musl's
libdl functions all return an error when called within a static executable. At
the moment, the only way to take advantage of this is shrinkwrapping, as a
working libdl is required to build a working Lisp core from scratch.
Additionally, remove the comment fro the comma variable definition. On at least
GNU make 4.3, the value is set to ", ", not ",".
Move the linkage table filling for shrinkwrapped executables into its own
function.

With changes to account for 851cbd2, 8968446, 27e216a, 5a2e639
Patch originally by Manuel Giraud <[email protected]>, I just cleaned up
some comments.

Also preemtively remove after-grovel-headers since upstream removed it
and it will make the rebase easier.
This is an internal feature. When present, genesis writes a C helper
file (linkage-table-prelink-info.c) that populates the C var
lisp_linkage_values. This variable (originally introduced as part of the
shrinkwrapping support) contains the true addresses of each variable and
function in the linkage table.

Then, when the runtime boots instead of using os_dlsym_default and
required-foreign-symbols to fill the linkage table, the data in
lisp_linkage_values is used instead. This has the affect of using the system
linker to prelink the entire linkage table instead of doing it ourselves.

Last, when the core boots, it has to fixup the prelinked entries. This is
required to make sure the linkage table points to the appropriate guards for
undefined foreign variables or functions. This cannot be done in the runtime
because some on some archs the undefined alien function guard is an assembly
routine generated in lisp. Instead, a NULL entry in lisp_linkage_values
indicates an undefined entry.

A useful feature for some use cases is to add extra symbols to the linkage
table at build time that we know we'll need at run time. This is accomplished
by passing the extra entries to the make process using the
--extra-linkage-table-entries argument.

The file must contain a single list. Each element is a linkage table entry
consisting of three elements. The first is a string naming the C symbol. The
second is T if the symbol is a variable, NIL if it is a function. The third is
T if the entry is undefined (this is currently pointless, but will become
useful in a future commit).

Enabling this feature on Windows causes a test failure. The root cause is that
mingw-w64 implements its trig functions as thin wrappers around fsin and
friends. This can be wildly inaccurate. When this feature is enabled, the trig
functions come from wingw-w64's libm. When this feature is disabled, the more
accurate trig functions from the Windows C runtime are used. See
https://sourceforge.net/p/mingw-w64/mailman/mingw-w64-public/thread/6141957e.8c6be.157053d0e19.Coremail.lh_mouse%40126.com/#msg35353216
and https://mingwpy.github.io/issues.html#math-precision-issues for more
details.

With changes to compensate for 851cbd2, 861a55d
-os-provides-dlopen and -sb-prelink-linkage-table is incompatible.

In order to build a working core with -os-provides-dlopen you must provide the
list of all C symbols introduced in make-target-2.

With changes to compensate for 851cbd2
git is needed to generate the version file.
the file was renamed at some point and the easiest way to handle the
rebase was to put those changes at the end
@tgbugs tgbugs force-pushed the static-executable-v2 branch from 7729ff3 to 4d2d54c Compare September 17, 2022 17:25
these changes prevent obvious compilation errors, however there are a
number of changes since 2.2.8 that cause a failure dropping into ldb
during compile, this like almost certainly because I missed something
when doing the rebase and because there are other compensatory changes
that have to be made to the original static linking code
@tgbugs
Copy link
Author

tgbugs commented Sep 17, 2022

I was able to rebase without compile failures up to 2.2.8 https://github.com/tgbugs/sbcl/tree/static-executable-v2-2.2.8 (though the runtime issue mentioned above is still present). However, beyond 2.2.8 there have been substantial changes made in renaming various functions, in coreparse, and in interaction with immobile space (see e.g. 851cbd2, 8968446, 27e216a, 5a2e639). As a result this branch now hits a memory fault during compilation.

@daewok do you have any bandwidth/interest to check in on this?

@paulapatience
Copy link

I was going to open an issue about my own progress rebasing the static-executable changes to the latest SBCL, but then I saw this PR. My own efforts, done independently of this PR, are available here: https://git.sr.ht/~paulapatience/sbcl. In particular, at least from what I can read from the commit titles here, I've updated the static-executable commits inline rather than adding commits at the end.

Like you, I have been able to build up to version 2.2.8 (I started directly at 2.2.6, which contained a change required to build in containers), after which I encountered an issue from 2.2.9 to 2.3.10, and then a different issue from 2.3.11.

@paulapatience
Copy link

In the end I was able to fix all compilation issues. Of note is that an updated version of asdf-release-ops is required (for those using it), available at https://git.sr.ht/~paulapatience/asdf-release-ops (branch latest-sbcl), for SBCL 2.2.9 and later, because the linkage tables were renamed. Also, starting from 2.3.10, compiling the standalone ldb has to be disabled when compiling with sb-prelink-linkage-table. In 2.3.11, the os_link_from_pointer_table commit becomes unnecessary, but I am not completely certain that my adjustment of the sb-prelink-linkage-table commit is correct. However, I am able to compile and run a statically linked program built with asdf-release-ops for every release from 2.2.6 until 2.4.4, and even until today's latest commit on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants