Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*.yml pony
*.sh pony
*.bash pony
*.ld pony

*.bat win
*.diff pony
Expand Down
50 changes: 50 additions & 0 deletions .release-notes/next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -2228,3 +2228,53 @@ A match with one case that is a sequence ending in `true` and another that is a

We've updated the LLVM version used to build Pony from 23.1.3 to 23.1.7.

## Fix GC freeing the other fields of a struct whose first field is embedded

A Pony `struct` has no object header. When the first field of a struct is embedded, the struct and that field have the same address.

A program can hold a reference to that embedded field apart from the struct. Before this fix, the runtime could then trace that address as the type of the field and never trace it as the struct. The runtime did not trace the other fields of the struct. The runtime freed the memory of those fields and reused it while the struct still pointed at them. This happened during garbage collection of an actor's heap and during the trace of a message that an actor sends.

For example, struct `S` embeds a `C` in its first field `c` and has a second field `d`. An actor holds `_s.c` in its field `_c`, and the actor declares `_c` before its field `_s`. Before this fix, reads of `_s.d.n` could stop returning the value that `_s.d.n` was created with. Whether the reads returned a wrong value depended on when garbage collection ran, so the wrong values did not occur in every run of the program.

Now the other fields of the struct stay alive while the struct is reachable.

## Fix tracing that never finishes when a reference cycle runs through embedded objects

Embedded objects can form a reference cycle. Before this fix, the runtime's trace went around that cycle again and again and never finished. This happened during garbage collection and during the trace of a message that an actor sends. The simplest case is an embedded object that references itself.

Three forms of the cycle caused the hang:

- An embedded field inside a class object on the Pony heap, during garbage collection. This includes a cycle through two such objects.
- An embedded field of an actor, during garbage collection.
- An embedded `val` field of an actor, when the actor sends that field.

For example, class `A` embeds a `C` in its field `c`, and `C` has a field `self_ref`. The program sets `a.c.self_ref` to `a.c` and keeps only `a.c`. Before this fix, that program hung. Now the trace finishes.

## Fix an actor being collected while another actor holds its embedded field

An actor can send an embedded `val` field to another actor and then have no more work to do. Before this fix, the runtime could collect the sending actor while the other actor still held the field. The runtime could then reuse the memory of the sending actor for actors that the program created later.

Now the sending actor stays alive while another actor holds its embedded field.

## Fix GC freeing a struct's fields when a Pointer points at its start

To get this bug, your program must make a `Pointer` through FFI. If your program uses no FFI, this bug does not affect it.

A class can hold a `Pointer` to the start of a struct and also hold the struct itself, with the `Pointer` field first. Before this fix, the runtime could free the fields of the struct while they were still reachable. This happened in two cases:

- During garbage collection, the runtime freed a field of the struct while the class still held the struct.
- When an actor sent such a class to another actor, the runtime did not count the field of the struct for the receiving actor. The sending actor then freed the field while the receiving actor still held it.

Now the fields of the struct stay alive.

## Fix the address of an embedded struct with no size

Before this fix, an embedded struct with no size got the address where it would sit in the object that holds it. For a field at the end of that object, this address is one past the end of the object. That address can be the address of a different object.

Now an embedded struct with no size has the address of the object that holds it, wherever the field is declared. Its address is always inside that object. As a result, `digestof` of the field gives the same value as `digestof` of the object that holds it, and `is` between the two gives `true`. Two embedded structs with no size in the same object are also `is` each other.

## Speed up GC for string and array literals

The compiler creates string literals, constant array literals, and primitive instances as constants. A program can never free these objects. The garbage collector now skips these objects and no longer traces them.

In one of our benchmarks, an actor holds many references to string literals and collects its garbage at the end of every behavior. With release builds and one scheduler thread, that actor now does about 4.7 times as many collections per second: 904.0 instead of 193.5. A second version of the benchmark went from 197.2 to 840.4 collections per second, about 4.3 times as many. Our other benchmarks changed by about 1–2%.
4 changes: 4 additions & 0 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,10 @@ cmake --preset release -DPONY_USES=reach_instrument
cmake --build --preset release
```

## Linker for the runtime tests

On ELF platforms, `libponyrt.tests` links with the linker script `src/libponyrt/gc/pony_sections.ld`, the same script ponyc writes when it links a program. GNU ld and lld accept it. gold doesn't support its `INSERT` command and mold doesn't support its `SECTIONS` command. With either of them as the host linker, the build links the runtime tests without the script, and the tests that check the sections it marks skip.

## Compiler Development

To ease development and support LSP tools like [clangd](https://clangd.llvm.org), create a `compile_commands.json` file with the following steps:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ All notable changes to the Pony compiler and standard library will be documented

### Fixed

- Fix GC freeing the other fields of a struct whose first field is embedded ([PR #7085](https://github.com/ponylang/ponyc/pull/7085))
- Fix tracing that never finishes when a reference cycle runs through embedded objects ([PR #7085](https://github.com/ponylang/ponyc/pull/7085))
- Fix an actor being collected while another actor holds its embedded field ([PR #7085](https://github.com/ponylang/ponyc/pull/7085))
- Fix GC freeing a struct's fields when a Pointer points at its start ([PR #7085](https://github.com/ponylang/ponyc/pull/7085))
- Fix the address of an embedded struct with no size ([PR #7085](https://github.com/ponylang/ponyc/pull/7085))
- Speed up GC for string and array literals ([PR #7085](https://github.com/ponylang/ponyc/pull/7085))
- Fix ponyc merging runtime bitcode built for another target ([PR #7021](https://github.com/ponylang/ponyc/pull/7021))
- Fix macOS programs built for a darwin target triple ([PR #7021](https://github.com/ponylang/ponyc/pull/7021))
- Fix object literals and lambdas that use consumed variables or variables with no value ([PR #7029](https://github.com/ponylang/ponyc/pull/7029))
Expand Down
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,21 @@ add_custom_target(pony_version ALL
COMMENT "Refreshing pony_version.h"
)

# The linker script ponyc writes for each ELF link, embedded as a C string.
set(PONY_SECTIONS_LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/src/libponyrt/gc/pony_sections.ld")
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
"${PONY_SECTIONS_LINKER_SCRIPT}")
file(READ "${PONY_SECTIONS_LINKER_SCRIPT}" _pony_sections_script)
string(REPLACE "\\" "\\\\" _pony_sections_script "${_pony_sections_script}")
string(REPLACE "\"" "\\\"" _pony_sections_script "${_pony_sections_script}")
string(REPLACE "\n" "\\n\"\n \"" _pony_sections_script "${_pony_sections_script}")
file(WRITE "${CMAKE_BINARY_DIR}/pony_generated/pony_sections_ld.h.tmp"
"// Generated by CMake from src/libponyrt/gc/pony_sections.ld. Do not edit.\n"
"#pragma once\n"
"static const char PONY_SECTIONS_LINKER_SCRIPT[] =\n \"${_pony_sections_script}\";\n")
configure_file("${CMAKE_BINARY_DIR}/pony_generated/pony_sections_ld.h.tmp"
"${CMAKE_BINARY_DIR}/pony_generated/pony_sections_ld.h" COPYONLY)

# Required definitions. We use these generators so that the defines are correct for both *nix (where the config applies at configuration time) and Windows (where the config applies at build time).
add_compile_definitions(
BUILD_COMPILER="${CMAKE_C_COMPILER_VERSION}"
Expand Down Expand Up @@ -954,6 +969,12 @@ else()
set_tests_properties(check-version libponyrt.tests libponyc.tests
PROPERTIES LABELS ci-core)

if(TARGET libponyrt.noscript)
add_test(NAME libponyrt.noscript
COMMAND $<TARGET_FILE:libponyrt.noscript>)
set_tests_properties(libponyrt.noscript PROPERTIES LABELS ci-core)
endif()

if(PONY_RUNTIME_BITCODE)
set(_check_bitcode_macos_args "")
if(APPLE)
Expand Down
80 changes: 77 additions & 3 deletions cmake/RunLinkHandoff.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
# info into place. At -V3, the compiler prints its process ID and its parent's
# and, for each link, whether it runs the link or hands it off; ponylink prints
# its process ID and its parent's. A handed-off link's ponylink has the
# compiler's parent (the driver) as its parent.
# compiler's parent (the driver) as its parent. On ELF, the test also checks
# how a link handles ponyc's linker script.
#
# Args (passed with -D): PONYC (the driver), PONYC_COMPILER (the compiler
# binary), PONYLINK (the ponylink binary), LAUNCHER
Expand Down Expand Up @@ -95,7 +96,7 @@ endfunction()
# finished, successful or not.
function(check_no_temporaries _name _dir _text)
file(GLOB _left
"${_dir}/*.bc" "${_dir}/*.lldargs"
"${_dir}/*.bc" "${_dir}/*.lldargs" "${_dir}/*.ponyc.ld"
"${_dir}/*.tmp" "${_dir}/*.lto_objects")
if(_left)
fail("${_name}: left behind ${_left}" "${_text}")
Expand Down Expand Up @@ -170,11 +171,20 @@ function(check_compiler_link _name _text)
endif()
endfunction()

# One package.
# One package. A linker script of the user's own, named after the program,
# is left as it was.
file(WRITE "${_fixture}/out-single/a.ld" "/* the user's */\n")
run_ponyc(single "${_fixture}" -V3 -o out-single a)
if(NOT single_rc EQUAL 0)
fail("single: ponyc exited ${single_rc}" "${single_text}")
endif()
if(NOT EXISTS "${_fixture}/out-single/a.ld")
fail("single: the link removed out-single/a.ld" "${single_text}")
endif()
file(READ "${_fixture}/out-single/a.ld" _user_script)
if(NOT _user_script STREQUAL "/* the user's */\n")
fail("single: the link changed out-single/a.ld" "${single_text}")
endif()
check_last_link("single" "${single_text}")
check_runs("single" "${_fixture}/out-single/a" "a" "${single_text}")
check_no_temporaries("single" "${_fixture}/out-single" "${single_text}")
Expand Down Expand Up @@ -489,6 +499,70 @@ if(_left OR EXISTS "${_fixture}/out-argsfail/b")
fail("argsfail: left ${_left} or linked anyway" "${argsfail_text}")
endif()

# The linker script can't be written (a non-empty directory sits at its path,
# on the ELF platforms that use one): ponyc reports it and fails, no program is
# left, and the link's temporary files are removed.
if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
file(MAKE_DIRECTORY "${_fixture}/out-scriptfail/b.ponyc.ld/blocker")
run_ponyc(scriptfail "${_fixture}" -o out-scriptfail b)
if(NOT scriptfail_rc EQUAL 255)
fail("scriptfail: ponyc exited ${scriptfail_rc}, not 255"
"${scriptfail_text}")
endif()
if(NOT scriptfail_text MATCHES
"failed to write linker script [^\n]*b\\.ponyc\\.ld: [^\n]+")
fail("scriptfail: no message with the script's path and the reason"
"${scriptfail_text}")
endif()
file(GLOB _left "${_fixture}/out-scriptfail/*.bc"
"${_fixture}/out-scriptfail/*.lldargs"
"${_fixture}/out-scriptfail/*.tmp")
if(_left OR EXISTS "${_fixture}/out-scriptfail/b")
fail("scriptfail: left ${_left} or linked anyway" "${scriptfail_text}")
endif()

# A program linked without the linker script, as when someone links the
# compiler's objects by hand, links and runs with the script's section
# symbols undefined. The program has a Holder (an actor with an embedded
# field), so the runtime must find an actor from its field without the
# descriptor section; the case checks only that the program runs.
file(MAKE_DIRECTORY "${_fixture}/noscript/src")
file(WRITE "${_fixture}/noscript/src/main.pony"
"class C\n var n: U64 = 7\n"
"actor Keeper\n var _c: (C tag | None) = None\n"
" be take(c: C tag) => _c = c\n"
"actor Holder\n embed c: C = C\n"
" new create(k: Keeper) => k.take(c)\n"
"actor Main\n new create(env: Env) =>\n"
" let k = Keeper\n var i: USize = 0\n"
" while i < 100 do Holder(k); i = i + 1 end\n")
run_launched(noscript_build "${_fixture}/noscript" --handoff-pipe
-- "${PONYC_COMPILER}" -o out src)
if(NOT noscript_build_text MATCHES "(^|\n)exited 0\n")
fail("noscript: the compile failed" "${noscript_build_text}")
endif()
file(READ "${_fixture}/noscript/out/src.lldargs" _args)
string(REGEX REPLACE "(^|\n)-T\n[^\n]*\n" "\\1" _text "${_args}")
if(_text STREQUAL _args)
fail("noscript: the link has no linker script to leave out"
"${noscript_build_text}")
endif()
file(WRITE "${_fixture}/noscript/noscript.lldargs" "${_text}")
execute_process(
COMMAND "${PONYLINK}" --ponyc-job
--args "${_fixture}/noscript/noscript.lldargs"
WORKING_DIRECTORY "${_fixture}/noscript"
RESULT_VARIABLE _rc OUTPUT_VARIABLE _out ERROR_VARIABLE _out)
if(NOT _rc EQUAL 0)
fail("noscript: ponylink exited ${_rc}" "${_out}")
endif()
execute_process(COMMAND "${_fixture}/noscript/out/src"
RESULT_VARIABLE _rc OUTPUT_VARIABLE _out ERROR_VARIABLE _out)
if(NOT _rc EQUAL 0)
fail("noscript: the program exited ${_rc}" "${_out}")
endif()
endif()

# An option ponylink doesn't know: a usage error, and ponylink removes nothing.
set(_sentinel "${_fixture}/sentinel")
file(WRITE "${_sentinel}" "keep\n")
Expand Down
1 change: 1 addition & 0 deletions cmake/RunNoAllocLink.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ set(_rejected_methods
"__make_|a heap allocation"
"__one_|a heap allocation"
"__escape_|a heap allocation"
"__shared_|a heap allocation"
"Holder_[a-z]+_create_|a heap allocation"
"__ffi_|an FFI call")

Expand Down
17 changes: 17 additions & 0 deletions cmake/RunOutputFileLinks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,23 @@ foreach(_kind IN LISTS _kinds)
fail("${_case}" "${_names} was left behind")
endif()

# The linker script, which ponyc writes for an ELF link and ponylink
# removes after the link.
if(NOT WIN32 AND NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
set(_case "script-${_kind}")
set(_out "${_fixture}/out-${_case}")
set(_names prog.ponyc.ld)
plant("${_out}" ${_kind} ${_names})
run_ponyc(prog -o "${_out}" -b prog .)
if(NOT _rc EQUAL 0)
fail("${_case}" "ponyc exited ${_rc}\n${_text}")
endif()
check_victims("${_case}" "${_out}" ${_names})
if(EXISTS "${_out}/${_names}")
fail("${_case}" "${_names} was left behind")
endif()
endif()

# The mutation info, and the coverage file that the program writes when
# it exits. Except on Windows, ponyc writes the mutation info to a
# temporary file that ponylink renames after the link. The program names
Expand Down
9 changes: 9 additions & 0 deletions src/common/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
# define PONY_EXTERN_C_END
#endif

/** Inline a function into every caller, whatever the optimiser would choose.
*
*/
#if defined(_MSC_VER)
# define PONY_FORCEINLINE __forceinline
#else
# define PONY_FORCEINLINE inline __attribute__((always_inline))
#endif

/** Determines the operating system.
*
*/
Expand Down
27 changes: 27 additions & 0 deletions src/common/pony_section_names.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef PONY_SECTION_NAMES_H
#define PONY_SECTION_NAMES_H

/* The sections the compiler puts its type descriptors and its other constants
* in, and whose bounds the runtime reads. The ELF names are also in the linker
* script libponyrt/gc/pony_sections.ld, which can't include this header.
*/

#define PONY_ELF_DESC_SECTION ".data.rel.ro.pony-desc"
#define PONY_ELF_CONST_SECTION ".data.rel.ro.pony-const"

#define PONY_MACHO_SEGMENT "__DATA_CONST"
#define PONY_MACHO_DESC_SECTION "__pony_desc"
#define PONY_MACHO_CONST_SECTION "__pony_const"

/* The COFF linker merges the .ponyc$ sections into one, ordered by the text
* after the $. The descriptors are between the runtime's markers in the
* DESC_START and CONST_START sections, and the other constants between the
* markers in the CONST_START and CONST_END sections.
*/
#define PONY_COFF_DESC_START_SECTION ".ponyc$a"
#define PONY_COFF_DESC_SECTION ".ponyc$d"
#define PONY_COFF_CONST_START_SECTION ".ponyc$e"
#define PONY_COFF_CONST_SECTION ".ponyc$m"
#define PONY_COFF_CONST_END_SECTION ".ponyc$z"

#endif
Loading