Skip to content

Updated Analytics Windows to allow multiple DLL hashes, and add usage of Options struct. #1744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
81045a0
Update script and code to handle known hashes for multiple DLLs.
jonsimantov Jun 16, 2025
2a2bc2b
Fixed issues on Windows with multi-dll-hash code.
jonsimantov Jun 19, 2025
9171093
Fix: Link Crypt32.lib for firebase_analytics on Windows
google-labs-jules[bot] Jun 23, 2025
e75fb8c
Fix: Link Crypt32.lib for firebase_analytics_test on Windows
google-labs-jules[bot] Jun 24, 2025
d806c1f
Update DLL.
jonsimantov Jun 25, 2025
0911aec
Merge branch 'analytics-dll-secure-multihash' into fix/analytics-dll-…
jonsimantov Jun 25, 2025
867937e
Merge branch 'fix/analytics-dll-hash-sizeof' of https://github.com/fi…
jonsimantov Jun 25, 2025
60199f3
Remove unused file.
jonsimantov Jun 25, 2025
7936e99
Remove extra file.
jonsimantov Jun 25, 2025
fc5aa8e
Initialize Analytics C SDK with AppOptions on desktop
google-labs-jules[bot] Jun 25, 2025
cfa4385
Update stub generation.
jonsimantov Jun 25, 2025
ab86de9
Merge branch 'analytics_windows_dll_updated' into feat/desktop-analyt…
jonsimantov Jun 25, 2025
e3e8a08
Format code.
jonsimantov Jun 25, 2025
9a65e98
Fix build issues.
jonsimantov Jun 25, 2025
fcd9aaa
Update Analytics to use new options struct. (#1745)
jonsimantov Jun 25, 2025
421ce66
Format code.
jonsimantov Jun 25, 2025
764619b
Only display Analytics warnings if we are not in stub mode.
jonsimantov Jun 25, 2025
72affd7
Merge branch 'feat/desktop-analytics-init-options' into analytics_win…
jonsimantov Jun 25, 2025
1e23573
Format code.
jonsimantov Jun 25, 2025
377a83c
Address review comments and add missing copyright notices.
jonsimantov Jun 25, 2025
44816bb
Remove extraneous commented out code.
jonsimantov Jun 25, 2025
f5fb8d2
Add another known hash to the list.
jonsimantov Jun 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions analytics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ set_property(TARGET firebase_analytics PROPERTY FOLDER "Firebase Cpp")
# Set up the dependency on Firebase App.
target_link_libraries(firebase_analytics
PUBLIC firebase_app)
if(WIN32)
target_link_libraries(firebase_analytics PUBLIC Crypt32.lib)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be PUBLIC? Looking over other cases, we usually try to make these private, though there might be a reason this one needs to be public

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try it and see what happens!

endif()
# Public headers all refer to each other relative to the src/include directory,
# while private headers are relative to the entire C++ SDK directory.
target_include_directories(firebase_analytics
Expand Down
67 changes: 52 additions & 15 deletions analytics/generate_windows_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,36 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
initialized pointers, and a dynamic loading function for Windows.

Args:
header_file_path (str): The path to the DLL file.
dll_file_path (str): The path to the DLL file.
header_file_path (str): The path to the input C header file.
output_h_path (str): The path for the generated C header output file.
output_c_path (str): The path for the generated C source output file.
"""
print(f"Reading DLL file: {dll_file_path}")
dll_hash = hash_file(dll_file_path)
dll_hash = hash_file(dll_file_path) # This is binary

# --- Manage known hashes ---
hash_file_path = os.path.join(os.path.dirname(dll_file_path), "known_dll_hashes.txt")
known_hex_hashes = []
try:
with open(hash_file_path, 'r') as f:
for line in f:
known_hex_hashes.append(line.strip())
except FileNotFoundError:
print(f"Info: '{hash_file_path}' not found, will be created.")
pass # File doesn't exist, list remains empty

current_dll_hex_hash = dll_hash.hex()
if current_dll_hex_hash not in known_hex_hashes:
known_hex_hashes.append(current_dll_hex_hash)
# Sort for consistency, although not strictly required by the prompt
# known_hex_hashes.sort() # Decided against sorting to maintain order of addition for now

with open(hash_file_path, 'w') as f:
for hex_hash in known_hex_hashes:
f.write(hex_hash + '\n')
print(f"Updated known hashes in: {hash_file_path}")
# --- End of manage known hashes ---

print(f"Reading header file: {header_file_path}")
try:
Expand All @@ -77,7 +100,7 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
includes = re.findall(r"#include\s+<.*?>", header_content)

# Find all typedefs, including their documentation comments
typedefs = re.findall(r"/\*\*(?:[\s\S]*?)\*/\s*typedef[\s\S]*?;\s*", header_content)
typedefs = re.findall(r"(?:/\*\*(?:[\s\S]*?)\*/\s*)?typedef[\s\S]*?;\s*", header_content)

# --- Extract function prototypes ---
function_pattern = re.compile(
Expand All @@ -104,7 +127,7 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
if "void" in return_type:
return_statement = " // No return value."
elif "*" in return_type:
return_statement = f' return ({return_type})(&g_stub_memory);'
return_statement = f' return ({return_type})(&g_stub_memory[0]);'
else: # bool, int64_t, etc.
return_statement = " return 1;"

Expand Down Expand Up @@ -136,6 +159,7 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
f.write(f"// Generated from {os.path.basename(header_file_path)} by {os.path.basename(sys.argv[0])}\n\n")
f.write(f"#ifndef {header_guard}\n")
f.write(f"#define {header_guard}\n\n")
f.write(f"#define ANALYTICS_API // filter out from header copy\n\n")
f.write("#include <stdbool.h> // needed for bool type in pure C\n\n")

f.write("// --- Copied from original header ---\n")
Expand All @@ -157,8 +181,10 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
f.write("\n// --- Dynamic Loader Declaration for Windows ---\n")
f.write("#if defined(_WIN32)\n\n")
f.write('#include <windows.h>\n')
f.write(f'\n// Google Analytics Windows DLL SHA256 hash, to be verified before loading.')
f.write(f'\nextern const unsigned char FirebaseAnalytics_WindowsDllHash[{len(dll_hash)}];\n\n');
f.write('\n// Array of known Google Analytics Windows DLL SHA256 hashes (hex strings).\n')
f.write('extern const char* FirebaseAnalytics_KnownWindowsDllHashes[];\n')
f.write('// Count of known Google Analytics Windows DLL SHA256 hashes.\n')
f.write('extern const int FirebaseAnalytics_KnownWindowsDllHashCount;\n\n')
f.write('// Load Analytics functions from the given DLL handle into function pointers.\n')
f.write(f'// Returns the number of functions successfully loaded.\n')
f.write("int FirebaseAnalytics_LoadDynamicFunctions(HMODULE dll_handle);\n\n")
Expand All @@ -178,15 +204,30 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
f.write(f"// Generated from {os.path.basename(header_file_path)} by {os.path.basename(sys.argv[0])}\n\n")
f.write(f'#include "{INCLUDE_PREFIX}{os.path.basename(output_h_path)}"\n\n')
f.write('#include <stddef.h>\n\n')
f.write("static void* g_stub_memory = NULL;\n\n")
f.write("// A nice big chunk of stub memory that can be returned by stubbed Create methods.\n")
f.write("static char g_stub_memory[256] = {0};\n\n")
f.write("// clang-format off\n")
f.write(f'\n// Number of Google Analytics functions expected to be loaded from the DLL.')
f.write(f'\nconst int FirebaseAnalytics_DynamicFunctionCount = {len(function_details_for_loader)};\n\n');
# f.write("#if defined(_WIN32)\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

# f.write('// Google Analytics Windows DLL SHA256 hash, to be verified before loading.\n')
# f.write('const unsigned char FirebaseAnalytics_WindowsDllHash[] = {\n ')
# f.write(', '.join(["0x%02x" % s for s in dll_hash]))
# f.write('\n};\n')
# f.write("#endif // defined(_WIN32)\n")

f.write("#if defined(_WIN32)\n")
f.write('// Google Analytics Windows DLL SHA256 hash, to be verified before loading.\n')
f.write('const unsigned char FirebaseAnalytics_WindowsDllHash[] = {\n ')
f.write(', '.join(["0x%02x" % s for s in dll_hash]))
f.write('\n};\n')
f.write('// Array of known Google Analytics Windows DLL SHA256 hashes (hex strings).\n')
f.write('const char* FirebaseAnalytics_KnownWindowsDllHashes[] = {\n')
if known_hex_hashes:
for i, hex_hash in enumerate(known_hex_hashes):
f.write(f' "{hex_hash}"')
if i < len(known_hex_hashes) - 1:
f.write(',')
f.write('\n')
f.write('};\n\n')
f.write('// Count of known Google Analytics Windows DLL SHA256 hashes.\n')
f.write(f'const int FirebaseAnalytics_KnownWindowsDllHashCount = {len(known_hex_hashes)};\n')
f.write("#endif // defined(_WIN32)\n")
f.write("\n// --- Stub Function Definitions ---\n")
f.write("\n\n".join(stub_functions))
Expand Down Expand Up @@ -231,25 +272,21 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
parser.add_argument(
"--windows_dll",
default = os.path.join(os.path.dirname(sys.argv[0]), "windows/analytics_win.dll"),
#required=True,
help="Path to the DLL file to calculate a hash."
)
parser.add_argument(
"--windows_header",
default = os.path.join(os.path.dirname(sys.argv[0]), "windows/include/public/c/analytics.h"),
#required=True,
help="Path to the input C header file."
)
parser.add_argument(
"--output_header",
default = os.path.join(os.path.dirname(sys.argv[0]), INCLUDE_PATH, "analytics_desktop_dynamic.h"),
#required=True,
help="Path for the generated output header file."
)
parser.add_argument(
"--output_source",
default = os.path.join(os.path.dirname(sys.argv[0]), INCLUDE_PATH, "analytics_desktop_dynamic.c"),
#required=True,
help="Path for the generated output source file."
)
args = parser.parse_args()
Expand Down
Loading
Loading