@tsplc/build 0.2.21 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +2 -2
  2. package/dist/cli.js +1052 -836
  3. package/dist/cli.js.map +27 -24
  4. package/dist/images.js +165 -0
  5. package/dist/images.js.map +12 -0
  6. package/dist/index.js +888 -668
  7. package/dist/index.js.map +23 -20
  8. package/dist/libraries.js +4 -3
  9. package/dist/libraries.js.map +3 -3
  10. package/docker/Dockerfile.dockerignore +0 -4
  11. package/docker/Dockerfile.source +5 -0
  12. package/docker/build-image +30 -26
  13. package/docker/config.ts +1 -1
  14. package/docker/install/wine-signal/README.md +1 -1
  15. package/docker/runner.ts +9 -7
  16. package/package.json +24 -8
  17. package/python/build.py +231 -20
  18. package/python/codesys_online.py +121 -10
  19. package/python/worker.py +2 -0
  20. package/src/artifacts.ts +9 -7
  21. package/src/build.ts +9 -4
  22. package/src/cli.ts +11 -20
  23. package/src/controller-online.ts +145 -61
  24. package/src/controller-protocol.ts +48 -5
  25. package/src/controller-session.ts +386 -147
  26. package/src/device-tree.ts +1 -1
  27. package/src/docker-build.ts +80 -98
  28. package/src/docker-context.ts +56 -3
  29. package/src/docker-run.ts +56 -3
  30. package/src/engineering-settings.ts +3 -3
  31. package/src/host/codesys-host.ts +3 -3
  32. package/src/host/codesys-worker.ts +77 -3
  33. package/src/images.ts +105 -0
  34. package/src/import-request.ts +1 -0
  35. package/src/index.ts +30 -16
  36. package/src/io.ts +1 -1
  37. package/src/libraries.ts +3 -2
  38. package/src/project-operations.ts +48 -0
  39. package/src/run.ts +9 -6
  40. package/src/sources.ts +2 -2
  41. package/docker/install/wine-signal/check.sh +0 -34
  42. package/docker/install/wine-signal/tests/avx-registers.c +0 -53
  43. package/docker/install/wine-signal/tests/frame-and-handler.c +0 -192
  44. package/docker/install/wine-signal/tests/loader-i386.c +0 -8
  45. package/docker/install/wine-signal/tests/signal-return.c +0 -47
  46. package/python/test_build.py +0 -579
  47. package/python/test_codesys_login.py +0 -139
  48. package/python/test_codesys_online.py +0 -259
  49. package/python/test_installer_packages.py +0 -217
  50. package/python/test_installer_verification.py +0 -307
  51. package/python/test_worker.py +0 -154
package/src/images.ts ADDED
@@ -0,0 +1,105 @@
1
+ import { copyFile, cp, mkdir, mkdtemp, rm } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import { resolve } from "node:path";
4
+ import { dockerImage } from "@tsplc/config/build";
5
+ import { prepareDockerContext } from "./docker-context.ts";
6
+ import { packageDirectory } from "./package-path.ts";
7
+
8
+ export const builderImage = dockerImage;
9
+
10
+ export const gatewayImage = "tsplc-edge-gateway:4.22.0.0";
11
+
12
+ /** The local CODESYS Control for Linux SL runtime; it is always built here, never published. */
13
+ export const runtimeImage = "tsplc-control-linux-sl:4.22.0.0";
14
+
15
+ /** The registry name each tsPLC version's copy of a local image is published under. */
16
+ export const publishedImage = (image: string, version: string) =>
17
+ `ghcr.io/rip-technologies/${image.split(":")[0]}:${version}`;
18
+
19
+ /** The platform the vendor installer runs on; the compiler image itself is always AMD64. */
20
+ export type SetupPlatform = "linux/amd64" | "linux/arm64";
21
+
22
+ /** The setup platform for a Docker server's `{{.OSType}}/{{.Architecture}}`, or null when it has none. */
23
+ export const setupPlatformFor = (server: string): SetupPlatform | null => {
24
+ if (server === "linux/aarch64" || server === "linux/arm64") {
25
+ return "linux/arm64";
26
+ }
27
+ if (server === "linux/x86_64" || server === "linux/amd64") {
28
+ return "linux/amd64";
29
+ }
30
+ return null;
31
+ };
32
+
33
+ /**
34
+ * Builds the builder image from the full recipe. `installer` is a local CODESYS installer,
35
+ * or null to download the official one; `winetricksCache` seeds the prerequisite downloads.
36
+ * `docker` runs one Docker client command, so each caller keeps its own terminal handling.
37
+ */
38
+ export async function buildBuilderImage(options: {
39
+ tag: string;
40
+ setupPlatform: SetupPlatform;
41
+ installer: string | null;
42
+ winetricksCache: string | null;
43
+ docker: (args: string[]) => Promise<void>;
44
+ }) {
45
+ const installerContext = await mkdtemp(
46
+ resolve(tmpdir(), "codesys-installer-"),
47
+ );
48
+ let context: string | undefined;
49
+ try {
50
+ context = await prepareDockerContext();
51
+ const cache = resolve(installerContext, "winetricks");
52
+ await mkdir(cache);
53
+ if (options.winetricksCache) {
54
+ await cp(options.winetricksCache, cache, { recursive: true });
55
+ }
56
+ if (options.installer) {
57
+ await copyFile(
58
+ options.installer,
59
+ resolve(installerContext, "codesys.exe"),
60
+ );
61
+ }
62
+ const arm = options.setupPlatform === "linux/arm64";
63
+ // FEX handles ARM setup's 32-bit prerequisites; the compiler runtime stays AMD64.
64
+ await options.docker([
65
+ "build",
66
+ "--platform",
67
+ "linux/amd64",
68
+ "--build-arg",
69
+ `SETUP_BASE=${arm ? "arm-setup" : "base"}`,
70
+ "--build-arg",
71
+ `SETUP_PLATFORM=${options.setupPlatform}`,
72
+ "--build-arg",
73
+ `INSTALLER_SOURCE=${options.installer ? "installer-local" : "installer-download"}`,
74
+ "--progress",
75
+ "plain",
76
+ "--build-context",
77
+ `installer=${installerContext}`,
78
+ "-f",
79
+ resolve(context, "docker/Dockerfile"),
80
+ "-t",
81
+ options.tag,
82
+ context,
83
+ ]);
84
+ } finally {
85
+ await Promise.all([
86
+ rm(installerContext, { recursive: true, force: true }),
87
+ ...(context ? [rm(context, { recursive: true, force: true })] : []),
88
+ ]);
89
+ }
90
+ }
91
+
92
+ export const buildGatewayImage = (options: {
93
+ tag: string;
94
+ docker: (args: string[]) => Promise<void>;
95
+ }) =>
96
+ options.docker([
97
+ "build",
98
+ "--platform",
99
+ "linux/amd64",
100
+ "--progress",
101
+ "plain",
102
+ "-t",
103
+ options.tag,
104
+ resolve(packageDirectory, "docker/edge-gateway"),
105
+ ]);
@@ -21,4 +21,5 @@ export const importRequest = async (
21
21
  ...(config.tasks && { tasks: config.tasks }),
22
22
  removeObjects: config.removeObjects,
23
23
  symbols: config.symbols,
24
+ ...(config.bulkImport && { bulkImport: true }),
24
25
  });
package/src/index.ts CHANGED
@@ -1,4 +1,3 @@
1
- export { buildProject } from "./build";
2
1
  export {
3
2
  type CodesysBuildConfig,
4
3
  type CyclicTask,
@@ -8,6 +7,8 @@ export {
8
7
  type ResolvedConfig,
9
8
  resolveConfig,
10
9
  } from "@tsplc/config/build";
10
+ export { readBuild } from "./artifacts.ts";
11
+ export { buildProject } from "./build";
11
12
  export {
12
13
  type ControllerLogin,
13
14
  findControllerLogin,
@@ -22,31 +23,44 @@ export {
22
23
  explicitDownload,
23
24
  failureReason,
24
25
  type LoginCommand,
25
- loginCommandSchema,
26
26
  type LoginResult,
27
+ loginCommandSchema,
27
28
  loginResultSchema,
29
+ type Rollback,
30
+ requiresDownload,
31
+ rollbackSchema,
32
+ TransferFailedError,
33
+ transferRollback,
28
34
  } from "./controller-protocol";
35
+ export { controllerBaseline } from "./controller-session.ts";
29
36
  export {
30
37
  type CodesysCompilerDiagnostic,
31
38
  CodesysCompilerError,
32
39
  } from "./diagnostics";
40
+ export {
41
+ configureDocker,
42
+ configureGateway,
43
+ configureRuntime,
44
+ } from "./docker-build.ts";
45
+ export {
46
+ captureDocker,
47
+ type ProgressOptions,
48
+ type ProgressStream,
49
+ } from "./docker-run.ts";
33
50
  export { rawEngineeringSettings } from "./engineering-settings";
34
- export { runProject } from "./run";
35
- export { loadSources, type SourceObject } from "./sources";
36
- export { StSyntaxError } from "./st/lexer";
37
- export { type ObjectKind, type ParsedSource, parseSource } from "./st/parse";
38
-
51
+ export { workerScriptsDigest } from "./host/codesys-worker.ts";
52
+ export { gatewayImage } from "./images.ts";
39
53
  export {
54
+ type InstalledLibrary,
40
55
  installLibraries,
41
- validateLibrary,
42
56
  libraryIndex,
43
- type InstalledLibrary,
57
+ validateLibrary,
44
58
  } from "./libraries.ts";
45
-
46
59
  export {
47
- configureDocker,
48
- configureGateway,
49
- configureRuntime,
50
- gatewayImage,
51
- } from "./docker-build.ts";
52
- export { readBuild } from "./artifacts.ts";
60
+ checkRawSources,
61
+ inspectProjectController,
62
+ } from "./project-operations.ts";
63
+ export { runProject } from "./run";
64
+ export { loadSources, type SourceObject } from "./sources";
65
+ export { StSyntaxError } from "./st/lexer";
66
+ export { type ObjectKind, type ParsedSource, parseSource } from "./st/parse";
package/src/io.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ioArray, ioScalarBits, sizePrefix } from "@tsplc/iec";
2
2
  import { qualifiers, variableSections } from "@tsplc/iec/st";
3
3
  import type { SourceObject } from "./sources.ts";
4
- import { tokenize, type Token } from "./st/lexer.ts";
4
+ import { type Token, tokenize } from "./st/lexer.ts";
5
5
 
6
6
  export interface DirectIoBinding {
7
7
  address: string;
package/src/libraries.ts CHANGED
@@ -3,6 +3,7 @@ import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
3
3
  import { basename, relative, resolve } from "node:path";
4
4
  import type { LibraryDependency } from "@tsplc/config";
5
5
  import { sha256Digest } from "@tsplc/config/build";
6
+ import { generatedDirectory } from "@tsplc/config/project-files";
6
7
  import { z } from "zod";
7
8
 
8
9
  export const libraryRegistry = "https://store-archive.codesys.com/CODESYSLibs";
@@ -122,7 +123,7 @@ export async function installLibraries(
122
123
  options: { frozen?: boolean; fetcher?: Fetcher } = {},
123
124
  ) {
124
125
  const file = resolve(root, "tsplc.lock.json");
125
- const store = resolve(root, ".tsplc/libraries");
126
+ const store = resolve(root, generatedDirectory, "libraries");
126
127
  const previous = await readLock(file);
127
128
  const installed: Record<string, InstalledLibrary> = {};
128
129
  const fetcher = options.fetcher ?? fetch;
@@ -226,7 +227,7 @@ export async function installLibraries(
226
227
  );
227
228
  await writeFile(
228
229
  temp,
229
- JSON.stringify({ version: 1, dependencies: portable }, null, 2) + "\n",
230
+ `${JSON.stringify({ version: 1, dependencies: portable }, null, 2)}\n`,
230
231
  );
231
232
  await rename(temp, file);
232
233
  }
@@ -0,0 +1,48 @@
1
+ import {
2
+ controllerFor,
3
+ type LoadedConfig,
4
+ rawBuildConfig,
5
+ } from "@tsplc/config";
6
+ import { resolveConfig } from "@tsplc/config/build";
7
+ import { loadHardware, resolveIo } from "@tsplc/typegen/hardware";
8
+ import { inspectController } from "./controller-online.ts";
9
+ import type { ProgressOptions } from "./docker-run.ts";
10
+ import { loadSources } from "./sources.ts";
11
+
12
+ /** Parses a raw Structured Text project's sources with its IO bound, without installing libraries or building. */
13
+ export const checkRawSources = async ({
14
+ config,
15
+ configFile,
16
+ root,
17
+ }: LoadedConfig) =>
18
+ loadSources(
19
+ resolveConfig(
20
+ {
21
+ ...rawBuildConfig(config),
22
+ io: resolveIo(config, await loadHardware(config, root)),
23
+ },
24
+ configFile,
25
+ ),
26
+ );
27
+
28
+ /**
29
+ * Reads the configured controller's state. Inspection needs only the connection and baseline,
30
+ * so it stays usable without local sources, EDS descriptors or installed library payloads.
31
+ */
32
+ export const inspectProjectController = (
33
+ { config, configFile }: LoadedConfig,
34
+ read: string[] | undefined,
35
+ options: ProgressOptions,
36
+ ) => {
37
+ if (!config.build) {
38
+ throw new Error("Configure build settings and controller connection.");
39
+ }
40
+ return inspectController(
41
+ resolveConfig(
42
+ { ...config.build, controller: controllerFor(config, "inspect") },
43
+ configFile,
44
+ ),
45
+ read,
46
+ options,
47
+ );
48
+ };
package/src/run.ts CHANGED
@@ -2,12 +2,12 @@ import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
2
2
  import { tmpdir } from "node:os";
3
3
  import { basename, dirname, join } from "node:path";
4
4
  import { setTimeout as delay } from "node:timers/promises";
5
+ import type { ResolvedConfig } from "@tsplc/config/build";
5
6
  import { z } from "zod";
6
7
  import { readBuild } from "./artifacts";
7
- import type { ResolvedConfig } from "@tsplc/config/build";
8
- import { runtimeImage } from "./docker-build";
9
- import { captureDocker } from "./docker-run";
8
+ import { captureDocker, type ProgressOptions, progressLog } from "./docker-run";
10
9
  import { withWorkerLock } from "./host/codesys-worker";
10
+ import { runtimeImage } from "./images.ts";
11
11
  import { getApplicationStatus } from "./runtime-status";
12
12
 
13
13
  const stateSchema = z.object({
@@ -17,7 +17,7 @@ const stateSchema = z.object({
17
17
 
18
18
  const service = "plc";
19
19
 
20
- /** The Docker Compose project of a tsPLC project: `FrameAssembly` runs as `frame-assembly`. */
20
+ /** The Docker Compose project of a tsPLC project: `DemoController` runs as `demo-controller`. */
21
21
  export const runtimeProject = (name: string) =>
22
22
  name
23
23
  .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
@@ -48,6 +48,7 @@ export const runtimeDefinition = (name: string) => ({
48
48
  export const runProject = async (
49
49
  config: Pick<ResolvedConfig, "outDir" | "timeouts">,
50
50
  name: string,
51
+ { progress = "stdout" }: ProgressOptions = {},
51
52
  ) =>
52
53
  withWorkerLock(config, async () => {
53
54
  // Validate before stopping anything. A missing receipt includes a failed rebuild.
@@ -79,7 +80,7 @@ export const runProject = async (
79
80
  new URL("../runtime/install.sh", import.meta.url),
80
81
  "utf8",
81
82
  );
82
- console.log(
83
+ progressLog(progress)(
83
84
  `Installing ${build.application} in Docker service ${service}`,
84
85
  );
85
86
  await compose("stop", service);
@@ -141,7 +142,9 @@ export const runProject = async (
141
142
  Date.parse(state.StartedAt),
142
143
  );
143
144
  if (status === "running") {
144
- console.log(`${build.application} is running in ${service}`);
145
+ progressLog(progress)(
146
+ `${build.application} is running in ${service}`,
147
+ );
145
148
  return { ...build, container };
146
149
  }
147
150
  if (status === "failed" || Date.now() >= deadline) {
package/src/sources.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { readdir, readFile } from "node:fs/promises";
2
2
  import { basename, resolve } from "node:path";
3
- import { identifierPattern } from "@tsplc/iec/st";
4
3
  import type { ResolvedConfig } from "@tsplc/config/build";
5
- import { type ObjectKind, type ParsedSource, parseSource } from "./st/parse";
4
+ import { identifierPattern } from "@tsplc/iec/st";
6
5
  import { bindStructuredTextIo } from "./io.ts";
6
+ import { type ObjectKind, type ParsedSource, parseSource } from "./st/parse";
7
7
 
8
8
  export type SourceObject = ParsedSource & {
9
9
  source: string;
@@ -1,34 +0,0 @@
1
- #!/bin/sh
2
- set -eu
3
- sources=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
4
- work=$(mktemp -d /tmp/tsplc-wine-signal.XXXXXX)
5
- echo "Native check artifacts: $work"
6
- "$sources/build.sh" "$work/compat"
7
- gcc -Wall -Wextra -Werror -O2 -fno-tree-vectorize \
8
- "$sources/tests/frame-and-handler.c" -ldl -o "$work/frame-and-handler"
9
- gcc -Wall -Wextra -Werror -O2 -mavx \
10
- "$sources/tests/avx-registers.c" -o "$work/avx-registers"
11
- gcc -Wall -Wextra -Werror -O2 \
12
- "$sources/tests/signal-return.c" -o "$work/signal-return"
13
- gcc -m32 -nostdlib -Wall -Wextra -Werror -O2 -Wl,-e,_start \
14
- -Wl,--dynamic-linker=/lib/ld-linux.so.2 \
15
- "$sources/tests/loader-i386.c" -o "$work/loader-i386"
16
- "$work/frame-and-handler"
17
- # Leave the calling shell's environment untouched, just as the Wine launcher does.
18
- preload="$work/compat/\$LIB/signal-state.so"
19
- LD_PRELOAD="$preload" "$work/signal-return"
20
- LD_PRELOAD="$preload" "$work/avx-registers"
21
- LD_PRELOAD="$preload" "$work/avx-registers" nonzero
22
- LD_DEBUG=libs,files LD_PRELOAD="$preload" "$work/signal-return" > "$work/amd64.log" 2>&1
23
- LD_DEBUG=libs,files LD_PRELOAD="$preload" "$work/loader-i386" > "$work/i386.log" 2>&1
24
- python3 - "$work" <<'PY_CHECK'
25
- import pathlib, sys
26
- work = pathlib.Path(sys.argv[1])
27
- for architecture, library in [('amd64', 'x86_64-linux-gnu'), ('i386', 'i386-linux-gnu')]:
28
- log = (work / (architecture + '.log')).read_text()
29
- expected = str(work / 'compat' / 'lib' / library / 'signal-state.so')
30
- if 'calling init: ' + expected not in log or any(message in log for message in
31
- ['ERROR:', 'wrong ELF class', 'cannot be preloaded']):
32
- raise SystemExit(log)
33
- print(architecture + ' loader: ' + expected + ' PASS (no preload warnings)')
34
- PY_CHECK
@@ -1,53 +0,0 @@
1
- #define _GNU_SOURCE
2
- #include <assert.h>
3
- #include <signal.h>
4
- #include <stdint.h>
5
- #include <stdio.h>
6
- #include <string.h>
7
- #include <sys/syscall.h>
8
- #include <ucontext.h>
9
- #include <unistd.h>
10
-
11
- static int preserve_nonzero;
12
-
13
- static void handler(int signal, siginfo_t *information, void *pointer)
14
- {
15
- (void)signal;
16
- (void)information;
17
- unsigned char *state = (void *)((ucontext_t *)pointer)->uc_mcontext.fpregs;
18
- uint64_t features;
19
- memcpy(&features, state + 512, sizeof(features));
20
- if ((features & 7) != 7) _exit(78);
21
- if (preserve_nonzero) {
22
- memset(state + 576, 0x3c, 256);
23
- } else {
24
- features &= ~UINT64_C(4);
25
- memcpy(state + 512, &features, sizeof(features));
26
- }
27
- }
28
-
29
- int main(int argc, char **argv)
30
- {
31
- (void)argv;
32
- preserve_nonzero = argc > 1;
33
- struct sigaction action = {.sa_sigaction = handler, .sa_flags = SA_SIGINFO};
34
- sigemptyset(&action.sa_mask);
35
- assert(!sigaction(SIGUSR1, &action, NULL));
36
- unsigned char seed[32], returned[32];
37
- memset(seed, 0x19, 16);
38
- memset(seed + 16, 0x29, 16);
39
- long process = getpid(), thread = syscall(SYS_gettid), number = SYS_tgkill;
40
- /* Do not call a libc function between loading and reading YMM0: the SysV
41
- calling convention would allow it to destroy the vector register. */
42
- __asm__ volatile("vmovdqu %2, %%ymm0\n\tsyscall\n\tvmovdqu %%ymm0, %0"
43
- : "=m"(returned), "+a"(number)
44
- : "m"(seed), "D"(process), "S"(thread), "d"((long)SIGUSR1)
45
- : "rcx", "r11", "memory", "ymm0");
46
- assert(number == 0);
47
- for (int index = 0; index < 16; ++index) assert(returned[index] == 0x19);
48
- for (int index = 16; index < 32; ++index)
49
- assert(returned[index] == (preserve_nonzero ? 0x3c : 0));
50
- printf("Real AVX register return: %s PASS\n",
51
- preserve_nonzero ? "nonzero preserved" : "absent component initialized");
52
- return 0;
53
- }
@@ -1,192 +0,0 @@
1
- #include "../signal-state.c"
2
- #include <assert.h>
3
- #include <stdio.h>
4
- #include <string.h>
5
- #include <sys/mman.h>
6
-
7
- static int calls;
8
- static uint64_t desired_features;
9
- static void *replacement;
10
- static void original(int signal, siginfo_t *information, void *pointer)
11
- {
12
- (void)information;
13
- assert(signal == SIGUSR1);
14
- ++calls;
15
- ucontext_t *context = pointer;
16
- if (replacement) context->uc_mcontext.fpregs = replacement;
17
- if (context->uc_mcontext.fpregs)
18
- ((struct signal_xstate *)context->uc_mcontext.fpregs)->features = desired_features;
19
- }
20
- static void different(int signal, siginfo_t *information, void *pointer)
21
- {
22
- (void)signal;
23
- (void)information;
24
- (void)pointer;
25
- }
26
- static void simple(int signal)
27
- {
28
- (void)signal;
29
- }
30
-
31
- static struct signal_xstate frame(void)
32
- {
33
- struct signal_xstate value;
34
- memset(&value, 0xa5, sizeof(value));
35
- value.magic = 0x46505853;
36
- value.extended_size = 836;
37
- value.supported_features = 7;
38
- value.xstate_size = 832;
39
- value.features = 7;
40
- value.header_reserved[0] = 0x6e6769732d656e69;
41
- /* Observed unused Rosetta header data. */
42
- value.end_magic = 0x46505845;
43
- return value;
44
- }
45
- static void invoke(struct signal_xstate *value)
46
- {
47
- ucontext_t context = {0};
48
- context.uc_mcontext.fpregs = (void *)value;
49
- struct sigaction kernel;
50
- assert(!next_sigaction(SIGUSR1, NULL, &kernel));
51
- kernel.sa_sigaction(SIGUSR1, NULL, &context);
52
- }
53
- static int cases;
54
- static void expect_unchanged(struct signal_xstate value)
55
- {
56
- struct signal_xstate expected = value;
57
- expected.features = desired_features;
58
- invoke(&value);
59
- assert(!memcmp(&value, &expected, sizeof(value)));
60
- ++cases;
61
- }
62
-
63
- int main(void)
64
- {
65
- struct sigaction action = {
66
- .sa_sigaction = original,
67
- .sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK
68
- };
69
- sigemptyset(&action.sa_mask);
70
- sigaddset(&action.sa_mask, SIGUSR2);
71
- struct sigaction before, query;
72
- assert(!sigaction(SIGUSR1, &action, &before));
73
- assert(!sigaction(SIGUSR1, NULL, &query));
74
- assert(query.sa_sigaction == original);
75
- assert((query.sa_flags & (SA_SIGINFO | SA_RESTART | SA_ONSTACK)) == action.sa_flags);
76
- assert(sigismember(&query.sa_mask, SIGUSR2));
77
- ++cases;
78
-
79
- desired_features = 3;
80
- struct signal_xstate value = frame(), expected = value;
81
- memset(expected.ymm_high, 0, sizeof(expected.ymm_high));
82
- invoke(&value);
83
- assert(!memcmp(&value, &expected, sizeof(value)));
84
- ++cases;
85
-
86
- desired_features = 7;
87
- expect_unchanged(frame());
88
- /* Nonzero AVX state remains intact. */
89
- desired_features = 3;
90
- value = frame();
91
- value.features = 3;
92
- expect_unchanged(value);
93
- value = frame();
94
- value.magic = 0;
95
- expect_unchanged(value);
96
- value = frame();
97
- value.extended_size = 835;
98
- expect_unchanged(value);
99
- value = frame();
100
- value.extended_size = 840;
101
- expect_unchanged(value);
102
- value = frame();
103
- value.xstate_size = 831;
104
- expect_unchanged(value);
105
- value = frame();
106
- value.supported_features = 15;
107
- expect_unchanged(value);
108
- value = frame();
109
- value.header_reserved[0] = UINT64_C(1) << 63;
110
- expected = value;
111
- memset(expected.ymm_high,0, sizeof(expected.ymm_high));
112
- invoke(&value);
113
- assert(!memcmp(&value, &expected, sizeof(value)));
114
- ++cases;
115
- value = frame();
116
- value.end_magic = 0;
117
- expect_unchanged(value);
118
- desired_features = 1;
119
- expect_unchanged(frame());
120
- desired_features = 5;
121
- expect_unchanged(frame());
122
- desired_features = 15;
123
- expect_unchanged(frame());
124
- desired_features = 3;
125
- invoke(NULL);
126
- ++cases;
127
-
128
- value = frame();
129
- struct signal_xstate second = frame();
130
- expected = second;
131
- memset(expected.ymm_high, 0, sizeof(expected.ymm_high));
132
- replacement = &second;
133
- invoke(&value);
134
- struct signal_xstate unchanged = frame();
135
- assert(!memcmp(&value, &unchanged, sizeof(value)));
136
- assert(!memcmp(&second, &expected, sizeof(second)));
137
- replacement = NULL;
138
- ++cases;
139
-
140
- /* The legacy header can end at a protected page boundary. No extended
141
- access is permitted after missing magic. */
142
- long page = sysconf(_SC_PAGESIZE);
143
- void *memory = mmap(NULL, (size_t)page * 2, PROT_READ | PROT_WRITE,
144
- MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
145
- assert(memory != MAP_FAILED);
146
- assert(!mprotect((char *)memory + page,(size_t)page, PROT_NONE));
147
- unsigned char *legacy = (unsigned char *)memory + page - 512;
148
- memset(legacy, 0, 512);
149
- ucontext_t context = {0};
150
- context.uc_mcontext.fpregs = (void *)legacy;
151
- assert(valid_state(&context) == NULL);
152
- munmap(memory,(size_t)page * 2);
153
- ++cases;
154
-
155
- struct sigaction changed = {.sa_sigaction = different, .sa_flags = SA_SIGINFO};
156
- sigemptyset(&changed.sa_mask);
157
- assert(!sigaction(SIGUSR1, &changed, &query));
158
- assert(query.sa_sigaction == original);
159
- assert(!next_sigaction(SIGUSR1, NULL, &query));
160
- assert(query.sa_sigaction == different);
161
- ++cases;
162
- assert(!sigaction(SIGUSR1, &action, &query));
163
- assert(query.sa_sigaction == different);
164
- ++cases;
165
- struct sigaction ignored = {.sa_handler = SIG_IGN};
166
- sigemptyset(&ignored.sa_mask);
167
- assert(!sigaction(SIGUSR1, &ignored, &query));
168
- assert(query.sa_sigaction == original);
169
- assert(!sigaction(SIGUSR1, NULL, &query));
170
- assert(query.sa_handler == SIG_IGN);
171
- ++cases;
172
- struct sigaction defaults = {.sa_handler = SIG_DFL};
173
- sigemptyset(&defaults.sa_mask);
174
- assert(!sigaction(SIGUSR1, &defaults, &query));
175
- assert(query.sa_handler == SIG_IGN);
176
- assert(!sigaction(SIGUSR1, NULL, &query));
177
- assert(query.sa_handler == SIG_DFL);
178
- ++cases;
179
- struct sigaction one = {.sa_handler = simple};
180
- sigemptyset(&one.sa_mask);
181
- assert(!sigaction(SIGUSR1, &one, &query));
182
- assert(query.sa_handler == SIG_DFL);
183
- assert(!next_sigaction(SIGUSR1, NULL, &query));
184
- assert(query.sa_handler == simple);
185
- ++cases;
186
- assert(!sigaction(SIGUSR2, &action, NULL));
187
- assert(!next_sigaction(SIGUSR2, NULL, &query));
188
- assert(query.sa_sigaction == original);
189
- ++cases;
190
- printf("Signal adapter semantics: %d cases PASS (%d original callbacks)\n", cases, calls);
191
- return 0;
192
- }
@@ -1,8 +0,0 @@
1
- /* A minimal dynamic i386 executable: its loader must expand LD_PRELOAD
2
- correctly, then this entry point exits without depending on 32-bit libc. */
3
- __attribute__((noreturn)) void _start(void)
4
- {
5
- __asm__ volatile("movl $1, %%eax\n\txorl %%ebx, %%ebx\n\tint $0x80"
6
- : : : "eax", "ebx", "memory");
7
- __builtin_unreachable();
8
- }
@@ -1,47 +0,0 @@
1
- #define _GNU_SOURCE
2
- #include <signal.h>
3
- #include <stdint.h>
4
- #include <stdio.h>
5
- #include <stdlib.h>
6
- #include <string.h>
7
- #include <ucontext.h>
8
- #include <unistd.h>
9
-
10
- static int normalize;
11
-
12
- static void handler(int signal, siginfo_t *information, void *context)
13
- {
14
- (void)signal;
15
- (void)information;
16
- unsigned char *state = (unsigned char *)((ucontext_t *)context)->uc_mcontext.fpregs;
17
- uint32_t magic, extended_size;
18
- uint64_t features;
19
- memcpy(&magic, state + 464, sizeof(magic));
20
- memcpy(&extended_size, state + 468, sizeof(extended_size));
21
- if (magic != 0x46505853 || extended_size < 832) _exit(77);
22
- memcpy(&features, state + 512, sizeof(features));
23
- if ((features & 7) != 7) _exit(78);
24
- features &= ~UINT64_C(4);
25
- memcpy(state + 512, &features, sizeof(features));
26
- if (normalize) {
27
- /* An absent AVX component requests its initial, all-zero YMM-high state.
28
- Store that state explicitly before marking the component present. */
29
- memset(state + 576, 0, 256);
30
- features |= 4;
31
- memcpy(state + 512, &features, sizeof(features));
32
- }
33
- ssize_t ignored = write(STDOUT_FILENO,
34
- normalize ? "Returning features 7\n" : "Returning features 3\n", 21);
35
- (void)ignored;
36
- }
37
-
38
- int main(int argc, char **argv)
39
- {
40
- (void)argv;
41
- normalize = argc > 1;
42
- struct sigaction action = { .sa_sigaction = handler, .sa_flags = SA_SIGINFO };
43
- sigemptyset(&action.sa_mask);
44
- if (sigaction(SIGUSR1, &action, NULL) || raise(SIGUSR1)) return 1;
45
- puts("Signal returned: PASS");
46
- return 0;
47
- }