Skip to content

build: Add security hardening build options - #2174

Merged
lgritz merged 1 commit into
AcademySoftwareFoundation:mainfrom
lgritz:lg-hardening
Oct 14, 2026
Merged

lgritz merged 1 commit into
AcademySoftwareFoundation:mainfrom
lgritz:lg-hardening

Conversation

@lgritz

@lgritz lgritz commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

Port OpenImageIO's hardening build options: the original feature from OIIO PR 4538, with fixes by OIIO PRs 4996 and 5318.

A new OSL_HARDENING cache variable (also settable from the environment) selects a level: 0=none, 1=fast (the default for non-Debug builds), 2=extensive (the default for Debug builds), 3=debug. Depending on the level, compiler, and build type, it enables:

  • Position-independent code, -fstack-protector-strong.
  • _FORTIFY_SOURCE, except in Debug builds (it needs optimization) and with address or memory sanitizers (which predefine it to 0).
  • _GLIBCXX_ASSERTIONS for gcc >= 14 at levels 2 and 3.
  • libc++ hardening modes (_LIBCPP_HARDENING_MODE) FAST, EXTENSIVE, or DEBUG to match the level.

Differences from the OIIO version:

  • OSL passes the directory's compile definitions to the clang commands that make the LLVM bitcode for the JIT (llvm_ops) and for the GPU. Fortified libc calls (__memcpy_chk, etc.) and libc++ assertion handlers don't belong in JIT or device code. So compiler.cmake now sets HARDENING_DEFINITIONS, and EMBED_LLVM_BITCODE_IN_CPP and MAKE_CUDA_BITCODE filter those definitions out.
  • OIIO tests the undefined variables OIIO_CLANG_VERSION and OIIO_APPLE_CLANG_VERSION before setting _LIBCPP_HARDENING_MODE, so libc++ hardening never turns on there. Here we test CLANG_VERSION_STRING >= 18 or APPLECLANG_VERSION_STRING >= 17 (Apple clang 17 is the first to ship a libc++ with hardening modes).

CI: like OIIO, test level 2 on the bleeding-edge job. OSL has no analog of OIIO's "all local builds" job, which uses level 3, so level 3 goes on the gcc-14 latest-releases job instead.

Assisted-by: Claude Code / Claude Opus 5.5

Port OpenImageIO's hardening build options: the original feature from
OIIO PR 4538 as fixed by OIIO PRs 4996 and 5318.

A new OSL_HARDENING cache variable (also settable from the environment)
selects a level: 0=none, 1=fast (the default for non-Debug builds),
2=extensive (the default for Debug builds), 3=debug. Depending on the
level, compiler, and build type, it enables:

* Position-independent code, -fstack-protector-strong.
* _FORTIFY_SOURCE, except in Debug builds (it needs optimization) and
  with address or memory sanitizers (which predefine it to 0).
* _GLIBCXX_ASSERTIONS for gcc >= 14 at levels 2 and 3.
* libc++ hardening modes (_LIBCPP_HARDENING_MODE) FAST, EXTENSIVE, or
  DEBUG to match the level.

Differences from the OIIO version:

* OSL passes the directory's compile definitions to the clang commands
  that make the LLVM bitcode for the JIT (llvm_ops) and for the GPU.
  Fortified libc calls (__memcpy_chk, etc.) and libc++ assertion
  handlers don't belong in JIT or device code. So compiler.cmake now
  sets HARDENING_DEFINITIONS, and EMBED_LLVM_BITCODE_IN_CPP and
  MAKE_CUDA_BITCODE filter those definitions out.
* OIIO tests the undefined variables OIIO_CLANG_VERSION and
  OIIO_APPLE_CLANG_VERSION before setting _LIBCPP_HARDENING_MODE, so
  libc++ hardening never turns on there. Here we test
  CLANG_VERSION_STRING >= 18 or APPLECLANG_VERSION_STRING >= 17 (Apple
  clang 17 is the first to ship a libc++ with hardening modes).

CI: like OIIO, test level 2 on the bleeding-edge job. OSL has no
analog of OIIO's "all local builds" job, which uses level 3, so level 3
goes on the gcc-14 latest-releases job instead.

Assisted-by: Claude Code / Claude Opus 5.5

Signed-off-by: Larry Gritz <lg@larrygritz.com>
@lgritz

lgritz commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator Author

No objections for 3 weeks -> merge

@lgritz
lgritz merged commit b7e6d1b into AcademySoftwareFoundation:main Oct 14, 2026
22 checks passed
@lgritz
lgritz deleted the lg-hardening branch October 15, 2026 00:25
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.

1 participant