Skip to content

Commit

Permalink
-obfuscate-source-code-locations on bounds checks and type assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
laytan committed Feb 5, 2025
1 parent 919e9a8 commit b77430b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 43 deletions.
8 changes: 2 additions & 6 deletions src/llvm_backend_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3364,9 +3364,7 @@ gb_internal lbValue lb_build_unary_and(lbProcedure *p, Ast *expr) {
auto args = array_make<lbValue>(permanent_allocator(), arg_count);
args[0] = ok;

args[1] = lb_find_or_add_entity_string(p->module, get_file_path_string(pos.file_id));
args[2] = lb_const_int(p->module, t_i32, pos.line);
args[3] = lb_const_int(p->module, t_i32, pos.column);
lb_set_file_line_col(p, array_slice(args, 1, args.count), pos);

if (!build_context.no_rtti) {
args[4] = lb_typeid(p->module, src_type);
Expand All @@ -3393,9 +3391,7 @@ gb_internal lbValue lb_build_unary_and(lbProcedure *p, Ast *expr) {
auto args = array_make<lbValue>(permanent_allocator(), 6);
args[0] = ok;

args[1] = lb_find_or_add_entity_string(p->module, get_file_path_string(pos.file_id));
args[2] = lb_const_int(p->module, t_i32, pos.line);
args[3] = lb_const_int(p->module, t_i32, pos.column);
lb_set_file_line_col(p, array_slice(args, 1, args.count), pos);

args[4] = any_id;
args[5] = id;
Expand Down
52 changes: 21 additions & 31 deletions src/llvm_backend_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ gb_global isize lb_global_type_info_member_offsets_index = 0;
gb_global isize lb_global_type_info_member_usings_index = 0;
gb_global isize lb_global_type_info_member_tags_index = 0;


gb_internal void lb_init_module(lbModule *m, Checker *c) {
m->info = &c->info;

Expand Down Expand Up @@ -540,6 +539,22 @@ gb_internal lbValue lb_build_addr_ptr(lbProcedure *p, Ast *expr) {
return lb_addr_get_ptr(p, addr);
}

gb_internal void lb_set_file_line_col(lbProcedure *p, Array<lbValue> arr, TokenPos pos) {
String file = get_file_path_string(pos.file_id);
i32 line = pos.line;
i32 col = pos.column;

if (build_context.obfuscate_source_code_locations) {
file = obfuscate_string(file, "F");
line = obfuscate_i32(line);
col = obfuscate_i32(col);
}

arr[0] = lb_find_or_add_entity_string(p->module, file);
arr[1] = lb_const_int(p->module, t_i32, line);
arr[2] = lb_const_int(p->module, t_i32, col);
}

gb_internal void lb_emit_bounds_check(lbProcedure *p, Token token, lbValue index, lbValue len) {
if (build_context.no_bounds_check) {
return;
Expand All @@ -553,14 +568,8 @@ gb_internal void lb_emit_bounds_check(lbProcedure *p, Token token, lbValue index
index = lb_emit_conv(p, index, t_int);
len = lb_emit_conv(p, len, t_int);

lbValue file = lb_find_or_add_entity_string(p->module, get_file_path_string(token.pos.file_id));
lbValue line = lb_const_int(p->module, t_i32, token.pos.line);
lbValue column = lb_const_int(p->module, t_i32, token.pos.column);

auto args = array_make<lbValue>(temporary_allocator(), 5);
args[0] = file;
args[1] = line;
args[2] = column;
lb_set_file_line_col(p, args, token.pos);
args[3] = index;
args[4] = len;

Expand All @@ -582,14 +591,8 @@ gb_internal void lb_emit_matrix_bounds_check(lbProcedure *p, Token token, lbValu
row_count = lb_emit_conv(p, row_count, t_int);
column_count = lb_emit_conv(p, column_count, t_int);

lbValue file = lb_find_or_add_entity_string(p->module, get_file_path_string(token.pos.file_id));
lbValue line = lb_const_int(p->module, t_i32, token.pos.line);
lbValue column = lb_const_int(p->module, t_i32, token.pos.column);

auto args = array_make<lbValue>(temporary_allocator(), 7);
args[0] = file;
args[1] = line;
args[2] = column;
lb_set_file_line_col(p, args, token.pos);
args[3] = row_index;
args[4] = column_index;
args[5] = row_count;
Expand All @@ -610,14 +613,8 @@ gb_internal void lb_emit_multi_pointer_slice_bounds_check(lbProcedure *p, Token
low = lb_emit_conv(p, low, t_int);
high = lb_emit_conv(p, high, t_int);

lbValue file = lb_find_or_add_entity_string(p->module, get_file_path_string(token.pos.file_id));
lbValue line = lb_const_int(p->module, t_i32, token.pos.line);
lbValue column = lb_const_int(p->module, t_i32, token.pos.column);

auto args = array_make<lbValue>(permanent_allocator(), 5);
args[0] = file;
args[1] = line;
args[2] = column;
lb_set_file_line_col(p, args, token.pos);
args[3] = low;
args[4] = high;

Expand All @@ -632,16 +629,11 @@ gb_internal void lb_emit_slice_bounds_check(lbProcedure *p, Token token, lbValue
return;
}

lbValue file = lb_find_or_add_entity_string(p->module, get_file_path_string(token.pos.file_id));
lbValue line = lb_const_int(p->module, t_i32, token.pos.line);
lbValue column = lb_const_int(p->module, t_i32, token.pos.column);
high = lb_emit_conv(p, high, t_int);

if (!lower_value_used) {
auto args = array_make<lbValue>(permanent_allocator(), 5);
args[0] = file;
args[1] = line;
args[2] = column;
lb_set_file_line_col(p, args, token.pos);
args[3] = high;
args[4] = len;

Expand All @@ -651,9 +643,7 @@ gb_internal void lb_emit_slice_bounds_check(lbProcedure *p, Token token, lbValue
low = lb_emit_conv(p, low, t_int);

auto args = array_make<lbValue>(permanent_allocator(), 6);
args[0] = file;
args[1] = line;
args[2] = column;
lb_set_file_line_col(p, args, token.pos);
args[3] = low;
args[4] = high;
args[5] = len;
Expand Down
8 changes: 2 additions & 6 deletions src/llvm_backend_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,9 +771,7 @@ gb_internal lbValue lb_emit_union_cast(lbProcedure *p, lbValue value, Type *type
auto args = array_make<lbValue>(permanent_allocator(), arg_count);
args[0] = ok;

args[1] = lb_const_string(m, get_file_path_string(pos.file_id));
args[2] = lb_const_int(m, t_i32, pos.line);
args[3] = lb_const_int(m, t_i32, pos.column);
lb_set_file_line_col(p, array_slice(args, 1, args.count), pos);

if (!build_context.no_rtti) {
args[4] = lb_typeid(m, src_type);
Expand Down Expand Up @@ -847,9 +845,7 @@ gb_internal lbAddr lb_emit_any_cast_addr(lbProcedure *p, lbValue value, Type *ty
auto args = array_make<lbValue>(permanent_allocator(), arg_count);
args[0] = ok;

args[1] = lb_const_string(m, get_file_path_string(pos.file_id));
args[2] = lb_const_int(m, t_i32, pos.line);
args[3] = lb_const_int(m, t_i32, pos.column);
lb_set_file_line_col(p, array_slice(args, 1, args.count), pos);

if (!build_context.no_rtti) {
args[4] = any_typeid;
Expand Down

0 comments on commit b77430b

Please sign in to comment.