Skip to content

Exception occurred 'InstrProhibited' during TLS connection initilization #99

@aljazerzen

Description

@aljazerzen

I'm trying to make a HTTPS request using embedded-tls on ESP32.

I'm running into the following error:

INFO - esp-wifi configuration EspWifiConfig { rx_queue_size: 5, tx_queue_size: 3, static_rx_buf_num: 10, dynamic_rx_buf_num: 32, static_tx_buf_num: 0, dynamic_tx_buf_num: 32, csi_enable: false, ampdu_rx_enable: true, ampdu_tx_enable: true, amsdu_tx_enable: false, rx_ba_win: 6, max_burst_size: 1, country_code: "CN", country_code_operating_class: 0, mtu: 1492, tick_rate_hz: 100, listen_interval: 3, beacon_timeout: 6, ap_beacon_timeout: 300, failure_retry_cnt: 1, scan_method: 0 }
DEBUG - Wifi mode Sta set
WARN - esp_wifi_internal_tx 12290
start connection task
Device capabilities: Ok(EnumSet(Client))
Starting wifi
DEBUG - Unhandled event: HomeChannelChange
Wifi started!
About to connect...
DEBUG - Unhandled event: HomeChannelChange
Wifi connected!
Waiting to get IP address...
Got IP: 192.168.8.149/24
INFO - HTTP connect...
WARN - Read buffer is smaller than 16640 bytes, which may cause problems!
DEBUG - start_record(Handshake(false))



Exception occurred 'InstrProhibited'
Context
PC=0xe0002c00       PS=0x00060011
A0=0x8010aa4e       A1=0x3ffd85b0       A2=0xe0002c00       A3=0x3ffd8af8       A4=0x3ffd97a0
0x3ffd85b0 - g_cnxMgr
    at ??:??
0x3ffd8af8 - g_wifi_mac_time_delta
    at ??:??
A5=0x3ffd9620       A6=0x3ffd9838       A7=0x3ffd97e8       A8=0x8008bb3b       A9=0x3ffb4080
0x3ffb4080 - net::____embassy_main_task::{{closure}}::HEAP
    at ??:??
A10=0x3ffb41e0      A11=0x4010c4ac      A12=0x3ffc54bc      A13=0x3ffb41e0      A14=0x3ffc54c0
0x3ffb41e0 - net::____embassy_main_task::{{closure}}::HEAP
    at ??:??
0x4010c4ac - _critical_section_1_0_release
    at ??:??
0x3ffc54bc - esp_wifi::preempt::CTX_NOW.0
    at ??:??
0x3ffb41e0 - net::____embassy_main_task::{{closure}}::HEAP
    at ??:??
0x3ffc54c0 - esp_wifi::preempt::CTX_NOW.0
    at ??:??
A15=0x3ffb2188
0x3ffb2188 - net::____embassy_main_task::{{closure}}::HEAP
    at ??:??
SAR=00000020
EXCCAUSE=0x00000014 EXCVADDR=0xe0002c00
LBEG=0x4000c2e0     LEND=0x4000c2f6     LCOUNT=0x00000000
THREADPTR=0x00000000
SCOMPARE1=0x00000100
BR=0x00000000
ACCLO=0x00000000    ACCHI=0x00000000
M0=0x00000000       M1=0x00000000       M2=0x00000000       M3=0x00000000
F64R_LO=0x00000000  F64R_HI=0x00000000  F64S=0x00000000
FCR=0x00000000      FSR=0x00000000
F0=0x00000000       F1=0x00000000       F2=0x00000000       F3=0x00000000       F4=0x00000000
F5=0x00000000       F6=0x00000000       F7=0x00000000       F8=0x00000000       F9=0x00000000
F10=0x00000000      F11=0x00000000      F12=0x00000000      F13=0x00000000      F14=0x00000000
F15=0x00000000
0x40080a64
esp_hal::interrupt::xtensa::vectored::handle_interrupt
    at ??:??
0x40080a33
handle_interrupts
    at ??:??
0x40080dc2
.RestoreContext
    at ??:??
0x40040001
0x400f8c45
p256::arithmetic::field::field_impl::fe_mul
    at ??:??
0x400e4ec7
<primeorder::projective::ProjectivePoint<C> as core::ops::arith::Mul<S>>::mul
    at ??:??
0x400e7a39
elliptic_curve::public_key::PublicKey<C>::from_secret_scalar
    at ??:??
0x400e1227
embedded_tls::handshake::client_hello::ClientHello<CipherSuite>::encode
    at ??:??
0x400df47b
embedded_tls::buffer::CryptoBuffer::with_u24_length
    at ??:??
0x400e73e7
embedded_tls::record::ClientRecord<CipherSuite>::encode_payload
    at ??:??

This is my code:

async fn send_http(stack: embassy_net::Stack<'_>, url: &str, rng: &mut Rng) {
    let dns_socket = embassy_net::dns::DnsSocket::new(stack);
    let tcp_state = embassy_net::tcp::client::TcpClientState::<1, 4096, 4096>::new();
    let tcp_client = embassy_net::tcp::client::TcpClient::new(stack, &tcp_state);

    let mut buf_rx = [0; 16384];
    let mut buf_tx = [0; 16384];
    let tls_config = reqwless::client::TlsConfig::new(
        generate_u64(rng),
        &mut buf_rx,
        &mut buf_tx,
        reqwless::client::TlsVerify::None,
    );

    let mut client =
        reqwless::client::HttpClient::new_with_tls(&tcp_client, &dns_socket, tls_config);

    log::info!("HTTP connect...");
    let request = client.request(reqwless::request::Method::GET, url).await;
    let request = match request {
        Ok(r) => r,
        Err(e) => {
            log::error!("Cannot connect: {e:?}");
            panic!();
        }
    };
    log::info!("HTTP connection open");

    let mut request = request.content_type(reqwless::headers::ContentType::TextPlain);

    let mut header_buf = [0; 4096];
    let response = request.send(&mut header_buf).await.unwrap();
    println!(
        "response: {:?}, content-length: {:?}",
        response.status, response.content_length
    );

    let mut body = response.body().reader();

    let mut buf = [0; 4096];
    while let Ok(bytes_read) = body.read(&mut buf).await {
        println!("{}", core::str::from_utf8(&buf[0..bytes_read]).unwrap());
        if bytes_read == 0 {
            break;
        }
    }
}

fn generate_u64(rng: &mut Rng) -> u64 {
    (rng.random() as u64) << 32 | (rng.random() as u64)
}

I'm I doing something wildly wrong, or is this actually a bug?

I'm willing to spend more time on this, investigate, open a fix PR, but I'd need help on how to even debug this.

PS: not sure if this is the correct location for this issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions