mirror of
https://github.com/odin-lang/Odin.git
synced 2026-06-03 17:24:39 +00:00
Add -obfuscate-source-code-locations
This commit is contained in:
@@ -381,6 +381,8 @@ struct BuildContext {
|
||||
|
||||
bool dynamic_map_calls;
|
||||
|
||||
bool obfuscate_source_code_locations;
|
||||
|
||||
RelocMode reloc_mode;
|
||||
bool disable_red_zone;
|
||||
|
||||
|
||||
@@ -287,11 +287,44 @@ gb_internal lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type
|
||||
return lb_const_value(m, t, tv.value);
|
||||
}
|
||||
|
||||
gb_internal lbValue lb_const_source_code_location_const(lbModule *m, String const &procedure, TokenPos const &pos) {
|
||||
gb_internal String lb_obfuscate_string(String const &s, char const *prefix) {
|
||||
if (s.len == 0) {
|
||||
return {};
|
||||
}
|
||||
GB_ASSERT(prefix != nullptr);
|
||||
u64 hash = gb_fnv64a(s.text, s.len);
|
||||
gbString res = gb_string_make(temporary_allocator(), prefix);
|
||||
res = gb_string_append_fmt(res, "x%llx", cast(long long unsigned)hash);
|
||||
return make_string_c(res);
|
||||
}
|
||||
|
||||
gb_internal i32 lb_obfuscate_i32(i32 i) {
|
||||
i32 x = cast(i32)gb_fnv64a(&i, sizeof(i));
|
||||
if (x < 0) {
|
||||
x = 1-x;
|
||||
}
|
||||
return cast(i32)x;
|
||||
}
|
||||
|
||||
gb_internal lbValue lb_const_source_code_location_const(lbModule *m, String const &procedure_, TokenPos const &pos) {
|
||||
String file = get_file_path_string(pos.file_id);
|
||||
String procedure = procedure_;
|
||||
|
||||
i32 line = pos.line;
|
||||
i32 column = pos.column;
|
||||
|
||||
if (build_context.obfuscate_source_code_locations) {
|
||||
file = lb_obfuscate_string(file, "F");
|
||||
procedure = lb_obfuscate_string(procedure, "P");
|
||||
|
||||
line = lb_obfuscate_i32(line);
|
||||
column = lb_obfuscate_i32(column);
|
||||
}
|
||||
|
||||
LLVMValueRef fields[4] = {};
|
||||
fields[0]/*file*/ = lb_find_or_add_entity_string(m, get_file_path_string(pos.file_id)).value;
|
||||
fields[1]/*line*/ = lb_const_int(m, t_i32, pos.line).value;
|
||||
fields[2]/*column*/ = lb_const_int(m, t_i32, pos.column).value;
|
||||
fields[0]/*file*/ = lb_find_or_add_entity_string(m, file).value;
|
||||
fields[1]/*line*/ = lb_const_int(m, t_i32, line).value;
|
||||
fields[2]/*column*/ = lb_const_int(m, t_i32, column).value;
|
||||
fields[3]/*procedure*/ = lb_find_or_add_entity_string(m, procedure).value;
|
||||
|
||||
lbValue res = {};
|
||||
|
||||
12
src/main.cpp
12
src/main.cpp
@@ -277,6 +277,7 @@ enum BuildFlagKind {
|
||||
BuildFlag_ForeignErrorProcedures,
|
||||
BuildFlag_NoRTTI,
|
||||
BuildFlag_DynamicMapCalls,
|
||||
BuildFlag_ObfuscateSourceCodeLocations,
|
||||
|
||||
BuildFlag_Compact,
|
||||
BuildFlag_GlobalDefinitions,
|
||||
@@ -467,6 +468,8 @@ gb_internal bool parse_build_flags(Array<String> args) {
|
||||
|
||||
add_flag(&build_flags, BuildFlag_DynamicMapCalls, str_lit("dynamic-map-calls"), BuildFlagParam_None, Command__does_check);
|
||||
|
||||
add_flag(&build_flags, BuildFlag_ObfuscateSourceCodeLocations, str_lit("obfuscate-source-code-locations"), BuildFlagParam_None, Command__does_build);
|
||||
|
||||
add_flag(&build_flags, BuildFlag_Short, str_lit("short"), BuildFlagParam_None, Command_doc);
|
||||
add_flag(&build_flags, BuildFlag_AllPackages, str_lit("all-packages"), BuildFlagParam_None, Command_doc);
|
||||
add_flag(&build_flags, BuildFlag_DocFormat, str_lit("doc-format"), BuildFlagParam_None, Command_doc);
|
||||
@@ -1113,6 +1116,11 @@ gb_internal bool parse_build_flags(Array<String> args) {
|
||||
case BuildFlag_DynamicMapCalls:
|
||||
build_context.dynamic_map_calls = true;
|
||||
break;
|
||||
|
||||
case BuildFlag_ObfuscateSourceCodeLocations:
|
||||
build_context.obfuscate_source_code_locations = true;
|
||||
break;
|
||||
|
||||
case BuildFlag_DefaultToNilAllocator:
|
||||
build_context.ODIN_DEFAULT_TO_NIL_ALLOCATOR = true;
|
||||
break;
|
||||
@@ -1947,6 +1955,10 @@ gb_internal void print_show_help(String const arg0, String const &command) {
|
||||
}
|
||||
|
||||
if (run_or_build) {
|
||||
print_usage_line(1, "-obfuscate-source-code-locations");
|
||||
print_usage_line(2, "Obfuscate the file and procedure strings, and line and column numbers, stored with a 'runtime.Source_Code_Location' value.");
|
||||
print_usage_line(0, "");
|
||||
|
||||
print_usage_line(1, "-sanitize:<string>");
|
||||
print_usage_line(2, "Enables sanitization analysis.");
|
||||
print_usage_line(2, "Available options:");
|
||||
|
||||
@@ -1982,16 +1982,25 @@ gb_internal bool cg_switch_stmt_can_be_trivial_jump_table(AstSwitchStmt *ss) {
|
||||
if (ss->tag == nullptr) {
|
||||
return false;
|
||||
}
|
||||
enum { DISALLOW_64_SWITCH = true };
|
||||
|
||||
bool is_typeid = false;
|
||||
TypeAndValue tv = type_and_value_of_expr(ss->tag);
|
||||
if (is_type_integer(core_type(tv.type))) {
|
||||
if (type_size_of(tv.type) > 8) {
|
||||
i64 sz = type_size_of(tv.type);
|
||||
if (sz > 8) {
|
||||
return false;
|
||||
}
|
||||
if (DISALLOW_64_SWITCH && sz == 8) {
|
||||
return false;
|
||||
}
|
||||
// okay
|
||||
} else if (is_type_typeid(tv.type)) {
|
||||
// okay
|
||||
is_typeid = true;
|
||||
if (DISALLOW_64_SWITCH && build_context.ptr_size == 8) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -475,6 +475,13 @@ gb_internal void cg_setup_type_info_data(cgModule *m) {
|
||||
// }
|
||||
tag_type = t_type_info_named;
|
||||
|
||||
i64 name_offset = type_offset_of(tag_type, 0);
|
||||
String name = t->Named.type_name->token.string;
|
||||
cg_global_const_string(m, name, t_string, global, offset+name_offset);
|
||||
|
||||
i64 base_offset = type_offset_of(tag_type, 1);
|
||||
cg_global_const_type_info_ptr(m, t->Named.base, global, offset+base_offset);
|
||||
|
||||
if (t->Named.type_name->pkg) {
|
||||
i64 pkg_offset = type_offset_of(tag_type, 2);
|
||||
String pkg_name = t->Named.type_name->pkg->name;
|
||||
@@ -495,8 +502,6 @@ gb_internal void cg_setup_type_info_data(cgModule *m) {
|
||||
TokenPos pos = t->Named.type_name->token.pos;
|
||||
cg_global_source_code_location_const(m, proc_name, pos, global, offset+loc_offset);
|
||||
|
||||
i64 base_offset = type_offset_of(tag_type, 1);
|
||||
cg_global_const_type_info_ptr(m, t->Named.base, global, offset+base_offset);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user