Browse Source

Obfuscate `#file` and `#procedure` when `-obfuscate-source-code-locations` is enabled

gingerBill 1 year ago
parent
commit
810cf22e5d
3 changed files with 25 additions and 15 deletions
  1. 10 2
      src/check_expr.cpp
  2. 13 0
      src/common.cpp
  3. 2 13
      src/llvm_backend_const.cpp

+ 10 - 2
src/check_expr.cpp

@@ -8157,8 +8157,12 @@ gb_internal ExprKind check_basic_directive_expr(CheckerContext *c, Operand *o, A
 	o->mode = Addressing_Constant;
 	o->mode = Addressing_Constant;
 	String name = bd->name.string;
 	String name = bd->name.string;
 	if (name == "file") {
 	if (name == "file") {
+		String file = get_file_path_string(bd->token.pos.file_id);
+		if (build_context.obfuscate_source_code_locations) {
+			file = obfuscate_string(file, "F");
+		}
 		o->type = t_untyped_string;
 		o->type = t_untyped_string;
-		o->value = exact_value_string(get_file_path_string(bd->token.pos.file_id));
+		o->value = exact_value_string(file);
 	} else if (name == "line") {
 	} else if (name == "line") {
 		o->type = t_untyped_integer;
 		o->type = t_untyped_integer;
 		o->value = exact_value_i64(bd->token.pos.line);
 		o->value = exact_value_i64(bd->token.pos.line);
@@ -8168,8 +8172,12 @@ gb_internal ExprKind check_basic_directive_expr(CheckerContext *c, Operand *o, A
 			o->type = t_untyped_string;
 			o->type = t_untyped_string;
 			o->value = exact_value_string(str_lit(""));
 			o->value = exact_value_string(str_lit(""));
 		} else {
 		} else {
+			String p = c->proc_name;
+			if (build_context.obfuscate_source_code_locations) {
+				p = obfuscate_string(p, "P");
+			}
 			o->type = t_untyped_string;
 			o->type = t_untyped_string;
-			o->value = exact_value_string(c->proc_name);
+			o->value = exact_value_string(p);
 		}
 		}
 	} else if (name == "caller_location") {
 	} else if (name == "caller_location") {
 		init_core_source_code_location(c->checker);
 		init_core_source_code_location(c->checker);

+ 13 - 0
src/common.cpp

@@ -353,6 +353,19 @@ gb_global bool global_module_path_set = false;
 #include "thread_pool.cpp"
 #include "thread_pool.cpp"
 
 
 
 
+gb_internal String 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);
+}
+
+
+
 struct StringIntern {
 struct StringIntern {
 	StringIntern *next;
 	StringIntern *next;
 	isize len;
 	isize len;

+ 2 - 13
src/llvm_backend_const.cpp

@@ -287,17 +287,6 @@ gb_internal lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type
 	return lb_const_value(m, t, tv.value);
 	return lb_const_value(m, t, tv.value);
 }
 }
 
 
-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) {
 gb_internal i32 lb_obfuscate_i32(i32 i) {
 	i32 x = cast(i32)gb_fnv64a(&i, sizeof(i));
 	i32 x = cast(i32)gb_fnv64a(&i, sizeof(i));
 	if (x < 0) {
 	if (x < 0) {
@@ -314,8 +303,8 @@ gb_internal lbValue lb_const_source_code_location_const(lbModule *m, String cons
 	i32 column = pos.column;
 	i32 column = pos.column;
 
 
 	if (build_context.obfuscate_source_code_locations) {
 	if (build_context.obfuscate_source_code_locations) {
-		file = lb_obfuscate_string(file, "F");
-		procedure = lb_obfuscate_string(procedure, "P");
+		file = obfuscate_string(file, "F");
+		procedure = obfuscate_string(procedure, "P");
 
 
 		line   = lb_obfuscate_i32(line);
 		line   = lb_obfuscate_i32(line);
 		column = lb_obfuscate_i32(column);
 		column = lb_obfuscate_i32(column);