Fix getting SEGFAULT in Java with incorrect parameters (#148)

* Java bindings: Fix SEGFAULT on incorrect parameters/wrong file

* Prettify allocate KZGSettings
This commit is contained in:
Dmitrii Shmatko 2023-02-20 16:02:34 +04:00 committed by GitHub
parent 9a764de619
commit f87eee57d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 14 deletions

View File

@ -11,7 +11,10 @@ void reset_trusted_setup()
{ {
if (settings) if (settings)
{ {
free_trusted_setup(settings); if (settings->fs)
{
free_trusted_setup(settings);
}
free(settings); free(settings);
settings = NULL; settings = NULL;
} }
@ -39,6 +42,21 @@ void throw_invalid_size_exception(JNIEnv *env, const char *prefix, size_t size,
throw_c_kzg_exception(env, C_KZG_BADARGS, message); throw_c_kzg_exception(env, C_KZG_BADARGS, message);
} }
KZGSettings *allocate_settings(JNIEnv *env) {
KZGSettings *s = malloc(sizeof(KZGSettings));
if (s == NULL)
{
throw_exception(env, "Failed to allocate memory for the Trusted Setup.");
}
else
{
s->fs = NULL;
s->g1_values = NULL;
s->g2_values = NULL;
}
return s;
}
JNIEXPORT jint JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_getFieldElementsPerBlob(JNIEnv *env, jclass thisCls) JNIEXPORT jint JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_getFieldElementsPerBlob(JNIEnv *env, jclass thisCls)
{ {
return (jint)FIELD_ELEMENTS_PER_BLOB; return (jint)FIELD_ELEMENTS_PER_BLOB;
@ -52,12 +70,7 @@ JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_loadTrustedSetup__Ljav
return; return;
} }
settings = malloc(sizeof(KZGSettings)); settings = allocate_settings(env);
if (settings == NULL)
{
throw_exception(env, "Failed to allocate memory for the Trusted Setup.");
return;
}
const char *file_native = (*env)->GetStringUTFChars(env, file, 0); const char *file_native = (*env)->GetStringUTFChars(env, file, 0);
@ -92,12 +105,7 @@ JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_loadTrustedSetup___3BJ
return; return;
} }
settings = malloc(sizeof(KZGSettings)); settings = allocate_settings(env);
if (settings == NULL)
{
throw_exception(env, "Failed to allocate memory for the Trusted Setup.");
return;
}
jbyte *g1_native = (*env)->GetByteArrayElements(env, g1, NULL); jbyte *g1_native = (*env)->GetByteArrayElements(env, g1, NULL);
jbyte *g2_native = (*env)->GetByteArrayElements(env, g2, NULL); jbyte *g2_native = (*env)->GetByteArrayElements(env, g2, NULL);

View File

@ -20,7 +20,7 @@ public class CKZG4844JNITest {
private enum TrustedSetupSource { private enum TrustedSetupSource {
FILE, FILE,
PARAMETERS, PARAMETERS,
RESOURCE; RESOURCE
} }
private static final Preset PRESET; private static final Preset PRESET;
@ -239,6 +239,32 @@ public class CKZG4844JNITest {
assertExceptionIsTrustedSetupIsNotLoaded(exception); assertExceptionIsTrustedSetupIsNotLoaded(exception);
} }
@Test
public void shouldThrowExceptionOnIncorrectTrustedSetupParameters() {
final LoadTrustedSetupParameters parameters =
TestUtils.createLoadTrustedSetupParameters(TRUSTED_SETUP_FILE_BY_PRESET.get(PRESET));
final CKZGException ckzgException =
assertThrows(
CKZGException.class,
() ->
CKZG4844JNI.loadTrustedSetup(
parameters.getG1(),
parameters.getG1Count() + 1,
parameters.getG2(),
parameters.getG2Count()));
assertTrue(ckzgException.getMessage().contains("C_KZG_BADARGS"));
}
@Test
public void shouldThrowExceptionOnIncorrectTrustedSetupFromFile() {
final Preset incorrectPreset = PRESET == Preset.MAINNET ? Preset.MINIMAL : Preset.MAINNET;
final CKZGException ckzgException =
assertThrows(
CKZGException.class,
() -> CKZG4844JNI.loadTrustedSetup(TRUSTED_SETUP_FILE_BY_PRESET.get(incorrectPreset)));
assertTrue(ckzgException.getMessage().contains("C_KZG_BADARGS"));
}
private void assertExceptionIsTrustedSetupIsNotLoaded(final RuntimeException exception) { private void assertExceptionIsTrustedSetupIsNotLoaded(final RuntimeException exception) {
assertEquals("Trusted Setup is not loaded.", exception.getMessage()); assertEquals("Trusted Setup is not loaded.", exception.getMessage());
} }