Skip to content

Commit

Permalink
Rename global variable relations
Browse files Browse the repository at this point in the history
This is the beginning of a series of commits that will try to unify the naming
conventions of the Fact Generator and Datalog code. It's important to undertake
this while the git history of the project is relatively short, so as to not hide
valuable history by doing it later.

At the same time, this set of changes will endeavor to make consistent,
reasonable trade-offs between identifier clarity and brevity, and document these
choices.
  • Loading branch information
langston-barrett committed Oct 6, 2022
1 parent d96ff8d commit 68b067c
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 108 deletions.
30 changes: 15 additions & 15 deletions FactGenerator/include/predicates.inc
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ PREDICATE(basic_block, predecessor, _basicblock_preds)
GROUP_END(basic_block)

GROUP_BEGIN(global_var)
PREDICATE(global_var, id, global_variable)
PREDICATE(global_var, name, global_variable_has_name)
PREDICATE(global_var, unmangl_name, global_variable_has_unmangled_name)
PREDICATE(global_var, type, global_variable_has_type)
PREDICATE(global_var, initializer, global_variable_has_initializer)
PREDICATE(global_var, section, global_variable_in_section)
PREDICATE(global_var, align, global_variable_aligned_to)
PREDICATE(global_var, flag, global_variable_has_flag)
PREDICATE(global_var, linkage, global_variable_has_linkage_type)
PREDICATE(global_var, visibility, global_variable_has_visibility)
PREDICATE(global_var, threadlocal_mode, global_variable_in_threadlocal_mode)
PREDICATE(global_var, id, global_var)
PREDICATE(global_var, name, global_var_name)
PREDICATE(global_var, demangled_name, global_var_demangled_name)
PREDICATE(global_var, type, global_var_type)
PREDICATE(global_var, initializer, global_var_initializer)
PREDICATE(global_var, section, global_var_in_section)
PREDICATE(global_var, align, global_var_aligned_to)
PREDICATE(global_var, flag, global_var_flag)
PREDICATE(global_var, linkage, global_var_linkage_type)
PREDICATE(global_var, visibility, global_var_visibility)
PREDICATE(global_var, threadlocal_mode, global_var_threadlocal_mode)
GROUP_END(global_var)

GROUP_BEGIN(alias)
Expand Down Expand Up @@ -555,10 +555,10 @@ PREDICATE(function_constant, id, function_constant)
PREDICATE(function_constant, name, function_constant_fn_name)
GROUP_END(function_constant)

GROUP_BEGIN(global_variable_constant)
PREDICATE(global_variable_constant, id, global_variable_constant)
PREDICATE(global_variable_constant, name, global_variable_constant_name)
GROUP_END(global_variable_constant)
GROUP_BEGIN(global_var_constant)
PREDICATE(global_var_constant, id, global_var_constant)
PREDICATE(global_var_constant, name, global_var_constant_name)
GROUP_END(global_var_constant)

GROUP_BEGIN(constant_array)
PREDICATE(constant_array, id, constant_array)
Expand Down
4 changes: 2 additions & 2 deletions FactGenerator/src/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ auto FactGenerator::writeConstant(const llvm::Constant &c)
const auto &global_var = cast<GlobalVariable>(c);
const std::string varname = "@" + global_var.getName().str();

writeFact(pred::global_variable_constant::id, id);
writeFact(pred::global_variable_constant::name, id, varname);
writeFact(pred::global_var_constant::id, id);
writeFact(pred::global_var_constant::name, id, varname);
} else if (isa<ConstantExpr>(c)) {
writeConstantExpr(cast<ConstantExpr>(c), id);
} else if (isa<ConstantArray>(c)) {
Expand Down
5 changes: 3 additions & 2 deletions FactGenerator/src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ void FactGenerator::writeGlobalVar(
refmode_t varType = recordType(gv.getType()->getElementType());
refmode_t thrLocMode = refmode(gv.getThreadLocalMode());

// Record unmangled variable name
writeFact(pred::global_var::unmangl_name, id, demangle(gv.getName().data()));
// Record demangled variable name
writeFact(
pred::global_var::demangled_name, id, demangle(gv.getName().data()));

// Record external linkage
if (!gv.hasInitializer() && gv.hasExternalLinkage())
Expand Down
32 changes: 16 additions & 16 deletions datalog/export/debug-output-extended.dl
Original file line number Diff line number Diff line change
Expand Up @@ -438,21 +438,21 @@
.output global_allocation_by_name (compress=true)
.output global_allocation_by_variable (compress=true)
.output global_region (compress=true)
.output global_variable (compress=true)
.output global_variable_aligned_to (compress=true)
.output global_variable_constant (compress=true)
.output global_variable_constant_name (compress=true)
.output global_variable_constant_reference (compress=true)
.output global_variable_has_flag (compress=true)
.output global_variable_has_initializer (compress=true)
.output global_variable_has_linkage_type (compress=true)
.output global_variable_has_name (compress=true)
.output global_variable_has_type (compress=true)
.output global_variable_has_unmangled_name (compress=true)
.output global_variable_has_visibility (compress=true)
.output global_variable_in_section (compress=true)
.output global_variable_in_threadlocal_mode (compress=true)
.output global_variable_is_constant (compress=true)
.output global_var (compress=true)
.output global_var_aligned_to (compress=true)
.output global_var_constant (compress=true)
.output global_var_constant_name (compress=true)
.output global_var_constant_reference (compress=true)
.output global_var_flag (compress=true)
.output global_var_initializer (compress=true)
.output global_var_linkage_type (compress=true)
.output global_var_name (compress=true)
.output global_var_type (compress=true)
.output global_var_demangled_name (compress=true)
.output global_var_visibility (compress=true)
.output global_var_in_section (compress=true)
.output global_var_in_threadlocal_mode (compress=true)
.output global_var_is_constant (compress=true)
.output half_type (compress=true)
.output heap_alloc_function (compress=true)
.output heap_allocation (compress=true)
Expand Down Expand Up @@ -685,7 +685,7 @@
.output schema_invalid_alias (compress=true)
.output schema_invalid_constant (compress=true)
.output schema_invalid_function (compress=true)
.output schema_invalid_global_variable (compress=true)
.output schema_invalid_global_var (compress=true)
.output schema_invalid_instruction (compress=true)
.output schema_invalid_type (compress=true)
.output schema_multiple_function_names (compress=true)
Expand Down
6 changes: 3 additions & 3 deletions datalog/export/debug-output.dl
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@
.output global_allocation_by_name (compress=true)
.output global_allocation_by_variable (compress=true)
.output global_region (compress=true)
.output global_variable_constant_reference (compress=true)
.output global_variable_is_constant (compress=true)
.output global_var_constant_reference (compress=true)
.output global_var_is_constant (compress=true)
.output half_type (compress=true)
.output heap_alloc_function (compress=true)
.output heap_allocation (compress=true)
Expand Down Expand Up @@ -424,7 +424,7 @@
.output schema_invalid_alias (compress=true)
.output schema_invalid_constant (compress=true)
.output schema_invalid_function (compress=true)
.output schema_invalid_global_variable (compress=true)
.output schema_invalid_global_var (compress=true)
.output schema_invalid_instruction (compress=true)
.output schema_invalid_type (compress=true)
.output schema_multiple_function_names (compress=true)
Expand Down
2 changes: 1 addition & 1 deletion datalog/points-to/allocations-decl.dl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ ptr_compatible_allocation(?alloc) :-
// would change their contents
ptr_compatible_allocation(?alloc) :-
global_allocation_by_variable(?globalVar,?alloc),
! global_variable_constant(?globalVar).
! global_var_constant(?globalVar).

//-------------------------------------
// Additional pseudo-allocation types
Expand Down
8 changes: 4 additions & 4 deletions datalog/points-to/allocations-globals.dl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
global_allocation(?alloc),
global_allocation_by_variable(?gVar, ?alloc)
:-
global_variable(?gVar),
global_variable_has_name(?gVar, ?name),
global_var(?gVar),
global_var_name(?gVar, ?name),
?alloc = cat("*global_alloc",?name).

/// 2. Function Allocations
Expand All @@ -25,7 +25,7 @@ global_allocation_by_function(?func, ?alloc)
?alloc = cat("*global_alloc",?name).

global_allocation_by_name(?globalName, ?alloc) :-
global_variable_has_name(?global, ?globalName),
global_var_name(?global, ?globalName),
global_allocation_by_variable(?global, ?alloc).

global_allocation_by_name(?globalName, ?alloc) :-
Expand All @@ -38,4 +38,4 @@ static_allocation_type(?alloc, ?type) :-

static_allocation_type(?alloc, ?type) :-
global_allocation_by_variable(?gVar, ?alloc),
global_variable_has_type(?gVar, ?type).
global_var_type(?gVar, ?type).
4 changes: 2 additions & 2 deletions datalog/points-to/allocations-sizes.dl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ array_allocation(?alloc) :-

allocation_size(?globalAlloc, ?n) :-
global_allocation_by_variable(?globalVar, ?globalAlloc),
global_variable_has_linkage_type(?globalVar, ?linkage),
global_var_linkage_type(?globalVar, ?linkage),
! external_linkage_type(?linkage),
static_allocation_type(?globalAlloc, ?type),
type_has_size(?type, ?n),
Expand All @@ -23,7 +23,7 @@ allocation_size(?globalAlloc, ?n) :-
// The sizes of external globals may not match the size of their types.
dynamically_sized_alloc(?alloc) :-
external_linkage_type(?externalLinkage),
global_variable_has_linkage_type(?globalVar, ?externalLinkage),
global_var_linkage_type(?globalVar, ?externalLinkage),
global_allocation_by_variable(?globalVar, ?alloc).

//-----------------------
Expand Down
14 changes: 7 additions & 7 deletions datalog/points-to/class-type.dl
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ class_type_name(?type, ?name) :-

.decl class_type_typeinfo(?classType: ClassType, ?typeInfo: GlobalVariable)

// Extract class name from unmangled type info name. The latter is of
// Extract class name from demangled type info name. The latter is of
// the form: typeinfo for CLASS-NAME
.decl _typeinfo_class_name(?typeInfo: GlobalVariable, ?className: symbol)
_typeinfo_class_name(?typeInfo, ?className) :-
typeinfo(?typeInfo),
global_variable_has_unmangled_name(?typeInfo, ?tIName),
global_var_demangled_name(?typeInfo, ?tIName),
contains("typeinfo for ", ?tIName),
?n = strlen(?tIName),
?className = substr(?tIName, 13, ?n - 13).
Expand All @@ -81,15 +81,15 @@ class_type_typeinfo(?classType, ?typeInfo) :-
class_type_name(?classType, ?className),
_typeinfo_by_class_name(?className, ?typeInfo).

// Extract class name from unmangled vtable name. The latter is of the
// Extract class name from demangled vtable name. The latter is of the
// form: vtable for CLASS-NAME
.decl _vtable_class_name(?vt: GlobalVariable, ?className: symbol)
_vtable_class_name(?vt, ?className) :-
vtable(?vt, _),
global_variable_has_unmangled_name(?vt, ?unmangledName),
contains("vtable for ", ?unmangledName),
?n = strlen(?unmangledName),
?className = substr(?unmangledName, 11, ?n - 11).
global_var_demangled_name(?vt, ?demangledName),
contains("vtable for ", ?demangledName),
?n = strlen(?demangledName),
?className = substr(?demangledName, 11, ?n - 11).

.decl _vtable_by_class_name(?className: symbol, ?vt: GlobalVariable)
_vtable_by_class_name(?className, ?vt) :-
Expand Down
4 changes: 2 additions & 2 deletions datalog/points-to/constant-init.dl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
.decl initialized_by_constant(?alloc: Allocation, ?constant: Constant)

initialized_by_constant(?baseAlloc, ?init) :-
global_variable_has_initializer(?var, ?init),
global_var_initializer(?var, ?init),
global_allocation_by_variable(?var, ?baseAlloc).

initialized_by_constant(?alloc, ?init) :-
global_variable_has_initializer(?var, ?init),
global_var_initializer(?var, ?init),
global_allocation_by_variable(?var, ?baseAlloc),
static_subobjects.alloc_subregion_at_array_index(?baseAlloc, 0, ?alloc).

Expand Down
4 changes: 2 additions & 2 deletions datalog/points-to/constant-points-to.dl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ constant_points_to(?constant, ?null) :-
// allocation.

constant_points_to(?constant, ?alloc) :-
global_variable_constant_name(?constant, ?name),
global_variable_has_name(?gVar, ?name),
global_var_constant_name(?constant, ?name),
global_var_name(?gVar, ?name),
global_allocation_by_variable(?gVar, ?alloc).

constant_points_to(?constant, ?alloc) :-
Expand Down
4 changes: 2 additions & 2 deletions datalog/points-to/cplusplus-exceptions.dl
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ _type_info_by_alloc(?obj, ?excTypeInfo) :-
typeinfo(?var),
external_typeinfo(?var) :-
external_linkage_type(?externalLinkage),
global_variable_has_linkage_type(?var, ?externalLinkage),
global_variable_has_name(?var, ?name),
global_var_linkage_type(?var, ?externalLinkage),
global_var_name(?var, ?name),
substr(?name, 0, strlen("@_ZTI")) = "@_ZTI".

.comp ThrowInstruction {
Expand Down
2 changes: 1 addition & 1 deletion datalog/points-to/cplusplus.dl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
.decl cxx_new_function(?func: FunctionDecl)

// Consider only functions with a single parameter of integer type, no
// definition, and whose unmangled name contains the keyword `new`
// definition, and whose demangled name contains the keyword `new`

cxx_new_function(?func),
heap_alloc_function(?func)
Expand Down
20 changes: 10 additions & 10 deletions datalog/points-to/virtual-tables.dl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ vtable(?vt, ?name) :-

typeinfo_vtable(?vt, ?name) :-
external_linkage_type(?externalLinkage),
global_variable_has_linkage_type(?vt, ?externalLinkage),
global_variable_has_name(?vt, ?name),
global_var_linkage_type(?vt, ?externalLinkage),
global_var_name(?vt, ?name),
contains("class_type_info", ?name),
contains("cxxabi", ?name).

Expand All @@ -51,7 +51,7 @@ typeinfo_vtable_multiple_inheritance(?vt, ?name) :-

.decl constant_contains_vptr(?vt: VirtualTable, ?constant: Constant)
constant_contains_vptr(?vt, ?constant) :-
global_variable_constant_name(?constant, ?name),
global_var_constant_name(?constant, ?name),
vtable(?vt, ?name).

constant_contains_vptr(?vt, ?outer) :-
Expand All @@ -74,7 +74,7 @@ typeinfo(?typeInfo),
typeinfo_has_vtable(?typeInfo,?vt) :-
constant_contains_vptr(?vt, ?initializer),
typeinfo_vtable(?vt, _),
global_variable_has_initializer(?typeInfo, ?initializer).
global_var_initializer(?typeInfo, ?initializer).

.decl typeinfo_baseclass(?typeInfo: GlobalVariable)
typeinfo_baseclass(?typeInfo) :-
Expand All @@ -96,7 +96,7 @@ typeinfo_multiple_inheritance(?typeInfo) :-

.decl _constant_contains_typeinfo(?typeInfo: GlobalVariable, ?constant:Constant)
_constant_contains_typeinfo(?typeInfo, ?constant) :-
global_variable_constant_reference(?constant, ?typeInfo),
global_var_constant_reference(?constant, ?typeInfo),
typeinfo(?typeInfo).

_constant_contains_typeinfo(?typeinfo, ?outer) :-
Expand All @@ -105,8 +105,8 @@ _constant_contains_typeinfo(?typeinfo, ?outer) :-

vtable(?vt, ?name),
vtable_typeinfo(?vt,?typeinfo) :-
global_variable_has_name(?vt, ?name),
global_variable_has_initializer(?vt, ?initializer), // get array constant of vtable
global_var_name(?vt, ?name),
global_var_initializer(?vt, ?initializer), // get array constant of vtable
constant_array_index(?initializer, 1, ?tic), // type info is at index 1
_constant_contains_typeinfo(?typeinfo, ?tic).

Expand All @@ -116,7 +116,7 @@ vtable_typeinfo(?vt,?typeinfo) :-
.decl _typeinfo_contains_typeinfo(?outer: GlobalVariable, ?index:number, ?inner: GlobalVariable)
_typeinfo_contains_typeinfo(?outer, ?index, ?inner) :-
typeinfo(?outer),
global_variable_has_initializer(?outer, ?init),
global_var_initializer(?outer, ?init),
constant_struct_index(?init, ?index, ?tic),
_constant_contains_typeinfo(?inner, ?tic).

Expand All @@ -131,7 +131,7 @@ typeinfo_base_class(?typeinfo, 0, ?baseTypeinfo) :-
typeinfo_base_class(?typeinfo, ?offset, ?baseTypeinfo) :-
typeinfo_multiple_inheritance(?typeinfo),
_typeinfo_contains_typeinfo(?typeinfo, ?index, ?baseTypeinfo),
global_variable_has_initializer(?typeinfo, ?init),
global_var_initializer(?typeinfo, ?init),
constant_struct_index(?init, ?index + 1, ?constant),
constant_to_int(?constant, ?offsetMask),
?offset = ?offsetMask / 256.
Expand All @@ -143,7 +143,7 @@ typeinfo_base_class(?typeinfo, ?offset, ?baseTypeinfo) :-

vtable_function(?vt,?index,?func) :-
vtable(?vt, _),
global_variable_has_initializer(?vt,?init),
global_var_initializer(?vt,?init),
constant_array_index(?init, ?index, ?funcAsInteger),
bitcast_constant_expression_from(?funcAsInteger, ?funcConstant),
function_constant_fn_name(?funcConstant, ?funcName),
Expand Down
Loading

0 comments on commit 68b067c

Please sign in to comment.