Files
carbon-lang/toolchain/base/BUILD
T
David BlaikieandRichard Smith 12fa65e53c Check for use of InstIds from the wrong SemIR::File (#5997)
Use the `CheckIRId` as a unique identifier for the scope of an `InstId`
- if an `InstId` is created within the scope of one `CheckIRId` it must
not be used in the scope of a different `CheckIRId`.

This is achieved without extra storage, but with false negatives for
large inputs.

When an `InstId` is created, the original index of the `Inst` is XORed
with a tag derived from the `CheckIRId` to produce the final `InstId`.
When the `InstId` is used, the expected tag is XORed with the `InstId`
to get back to the original index - if the tags don't match, the
resulting index will be corrupted, likely too large - resulting in an
out of bounds index CHECK-failure.

(the tag value is derived as such:
* take the CheckIRId
* left shift one bit (padding zero)
* left shift another bit (padding 1 - used to signify that the resulting
`InstId` has a tag combined into it)
* reverse the bits

In this way, the tag is unlikely to overlap with the index for small
test cases - making it possible to separate out the `CheckIRId` from the
index in these cases to provide more meaningful debugging/CHECK
messages, and more informative `SemIR` textual dumping that can now
include the `CheckIRId` along with the `Inst`'s index in the name of an
`inst`)

The test churn here is improved printing as tagged `InstId`s can now,
with best effort (more likely for small test cases where the `CheckIRId`
and the `Inst` index aren't at risk of overlapping from the high and low
bits), render the `CheckIRId` as part of the inst's name. Going from
`instNN` to `irMM.instNN`.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2025-10-02 23:07:36 +00:00

291 lines
6.1 KiB
Python

# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
load("//bazel/cc_rules:defs.bzl", "cc_library", "cc_test")
load("llvm_tools.bzl", "LLVM_MAIN_TOOLS", "generate_llvm_tools_def")
load("runtime_sources.bzl", "generate_runtime_sources_cc_library")
package(default_visibility = ["//visibility:public"])
cc_library(
name = "block_value_store",
hdrs = ["block_value_store.h"],
deps = [
":mem_usage",
":value_store",
":yaml",
"//common:check",
"//common:hashing",
"//common:set",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "canonical_value_store",
hdrs = ["canonical_value_store.h"],
deps = [
":mem_usage",
":value_store",
":value_store_types",
":yaml",
"//common:hashtable_key_context",
"//common:set",
],
)
cc_test(
name = "canonical_value_store_test",
size = "small",
srcs = ["canonical_value_store_test.cpp"],
deps = [
":canonical_value_store",
":value_ids",
"//testing/base:gtest_main",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "clang_invocation",
srcs = ["clang_invocation.cpp"],
hdrs = ["clang_invocation.h"],
deps = [
"//common:check",
"//toolchain/diagnostics:diagnostic_emitter",
"@llvm-project//clang:basic",
"@llvm-project//clang:frontend",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "fixed_size_value_store",
hdrs = ["fixed_size_value_store.h"],
deps = [
":mem_usage",
":value_store",
":value_store_types",
"//common:check",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "for_each_macro",
hdrs = ["for_each_macro.h"],
deps = [
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "index_base",
hdrs = ["index_base.h"],
deps = [
"//common:ostream",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "kind_switch",
hdrs = ["kind_switch.h"],
deps = [
":for_each_macro",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "kind_switch_test",
size = "small",
srcs = ["kind_switch_test.cpp"],
deps = [
":kind_switch",
"//common:raw_string_ostream",
"//testing/base:gtest_main",
"@googletest//:gtest",
],
)
cc_library(
name = "mem_usage",
hdrs = ["mem_usage.h"],
deps = [
":yaml",
"//common:map",
"//common:set",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "relational_value_store",
hdrs = ["relational_value_store.h"],
deps = [
":value_store_types",
"//common:check",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "timings",
hdrs = ["timings.h"],
deps = [
":yaml",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "value_ids",
hdrs = ["value_ids.h"],
deps = [
":index_base",
"//common:check",
"//common:ostream",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "value_store",
hdrs = ["value_store.h"],
deps = [
":mem_usage",
":value_store_types",
":yaml",
"//common:check",
"//common:hashtable_key_context",
"//common:ostream",
"//common:set",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "value_store_test",
size = "small",
srcs = ["value_store_test.cpp"],
deps = [
":value_ids",
":value_store",
"//common:raw_string_ostream",
"//testing/base:gtest_main",
"@googletest//:gtest",
],
)
cc_library(
name = "value_store_types",
hdrs = ["value_store_types.h"],
deps = [
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "int",
srcs = ["int.cpp"],
hdrs = ["int.h"],
deps = [
":canonical_value_store",
":index_base",
":mem_usage",
":value_store",
":yaml",
"//common:check",
"//common:hashtable_key_context",
"//common:ostream",
"//common:set",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "int_test",
size = "small",
srcs = ["int_test.cpp"],
deps = [
":int",
"//common:raw_string_ostream",
"//testing/base:gtest_main",
"//toolchain/testing:yaml_test_helpers",
"@googletest//:gtest",
],
)
generate_llvm_tools_def(
name = "llvm_tools_def",
out = "llvm_tools.def",
)
config_setting(
name = "is_macos",
constraint_values = ["@platforms//os:macos"],
)
cc_library(
name = "llvm_tools",
srcs = ["llvm_tools.cpp"],
hdrs = ["llvm_tools.h"],
linkopts = select({
# TODO: This should be moved upstream to LLVM's tool libraries that
# require it.
":is_macos": [
"-framework",
"CoreFoundation",
],
"//conditions:default": [],
}),
deps = [
":llvm_tools_def",
"//common:command_line",
"//common:enum_base",
"@llvm-project//llvm:Support",
] + [info.lib for info in LLVM_MAIN_TOOLS.values()],
)
generate_runtime_sources_cc_library(name = "runtime_sources")
cc_library(
name = "shared_value_stores",
hdrs = ["shared_value_stores.h"],
deps = [
":canonical_value_store",
":int",
":mem_usage",
":value_ids",
":value_store",
":yaml",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "shared_value_stores_test",
size = "small",
srcs = ["shared_value_stores_test.cpp"],
deps = [
":shared_value_stores",
"//common:raw_string_ostream",
"//testing/base:gtest_main",
"//toolchain/testing:yaml_test_helpers",
"@googletest//:gtest",
],
)
cc_library(
name = "yaml",
hdrs = ["yaml.h"],
deps = [
"//common:check",
"//common:ostream",
"@llvm-project//llvm:Support",
],
)