diff --git a/tslang/docs/superpowers/specs/2026-10-25-own-facts-from-objects-design.md b/tslang/docs/superpowers/specs/2026-10-25-own-facts-from-objects-design.md index f4daeb6df..11dcb7ab9 100644 --- a/tslang/docs/superpowers/specs/2026-10-25-own-facts-from-objects-design.md +++ b/tslang/docs/superpowers/specs/2026-10-25-own-facts-from-objects-design.md @@ -74,9 +74,12 @@ This is as sound as the store rule it reuses: inlining the callee's stores would imported no-drops function; one with places sets `Callees::importedOverwrites`, so the call gets `__own_drop_fields` instead of `__own_no_drops`, and `callMayDrop` counts it. A `..new` gets `__own_fresh_result` from a no-drops library, as before, and any function whose line says `fresh`. -- **The default library.** Without `--no-default-lib`, and not under the JIT, the driver also reads - the facts of the static default library the link will use (`getDefaultLibBinary`, the build and - model of this compile; never its DLL, which a program may load another copy of). +- **The default library.** Without `--no-default-lib`, the driver also reads the facts of the + default library the program runs with (`getDefaultLibBinary`, the build and model of this + compile): for an exe its static library, which the link uses (never its DLL, which a program may + load another copy of); under the JIT the DLL the JIT loads, read from its file as its borrow facts + are. (2026-10-26: the JIT read none, so every call into the library could destroy anything, and + programs that compiled to an exe failed under the JIT. A JIT cache entry depends on the files read.) The facts must describe the code linked: the build passes the objects of the modules it links (or objects compiled from the same sources with the same flags). tslang cannot check that, as it cannot diff --git a/tslang/include/TypeScript/DataStructs.h b/tslang/include/TypeScript/DataStructs.h index 6800f3b22..3e4591fb2 100644 --- a/tslang/include/TypeScript/DataStructs.h +++ b/tslang/include/TypeScript/DataStructs.h @@ -76,6 +76,8 @@ struct CompileOptions // The own facts (__tsownfacts_ texts, concatenated) of the objects named by --own-facts: the // modules this one imports, compiled under -mm=own. Empty: none. std::string importedOwnFacts; + // the files they were read from: a JIT cache entry depends on them + std::vector importedOwnFactsFiles; // an import led back to a file still being generated: the modules in the cycle cannot be // compiled one by one (the JIT cache then compiles the program as one module) bool importCycle = false; diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 39a88e0a5..3e7c359bc 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -2935,6 +2935,25 @@ foreach(defaultlib_collector_mode jit compile) set_tests_properties(test-${defaultlib_collector_mode}-gc-defaultlib-collector PROPERTIES SKIP_REGULAR_EXPRESSION "SKIPPED:") endforeach() +# -mm=own with the real default library: its facts reach the program, under the JIT as for an exe +# (see own-defaultlib-facts.cmake). Skipped when no own default library has been built. +foreach(own_defaultlib_mode jit compile) + add_test(NAME test-${own_defaultlib_mode}-own-defaultlib-facts + COMMAND ${CMAKE_COMMAND} + "-DMODE=${own_defaultlib_mode}" + "-DTSLANG=$" + "-DTSLANG_BIN=${CMAKE_BINARY_DIR}/bin" + "-DTEST=${CMAKE_CURRENT_SOURCE_DIR}/defaultlib/own_default_lib_facts.ts" + "-DWORK_DIR=${CMAKE_CURRENT_BINARY_DIR}/own-defaultlib-facts-${own_defaultlib_mode}" + "-DGC_LIB=${TEST_GC_LIBDIR}" + "-DLLVM_LIB=${defaultlib_collector_llvm_lib}" + "-DTSLANG_LIB=${CMAKE_BINARY_DIR}/lib" + "-DOPT=${defaultlib_collector_opt}" + "-DDEFAULT_LIB_CANDIDATES=${PROJECT_SOURCE_DIR}/../TypeScriptCompilerDefaultLib/__build;${PROJECT_SOURCE_DIR}/../../TypeScriptCompilerDefaultLib/__build" + -P "${CMAKE_CURRENT_SOURCE_DIR}/own-defaultlib-facts.cmake") + set_tests_properties(test-${own_defaultlib_mode}-own-defaultlib-facts PROPERTIES SKIP_REGULAR_EXPRESSION "SKIPPED:") +endforeach() + # x86 twin of the compile-mode default-library collector test above (the x86 default library, # spec Phase 4, As built). No JIT twin: the JIT refuses an x86 target by design (see jit.cpp). # GC_LIB and TSLANG_LIB stay ROOT paths - tslang appends the `x86` segment itself for an i686 diff --git a/tslang/test/tester/defaultlib/own_default_lib_facts.ts b/tslang/test/tester/defaultlib/own_default_lib_facts.ts new file mode 100644 index 000000000..22f58a035 --- /dev/null +++ b/tslang/test/tester/defaultlib/own_default_lib_facts.ts @@ -0,0 +1,22 @@ +// -mm=own with the default library (own-defaultlib-facts.cmake): a field lent to a library method +// that frees nothing a caller can reach. The library's facts say so, under the JIT too: they are read +// from the DLL the JIT loads, as its borrow facts are. Without them each call may destroy anything, +// and the field could not be lent. +class Doc { + constructor(public text: string, public at: Date) {} + + find(word: string) { + return this.text.indexOf(word); + } + + time() { + return this.at.getTime(); + } +} + +function main() { + const d = new Doc("hello world", new Date(301349860000)); + assert(d.find("world") == 6); + assert(d.time() == 301349860000); + print("done."); +} diff --git a/tslang/test/tester/own-defaultlib-facts.cmake b/tslang/test/tester/own-defaultlib-facts.cmake new file mode 100644 index 000000000..fa8b0e14a --- /dev/null +++ b/tslang/test/tester/own-defaultlib-facts.cmake @@ -0,0 +1,91 @@ +# -mm=own with the real default library: a field lent to a library method that frees nothing. +# The library's own facts say so - read from its static library for an exe, and from the DLL the +# JIT loads under the JIT (borrowfacts.cpp, readOwnFacts). Without them every library call may +# destroy anything, and the program does not compile. +# +# MODE=jit run it under the JIT +# MODE=compile build an exe and run it +# +# Every test-runner test passes --no-default-lib, so this drives tslang itself. With no default +# library built the test prints SKIPPED and CTest reports it skipped. + +cmake_minimum_required(VERSION 3.17.3) + +foreach(var MODE TSLANG TSLANG_BIN TEST WORK_DIR GC_LIB LLVM_LIB TSLANG_LIB OPT DEFAULT_LIB_CANDIDATES) + if(NOT DEFINED ${var}) + message(FATAL_ERROR "${var} is required") + endif() +endforeach() + +if(WIN32) + set(exe_suffix ".exe") + set(runtime "${TSLANG_BIN}/TypeScriptRuntime.dll") +else() + set(exe_suffix "") + set(runtime "${TSLANG_LIB}/libTypeScriptRuntime.so") +endif() + +# the folder of the library the run uses: the DLL for the JIT, the static library for an exe +if(MODE STREQUAL "jit") + set(kind "dll") +else() + set(kind "lib") +endif() + +execute_process(COMMAND "${TSLANG}" --print-default-lib-dir=${kind} ${OPT} -mm=own + OUTPUT_VARIABLE default_lib_subdir + OUTPUT_STRIP_TRAILING_WHITESPACE + RESULT_VARIABLE status) +if(NOT status EQUAL 0 OR default_lib_subdir STREQUAL "") + message(FATAL_ERROR "tslang --print-default-lib-dir=${kind} ${OPT} -mm=own: exit ${status}") +endif() + +set(default_lib "") +foreach(candidate "$ENV{DEFAULT_LIB_PATH}" ${DEFAULT_LIB_CANDIDATES}) + if(NOT candidate STREQUAL "" AND IS_DIRECTORY "${candidate}/${default_lib_subdir}") + set(default_lib "${candidate}") + break() + endif() +endforeach() + +if(default_lib STREQUAL "") + if("$ENV{TSLANG_REQUIRE_DEFAULT_LIB}" STREQUAL "1") + message(FATAL_ERROR "no own default library built in ${default_lib_subdir} under DEFAULT_LIB_PATH='$ENV{DEFAULT_LIB_PATH}'") + endif() + message("SKIPPED: no own default library built in ${default_lib_subdir} (set DEFAULT_LIB_PATH to the folder holding 'defaultlib')") + return() +endif() + +set(ENV{DEFAULT_LIB_PATH} "") +file(REMOVE_RECURSE "${WORK_DIR}") +file(MAKE_DIRECTORY "${WORK_DIR}") + +set(common ${OPT} -mm=own "--default-lib-path=${default_lib}" "--llvm-lib-path=${LLVM_LIB}" + "--tslang-lib-path=${TSLANG_LIB}" "--gc-lib-path=${GC_LIB}") + +if(WIN32) + set(run_env "PATH=${default_lib}/${default_lib_subdir};$ENV{PATH}") +else() + set(run_env "LD_LIBRARY_PATH=${default_lib}/${default_lib_subdir}:$ENV{LD_LIBRARY_PATH}") +endif() + +if(MODE STREQUAL "jit") + set(command "${TSLANG}" --emit=jit ${common} "--shared-libs=${runtime}" "${TEST}") +else() + execute_process(COMMAND "${TSLANG}" --emit=exe ${common} "${TEST}" -o "${WORK_DIR}/test${exe_suffix}" + OUTPUT_VARIABLE out ERROR_VARIABLE err RESULT_VARIABLE status) + if(NOT status EQUAL 0) + message(FATAL_ERROR "--emit=exe -mm=own: exit ${status}\n${out}\n${err}") + endif() + + set(command "${WORK_DIR}/test${exe_suffix}") +endif() + +execute_process(COMMAND ${CMAKE_COMMAND} -E env "${run_env}" ${command} + WORKING_DIRECTORY "${WORK_DIR}" + OUTPUT_VARIABLE out ERROR_VARIABLE err RESULT_VARIABLE status) +if(NOT status EQUAL 0 OR NOT out MATCHES "done\.") + message(FATAL_ERROR "${MODE} -mm=own: exit ${status}\n${out}\n${err}") +endif() + +message(STATUS "${MODE} -mm=own: done.") diff --git a/tslang/tslang/borrowfacts.cpp b/tslang/tslang/borrowfacts.cpp index f70ba1244..b762cc220 100644 --- a/tslang/tslang/borrowfacts.cpp +++ b/tslang/tslang/borrowfacts.cpp @@ -184,20 +184,36 @@ void readOwnFacts(CompileOptions &compileOptions) // and the default library's: its own build is the one every own program links (statically: its // DLL, which a program may load another copy of, is never read), so `Math.abs` is a call of a - // function whose facts are known + // function whose facts are known. Under the JIT, the DLL the JIT loads is the code that runs: + // its facts are read from its file, as its borrow facts are (readBorrowFacts). Without them + // every call into the library may destroy anything. std::vector files(ownFacts.begin(), ownFacts.end()); - if (!compileOptions.noDefaultLib && emitAction != Action::RunJIT) + std::string jitDefaultLib; + if (!compileOptions.noDefaultLib) { - files.push_back(getDefaultLibBinary(/*shared=*/false, compileOptions, getLinkTargetTriple())); + if (emitAction == Action::RunJIT) + { + // the JIT is host-only, and loads this file (jit.cpp, prepareJitProcess) + jitDefaultLib = getDefaultLibBinary(/*shared=*/true, compileOptions, + llvm::Triple(llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple()))); + files.push_back(jitDefaultLib); + } + else + { + files.push_back(getDefaultLibBinary(/*shared=*/false, compileOptions, getLinkTargetTriple())); + } } for (auto &file : files) { - if (!llvm::sys::fs::is_regular_file(file) || !isStaticLinkInput(file)) + if (!llvm::sys::fs::is_regular_file(file) || (!isStaticLinkInput(file) && file != jitDefaultLib)) { continue; } + // a JIT cache entry depends on what they said + compileOptions.importedOwnFactsFiles.push_back(file); + for (auto &[symbol, text] : Dump::readCStringSymbols(file, SHARED_LIB_OWN_OBJECT_FACTS)) { compileOptions.importedOwnFacts += text; diff --git a/tslang/tslang/jitcache.cpp b/tslang/tslang/jitcache.cpp index 8e21c7ab6..760b1ec62 100644 --- a/tslang/tslang/jitcache.cpp +++ b/tslang/tslang/jitcache.cpp @@ -581,6 +581,12 @@ class JitUnitBuilder addDep(library, fileHash(library)); } + // ...and those whose own facts decided what -mm=own accepted and erased (borrowfacts.cpp) + for (auto &library : options.importedOwnFactsFiles) + { + addDep(library, fileHash(library)); + } + registerMLIRDialects(*module); llvm::LLVMContext llvmContext;